PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.7
Jetpack – WP Security, Backup, Speed, & Growth v7.7
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / functions.opengraph.php

functions.opengraph.php in Jetpack – WP Security, Backup, Speed, & Growth 7.7, at functions.opengraph.php

443 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Open Graph Tags
4 *
5 * Add Open Graph tags so that Facebook (and any other service that supports them)
6 * can crawl the site better and we provide a better sharing experience.
7 *
8 * @link https://ogp.me/
9 * @link https://developers.facebook.com/docs/opengraph/
10 *
11 * @package Jetpack
12 */
13
14 add_action( 'wp_head', 'jetpack_og_tags' );
15
16 /**
17 * Outputs Open Graph tags generated by Jetpack.
18 */
19 function jetpack_og_tags() {
20 /**
21 * Allow Jetpack to output Open Graph Meta Tags.
22 *
23 * @module sharedaddy, publicize
24 *
25 * @since 2.0.0
26 * @deprecated 2.0.3 Duplicative filter. Use `jetpack_enable_open_graph`.
27 *
28 * @param bool true Should Jetpack's Open Graph Meta Tags be enabled. Default to true.
29 */
30 if ( false === apply_filters( 'jetpack_enable_opengraph', true ) ) {
31 _deprecated_function( 'jetpack_enable_opengraph', '2.0.3', 'jetpack_enable_open_graph' );
32 return;
33 }
34
35 // Disable the widont filter on WP.com to avoid stray &nbsps.
36 $disable_widont = remove_filter( 'the_title', 'widont' );
37
38 $og_output = "\n<!-- Jetpack Open Graph Tags -->\n";
39 $tags = array();
40
41 /**
42 * Filter the minimum width of the images used in Jetpack Open Graph Meta Tags.
43 *
44 * @module sharedaddy, publicize
45 *
46 * @since 2.0.0
47 *
48 * @param int 200 Minimum image width used in Jetpack Open Graph Meta Tags.
49 */
50 $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) );
51 /**
52 * Filter the minimum height of the images used in Jetpack Open Graph Meta Tags.
53 *
54 * @module sharedaddy, publicize
55 *
56 * @since 2.0.0
57 *
58 * @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags.
59 */
60 $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) );
61 $description_length = 197;
62
63 if ( is_home() || is_front_page() ) {
64 $site_type = Jetpack_Options::get_option_and_ensure_autoload( 'open_graph_protocol_site_type', '' );
65 $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website';
66 $tags['og:title'] = get_bloginfo( 'name' );
67 $tags['og:description'] = get_bloginfo( 'description' );
68
69 $front_page_id = get_option( 'page_for_posts' );
70 if ( 'page' === get_option( 'show_on_front' ) && $front_page_id && is_home() ) {
71 $tags['og:url'] = get_permalink( $front_page_id );
72 } else {
73 $tags['og:url'] = home_url( '/' );
74 }
75
76 // Associate a blog's root path with one or more Facebook accounts.
77 $facebook_admins = Jetpack_Options::get_option_and_ensure_autoload( 'facebook_admins', array() );
78 if ( ! empty( $facebook_admins ) ) {
79 $tags['fb:admins'] = $facebook_admins;
80 }
81 } elseif ( is_author() ) {
82 $tags['og:type'] = 'profile';
83
84 $author = get_queried_object();
85
86 if ( is_a( $author, 'WP_User' ) ) {
87 $tags['og:title'] = $author->display_name;
88 if ( ! empty( $author->user_url ) ) {
89 $tags['og:url'] = $author->user_url;
90 } else {
91 $tags['og:url'] = get_author_posts_url( $author->ID );
92 }
93 $tags['og:description'] = $author->description;
94 $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID );
95 $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID );
96 }
97 } elseif ( is_archive() ) {
98 $tags['og:type'] = 'website';
99 $tags['og:title'] = wp_get_document_title();
100
101 $archive = get_queried_object();
102 if ( ! empty( $archive ) ) {
103 if ( is_category() || is_tag() || is_tax() ) {
104 $tags['og:url'] = get_term_link( $archive->term_id, $archive->taxonomy );
105 $tags['og:description'] = $archive->description;
106 } elseif ( is_post_type_archive() ) {
107 $tags['og:url'] = get_post_type_archive_link( $archive->name );
108 $tags['og:description'] = $archive->description;
109 }
110 }
111 } elseif ( is_singular() ) {
112 global $post;
113 $data = $post; // so that we don't accidentally explode the global.
114
115 $tags['og:type'] = 'article';
116 if ( empty( $data->post_title ) ) {
117 $tags['og:title'] = ' ';
118 } else {
119 /** This filter is documented in core/src/wp-includes/post-template.php */
120 $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() );
121 }
122
123 $tags['og:url'] = get_permalink( $data->ID );
124 if ( ! post_password_required() ) {
125 if ( ! empty( $data->post_excerpt ) ) {
126 $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) );
127 } else {
128 $exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content );
129 $tags['og:description'] = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) );
130 }
131 }
132 if ( empty( $tags['og:description'] ) ) {
133 /**
134 * Filter the fallback `og:description` used when no excerpt information is provided.
135 *
136 * @module sharedaddy, publicize
137 *
138 * @since 3.9.0
139 *
140 * @param string $var Fallback og:description. Default is translated `Visit the post for more'.
141 * @param object $data Post object for the current post.
142 */
143 $tags['og:description'] = apply_filters( 'jetpack_open_graph_fallback_description', __( 'Visit the post for more.', 'jetpack' ), $data );
144 } else {
145 // Intentionally not using a filter to prevent pollution. @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382 .
146 $tags['og:description'] = wp_kses( trim( convert_chars( wptexturize( $tags['og:description'] ) ) ), array() );
147 }
148
149 $tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) );
150 $tags['article:modified_time'] = date( 'c', strtotime( $data->post_modified_gmt ) );
151 if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
152 $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
153 if ( ! empty( $publicize_facebook_user ) ) {
154 $tags['article:author'] = esc_url( $publicize_facebook_user );
155 }
156 }
157 }
158
159 /**
160 * Allow plugins to inject additional template-specific Open Graph tags.
161 *
162 * @module sharedaddy, publicize
163 *
164 * @since 3.0.0
165 *
166 * @param array $tags Array of Open Graph Meta tags.
167 * @param array $args Array of image size parameters.
168 */
169 $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
170
171 // Re-enable widont if we had disabled it.
172 if ( $disable_widont ) {
173 add_filter( 'the_title', 'widont' );
174 }
175
176 /**
177 * Do not return any Open Graph Meta tags if we don't have any info about a post.
178 *
179 * @module sharedaddy, publicize
180 *
181 * @since 3.0.0
182 *
183 * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post.
184 */
185 if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) {
186 return;
187 }
188
189 $tags['og:site_name'] = get_bloginfo( 'name' );
190
191 // Get image info and build tags.
192 if ( ! post_password_required() ) {
193 $image_info = jetpack_og_get_image( $image_width, $image_height );
194 $tags['og:image'] = $image_info['src'];
195
196 if ( ! empty( $image_info['width'] ) ) {
197 $tags['og:image:width'] = (int) $image_info['width'];
198 }
199 if ( ! empty( $image_info['height'] ) ) {
200 $tags['og:image:height'] = (int) $image_info['height'];
201 }
202 if ( ! empty( $image_info['alt_text'] ) ) {
203 $tags['og:image:alt'] = esc_attr( $image_info['alt_text'] );
204 }
205 }
206
207 // Facebook whines if you give it an empty title.
208 if ( empty( $tags['og:title'] ) ) {
209 $tags['og:title'] = __( '(no title)', 'jetpack' );
210 }
211
212 // Shorten the description if it's too long.
213 if ( isset( $tags['og:description'] ) ) {
214 $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '' : $tags['og:description'];
215 }
216
217 // Try to add OG locale tag if the WP->FB data mapping exists.
218 if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
219 require_once JETPACK__GLOTPRESS_LOCALES_PATH;
220 $_locale = get_locale();
221
222 // We have to account for w.org vs WP.com locale divergence.
223 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
224 $gp_locale = GP_Locales::by_field( 'slug', $_locale );
225 } else {
226 $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
227 }
228 }
229
230 if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) {
231 $tags['og:locale'] = $gp_locale->facebook_locale;
232 }
233
234 /**
235 * Allow the addition of additional Open Graph Meta tags, or modify the existing tags.
236 *
237 * @module sharedaddy, publicize
238 *
239 * @since 2.0.0
240 *
241 * @param array $tags Array of Open Graph Meta tags.
242 * @param array $args Array of image size parameters.
243 */
244 $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
245
246 // secure_urls need to go right after each og:image to work properly so we will abstract them here.
247 $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
248 $secure = $tags['og:image:secure_url'];
249 unset( $tags['og:image:secure_url'] );
250 $secure_image_num = 0;
251
252 foreach ( (array) $tags as $tag_property => $tag_content ) {
253 // to accommodate multiple images.
254 $tag_content = (array) $tag_content;
255 $tag_content = array_unique( $tag_content );
256
257 foreach ( $tag_content as $tag_content_single ) {
258 if ( empty( $tag_content_single ) ) {
259 continue; // Don't ever output empty tags.
260 }
261 $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
262 /**
263 * Filter the HTML Output of each Open Graph Meta tag.
264 *
265 * @module sharedaddy, publicize
266 *
267 * @since 2.0.0
268 *
269 * @param string $og_tag HTML HTML Output of each Open Graph Meta tag.
270 */
271 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
272 $og_output .= "\n";
273
274 if ( 'og:image' === $tag_property ) {
275 if ( is_array( $secure ) && ! empty( $secure[ $secure_image_num ] ) ) {
276 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
277 /** This filter is documented in functions.opengraph.php */
278 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
279 $og_output .= "\n";
280 } elseif ( ! is_array( $secure ) && ! empty( $secure ) ) {
281 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
282 /** This filter is documented in functions.opengraph.php */
283 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
284 $og_output .= "\n";
285 }
286 $secure_image_num++;
287 }
288 }
289 }
290 $og_output .= "\n<!-- End Jetpack Open Graph Tags -->\n";
291 // This is trusted output or added by a filter.
292 echo $og_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
293 }
294
295 /**
296 * Returns an image used in social shares.
297 *
298 * @since 2.0.0
299 *
300 * @param int $width Minimum width for the image. Default is 200 based on Facebook's requirement.
301 * @param int $height Minimum height for the image. Default is 200 based on Facebook's requirement.
302 * @param null $deprecated Deprecated.
303 *
304 * @return array The source ('src'), 'width', and 'height' of the image.
305 */
306 function jetpack_og_get_image( $width = 200, $height = 200, $deprecated = null ) {
307 if ( ! empty( $deprecated ) ) {
308 _deprecated_argument( __FUNCTION__, '6.6.0' );
309 }
310 $image = array();
311
312 if ( is_singular() && ! is_home() ) {
313 // Grab obvious image if post is an attachment page for an image.
314 if ( is_attachment( get_the_ID() ) && 'image' === substr( get_post_mime_type(), 0, 5 ) ) {
315 $image['src'] = wp_get_attachment_url( get_the_ID() );
316 }
317
318 // Attempt to find something good for this post using our generalized PostImages code.
319 if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) {
320 $post_images = Jetpack_PostImages::get_images(
321 get_the_ID(),
322 array(
323 'width' => $width,
324 'height' => $height,
325 )
326 );
327 if ( $post_images && ! is_wp_error( $post_images ) ) {
328 foreach ( (array) $post_images as $post_image ) {
329 $image['src'] = $post_image['src'];
330 if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
331 $image['width'] = $post_image['src_width'];
332 $image['height'] = $post_image['src_height'];
333 }
334 if ( ! empty( $post_image['alt_text'] ) ) {
335 $image['alt_text'] = $post_image['alt_text'];
336 }
337 }
338 }
339 }
340 } elseif ( is_author() ) {
341 $author = get_queried_object();
342 if ( is_a( $author, 'WP_User' ) ) {
343 $image['src'] = get_avatar_url(
344 $author->user_email,
345 array(
346 'size' => $width,
347 )
348 );
349 }
350 }
351
352 // First fall back, blavatar.
353 if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
354 $blavatar_domain = blavatar_domain( site_url() );
355 if ( blavatar_exists( $blavatar_domain ) ) {
356 $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true );
357 $image['width'] = $width;
358 $image['height'] = $height;
359 }
360 }
361
362 // Second fall back, Site Logo.
363 if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) {
364 $image_id = jetpack_get_site_logo( 'id' );
365 $logo = wp_get_attachment_image_src( $image_id, 'full' );
366 if (
367 isset( $logo[0], $logo[1], $logo[2] )
368 && ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) )
369 ) {
370 $image['src'] = $logo[0];
371 $image['width'] = $logo[1];
372 $image['height'] = $logo[2];
373 }
374 }
375
376 // Third fall back, Core Site Icon, if valid in size.
377 if ( empty( $image ) && has_site_icon() ) {
378 $image_id = get_option( 'site_icon' );
379 $icon = wp_get_attachment_image_src( $image_id, 'full' );
380 if (
381 isset( $icon[0], $icon[1], $icon[2] )
382 && ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) )
383 ) {
384 $image['src'] = $icon[0];
385 $image['width'] = $icon[1];
386 $image['height'] = $icon[2];
387 }
388 }
389
390 // Final fall back, blank image.
391 if ( empty( $image ) ) {
392 /**
393 * Filter the default Open Graph Image tag, used when no Image can be found in a post.
394 *
395 * @since 3.0.0
396 *
397 * @param string $str Default Image URL.
398 */
399 $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
400 }
401
402 return $image;
403 }
404
405
406 /**
407 * Validate the width and height against required width and height
408 *
409 * @param int $width Width of the image.
410 * @param int $height Height of the image.
411 * @param int $req_width Required width to pass validation.
412 * @param int $req_height Required height to pass validation.
413 *
414 * @return bool - True if the image passed the required size validation
415 */
416 function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) {
417 if ( ! $width || ! $height ) {
418 return false;
419 }
420
421 $valid_width = ( $width >= $req_width );
422 $valid_height = ( $height >= $req_height );
423 $is_image_acceptable = $valid_width && $valid_height;
424
425 return $is_image_acceptable;
426 }
427
428 /**
429 * Gets a gravatar URL of the specified size.
430 *
431 * @param string $email E-mail address to get gravatar for.
432 * @param int $width Size of returned gravatar.
433 * @return array|bool|mixed|string
434 */
435 function jetpack_og_get_image_gravatar( $email, $width ) {
436 return get_avatar_url(
437 $email,
438 array(
439 'size' => $width,
440 )
441 );
442 }
443