PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.2.1
Responsive Lightbox & Gallery v2.2.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 / class-remote-library.php
responsive-lightbox / includes Last commit date
providers 6 years ago class-fast-image.php 8 years ago class-folders-walker.php 7 years ago class-folders.php 6 years ago class-frontend.php 6 years ago class-galleries.php 6 years ago class-remote-library-api.php 6 years ago class-remote-library.php 6 years ago class-settings.php 6 years ago class-tour.php 6 years ago class-welcome.php 6 years ago class-widgets.php 6 years ago functions.php 6 years ago
class-remote-library.php
515 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Responsive Lightbox Remote Library class.
8 *
9 * @class Responsive_Lightbox_Remote_Library
10 */
11 class Responsive_Lightbox_Remote_Library {
12
13 public $providers = array();
14
15 /**
16 * Constructor.
17 *
18 * @return void
19 */
20 public function __construct() {
21 // is remote library active?
22 if ( ! Responsive_Lightbox()->options['remote_library']['active'] )
23 return;
24
25 // actions
26 add_action( 'wp_ajax_rl_remote_library_query', array( $this, 'ajax_query_media' ) );
27 add_action( 'wp_ajax_rl_upload_image', array( $this, 'ajax_upload_image' ) );
28 add_action( 'admin_enqueue_scripts', array( $this, 'remote_library_scripts' ) );
29
30 // add filter to send new data to editor
31 add_filter( 'image_send_to_editor', array( $this, 'send_image_to_editor' ), 21, 8 );
32 }
33
34 /**
35 * Hidden field with response data for gallery preview.
36 *
37 * @param array $args Field arguments
38 * @return string Rendered field
39 */
40 public function remote_library_response_data( $args ) {
41 // access main instance
42 $rl = Responsive_Lightbox();
43
44 // get active providers
45 $providers = $this->get_active_providers();
46
47 $html = '';
48
49 // any providers?
50 if ( ! empty( $providers ) ) {
51 foreach ( $providers as $provider ) {
52 // get provider
53 $provider = $rl->providers[$provider];
54
55 // add response data arguments if needed
56 if ( ! empty( $provider['response_args'] ) ) {
57 $response = $provider['instance']->get_response_data();
58
59 foreach ( $provider['response_args'] as $arg ) {
60 if ( array_key_exists( $arg, $response ) ) {
61 $html .= '<input id="rl_' . $args['tab_id'] . '_' . $args['menu_item'] . '_' . $args['field'] . '_' . $provider['slug'] . '_' . $arg . '" type="hidden" value="' . base64_encode( json_encode( $response[$arg] ) ) . '" name="rl_gallery[' . $args['tab_id'] . '][' . $args['menu_item'] . '][' . $args['field'] . '][' . $provider['slug'] . '][' . $arg . ']" data-previous="" data-subarg="2" />';
62 }
63 }
64 }
65 }
66 }
67
68 return $html;
69 }
70
71 /**
72 * Send updated image data to editor.
73 *
74 * @param string $html The image HTML markup to send
75 * @param int $id The attachment ID
76 * @param string $caption The image caption
77 * @param string $title The image title
78 * @param string $align The image alignment
79 * @param string $url The image source URL
80 * @param string|array $size Size of image
81 * @param string $alt The image alternative text.
82 * @return string Updated image HTML
83 */
84 function send_image_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
85 if ( $id === Responsive_Lightbox()->galleries->maybe_generate_thumbnail() && isset( $_POST['attachment'] ) ) {
86 $attachment = wp_unslash( $_POST['attachment'] );
87
88 if ( isset( $attachment['remote_library_image'], $attachment['width'], $attachment['height'] ) ) {
89 $html = preg_replace( '/src=(\'|")(.*?)(\'|")/', 'src="' . $url . '"', $html );
90 $html = preg_replace( '/width=(\'|")(.*?)(\'|")/', 'width="' . ( (int) $attachment['width'] ) . '"', $html );
91 $html = preg_replace( '/height=(\'|")(.*?)(\'|")/', 'height="' . ( (int) $attachment['height'] ) . '"', $html );
92 $html = preg_replace( '/(\s)?id="attachment_' . $id . '"/', '', $html );
93 $html = preg_replace( '/(\s)?wp-image-' . $id . '/', '', $html );
94 }
95 }
96
97 return $html;
98 }
99
100 /**
101 * Get all available providers.
102 *
103 * @return array Providers
104 */
105 public function get_providers() {
106 return apply_filters( 'rl_get_providers', Responsive_Lightbox()->providers );
107 }
108
109 /**
110 * Get all active providers.
111 *
112 * @return array Providers
113 */
114 public function get_active_providers() {
115 $providers = $this->get_providers();
116 $active_providers = array();
117
118 foreach ( $providers as $provider => $data ) {
119 if ( Responsive_Lightbox()->options['remote_library'][$provider]['active'] ) {
120 $active_providers[] = $provider;
121 }
122 }
123
124 return apply_filters( 'rl_get_active_providers', $active_providers );
125 }
126
127 /**
128 * Check whether provider is active.
129 *
130 * @param string $provider Media provider
131 * @return bool
132 */
133 public function is_active_provider( $provider ) {
134 $providers = $this->get_providers();
135 $rl = Responsive_Lightbox();
136
137 return (bool) apply_filters( 'rl_is_active_provider', array_key_exists( $provider, $rl->options['remote_library'] ) && $rl->options['remote_library'][$provider]['active'], $provider );
138 }
139
140 /**
141 * Scripts and styles for media frame.
142 *
143 * @return void
144 */
145 public function remote_library_scripts() {
146 global $pagenow;
147
148 // display only for post edit pages
149 if ( ! ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) )
150 return;
151
152 // get main instance
153 $rl = Responsive_Lightbox();
154
155 wp_enqueue_script( 'rl-remote-library-media', RESPONSIVE_LIGHTBOX_URL . '/js/admin-media.js', array( 'jquery', 'media-views', 'media-models' ), $rl->defaults['version'] );
156
157 wp_localize_script(
158 'rl-remote-library-media',
159 'rlRemoteLibraryMedia',
160 array(
161 'thumbnailID' => $rl->galleries->maybe_generate_thumbnail(),
162 'postID' => get_the_ID(),
163 'providers' => $this->get_providers(),
164 'providersActive' => $this->get_active_providers(),
165 'allProviders' => __( 'All providers', 'responsive-lightbox' ),
166 'uploadAndInsert' => __( 'Upload and Insert', 'responsive-lightbox' ),
167 'filterByremoteLibrary' => __( 'Filter by remote library', 'responsive-lightbox' ),
168 'getUploadNonce' => wp_create_nonce( 'rl-remote-library-upload-image' )
169 )
170 );
171
172 // enqueue gallery
173 $rl->galleries->enqueue_gallery_scripts_styles();
174 }
175
176 /**
177 * AJAX media query action.
178 *
179 * @return void
180 */
181 public function ajax_query_media() {
182 $data = stripslashes_deep( $_POST );
183 $results = array(
184 'last' => false,
185 'images' => array(),
186 'data' => array()
187 );
188
189 if ( isset( $data['media_provider'], $data['media_search'], $data['media_page'] ) && ( $data['media_provider'] === 'all' || $this->is_active_provider( $data['media_provider'] ) ) ) {
190 $data['preview_page'] = (int) $data['media_page'];
191 $data['preview_per_page'] = 20;
192
193 // get images
194 $results['images'] = $this->get_remote_library_images( $data );
195
196 // get main instance
197 $rl = Responsive_Lightbox();
198
199 // single provider?
200 if ( $data['media_provider'] !== 'all' ) {
201 // get provider
202 $provider = $rl->providers[$data['media_provider']];
203
204 // add response data arguments if needed
205 if ( ! empty( $provider['response_args'] ) ) {
206 $response = $provider['instance']->get_response_data();
207
208 foreach ( $provider['response_args'] as $arg ) {
209 if ( array_key_exists( $arg, $response ) )
210 $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) );
211 }
212 }
213 } else {
214 // get active providers
215 $providers = $this->get_active_providers();
216
217 if ( ! empty( $providers ) ) {
218 foreach ( $providers as $provider ) {
219 // get provider
220 $provider = $rl->providers[$provider];
221
222 // add response data arguments if needed
223 if ( ! empty( $provider['response_args'] ) ) {
224 $response = $provider['instance']->get_response_data();
225
226 foreach ( $provider['response_args'] as $arg ) {
227 if ( array_key_exists( $arg, $response ) )
228 $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) );
229 }
230 }
231 }
232 }
233 }
234
235 if ( ! empty( $results['images'] ) ) {
236 // create WP compatible attachments
237 $results['images'] = $this->create_wp_remote_attachments( $results['images'], $data );
238
239 // handle last page if needed
240 $results['last'] = apply_filters( 'rl_remote_library_query_last_page', false, $results, $data );
241 } else
242 $results['last'] = true;
243 }
244
245 // send JSON
246 wp_send_json( $results );
247 }
248
249 /**
250 * AJAX upload image action.
251 *
252 * @return void
253 */
254 public function ajax_upload_image() {
255 $data = stripslashes_deep( $_POST );
256 $new_data = array();
257
258 // verified upload?
259 if ( current_user_can( 'upload_files' ) && isset( $data['rlnonce'], $data['image'], $data['post_id'] ) && wp_verify_nonce( $data['rlnonce'], 'rl-remote-library-upload-image' ) ) {
260 if ( ! function_exists( 'media_handle_upload' ) )
261 require_once( path_join( ABSPATH, 'wp-admin/includes/media.php' ) );
262
263 if ( ! function_exists( 'wp_handle_upload' ) )
264 require_once( path_join( ABSPATH, 'wp-admin/includes/file.php' ) );
265
266 if ( ! empty( $data['image']['url'] ) ) {
267 // get image as binary data
268 $response = wp_safe_remote_get( esc_url_raw( $data['image']['url'] ) );
269
270 // get file name
271 $file_name = basename( parse_url( $data['image']['name'], PHP_URL_PATH ) );
272
273 // check extension
274 $file_ext = strrpos( $file_name, '.' );
275
276 // no extension?
277 if ( $file_ext === false )
278 $file_name .= '.jpg';
279
280 // no errors?
281 if ( ! is_wp_error( $response ) ) {
282 $bits = wp_remote_retrieve_body( $response );
283 $loaded = wp_upload_bits( $file_name, null, $bits, current_time( 'Y/m' ) );
284
285 if ( isset( $loaded['error'] ) && $loaded['error'] ) {
286 $results = array(
287 'error' => true,
288 'message' => $loaded['error']
289 );
290 } else {
291 // simulate upload
292 $_FILES['rl-remote-image'] = array(
293 'error' => 0,
294 'name' => $file_name,
295 'tmp_name' => $loaded['file'],
296 'size' => filesize( $loaded['file'] )
297 );
298
299 // get post ID
300 $post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
301
302 // upload image
303 $attachment_id = media_handle_upload(
304 'rl-remote-image',
305 $post_id,
306 array(
307 'post_title' => $data['image']['title'],
308 'post_content' => $data['image']['description'],
309 'post_excerpt' => $data['image']['caption']
310 ), array(
311 'action' => 'rl_remote_library_handle_upload',
312 'test_form' => false
313 )
314 );
315
316 // upload success?
317 if ( ! is_wp_error( $attachment_id ) ) {
318 add_post_meta( $attachment_id, '_wp_attachment_image_alt', $data['image']['alt'] );
319
320 $new_data['id'] = $attachment_id;
321 $new_data['full'] = wp_get_attachment_image_src( $attachment_id, 'full' );
322 }
323 }
324 }
325 }
326 }
327
328 // send JSON
329 wp_send_json( $new_data );
330 }
331
332 /**
333 * Create WP compatible attachments for JavaScript.
334 *
335 * @param array $results Requested images
336 * @param array $args Additional arguments
337 * @return array Compatible attachments
338 */
339 public function create_wp_remote_attachments( $results, $args ) {
340 $user = wp_get_current_user();
341 $copy = $results;
342 $time = current_time( 'timestamp' );
343 $date_format = get_option( 'date_format' );
344 $date = date_i18n( __( 'F j Y' ), $time );
345
346 foreach ( $results as $no => $result ) {
347 // detect orientation
348 $orientation = $result['width'] > $result['height'] ? 'landscape' : 'portrait';
349
350 // make sure those attributes are strings
351 $copy[$no]['caption'] = (string) $result['caption'];
352 $copy[$no]['description'] = (string) $result['description'];
353 $copy[$no]['title'] = (string) $result['title'];
354 $copy[$no]['filename'] = $copy[$no]['name'] = (string) $result['filename'];
355
356 // rest of attributes
357 $copy[$no]['id'] = 'rl-attachment-' . ( ( $args['preview_page'] - 1 ) * $args['preview_per_page'] + $no ) . '-' . $args['media_provider'];
358 $copy[$no]['remote_library_image'] = true;
359 $copy[$no]['author'] = $user->ID;
360 $copy[$no]['authorName'] = $user->user_login;
361 $copy[$no]['can'] = array(
362 'save' => true,
363 'remove' => false
364 );
365 $copy[$no]['compat'] = '';
366 $copy[$no]['date'] = $time;
367 $copy[$no]['dateFormatted'] = $date;
368 $copy[$no]['delete'] = '';
369 $copy[$no]['edit'] = '';
370 $copy[$no]['update'] = '';
371 $copy[$no]['filesizeHumanReadable'] = '';
372 $copy[$no]['filesizeInBytes'] = 0;
373 $copy[$no]['icon'] = '';
374 $copy[$no]['link'] = $result['url'];
375 $copy[$no]['menuOrder'] = 0;
376 $copy[$no]['meta'] = false;
377
378 // check extension
379 $file_ext = strrpos( basename( parse_url( $result['url'], PHP_URL_PATH ) ), '.' );
380
381 // no extension?
382 if ( $file_ext === false )
383 $file_ext .= 'jpg';
384
385 if ( $file_ext === 'png' || $file_ext === 'gif' ) {
386 $copy[$no]['mime'] = 'image/' . $file_ext;
387 $copy[$no]['subtype'] = $file_ext;
388 } else {
389 $copy[$no]['mime'] = 'image/jpeg';
390 $copy[$no]['subtype'] = 'jpeg';
391 }
392
393 $copy[$no]['modified'] = $time;
394 $copy[$no]['nonces'] = array(
395 'delete' => '',
396 'edit' => '',
397 'update' => ''
398 );
399 $copy[$no]['orientation'] = $orientation;
400 $copy[$no]['status'] = 'inherit';
401 $copy[$no]['type'] = 'image';
402 $copy[$no]['uploadedTo'] = 0;
403 $copy[$no]['uploadedToLink'] = '';
404 $copy[$no]['uploadedToTitle'] = '';
405 $copy[$no]['sizes'] = array(
406 // 'thumbnail' => array(
407 // 'height' => $result['thumbnail_height'],
408 // 'width' => $result['thumbnail_width'],
409 // 'orientation' => $orientation,
410 // 'url' => $result['thumbnail_url']
411 // ),
412 'medium' => array(
413 'height' => $result['thumbnail_height'],
414 'width' => $result['thumbnail_width'],
415 'orientation' => $orientation,
416 'url' => $result['thumbnail_url']
417 ),
418 'full' => array(
419 'height' => $result['height'],
420 'width' => $result['width'],
421 'orientation' => $orientation,
422 'url' => $result['url']
423 )
424 );
425 }
426
427 return apply_filters( 'rl_remote_library_wp_attachments', $copy, $args );
428 }
429
430 /**
431 * Remote library media query.
432 *
433 * @param array $args Arguments
434 * @return array Images
435 */
436 public function get_remote_library_images( $args ) {
437 $args = stripslashes_deep( $args );
438
439 // search phrase
440 if ( isset( $args['media_search'] ) )
441 $args['media_search'] = strtolower( trim( $args['media_search'] ) );
442 else
443 $args['media_search'] = '';
444
445 // media provider
446 if ( isset( $args['media_provider'] ) )
447 $args['media_provider'] = trim( $args['media_provider'] );
448 else
449 $args['media_provider'] = 'all';
450
451 // page number
452 if ( isset( $args['preview_page'] ) )
453 $args['preview_page'] = (int) $args['preview_page'];
454 else
455 $args['preview_page'] = 1;
456
457 // number of images per page
458 if ( isset( $args['preview_per_page'] ) )
459 $args['preview_per_page'] = (int) $args['preview_per_page'];
460 else
461 $args['preview_per_page'] = 20;
462
463 // get active providers
464 $providers = $this->get_active_providers();
465
466 // prepare valid providers
467 $valid_providers = array();
468
469 if ( $args['media_provider'] === 'all' )
470 $valid_providers = $providers;
471 elseif ( in_array( $args['media_provider'], $providers, true ) )
472 $valid_providers[] = $args['media_provider'];
473
474 $results = array();
475
476 // any valid providers?
477 if ( ! empty( $valid_providers ) ) {
478 // get main instance
479 $rl = Responsive_Lightbox();
480
481 foreach ( $valid_providers as $provider_name ) {
482 if ( ! empty( $args['response_data'][$provider_name] ) ) {
483 // get provider
484 $provider = $rl->providers[$provider_name];
485
486 if ( ! empty( $provider['response_args'] ) ) {
487 foreach ( $provider['response_args'] as $arg ) {
488 if ( array_key_exists( $arg, $args['response_data'][$provider_name] ) ) {
489 $base64 = base64_decode( $args['response_data'][$provider_name][$arg] );
490
491 if ( ! empty( $base64 ) )
492 $args['response_data'][$provider_name][$arg] = json_decode( $base64, true );
493 }
494 }
495 }
496 }
497
498 $results = apply_filters( 'rl_remote_library_query', $results, $args['media_search'], $provider_name, $args );
499 $nor = count( $results );
500
501 // more than requested images?
502 if ( $nor > $args['preview_per_page'] ) {
503 // get part of images
504 $results = array_slice( $results, 0, $args['preview_per_page'], true );
505
506 break;
507 // same amount of images?
508 } elseif ( $nor === $args['preview_per_page'] )
509 break;
510 }
511 }
512
513 return $results;
514 }
515 }