class-unsplash.php
242 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | /** |
| 6 | * Responsive Lightbox Remote Library Unsplash class. |
| 7 | * |
| 8 | * Library: https://unsplash.com |
| 9 | * API: https://unsplash.com/developers |
| 10 | * |
| 11 | * @class Responsive_Lightbox_Remote_Library_Unsplash |
| 12 | */ |
| 13 | class Responsive_Lightbox_Remote_Library_Unsplash extends Responsive_Lightbox_Remote_Library_API { |
| 14 | |
| 15 | protected $allowed_hosts = [ 'unsplash.com' ]; |
| 16 | protected $allowed_formats = [ |
| 17 | 'jpe' => 'image/jpeg', |
| 18 | 'jpeg' => 'image/jpeg', |
| 19 | 'jpg' => 'image/jpeg' |
| 20 | ]; |
| 21 | |
| 22 | /** |
| 23 | * Class constructor. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | // provider slug |
| 29 | $this->slug = 'unsplash'; |
| 30 | |
| 31 | // provider name |
| 32 | $this->name = __( 'Unsplash', 'responsive-lightbox' ); |
| 33 | |
| 34 | // default values |
| 35 | $this->defaults = [ |
| 36 | 'active' => false, |
| 37 | 'api_key' => '' |
| 38 | ]; |
| 39 | |
| 40 | // setting fields - Settings API multi-field format |
| 41 | $this->fields = [ |
| 42 | 'active' => [ |
| 43 | 'title' => $this->name, |
| 44 | 'section' => 'responsive_lightbox_remote_library_providers', |
| 45 | 'type' => 'boolean', |
| 46 | 'label' => __( 'Enable Unsplash support.', 'responsive-lightbox' ), |
| 47 | 'parent' => 'unsplash' |
| 48 | ], |
| 49 | 'api_key' => [ |
| 50 | 'section' => 'responsive_lightbox_remote_library_providers', |
| 51 | 'type' => 'text', |
| 52 | 'class' => 'large-text', |
| 53 | 'placeholder' => __( 'Access key', 'responsive-lightbox' ), |
| 54 | 'description' => sprintf( __( 'Provide your %s key.', 'responsive-lightbox' ), '<a href="https://unsplash.com/oauth/applications/new">Unsplash API</a>' ), |
| 55 | 'parent' => 'unsplash', |
| 56 | 'animation' => 'slide', |
| 57 | 'logic' => [ |
| 58 | 'field' => 'unsplash_active', |
| 59 | 'operator' => 'is', |
| 60 | 'value' => 'true' |
| 61 | ] |
| 62 | ] |
| 63 | ]; |
| 64 | |
| 65 | // add provider |
| 66 | parent::add_provider( $this ); |
| 67 | } |
| 68 | |
| 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( $input['unsplash'] ) ) { |
| 79 | $input['unsplash'] = $this->rl->defaults['remote_library']['unsplash']; |
| 80 | } else { |
| 81 | // Support both legacy key (active) and Settings API key (unsplash_active) |
| 82 | if ( ! isset( $input['unsplash']['active'] ) && ! isset( $input['unsplash']['unsplash_active'] ) ) { |
| 83 | $input['unsplash']['active'] = false; |
| 84 | } |
| 85 | |
| 86 | // api key - sanitize alphanumeric with hyphens and dots |
| 87 | if ( ! empty( $input['unsplash']['api_key'] ) && is_string( $input['unsplash']['api_key'] ) ) { |
| 88 | $input['unsplash']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $input['unsplash']['api_key'] ); |
| 89 | } else { |
| 90 | $input['unsplash']['api_key'] = ''; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $input; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Prepare data to run remote query. |
| 99 | * |
| 100 | * @param string $search_phrase Search phrase |
| 101 | * @param array $args Provider arguments |
| 102 | * @return void |
| 103 | */ |
| 104 | public function prepare_query( $search_phrase, $args = [] ) { |
| 105 | // check page parameter |
| 106 | if ( isset( $args['preview_page'] ) ) |
| 107 | $args['preview_page'] = (int) $args['preview_page']; |
| 108 | else |
| 109 | $args['preview_page'] = 1; |
| 110 | |
| 111 | if ( $args['preview_page'] < 1 ) |
| 112 | $args['preview_page'] = 1; |
| 113 | |
| 114 | // check limit |
| 115 | if ( isset( $args['limit'] ) && ( $limit = (int) $args['limit'] ) > 0 ) |
| 116 | $args['preview_per_page'] = $limit; |
| 117 | else { |
| 118 | // check per page parameter |
| 119 | if ( isset( $args['preview_per_page'] ) ) |
| 120 | $args['preview_per_page'] = (int) $args['preview_per_page']; |
| 121 | else |
| 122 | $args['preview_per_page'] = 20; |
| 123 | |
| 124 | if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 30 ) |
| 125 | $args['preview_per_page'] = 20; |
| 126 | } |
| 127 | |
| 128 | // set query arguments |
| 129 | $this->query_args = $args; |
| 130 | |
| 131 | $query_args = [ |
| 132 | 'per_page' => $args['preview_per_page'], |
| 133 | 'page' => $args['preview_page'], |
| 134 | 'order_by' => 'latest' |
| 135 | ]; |
| 136 | |
| 137 | if ( $search_phrase !== '' ) { |
| 138 | $query_args['query'] = urlencode( $search_phrase ); |
| 139 | |
| 140 | $url = 'https://api.unsplash.com/search/photos'; |
| 141 | } else |
| 142 | $url = 'https://api.unsplash.com/photos'; |
| 143 | |
| 144 | // set query string |
| 145 | $this->query = add_query_arg( $query_args, $url ); |
| 146 | |
| 147 | // set query remote arguments |
| 148 | $this->query_remote_args = [ |
| 149 | 'timeout' => 30, |
| 150 | 'headers' => [ |
| 151 | 'Authorization' => 'Client-ID ' . $this->rl->options['remote_library']['unsplash']['api_key'], |
| 152 | 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version'] |
| 153 | ] |
| 154 | ]; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get images from media provider. |
| 159 | * |
| 160 | * @param mixed $response Remote response |
| 161 | * @param array $args Query arguments |
| 162 | * @return array|WP_Error |
| 163 | */ |
| 164 | public function get_query_results( $response, $args = [] ) { |
| 165 | $results = []; |
| 166 | $error = new WP_Error( 'rl_remote_library_unsplash_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) ); |
| 167 | |
| 168 | // retrieve body |
| 169 | $response_body = wp_remote_retrieve_body( $response ); |
| 170 | |
| 171 | // any data? |
| 172 | if ( $response_body !== '' ) { |
| 173 | $response_json = json_decode( $response_body, true ); |
| 174 | |
| 175 | // invalid data? |
| 176 | if ( $response_json === null ) |
| 177 | $results = $error; |
| 178 | else { |
| 179 | // set response data |
| 180 | $this->response_data = $response_json; |
| 181 | |
| 182 | // search phrase query? |
| 183 | if ( $args['media_search'] !== '' ) { |
| 184 | // get results |
| 185 | $results = isset( $response_json['results'] ) && is_array( $response_json['results'] ) ? $response_json['results'] : []; |
| 186 | |
| 187 | // sanitize images |
| 188 | $results = $this->sanitize_results( $results ); |
| 189 | } else |
| 190 | $results = $this->sanitize_results( $response_json ); |
| 191 | } |
| 192 | } else |
| 193 | $results = $error; |
| 194 | |
| 195 | return $results; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Sanitize single result. |
| 200 | * |
| 201 | * @param array $result Single result |
| 202 | * @return array |
| 203 | */ |
| 204 | public function sanitize_result( $result ) { |
| 205 | // set dimensions |
| 206 | $width = (int) $result['width']; |
| 207 | $height = (int) $result['height']; |
| 208 | $thumbnail_width = 200; |
| 209 | |
| 210 | // calculate new height based on original ratio |
| 211 | $thumbnail_height = (int) floor( $thumbnail_width / ( $width / $height ) ); |
| 212 | |
| 213 | $imagedata = [ |
| 214 | 'id' => 0, |
| 215 | 'link' => '', |
| 216 | 'source' => esc_url_raw( $result['links']['html'] ), |
| 217 | 'title' => sanitize_text_field( $result['id'] ), |
| 218 | 'caption' => $this->get_attribution( 'Unsplash', $result['links']['html'], $result['user']['name'], $result['user']['links']['html'] ), |
| 219 | 'description' => ! empty( $result['description'] ) ? sanitize_text_field( $result['description'] ) : '', |
| 220 | 'alt' => '', |
| 221 | 'url' => esc_url_raw( $result['urls']['raw'] ), |
| 222 | 'width' => $width, |
| 223 | 'height' => $height, |
| 224 | 'orientation' => $height > $width ? 'portrait' : 'landscape', |
| 225 | 'thumbnail_url' => esc_url_raw( $result['urls']['small'] ), |
| 226 | 'thumbnail_width' => $thumbnail_width, |
| 227 | 'thumbnail_height' => $thumbnail_height, |
| 228 | 'thumbnail_orientation' => $thumbnail_height > $thumbnail_width ? 'portrait' : 'landscape', |
| 229 | 'media_provider' => 'unsplash', |
| 230 | 'filename' => sanitize_file_name( basename( $result['urls']['raw'] ) ), |
| 231 | 'dimensions' => $width . ' x ' . $height, |
| 232 | 'type' => 'image' |
| 233 | ]; |
| 234 | |
| 235 | // create thumbnail link |
| 236 | $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' ); |
| 237 | |
| 238 | return $imagedata; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | new Responsive_Lightbox_Remote_Library_Unsplash(); |