Completed
Push — add/admin-page-package ( 7976aa...cbfc2c )
by
unknown
136:41 queued 124:01
created

functions.opengraph.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 http://ogp.me/
9
 * @link http://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_singular() ) {
98
		global $post;
99
		$data = $post; // so that we don't accidentally explode the global.
100
101
		$tags['og:type'] = 'article';
102
		if ( empty( $data->post_title ) ) {
103
			$tags['og:title'] = ' ';
104
		} else {
105
			/** This filter is documented in core/src/wp-includes/post-template.php */
106
			$tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() );
107
		}
108
109
		$tags['og:url'] = get_permalink( $data->ID );
110
		if ( ! post_password_required() ) {
111
			if ( ! empty( $data->post_excerpt ) ) {
112
				$tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) );
113
			} else {
114
				$exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content );
115
				$tags['og:description']       = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) );
116
			}
117
		}
118
		if ( empty( $tags['og:description'] ) ) {
119
				/**
120
				 * Filter the fallback `og:description` used when no excerpt information is provided.
121
				 *
122
				 * @module sharedaddy, publicize
123
				 *
124
				 * @since 3.9.0
125
				 *
126
				 * @param string $var  Fallback og:description. Default is translated `Visit the post for more'.
127
				 * @param object $data Post object for the current post.
128
				 */
129
			$tags['og:description'] = apply_filters( 'jetpack_open_graph_fallback_description', __( 'Visit the post for more.', 'jetpack' ), $data );
130
		} else {
131
			// Intentionally not using a filter to prevent pollution. @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382 .
132
			$tags['og:description'] = wp_kses( trim( convert_chars( wptexturize( $tags['og:description'] ) ) ), array() );
133
		}
134
135
		$tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) );
136
		$tags['article:modified_time']  = date( 'c', strtotime( $data->post_modified_gmt ) );
137
		if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
138
			$publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
139
			if ( ! empty( $publicize_facebook_user ) ) {
140
				$tags['article:author'] = esc_url( $publicize_facebook_user );
141
			}
142
		}
143
	}
144
145
	/**
146
	 * Allow plugins to inject additional template-specific Open Graph tags.
147
	 *
148
	 * @module sharedaddy, publicize
149
	 *
150
	 * @since 3.0.0
151
	 *
152
	 * @param array $tags Array of Open Graph Meta tags.
153
	 * @param array $args Array of image size parameters.
154
	 */
155
	$tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
156
157
	// Re-enable widont if we had disabled it.
158
	if ( $disable_widont ) {
159
		add_filter( 'the_title', 'widont' );
160
	}
161
162
	/**
163
	 * Do not return any Open Graph Meta tags if we don't have any info about a post.
164
	 *
165
	 * @module sharedaddy, publicize
166
	 *
167
	 * @since 3.0.0
168
	 *
169
	 * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post.
170
	 */
171
	if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) {
172
		return;
173
	}
174
175
	$tags['og:site_name'] = get_bloginfo( 'name' );
176
177
	// Get image info and build tags.
178
	if ( ! post_password_required() ) {
179
		$image_info       = jetpack_og_get_image( $image_width, $image_height );
180
		$tags['og:image'] = $image_info['src'];
181
182
		if ( ! empty( $image_info['width'] ) ) {
183
			$tags['og:image:width'] = (int) $image_info['width'];
184
		}
185
		if ( ! empty( $image_info['height'] ) ) {
186
			$tags['og:image:height'] = (int) $image_info['height'];
187
		}
188
		if ( ! empty( $image_info['alt_text'] ) ) {
189
			$tags['og:image:alt'] = esc_attr( $image_info['alt_text'] );
190
		}
191
	}
192
193
	// Facebook whines if you give it an empty title.
194
	if ( empty( $tags['og:title'] ) ) {
195
		$tags['og:title'] = __( '(no title)', 'jetpack' );
196
	}
197
198
	// Shorten the description if it's too long.
