PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 / integrations / front-end / open-graph-oembed.php

open-graph-oembed.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/integrations/front-end/open-graph-oembed.php

156 lines 3.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\Integrations\Front_End;
4
5 use WP_Post;
6 use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
7 use Yoast\WP\SEO\Conditionals\Open_Graph_Conditional;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9 use Yoast\WP\SEO\Surfaces\Meta_Surface;
10
11 /**
12 * Class Open_Graph_OEmbed.
13 */
14 class Open_Graph_OEmbed implements Integration_Interface {
15
16 /**
17 * The meta surface.
18 *
19 * @var Meta_Surface
20 */
21 private $meta;
22
23 /**
24 * The oEmbed data.
25 *
26 * @var array
27 */
28 private $data;
29
30 /**
31 * The post ID for the current post.
32 *
33 * @var int
34 */
35 private $post_id;
36
37 /**
38 * The post meta.
39 *
40 * @var Meta|false
41 */
42 private $post_meta;
43
44 /**
45 * Returns the conditionals based in which this loadable should be active.
46 *
47 * @return array
48 */
49 public static function get_conditionals() {
50 return [ Front_End_Conditional::class, Open_Graph_Conditional::class ];
51 }
52
53 /**
54 * Initializes the integration.
55 *
56 * This is the place to register hooks and filters.
57 *
58 * @return void
59 */
60 public function register_hooks() {
61 \add_filter( 'oembed_response_data', [ $this, 'set_oembed_data' ], 10, 2 );
62 }
63
64 /**
65 * Open_Graph_OEmbed constructor.
66 *
67 * @param Meta_Surface $meta The meta surface.
68 */
69 public function __construct( Meta_Surface $meta ) {
70 $this->meta = $meta;
71 }
72
73 /**
74 * Callback function to pass to the oEmbed's response data that will enable
75 * support for using the image and title set by the WordPress SEO plugin's fields. This
76 * address the concern where some social channels/subscribed use oEmebed data over Open Graph data
77 * if both are present.
78 *
79 * @link https://developer.wordpress.org/reference/hooks/oembed_response_data/ for hook info.
80 *
81 * @param array $data The oEmbed data.
82 * @param WP_Post $post The current Post object.
83 *
84 * @return array An array of oEmbed data with modified values where appropriate.
85 */
86 public function set_oembed_data( $data, $post ) {
87 // Data to be returned.
88 $this->data = $data;
89 $this->post_id = $post->ID;
90 $this->post_meta = $this->meta->for_post( $this->post_id );
91
92 if ( ! empty( $this->post_meta ) ) {
93 $this->set_title();
94 $this->set_description();
95 $this->set_image();
96 }
97
98 return $this->data;
99 }
100
101 /**
102 * Sets the OpenGraph title if configured.
103 *
104 * @return void
105 */
106 protected function set_title() {
107 $opengraph_title = $this->post_meta->open_graph_title;
108
109 if ( ! empty( $opengraph_title ) ) {
110 $this->data['title'] = $opengraph_title;
111 }
112 }
113
114 /**
115 * Sets the OpenGraph description if configured.
116 *
117 * @return void
118 */
119 protected function set_description() {
120 $opengraph_description = $this->post_meta->open_graph_description;
121
122 if ( ! empty( $opengraph_description ) ) {
123 $this->data['description'] = $opengraph_description;
124 }
125 }
126
127 /**
128 * Sets the image if it has been configured.
129 *
130 * @return void
131 */
132 protected function set_image() {
133 $images = $this->post_meta->open_graph_images;
134
135 if ( ! \is_array( $images ) ) {
136 return;
137 }
138
139 $image = \reset( $images );
140
141 if ( empty( $image ) || ! isset( $image['url'] ) ) {
142 return;
143 }
144
145 $this->data['thumbnail_url'] = $image['url'];
146
147 if ( isset( $image['width'] ) ) {
148 $this->data['thumbnail_width'] = $image['width'];
149 }
150
151 if ( isset( $image['height'] ) ) {
152 $this->data['thumbnail_height'] = $image['height'];
153 }
154 }
155 }
156