PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.3.3
Responsive Lightbox & Gallery v2.3.3
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 4 years ago class-fast-image.php 8 years ago class-folders-walker.php 7 years ago class-folders.php 4 years ago class-frontend.php 4 years ago class-galleries.php 4 years ago class-multilang.php 4 years ago class-remote-library-api.php 4 years ago class-remote-library.php 4 years ago class-settings.php 4 years ago class-tour.php 4 years ago class-welcome.php 4 years ago class-widgets.php 4 years ago functions.php 4 years ago
class-remote-library.php
517 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="' . ( ! empty( $attachment['rl_url'] ) ? $attachment['rl_url'] : $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 $wp_version;
147 global $pagenow;
148
149 // display only for post edit pages
150 if ( ! ( ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) || ( version_compare( $wp_version, '5.8', '>=' ) && ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' ) ) ) )
151 return;
152
153 // get main instance
154 $rl = Responsive_Lightbox();
155
156 wp_enqueue_script( 'rl-remote-library-media', RESPONSIVE_LIGHTBOX_URL . '/js/admin-media.js', array( 'jquery', 'media-models', 'underscore' ), $rl->defaults['version'] );
157
158 wp_localize_script(
159 'rl-remote-library-media',
160 'rlRemoteLibraryMedia',
161 array(
162 'thumbnailID' => $rl->galleries->maybe_generate_thumbnail(),
163 'postID' => get_the_ID(),
164 'providers' => $this->get_providers(),
165 'providersActive' => $this->get_active_providers(),
166 'allProviders' => __( 'All providers', 'responsive-lightbox' ),
167 'uploadAndInsert' => __( 'Upload and Insert', 'responsive-lightbox' ),
168 'uploadAndSelect' => __( 'Upload and Select', 'responsive-lightbox' ),
169 'filterByremoteLibrary' => __( 'Filter by remote library', 'responsive-lightbox' ),
170 'getUploadNonce' => wp_create_nonce( 'rl-remote-library-upload-image' )
171 )
172 );
173
174 // enqueue gallery
175 $rl->galleries->enqueue_gallery_scripts_styles();
176 }
177
178 /**
179 * AJAX media query action.
180 *
181 * @return void
182 */
183 public function ajax_query_media() {
184 $data = stripslashes_deep( $_POST );
185 $results = array(
186 'last' => false,
187 'images' => array(),
188 'data' => array()
189 );
190
191 if ( isset( $data['media_provider'], $data['media_search'], $data['media_page'] ) && ( $data['media_provider'] === 'all' || $this->is_active_provider( $data['media_provider'] ) ) ) {
192 $data['preview_page'] = (int) $data['media_page'];
193 $data['preview_per_page'] = 20;
194
195 // get images
196 $results['images'] = $this->get_remote_library_images( $data );
197
198 // get main instance
199 $rl = Responsive_Lightbox();
200
201 // single provider?
202 if ( $data['media_provider'] !== 'all' ) {
203 // get provider
204 $provider = $rl->providers[$data['media_provider']];
205
206 // add response data arguments if needed
207 if ( ! empty( $provider['response_args'] ) ) {
208 $response = $provider['instance']->get_response_data();
209
210 foreach ( $provider['response_args'] as $arg ) {
211 if ( array_key_exists( $arg, $response ) )
212 $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) );
213 }
214 }
215 } else {
216 // get active providers
217 $providers = $this->get_active_providers();
218
219 if ( ! empty( $providers ) ) {
220 foreach ( $providers as $provider ) {
221 // get provider
222 $provider = $rl->providers[$provider];
223
224 // add response data arguments if needed
225 if ( ! empty( $provider['response_args'] ) ) {
226 $response = $provider['instance']->get_response_data();
227
228 foreach ( $provider['response_args'] as $arg ) {
229 if ( array_key_exists( $arg, $response ) )
230 $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) );
231 }
232 }
233 }
234 }
235 }
236
237 if ( ! empty( $results['images'] ) ) {
238 // create WP compatible attachments
239 $results['images'] = $this->create_wp_remote_attachments( $results['images'], $data );
240
241 // handle last page if needed
242 $results['last'] = apply_filters( 'rl_remote_library_query_last_page', false, $results, $data );
243 } else
244 $results['last'] = true;
245 }
246
247 // send JSON
248 wp_send_json( $results );
249 }
250
251 /**
252 * AJAX upload image action.
253 *
254 * @return void
255 */
256 public function ajax_upload_image() {
257 $data = stripslashes_deep( $_POST );
258 $new_data = array();
259
260 // verified upload?
261 if ( current_user_can( 'upload_files' ) && isset( $data['rlnonce'], $data['image'], $data['post_id'] ) && wp_verify_nonce( $data['rlnonce'], 'rl-remote-library-upload-image' ) ) {
262 if ( ! function_exists( 'media_handle_upload' ) )
263 require_once( path_join( ABSPATH, 'wp-admin/includes/media.php' ) );
264
265 if ( ! function_exists( 'wp_handle_upload' ) )
266 require_once( path_join( ABSPATH, 'wp-admin/includes/file.php' ) );
267
268 if ( ! empty( $data['image']['url'] ) ) {
269 // get image as binary data
270 $response = wp_safe_remote_get( esc_url_raw( $data['image']['url'] ) );
271
272 // get file name
273 $file_name = basename( parse_url( $data['image']['name'], PHP_URL_PATH ) );
274
275 // check extension
276 $file_ext = strrpos( $file_name, '.' );
277
278 // no extension?
279 if ( $file_ext === false )
280 $file_name .= '.jpg';
281
282 // no errors?
283 if ( ! is_wp_error( $response ) ) {
284 $bits = wp_remote_retrieve_body( $response );
285 $loaded = wp_upload_bits( $file_name, null, $bits, current_time( 'Y/m' ) );
286
287 if ( isset( $loaded['error'] ) && $loaded['error'] ) {
288 $results = array(
289 'error' => true,
290 'message' => $loaded['error']
291 );
292 } else {
293 // simulate upload
294 $_FILES['rl-remote-image'] = array(
295 'error' => 0,
296 'name' => $file_name,
297 'tmp_name' => $loaded['file'],
298 'size' => filesize( $loaded['file'] )
299 );
300
301 // get post ID
302 $post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
303
304 // upload image
305 $attachment_id = media_handle_upload(
306 'rl-remote-image',
307 $post_id,
308 array(
309 'post_title' => $data['image']['title'],
310 'post_content' => $data['image']['description'],
311 'post_excerpt' => $data['image']['caption']
312 ), array(
313 'action' => 'rl_remote_library_handle_upload',
314 'test_form' => false
315 )
316 );
317
318 // upload success?
319 if ( ! is_wp_error( $attachment_id ) ) {
320 add_post_meta( $attachment_id, '_wp_attachment_image_alt', $data['image']['alt'] );
321
322 $new_data['id'] = $attachment_id;
323 $new_data['full'] = wp_get_attachment_image_src( $attachment_id, 'full' );
324 }
325 }
326 }
327 }
328 }
329
330 // send JSON
331 wp_send_json( $new_data );
332 }
333
334 /**
335 * Create WP compatible attachments for JavaScript.
336 *
337 * @param array $results Requested images
338 * @param array $args Additional arguments
339 * @return array Compatible attachments
340 */
341 public function create_wp_remote_attachments( $results, $args ) {
342 $user = wp_get_current_user();
343 $copy = $results;
344 $time = current_time( 'timestamp' );
345 $date_format = get_option( 'date_format' );
346 $date = date_i18n( __( 'F j Y' ), $time );
347
348 foreach ( $results as $no => $result ) {
349 // detect orientation
350 $orientation = $result['width'] > $result['height'] ? 'landscape' : 'portrait';
351
352 // make sure those attributes are strings
353 $copy[$no]['caption'] = (string) $result['caption'];
354 $copy[$no]['description'] = (string) $result['description'];
355 $copy[$no]['title'] = (string) $result['title'];
356 $copy[$no]['filename'] = $copy[$no]['name'] = (string) $result['filename'];
357
358 // rest of attributes
359 $copy[$no]['id'] = 'rl-attachment-' . ( ( $args['preview_page'] - 1 ) * $args['preview_per_page'] + $no ) . '-' . $args['media_provider'];
360 $copy[$no]['remote_library_image'] = true;
361 $copy[$no]['author'] = $user->ID;
362 $copy[$no]['authorName'] = $user->user_login;
363 $copy[$no]['can'] = array(
364 'save' => true,
365 'remove' => false
366 );
367 $copy[$no]['compat'] = '';
368 $copy[$no]['date'] = $time;
369 $copy[$no]['dateFormatted'] = $date;
370 $copy[$no]['delete'] = '';
371 $copy[$no]['edit'] = '';
372 $copy[$no]['update'] = '';
373 $copy[$no]['filesizeHumanReadable'] = '';
374 $copy[$no]['filesizeInBytes'] = 0;
375 $copy[$no]['icon'] = '';
376 $copy[$no]['link'] = $result['url'];
377 $copy[$no]['menuOrder'] = 0;
378 $copy[$no]['meta'] = false;
379
380 // check extension
381 $file_ext = strrpos( basename( parse_url( $result['url'], PHP_URL_PATH ) ), '.' );
382
383 // no extension?
384 if ( $file_ext === false )
385 $file_ext .= 'jpg';
386
387 if ( $file_ext === 'png' || $file_ext === 'gif' ) {
388 $copy[$no]['mime'] = 'image/' . $file_ext;
389 $copy[$no]['subtype'] = $file_ext;
390 } else {
391 $copy[$no]['mime'] = 'image/jpeg';
392 $copy[$no]['subtype'] = 'jpeg';
393 }
394
395 $copy[$no]['modified'] = $time;
396 $copy[$no]['nonces'] = array(
397 'delete' => '',
398 'edit' => '',
399 'update' => ''
400 );
401 $copy[$no]['orientation'] = $orientation;
402 $copy[$no]['status'] = 'inherit';
403 $copy[$no]['type'] = 'image';
404 $copy[$no]['uploadedTo'] = 0;
405 $copy[$no]['uploadedToLink'] = '';
406 $copy[$no]['uploadedToTitle'] = '';
407 $copy[$no]['sizes'] = array(
408 // 'thumbnail' => array(
409 // 'height' => $result['thumbnail_height'],
410 // 'width' => $result['thumbnail_width'],
411 // 'orientation' => $orientation,
412 // 'url' => $result['thumbnail_url']
413 // ),
414 'medium' => array(
415 'height' => $result['thumbnail_height'],
416 'width' => $result['thumbnail_width'],
417 'orientation' => $orientation,
418 'url' => $result['thumbnail_url']
419 ),
420 'full' => array(
421 'height' => $result['height'],
422 'width' => $result['width'],
423 'orientation' => $orientation,
424 'url' => $result['url']
425 )
426 );
427 }
428
429 return apply_filters( 'rl_remote_library_wp_attachments', $copy, $args );
430 }
431
432 /**
433 * Remote library media query.
434 *
435 * @param array $args Arguments
436 * @return array Images
437 */
438 public function get_remote_library_images( $args ) {
439 $args = stripslashes_deep( $args );
440
441 // search phrase
442 if ( isset( $args['media_search'] ) )
443 $args['media_search'] = strtolower( trim( $args['media_search'] ) );
444 else
445 $args['media_search'] = '';
446
447 // media provider
448 if ( isset( $args['media_provider'] ) )
449 $args['media_provider'] = trim( $args['media_provider'] );
450 else
451 $args['media_provider'] = 'all';
452
453 // page number
454 if ( isset( $args['preview_page'] ) )
455 $args['preview_page'] = (int) $args['preview_page'];
456 else
457 $args['preview_page'] = 1;
458
459 // number of images per page
460 if ( isset( $args['preview_per_page'] ) )
461 $args['preview_per_page'] = (int) $args['preview_per_page'];
462 else
463 $args['preview_per_page'] = 20;
464
465 // get active providers
466 $providers = $this->get_active_providers();
467
468 // prepare valid providers
469 $valid_providers = array();
470
471 if ( $args['media_provider'] === 'all' )
472 $valid_providers = $providers;
473 elseif ( in_array( $args['media_provider'], $providers, true ) )
474 $valid_providers[] = $args['media_provider'];
475
476 $results = array();
477
478 // any valid providers?
479 if ( ! empty( $valid_providers ) ) {
480 // get main instance
481 $rl = Responsive_Lightbox();
482
483 foreach ( $valid_providers as $provider_name ) {
484 if ( ! empty( $args['response_data'][$provider_name] ) ) {
485 // get provider
486 $provider = $rl->providers[$provider_name];
487
488 if ( ! empty( $provider['response_args'] ) ) {
489 foreach ( $provider['response_args'] as $arg ) {
490 if ( array_key_exists( $arg, $args['response_data'][$provider_name] ) ) {
491 $base64 = base64_decode( $args['response_data'][$provider_name][$arg] );
492
493 if ( ! empty( $base64 ) )
494 $args['response_data'][$provider_name][$arg] = json_decode( $base64, true );
495 }
496 }
497 }
498 }
499
500 $results = apply_filters( 'rl_remote_library_query', $results, $args['media_search'], $provider_name, $args );
501 $nor = count( $results );
502
503 // more than requested images?
504 if ( $nor > $args['preview_per_page'] ) {
505 // get part of images
506 $results = array_slice( $results, 0, $args['preview_per_page'], true );
507
508 break;
509 // same amount of images?
510 } elseif ( $nor === $args['preview_per_page'] )
511 break;
512 }
513 }
514
515 return $results;
516 }
517 }