Skip to content

Instantly share code, notes, and snippets.

@ChrisHardie
Created February 18, 2020 16:30
Show Gist options
  • Save ChrisHardie/892ba8fe3d4e3ab69b7cb7f45f11c83e to your computer and use it in GitHub Desktop.
Save ChrisHardie/892ba8fe3d4e3ab69b7cb7f45f11c83e to your computer and use it in GitHub Desktop.
Flickr-to-WordPress: a class defining a REST API endpoint to determine the local WordPress equivalent of an imported Flickr photo
<?php
/**
* Class JCHPhotos_Plugin_API_Routes
*
* Handles API endpoints for looking up Flickr-to-WordPres translation stuff.
*/
class JCHPhotos_Plugin_API_Routes {
protected $flickr_username = 'yourflickrusernamehere';
public function __construct() {
add_action( 'rest_api_init', array( $this, 'jchphotos_register_rest_routes' ) );
}
/**
* Register some REST API routes
*/
public function jchphotos_register_rest_routes() {
// Looks like: /wp-json/myphotos/v1/find-by-flickr-url/?flickr-url=https://www.flickr.com/...
register_rest_route(
'myphotos/v1',
'/find-by-flickr-url/',
array(
'methods' => 'GET',
'callback' => array( $this, 'jchphotos_find_by_flickr_url' ),
'args' => array(
'flickr-url' => array(
'required' => true,
'validate_callback' => array( $this, 'validate_flickr_url' ),
),
'width' => array(
'sanitize_callback' => static function( $param ) {
return (int) $param;
},
),
'height' => array(
'sanitize_callback' => static function( $param ) {
return (int) $param;
},
),
),
)
);
}
/**
* Get the local photo post and attachment information, given a Flickr URL and optional width/height.
* @param $data
*/
public function jchphotos_find_by_flickr_url( $data ) {
// This is validated by the REST API route, so doing just basic sanitizing here.
$flickr_url = esc_url_raw( $data['flickr-url'], array( 'https', 'http' ) );
// Query WordPress for a matching photo post.
// TODO we may need to loosen up the comparison here
$matching_photos = get_posts(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => '_flickr_photopage',
'value' => $flickr_url,
'compare' => '=',
),
),
)
);
$response = array();
// If we got some kind of valid result back, use it.
if ( ! is_wp_error( $matching_photos ) ) {
// If the result wasn't empty,
if ( 0 < count( $matching_photos ) ) {
// Use the first result, since we only asked for one.
$matching_photo = $matching_photos[0];
// Prep an API response
$response = array(
'result' => 'found',
'post_id' => $matching_photo->ID,
'permalink' => get_permalink( $matching_photo ),
);
} else {
$response['result'] = 'notfound';
}
// If we have a featured image / thumbnail, use it.
$thumbnail_id = get_post_thumbnail_id( $matching_photo );
if ( $thumbnail_id ) {
$response['thumbnail_id'] = (int) $thumbnail_id;
// If we received a target width and height, then try to get the right intermediate image size.
if ( $data['width'] && $data['height'] ) {
$width_for_passing = (int) $data['width'];
$height_for_passing = (int) $data['height'];
// wp_get_attachment_image_src does a thing where if you pass it a width and height,
// and they aren't in the right proportions of the image itself, it'll return the full
// size version. By only sending the max, it should come back with the appropriate next size up.
( max( $width_for_passing, $height_for_passing ) === $height_for_passing ) ? $width_for_passing = 0 : $height_for_passing = 0;
$thumbnail_url_result = wp_get_attachment_image_src( $thumbnail_id, array( $width_for_passing, $height_for_passing ) );
} else {
// Otherwise, use the full size image (often scaled from original)
$thumbnail_url_result = wp_get_attachment_image_src( $thumbnail_id, 'full' );
}
$response['thumbnail_url'] = esc_url( $thumbnail_url_result[0] );
// Despite the documentation, the returned width/height are only the appropriate display dimensions given
// the input/desired size, not the actual size of the image URL being returned. This is fine.
$response['thumbnail_width'] = (int) $thumbnail_url_result[1];
$response['thumbnail_height'] = (int) $thumbnail_url_result[2];
}
} else {
$response['result'] = 'error';
}
wp_send_json( $response );
}
/**
* Check that the passed Flickr URL is valid.
* @param string $flickr_url
* @return bool
*/
public function validate_flickr_url( $flickr_url = null ) {
return ( 1 === preg_match( '^https?://(www.)?flickr.com/photos/' . $this->flickr_username . '/\d+/?.*$^', $flickr_url ) );
}
}
new JCHPhotos_Plugin_API_Routes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment