| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* WPSEO_Content_Images. |
| 10 |
*/ |
| 11 |
class WPSEO_Content_Images { |
| 12 |
|
| 13 |
/** |
| 14 |
* Retrieves images from the post content. |
| 15 |
* |
| 16 |
* @param int $post_id The post ID. |
| 17 |
* @param WP_Post|null $post The post object. |
| 18 |
* |
| 19 |
* @return array An array of images found in this post. |
| 20 |
*/ |
| 21 |
public function get_images( $post_id, $post = null ) { |
| 22 |
return $this->get_images_from_content( $this->get_post_content( $post_id, $post ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Grabs the images from the content. |
| 27 |
* |
| 28 |
* @param string $content The post content string. |
| 29 |
* |
| 30 |
* @return array An array of image URLs. |
| 31 |
*/ |
| 32 |
public function get_images_from_content( $content ) { |
| 33 |
if ( ! is_string( $content ) ) { |
| 34 |
return []; |
| 35 |
} |
| 36 |
|
| 37 |
$content_images = $this->get_img_tags_from_content( $content ); |
| 38 |
|
| 39 |
$images = array_map( [ $this, 'get_img_tag_source' ], $content_images ); |
| 40 |
$images = array_filter( $images ); |
| 41 |
$images = array_unique( $images ); |
| 42 |
$images = array_values( $images ); // Reset the array keys. |
| 43 |
return $images; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Gets the image tags from a given content string. |
| 48 |
* |
| 49 |
* @param string $content The content to search for image tags. |
| 50 |
* |
| 51 |
* @return array An array of `<img>` tags. |
| 52 |
*/ |
| 53 |
private function get_img_tags_from_content( $content ) { |
| 54 |
if ( strpos( $content, '<img' ) === false ) { |
| 55 |
return []; |
| 56 |
} |
| 57 |
|
| 58 |
preg_match_all( '`<img [^>]+>`', $content, $matches ); |
| 59 |
if ( isset( $matches[0] ) ) { |
| 60 |
return $matches[0]; |
| 61 |
} |
| 62 |
|
| 63 |
return []; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Retrieves the image URL from an image tag. |
| 68 |
* |
| 69 |
* @param string $image Image HTML element. |
| 70 |
* |
| 71 |
* @return string|bool The image URL on success, false on failure. |
| 72 |
*/ |
| 73 |
private function get_img_tag_source( $image ) { |
| 74 |
preg_match( '`src=(["\'])(.*?)\1`', $image, $matches ); |
| 75 |
if ( isset( $matches[2] ) && filter_var( $matches[2], FILTER_VALIDATE_URL ) ) { |
| 76 |
return $matches[2]; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Retrieves the post content we want to work with. |
| 83 |
* |
| 84 |
* @param int $post_id The post ID. |
| 85 |
* @param WP_Post|array|null $post The post. |
| 86 |
* |
| 87 |
* @return string The content of the supplied post. |
| 88 |
*/ |
| 89 |
private function get_post_content( $post_id, $post ) { |
| 90 |
$post ??= get_post( $post_id ); |
| 91 |
|
| 92 |
if ( $post === null ) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Filter: 'wpseo_pre_analysis_post_content' - Allow filtering the content before analysis. |
| 98 |
* |
| 99 |
* @param string $post_content The Post content string. |
| 100 |
* @param WP_Post $post The current post. |
| 101 |
*/ |
| 102 |
$content = apply_filters( 'wpseo_pre_analysis_post_content', $post->post_content, $post ); |
| 103 |
|
| 104 |
if ( ! is_string( $content ) ) { |
| 105 |
$content = ''; |
| 106 |
} |
| 107 |
|
| 108 |
return $content; |
| 109 |
} |
| 110 |
} |
| 111 |
|