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