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