providers
1 year ago
class-fast-image.php
2 years ago
class-folders.php
6 months ago
class-frontend.php
6 months ago
class-galleries.php
6 months ago
class-multilang.php
2 years ago
class-remote-library-api.php
1 year ago
class-remote-library.php
6 months ago
class-settings.php
6 months ago
class-tour.php
2 years ago
class-welcome.php
2 years ago
class-widgets.php
2 years ago
functions.php
3 years ago
class-remote-library.php
713 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 | private $image_formats = [ |
| 15 | 'bmp' => 'image/bmp', |
| 16 | 'gif' => 'image/gif', |
| 17 | 'jpe' => 'image/jpeg', |
| 18 | 'jpeg' => 'image/jpeg', |
| 19 | 'jpg' => 'image/jpeg', |
| 20 | 'png' => 'image/png', |
| 21 | 'tif' => 'image/tiff', |
| 22 | 'tiff' => 'image/tiff', |
| 23 | 'webp' => 'image/webp' |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * Class constructor. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | // is remote library active? |
| 33 | if ( ! Responsive_Lightbox()->options['remote_library']['active'] ) |
| 34 | return; |
| 35 | |
| 36 | // actions |
| 37 | add_action( 'wp_ajax_rl_remote_library_query', [ $this, 'ajax_query_media' ] ); |
| 38 | add_action( 'wp_ajax_rl_upload_image', [ $this, 'ajax_upload_image' ] ); |
| 39 | add_action( 'admin_enqueue_scripts', [ $this, 'remote_library_scripts' ] ); |
| 40 | |
| 41 | // add filter to send new data to editor |
| 42 | add_filter( 'image_send_to_editor', [ $this, 'send_image_to_editor' ], 21, 8 ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Hidden field with response data for gallery preview. |
| 47 | * |
| 48 | * @param array $args Field arguments |
| 49 | * @return string |
| 50 | */ |
| 51 | public function remote_library_response_data( $args ) { |
| 52 | // get main instance |
| 53 | $rl = Responsive_Lightbox(); |
| 54 | |
| 55 | // get active providers |
| 56 | $providers = $this->get_active_providers(); |
| 57 | |
| 58 | $html = ''; |
| 59 | |
| 60 | // any providers? |
| 61 | if ( ! empty( $providers ) ) { |
| 62 | foreach ( $providers as $provider ) { |
| 63 | // get provider |
| 64 | $provider = $rl->providers[$provider]; |
| 65 | |
| 66 | // add response data arguments if needed |
| 67 | if ( ! empty( $provider['response_args'] ) ) { |
| 68 | $response = $provider['instance']->get_response_data(); |
| 69 | |
| 70 | foreach ( $provider['response_args'] as $arg ) { |
| 71 | if ( array_key_exists( $arg, $response ) ) { |
| 72 | $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>'; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return $html; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Send updated image data to editor. |
| 84 | * |
| 85 | * @param string $html The image HTML markup to send |
| 86 | * @param int $id The attachment ID |
| 87 | * @param string $caption The image caption |
| 88 | * @param string $title The image title |
| 89 | * @param string $align The image alignment |
| 90 | * @param string $url The image source URL |
| 91 | * @param string|array $size Size of image |
| 92 | * @param string $alt The image alternative text. |
| 93 | * @return string |
| 94 | */ |
| 95 | function send_image_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ) { |
| 96 | if ( $id === Responsive_Lightbox()->galleries->maybe_generate_thumbnail() && isset( $_POST['attachment'] ) && is_array( $_POST['attachment'] ) ) { |
| 97 | $attachment = wp_unslash( $_POST['attachment'] ); |
| 98 | |
| 99 | if ( isset( $attachment['remote_library_image'], $attachment['width'], $attachment['height'] ) ) { |
| 100 | $html = preg_replace( '/src=(\'|")(.*?)(\'|")/', 'src="' . ( ! empty( $attachment['rl_url'] ) ? esc_url( $attachment['rl_url'] ) : $url ) . '"', $html ); |
| 101 | $html = preg_replace( '/width=(\'|")(.*?)(\'|")/', 'width="' . (int) $attachment['width'] . '"', $html ); |
| 102 | $html = preg_replace( '/height=(\'|")(.*?)(\'|")/', 'height="' . (int) $attachment['height'] . '"', $html ); |
| 103 | $html = preg_replace( '/(\s)?id="attachment_' . (int) $id . '"/', '', $html ); |
| 104 | $html = preg_replace( '/(\s)?wp-image-' . (int) $id . '/', '', $html ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $html; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get all available providers. |
| 113 | * |
| 114 | * @return array |
| 115 | */ |
| 116 | public function get_providers() { |
| 117 | return (array) apply_filters( 'rl_get_providers', Responsive_Lightbox()->providers ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get all active providers. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function get_active_providers() { |
| 126 | $providers = $this->get_providers(); |
| 127 | $active_providers = []; |
| 128 | |
| 129 | foreach ( $providers as $provider => $data ) { |
| 130 | if ( Responsive_Lightbox()->options['remote_library'][$provider]['active'] ) |
| 131 | $active_providers[] = $provider; |
| 132 | } |
| 133 | |
| 134 | return (array) apply_filters( 'rl_get_active_providers', $active_providers ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check whether provider is active. |
| 139 | * |
| 140 | * @param string $provider Media provider |
| 141 | * @return bool |
| 142 | */ |
| 143 | public function is_active_provider( $provider ) { |
| 144 | $providers = $this->get_providers(); |
| 145 | $rl = Responsive_Lightbox(); |
| 146 | |
| 147 | return (bool) apply_filters( 'rl_is_active_provider', array_key_exists( $provider, $rl->options['remote_library'] ) && $rl->options['remote_library'][$provider]['active'], $provider ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Scripts and styles for media frame. |
| 152 | * |
| 153 | * @global string $wp_version |
| 154 | * @global string $pagenow |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | public function remote_library_scripts() { |
| 159 | global $wp_version; |
| 160 | global $pagenow; |
| 161 | |
| 162 | // display only for post edit pages |
| 163 | if ( ! ( ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) || ( version_compare( $wp_version, '5.8', '>=' ) && ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' ) ) ) ) |
| 164 | return; |
| 165 | |
| 166 | // get main instance |
| 167 | $rl = Responsive_Lightbox(); |
| 168 | |
| 169 | wp_enqueue_script( 'responsive-lightbox-remote-library-media', RESPONSIVE_LIGHTBOX_URL . '/js/admin-media.js', [ 'jquery', 'media-models', 'underscore' ], $rl->defaults['version'] ); |
| 170 | |
| 171 | // prepare script data |
| 172 | $script_data = [ |
| 173 | 'thumbnailID' => $rl->galleries->maybe_generate_thumbnail(), |
| 174 | 'postID' => get_the_ID(), |
| 175 | 'providers' => $this->get_providers(), |
| 176 | 'providersActive' => $this->get_active_providers(), |
| 177 | 'allProviders' => esc_html__( 'All providers', 'responsive-lightbox' ), |
| 178 | 'uploadAndInsert' => esc_html__( 'Upload and Insert', 'responsive-lightbox' ), |
| 179 | 'uploadAndSelect' => esc_html__( 'Upload and Select', 'responsive-lightbox' ), |
| 180 | 'filterByremoteLibrary' => esc_html__( 'Filter by remote library', 'responsive-lightbox' ), |
| 181 | 'getUploadNonce' => wp_create_nonce( 'rl-remote-library-upload-image' ), |
| 182 | 'queryNonce' => wp_create_nonce( 'rl-remote-library-query' ) |
| 183 | ]; |
| 184 | |
| 185 | wp_add_inline_script( 'responsive-lightbox-remote-library-media', 'var rlRemoteLibraryMedia = ' . wp_json_encode( $script_data ) . ";\n", 'before' ); |
| 186 | |
| 187 | // enqueue gallery |
| 188 | $rl->galleries->enqueue_gallery_scripts_styles(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * AJAX media query action. |
| 193 | * |
| 194 | * @return void |
| 195 | */ |
| 196 | public function ajax_query_media() { |
| 197 | $data = stripslashes_deep( $_POST ); |
| 198 | |
| 199 | // check user capabilities |
| 200 | if ( ! current_user_can( 'upload_files' ) ) |
| 201 | wp_send_json_error( esc_html__( 'Insufficient permissions.', 'responsive-lightbox' ) ); |
| 202 | |
| 203 | // verify nonce |
| 204 | if ( ! isset( $data['nonce'] ) || ! wp_verify_nonce( $data['nonce'], 'rl-remote-library-query' ) ) |
| 205 | wp_send_json_error( esc_html__( 'Invalid nonce.', 'responsive-lightbox' ) ); |
| 206 | |
| 207 | $results = [ |
| 208 | 'last' => false, |
| 209 | 'images' => [], |
| 210 | 'data' => [] |
| 211 | ]; |
| 212 | |
| 213 | if ( isset( $data['media_provider'], $data['media_search'], $data['media_page'] ) ) { |
| 214 | $data['media_provider'] = sanitize_key( $data['media_provider'] ); |
| 215 | |
| 216 | if ( $data['media_provider'] === 'all' || $this->is_active_provider( $data['media_provider'] ) ) { |
| 217 | $data['preview_page'] = (int) $data['media_page']; |
| 218 | $data['preview_per_page'] = 20; |
| 219 | |
| 220 | // get images |
| 221 | $results['images'] = $this->get_remote_library_images( $data ); |
| 222 | |
| 223 | // get main instance |
| 224 | $rl = Responsive_Lightbox(); |
| 225 | |
| 226 | // single provider? |
| 227 | if ( $data['media_provider'] !== 'all' ) { |
| 228 | // get provider |
| 229 | $provider = $rl->providers[$data['media_provider']]; |
| 230 | |
| 231 | // add response data arguments if needed |
| 232 | if ( ! empty( $provider['response_args'] ) ) { |
| 233 | $response = $provider['instance']->get_response_data(); |
| 234 | |
| 235 | foreach ( $provider['response_args'] as $arg ) { |
| 236 | if ( array_key_exists( $arg, $response ) ) |
| 237 | $results['data'][$provider['slug']][$arg] = base64_encode( wp_json_encode( $response[$arg] ) ); |
| 238 | } |
| 239 | } |
| 240 | } else { |
| 241 | // get active providers |
| 242 | $providers = $this->get_active_providers(); |
| 243 | |
| 244 | if ( ! empty( $providers ) ) { |
| 245 | foreach ( $providers as $provider ) { |
| 246 | // get provider |
| 247 | $provider = $rl->providers[$provider]; |
| 248 | |
| 249 | // add response data arguments if needed |
| 250 | if ( ! empty( $provider['response_args'] ) ) { |
| 251 | $response = $provider['instance']->get_response_data(); |
| 252 | |
| 253 | foreach ( $provider['response_args'] as $arg ) { |
| 254 | if ( array_key_exists( $arg, $response ) ) |
| 255 | $results['data'][$provider['slug']][$arg] = base64_encode( wp_json_encode( $response[$arg] ) ); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if ( ! empty( $results['images'] ) ) { |
| 263 | // create WP compatible attachments |
| 264 | $results['images'] = $this->create_wp_remote_attachments( $results['images'], $data ); |
| 265 | |
| 266 | // handle last page if needed |
| 267 | $results['last'] = apply_filters( 'rl_remote_library_query_last_page', false, $results, $data ); |
| 268 | } else |
| 269 | $results['last'] = true; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // send data |
| 274 | wp_send_json( $results ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * AJAX upload image action. |
| 279 | * |
| 280 | * @return void |
| 281 | */ |
| 282 | public function ajax_upload_image() { |
| 283 | // clear post data |
| 284 | $data = stripslashes_deep( $_POST ); |
| 285 | |
| 286 | // default result |
| 287 | $result = [ |
| 288 | 'id' => 0, |
| 289 | 'full' => [ '', 0, 0, false ], |
| 290 | 'error' => false, |
| 291 | 'message' => '' |
| 292 | ]; |
| 293 | |
| 294 | // verified upload? |
| 295 | if ( current_user_can( 'upload_files' ) && isset( $data['rlnonce'], $data['image'], $data['post_id'] ) && wp_verify_nonce( $data['rlnonce'], 'rl-remote-library-upload-image' ) ) { |
| 296 | // include required files if needed |
| 297 | if ( ! function_exists( 'media_handle_upload' ) ) |
| 298 | require_once( path_join( ABSPATH, 'wp-admin/includes/media.php' ) ); |
| 299 | |
| 300 | if ( ! function_exists( 'wp_handle_upload' ) ) |
| 301 | require_once( path_join( ABSPATH, 'wp-admin/includes/file.php' ) ); |
| 302 | |
| 303 | // get media provider |
| 304 | $media_provider = ! empty( $data['image']['media_provider'] ) ? sanitize_key( $data['image']['media_provider'] ) : ''; |
| 305 | |
| 306 | // get active providers |
| 307 | $providers = $this->get_active_providers(); |
| 308 | |
| 309 | if ( in_array( $media_provider, $providers, true ) ) { |
| 310 | // get image formats |
| 311 | $image_formats = $this->get_allowed_image_formats( $media_provider ); |
| 312 | |
| 313 | if ( ! empty( $data['image']['url'] ) && ! empty( $data['image']['mime'] ) && ! empty( $data['image']['subtype'] ) && array_key_exists( $data['image']['subtype'], $image_formats ) ) { |
| 314 | // get image url |
| 315 | $image_url = esc_url_raw( $data['image']['url'] ); |
| 316 | |
| 317 | // get allowed hosts |
| 318 | $hosts = $this->get_allowed_hosts( $media_provider ); |
| 319 | |
| 320 | if ( ! empty( $hosts ) ) { |
| 321 | $valid_host = false; |
| 322 | |
| 323 | // get image host |
| 324 | $image_host = parse_url( $image_url, PHP_URL_HOST ); |
| 325 | |
| 326 | // check allowed hosts |
| 327 | foreach ( $hosts as $host ) { |
| 328 | // invalid host? |
| 329 | if ( strpos( $image_host, $host ) !== false ) { |
| 330 | $valid_host = true; |
| 331 | |
| 332 | // no need to check rest of the hosts |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } else |
| 337 | $valid_host = true; |
| 338 | |
| 339 | if ( $valid_host ) { |
| 340 | // get max image size (ensure at least 1MB) |
| 341 | $max_size = max( 1, absint( Responsive_Lightbox()->options['remote_library']['max_image_size'] ) ) * 1024 * 1024; |
| 342 | |
| 343 | // check image size via HEAD request |
| 344 | $head_response = wp_remote_head( $image_url ); |
| 345 | $skip_size_check = false; |
| 346 | |
| 347 | if ( is_wp_error( $head_response ) ) { |
| 348 | $skip_size_check = true; |
| 349 | } else { |
| 350 | $content_length = wp_remote_retrieve_header( $head_response, 'content-length' ); |
| 351 | |
| 352 | if ( $content_length && (int) $content_length > $max_size ) { |
| 353 | $result['error'] = true; |
| 354 | $result['message'] = __( 'Image size exceeds maximum allowed size.', 'responsive-lightbox' ); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if ( empty( $result['error'] ) ) { |
| 359 | // get image as binary data with timeout |
| 360 | $response = wp_safe_remote_get( $image_url, [ 'timeout' => 30 ] ); |
| 361 | |
| 362 | // no errors? |
| 363 | if ( ! is_wp_error( $response ) ) { |
| 364 | // get image binary data |
| 365 | $image_bits = wp_remote_retrieve_body( $response ); |
| 366 | |
| 367 | // check body size if HEAD was skipped or as fallback |
| 368 | $body_size = strlen( $image_bits ); |
| 369 | if ( $skip_size_check || $body_size > $max_size ) { |
| 370 | if ( $body_size > $max_size ) { |
| 371 | $result['error'] = true; |
| 372 | $result['message'] = __( 'Image size exceeds maximum allowed size.', 'responsive-lightbox' ); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if ( empty( $result['error'] ) ) { |
| 377 | // get sanitized file name |
| 378 | $file_name = sanitize_file_name( pathinfo( $data['image']['name'], PATHINFO_BASENAME ) ); |
| 379 | |
| 380 | // get file extension |
| 381 | $file_ext = pathinfo( $file_name, PATHINFO_EXTENSION ); |
| 382 | |
| 383 | // no extension? |
| 384 | if ( $file_ext === '' || ! array_key_exists( $file_ext, $image_formats ) ) { |
| 385 | $file_name .= '.jpg'; |
| 386 | $file_ext = 'jpg'; |
| 387 | } |
| 388 | |
| 389 | // simple mime checking |
| 390 | $check = wp_check_filetype( $file_name ); |
| 391 | |
| 392 | if ( $check['type'] === $data['image']['mime'] && $check['ext'] !== false && array_key_exists( $file_ext, $image_formats ) ) { |
| 393 | // upload image |
| 394 | $uploaded_image = wp_upload_bits( $file_name, null, $image_bits, current_time( 'Y/m' ) ); |
| 395 | |
| 396 | if ( isset( $uploaded_image['error'] ) && $uploaded_image['error'] ) { |
| 397 | $result['error'] = true; |
| 398 | $result['message'] = $uploaded_image['error']; |
| 399 | } else { |
| 400 | // get file name |
| 401 | $file_name = pathinfo( $uploaded_image['file'], PATHINFO_BASENAME ); |
| 402 | |
| 403 | // simulate upload |
| 404 | $_FILES['rl-remote-image'] = [ |
| 405 | 'error' => 0, |
| 406 | 'name' => $file_name, |
| 407 | 'tmp_name' => $uploaded_image['file'], |
| 408 | 'size' => filesize( $uploaded_image['file'] ) |
| 409 | ]; |
| 410 | |
| 411 | // get post id |
| 412 | $post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0; |
| 413 | |
| 414 | // more reliable mime type checking |
| 415 | $check = wp_check_filetype_and_ext( $uploaded_image['file'], $file_name ); |
| 416 | |
| 417 | // correct mime type and extension? |
| 418 | if ( strpos( $data['image']['mime'], 'image/' ) === 0 && $check['type'] === $data['image']['mime'] && $check['ext'] !== false && wp_get_image_mime( $uploaded_image['file'] ) === $check['type'] ) { |
| 419 | // upload image, wp handle sanitization and validation here |
| 420 | $attachment_id = media_handle_upload( |
| 421 | 'rl-remote-image', |
| 422 | $post_id, |
| 423 | [ |
| 424 | 'post_title' => empty( $data['image']['title'] ) ? $file_name : $data['image']['title'], |
| 425 | 'post_content' => empty( $data['image']['description'] ) ? '' : $data['image']['description'], |
| 426 | 'post_excerpt' => empty( $data['image']['caption'] ) ? '' : $data['image']['caption'] |
| 427 | ], |
| 428 | [ |
| 429 | 'action' => 'rl_remote_library_handle_upload', |
| 430 | 'test_form' => false |
| 431 | ] |
| 432 | ); |
| 433 | |
| 434 | // upload success? |
| 435 | if ( ! is_wp_error( $attachment_id ) ) { |
| 436 | add_post_meta( $attachment_id, '_wp_attachment_image_alt', empty( $data['image']['alt'] ) ? '' : $data['image']['alt'] ); |
| 437 | |
| 438 | $result['id'] = $attachment_id; |
| 439 | $result['full'] = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 440 | } else { |
| 441 | $result['error'] = true; |
| 442 | $result['message'] = $attachment_id->get_error_message(); |
| 443 | } |
| 444 | // file still exists? |
| 445 | } elseif ( file_exists( $uploaded_image['file'] ) ) { |
| 446 | $result['error'] = true; |
| 447 | $result['message'] = __( 'Invalid MIME type', 'responsive-lightbox' ); |
| 448 | |
| 449 | // delete file |
| 450 | wp_delete_file( $uploaded_image['file'] ); |
| 451 | } |
| 452 | } |
| 453 | } else { |
| 454 | $result['error'] = true; |
| 455 | $result['message'] = __( 'Invalid image type', 'responsive-lightbox' ); |
| 456 | } |
| 457 | } else { |
| 458 | $result['error'] = true; |
| 459 | $result['message'] = $response->get_error_message(); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } else { |
| 464 | $result['error'] = true; |
| 465 | $result['message'] = __( 'Invalid host', 'responsive-lightbox' ); |
| 466 | } |
| 467 | } else { |
| 468 | $result['error'] = true; |
| 469 | $result['message'] = __( 'Missing or invalid image data', 'responsive-lightbox' ); |
| 470 | } |
| 471 | } else { |
| 472 | $result['error'] = true; |
| 473 | $result['message'] = __( 'Invalid media provider', 'responsive-lightbox' ); |
| 474 | } |
| 475 | } else { |
| 476 | $result['error'] = true; |
| 477 | $result['message'] = __( 'Access denied', 'responsive-lightbox' ); |
| 478 | } |
| 479 | |
| 480 | // send data |
| 481 | wp_send_json( $result ); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Create WP compatible attachments for JavaScript. |
| 486 | * |
| 487 | * @param array $results Requested images |
| 488 | * @param array $args Additional arguments |
| 489 | * @return array |
| 490 | */ |
| 491 | public function create_wp_remote_attachments( $results, $args ) { |
| 492 | // get current user |
| 493 | $user = wp_get_current_user(); |
| 494 | |
| 495 | // copy results |
| 496 | $copy = $results; |
| 497 | |
| 498 | // get current time |
| 499 | $time = current_time( 'timestamp' ); |
| 500 | |
| 501 | // get date format |
| 502 | $date_format = get_option( 'date_format' ); |
| 503 | |
| 504 | // format date |
| 505 | $date = date_i18n( __( 'F j Y' ), $time ); |
| 506 | |
| 507 | // $result is already sanitized by specific provider sanitize_result function |
| 508 | foreach ( $results as $no => $result ) { |
| 509 | // make sure those attributes are strings |
| 510 | $copy[$no]['caption'] = (string) $result['caption']; |
| 511 | $copy[$no]['description'] = (string) $result['description']; |
| 512 | $copy[$no]['title'] = (string) $result['title']; |
| 513 | $copy[$no]['filename'] = $copy[$no]['name'] = (string) $result['filename']; |
| 514 | |
| 515 | // rest of attributes |
| 516 | $copy[$no]['id'] = 'rl-attachment-' . ( ( $args['preview_page'] - 1 ) * $args['preview_per_page'] + $no ) . '-' . $args['media_provider']; |
| 517 | $copy[$no]['remote_library_image'] = true; |
| 518 | $copy[$no]['author'] = $user->ID; |
| 519 | $copy[$no]['authorName'] = esc_html( $user->user_login ); |
| 520 | $copy[$no]['can'] = [ |
| 521 | 'save' => true, |
| 522 | 'remove' => false |
| 523 | ]; |
| 524 | $copy[$no]['compat'] = ''; |
| 525 | $copy[$no]['date'] = $time; |
| 526 | $copy[$no]['dateFormatted'] = $date; |
| 527 | $copy[$no]['delete'] = ''; |
| 528 | $copy[$no]['edit'] = ''; |
| 529 | $copy[$no]['update'] = ''; |
| 530 | $copy[$no]['filesizeHumanReadable'] = ''; |
| 531 | $copy[$no]['filesizeInBytes'] = 0; |
| 532 | $copy[$no]['icon'] = ''; |
| 533 | $copy[$no]['link'] = $result['url']; |
| 534 | $copy[$no]['menuOrder'] = 0; |
| 535 | $copy[$no]['meta'] = false; |
| 536 | |
| 537 | // check extension |
| 538 | $file_ext = pathinfo( $result['url'], PATHINFO_EXTENSION ); |
| 539 | |
| 540 | // get image formats |
| 541 | $image_formats = $this->get_allowed_image_formats( $args['media_provider'] ); |
| 542 | |
| 543 | if ( array_key_exists( $file_ext, $image_formats ) ) { |
| 544 | $copy[$no]['mime'] = $image_formats[$file_ext]; |
| 545 | $copy[$no]['subtype'] = $file_ext; |
| 546 | } else { |
| 547 | $copy[$no]['mime'] = 'image/jpeg'; |
| 548 | $copy[$no]['subtype'] = 'jpg'; |
| 549 | } |
| 550 | |
| 551 | $copy[$no]['modified'] = $time; |
| 552 | $copy[$no]['nonces'] = [ |
| 553 | 'delete' => '', |
| 554 | 'edit' => '', |
| 555 | 'update' => '' |
| 556 | ]; |
| 557 | $copy[$no]['orientation'] = $result['orientation']; |
| 558 | $copy[$no]['status'] = 'inherit'; |
| 559 | $copy[$no]['type'] = 'image'; |
| 560 | $copy[$no]['uploadedTo'] = 0; |
| 561 | $copy[$no]['uploadedToLink'] = ''; |
| 562 | $copy[$no]['uploadedToTitle'] = ''; |
| 563 | $copy[$no]['sizes'] = [ |
| 564 | 'medium' => [ |
| 565 | 'height' => $result['thumbnail_height'], |
| 566 | 'width' => $result['thumbnail_width'], |
| 567 | 'orientation' => $result['thumbnail_orientation'], |
| 568 | 'url' => $result['thumbnail_url'] |
| 569 | ], |
| 570 | 'full' => [ |
| 571 | 'height' => $result['height'], |
| 572 | 'width' => $result['width'], |
| 573 | 'orientation' => $result['orientation'], |
| 574 | 'url' => $result['url'] |
| 575 | ] |
| 576 | ]; |
| 577 | } |
| 578 | |
| 579 | return (array) apply_filters( 'rl_remote_library_wp_attachments', $copy, $args ); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Remote library media query. |
| 584 | * |
| 585 | * @param array $args |
| 586 | * @return array |
| 587 | */ |
| 588 | public function get_remote_library_images( $args ) { |
| 589 | $args = stripslashes_deep( $args ); |
| 590 | |
| 591 | // search phrase |
| 592 | if ( isset( $args['media_search'] ) ) |
| 593 | $args['media_search'] = strtolower( trim( $args['media_search'] ) ); |
| 594 | else |
| 595 | $args['media_search'] = ''; |
| 596 | |
| 597 | // media provider |
| 598 | if ( isset( $args['media_provider'] ) ) |
| 599 | $args['media_provider'] = trim( $args['media_provider'] ); |
| 600 | else |
| 601 | $args['media_provider'] = 'all'; |
| 602 | |
| 603 | // page number |
| 604 | if ( isset( $args['preview_page'] ) ) |
| 605 | $args['preview_page'] = (int) $args['preview_page']; |
| 606 | else |
| 607 | $args['preview_page'] = 1; |
| 608 | |
| 609 | // number of images per page |
| 610 | if ( isset( $args['preview_per_page'] ) ) |
| 611 | $args['preview_per_page'] = (int) $args['preview_per_page']; |
| 612 | else |
| 613 | $args['preview_per_page'] = 20; |
| 614 | |
| 615 | // get active providers |
| 616 | $providers = $this->get_active_providers(); |
| 617 | |
| 618 | // prepare valid providers |
| 619 | $valid_providers = []; |
| 620 | |
| 621 | if ( $args['media_provider'] === 'all' ) |
| 622 | $valid_providers = $providers; |
| 623 | elseif ( in_array( $args['media_provider'], $providers, true ) ) |
| 624 | $valid_providers[] = $args['media_provider']; |
| 625 | |
| 626 | $results = []; |
| 627 | |
| 628 | // any valid providers? |
| 629 | if ( ! empty( $valid_providers ) ) { |
| 630 | // get main instance |
| 631 | $rl = Responsive_Lightbox(); |
| 632 | |
| 633 | foreach ( $valid_providers as $provider_name ) { |
| 634 | if ( ! empty( $args['response_data'][$provider_name] ) ) { |
| 635 | // get provider |
| 636 | $provider = $rl->providers[$provider_name]; |
| 637 | |
| 638 | if ( ! empty( $provider['response_args'] ) ) { |
| 639 | foreach ( $provider['response_args'] as $arg ) { |
| 640 | if ( array_key_exists( $arg, $args['response_data'][$provider_name] ) ) { |
| 641 | $base64 = base64_decode( $args['response_data'][$provider_name][$arg] ); |
| 642 | |
| 643 | if ( ! empty( $base64 ) ) |
| 644 | $args['response_data'][$provider_name][$arg] = json_decode( $base64, true ); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // get results |
| 651 | $results = apply_filters( 'rl_remote_library_query', $results, $args['media_search'], $provider_name, $args ); |
| 652 | |
| 653 | // number of results |
| 654 | $nor = count( $results ); |
| 655 | |
| 656 | // more than requested images? |
| 657 | if ( $nor > $args['preview_per_page'] ) { |
| 658 | // get part of images |
| 659 | $results = array_slice( $results, 0, $args['preview_per_page'], true ); |
| 660 | |
| 661 | break; |
| 662 | // same amount of images? |
| 663 | } elseif ( $nor === $args['preview_per_page'] ) |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return $results; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Get allowed hosts. |
| 673 | * |
| 674 | * @param string $provider |
| 675 | * @return array |
| 676 | */ |
| 677 | public function get_allowed_hosts( $provider ) { |
| 678 | // get active providers |
| 679 | $providers = $this->get_active_providers(); |
| 680 | |
| 681 | if ( in_array( $provider, $providers, true ) ) { |
| 682 | // get available provider host |
| 683 | $hosts = Responsive_Lightbox()->providers[$provider]['instance']->get_allowed_hosts(); |
| 684 | } else |
| 685 | $hosts = []; |
| 686 | |
| 687 | return $hosts; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Get allowed image formats. |
| 692 | * |
| 693 | * @param string $provider |
| 694 | * @return array |
| 695 | */ |
| 696 | public function get_allowed_image_formats( $provider = 'all' ) { |
| 697 | if ( $provider === 'all' ) { |
| 698 | $image_formats = $this->image_formats; |
| 699 | } else { |
| 700 | // get active providers |
| 701 | $providers = $this->get_active_providers(); |
| 702 | |
| 703 | if ( in_array( $provider, $providers, true ) ) { |
| 704 | // get available provider image formats |
| 705 | $image_formats = Responsive_Lightbox()->providers[$provider]['instance']->get_allowed_formats(); |
| 706 | } else |
| 707 | $image_formats = []; |
| 708 | } |
| 709 | |
| 710 | return $image_formats; |
| 711 | } |
| 712 | } |
| 713 |