PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.7.2
Responsive Lightbox & Gallery v2.7.2
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-wikimedia.php
responsive-lightbox / includes / providers Last commit date
class-flickr.php 4 months ago class-unsplash.php 4 months ago class-wikimedia.php 4 months ago
class-wikimedia.php
282 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) )
3 exit;
4
5 /**
6 * Responsive Lightbox Remote Library Wikimedia class.
7 *
8 * Library: https://commons.wikimedia.org
9 * API: https://commons.wikimedia.org/w/api.php?action=help&modules=query%2Ballimages
10 *
11 * @class Responsive_Lightbox_Remote_Library_Wikimedia
12 */
13 class Responsive_Lightbox_Remote_Library_Wikimedia extends Responsive_Lightbox_Remote_Library_API {
14
15 protected $allowed_hosts = [ 'wikimedia.org' ];
16 protected $allowed_formats = [
17 'gif' => 'image/gif',
18 'jpe' => 'image/jpeg',
19 'jpeg' => 'image/jpeg',
20 'jpg' => 'image/jpeg',
21 'png' => 'image/png',
22 'tif' => 'image/tiff',
23 'tiff' => 'image/tiff',
24 'webp' => 'image/webp'
25 ];
26
27 /**
28 * Class constructor.
29 *
30 * @return void
31 */
32 public function __construct() {
33 // provider slug
34 $this->slug = 'wikimedia';
35
36 // provider name
37 $this->name = __( 'Wikimedia', 'responsive-lightbox' );
38
39 // default values
40 $this->defaults = [
41 'active' => true
42 ];
43
44 // setting fields - Settings API format (no credentials required)
45 $this->fields = [
46 'active' => [
47 'title' => $this->name,
48 'section' => 'responsive_lightbox_remote_library_providers',
49 'type' => 'boolean',
50 'label' => __( 'Enable Wikimedia support.', 'responsive-lightbox' ),
51 'parent' => 'wikimedia'
52 ]
53 ];
54
55 // response data
56 $this->response_data_args = [
57 'continue'
58 ];
59
60 // add provider
61 parent::add_provider( $this );
62
63 // handle last page
64 add_filter( 'rl_remote_library_query_last_page', [ $this, 'handle_last_page' ], 10, 3 );
65 }
66
67
68
69 /**
70 * Validate settings.
71 *
72 * @param array $input POST data
73 * @return array
74 */
75 public function validate_settings( $input ) {
76 if ( ! isset( $input['wikimedia'] ) ) {
77 $input['wikimedia'] = $this->rl->defaults['remote_library']['wikimedia'];
78 } else {
79 // Support both legacy key (active) and Settings API key (wikimedia_active)
80 if ( ! isset( $input['wikimedia']['active'] ) && ! isset( $input['wikimedia']['wikimedia_active'] ) ) {
81 $input['wikimedia']['active'] = false;
82 }
83 }
84
85 return $input;
86 }
87
88 /**
89 * Prepare data to run remote query.
90 *
91 * @param string $search_phrase Search phrase
92 * @param array $args Provider arguments
93 * @return void
94 */
95 public function prepare_query( $search_phrase, $args = [] ) {
96 // check page parameter
97 if ( isset( $args['preview_page'] ) )
98 $args['preview_page'] = (int) $args['preview_page'];
99 else
100 $args['preview_page'] = 1;
101
102 if ( $args['preview_page'] < 1 )
103 $args['preview_page'] = 1;
104
105 // check limit
106 if ( isset( $args['limit'] ) && ( $limit = (int) $args['limit'] ) > 0 )
107 $args['preview_per_page'] = $limit;
108 else {
109 // check per page parameter
110 if ( isset( $args['preview_per_page'] ) )
111 $args['preview_per_page'] = (int) $args['preview_per_page'];
112 else
113 $args['preview_per_page'] = 20;
114
115 if ( $args['preview_per_page'] < 5 || $args['preview_per_page'] > 200 )
116 $args['preview_per_page'] = 20;
117 }
118
119 // set query arguments
120 $this->query_args = $args;
121
122 $query_args = [
123 'action' => 'query',
124 'format' => 'json',
125 'list' => 'allimages',
126 'aiprefix' => urlencode( $search_phrase ),
127 'ailimit' => $args['preview_per_page'],
128 'aisort' => 'name',
129 'aidir' => 'ascending',
130 'aiprop' => 'url|size|extmetadata|dimensions'
131 ];
132
133 if ( isset( $args['response_data']['wikimedia']['continue']['aicontinue'] ) )
134 $query_args['aicontinue'] = $args['response_data']['wikimedia']['continue']['aicontinue'];
135
136 // set query string
137 $this->query = add_query_arg( $query_args, 'https://commons.wikimedia.org/w/api.php' );
138
139 // set query remote arguments
140 $this->query_remote_args = [
141 'timeout' => 30,
142 'headers' => [
143 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
144 ]
145 ];
146 }
147
148 /**
149 * Get images from media provider.
150 *
151 * @param mixed $response Remote response
152 * @param array $args Query arguments
153 * @return array|WP_Error
154 */
155 public function get_query_results( $response, $args = [] ) {
156 $results = [];
157 $error = new WP_Error( 'rl_remote_library_wikimedia_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
158
159 // retrieve body
160 $response_body = wp_remote_retrieve_body( $response );
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['success'] ) && $response_json['success'] === false ) )
168 $results = $error;
169 else {
170 // set response data
171 $this->response_data = $response_json;
172
173 // get results
174 $results = isset( $response_json['query'] ) && is_array( $response_json['query'] ) && isset( $response_json['query']['allimages'] ) && is_array( $response_json['query']['allimages'] ) ? $response_json['query']['allimages'] : [];
175
176 // sanitize images
177 $results = $this->sanitize_results( $results );
178 }
179 } else
180 $results = $error;
181
182 return $results;
183 }
184
185 /**
186 * Handle query last page.
187 *
188 * @param bool $last Whether is it last page
189 * @param array $result Query result
190 * @param array $args Query arguments
191 * @return bool
192 */
193 public function handle_last_page( $last, $result, $args ) {
194 if ( $args['media_provider'] === 'wikimedia' && empty( $result['data']['wikimedia']['continue'] ) )
195 return true;
196
197 return $last;
198 }
199
200 /**
201 * Sanitize single result.
202 *
203 * @param array $result Single result
204 * @return array|false
205 */
206 public function sanitize_result( $result ) {
207 // allow only jpg, png and gif images
208 if ( preg_match( '/\.(jpe?g|gif|png)$/i', $result['url'] ) !== 1 )
209 return false;
210
211 // get part of an url
212 $url = explode( 'https://upload.wikimedia.org/wikipedia/commons/', $result['url'] );
213
214 // set dimensions
215 $width = (int) $result['width'];
216 $height = (int) $result['height'];
217
218 // calculate ratio
219 $ratio = $width / $height;
220
221 // try to get thumbnail url and dimensions
222 if ( ! empty( $url[1] ) ) {
223 $thumbnail_url = $result['url'];
224 $thumbnail_width = 0;
225 $thumbnail_height = 0;
226
227 $name = explode( '/', $url[1] );
228
229 if ( ! empty( $name[2] ) ) {
230 // standard smallest size
231 $thumbnail_width = 240;
232
233 // calculate new height based on original ratio
234 $thumbnail_height = (int) floor( $thumbnail_width / $ratio );
235
236 // use larger size if height is less than 150 pixels
237 if ( $thumbnail_height < 150 ) {
238 $thumbnail_width = 480;
239
240 // calculate new height based on original ratio
241 $thumbnail_height = (int) floor( $thumbnail_width / $ratio );
242 }
243
244 $thumbnail_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/' . $url[1] . '/' . $thumbnail_width . 'px-' . $name[2];
245 }
246 } else {
247 $thumbnail_url = $result['url'];
248 $thumbnail_width = $width;
249 $thumbnail_height = $height;
250 }
251
252 $imagedata = [
253 'id' => 0,
254 'link' => '',
255 'source' => esc_url_raw( $result['descriptionshorturl'] ),
256 'title' => sanitize_text_field( $result['title'] ),
257 'caption' => $this->get_attribution( 'Wikimedia', $result['descriptionshorturl'] ),
258 'description' => isset( $result['extmetadata']['ImageDescription']['value'] ) ? sanitize_text_field( $result['extmetadata']['ImageDescription']['value'] ) : '',
259 'alt' => isset( $result['extmetadata']['Categories']['value'] ) ? str_replace( '|', ', ', sanitize_text_field( $result['extmetadata']['Categories']['value'] ) ) : '',
260 'url' => esc_url_raw( $result['url'] ),
261 'width' => $width,
262 'height' => $height,
263 'orientation' => $height > $width ? 'portrait' : 'landscape',
264 'thumbnail_url' => esc_url_raw( $thumbnail_url ),
265 'thumbnail_width' => $thumbnail_width,
266 'thumbnail_height' => $thumbnail_height,
267 'thumbnail_orientation' => $thumbnail_height > $thumbnail_width ? 'portrait' : 'landscape',
268 'media_provider' => 'wikimedia',
269 'filename' => sanitize_file_name( $result['name'] ),
270 'dimensions' => $width . ' x ' . $height,
271 'type' => 'image'
272 ];
273
274 // create thumbnail link
275 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
276
277 return $imagedata;
278 }
279 }
280
281 new Responsive_Lightbox_Remote_Library_Wikimedia();
282