providers
5 years ago
class-fast-image.php
8 years ago
class-folders-walker.php
7 years ago
class-folders.php
5 years ago
class-frontend.php
5 years ago
class-galleries.php
5 years ago
class-multilang.php
5 years ago
class-remote-library-api.php
5 years ago
class-remote-library.php
5 years ago
class-settings.php
5 years ago
class-tour.php
5 years ago
class-welcome.php
5 years ago
class-widgets.php
5 years ago
functions.php
5 years ago
class-remote-library.php
516 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 $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-models', 'underscore' ), $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 | 'uploadAndSelect' => __( 'Upload and Select', 'responsive-lightbox' ), |
| 168 | 'filterByremoteLibrary' => __( 'Filter by remote library', 'responsive-lightbox' ), |
| 169 | 'getUploadNonce' => wp_create_nonce( 'rl-remote-library-upload-image' ) |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | // enqueue gallery |
| 174 | $rl->galleries->enqueue_gallery_scripts_styles(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * AJAX media query action. |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | public function ajax_query_media() { |
| 183 | $data = stripslashes_deep( $_POST ); |
| 184 | $results = array( |
| 185 | 'last' => false, |
| 186 | 'images' => array(), |
| 187 | 'data' => array() |
| 188 | ); |
| 189 | |
| 190 | if ( isset( $data['media_provider'], $data['media_search'], $data['media_page'] ) && ( $data['media_provider'] === 'all' || $this->is_active_provider( $data['media_provider'] ) ) ) { |
| 191 | $data['preview_page'] = (int) $data['media_page']; |
| 192 | $data['preview_per_page'] = 20; |
| 193 | |
| 194 | // get images |
| 195 | $results['images'] = $this->get_remote_library_images( $data ); |
| 196 | |
| 197 | // get main instance |
| 198 | $rl = Responsive_Lightbox(); |
| 199 | |
| 200 | // single provider? |
| 201 | if ( $data['media_provider'] !== 'all' ) { |
| 202 | // get provider |
| 203 | $provider = $rl->providers[$data['media_provider']]; |
| 204 | |
| 205 | // add response data arguments if needed |
| 206 | if ( ! empty( $provider['response_args'] ) ) { |
| 207 | $response = $provider['instance']->get_response_data(); |
| 208 | |
| 209 | foreach ( $provider['response_args'] as $arg ) { |
| 210 | if ( array_key_exists( $arg, $response ) ) |
| 211 | $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) ); |
| 212 | } |
| 213 | } |
| 214 | } else { |
| 215 | // get active providers |
| 216 | $providers = $this->get_active_providers(); |
| 217 | |
| 218 | if ( ! empty( $providers ) ) { |
| 219 | foreach ( $providers as $provider ) { |
| 220 | // get provider |
| 221 | $provider = $rl->providers[$provider]; |
| 222 | |
| 223 | // add response data arguments if needed |
| 224 | if ( ! empty( $provider['response_args'] ) ) { |
| 225 | $response = $provider['instance']->get_response_data(); |
| 226 | |
| 227 | foreach ( $provider['response_args'] as $arg ) { |
| 228 | if ( array_key_exists( $arg, $response ) ) |
| 229 | $results['data'][$provider['slug']][$arg] = base64_encode( json_encode( $response[$arg] ) ); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if ( ! empty( $results['images'] ) ) { |
| 237 | // create WP compatible attachments |
| 238 | $results['images'] = $this->create_wp_remote_attachments( $results['images'], $data ); |
| 239 | |
| 240 | // handle last page if needed |
| 241 | $results['last'] = apply_filters( 'rl_remote_library_query_last_page', false, $results, $data ); |
| 242 | } else |
| 243 | $results['last'] = true; |
| 244 | } |
| 245 | |
| 246 | // send JSON |
| 247 | wp_send_json( $results ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * AJAX upload image action. |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public function ajax_upload_image() { |
| 256 | $data = stripslashes_deep( $_POST ); |
| 257 | $new_data = array(); |
| 258 | |
| 259 | // verified upload? |
| 260 | if ( current_user_can( 'upload_files' ) && isset( $data['rlnonce'], $data['image'], $data['post_id'] ) && wp_verify_nonce( $data['rlnonce'], 'rl-remote-library-upload-image' ) ) { |
| 261 | if ( ! function_exists( 'media_handle_upload' ) ) |
| 262 | require_once( path_join( ABSPATH, 'wp-admin/includes/media.php' ) ); |
| 263 | |
| 264 | if ( ! function_exists( 'wp_handle_upload' ) ) |
| 265 | require_once( path_join( ABSPATH, 'wp-admin/includes/file.php' ) ); |
| 266 | |
| 267 | if ( ! empty( $data['image']['url'] ) ) { |
| 268 | // get image as binary data |
| 269 | $response = wp_safe_remote_get( esc_url_raw( $data['image']['url'] ) ); |
| 270 | |
| 271 | // get file name |
| 272 | $file_name = basename( parse_url( $data['image']['name'], PHP_URL_PATH ) ); |
| 273 | |
| 274 | // check extension |
| 275 | $file_ext = strrpos( $file_name, '.' ); |
| 276 | |
| 277 | // no extension? |
| 278 | if ( $file_ext === false ) |
| 279 | $file_name .= '.jpg'; |
| 280 | |
| 281 | // no errors? |
| 282 | if ( ! is_wp_error( $response ) ) { |
| 283 | $bits = wp_remote_retrieve_body( $response ); |
| 284 | $loaded = wp_upload_bits( $file_name, null, $bits, current_time( 'Y/m' ) ); |
| 285 | |
| 286 | if ( isset( $loaded['error'] ) && $loaded['error'] ) { |
| 287 | $results = array( |
| 288 | 'error' => true, |
| 289 | 'message' => $loaded['error'] |
| 290 | ); |
| 291 | } else { |
| 292 | // simulate upload |
| 293 | $_FILES['rl-remote-image'] = array( |
| 294 | 'error' => 0, |
| 295 | 'name' => $file_name, |
| 296 | 'tmp_name' => $loaded['file'], |
| 297 | 'size' => filesize( $loaded['file'] ) |
| 298 | ); |
| 299 | |
| 300 | // get post ID |
| 301 | $post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0; |
| 302 | |
| 303 | // upload image |
| 304 | $attachment_id = media_handle_upload( |
| 305 | 'rl-remote-image', |
| 306 | $post_id, |
| 307 | array( |
| 308 | 'post_title' => $data['image']['title'], |
| 309 | 'post_content' => $data['image']['description'], |
| 310 | 'post_excerpt' => $data['image']['caption'] |
| 311 | ), array( |
| 312 | 'action' => 'rl_remote_library_handle_upload', |
| 313 | 'test_form' => false |
| 314 | ) |
| 315 | ); |
| 316 | |
| 317 | // upload success? |
| 318 | if ( ! is_wp_error( $attachment_id ) ) { |
| 319 | add_post_meta( $attachment_id, '_wp_attachment_image_alt', $data['image']['alt'] ); |
| 320 | |
| 321 | $new_data['id'] = $attachment_id; |
| 322 | $new_data['full'] = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // send JSON |
| 330 | wp_send_json( $new_data ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Create WP compatible attachments for JavaScript. |
| 335 | * |
| 336 | * @param array $results Requested images |
| 337 | * @param array $args Additional arguments |
| 338 | * @return array Compatible attachments |
| 339 | */ |
| 340 | public function create_wp_remote_attachments( $results, $args ) { |
| 341 | $user = wp_get_current_user(); |
| 342 | $copy = $results; |
| 343 | $time = current_time( 'timestamp' ); |
| 344 | $date_format = get_option( 'date_format' ); |
| 345 | $date = date_i18n( __( 'F j Y' ), $time ); |
| 346 | |
| 347 | foreach ( $results as $no => $result ) { |
| 348 | // detect orientation |
| 349 | $orientation = $result['width'] > $result['height'] ? 'landscape' : 'portrait'; |
| 350 | |
| 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'] = $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 | // 'thumbnail' => array( |
| 408 | // 'height' => $result['thumbnail_height'], |
| 409 | // 'width' => $result['thumbnail_width'], |
| 410 | // 'orientation' => $orientation, |
| 411 | // 'url' => $result['thumbnail_url'] |
| 412 | // ), |
| 413 | 'medium' => array( |
| 414 | 'height' => $result['thumbnail_height'], |
| 415 | 'width' => $result['thumbnail_width'], |
| 416 | 'orientation' => $orientation, |
| 417 | 'url' => $result['thumbnail_url'] |
| 418 | ), |
| 419 | 'full' => array( |
| 420 | 'height' => $result['height'], |
| 421 | 'width' => $result['width'], |
| 422 | 'orientation' => $orientation, |
| 423 | 'url' => $result['url'] |
| 424 | ) |
| 425 | ); |
| 426 | } |
| 427 | |
| 428 | return apply_filters( 'rl_remote_library_wp_attachments', $copy, $args ); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Remote library media query. |
| 433 | * |
| 434 | * @param array $args Arguments |
| 435 | * @return array Images |
| 436 | */ |
| 437 | public function get_remote_library_images( $args ) { |
| 438 | $args = stripslashes_deep( $args ); |
| 439 | |
| 440 | // search phrase |
| 441 | if ( isset( $args['media_search'] ) ) |
| 442 | $args['media_search'] = strtolower( trim( $args['media_search'] ) ); |
| 443 | else |
| 444 | $args['media_search'] = ''; |
| 445 | |
| 446 | // media provider |
| 447 | if ( isset( $args['media_provider'] ) ) |
| 448 | $args['media_provider'] = trim( $args['media_provider'] ); |
| 449 | else |
| 450 | $args['media_provider'] = 'all'; |
| 451 | |
| 452 | // page number |
| 453 | if ( isset( $args['preview_page'] ) ) |
| 454 | $args['preview_page'] = (int) $args['preview_page']; |
| 455 | else |
| 456 | $args['preview_page'] = 1; |
| 457 | |
| 458 | // number of images per page |
| 459 | if ( isset( $args['preview_per_page'] ) ) |
| 460 | $args['preview_per_page'] = (int) $args['preview_per_page']; |
| 461 | else |
| 462 | $args['preview_per_page'] = 20; |
| 463 | |
| 464 | // get active providers |
| 465 | $providers = $this->get_active_providers(); |
| 466 | |
| 467 | // prepare valid providers |
| 468 | $valid_providers = array(); |
| 469 | |
| 470 | if ( $args['media_provider'] === 'all' ) |
| 471 | $valid_providers = $providers; |
| 472 | elseif ( in_array( $args['media_provider'], $providers, true ) ) |
| 473 | $valid_providers[] = $args['media_provider']; |
| 474 | |
| 475 | $results = array(); |
| 476 | |
| 477 | // any valid providers? |
| 478 | if ( ! empty( $valid_providers ) ) { |
| 479 | // get main instance |
| 480 | $rl = Responsive_Lightbox(); |
| 481 | |
| 482 | foreach ( $valid_providers as $provider_name ) { |
| 483 | if ( ! empty( $args['response_data'][$provider_name] ) ) { |
| 484 | // get provider |
| 485 | $provider = $rl->providers[$provider_name]; |
| 486 | |
| 487 | if ( ! empty( $provider['response_args'] ) ) { |
| 488 | foreach ( $provider['response_args'] as $arg ) { |
| 489 | if ( array_key_exists( $arg, $args['response_data'][$provider_name] ) ) { |
| 490 | $base64 = base64_decode( $args['response_data'][$provider_name][$arg] ); |
| 491 | |
| 492 | if ( ! empty( $base64 ) ) |
| 493 | $args['response_data'][$provider_name][$arg] = json_decode( $base64, true ); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | $results = apply_filters( 'rl_remote_library_query', $results, $args['media_search'], $provider_name, $args ); |
| 500 | $nor = count( $results ); |
| 501 | |
| 502 | // more than requested images? |
| 503 | if ( $nor > $args['preview_per_page'] ) { |
| 504 | // get part of images |
| 505 | $results = array_slice( $results, 0, $args['preview_per_page'], true ); |
| 506 | |
| 507 | break; |
| 508 | // same amount of images? |
| 509 | } elseif ( $nor === $args['preview_per_page'] ) |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return $results; |
| 515 | } |
| 516 | } |