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