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 / class-remote-library.php
responsive-lightbox / includes Last commit date
providers 2 years ago class-fast-image.php 2 years ago class-folders.php 2 years ago class-frontend.php 2 years ago class-galleries.php 2 years ago class-multilang.php 2 years ago class-remote-library-api.php 2 years ago class-remote-library.php 2 years ago class-settings.php 2 years ago class-tour.php 2 years ago class-welcome.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-remote-library.php
528 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 = [];
14
15 /**
16 * Class 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', [ $this, 'ajax_query_media' ] );
27 add_action( 'wp_ajax_rl_upload_image', [ $this, 'ajax_upload_image' ] );
28 add_action( 'admin_enqueue_scripts', [ $this, 'remote_library_scripts' ] );
29
30 // add filter to send new data to editor
31 add_filter( 'image_send_to_editor', [ $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
39 */
40 public function remote_library_response_data( $args ) {
41 // get 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 .= '<span id="' . esc_attr( 'rl_' . $args['tab_id'] . '_' . $args['menu_item'] . '_' . $args['field'] . '_' . $provider['slug'] . '_' . $arg ) . '" class="rl-response-data" data-value="' . esc_attr( base64_encode( wp_json_encode( $response[$arg] ) ) ) . '" data-name="' . esc_attr( $arg ) . '" data-provider="' . esc_attr( $provider['slug'] ) . '"></span>';
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
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'] ) && is_array( $_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'] ) ? esc_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_' . (int) $id . '"/', '', $html );
93 $html = preg_replace( '/(\s)?wp-image-' . (int) $id . '/', '', $html );
94 }
95 }
96
97 return $html;
98 }
99
100 /**
101 * Get all available providers.
102 *
103 * @return array
104 */
105 public function get_providers() {
106 return (array) apply_filters( 'rl_get_providers', Responsive_Lightbox()->providers );
107 }
108
109 /**
110 * Get all active providers.
111 *
112 * @return array
113 */
114 public function get_active_providers() {
115 $providers = $this->get_providers();
116 $active_providers = [];
117
118 foreach ( $providers as $provider => $data ) {
119 if ( Responsive_Lightbox()->options['remote_library'][$provider]['active'] )
120 $active_providers[] = $provider;
121 }
122
123 return (array) apply_filters( 'rl_get_active_providers', $active_providers );
124 }
125
126 /**
127 * Check whether provider is active.
128 *
129 * @param string $provider Media provider
130 * @return bool
131 */
132 public function is_active_provider( $provider ) {
133 $providers = $this->get_providers();
134 $rl = Responsive_Lightbox();
135
136 return (bool) apply_filters( 'rl_is_active_provider', array_key_exists( $provider, $rl->options['remote_library'] ) && $rl->options['remote_library'][$provider]['active'], $provider );
137 }
138
139 /**
140 * Scripts and styles for media frame.
141 *
142 * @global string $wp_version
143 * @global string $pagenow
144 *
145 * @return void
146 */
147 public function remote_library_scripts() {
148 global $wp_version;
149 global $pagenow;
150
151 // display only for post edit pages
152 if ( ! ( ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) || ( version_compare( $wp_version, '5.8', '>=' ) && ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' ) ) ) )
153 return;
154
155 // get main instance
156 $rl = Responsive_Lightbox();
157
158 wp_enqueue_script( 'responsive-lightbox-remote-library-media', RESPONSIVE_LIGHTBOX_URL . '/js/admin-media.js', [ 'jquery', 'media-models', 'underscore' ], $rl->defaults['version'] );
159
160 // prepare script data
161 $script_data = [
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' => esc_html__( 'All providers', 'responsive-lightbox' ),
167 'uploadAndInsert' => esc_html__( 'Upload and Insert', 'responsive-lightbox' ),
168 'uploadAndSelect' => esc_html__( 'Upload and Select', 'responsive-lightbox' ),
169 'filterByremoteLibrary' => esc_html__( 'Filter by remote library', 'responsive-lightbox' ),
170 'getUploadNonce' => wp_create_nonce( 'rl-remote-library-upload-image' )
171 ];
172
173 wp_add_inline_script( 'responsive-lightbox-remote-library-media', 'var rlRemoteLibraryMedia = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
174
175 // enqueue gallery
176 $rl->galleries->enqueue_gallery_scripts_styles();
177 }
178
179 /**
180 * AJAX media query action.
181 *
182 * @return void
183 */
184 public function ajax_query_media() {
185 $data = stripslashes_deep( $_POST );
186 $results = [
187 'last' => false,
188 'images' => [],
189 'data' => []
190 ];
191
192 if ( isset( $data['media_provider'], $data['media_search'], $data['media_page'] ) ) {
193 $data['media_provider'] = sanitize_key( $data['media_provider'] );
194
195 if ( $data['media_provider'] === 'all' || $this->is_active_provider( $data['media_provider'] ) ) {
196 $data['preview_page'] = (int) $data['media_page'];
197 $data['preview_per_page'] = 20;
198
199 // get images
200 $results['images'] = $this->get_remote_library_images( $data );
201
202 // get main instance
203 $rl = Responsive_Lightbox();
204
205 // single provider?
206 if ( $data['media_provider'] !== 'all' ) {
207 // get provider
208 $provider = $rl->providers[$data['media_provider']];
209
210 // add response data arguments if needed
211 if ( ! empty( $provider['response_args'] ) ) {
212 $response = $provider['instance']->get_response_data();
213
214 foreach ( $provider['response_args'] as $arg ) {
215 if ( array_key_exists( $arg, $response ) )
216 $results['data'][$provider['slug']][$arg] = base64_encode( wp_json_encode( $response[$arg] ) );
217 }
218 }
219 } else {
220 // get active providers
221 $providers = $this->get_active_providers();
222
223 if ( ! empty( $providers ) ) {
224 foreach ( $providers as $provider ) {
225 // get provider
226 $provider = $rl->providers[$provider];
227
228 // add response data arguments if needed
229 if ( ! empty( $provider['response_args'] ) ) {
230 $response = $provider['instance']->get_response_data();
231
232 foreach ( $provider['response_args'] as $arg ) {
233 if ( array_key_exists( $arg, $response ) )
234 $results['data'][$provider['slug']][$arg] = base64_encode( wp_json_encode( $response[$arg] ) );
235 }
236 }
237 }
238 }
239 }
240
241 if ( ! empty( $results['images'] ) ) {
242 // create WP compatible attachments
243 $results['images'] = $this->create_wp_remote_attachments( $results['images'], $data );
244
245 // handle last page if needed
246 $results['last'] = apply_filters( 'rl_remote_library_query_last_page', false, $results, $data );
247 } else
248 $results['last'] = true;
249 }
250 }
251
252 // send data
253 wp_send_json( $results );
254 }
255
256 /**
257 * AJAX upload image action.
258 *
259 * @return void
260 */
261 public function ajax_upload_image() {
262 $data = stripslashes_deep( $_POST );
263 $new_data = [];
264
265 // verified upload?
266 if ( current_user_can( 'upload_files' ) && isset( $data['rlnonce'], $data['image'], $data['post_id'] ) && wp_verify_nonce( $data['rlnonce'], 'rl-remote-library-upload-image' ) ) {
267 if ( ! function_exists( 'media_handle_upload' ) )
268 require_once( path_join( ABSPATH, 'wp-admin/includes/media.php' ) );
269
270 if ( ! function_exists( 'wp_handle_upload' ) )
271 require_once( path_join( ABSPATH, 'wp-admin/includes/file.php' ) );
272
273 if ( ! empty( $data['image']['url'] ) ) {
274 // get image as binary data
275 $response = wp_safe_remote_get( esc_url_raw( $data['image']['url'] ) );
276
277 // get file name
278 $file_name = basename( parse_url( $data['image']['name'], PHP_URL_PATH ) );
279
280 // check extension
281 $file_ext = strrpos( $file_name, '.' );
282
283 // no extension?
284 if ( $file_ext === false )
285 $file_name .= '.jpg';
286
287 // no errors?
288 if ( ! is_wp_error( $response ) ) {
289 $bits = wp_remote_retrieve_body( $response );
290 $loaded = wp_upload_bits( $file_name, null, $bits, current_time( 'Y/m' ) );
291
292 if ( isset( $loaded['error'] ) && $loaded['error'] ) {
293 $results = [
294 'error' => true,
295 'message' => $loaded['error']
296 ];
297 } else {
298 // simulate upload
299 $_FILES['rl-remote-image'] = [
300 'error' => 0,
301 'name' => $file_name,
302 'tmp_name' => $loaded['file'],
303 'size' => filesize( $loaded['file'] )
304 ];
305
306 // get post ID
307 $post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
308
309 // upload image, wp handle sanitization and validation here
310 $attachment_id = media_handle_upload(
311 'rl-remote-image',
312 $post_id,
313 [
314 'post_title' => $data['image']['title'],
315 'post_content' => $data['image']['description'],
316 'post_excerpt' => $data['image']['caption']
317 ],
318 [
319 'action' => 'rl_remote_library_handle_upload',
320 'test_form' => false
321 ]
322 );
323
324 // upload success?
325 if ( ! is_wp_error( $attachment_id ) ) {
326 add_post_meta( $attachment_id, '_wp_attachment_image_alt', $data['image']['alt'] );
327
328 $new_data['id'] = $attachment_id;
329 $new_data['full'] = wp_get_attachment_image_src( $attachment_id, 'full' );
330 }
331 }
332 }
333 }
334 }
335
336 // send data
337 wp_send_json( $new_data );
338 }
339
340 /**
341 * Create WP compatible attachments for JavaScript.
342 *
343 * @param array $results Requested images
344 * @param array $args Additional arguments
345 * @return array
346 */
347 public function create_wp_remote_attachments( $results, $args ) {
348 // get current user
349 $user = wp_get_current_user();
350
351 // copy results
352 $copy = $results;
353
354 // get current time
355 $time = current_time( 'timestamp' );
356
357 // get date format
358 $date_format = get_option( 'date_format' );
359
360 // format date
361 $date = date_i18n( __( 'F j Y' ), $time );
362
363 // $result is already sanitized by specific provider sanitize_result function
364 foreach ( $results as $no => $result ) {
365 // make sure those attributes are strings
366 $copy[$no]['caption'] = (string) $result['caption'];
367 $copy[$no]['description'] = (string) $result['description'];
368 $copy[$no]['title'] = (string) $result['title'];
369 $copy[$no]['filename'] = $copy[$no]['name'] = (string) $result['filename'];
370
371 // rest of attributes
372 $copy[$no]['id'] = 'rl-attachment-' . ( ( $args['preview_page'] - 1 ) * $args['preview_per_page'] + $no ) . '-' . $args['media_provider'];
373 $copy[$no]['remote_library_image'] = true;
374 $copy[$no]['author'] = $user->ID;
375 $copy[$no]['authorName'] = esc_html( $user->user_login );
376 $copy[$no]['can'] = [
377 'save' => true,
378 'remove' => false
379 ];
380 $copy[$no]['compat'] = '';
381 $copy[$no]['date'] = $time;
382 $copy[$no]['dateFormatted'] = $date;
383 $copy[$no]['delete'] = '';
384 $copy[$no]['edit'] = '';
385 $copy[$no]['update'] = '';
386 $copy[$no]['filesizeHumanReadable'] = '';
387 $copy[$no]['filesizeInBytes'] = 0;
388 $copy[$no]['icon'] = '';
389 $copy[$no]['link'] = $result['url'];
390 $copy[$no]['menuOrder'] = 0;
391 $copy[$no]['meta'] = false;
392
393 // check extension
394 $file_ext = strrpos( basename( parse_url( $result['url'], PHP_URL_PATH ) ), '.' );
395
396 // no extension?
397 if ( $file_ext === false )
398 $file_ext .= 'jpg';
399
400 if ( $file_ext === 'png' || $file_ext === 'gif' ) {
401 $copy[$no]['mime'] = 'image/' . $file_ext;
402 $copy[$no]['subtype'] = $file_ext;
403 } else {
404 $copy[$no]['mime'] = 'image/jpeg';
405 $copy[$no]['subtype'] = 'jpeg';
406 }
407
408 $copy[$no]['modified'] = $time;
409 $copy[$no]['nonces'] = [
410 'delete' => '',
411 'edit' => '',
412 'update' => ''
413 ];
414 $copy[$no]['orientation'] = $result['orientation'];
415 $copy[$no]['status'] = 'inherit';
416 $copy[$no]['type'] = 'image';
417 $copy[$no]['uploadedTo'] = 0;
418 $copy[$no]['uploadedToLink'] = '';
419 $copy[$no]['uploadedToTitle'] = '';
420 $copy[$no]['sizes'] = [
421 'medium' => [
422 'height' => $result['thumbnail_height'],
423 'width' => $result['thumbnail_width'],
424 'orientation' => $result['thumbnail_orientation'],
425 'url' => $result['thumbnail_url']
426 ],
427 'full' => [
428 'height' => $result['height'],
429 'width' => $result['width'],
430 'orientation' => $result['orientation'],
431 'url' => $result['url']
432 ]
433 ];
434 }
435
436 return (array) apply_filters( 'rl_remote_library_wp_attachments', $copy, $args );
437 }
438
439 /**
440 * Remote library media query.
441 *
442 * @param array $args
443 * @return array
444 */
445 public function get_remote_library_images( $args ) {
446 $args = stripslashes_deep( $args );
447
448 // search phrase
449 if ( isset( $args['media_search'] ) )
450 $args['media_search'] = strtolower( trim( $args['media_search'] ) );
451 else
452 $args['media_search'] = '';
453
454 // media provider
455 if ( isset( $args['media_provider'] ) )
456 $args['media_provider'] = trim( $args['media_provider'] );
457 else
458 $args['media_provider'] = 'all';
459
460 // page number
461 if ( isset( $args['preview_page'] ) )
462 $args['preview_page'] = (int) $args['preview_page'];
463 else
464 $args['preview_page'] = 1;
465
466 // number of images per page
467 if ( isset( $args['preview_per_page'] ) )
468 $args['preview_per_page'] = (int) $args['preview_per_page'];
469 else
470 $args['preview_per_page'] = 20;
471
472 // get active providers
473 $providers = $this->get_active_providers();
474
475 // prepare valid providers
476 $valid_providers = [];
477
478 if ( $args['media_provider'] === 'all' )
479 $valid_providers = $providers;
480 elseif ( in_array( $args['media_provider'], $providers, true ) )
481 $valid_providers[] = $args['media_provider'];
482
483 $results = [];
484
485 // any valid providers?
486 if ( ! empty( $valid_providers ) ) {
487 // get main instance
488 $rl = Responsive_Lightbox();
489
490 foreach ( $valid_providers as $provider_name ) {
491 if ( ! empty( $args['response_data'][$provider_name] ) ) {
492 // get provider
493 $provider = $rl->providers[$provider_name];
494
495 if ( ! empty( $provider['response_args'] ) ) {
496 foreach ( $provider['response_args'] as $arg ) {
497 if ( array_key_exists( $arg, $args['response_data'][$provider_name] ) ) {
498 $base64 = base64_decode( $args['response_data'][$provider_name][$arg] );
499
500 if ( ! empty( $base64 ) )
501 $args['response_data'][$provider_name][$arg] = json_decode( $base64, true );
502 }
503 }
504 }
505 }
506
507 // get results
508 $results = apply_filters( 'rl_remote_library_query', $results, $args['media_search'], $provider_name, $args );
509
510 // number of results
511 $nor = count( $results );
512
513 // more than requested images?
514 if ( $nor > $args['preview_per_page'] ) {
515 // get part of images
516 $results = array_slice( $results, 0, $args['preview_per_page'], true );
517
518 break;
519 // same amount of images?
520 } elseif ( $nor === $args['preview_per_page'] )
521 break;
522 }
523 }
524
525 return $results;
526 }
527 }
528