class-wpseo-content-images.php 2.8 KB

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