199
	if ( isset( $tags['og:description'] ) ) {
200
		$tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '…' : $tags['og:description'];
201
	}
202
203
	// Try to add OG locale tag if the WP->FB data mapping exists.
204
	if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
205
		require_once JETPACK__GLOTPRESS_LOCALES_PATH;
206
		$_locale = get_locale();
207
208
		// We have to account for w.org vs WP.com locale divergence.
209
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
210
			$gp_locale = GP_Locales::by_field( 'slug', $_locale );
211
		} else {
212
			$gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
213
		}
214
	}
215
216
	if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) {
217
		$tags['og:locale'] = $gp_locale->facebook_locale;
218
	}
219
220
	/**
221
	 * Allow the addition of additional Open Graph Meta tags, or modify the existing tags.
222
	 *
223
	 * @module sharedaddy, publicize
224
	 *
225
	 * @since 2.0.0
226
	 *
227
	 * @param array $tags Array of Open Graph Meta tags.
228
	 * @param array $args Array of image size parameters.
229
	 */
230
	$tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
231
232
	// secure_urls need to go right after each og:image to work properly so we will abstract them here.
233
	$tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
234
	$secure                      = $tags['og:image:secure_url'];
235
	unset( $tags['og:image:secure_url'] );
236
	$secure_image_num = 0;
237
238
	foreach ( (array) $tags as $tag_property => $tag_content ) {
239
		// to accommodate multiple images.
240
		$tag_content = (array) $tag_content;
241
		$tag_content = array_unique( $tag_content );
242
243
		foreach ( $tag_content as $tag_content_single ) {
244
			if ( empty( $tag_content_single ) ) {
245
				continue; // Don't ever output empty tags.
246
			}
247
			$og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
248
			/**
249
			 * Filter the HTML Output of each Open Graph Meta tag.
250
			 *
251
			 * @module sharedaddy, publicize
252
			 *
253
			 * @since 2.0.0
254
			 *
255
			 * @param string $og_tag HTML HTML Output of each Open Graph Meta tag.
256
			 */
257
			$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
258
			$og_output .= "\n";
259
260
			if ( 'og:image' === $tag_property ) {
261
				if ( is_array( $secure ) && ! empty( $secure[ $secure_image_num ] ) ) {
262
					$og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
263
					/** This filter is documented in functions.opengraph.php */
264
					$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
265
					$og_output .= "\n";
266
				} elseif ( ! is_array( $secure ) && ! empty( $secure ) ) {
267
					$og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
268
					/** This filter is documented in functions.opengraph.php */
269
					$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
270
					$og_output .= "\n";
271
				}
272
				$secure_image_num++;
273
			}
274
		}
275
	}
276
	$og_output .= "\n<!-- End Jetpack Open Graph Tags -->\n";
277
	// This is trusted output or added by a filter.
278
	echo $og_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
279
}
280
281
/**
282
 * Returns an image used in social shares.
283
 *
284
 * @since 2.0.0
285
 *
286
 * @param int  $width Minimum width for the image. Default is 200 based on Facebook's requirement.
287
 * @param int  $height Minimum height for the image. Default is 200 based on Facebook's requirement.
288
 * @param null $deprecated Deprecated.
289
 *
290
 * @return array The source ('src'), 'width', and 'height' of the image.
291
 */
