PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.2.5
Jetpack – WP Security, Backup, Speed, & Growth v3.2.5
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
245 lines 8.8 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 $tags['og:description'] = ! empty( $data->post_excerpt ) ? preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) ): wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_content, array() ) ) ) );
70 if ( empty( $tags['og:description'] ) )
71 $tags['og:description'] = __('Visit the post for more.', 'jetpack');
72 $tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) );
73 $tags['article:modified_time'] = date( 'c', strtotime( $data->post_modified_gmt ) );
74 if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
75 $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
76 if ( ! empty( $publicize_facebook_user ) ) {
77 $tags['article:author'] = esc_url( $publicize_facebook_user );
78 } else {
79 $tags['article:author'] = get_author_posts_url( $data->post_author );
80 }
81 }
82 }
83
84 // Allow plugins to inject additional template-specific open graph tags
85 $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
86
87 // Re-enable widont if we had disabled it
88 if ( $disable_widont )
89 add_filter( 'the_title', 'widont' );
90
91 if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) )
92 return;
93
94 $tags['og:site_name'] = get_bloginfo( 'name' );
95
96 if ( !post_password_required() )
97 $tags['og:image'] = jetpack_og_get_image( $image_width, $image_height );
98
99 // Facebook whines if you give it an empty title
100 if ( empty( $tags['og:title'] ) )
101 $tags['og:title'] = __( '(no title)', 'jetpack' );
102
103 // Shorten the description if it's too long
104 if ( isset( $tags['og:description'] ) ) {
105 $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];
106 }
107
108 // Add any additional tags here, or modify what we've come up with
109 $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
110
111 // secure_urls need to go right after each og:image to work properly so we will abstract them here
112 $secure = $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
113 unset( $tags['og:image:secure_url'] );
114 $secure_image_num = 0;
115
116 foreach ( (array) $tags as $tag_property => $tag_content ) {
117 // to accommodate multiple images
118 $tag_content = (array) $tag_content;
119 $tag_content = array_unique( $tag_content );
120
121 foreach ( $tag_content as $tag_content_single ) {
122 if ( empty( $tag_content_single ) )
123 continue; // Don't ever output empty tags
124 $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
125 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
126 $og_output .= "\n";
127
128 if ( 'og:image' == $tag_property ) {
129 if ( is_array( $secure ) && !empty( $secure[$secure_image_num] ) ) {
130 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
131 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
132 $og_output .= "\n";
133 } else if ( !is_array( $secure ) && !empty( $secure ) ) {
134 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
135 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
136 $og_output .= "\n";
137 }
138 $secure_image_num++;
139 }
140 }
141 }
142 echo $og_output;
143 }
144
145 function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
146 $image = '';
147
148 if ( is_singular() && !is_home() ) {
149 global $post;
150 $image = '';
151
152 // Attempt to find something good for this post using our generalized PostImages code
153 if ( class_exists( 'Jetpack_PostImages' ) ) {
154 $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
155 if ( $post_images && !is_wp_error( $post_images ) ) {
156 $image = array();
157 foreach ( (array) $post_images as $post_image ) {
158 $image[] = $post_image['src'];
159 }
160 }
161 }
162 } else if ( is_author() ) {
163 $author = get_queried_object();
164 if ( function_exists( 'get_avatar_url' ) ) {
165 $avatar = get_avatar_url( $author->user_email, $width );
166
167 if ( ! empty( $avatar ) ) {
168 if ( is_array( $avatar ) )
169 $image = $avatar[0];
170 else
171 $image = $avatar;
172 }
173 }
174 else {
175 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
176 if ( !$has_filter ) {
177 add_filter( 'pre_option_show_avatars', '__return_true' );
178 }
179 $avatar = get_avatar( $author->user_email, $width );
180 if ( !$has_filter ) {
181 remove_filter( 'pre_option_show_avatars', '__return_true' );
182 }
183
184 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
185 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) );
186 $image = wp_specialchars_decode( $matches[1], ENT_QUOTES );
187 }
188 }
189 }
190
191 if ( empty( $image ) )
192 $image = array();
193 else if ( !is_array( $image ) )
194 $image = array( $image );
195
196 // First fall back, blavatar
197 if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
198 $blavatar_domain = blavatar_domain( site_url() );
199 if ( blavatar_exists( $blavatar_domain ) )
200 $image[] = blavatar_url( $blavatar_domain, 'img', $width );
201 }
202
203 // Second fall back, blank image
204 if ( empty( $image ) ) {
205 $image[] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
206 }
207
208 return $image;
209 }
210
211 /**
212 * @param $email
213 * @param $width
214 * @return array|bool|mixed|string
215 */
216 function jetpack_og_get_image_gravatar( $email, $width ) {
217 $image = '';
218 if ( function_exists( 'get_avatar_url' ) ) {
219 $avatar = get_avatar_url($email, $width);
220 if ( ! empty( $avatar ) ) {
221 if ( is_array( $avatar ) )
222 $image = $avatar[0];
223 else
224 $image = $avatar;
225 }
226 } else {
227 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
228 if ( !$has_filter ) {
229 add_filter( 'pre_option_show_avatars', '__return_true' );
230 }
231 $avatar = get_avatar( $email, $width );
232
233 if ( !$has_filter ) {
234 remove_filter( 'pre_option_show_avatars', '__return_true' );
235 }
236
237 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
238 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) )
239 $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
240 }
241 }
242
243 return $image;
244 }
245