WPSEO_OpenGraph_Image

Class WPSEO_OpenGraph_Image.

Defined (1)

The class is defined in the following location(s).

/frontend/class-opengraph.php  
  1. class WPSEO_OpenGraph_Image { 
  2.  
  3. /** 
  4. * @var array $options Holds options passed to the constructor 
  5. */ 
  6. private $options; 
  7.  
  8. /** 
  9. * @var array $images Holds the images that have been put out as OG image. 
  10. */ 
  11. private $images = array(); 
  12.  
  13. /** 
  14. * @TODO This needs to be refactored since we only hold one set of dimensions for multiple images. R. 
  15. * @var array $dimensions Holds image dimensions, if determined. 
  16. */ 
  17. protected $dimensions = array(); 
  18.  
  19. /** 
  20. * Constructor 
  21. * @param array $options Options set. 
  22. * @param string|boolean $image Optional image URL. 
  23. */ 
  24. public function __construct( $options, $image = false ) { 
  25. $this->options = $options; 
  26.  
  27. if ( ! empty( $image ) && $this->add_image( $image ) ) { 
  28. // Safely assume an image was added so we don't need to automatically determine it anymore. 
  29. else { 
  30. $this->set_images(); 
  31.  
  32. /** 
  33. * Return the images array 
  34. * @return array 
  35. */ 
  36. public function get_images() { 
  37. return $this->images; 
  38.  
  39. /** 
  40. * Return the dimensions array. 
  41. * @return array 
  42. */ 
  43. public function get_dimensions() { 
  44. return $this->dimensions; 
  45.  
  46. /** 
  47. * Display an OpenGraph image tag 
  48. * @param string $img - Source URL to the image. 
  49. * @return bool 
  50. */ 
  51. public function add_image( $img ) { 
  52.  
  53. $original = trim( $img ); 
  54.  
  55. // Filter: 'wpseo_opengraph_image' - Allow changing the OpenGraph image. 
  56. $img = trim( apply_filters( 'wpseo_opengraph_image', $img ) ); 
  57.  
  58. if ( $original !== $img ) { 
  59. $this->dimensions = array(); 
  60.  
  61. if ( empty( $img ) ) { 
  62. return false; 
  63.  
  64. if ( WPSEO_Utils::is_url_relative( $img ) === true ) { 
  65. $img = $this->get_relative_path( $img ); 
  66.  
  67. if ( in_array( $img, $this->images ) ) { 
  68. return false; 
  69. array_push( $this->images, $img ); 
  70.  
  71. return true; 
  72.  
  73. /** 
  74. * Check if page is front page or singular and call the corresponding functions. If not, call get_default_image. 
  75. */ 
  76. private function set_images() { 
  77.  
  78. /** 
  79. * Filter: wpseo_add_opengraph_images - Allow developers to add images to the OpenGraph tags 
  80. * @api WPSEO_OpenGraph_Image The current object. 
  81. */ 
  82. do_action( 'wpseo_add_opengraph_images', $this ); 
  83.  
  84. if ( is_front_page() ) { 
  85. $this->get_front_page_image(); 
  86. elseif ( is_home() ) { // Posts page, which won't be caught by is_singular() below. 
  87. $this->get_posts_page_image(); 
  88.  
  89. if ( is_singular() ) { 
  90. $this->get_singular_image(); 
  91.  
  92. if ( is_category() || is_tax() || is_tag() ) { 
  93. $this->get_opengraph_image_taxonomy(); 
  94.  
  95. $this->get_default_image(); 
  96.  
  97. /** 
  98. * If the frontpage image exists, call add_image 
  99. */ 
  100. private function get_front_page_image() { 
  101. if ( $this->options['og_frontpage_image'] !== '' ) { 
  102. $this->add_image( $this->options['og_frontpage_image'] ); 
  103.  
  104. /** 
  105. * Get the images of the posts page. 
  106. */ 
  107. private function get_posts_page_image() { 
  108.  
  109. $post_id = get_option( 'page_for_posts' ); 
  110.  
  111. if ( $this->get_opengraph_image_post( $post_id ) ) { 
  112. return; 
  113.  
  114. if ( $this->get_featured_image( $post_id ) ) { 
  115. return; 
  116.  
  117. /** 
  118. * Get the images of the singular post. 
  119. */ 
  120. private function get_singular_image() { 
  121. global $post; 
  122.  
  123. if ( $this->get_opengraph_image_post() ) { 
  124. return; 
  125.  
  126. if ( $this->get_attachment_page_image( $post->ID ) ) { 
  127. return; 
  128.  
  129. if ( $this->get_featured_image( $post->ID ) ) { 
  130. return; 
  131.  
  132. $this->get_content_images( $post ); 
  133.  
  134. /** 
  135. * Get default image and call add_image 
  136. */ 
  137. private function get_default_image() { 
  138. if ( count( $this->images ) === 0 && isset( $this->options['og_default_image'] ) && $this->options['og_default_image'] !== '' ) { 
  139. $this->add_image( $this->options['og_default_image'] ); 
  140.  
  141. /** 
  142. * If opengraph-image is set, call add_image and return true. 
  143. * @param int $post_id Optional post ID to use. 
  144. * @return bool 
  145. */ 
  146. private function get_opengraph_image_post( $post_id = 0 ) { 
  147. $ogimg = WPSEO_Meta::get_value( 'opengraph-image', $post_id ); 
  148. if ( $ogimg !== '' ) { 
  149. $this->add_image( $ogimg ); 
  150.  
  151. return true; 
  152.  
  153. return false; 
  154.  
  155. /** 
  156. * Check if taxonomy has an image and add this image 
  157. */ 
  158. private function get_opengraph_image_taxonomy() { 
  159. if ( ( $ogimg = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-image' ) ) !== '' ) { 
  160. $this->add_image( $ogimg ); 
  161.  
  162. /** 
  163. * If there is a featured image, check image size. If image size is correct, call add_image and return true 
  164. * @param int $post_id The post ID. 
  165. * @return bool 
  166. */ 
  167. private function get_featured_image( $post_id ) { 
  168.  
  169. if ( has_post_thumbnail( $post_id ) ) { 
  170. /** 
  171. * Filter: 'wpseo_opengraph_image_size' - Allow changing the image size used for OpenGraph sharing 
  172. * @api string $unsigned Size string 
  173. */ 
  174. $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), apply_filters( 'wpseo_opengraph_image_size', 'original' ) ); 
  175.  
  176. if ( $this->check_featured_image_size( $thumb ) ) { 
  177.  
  178. $this->dimensions['width'] = $thumb[1]; 
  179. $this->dimensions['height'] = $thumb[2]; 
  180.  
  181. return $this->add_image( $thumb[0] ); 
  182.  
  183. return false; 
  184.  
  185. /** 
  186. * If this is an attachment page, call add_image with the attachment and return true 
  187. * @param int $post_id The post ID. 
  188. * @return bool 
  189. */ 
  190. private function get_attachment_page_image( $post_id ) { 
  191. if ( get_post_type( $post_id ) === 'attachment' ) { 
  192. $mime_type = get_post_mime_type( $post_id ); 
  193. switch ( $mime_type ) { 
  194. case 'image/jpeg': 
  195. case 'image/png': 
  196. case 'image/gif': 
  197. return $this->add_image( wp_get_attachment_url( $post_id ) ); 
  198.  
  199. return false; 
  200.  
  201. /** 
  202. * Filter: 'wpseo_pre_analysis_post_content' - Allow filtering the content before analysis 
  203. * @api string $post_content The Post content string 
  204. * @param object $post - The post object. 
  205. */ 
  206. private function get_content_images( $post ) { 
  207. $content = apply_filters( 'wpseo_pre_analysis_post_content', $post->post_content, $post ); 
  208.  
  209. if ( preg_match_all( '`<img [^>]+>`', $content, $matches ) ) { 
  210. foreach ( $matches[0] as $img ) { 
  211. if ( preg_match( '`src=(["\'])(.*?)\1`', $img, $match ) ) { 
  212. $this->add_image( $match[2] ); 
  213.  
  214. /** 
  215. * Check size of featured image. If image is too small, return false, else return true 
  216. * @param array $img_data wp_get_attachment_image_src: url, width, height, icon. 
  217. * @return bool 
  218. */ 
  219. private function check_featured_image_size( $img_data ) { 
  220.  
  221. if ( ! is_array( $img_data ) ) { 
  222. return false; 
  223.  
  224. // Get the width and height of the image. 
  225. if ( $img_data[1] < 200 || $img_data[2] < 200 ) { 
  226. return false; 
  227.  
  228. return true; 
  229.  
  230. /** 
  231. * Get the relative path of the image 
  232. * @param array $img Image data array. 
  233. * @return bool|string 
  234. */ 
  235. private function get_relative_path( $img ) { 
  236. if ( $img[0] != '/' ) { 
  237. return false; 
  238.  
  239. // If it's a relative URL, it's relative to the domain, not necessarily to the WordPress install, we 
  240. // want to preserve domain name and URL scheme (http / https) though. 
  241. $parsed_url = wp_parse_url( home_url() ); 
  242. $img = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img; 
  243.  
  244. return $img;