PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
← All changes | includes/filters/content.php +103 -49 1.0.01.4.1 View file →
@@ -77,14 +77,14 @@
77 77 */
78 78 public function filter_post_backward( $content ) {
79 79
80 80 // Enable backword compatibility
81 - add_filter( 'wpmcs_enable_backward_url_repalcement', [ $this, 'enable_backward_url_check' ] );
81 + add_filter( 'wpmcs_enable_backward_url_replacement', [ $this, 'enable_backward_url_check' ] );
82 82
83 83 $content = $this->filter_post( $content );
84 84
85 85 // remove backword compatibility after process
86 - remove_filter( 'wpmcs_enable_backward_url_repalcement', [ $this, 'enable_backward_url_check' ] );
86 + remove_filter( 'wpmcs_enable_backward_url_replacement', [ $this, 'enable_backward_url_check' ] );
87 87
88 88 return $content;
89 89 }
90 90
@@ -213,13 +213,12 @@
213 213 * @param string $content
214 214 * @return string
215 215 */
216 216 protected function pre_filter_content( $content ) {
217 - global $wpmcsService;
218 217 $uploads = wp_upload_dir();
219 218 $base_url = Utils::remove_scheme( $uploads['baseurl'] );
220 219
221 - return $wpmcsService->remove_query_strings( $content, $base_url );
220 + return Utils::remove_query_strings( $content, $base_url );
222 221 }
223 222
224 223 /**
225 224 * Get URLs from img src.
@@ -259,9 +258,9 @@
259 258 if ( ! Utils::is_file_url( $url ) ) {
260 259 continue;
261 260 }
262 261
263 - if ( ! $this->url_needs_replacing( $url ) ) {
262 + if ( ! self::url_needs_replacing( $url ) ) {
264 263 // URL already correct, skip
265 264 continue;
266 265 }
267 266
@@ -266,9 +265,12 @@
266 265 }
267 266
268 267 $bare_url = Utils::reduce_url( $url );
269 268
270 - $item_sources[ $bare_url ] = absint( $class_id[1] );
269 + $item_sources[ $bare_url ] = [
270 + 'id' => absint( $class_id[1] ),
271 + 'source_type' => 'media_library'
272 + ];
271 273 }
272 274
273 275 if ( count( $item_sources ) > 1 ) {
274 276 /*
@@ -276,9 +278,9 @@
276 278 *
277 279 * To avoid making a database call for each image, a single query
278 280 * warms the object cache with the meta information for all images.
279 281 */
280 - update_meta_cache( 'post', array_unique( $item_sources ) );
282 + update_meta_cache( 'post', array_unique(array_column($item_sources, 'id')));
281 283 }
282 284
283 285 foreach ( $item_sources as $url => $item_source ) {
284 286 if ( ! $this->item_matches_src( $item_source, $url ) ) {
@@ -298,26 +300,25 @@
298 300 * @param string $url
299 301 *
300 302 * @return bool
301 303 */
302 - public function url_needs_replacing( $url ) {
303 - $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_repalcement', false);
304 + public static function url_needs_replacing( $url ) {
305 + $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false);
304 306
305 307 if( $reverse_compatibility ) {
306 308 if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) !== $url ) {
309 + // Local URL, no replacement needed.
307 310 return false;
308 311 }
309 312
310 313 if ( str_replace( Filter::get_remote_domains(), '', $url ) === $url ) {
314 + // Not a known remote URL, no replacement needed.
311 315 return false;
312 316 }
313 317 } else {
314 318 if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) {
315 319 // Remote URL, no replacement needed
316 - if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) {
317 - // Remote URL, no replacement needed
318 - return false;
319 - }
320 + return false;
320 321 }
321 322 }
322 323
323 324 // Server URL, perform replacement
@@ -332,42 +333,46 @@
332 333 *
333 334 * @return bool
334 335 */
335 336 public function item_matches_src( $item_source, $url ): bool {
336 - global $wpmcsItem;
337 337 $upload_dir = wp_get_upload_dir();
338 - $item = $wpmcsItem->get( $item_source );
338 + $wpmcsItem = Item::instance();
339 339
340 + $backward_replacement = apply_filters('wpmcs_enable_backward_url_replacement', false);
340 341
341 - $backward_replacement = apply_filters('wpmcs_enable_backward_url_repalcement', false);
342 -
343 342 if (
344 - Utils::is_empty($item) ||
345 - get_post_type( $item_source ) !== 'attachment'
343 + $this->is_empty_item_source($item_source) ||
344 + 'media_library' !== $item_source['source_type'] ||
345 + get_post_type( $item_source['id'] ) !== 'attachment'
346 346 ) {
347 347 return false;
348 348 }
349 -
349 +
350 + $item = $wpmcsItem->get( $item_source['id'], $item_source['source_type'] );
351 +
352 + if(!$item) return false;
353 +
350 354 $bare_url = [];
351 355
352 356 $bare_url[] = $backward_replacement
353 - ? Utils::reduce_url($wpmcsItem->get_url( $item_source ))
357 + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'full', $item_source['source_type'] ))
354 358 : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['source_path']);
355 359
356 360
357 - $extras = $wpmcsItem->get_extras( $item_source );
358 361
359 - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) {
362 + if(isset($item['original_source_path']) && !Utils::is_empty($item['original_source_path'])){
360 363 $bare_url[] = $backward_replacement
361 - ? Utils::reduce_url($wpmcsItem->get_url( $item_source, 'original' ))
362 - : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$extras['original']['source_path']);
364 + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'original', $item_source['source_type'] ))
365 + : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['original_source_path']);
363 366 }
364 367
368 + $extras = $wpmcsItem->get_extras( $item_source['id'], false, $item_source['source_type'] );
369 +
365 370 if(isset($extras['sizes']) && !Utils::is_empty($extras['sizes'])) {
366 371 foreach($extras['sizes'] as $size => $sub_image) {
367 372 if(!Utils::is_empty($sub_image)){
368 373 $bare_url[] = $backward_replacement
369 - ? Utils::reduce_url($wpmcsItem->get_url( $item_source, $size ))
374 + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], $size, $item_source['source_type'] ))
370 375 : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$sub_image['source_path']);
371 376 }
372 377 }
373 378 }
@@ -389,25 +394,28 @@
389 394 * @param string $find
390 395 * @param array $to_cache
391 396 */
392 397 protected function push_to_url_pairs( &$url_pairs, $item_source, $find, &$to_cache ) {
393 - global $wpmcsItem;
394 -
395 398 $upload_dir = wp_get_upload_dir();
396 - $item = $wpmcsItem->get( $item_source );
399 + $wpmcsItem = Item::instance();
400 + $item = $wpmcsItem->get( $item_source['id'], $item_source['source_type'] );
397 401
398 402 if (
399 403 Utils::is_empty($item) ||
400 - get_post_type( $item_source ) !== 'attachment'
404 + get_post_type( $item_source['id'] ) !== 'attachment'
401 405 ) {
402 406 return false;
403 407 }
404 408
409 + if ( ! apply_filters( 'wpmcs_push_to_url_pairs', true, $item_source['id'], $item_source['source_type'] ) ) {
410 + return false;
411 + }
412 +
405 413 $base = trailingslashit($upload_dir['baseurl']);
406 - $backward_replacement = apply_filters('wpmcs_enable_backward_url_repalcement', false);
414 + $backward_replacement = apply_filters('wpmcs_enable_backward_url_replacement', false);
407 415
408 416 $source_server_file = Utils::remove_scheme($base.$item['source_path']);
409 - $source_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source));
417 + $source_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source['id'], 'full', $item_source['source_type']));
410 418
411 419 $url_pairs[$source_server_file] = $source_cloud_file;
412 420 if($backward_replacement) {
413 421 $url_pairs[$source_cloud_file] = $source_server_file; // Backward compatibility
@@ -412,22 +420,29 @@
412 420 if($backward_replacement) {
413 421 $url_pairs[$source_cloud_file] = $source_server_file; // Backward compatibility
414 422 }
415 423
416 - $extras = $wpmcsItem->get_extras($item_source);
417 424
418 - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) {
419 - $original_server_file = Utils::remove_scheme($base.$extras['original']['source_path']);
420 - $original_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source, 'original'));
425 + if(
426 + isset($item['original_source_path']) &&
427 + !Utils::is_empty($item['original_source_path'])
428 + ) {
429 + $original_server_file = Utils::remove_scheme($base.$item['original_source_path']);
430 + $original_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source['id'], 'original', $item_source['source_type']));
421 431 $url_pairs[$original_server_file] = $original_cloud_file;
422 432 if($backward_replacement) {
423 433 $url_pairs[$original_cloud_file] = $original_server_file; // Backward compatibility
424 434 }
425 435 }
436 +
437 + $extras = $wpmcsItem->get_extras($item_source['id'], false, $item_source['source_type']);
438 +
426 439 if(isset($extras['sizes'])){
427 440 foreach ($extras['sizes'] as $size => $sub_image) {
441 + if(Utils::is_empty($sub_image['source_path'])) continue;
442 +
428 443 $sub_server_file = Utils::remove_scheme($base.$sub_image['source_path']);
429 - $sub_cloud_file = Utils::remove_scheme($wpmcsItem->get_url( $item_source, $size ));
444 + $sub_cloud_file = Utils::remove_scheme($wpmcsItem->get_url( $item_source['id'], $size, $item_source['source_type'] ));
430 445 $url_pairs[$sub_server_file] = $sub_cloud_file;
431 446 if($backward_replacement) {
432 447 $url_pairs[$sub_cloud_file ] = $sub_server_file; // Backward compatibility
433 448 }
@@ -511,9 +526,9 @@
511 526 if ( ! Utils::is_file_url( $url ) ) {
512 527 continue;
513 528 }
514 529
515 - if ( ! $this->url_needs_replacing( $url ) ) {
530 + if ( ! self::url_needs_replacing( $url ) ) {
516 531 // URL already correct, skip
517 532 continue;
518 533 }
519 534
@@ -522,9 +537,9 @@
522 537
523 538 // If attachment ID recently or previously cached, skip full search.
524 539 if ( isset( $to_cache[ $bare_url ] ) ) {
525 540 $item_source = $to_cache[ $bare_url ];
526 - } elseif ( isset( $cache[ $bare_url ] ) ) {
541 + } elseif ( isset( $cache[ $bare_url ] ) && is_array($cache[ $bare_url ]) ) {
527 542 $item_source = $cache[ $bare_url ];
528 543 }
529 544
530 545 if ( is_null( $item_source ) ) {
@@ -562,14 +577,17 @@
562 577
563 578 if ( empty( $urls ) ) {
564 579 return $results;
565 580 }
566 -
581 + // Check if backward compatibility is enabled
582 + $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false);
583 +
567 584 if ( ! is_array( $urls ) ) {
568 585 $urls = array( $urls );
569 586 }
587 +
588 + $wpmcsItem = Item::instance();
570 589
571 - global $wpmcsItem;
572 590 $query_set = array();
573 591 $paths = array();
574 592 $full_urls = array();
575 593
@@ -596,9 +614,9 @@
596 614 continue;
597 615 }
598 616
599 617
600 - $path = Utils::get_attachment_source_path( $full_url );
618 + $path = Utils::get_attachment_source_path( $full_url, $reverse_compatibility ? 'key' : 'source' );
601 619
602 620 $paths[ $path ] = $full_url;
603 621 $full_urls[ $full_url ][] = $url;
604 622 }
@@ -603,20 +621,32 @@
603 621 $full_urls[ $full_url ][] = $url;
604 622 }
605 623
606 624 if ( ! empty( $paths ) ) {
607 - $wpmcsItems = $wpmcsItem->get_items_by_source_paths( array_keys( $paths ) );
625 + if($reverse_compatibility) {
626 + // For backward compatibility, search inside 'key' and 'original_key' columns.
627 + $wpmcsItems = $wpmcsItem->get_items_by_paths( array_keys( $paths ), true, false, 'key' );
628 + } else {
629 + $wpmcsItems = $wpmcsItem->get_items_by_paths( array_keys( $paths ) );
630 + }
608 631
609 632 if ( ! empty( $wpmcsItems ) ) {
610 633 foreach ( $wpmcsItems as $item ) {
611 - // Each returned item may have matched on either the source_path or original_source_path.
634 + // Each returned item may have matched on either the source_path or original_source_path
635 + // (or, in backward-compatibility mode, key or original_key — $paths above is keyed by
636 + // whichever field type get_attachment_source_path() was asked for, so the lookup below
637 + // must compare against that same field, not always source_path).
612 638 // Because the base image file name of a thumbnail might match the primary rather scaled or rotated full image
613 639 // it's possible that both source paths are used by separate URLs.
614 640 $source_id = (int)$item['source_id'];
615 - $source_paths = [ $item['source_path'] ];
616 - $original = $wpmcsItem->get_extras($source_id, 'original');
617 - if(isset($original['source_path'])) {
618 - $source_paths[] = $original['source_path'];
641 + $source_type = $item['source_type'];
642 + $path_field = $reverse_compatibility ? 'key' : 'source_path';
643 + $original_field = $reverse_compatibility ? 'original_key' : 'original_source_path';
644 + $source_paths = [ $item[ $path_field ] ];
645 +
646 + // If the original path/key is set, add it to source_paths.
647 + if(isset($item[ $original_field ]) && !Utils::is_empty($item[ $original_field ])) {
648 + $source_paths[] = $item[ $original_field ];
619 649 }
620 650
621 651 foreach ( $source_paths as $source_path ) {
622 652 if ( ! empty( $paths[ $source_path ] ) ) {
@@ -622,9 +652,12 @@
622 652 if ( ! empty( $paths[ $source_path ] ) ) {
623 653 $matched_full_url = $paths[ $source_path ];
624 654
625 655 if ( ! empty( $full_urls[ $matched_full_url ] ) ) {
626 - $item_source = $source_id;
656 + $item_source = [
657 + 'id' => $source_id,
658 + 'source_type' => $source_type
659 + ];
627 660
628 661 $this->query_cache[ $matched_full_url ] = $item_source;
629 662
630 663 foreach ( $full_urls[ $matched_full_url ] as $url ) {
@@ -651,8 +684,29 @@
651 684 }
652 685
653 686 return $results;
654 687 }
688 +
689 + /**
690 + * Is the supplied item_source considered to be empty?
691 + *
692 + * @param array $item_source
693 + *
694 + * @return bool
695 + */
696 + public function is_empty_item_source( $item_source ) {
697 + if (
698 + empty( $item_source['source_type'] ) ||
699 + ! isset( $item_source['id'] ) ||
700 + ! is_numeric( $item_source['id'] ) ||
701 + $item_source['id'] < 0
702 + ) {
703 + return true;
704 + }
705 +
706 + return false;
707 + }
708 +
655 709
656 710
657 711 /**
658 712 * Define the method for the backword compatibility filter