PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
← All changes | lib/oembed.php +344 -0 22.5.222.7.0 View file →
@@ -1,0 +1,344 @@
1 +<?php
2 +/*
3 + * License: GPLv3
4 + * License URI: https://www.gnu.org/licenses/gpl.txt
5 + * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/)
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 +
10 + die( 'These aren\'t the droids you\'re looking for.' );
11 +}
12 +
13 +if ( ! defined( 'WPSSO_PLUGINDIR' ) ) {
14 +
15 + die( 'Do. Or do not. There is no try.' );
16 +}
17 +
18 +if ( ! class_exists( 'WpssoOembed' ) ) {
19 +
20 + class WpssoOembed {
21 +
22 + private $p; // Wpsso class object.
23 +
24 + public function __construct( &$plugin ) {
25 +
26 + $this->p =& $plugin;
27 +
28 + if ( $this->p->debug->enabled ) {
29 +
30 + $this->p->debug->mark();
31 + }
32 +
33 + if ( ! SucomUtilWP::oembed_enabled() ) { // Nothing to do.
34 +
35 + return;
36 + }
37 +
38 + /*
39 + * Replace the WordPress theme-compat/embed.php and any theme embed.php template with our own.
40 + */
41 + add_filter( 'template_include', array( $this, 'template_include_embed' ), 10000, 1 );
42 +
43 + /*
44 + * The WordPress locate_template() and load_template() functions are not filtered and only check the
45 + * STYLESHEETPATH, TEMPLATEPATH, and WordPress theme-compat folders, so preempt their use in
46 + * get_template_part() by hooking the "get_template_part_{$slug}" action to require our own embed template
47 + * part(s).
48 + */
49 + add_action( 'get_template_part_wpsso/embed', array( $this, 'template_part_embed' ), 10, 3 );
50 +
51 + /*
52 + * Filters that receive a $post object.
53 + */
54 + add_filter( 'oembed_response_data', array( $this, 'post_oembed_response_data' ), 10000, 4 );
55 + add_filter( 'oembed_response_data', array( $this, 'post_oembed_response_data_rich' ), 11000, 4 );
56 + add_filter( 'post_embed_url', array( $this, 'post_embed_url' ), 10000, 2 );
57 + add_filter( 'embed_html', array( $this, 'post_embed_html' ), 10000, 4 );
58 +
59 + /*
60 + * Filters that are called in the loop.
61 + */
62 + add_filter( 'embed_thumbnail_url', array( $this, 'the_embed_thumbnail_url' ), 10000, 1 );
63 + add_filter( 'embed_thumbnail_url_image_shape', array( $this, 'the_embed_thumbnail_url_image_shape' ), 10000, 2 );
64 +
65 + add_filter( 'embed_thumbnail_id', array( $this, 'the_embed_thumbnail_id' ), 10000, 1 );
66 + add_filter( 'embed_thumbnail_image_size', array( $this, 'the_embed_thumbnail_image_size' ), 10000, 2 );
67 + add_filter( 'embed_thumbnail_image_shape', array( $this, 'the_embed_thumbnail_image_shape' ), 10000, 2 );
68 +
69 + add_filter( 'the_excerpt_embed', array( $this, 'the_embed_excerpt' ), 10000, 1 );
70 + add_filter( 'embed_site_title_html', array( $this, 'the_embed_site_title_html' ), 10000, 1 );
71 + }
72 +
73 + /*
74 + * Replace the WordPress theme-compat/embed.php and any theme embed.php template with our own.
75 + */
76 + public function template_include_embed( $template ) {
77 +
78 + if ( false !== strpos( $template, '/embed.php' ) ) {
79 +
80 + $template = preg_replace( '/^.*\/(embed\.php)$/', WPSSO_PLUGINDIR . 'lib/theme-compat/$1', $template );
81 + }
82 +
83 + return $template;
84 + }
85 +
86 + /*
87 + * The WordPress locate_template() and load_template() functions are not filtered and only check the
88 + * STYLESHEETPATH, TEMPLATEPATH, and WordPress theme-compat folders, so preempt their use in get_template_part() by
89 + * hooking the "get_template_part_{$slug}" action to require our own embed template part(s).
90 + *
91 + * Example:
92 + *
93 + * $slug = 'wpsso/embed'
94 + * $name = 'content'
95 + */
96 + public function template_part_embed( $slug, $name, $args ) {
97 +
98 + if ( $this->p->debug->enabled ) {
99 +
100 + $this->p->debug->mark();
101 + }
102 +
103 + if ( 0 === strpos( $slug, 'wpsso/' ) ) { // Just in case.
104 +
105 + $template = preg_replace( '/^wpsso\/(.*)/', WPSSO_PLUGINDIR . 'lib/theme-compat/$1-' . $name . '.php', $slug );
106 + $filter_name = SucomUtil::sanitize_hookname( $slug . '_' . $name . '_template_path' );
107 +
108 + if ( $this->p->debug->enabled ) {
109 +
110 + $this->p->debug->log( 'applying filters "' . $filter_name . '"' );
111 + }
112 +
113 + $template = apply_filters( $filter_name, $template, $args );
114 +
115 + if ( file_exists( $template ) ) { // Just in case.
116 +
117 + require $template;
118 + }
119 + }
120 + }
121 +
122 + /*
123 + * Filters the oEmbed response data.
124 + *
125 + * $data = array(
126 + * 'version' => '1.0',
127 + * 'provider_name' => get_bloginfo( 'name' ),
128 + * 'provider_url' => get_home_url(),
129 + * 'author_name' => get_bloginfo( 'name' ),
130 + * 'author_url' => get_home_url(),
131 + * 'title' => get_the_title( $post ),
132 + * 'type' => 'link',
133 + * );
134 + *
135 + * $author = get_userdata( $post->post_author );
136 + *
137 + * if ( $author ) {
138 + *
139 + * $data[ 'author_name' ] = $author->display_name;
140 + * $data[ 'author_url' ] = get_author_posts_url( $author->ID );
141 + * }
142 + */
143 + public function post_oembed_response_data( $data, $post, $width, $height ) {
144 +
145 + if ( ! empty( $post->ID ) ) { // Just in case.
146 +
147 + if ( $this->p->debug->enabled ) {
148 +
149 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
150 + }
151 +
152 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
153 +
154 + if ( ! empty( $head_info[ 'og:title' ] ) ) {
155 +
156 + $data[ 'title' ] = $head_info[ 'og:title' ];
157 + }
158 + }
159 +
160 + return $data;
161 + }
162 +
163 + /*
164 + * Filters the oEmbed response data to return an iframe embed code.
165 + *
166 + * $data[ 'width' ] = absint( $width );
167 + * $data[ 'height' ] = absint( $height );
168 + * $data[ 'type' ] = 'rich';
169 + * $data[ 'html' ] = get_post_embed_html( $width, $height, $post );
170 + * $data[ 'thumbnail_url' ] = $thumbnail_url;
171 + * $data[ 'thumbnail_width' ] = $thumbnail_width;
172 + * $data[ 'thumbnail_height' ] = $thumbnail_height;
173 + */
174 + public function post_oembed_response_data_rich( $data, $post, $width, $height ) {
175 +
176 + if ( ! empty( $post->ID ) ) { // Just in case.
177 +
178 + if ( $this->p->debug->enabled ) {
179 +
180 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
181 + }
182 +
183 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
184 +
185 + if ( isset( $head_info[ 'og:image:width' ] ) && $head_info[ 'og:image:width' ] > 0 &&
186 + isset( $head_info[ 'og:image:height' ] ) && $head_info[ 'og:image:height' ] > 0 ) {
187 +
188 + if ( $image_url = SucomUtil::get_first_mt_media_url( $head_info ) ) {
189 +
190 + $data[ 'thumbnail_url' ] = $image_url;
191 + $data[ 'thumbnail_width' ] = $head_info[ 'og:image:width' ];
192 + $data[ 'thumbnail_height' ] = $head_info[ 'og:image:height' ];
193 + }
194 + }
195 + }
196 +
197 + return $data;
198 + }
199 +
200 + public function post_embed_url( $embed_url, $post ) {
201 +
202 + return $embed_url;
203 + }
204 +
205 + public function post_embed_html( $output, $post, $width, $height ) {
206 +
207 + return $output;
208 + }
209 +
210 + /*
211 + * Filters the thumbnail image URL for use in the embed template.
212 + */
213 + public function the_embed_thumbnail_url( $thumbnail_url ) {
214 +
215 + global $post;
216 +
217 + if ( ! empty( $post->ID ) ) { // Just in case.
218 +
219 + if ( $this->p->debug->enabled ) {
220 +
221 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
222 + }
223 +
224 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
225 +
226 + if ( $image_url = SucomUtil::get_first_mt_media_url( $head_info ) ) {
227 +
228 + $thumbnail_url = $image_url;
229 + }
230 + }
231 +
232 + return $thumbnail_url;
233 + }
234 +
235 + public function the_embed_thumbnail_url_image_shape( $shape, $thumbnail_url ) {
236 +
237 + global $post;
238 +
239 + if ( ! empty( $post->ID ) ) { // Just in case.
240 +
241 + if ( $this->p->debug->enabled ) {
242 +
243 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
244 + }
245 +
246 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
247 +
248 + if ( ! empty( $head_info[ 'og:image:width' ] ) && ! empty( $head_info[ 'og:image:height' ] ) ) {
249 +
250 + if ( $head_info[ 'og:image:width' ] > $head_info[ 'og:image:height' ] ) {
251 +
252 + $shape = 'rectangular';
253 +
254 + } else {
255 +
256 + $shape = 'square';
257 + }
258 + }
259 + }
260 +
261 + return $shape;
262 + }
263 +
264 + /*
265 + * Filters the thumbnail image ID for use in the embed template.
266 + */
267 + public function the_embed_thumbnail_id( $pid ) {
268 +
269 + global $post;
270 +
271 + if ( ! empty( $post->ID ) ) { // Just in case.
272 +
273 + if ( $this->p->debug->enabled ) {
274 +
275 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
276 + }
277 +
278 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
279 +
280 + if ( ! empty( $head_info[ 'og:image:id' ] ) ) {
281 +
282 + $pid = $head_info[ 'og:image:id' ];
283 + }
284 + }
285 +
286 + return $pid;
287 + }
288 +
289 + /*
290 + * Filters the thumbnail image size for use in the embed template.
291 + */
292 + public function the_embed_thumbnail_image_size( $size_name, $pid ) {
293 +
294 + $size_name = 'wpsso-opengraph';
295 +
296 + return $size_name;
297 + }
298 +
299 + /*
300 + * Filters the thumbnail shape for use in the embed template.
301 + *
302 + * The 'rectangular' shape puts the image above the title (like Facebook) and the 'square' shape puts the image
303 + * below the title.
304 + */
305 + public function the_embed_thumbnail_image_shape( $shape, $pid ) {
306 +
307 + $shape = 'rectangular';
308 +
309 + return $shape;
310 + }
311 +
312 + /*
313 + * Filters the post excerpt for the embed template.
314 + *
315 + * $excerpt = get_the_excerpt();
316 + */
317 + public function the_embed_excerpt( $excerpt ) {
318 +
319 + global $post;
320 +
321 + if ( ! empty( $post->ID ) ) { // Just in case.
322 +
323 + if ( $this->p->debug->enabled ) {
324 +
325 + $this->p->debug->log( 'calling WpssoPost->get_head_info()' );
326 + }
327 +
328 + $head_info = $this->p->post->get_head_info( $post->ID ); // Uses a local cache.
329 +
330 + if ( ! empty( $head_info[ 'og:description' ] ) ) {
331 +
332 + $excerpt = $head_info[ 'og:description' ];
333 + }
334 + }
335 +
336 + return $excerpt;
337 + }
338 +
339 + public function the_embed_site_title_html( $site_title ) {
340 +
341 + return $site_title;
342 + }
343 + }
344 +}