| @@ -5,8 +5,10 @@ | ||
| 5 | 5 | * |
| 6 | 6 | * @package automattic/jetpack |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | +use Automattic\Jetpack\Post_Media\Images; | |
| 10 | + | |
| 9 | 11 | /** |
| 10 | 12 | * Class with methods to extract metadata from a post/page about videos, images, links, mentions embedded |
| 11 | 13 | * in or attached to the post/page. |
| 12 | 14 | * |
| @@ -31,12 +33,14 @@ | ||
| 31 | 33 | * |
| 32 | 34 | * @var string[] |
| 33 | 35 | */ |
| 34 | 36 | private static $keeper_shortcodes = array( |
| 37 | + 'audio', | |
| 35 | 38 | 'youtube', |
| 36 | 39 | 'vimeo', |
| 37 | 40 | 'hulu', |
| 38 | 41 | 'ted', |
| 42 | + 'video', | |
| 39 | 43 | 'wpvideo', |
| 40 | 44 | 'videopress', |
| 41 | 45 | ); |
| 42 | 46 | |
| @@ -79,9 +83,9 @@ | ||
| 79 | 83 | if ( self::IMAGES & $what_to_extract ) { |
| 80 | 84 | $extracted = self::get_image_fields( $post, array(), $extract_alt_text ); |
| 81 | 85 | |
| 82 | 86 | // Turn off images so we can safely call extract_from_content() below. |
| 83 | - $what_to_extract = $what_to_extract - self::IMAGES; | |
| 87 | + $what_to_extract -= self::IMAGES; | |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | if ( function_exists( 'restore_current_blog' ) ) { |
| 87 | 91 | restore_current_blog(); |
| @@ -196,8 +200,13 @@ | ||
| 196 | 200 | if ( function_exists( $shortcode_get_id_func ) ) { |
| 197 | 201 | $id = call_user_func( $shortcode_get_id_func, $attr ); |
| 198 | 202 | } elseif ( method_exists( $shortcode_class_name, $shortcode_get_id_method ) ) { |
| 199 | 203 | $id = call_user_func( array( $shortcode_class_name, $shortcode_get_id_method ), $attr ); |
| 204 | + } elseif ( 'video' === $shortcode ) { | |
| 205 | + $id = $attr['src'] ?? $attr['url'] ?? $attr['mp4'] ?? $attr['m4v'] ?? $attr['webm'] ?? $attr['ogv'] ?? $attr['wmv'] ?? $attr['flv'] ?? null; | |
| 206 | + } elseif ( 'audio' === $shortcode ) { | |
| 207 | + preg_match( '#(https?://(?:[^\s"|\']+)\.(?:mp3|ogg|flac|m4a|wav))([ "\'|]|$)#', implode( ' ', $attr ), $audio_matches ); | |
| 208 | + $id = $audio_matches[1] ?? null; | |
| 200 | 209 | } |
| 201 | 210 | if ( ! empty( $id ) |
| 202 | 211 | && ( ! isset( $shortcode_details[ $shortcode_name ] ) || ! in_array( $id, $shortcode_details[ $shortcode_name ], true ) ) ) { |
| 203 | 212 | $shortcode_details[ $shortcode_name ][] = $id; |
| @@ -293,14 +302,16 @@ | ||
| 293 | 302 | $link_analyzed .= ( ! empty( $link_analyzed ) ? ' ' : '' ) . $url['fragment']; |
| 294 | 303 | } |
| 295 | 304 | } |
| 296 | 305 | |
| 297 | - // @todo Check unique before adding | |
| 298 | - $links[] = array( | |
| 306 | + $link = array( | |
| 299 | 307 | 'url' => $link_all_but_proto, |
| 300 | 308 | 'host_reversed' => $host_reversed, |
| 301 | 309 | 'host' => $url['host'], |
| 302 | 310 | ); |
| 311 | + if ( ! in_array( $link, $links, true ) ) { | |
| 312 | + $links[] = $link; | |
| 313 | + } | |
| 303 | 314 | } |
| 304 | 315 | } |
| 305 | 316 | |
| 306 | 317 | $link_count = count( $links ); |
| @@ -337,9 +348,13 @@ | ||
| 337 | 348 | list( $proto, $link_all_but_proto ) = explode( '://', $link_raw ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 338 | 349 | |
| 339 | 350 | // Check whether this "link" is really an embed. |
| 340 | 351 | foreach ( $oembed->providers as $matchmask => $data ) { |
| 341 | - list( $providerurl, $regex ) = $data; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 352 | + // Guard against malformed oEmbed providers. | |
| 353 | + if ( ! isset( $data[0] ) ) { | |
| 354 | + continue; | |
| 355 | + } | |
| 356 | + $regex = $data[1] ?? false; | |
| 342 | 357 | |
| 343 | 358 | // Turn the asterisk-type provider URLs into regex. |
| 344 | 359 | if ( ! $regex ) { |
| 345 | 360 | $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i'; |
| @@ -373,9 +388,9 @@ | ||
| 373 | 388 | |
| 374 | 389 | /** |
| 375 | 390 | * Get image fields for matching images. |
| 376 | 391 | * |
| 377 | - * @uses Jetpack_PostImages | |
| 392 | + * @uses Images | |
| 378 | 393 | * |
| 379 | 394 | * @param WP_Post $post A post object. |
| 380 | 395 | * @param array $args Optional args, see defaults list for details. |
| 381 | 396 | * @param boolean $extract_alt_text Should alt_text be extracted, defaults to false. |
| @@ -398,9 +413,9 @@ | ||
| 398 | 413 | $image_list = array(); |
| 399 | 414 | $image_booleans = array(); |
| 400 | 415 | $image_booleans['gallery'] = 0; |
| 401 | 416 | |
| 402 | - $from_featured_image = Jetpack_PostImages::from_thumbnail( $post->ID, $args['width'], $args['height'] ); | |
| 417 | + $from_featured_image = Images::from_thumbnail( $post->ID, $args['width'], $args['height'] ); | |
| 403 | 418 | if ( ! empty( $from_featured_image ) ) { |
| 404 | 419 | if ( $extract_alt_text ) { |
| 405 | 420 | $image_list = array_merge( $image_list, self::reduce_extracted_images( $from_featured_image ) ); |
| 406 | 421 | } else { |
| @@ -408,9 +423,9 @@ | ||
| 408 | 423 | $image_list = array_merge( $image_list, $srcs ); |
| 409 | 424 | } |
| 410 | 425 | } |
| 411 | 426 | |
| 412 | - $from_slideshow = Jetpack_PostImages::from_slideshow( $post->ID, $args['width'], $args['height'] ); | |
| 427 | + $from_slideshow = Images::from_slideshow( $post->ID, $args['width'], $args['height'] ); | |
| 413 | 428 | if ( ! empty( $from_slideshow ) ) { |
| 414 | 429 | if ( $extract_alt_text ) { |
| 415 | 430 | $image_list = array_merge( $image_list, self::reduce_extracted_images( $from_slideshow ) ); |
| 416 | 431 | } else { |
| @@ -418,9 +433,9 @@ | ||
| 418 | 433 | $image_list = array_merge( $image_list, $srcs ); |
| 419 | 434 | } |
| 420 | 435 | } |
| 421 | 436 | |
| 422 | - $from_gallery = Jetpack_PostImages::from_gallery( $post->ID ); | |
| 437 | + $from_gallery = Images::from_gallery( $post->ID ); | |
| 423 | 438 | if ( ! empty( $from_gallery ) ) { |
| 424 | 439 | if ( $extract_alt_text ) { |
| 425 | 440 | $image_list = array_merge( $image_list, self::reduce_extracted_images( $from_gallery ) ); |
| 426 | 441 | } else { |
| @@ -436,9 +451,9 @@ | ||
| 436 | 451 | return self::build_image_struct( $image_list, $image_booleans ); |
| 437 | 452 | } |
| 438 | 453 | |
| 439 | 454 | /** |
| 440 | - * Given an extracted image array reduce to src and alt_text. | |
| 455 | + * Given an extracted image array reduce to src, alt_text, src_width, and src_height. | |
| 441 | 456 | * |
| 442 | 457 | * @param array $images extracted image array. |
| 443 | 458 | * |
| 444 | 459 | * @return array reduced image array |
| @@ -449,16 +464,21 @@ | ||
| 449 | 464 | // skip if src isn't set. |
| 450 | 465 | if ( empty( $image['src'] ) ) { |
| 451 | 466 | continue; |
| 452 | 467 | } |
| 468 | + $ret_image = array( | |
| 469 | + 'url' => $image['src'], | |
| 470 | + ); | |
| 471 | + if ( ! empty( $image['src_height'] ) || ! empty( $image['src_width'] ) ) { | |
| 472 | + $ret_image['src_width'] = $image['src_width'] ?? ''; | |
| 473 | + $ret_image['src_height'] = $image['src_height'] ?? ''; | |
| 474 | + } | |
| 453 | 475 | if ( ! empty( $image['alt_text'] ) ) { |
| 454 | - $ret_images[] = array( | |
| 455 | - 'url' => $image['src'], | |
| 456 | - 'alt_text' => $image['alt_text'], | |
| 457 | - ); | |
| 476 | + $ret_image['alt_text'] = $image['alt_text']; | |
| 458 | 477 | } else { |
| 459 | - $ret_images[] = $image['src']; | |
| 478 | + $ret_image = $image['src']; | |
| 460 | 479 | } |
| 480 | + $ret_images[] = $ret_image; | |
| 461 | 481 | } |
| 462 | 482 | return $ret_images; |
| 463 | 483 | } |
| 464 | 484 | |
| @@ -466,13 +486,14 @@ | ||
| 466 | 486 | * Helper function to get images from HTML and return it with the set sturcture. |
| 467 | 487 | * |
| 468 | 488 | * @param string $content HTML content. |
| 469 | 489 | * @param array $image_list Array of already found images. |
| 490 | + * @param string $extract_alt_text Whether or not to extract the alt text. | |
| 470 | 491 | * |
| 471 | 492 | * @return array|array[] Array of images. |
| 472 | 493 | */ |
| 473 | - public static function extract_images_from_content( $content, $image_list ) { | |
| 474 | - $image_list = self::get_images_from_html( $content, $image_list ); | |
| 494 | + public static function extract_images_from_content( $content, $image_list, $extract_alt_text = false ) { | |
| 495 | + $image_list = self::get_images_from_html( $content, $image_list, $extract_alt_text ); | |
| 475 | 496 | return self::build_image_struct( $image_list, array() ); |
| 476 | 497 | } |
| 477 | 498 | |
| 478 | 499 | /** |
| @@ -494,11 +515,9 @@ | ||
| 494 | 515 | $retval['image'][] = $img; |
| 495 | 516 | } |
| 496 | 517 | } |
| 497 | 518 | $image_booleans['image'] = count( $retval['image'] ); |
| 498 | - if ( ! empty( $image_booleans ) ) { | |
| 499 | - $retval['has'] = $image_booleans; | |
| 500 | - } | |
| 519 | + $retval['has'] = $image_booleans; | |
| 501 | 520 | return $retval; |
| 502 | 521 | } else { |
| 503 | 522 | return array(); |
| 504 | 523 | } |
| @@ -514,9 +533,9 @@ | ||
| 514 | 533 | * @return array Image URLs extracted from the HTML, stripped of query params and de-duped |
| 515 | 534 | */ |
| 516 | 535 | public static function get_images_from_html( $html, $images_already_extracted, $extract_alt_text = false ) { |
| 517 | 536 | $image_list = $images_already_extracted; |
| 518 | - $from_html = Jetpack_PostImages::from_html( $html ); | |
| 537 | + $from_html = Images::from_html( $html ); | |
| 519 | 538 | // early return if no image in html. |
| 520 | 539 | if ( empty( $from_html ) ) { |
| 521 | 540 | return $image_list; |
| 522 | 541 | } |
| @@ -542,16 +561,23 @@ | ||
| 542 | 561 | continue; |
| 543 | 562 | } |
| 544 | 563 | |
| 545 | 564 | if ( ! in_array( $queryless, $image_list, true ) ) { |
| 546 | - if ( $extract_alt_text && ! empty( $extracted_image['alt_text'] ) ) { | |
| 547 | - $image_list[] = array( | |
| 548 | - 'url' => $queryless, | |
| 549 | - 'alt_text' => $extracted_image['alt_text'], | |
| 550 | - ); | |
| 565 | + $image_to_add = array( | |
| 566 | + 'url' => $queryless, | |
| 567 | + ); | |
| 568 | + if ( $extract_alt_text ) { | |
| 569 | + if ( ! empty( $extracted_image['alt_text'] ) ) { | |
| 570 | + $image_to_add['alt_text'] = $extracted_image['alt_text']; | |
| 571 | + } | |
| 572 | + if ( ! empty( $extracted_image['src_width'] ) || ! empty( $extracted_image['src_height'] ) ) { | |
| 573 | + $image_to_add['src_width'] = $extracted_image['src_width']; | |
| 574 | + $image_to_add['src_height'] = $extracted_image['src_height']; | |
| 575 | + } | |
| 551 | 576 | } else { |
| 552 | - $image_list[] = $queryless; | |
| 577 | + $image_to_add = $queryless; | |
| 553 | 578 | } |
| 579 | + $image_list[] = $image_to_add; | |
| 554 | 580 | } |
| 555 | 581 | } |
| 556 | 582 | return $image_list; |
| 557 | 583 | } |