PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
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 18.1, at src/integrations/front-end/open-graph-oembed.php

139 lines 3.1 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 * Returns the conditionals based in which this loadable should be active.
39 *
40 * @return array
41 */
42 public static function get_conditionals() {
43 return [ Front_End_Conditional::class, Open_Graph_Conditional::class ];
44 }
45
46 /**
47 * Initializes the integration.
48 *
49 * This is the place to register hooks and filters.
50 *
51 * @return void
52 */
53 public function register_hooks() {
54 \add_filter( 'oembed_response_data', [ $this, 'set_oembed_data' ], 10, 2 );
55 }
56
57 /**
58 * Open_Graph_OEmbed constructor.
59 *
60 * @param Meta_Surface $meta The meta surface.
61 */
62 public function __construct( Meta_Surface $meta ) {
63 $this->meta = $meta;
64 }
65
66 /**
67 * Callback function to pass to the oEmbed's response data that will enable
68 * support for using the image and title set by the WordPress SEO plugin's fields. This
69 * address the concern where some social channels/subscribed use oEmebed data over Open Graph data
70 * if both are present.
71 *
72 * @link https://developer.wordpress.org/reference/hooks/oembed_response_data/ for hook info.
73 *
74 * @param array $data The oEmbed data.
75 * @param WP_Post $post The current Post object.
76 *
77 * @return array An array of oEmbed data with modified values where appropriate.
78 */
79 public function set_oembed_data( $data, $post ) {
80 // Data to be returned.
81 $this->data = $data;
82 $this->post_id = $post->ID;
83
84 $this->set_title();
85 $this->set_description();
86 $this->set_image();
87
88 return $this->data;
89 }
90
91 /**
92 * Sets the OpenGraph title if configured.
93 */
94 protected function set_title() {
95 $opengraph_title = $this->meta->for_post( $this->post_id )->open_graph_title;
96
97 if ( ! empty( $opengraph_title ) ) {
98 $this->data['title'] = $opengraph_title;
99 }
100 }
101
102 /**
103 * Sets the OpenGraph description if configured.
104 */
105 protected function set_description() {
106 $opengraph_description = $this->meta->for_post( $this->post_id )->open_graph_description;
107
108 if ( ! empty( $opengraph_description ) ) {
109 $this->data['description'] = $opengraph_description;
110 }
111 }
112
113 /**
114 * Sets the image if it has been configured.
115 */
116 protected function set_image() {
117 $images = $this->meta->for_post( $this->post_id )->open_graph_images;
118 $image = \reset( $images );
119
120 if ( empty( $image ) ) {
121 return;
122 }
123
124 if ( ! isset( $image['url'] ) ) {
125 return;
126 }
127
128 $this->data['thumbnail_url'] = $image['url'];
129
130 if ( isset( $image['width'] ) ) {
131 $this->data['thumbnail_width'] = $image['width'];
132 }
133
134 if ( isset( $image['height'] ) ) {
135 $this->data['thumbnail_height'] = $image['height'];
136 }
137 }
138 }
139