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-wikimedia.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-wikimedia.php
248 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) )
3 exit;
4
5 /**
6 * Responsive Lightbox Remote Library Wikimedia class.
7 *
8 * @class Responsive_Lightbox_Remote_Library_Wikimedia
9 */
10 class Responsive_Lightbox_Remote_Library_Wikimedia 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 = 'wikimedia';
20
21 // provider name
22 $this->name = __( 'Wikimedia', 'responsive-lightbox' );
23
24 // default values
25 $this->defaults = array(
26 'active' => false
27 );
28
29 // setting fields
30 $this->fields = array(
31 'title' => $this->name,
32 'section' => 'responsive_lightbox_remote_library_providers',
33 'type' => 'custom',
34 'callback' => array( $this, 'render_field' )
35 );
36
37 // response data
38 $this->response_data_args = array(
39 'continue'
40 );
41
42 // add provider
43 parent::add_provider( $this );
44
45 // handle last page
46 add_filter( 'rl_remote_library_query_last_page', array( $this, 'handle_last_page' ), 10, 3 );
47 }
48
49 /**
50 * Render field.
51 *
52 * @return void
53 */
54 public function render_field() {
55 echo '
56 <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 ) . ' />' . __( 'Enable Wikimedia.', 'responsive-lightbox' ) . '</label></p>';
57 }
58
59 /**
60 * Validate settings.
61 *
62 * @param array $input POST data
63 * @return array Validated settings
64 */
65 public function validate_settings( $input ) {
66 if ( ! isset( $_POST['responsive_lightbox_remote_library'] ) )
67 $input['wikimedia'] = $this->rl->defaults['remote_library']['wikimedia'];
68 else {
69 // active
70 $input['wikimedia']['active'] = isset( $_POST['responsive_lightbox_remote_library']['wikimedia']['active'] );
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'] > 200 )
100 $args['preview_per_page'] = 20;
101
102 // set query arguments
103 $this->query_args = $args;
104
105 $query_args = array(
106 'action' => 'query',
107 'format' => 'json',
108 'list' => 'allimages',
109 'aiprefix' => urlencode( $search_phrase ),
110 'ailimit' => $args['preview_per_page'],
111 'aioffset' => 0,
112 'aisort' => 'name',
113 'aidir' => 'ascending',
114 'aiprop' => 'url|size|extmetadata'
115 );
116
117 if ( isset( $args['response_data']['wikimedia']['continue']['aicontinue'] ) )
118 $query_args['aicontinue'] = $args['response_data']['wikimedia']['continue']['aicontinue'];
119
120 // set query string
121 $this->query = add_query_arg( $query_args, 'https://commons.wikimedia.org/w/api.php' );
122
123 // set query remote arguments
124 $this->query_remote_args = array(
125 'timeout' => 30,
126 'headers' => array(
127 'User-Agent' => __( 'Responsive Lightbox', 'responsive-lightbox' ) . ' ' . $this->rl->defaults['version']
128 )
129 );
130 }
131
132 /**
133 * Get images from media provider.
134 *
135 * @param mixed $response Remote response
136 * @param array $args Query arguments
137 * @return array Valid images or WP_Error
138 */
139 public function get_query_results( $response, $args = array() ) {
140 $results = array();
141 $error = new WP_Error( 'rl_remote_library_wikimedia_get_query_results', __( 'Parsing request error', 'responsive-lightbox' ) );
142
143 // retrieve body
144 $response_body = wp_remote_retrieve_body( $response );
145
146 // any data?
147 if ( $response_body !== '' ) {
148 $response_json = json_decode( $response_body, true );
149
150 // invalid data?
151 if ( $response_json === null || ( isset( $response_json['success'] ) && $response_json['success'] === false ) )
152 $results = $error;
153 else {
154 $this->response_data = $response_json;
155
156 // get results
157 $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'] : array();
158
159 // sanitize images
160 $results = $this->sanitize_results( $results );
161 }
162 } else
163 $results = $error;
164
165 return $results;
166 }
167
168 /**
169 * Handle query last page.
170 *
171 * @param bool $last Whether is it last page
172 * @param array $result Query result
173 * @param array $args Query arguments
174 * @return bool
175 */
176 public function handle_last_page( $last, $result, $args ) {
177 if ( $args['media_provider'] === 'wikimedia' && empty( $result['data']['wikimedia']['continue'] ) )
178 return true;
179
180 return $last;
181 }
182
183 /**
184 * Sanitize single result.
185 *
186 * @param array $result Single result
187 * @return mixed Array on success, otherwise false
188 */
189 public function sanitize_result( $result ) {
190 // allow only JPG, PNG and GIF images
191 if ( preg_match( '/\.(jpe?g|gif|png)$/i', $result['url'] ) !== 1 )
192 return false;
193
194 // get part of an URL
195 $url = explode( 'https://upload.wikimedia.org/wikipedia/commons/', $result['url'] );
196
197 // set dimensions
198 $width = (int) $result['width'];
199 $height = (int) $result['height'];
200
201 // try to get thumbnail url and dimensions
202 if ( ! empty( $url[1] ) ) {
203 $thumbnail_url = $result['url'];
204 $thumbnail_width = 0;
205 $thumbnail_height = 0;
206
207 $name = explode( '/', $url[1] );
208
209 if ( ! empty( $name[2] ) ) {
210 $thumbnail_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/' . $url[1] . '/240px-' . $name[2];
211 $thumbnail_width = 150;
212
213 // calculate new height based on original ratio
214 $thumbnail_height = (int) floor( $thumbnail_width / ( $width / $height ) );
215 }
216 } else {
217 $thumbnail_url = $result['url'];
218 $thumbnail_width = $width;
219 $thumbnail_height = $height;
220 }
221
222 $imagedata = array(
223 'id' => 0,
224 'link' => '',
225 'source' => $result['descriptionshorturl'],
226 'title' => $result['title'],
227 'caption' => $this->get_attribution( 'Wikimedia', $result['descriptionshorturl'] ),
228 'description' => isset( $result['extmetadata']['ImageDescription']['value'] ) ? $result['extmetadata']['ImageDescription']['value'] : '',
229 'alt' => isset( $result['extmetadata']['Categories']['value'] ) ? str_replace( '|', ', ', $result['extmetadata']['Categories']['value'] ) : '',
230 'url' => $result['url'],
231 'width' => $width,
232 'height' => $height,
233 'thumbnail_url' => $thumbnail_url,
234 'thumbnail_width' => $thumbnail_width,
235 'thumbnail_height' => $thumbnail_height,
236 'media_provider' => 'wikimedia',
237 'filename' => $result['name'],
238 'dimensions' => $width . ' x ' . $height
239 );
240
241 // create thumbnail link
242 $imagedata['thumbnail_link'] = $this->rl->galleries->get_gallery_image_link( $imagedata, 'thumbnail' );
243
244 return $imagedata;
245 }
246 }
247
248 new Responsive_Lightbox_Remote_Library_Wikimedia();