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