292
function jetpack_og_get_image( $width = 200, $height = 200, $deprecated = null ) {
293
	if ( ! empty( $deprecated ) ) {
294
		_deprecated_argument( __FUNCTION__, '6.6.0' );
295
	}
296
	$image = array();
297
298
	if ( is_singular() && ! is_home() ) {
299
		// Grab obvious image if post is an attachment page for an image.
300
		if ( is_attachment( get_the_ID() ) && 'image' === substr( get_post_mime_type(), 0, 5 ) ) {
301
			$image['src'] = wp_get_attachment_url( get_the_ID() );
302
		}
303
304
		// Attempt to find something good for this post using our generalized PostImages code.
305
		if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) {
306
			$post_images = Jetpack_PostImages::get_images(
307
				get_the_ID(),
308
				array(
309
					'width'  => $width,
310
					'height' => $height,
311
				)
312
			);
313
			if ( $post_images && ! is_wp_error( $post_images ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post_images of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
314
				foreach ( (array) $post_images as $post_image ) {
315
					$image['src'] = $post_image['src'];
316 View Code Duplication
					if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
317
						$image['width']  = $post_image['src_width'];
318
						$image['height'] = $post_image['src_height'];
319
					}
320
					if ( ! empty( $post_image['alt_text'] ) ) {
321
						$image['alt_text'] = $post_image['alt_text'];
322
					}
323
				}
324
			}
325
		}
326
	} elseif ( is_author() ) {
327
		$author = get_queried_object();
328
		if ( is_a( $author, 'WP_User' ) ) {
329
			$image['src'] = get_avatar_url(
330
				$author->user_email,
331
				array(
332
					'size' => $width,
333
				)
334
			);
335
		}
336
	}
337
338
	// First fall back, blavatar.
339
	if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
340
		$blavatar_domain = blavatar_domain( site_url() );
341
		if ( blavatar_exists( $blavatar_domain ) ) {
342
			$image['src']    = blavatar_url( $blavatar_domain, 'img', $width, false, true );
343
			$image['width']  = $width;
344
			$image['height'] = $height;
345
		}
346
	}
347
348
	// Second fall back, Site Logo.
349 View Code Duplication
	if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) {
350
		$image_id = jetpack_get_site_logo( 'id' );
351
		$logo     = wp_get_attachment_image_src( $image_id, 'full' );
352
		if (
353
			isset( $logo[0], $logo[1], $logo[2] )
354
			&& ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) )
355
		) {
356
			$image['src']    = $logo[0];
357
			$image['width']  = $logo[1];
358
			$image['height'] = $logo[2];
359
		}
360
	}
361
362
	// Third fall back, Core Site Icon, if valid in size.
363 View Code Duplication
	if ( empty( $image ) && has_site_icon() ) {
364
		$image_id = get_option( 'site_icon' );
365
		$icon     = wp_get_attachment_image_src( $image_id, 'full' );
366
		if (
367
			isset( $icon[0], $icon[1], $icon[2] )
368
			&& ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) )
369
		) {
370
			$image['src']    = $icon[0];
371
			$image['width']  = $icon[1];
372
			$image['height'] = $icon[2];
373
		}
374
	}
375
376
	// Final fall back, blank image.
377
	if ( empty( $image ) ) {
378
		/**
379
		 * Filter the default Open Graph Image tag, used when no Image can be found in a post.
380
		 *
381
		 * @since 3.0.0
382
		 *
383
		 * @param string $str Default Image URL.
384
		 */
385
		$image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
386
	}
387
388
	return $image;
389
}
390
391
392
/**
393
 * Validate the width and height against required width and height
394
 *
395
 * @param int $width      Width of the image.
396
 * @param int $height     Height of the image.
397
 * @param int $req_width  Required width to pass validation.
398
 * @param int $req_height Required height to pass validation.
399
 *
400
 * @return bool - True if the image passed the required size validation
401
 */
402
function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) {
403
	if ( ! $width || ! $height ) {
404
		return false;
405
	}
406
407
	$valid_width         = ( $width >= $req_width );
408
	$valid_height        = ( $height >= $req_height );
409
	$is_image_acceptable = $valid_width && $valid_height;
410
411
	return $is_image_acceptable;
412
}
413
414
/**
415
 * Gets a gravatar URL of the specified size.
416
 *
417
 * @param string $email E-mail address to get gravatar for.
418
 * @param int    $width Size of returned gravatar.
419
 * @return array|bool|mixed|string
420
 */
421
function jetpack_og_get_image_gravatar( $email, $width ) {
422
	return get_avatar_url(
423
		$email,
424
		array(
425
			'size' => $width,
426
		)
427
	);
428
}
429