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