PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.6.0
Responsive Lightbox & Gallery v2.6.0
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / providers / class-unsplash.php
responsive-lightbox / includes / providers Last commit date
class-flickr.php 1 year ago class-unsplash.php 1 year ago class-wikimedia.php 1 year ago
class-unsplash.php
234 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
41 $this->fields = [
42 'title' => $this->name,
43 'section' => 'responsive_lightbox_remote_library_providers',
44 'type' => 'custom',
45 'callback' => [ $this, 'render_field' ]
46 ];
47
48 // add provider
49 parent::add_provider( $this );
50 }
51
52 /**
53 * Render field.
54 *
55 * @return string
56 */
57 public function render_field() {
58 return '
59 <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>
60 <div class="rl-media-provider-options"' . ( $this->rl->options['remote_library']['unsplash']['active'] ? '' : ' style="display: none;"' ) . '>
61 <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>
62 <p class="description">' . sprintf( esc_html__( 'Provide your %s key.', 'responsive-lightbox' ), '<a href="https://unsplash.com/oauth/applications/new">Unsplash API</a>' ) . '</p>
63 </div>';
64 }
65
66 /**
67 * Validate settings.
68 *
69 * @param array $input POST data
70 * @return array
71 */
72 public function validate_settings( $input ) {
73 if ( ! isset( $_POST['responsive_lightbox_remote_library'] ) )
74 $input['unsplash'] = $this->rl->defaults['remote_library']['unsplash'];
75 else {
76 // active
77 $input['unsplash']['active'] = isset( $_POST['responsive_lightbox_remote_library']['unsplash']['active'] );
78
79 // api key
80 if ( ! empty( $_POST['responsive_lightbox_remote_library']['unsplash']['api_key'] ) && is_string( $_POST['responsive_lightbox_remote_library']['unsplash']['api_key'] ) )
81 $input['unsplash']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $_POST['responsive_lightbox_remote_library']['unsplash']['api_key'] );
82 else
83 $input['unsplash']['api_key'] = '';
84 }
85
86 return $input;
87 }
88
89 /**
90 * Prepare data to run remote query.
91 *
92 * @param string $search_phrase Search phrase
93 * @param array $args Provider arguments
94 * @return void
95 */
96 public function prepare_query( $search_phrase, $args = [] ) {
97 // check page parameter
98 if ( isset( $args['preview_page'] ) )
99 $args['preview_page'] = (int) $args['preview_page'];
100 else
101 $args['preview_page'] = 1;
102
103 if ( $args['preview_page'] < 1 )
104 $args['preview_page'] = 1;
105
106 // check limit
107 if ( isset( $args['limit'] ) && ( $limit = (int) $args['limit'] ) > 0 )
108 $args['preview_per_page'] = $limit;
109 else {
110 // check per page parameter
111 if ( isset( $args['preview_per_page'] ) )
112 $args['preview_per_page'] = (int) $args['preview_per_page'];
113 else
114 $args['preview_per_page'] = 20;
115
116 if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 30 )
117 $args['preview_per_page'] = 20;
118 }
119
120 // set query arguments
121 $this->query_args = $args;
122
123 $query_args = [
124 'per_page' => $args['preview_per_page'],
125 'page' => $args['preview_page'],
126 'order_by' => 'latest'
127 ];
128
129 if ( $search_phrase !== '' ) {
130 $query_args['query'] = urlencode( $search_phrase );
131
132 $url = 'https://api.unsplash.com/search/photos';
133 } else
134 $url = 'https://api.unsplash.com/photos';
135
136 // set query string
137 $this->query = add_query_arg( $query_args, $url );
138
139 // set query remote arguments
140 $this->query_remote_args = [
141 'timeout' => 30,
142 'headers' => [
143 'Authorization' => 'Client-ID ' . $this->rl->options['remote_library']['unsplash']['api_key'],
144 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
145 ]
146 ];
147 }
148
149 /**
150 * Get images from media provider.
151 *
152 * @param mixed $response Remote response
153 * @param array $args Query arguments
154 * @return array|WP_Error
155 */
156 public function get_query_results( $response, $args = [] ) {
157 $results = [];
158 $error = new WP_Error( 'rl_remote_library_unsplash_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
159
160 // retrieve body
161 $response_body = wp_remote_retrieve_body( $response );
162
163 // any data?
164 if ( $response_body !== '' ) {
165 $response_json = json_decode( $response_body, true );
166
167 // invalid data?
168 if ( $response_json === null )
169 $results = $error;
170 else {
171 // set response data
172 $this->response_data = $response_json;
173
174 // search phrase query?
175 if ( $args['media_search'] !== '' ) {
176 // get results
177 $results = isset( $response_json['results'] ) && is_array( $response_json['results'] ) ? $response_json['results'] : [];
178
179 // sanitize images
180 $results = $this->sanitize_results( $results );
181 } else
182 $results = $this->sanitize_results( $response_json );
183 }
184 } else
185 $results = $error;
186
187 return $results;
188 }
189
190 /**
191 * Sanitize single result.
192 *
193 * @param array $result Single result
194 * @return array
195 */
196 public function sanitize_result( $result ) {
197 // set dimensions
198 $width = (int) $result['width'];
199 $height = (int) $result['height'];
200 $thumbnail_width = 200;
201
202 // calculate new height based on original ratio
203 $thumbnail_height = (int) floor( $thumbnail_width / ( $width / $height ) );
204
205 $imagedata = [
206 'id' => 0,
207 'link' => '',
208 'source' => esc_url_raw( $result['links']['html'] ),
209 'title' => sanitize_text_field( $result['id'] ),
210 'caption' => $this->get_attribution( 'Unsplash', $result['links']['html'], $result['user']['name'], $result['user']['links']['html'] ),
211 'description' => ! empty( $result['description'] ) ? sanitize_text_field( $result['description'] ) : '',
212 'alt' => '',
213 'url' => esc_url_raw( $result['urls']['raw'] ),
214 'width' => $width,
215 'height' => $height,
216 'orientation' => $height > $width ? 'portrait' : 'landscape',
217 'thumbnail_url' => esc_url_raw( $result['urls']['small'] ),
218 'thumbnail_width' => $thumbnail_width,
219 'thumbnail_height' => $thumbnail_height,
220 'thumbnail_orientation' => $thumbnail_height > $thumbnail_width ? 'portrait' : 'landscape',
221 'media_provider' => 'unsplash',
222 'filename' => sanitize_file_name( basename( $result['urls']['raw'] ) ),
223 'dimensions' => $width . ' x ' . $height,
224 'type' => 'image'
225 ];
226
227 // create thumbnail link
228 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
229
230 return $imagedata;
231 }
232 }
233
234 new Responsive_Lightbox_Remote_Library_Unsplash();