PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.4.7
Responsive Lightbox & Gallery v2.4.7
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-flickr.php
responsive-lightbox / includes / providers Last commit date
class-flickr.php 2 years ago class-unsplash.php 2 years ago class-wikimedia.php 2 years ago
class-flickr.php
273 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 /**
16 * Class constructor.
17 *
18 * @return void
19 */
20 public function __construct() {
21 // provider slug
22 $this->slug = 'flickr';
23
24 // provider name
25 $this->name = __( 'Flickr', '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_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>
53 <div class="rl-media-provider-options"' . ( $this->rl->options['remote_library']['flickr']['active'] ? '' : ' style="display: none;"' ) . '>
54 <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>
55 <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>
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['flickr'] = $this->rl->defaults['remote_library']['flickr'];
68 else {
69 // active
70 $input['flickr']['active'] = isset( $_POST['responsive_lightbox_remote_library']['flickr']['active'] );
71
72 // api key
73 if ( ! empty( $_POST['responsive_lightbox_remote_library']['flickr']['api_key'] ) && is_string( $_POST['responsive_lightbox_remote_library']['flickr']['api_key'] ) )
74 $input['flickr']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $_POST['responsive_lightbox_remote_library']['flickr']['api_key'] );
75 else
76 $input['flickr']['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'] > 500 )
110 $args['preview_per_page'] = 20;
111 }
112
113 // set query arguments
114 $this->query_args = $args;
115
116 $query_args = [
117 'api_key' => $this->rl->options['remote_library']['flickr']['api_key'],
118 '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',
119 'per_page' => $args['preview_per_page'],
120 'page' => $args['preview_page'],
121 'method' => 'flickr.photos.getRecent',
122 'format' => 'json'
123 ];
124
125 if ( $search_phrase !== '' ) {
126 $query_args['content_type'] = 1;
127 $query_args['method'] = 'flickr.photos.search';
128 $query_args['text'] = urlencode( $search_phrase );
129 $query_args['sort'] = 'date-posted-desc';
130 }
131
132 // set query string
133 $this->query = add_query_arg( $query_args, 'https://api.flickr.com/services/rest/' );
134
135 // set query remote arguments
136 $this->query_remote_args = [
137 'timeout' => 30,
138 'headers' => [
139 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
140 ]
141 ];
142 }
143
144 /**
145 * Get images from media provider.
146 *
147 * @param mixed $response Remote response
148 * @param array $args Query arguments
149 * @return array|WP_Error
150 */
151 public function get_query_results( $response, $args = [] ) {
152 $results = [];
153 $error = new WP_Error( 'rl_remote_library_flickr_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
154
155 // retrieve body
156 $response_body = wp_remote_retrieve_body( $response );
157
158 // check for flickr string
159 if ( strpos( $response_body, 'jsonFlickrApi(' ) === 0 )
160 $response_body = substr( $response_body, 14, -1 );
161
162 // any data?
163 if ( $response_body !== '' ) {
164 $response_json = json_decode( $response_body, true );
165
166 // invalid data?
167 if ( $response_json === null || ( isset( $response_json['stat'] ) && $response_json['stat'] === 'fail' ) )
168 $results = $error;
169 else {
170 // set response data
171 $this->response_data = $response_json;
172
173 // get results
174 $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'] : [];
175
176 // sanitize images
177 $results = $this->sanitize_results( $results );
178 }
179 } else
180 $results = $error;
181
182 return $results;
183 }
184
185 /**
186 * Sanitize single result.
187 *
188 * @param array $result Single result
189 * @return array|false
190 */
191 public function sanitize_result( $result ) {
192 // original size exists?
193 if ( isset( $result['url_o'] ) )
194 $large = [ $result['url_o'], $result['width_o'], $result['height_o'] ];
195 // large 2048 size exists?
196 elseif ( isset( $result['url_k'] ) )
197 $large = [ $result['url_k'], $result['width_k'], $result['height_k'] ];
198 // large 1600 size exists?
199 elseif ( isset( $result['url_h'] ) )
200 $large = [ $result['url_h'], $result['width_h'], $result['height_h'] ];
201 // large 1024 size exists?
202 elseif ( isset( $result['url_l'] ) )
203 $large = [ $result['url_l'], $result['width_l'], $result['height_l'] ];
204 // medium 800 size exists?
205 elseif ( isset( $result['url_c'] ) )
206 $large = [ $result['url_c'], $result['width_c'], $result['height_c'] ];
207 // medium 640 size exists?
208 elseif ( isset( $result['url_z'] ) )
209 $large = [ $result['url_z'], $result['width_z'], $result['height_z'] ];
210 // medium 500 size exists?
211 elseif ( isset( $result['url_m'] ) )
212 $large = [ $result['url_m'], $result['width_m'], $result['height_m'] ];
213 // small 320 size exists?
214 elseif ( isset( $result['url_n'] ) )
215 $large = [ $result['url_n'], $result['width_n'], $result['height_n'] ];
216 // small 240 size exists?
217 elseif ( isset( $result['url_s'] ) )
218 $large = [ $result['url_s'], $result['width_s'], $result['height_s'] ];
219 // thumbnail size exists?
220 elseif ( isset( $result['url_t'] ) )
221 $large = [ $result['url_t'], $result['width_t'], $result['height_t'] ];
222 // skip this photo
223 else
224 return false;
225
226 // large square size exists?
227 if ( isset( $result['url_q'] ) )
228 $small = [ $result['url_q'], $result['width_q'], $result['height_q'] ];
229 // medium 500 size exists?
230 elseif ( isset( $result['url_m'] ) )
231 $small = [ $result['url_m'], $result['width_m'], $result['height_m'] ];
232 // small 320 size exists?
233 elseif ( isset( $result['url_n'] ) )
234 $small = [ $result['url_n'], $result['width_n'], $result['height_n'] ];
235 // small 240 size exists?
236 elseif ( isset( $result['url_s'] ) )
237 $small = [ $result['url_s'], $result['width_s'], $result['height_s'] ];
238 // skip this photo
239 else
240 return false;
241
242 $source = 'https://www.flickr.com/photos/' . $result['owner'] . '/' . $result['id'];
243
244 $imagedata = [
245 'id' => 0,
246 'link' => '',
247 'source' => esc_url_raw( $source ),
248 'title' => sanitize_text_field( $result['title'] ),
249 'caption' => $this->get_attribution( 'Flickr', $source, $result['ownername'], 'https://www.flickr.com/photos/' . $result['owner'] ),
250 'description' => ! empty( $result['description']['_content'] ) ? sanitize_text_field( $result['description']['_content'] ) : '',
251 'alt' => sanitize_text_field( $result['tags'] ),
252 'url' => esc_url_raw( $large[0] ),
253 'width' => (int) $large[1],
254 'height' => (int) $large[2],
255 'orientation' => (int) $large[2] > (int) $large[1] ? 'portrait' : 'landscape',
256 'thumbnail_url' => esc_url_raw( $small[0] ),
257 'thumbnail_width' => (int) $small[1],
258 'thumbnail_height' => (int) $small[2],
259 'thumbnail_orientation' => (int) $small[2] > (int) $small[1] ? 'portrait' : 'landscape',
260 'media_provider' => 'flickr',
261 'filename' => basename( sanitize_file_name( $large[0] ) ),
262 'dimensions' => (int) $large[1] . ' x ' . (int) $large[2],
263 'type' => 'image'
264 ];
265
266 // create thumbnail link
267 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
268
269 return $imagedata;
270 }
271 }
272
273 new Responsive_Lightbox_Remote_Library_Flickr();