PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.3.1
Responsive Lightbox & Gallery v2.3.1
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 5 years ago class-unsplash.php 5 years ago class-wikimedia.php 5 years ago
class-flickr.php
250 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) )
3 exit;
4
5 /**
6 * Responsive Lightbox Remote Library Flickr class.
7 *
8 * @class Responsive_Lightbox_Remote_Library_Flickr
9 */
10 class Responsive_Lightbox_Remote_Library_Flickr extends Responsive_Lightbox_Remote_Library_API {
11
12 /**
13 * Constructor.
14 *
15 * @return void
16 */
17 public function __construct() {
18 // provider slug
19 $this->slug = 'flickr';
20
21 // provider name
22 $this->name = __( 'Flickr', 'responsive-lightbox' );
23
24 // default values
25 $this->defaults = array(
26 'active' => false,
27 'api_key' => ''
28 );
29
30 // setting fields
31 $this->fields = array(
32 'title' => $this->name,
33 'section' => 'responsive_lightbox_remote_library_providers',
34 'type' => 'custom',
35 'callback' => array( $this, 'render_field' )
36 );
37
38 // add provider
39 parent::add_provider( $this );
40 }
41
42 /**
43 * Render field.
44 *
45 * @return void
46 */
47 public function render_field() {
48 echo '
49 <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 ) . ' />' . __( 'Enable Flickr.', 'responsive-lightbox' ) . '</label></p>
50 <div class="rl-media-provider-options"' . ( $this->rl->options['remote_library']['flickr']['active'] ? '' : ' style="display: none;"' ) . '>
51 <p><input id="rl_flickr_api_key" class="large-text" placeholder="' . __( 'API key', 'responsive-lightbox' ) . '" type="text" value="' . $this->rl->options['remote_library']['flickr']['api_key'] . '" name="responsive_lightbox_remote_library[flickr][api_key]"></p>
52 <p class="description">' . sprintf( __( 'Provide your <a href="%s">Flickr API key</a>.', 'responsive-lightbox' ), 'https://www.flickr.com/services/apps/create/' ) . '</p>
53 </div>';
54 }
55
56 /**
57 * Validate settings.
58 *
59 * @param array $input POST data
60 * @return array Validated settings
61 */
62 public function validate_settings( $input ) {
63 if ( ! isset( $_POST['responsive_lightbox_remote_library'] ) )
64 $input['flickr'] = $this->rl->defaults['remote_library']['flickr'];
65 else {
66 // active
67 $input['flickr']['active'] = isset( $_POST['responsive_lightbox_remote_library']['flickr']['active'] );
68
69 // api key
70 $input['flickr']['api_key'] = preg_replace( '/[^0-9a-zA-Z\-.]/', '', $_POST['responsive_lightbox_remote_library']['flickr']['api_key'] );
71 }
72
73 return $input;
74 }
75
76 /**
77 * Prepare data to run remote query.
78 *
79 * @param string $search_phrase Search phrase
80 * @param array $args Provider arguments
81 * @return void
82 */
83 public function prepare_query( $search_phrase, $args = array() ) {
84 // check page parameter
85 if ( isset( $args['preview_page'] ) )
86 $args['preview_page'] = (int) $args['preview_page'];
87 else
88 $args['preview_page'] = 1;
89
90 if ( $args['preview_page'] < 1 )
91 $args['preview_page'] = 1;
92
93 // check per page parameter
94 if ( isset( $args['preview_per_page'] ) )
95 $args['preview_per_page'] = (int) $args['preview_per_page'];
96 else
97 $args['preview_per_page'] = 20;
98
99 if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 500 )
100 $args['preview_per_page'] = 20;
101
102 // set query arguments
103 $this->query_args = $args;
104
105 $query_args = array(
106 'api_key' => $this->rl->options['remote_library']['flickr']['api_key'],
107 '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',
108 'per_page' => $args['preview_per_page'],
109 'page' => $args['preview_page'],
110 'method' => 'flickr.photos.getRecent',
111 'format' => 'json'
112 );
113
114 if ( $search_phrase !== '' ) {
115 $query_args['content_type'] = 1;
116 $query_args['method'] = 'flickr.photos.search';
117 $query_args['text'] = urlencode( $search_phrase );
118 $query_args['sort'] = 'date-posted-desc';
119 }
120
121 // set query string
122 $this->query = add_query_arg( $query_args, 'https://api.flickr.com/services/rest/' );
123
124 // set query remote arguments
125 $this->query_remote_args = array(
126 'timeout' => 30,
127 'headers' => array(
128 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
129 )
130 );
131 }
132
133 /**
134 * Get images from media provider.
135 *
136 * @param mixed $response Remote response
137 * @param array $args Query arguments
138 * @return array Valid images or WP_Error
139 */
140 public function get_query_results( $response, $args = array() ) {
141 $results = array();
142 $error = new WP_Error( 'rl_remote_library_flickr_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
143
144 // retrieve body
145 $response_body = wp_remote_retrieve_body( $response );
146
147 // check for flickr string
148 if ( strpos( $response_body, 'jsonFlickrApi(' ) === 0 )
149 $response_body = substr( $response_body, 14, -1 );
150
151 // any data?
152 if ( $response_body !== '' ) {
153 $response_json = json_decode( $response_body, true );
154
155 // invalid data?
156 if ( $response_json === null || ( isset( $response_json['stat'] ) && $response_json['stat'] === 'fail' ) )
157 $results = $error;
158 else {
159 // get results
160 $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'] : array();
161
162 // sanitize results
163 $results = $this->sanitize_results( $results );
164 }
165 } else
166 $results = $error;
167
168 return $results;
169 }
170
171 /**
172 * Sanitize single result.
173 *
174 * @param array $result Single result
175 * @return mixed Array on success, otherwise false
176 */
177 public function sanitize_result( $result ) {
178 // original size exists?
179 if ( isset( $result['url_o'] ) )
180 $large = array( $result['url_o'], $result['width_o'], $result['height_o'] );
181 // large 2048 size exists?
182 elseif ( isset( $result['url_k'] ) )
183 $large = array( $result['url_k'], $result['width_k'], $result['height_k'] );
184 // large 1600 size exists?
185 elseif ( isset( $result['url_h'] ) )
186 $large = array( $result['url_h'], $result['width_h'], $result['height_h'] );
187 // large 1024 size exists?
188 elseif ( isset( $result['url_l'] ) )
189 $large = array( $result['url_l'], $result['width_l'], $result['height_l'] );
190 // medium 800 size exists?
191 elseif ( isset( $result['url_c'] ) )
192 $large = array( $result['url_c'], $result['width_c'], $result['height_c'] );
193 // medium 640 size exists?
194 elseif ( isset( $result['url_z'] ) )
195 $large = array( $result['url_z'], $result['width_z'], $result['height_z'] );
196 // medium 500 size exists?
197 elseif ( isset( $result['url_m'] ) )
198 $large = array( $result['url_m'], $result['width_m'], $result['height_m'] );
199 // small 320 size exists?
200 elseif ( isset( $result['url_n'] ) )
201 $large = array( $result['url_n'], $result['width_n'], $result['height_n'] );
202 // small 240 size exists?
203 elseif ( isset( $result['url_s'] ) )
204 $large = array( $result['url_s'], $result['width_s'], $result['height_s'] );
205 // thumbnail size exists?
206 elseif ( isset( $result['url_t'] ) )
207 $large = array( $result['url_t'], $result['width_t'], $result['height_t'] );
208 // skip this photo
209 else
210 return false;
211
212 // large square size exists?
213 if ( isset( $result['url_q'] ) )
214 $small = array( $result['url_q'], $result['width_q'], $result['height_q'] );
215 // square size exists?
216 elseif ( isset( $result['url_sq'] ) )
217 $small = array( $result['url_sq'], $result['width_sq'], $result['height_sq'] );
218 // skip this photo
219 else
220 return false;
221
222 $source = 'https://www.flickr.com/photos/' . $result['owner'] . '/' . $result['id'];
223
224 $imagedata = array(
225 'id' => 0,
226 'link' => '',
227 'source' => $source,
228 'title' => $result['title'],
229 'caption' => $this->get_attribution( 'Flickr', $source, $result['ownername'], 'https://www.flickr.com/photos/' . $result['owner'] ),
230 'description' => $result['description']['_content'],
231 'alt' => $result['tags'],
232 'url' => $large[0],
233 'width' => $large[1],
234 'height' => $large[2],
235 'thumbnail_url' => $small[0],
236 'thumbnail_width' => $small[1],
237 'thumbnail_height' => $small[2],
238 'media_provider' => 'flickr',
239 'filename' => basename( $large[0] ),
240 'dimensions' => $large[1] . ' x ' . $large[2]
241 );
242
243 // thumbnail link does not exist?
244 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
245
246 return $imagedata;
247 }
248 }
249
250 new Responsive_Lightbox_Remote_Library_Flickr();