PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-wpseo-content-images.php

class-wpseo-content-images.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at inc/class-wpseo-content-images.php

112 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $images = array_map( [ $this, 'get_img_tag_source' ], $content_images );
39 $images = array_filter( $images );
40 $images = array_unique( $images );
41 $images = array_values( $images ); // Reset the array keys.
42
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] ) ) {
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 if ( $post === null ) {
91 $post = get_post( $post_id );
92 }
93
94 if ( $post === null ) {
95 return '';
96 }
97
98 /**
99 * Filter: 'wpseo_pre_analysis_post_content' - Allow filtering the content before analysis.
100 *
101 * @api string $post_content The Post content string.
102 */
103 $content = apply_filters( 'wpseo_pre_analysis_post_content', $post->post_content, $post );
104
105 if ( ! is_string( $content ) ) {
106 $content = '';
107 }
108
109 return $content;
110 }
111 }
112