PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 / src / presenters / open-graph / image-presenter.php

image-presenter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at src/presenters/open-graph/image-presenter.php

104 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Presenters\Open_Graph;
4
5 use Yoast\WP\SEO\Presentations\Indexable_Presentation;
6 use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;
7
8 /**
9 * Presenter class for the Open Graph image.
10 */
11 class Image_Presenter extends Abstract_Indexable_Presenter {
12
13 /**
14 * The tag key name.
15 *
16 * @var string
17 */
18 protected $key = 'og:image';
19
20 /**
21 * Image tags that we output for each image.
22 *
23 * @var array
24 */
25 protected static $image_tags = [
26 'width' => 'width',
27 'height' => 'height',
28 'type' => 'type',
29 ];
30
31 /**
32 * Returns the image for a post.
33 *
34 * @return string The image tag.
35 */
36 public function present() {
37 $images = $this->get();
38
39 if ( empty( $images ) ) {
40 return '';
41 }
42
43 $return = '';
44 foreach ( $images as $image_index => $image_meta ) {
45 $image_url = $image_meta['url'];
46
47 $return .= '<meta property="og:image" content="' . \esc_url( $image_url ) . '" />';
48
49 foreach ( static::$image_tags as $key => $value ) {
50 if ( empty( $image_meta[ $key ] ) ) {
51 continue;
52 }
53
54 $return .= \PHP_EOL . "\t" . '<meta property="og:image:' . \esc_attr( $key ) . '" content="' . $image_meta[ $key ] . '" />';
55 }
56 }
57
58 return $return;
59 }
60
61 /**
62 * Gets the raw value of a presentation.
63 *
64 * @return array The raw value.
65 */
66 public function get() {
67 $images = [];
68
69 foreach ( $this->presentation->open_graph_images as $open_graph_image ) {
70 $images[] = \array_intersect_key(
71 // First filter the object.
72 $this->filter( $open_graph_image ),
73 // Then strip all keys that aren't in the image tags or the url.
74 \array_flip( \array_merge( static::$image_tags, [ 'url' ] ) )
75 );
76 }
77
78 return \array_filter( $images );
79 }
80
81 /**
82 * Run the image content through the `wpseo_opengraph_image` filter.
83 *
84 * @param array $image The image.
85 *
86 * @return array The filtered image.
87 */
88 protected function filter( $image ) {
89 /**
90 * Filter: 'wpseo_opengraph_image' - Allow changing the Open Graph image.
91 *
92 * @api string - The URL of the Open Graph image.
93 *
94 * @param Indexable_Presentation $presentation The presentation of an indexable.
95 */
96 $image_url = \trim( \apply_filters( 'wpseo_opengraph_image', $image['url'], $this->presentation ) );
97 if ( ! empty( $image_url ) && \is_string( $image_url ) ) {
98 $image['url'] = $image_url;
99 }
100
101 return $image;
102 }
103 }
104