class-flickr.php
293 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | /** |
| 6 | * Responsive Lightbox Remote Library Flickr class. |
| 7 | * |
| 8 | * Library: https://www.flickr.com |
| 9 | * API: https://www.flickr.com/services/developer/api/ |
| 10 | * |
| 11 | * @class Responsive_Lightbox_Remote_Library_Flickr |
| 12 | */ |
| 13 | class Responsive_Lightbox_Remote_Library_Flickr extends Responsive_Lightbox_Remote_Library_API { |
| 14 | |
| 15 | protected $allowed_hosts = [ 'staticflickr.com' ]; |
| 16 | protected $allowed_formats = [ |
| 17 | 'bmp' => 'image/bmp', |
| 18 | 'gif' => 'image/gif', |
| 19 | 'jpe' => 'image/jpeg', |
| 20 | 'jpeg' => 'image/jpeg', |
| 21 | 'jpg' => 'image/jpeg', |
| 22 | 'png' => 'image/png', |
| 23 | 'tif' => 'image/tiff', |
| 24 | 'tiff' => 'image/tiff' |
| 25 | ]; |
| 26 | |
| 27 | /** |
| 28 | * Class constructor. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | // provider slug |
| 34 | $this->slug = 'flickr'; |
| 35 | |
| 36 | // provider name |
| 37 | $this->name = __( 'Flickr', 'responsive-lightbox' ); |
| 38 | |
| 39 | // default values |
| 40 | $this->defaults = [ |
| 41 | 'active' => false, |
| 42 | 'api_key' => '' |
| 43 | ]; |
| 44 | |
| 45 | // setting fields - Settings API multi-field format |
| 46 | $this->fields = [ |
| 47 | 'active' => [ |
| 48 | 'title' => $this->name, |
| 49 | 'section' => 'responsive_lightbox_remote_library_providers', |
| 50 | 'type' => 'boolean', |
| 51 | 'label' => __( 'Enable Flickr support.', 'responsive-lightbox' ), |
| 52 | 'parent' => 'flickr' |
| 53 | ], |
| 54 | 'api_key' => [ |
| 55 | 'section' => 'responsive_lightbox_remote_library_providers', |
| 56 | 'type' => 'text', |
| 57 | 'class' => 'large-text', |
| 58 | 'placeholder' => __( 'API key', 'responsive-lightbox' ), |
| 59 | 'description' => sprintf( __( 'Provide your %s key.', 'responsive-lightbox' ), '<a href="https://www.flickr.com/services/apps/create/">Flickr API</a>' ), |
| 60 | 'parent' => 'flickr', |
| 61 | 'animation' => 'slide', |
| 62 | 'logic' => [ |
| 63 | 'field' => 'flickr_active', |
| 64 | 'operator' => 'is', |
| 65 | 'value' => 'true' |
| 66 | ] |
| 67 | ] |
| 68 | ]; |
| 69 | |
| 70 | // add provider |
| 71 | parent::add_provider( $this ); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Validate settings. |
| 78 | * |
| 79 | * @param array $input POST data |
| 80 | * @return array |
| 81 | */ |
| 82 | public function validate_settings( $input ) { |
| 83 | if ( ! isset( $input['flickr'] ) ) { |
| 84 | $input['flickr'] = $this->rl->defaults['remote_library']['flickr']; |
| 85 | } else { |
| 86 | // Support both legacy key (active) and Settings API key (flickr_active) |
| 87 | if ( ! isset( $input['flickr']['active'] ) && ! isset( $input['flickr']['flickr_active'] ) ) { |
| 88 | $input['flickr']['active'] = false; |
| 89 | } |
| 90 | |
| 91 | // api key - sanitize alphanumeric with hyphens and dots |
| 92 | if ( ! empty( $input['flickr']['api_key'] ) && is_string( $input['flickr']['api_key'] ) ) { |
| 93 | $input['flickr']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $input['flickr']['api_key'] ); |
| 94 | } else { |
| 95 | $input['flickr']['api_key'] = ''; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $input; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prepare data to run remote query. |
| 104 | * |
| 105 | * @param string $search_phrase Search phrase |
| 106 | * @param array $args Provider arguments |
| 107 | * @return void |
| 108 | */ |
| 109 | public function prepare_query( $search_phrase, $args = [] ) { |
| 110 | // check page parameter |
| 111 | if ( isset( $args['preview_page'] ) ) |
| 112 | $args['preview_page'] = (int) $args['preview_page']; |
| 113 | else |
| 114 | $args['preview_page'] = 1; |
| 115 | |
| 116 | if ( $args['preview_page'] < 1 ) |
| 117 | $args['preview_page'] = 1; |
| 118 | |
| 119 | // check limit |
| 120 | if ( isset( $args['limit'] ) && ( $limit = (int) $args['limit'] ) > 0 ) |
| 121 | $args['preview_per_page'] = $limit; |
| 122 | else { |
| 123 | // check per page parameter |
| 124 | if ( isset( $args['preview_per_page'] ) ) |
| 125 | $args['preview_per_page'] = (int) $args['preview_per_page']; |
| 126 | else |
| 127 | $args['preview_per_page'] = 20; |
| 128 | |
| 129 | if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 500 ) |
| 130 | $args['preview_per_page'] = 20; |
| 131 | } |
| 132 | |
| 133 | // set query arguments |
| 134 | $this->query_args = $args; |
| 135 | |
| 136 | $query_args = [ |
| 137 | 'api_key' => $this->rl->options['remote_library']['flickr']['api_key'], |
| 138 | 'extras' => 'owner_name,url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o,description,tags', |
| 139 | 'per_page' => $args['preview_per_page'], |
| 140 | 'page' => $args['preview_page'], |
| 141 | 'method' => 'flickr.photos.getRecent', |
| 142 | 'format' => 'json' |
| 143 | ]; |
| 144 | |
| 145 | if ( $search_phrase !== '' ) { |
| 146 | $query_args['content_type'] = 1; |
| 147 | $query_args['method'] = 'flickr.photos.search'; |
| 148 | $query_args['text'] = urlencode( $search_phrase ); |
| 149 | $query_args['sort'] = 'date-posted-desc'; |
| 150 | } |
| 151 | |
| 152 | // set query string |
| 153 | $this->query = add_query_arg( $query_args, 'https://api.flickr.com/services/rest/' ); |
| 154 | |
| 155 | // set query remote arguments |
| 156 | $this->query_remote_args = [ |
| 157 | 'timeout' => 30, |
| 158 | 'headers' => [ |
| 159 | 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version'] |
| 160 | ] |
| 161 | ]; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get images from media provider. |
| 166 | * |
| 167 | * @param mixed $response Remote response |
| 168 | * @param array $args Query arguments |
| 169 | * @return array|WP_Error |
| 170 | */ |
| 171 | public function get_query_results( $response, $args = [] ) { |
| 172 | $results = []; |
| 173 | $error = new WP_Error( 'rl_remote_library_flickr_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) ); |
| 174 | |
| 175 | // retrieve body |
| 176 | $response_body = wp_remote_retrieve_body( $response ); |
| 177 | |
| 178 | // check for flickr string |
| 179 | if ( strpos( $response_body, 'jsonFlickrApi(' ) === 0 ) |
| 180 | $response_body = substr( $response_body, 14, -1 ); |
| 181 | |
| 182 | // any data? |
| 183 | if ( $response_body !== '' ) { |
| 184 | $response_json = json_decode( $response_body, true ); |
| 185 | |
| 186 | // invalid data? |
| 187 | if ( $response_json === null || ( isset( $response_json['stat'] ) && $response_json['stat'] === 'fail' ) ) |
| 188 | $results = $error; |
| 189 | else { |
| 190 | // set response data |
| 191 | $this->response_data = $response_json; |
| 192 | |
| 193 | // get results |
| 194 | $results = isset( $response_json['photos'] ) && is_array( $response_json['photos'] ) && isset( $response_json['photos']['photo'] ) && is_array( $response_json['photos']['photo'] ) ? $response_json['photos']['photo'] : []; |
| 195 | |
| 196 | // sanitize images |
| 197 | $results = $this->sanitize_results( $results ); |
| 198 | } |
| 199 | } else |
| 200 | $results = $error; |
| 201 | |
| 202 | return $results; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sanitize single result. |
| 207 | * |
| 208 | * @param array $result Single result |
| 209 | * @return array|false |
| 210 | */ |
| 211 | public function sanitize_result( $result ) { |
| 212 | // original size exists? |
| 213 | if ( isset( $result['url_o'] ) ) |
| 214 | $large = [ $result['url_o'], $result['width_o'], $result['height_o'] ]; |
| 215 | // large 2048 size exists? |
| 216 | elseif ( isset( $result['url_k'] ) ) |
| 217 | $large = [ $result['url_k'], $result['width_k'], $result['height_k'] ]; |
| 218 | // large 1600 size exists? |
| 219 | elseif ( isset( $result['url_h'] ) ) |
| 220 | $large = [ $result['url_h'], $result['width_h'], $result['height_h'] ]; |
| 221 | // large 1024 size exists? |
| 222 | elseif ( isset( $result['url_l'] ) ) |
| 223 | $large = [ $result['url_l'], $result['width_l'], $result['height_l'] ]; |
| 224 | // medium 800 size exists? |
| 225 | elseif ( isset( $result['url_c'] ) ) |
| 226 | $large = [ $result['url_c'], $result['width_c'], $result['height_c'] ]; |
| 227 | // medium 640 size exists? |
| 228 | elseif ( isset( $result['url_z'] ) ) |
| 229 | $large = [ $result['url_z'], $result['width_z'], $result['height_z'] ]; |
| 230 | // medium 500 size exists? |
| 231 | elseif ( isset( $result['url_m'] ) ) |
| 232 | $large = [ $result['url_m'], $result['width_m'], $result['height_m'] ]; |
| 233 | // small 320 size exists? |
| 234 | elseif ( isset( $result['url_n'] ) ) |
| 235 | $large = [ $result['url_n'], $result['width_n'], $result['height_n'] ]; |
| 236 | // small 240 size exists? |
| 237 | elseif ( isset( $result['url_s'] ) ) |
| 238 | $large = [ $result['url_s'], $result['width_s'], $result['height_s'] ]; |
| 239 | // thumbnail size exists? |
| 240 | elseif ( isset( $result['url_t'] ) ) |
| 241 | $large = [ $result['url_t'], $result['width_t'], $result['height_t'] ]; |
| 242 | // skip this photo |
| 243 | else |
| 244 | return false; |
| 245 | |
| 246 | // large square size exists? |
| 247 | if ( isset( $result['url_q'] ) ) |
| 248 | $small = [ $result['url_q'], $result['width_q'], $result['height_q'] ]; |
| 249 | // medium 500 size exists? |
| 250 | elseif ( isset( $result['url_m'] ) ) |
| 251 | $small = [ $result['url_m'], $result['width_m'], $result['height_m'] ]; |
| 252 | // small 320 size exists? |
| 253 | elseif ( isset( $result['url_n'] ) ) |
| 254 | $small = [ $result['url_n'], $result['width_n'], $result['height_n'] ]; |
| 255 | // small 240 size exists? |
| 256 | elseif ( isset( $result['url_s'] ) ) |
| 257 | $small = [ $result['url_s'], $result['width_s'], $result['height_s'] ]; |
| 258 | // skip this photo |
| 259 | else |
| 260 | return false; |
| 261 | |
| 262 | $source = 'https://www.flickr.com/photos/' . $result['owner'] . '/' . $result['id']; |
| 263 | |
| 264 | $imagedata = [ |
| 265 | 'id' => 0, |
| 266 | 'link' => '', |
| 267 | 'source' => esc_url_raw( $source ), |
| 268 | 'title' => sanitize_text_field( $result['title'] ), |
| 269 | 'caption' => $this->get_attribution( 'Flickr', $source, $result['ownername'], 'https://www.flickr.com/photos/' . $result['owner'] ), |
| 270 | 'description' => ! empty( $result['description']['_content'] ) ? sanitize_text_field( $result['description']['_content'] ) : '', |
| 271 | 'alt' => sanitize_text_field( $result['tags'] ), |
| 272 | 'url' => esc_url_raw( $large[0] ), |
| 273 | 'width' => (int) $large[1], |
| 274 | 'height' => (int) $large[2], |
| 275 | 'orientation' => (int) $large[2] > (int) $large[1] ? 'portrait' : 'landscape', |
| 276 | 'thumbnail_url' => esc_url_raw( $small[0] ), |
| 277 | 'thumbnail_width' => (int) $small[1], |
| 278 | 'thumbnail_height' => (int) $small[2], |
| 279 | 'thumbnail_orientation' => (int) $small[2] > (int) $small[1] ? 'portrait' : 'landscape', |
| 280 | 'media_provider' => 'flickr', |
| 281 | 'filename' => sanitize_file_name( basename( $large[0] ) ), |
| 282 | 'dimensions' => (int) $large[1] . ' x ' . (int) $large[2], |
| 283 | 'type' => 'image' |
| 284 | ]; |
| 285 | |
| 286 | // create thumbnail link |
| 287 | $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' ); |
| 288 | |
| 289 | return $imagedata; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | new Responsive_Lightbox_Remote_Library_Flickr(); |