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