PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.6.4
Jetpack – WP Security, Backup, Speed, & Growth v3.6.4
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 3.6.4, at functions.opengraph.php

295 lines 10.6 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 http://ogp.me/
9 * @link http://developers.facebook.com/docs/opengraph/
10 */
11 add_action( 'wp_head', 'jetpack_og_tags' );
12
13 function jetpack_og_tags() {
14 if ( false === apply_filters( 'jetpack_enable_opengraph', true ) ) {
15 _deprecated_function( 'jetpack_enable_opengraph', '2.0.3', 'jetpack_enable_open_graph' );
16 return;
17 }
18
19 // Disable the widont filter on WP.com to avoid stray &nbsps
20 $disable_widont = remove_filter( 'the_title', 'widont' );
21
22 $og_output = "\n<!-- Jetpack Open Graph Tags -->\n";
23 $tags = array();
24
25 $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) );
26 $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) );
27 $description_length = 197;
28
29 if ( is_home() || is_front_page() ) {
30 $site_type = get_option( 'open_graph_protocol_site_type' );
31 $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website';
32 $tags['og:title'] = get_bloginfo( 'name' );
33 $tags['og:description'] = get_bloginfo( 'description' );
34
35 $front_page_id = get_option( 'page_for_posts' );
36 if ( $front_page_id && is_home() )
37 $tags['og:url'] = get_permalink( $front_page_id );
38 else
39 $tags['og:url'] = home_url( '/' );
40
41 // Associate a blog's root path with one or more Facebook accounts
42 $facebook_admins = get_option( 'facebook_admins' );
43 if ( ! empty( $facebook_admins ) )
44 $tags['fb:admins'] = $facebook_admins;
45
46 } else if ( is_author() ) {
47 $tags['og:type'] = 'profile';
48
49 $author = get_queried_object();
50
51 $tags['og:title'] = $author->display_name;
52 if ( ! empty( $author->user_url ) ) {
53 $tags['og:url'] = $author->user_url;
54 } else {
55 $tags['og:url'] = get_author_posts_url( $author->ID );
56 }
57 $tags['og:description'] = $author->description;
58 $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID );
59 $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID );
60
61 } else if ( is_singular() ) {
62 global $post;
63 $data = $post; // so that we don't accidentally explode the global
64
65 $tags['og:type'] = 'article';
66 $tags['og:title'] = empty( $data->post_title ) ? ' ' : wp_kses( $data->post_title, array() ) ;
67 $tags['og:url'] = get_permalink( $data->ID );
68 if ( ! post_password_required() ) {
69 if ( ! empty( $data->post_excerpt ) ) {
70 $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) );
71 } else {
72 $exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content );
73 $tags['og:description'] = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) );
74 }
75 }
76 if ( empty( $tags['og:description'] ) )
77 $tags['og:description'] = __('Visit the post for more.', 'jetpack');
78 $tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) );
79 $tags['article:modified_time'] = date( 'c', strtotime( $data->post_modified_gmt ) );
80 if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
81 $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
82 if ( ! empty( $publicize_facebook_user ) ) {
83 $tags['article:author'] = esc_url( $publicize_facebook_user );
84 }
85 }
86 }
87
88 // Allow plugins to inject additional template-specific open graph tags
89 $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
90
91 // Re-enable widont if we had disabled it
92 if ( $disable_widont )
93 add_filter( 'the_title', 'widont' );
94
95 if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) )
96 return;
97
98 $tags['og:site_name'] = get_bloginfo( 'name' );
99
100 // Get image info and build tags
101 if ( ! post_password_required() ) {
102 $image_info = jetpack_og_get_image( $image_width, $image_height );
103 $tags['og:image'] = $image_info['src'];
104
105 if ( ! empty( $image_info['width'] ) ) {
106 $tags['og:image:width'] = $image_info['width'];
107 }
108 if ( ! empty( $image_info['height'] ) ) {
109 $tags['og:image:height'] = $image_info['height'];
110 }
111 }
112
113 // Facebook whines if you give it an empty title
114 if ( empty( $tags['og:title'] ) )
115 $tags['og:title'] = __( '(no title)', 'jetpack' );
116
117 // Shorten the description if it's too long
118 if ( isset( $tags['og:description'] ) ) {
119 $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];
120 }
121
122 // Try to add OG locale tag if the WP->FB data mapping exists
123 if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
124 require_once JETPACK__GLOTPRESS_LOCALES_PATH;
125 $_locale = get_locale();
126
127 // We have to account for WP.org vs WP.com locale divergence
128 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
129 $gp_locale = GP_Locales::by_field( 'slug', $_locale );
130 } else {
131 $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
132 }
133 }
134
135 if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) {
136 $tags['og:locale'] = $gp_locale->facebook_locale;
137 }
138
139 // Add any additional tags here, or modify what we've come up with
140 $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
141
142 // secure_urls need to go right after each og:image to work properly so we will abstract them here
143 $secure = $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
144 unset( $tags['og:image:secure_url'] );
145 $secure_image_num = 0;
146
147 foreach ( (array) $tags as $tag_property => $tag_content ) {
148 // to accommodate multiple images
149 $tag_content = (array) $tag_content;
150 $tag_content = array_unique( $tag_content );
151
152 foreach ( $tag_content as $tag_content_single ) {
153 if ( empty( $tag_content_single ) )
154 continue; // Don't ever output empty tags
155 $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
156 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
157 $og_output .= "\n";
158
159 if ( 'og:image' == $tag_property ) {
160 if ( is_array( $secure ) && !empty( $secure[$secure_image_num] ) ) {
161 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
162 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
163 $og_output .= "\n";
164 } else if ( !is_array( $secure ) && !empty( $secure ) ) {
165 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
166 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
167 $og_output .= "\n";
168 }
169 $secure_image_num++;
170 }
171 }
172 }
173 echo $og_output;
174 }
175
176 function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
177 $image = '';
178
179 if ( is_singular() && ! is_home() ) {
180 global $post;
181 $image = '';
182
183 // Attempt to find something good for this post using our generalized PostImages code
184 if ( class_exists( 'Jetpack_PostImages' ) ) {
185 $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
186 if ( $post_images && ! is_wp_error( $post_images ) ) {
187 $image = array();
188 foreach ( (array) $post_images as $post_image ) {
189 $image['src'] = $post_image['src'];
190 if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
191 $image['width'] = $post_image['src_width'];
192 $image['height'] = $post_image['src_height'];
193 }
194 }
195 }
196 }
197 } else if ( is_author() ) {
198 $author = get_queried_object();
199 if ( function_exists( 'get_avatar_url' ) ) {
200 // Prefer the core function get_avatar_url() if available, WP 4.2+
201 $image['src'] = get_avatar_url( $author->user_email, array( 'size' => $width ) );
202 }
203 else {
204 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
205 if ( ! $has_filter ) {
206 add_filter( 'pre_option_show_avatars', '__return_true' );
207 }
208 $avatar = get_avatar( $author->user_email, $width );
209 if ( ! $has_filter ) {
210 remove_filter( 'pre_option_show_avatars', '__return_true' );
211 }
212
213 if ( ! empty( $avatar ) && ! is_wp_error( $avatar ) ) {
214 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) );
215 $image['src'] = wp_specialchars_decode( $matches[1], ENT_QUOTES );
216 }
217 }
218 }
219
220 if ( empty( $image ) ) {
221 $image = array();
222 } else if ( ! is_array( $image ) ) {
223 $image = array(
224 'src' => $image
225 );
226 }
227
228 // First fall back, blavatar
229 if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
230 $blavatar_domain = blavatar_domain( site_url() );
231 if ( blavatar_exists( $blavatar_domain ) ) {
232 $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true );
233 $image['width'] = $width;
234 $image['height'] = $height;
235 }
236 }
237
238 // Second fall back, Site Logo
239 if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) {
240 $image['src'] = jetpack_get_site_logo( 'url' );
241 }
242
243 // Third fall back, Site Icon
244 if ( empty( $image ) && ( function_exists( 'jetpack_has_site_icon' ) && jetpack_has_site_icon() ) ) {
245 $image['src'] = jetpack_site_icon_url( null, '512' );
246 }
247
248 // Fourth fall back, Core Site Icon. Added in WP 4.3.
249 if ( empty( $image ) && ( function_exists( 'has_site_icon') && has_site_icon() ) ) {
250 $image['src'] = get_site_icon_url( null, '512' );
251 }
252
253 // Finally fall back, blank image
254 if ( empty( $image ) ) {
255 $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
256 }
257
258 return $image;
259 }
260
261 /**
262 * @param $email
263 * @param $width
264 * @return array|bool|mixed|string
265 */
266 function jetpack_og_get_image_gravatar( $email, $width ) {
267 $image = '';
268 if ( function_exists( 'get_avatar_url' ) ) {
269 $avatar = get_avatar_url( $email, $width );
270 if ( ! empty( $avatar ) ) {
271 if ( is_array( $avatar ) )
272 $image = $avatar[0];
273 else
274 $image = $avatar;
275 }
276 } else {
277 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
278 if ( !$has_filter ) {
279 add_filter( 'pre_option_show_avatars', '__return_true' );
280 }
281 $avatar = get_avatar( $email, $width );
282
283 if ( !$has_filter ) {
284 remove_filter( 'pre_option_show_avatars', '__return_true' );
285 }
286
287 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
288 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) )
289 $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
290 }
291 }
292
293 return $image;
294 }
295