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