class-wpseo-content-images.php 3.1 KB

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