PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 28.5, at src/presenters/open-graph/image-presenter.php

155 lines 4.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<string>
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_meta ) {
45 $image_url = $image_meta['url'];
46
47 if ( \is_attachment() ) {
48 global $wp;
49 $image_url = \home_url( $wp->request );
50 }
51
52 $class = \is_admin_bar_showing() ? ' class="yoast-seo-meta-tag"' : '';
53
54 $return .= '<meta property="og:image" content="' . \esc_url( $image_url, null, 'attribute' ) . '"' . $class . ' />';
55
56 foreach ( static::$image_tags as $key => $value ) {
57 if ( empty( $image_meta[ $key ] ) ) {
58 continue;
59 }
60
61 $return .= \PHP_EOL . "\t" . '<meta property="og:image:' . \esc_attr( $key ) . '" content="' . \esc_attr( $image_meta[ $key ] ) . '"' . $class . ' />';
62 }
63 }
64
65 return $return;
66 }
67
68 /**
69 * Gets the raw value of a presentation.
70 *
71 * @return array<string, int> The raw value.
72 */
73 public function get() {
74 $images = [];
75
76 foreach ( $this->presentation->open_graph_images as $open_graph_image ) {
77 $images[] = \array_intersect_key(
78 // First filter the object.
79 $this->filter( $open_graph_image ),
80 // Then strip all keys that aren't in the image tags or the url.
81 \array_flip( \array_merge( static::$image_tags, [ 'url' ] ) ),
82 );
83 }
84
85 return \array_filter( $images );
86 }
87
88 /**
89 * Run the image content through the `wpseo_opengraph_image` filter.
90 *
91 * @param array<string, string|int> $image The image.
92 *
93 * @return array<string, string|int> The filtered image.
94 */
95 protected function filter( $image ) {
96 /**
97 * Filter: 'wpseo_opengraph_image' - Allow changing the Open Graph image url.
98 *
99 * @param string $image_url The URL of the Open Graph image.
100 * @param Indexable_Presentation $presentation The presentation of an indexable.
101 */
102 $image_url = \apply_filters( 'wpseo_opengraph_image', $image['url'], $this->presentation );
103 if ( ! empty( $image_url ) && \is_string( $image_url ) ) {
104 $image['url'] = \trim( $image_url );
105 }
106
107 $image_type = ( $image['type'] ?? '' );
108 /**
109 * Filter: 'wpseo_opengraph_image_type' - Allow changing the Open Graph image type.
110 *
111 * @param string $image_type The type of the Open Graph image.
112 * @param Indexable_Presentation $presentation The presentation of an indexable.
113 */
114 $image_type = \apply_filters( 'wpseo_opengraph_image_type', $image_type, $this->presentation );
115 if ( ! empty( $image_type ) && \is_string( $image_type ) ) {
116 $image['type'] = \trim( $image_type );
117 }
118 else {
119 $image['type'] = '';
120 }
121
122 $image_width = ( $image['width'] ?? '' );
123 /**
124 * Filter: 'wpseo_opengraph_image_width' - Allow changing the Open Graph image width.
125 *
126 * @param int $image_width The width of the Open Graph image.
127 * @param Indexable_Presentation $presentation The presentation of an indexable.
128 */
129 $image_width = (int) \apply_filters( 'wpseo_opengraph_image_width', $image_width, $this->presentation );
130 if ( ! empty( $image_width ) && $image_width > 0 ) {
131 $image['width'] = $image_width;
132 }
133 else {
134 $image['width'] = '';
135 }
136
137 $image_height = ( $image['height'] ?? '' );
138 /**
139 * Filter: 'wpseo_opengraph_image_height' - Allow changing the Open Graph image height.
140 *
141 * @param int $image_height The height of the Open Graph image.
142 * @param Indexable_Presentation $presentation The presentation of an indexable.
143 */
144 $image_height = (int) \apply_filters( 'wpseo_opengraph_image_height', $image_height, $this->presentation );
145 if ( ! empty( $image_height ) && $image_height > 0 ) {
146 $image['height'] = $image_height;
147 }
148 else {
149 $image['height'] = '';
150 }
151
152 return $image;
153 }
154 }
155