| 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 |
|