-
Notifications
You must be signed in to change notification settings - Fork 0
/
rede.php
51 lines (45 loc) · 1.48 KB
/
rede.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Plugin Name: Remote EXIF Data Endpoint
* Plugin URI: https://github.com/emrikol/rede
* Description: Creates an endpoint to return EXIF data of remote images.
* Version: 1.0.0
* Author: Derrick Tennant
* Author URI: https://emrikol.com/
* License: GPL3
* GitHub Plugin URI: https://github.com/emrikol/rede/
*/
function rede_register_route() {
register_rest_route( 'exif-data/v1', '/read/', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'rede_read_data',
'args' => array(
'url' => array(
'sanitize_callback' => 'rede_sanitize_url',
'default' => false,
),
),
) );
}
add_action( 'rest_api_init', 'rede_register_route' );
function rede_sanitize_url( $url ) {
return esc_url_raw( rawurldecode( $url ) );
}
function rede_read_data( $request ) {
$url = $request['url'];
$cache_key = md5( 'exif-json:' . $url );
$exif = get_transient( $cache_key );
if ( false === $exif ) {
if ( is_callable( 'exif_read_data' ) ) {
$raw_exif = @exif_read_data( $url ); // On bad data, exif_read_data() will throw a warning, suppress. @codingStandardsIgnoreLine.
if ( false === $raw_exif ) {
return rest_ensure_response( array( 'success' => false, 'data' => 'EXIF Not Found for image' ) );
}
$exif = $raw_exif;
set_transient( $cache_key, $exif );
} else {
return rest_ensure_response( array( 'success' => false, 'data' => 'exif_read_data function not found!' ) );
}
}
return rest_ensure_response( array( 'success' => true, 'data' => $exif ) );
}