PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.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
223 lines 7.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 : 'blog';
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 $tags['og:url'] = get_author_posts_url( $author->ID );
53 $tags['og:description'] = $author->description;
54 $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID );
55 $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID );
56
57 } else if ( is_singular() ) {
58 global $post;
59 $data = $post; // so that we don't accidentally explode the global
60
61 $tags['og:type'] = 'article';
62 $tags['og:title'] = empty( $data->post_title ) ? ' ' : wp_kses( $data->post_title, array() ) ;
63 $tags['og:url'] = get_permalink( $data->ID );
64 if ( !post_password_required() )
65 $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() ) ) ) );
66 $tags['og:description'] = empty( $tags['og:description'] ) ? ' ' : $tags['og:description'];
67 }
68
69 // Re-enable widont if we had disabled it
70 if ( $disable_widont )
71 add_filter( 'the_title', 'widont' );
72
73 if ( empty( $tags ) )
74 return;
75
76 $tags['og:site_name'] = get_bloginfo( 'name' );
77 $tags['og:image'] = jetpack_og_get_image( $image_width, $image_height );
78
79 // Facebook whines if you give it an empty title
80 if ( empty( $tags['og:title'] ) )
81 $tags['og:title'] = __( '(no title)', 'jetpack' );
82
83 // Shorten the description if it's too long
84 $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];
85
86 // Add any additional tags here, or modify what we've come up with
87 $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
88
89 // secure_urls need to go right after each og:image to work properly so we will abstract them here
90 $secure = $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
91 unset( $tags['og:image:secure_url'] );
92 $secure_image_num = 0;
93
94 foreach ( (array) $tags as $tag_property => $tag_content ) {
95 // to accomodate multiple images
96 $tag_content = (array) $tag_content;
97 $tag_content = array_unique( $tag_content );
98
99 foreach ( $tag_content as $tag_content_single ) {
100 if ( empty( $tag_content_single ) )
101 continue; // Don't ever output empty tags
102 $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
103 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
104 $og_output .= "\n";
105
106 if ( 'og:image' == $tag_property ) {
107 if ( is_array( $secure ) && !empty( $secure[$secure_image_num] ) ) {
108 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
109 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
110 $og_output .= "\n";
111 } else if ( !is_array( $secure ) && !empty( $secure ) ) {
112 $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
113 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
114 $og_output .= "\n";
115 }
116 $secure_image_num++;
117 }
118 }
119 }
120
121 echo $og_output;
122 }
123
124 function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
125 $image = '';
126
127 if ( is_singular() && !is_home() && !is_front_page() ) {
128 global $post;
129 $image = '';
130
131 // Attempt to find something good for this post using our generalized PostImages code
132 if ( class_exists( 'Jetpack_PostImages' ) ) {
133 $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
134 if ( $post_images && !is_wp_error( $post_images ) ) {
135 $image = array();
136 foreach ( (array) $post_images as $post_image ) {
137 $image[] = $post_image['src'];
138 }
139 }
140 }
141 } else if ( is_author() ) {
142 $author = get_queried_object();
143 if ( function_exists( 'get_avatar_url' ) ) {
144 $avatar = get_avatar_url( $author->user_email, $width );
145
146 if ( ! empty( $avatar ) ) {
147 if ( is_array( $avatar ) )
148 $image = $avatar[0];
149 else
150 $image = $avatar;
151 }
152 }
153 else {
154 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
155 if ( !$has_filter ) {
156 add_filter( 'pre_option_show_avatars', '__return_true' );
157 }
158 $avatar = get_avatar( $author->user_email, $width );
159 if ( !$has_filter ) {
160 remove_filter( 'pre_option_show_avatars', '__return_true' );
161 }
162
163 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
164 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) );
165 $image = wp_specialchars_decode( $matches[1], ENT_QUOTES );
166 }
167 }
168 }
169
170 if ( empty( $image ) )
171 $image = array();
172 else if ( !is_array( $image ) )
173 $image = array( $image );
174
175 // First fall back, blavatar
176 if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
177 $blavatar_domain = blavatar_domain( site_url() );
178 if ( blavatar_exists( $blavatar_domain ) )
179 $image[] = blavatar_url( $blavatar_domain, 'img', $width );
180 }
181
182 // Second fall back, blank image
183 if ( empty( $image ) ) {
184 $image[] = "http://wordpress.com/i/blank.jpg";
185 }
186
187 return $image;
188 }
189
190 /**
191 * @param $email
192 * @param $width
193 * @return array|bool|mixed|string
194 */
195 function jetpack_og_get_image_gravatar( $email, $width ) {
196 $image = '';
197 if ( function_exists( 'get_avatar_url' ) ) {
198 $avatar = get_avatar_url($email, $width);
199 if ( ! empty( $avatar ) ) {
200 if ( is_array( $avatar ) )
201 $image = $avatar[0];
202 else
203 $image = $avatar;
204 }
205 } else {
206 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
207 if ( !$has_filter ) {
208 add_filter( 'pre_option_show_avatars', '__return_true' );
209 }
210 $avatar = get_avatar( $email, $width );
211
212 if ( !$has_filter ) {
213 remove_filter( 'pre_option_show_avatars', '__return_true' );
214 }
215
216 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
217 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) )
218 $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
219 }
220 }
221
222 return $image;
223 }