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

161 lines 5.7 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 ) ? strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) : wp_trim_words( 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 foreach ( (array) $tags as $tag_property => $tag_content ) {
90 // to accomodate multiple images
91 $tag_content = (array) $tag_content;
92 $tag_content = array_unique( $tag_content );
93
94 foreach ( $tag_content as $tag_content_single ) {
95 if ( empty( $tag_content_single ) )
96 continue; // Don't ever output empty tags
97 $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
98 $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
99 $og_output .= "\n";
100 }
101 }
102
103 echo $og_output;
104 }
105
106 function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
107 $image = '';
108
109 if ( is_singular() && !is_home() && !is_front_page() ) {
110 global $post;
111 $image = '';
112
113 // Attempt to find something good for this post using our generalized PostImages code
114 if ( class_exists( 'Jetpack_PostImages' ) ) {
115 $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
116 if ( $post_images && !is_wp_error( $post_images ) ) {
117 $image = array();
118 foreach ( (array) $post_images as $post_image ) {
119 $image[] = $post_image['src'];
120 }
121 }
122 }
123 } else if ( is_author() ) {
124 $author = get_queried_object();
125 if ( function_exists( 'get_avatar_url' ) ) {
126 $avatar = get_avatar_url( $author->user_email, $width );
127
128 if ( ! empty( $avatar ) ) {
129 if ( is_array( $avatar ) )
130 $image = $avatar[0];
131 else
132 $image = $avatar;
133 }
134 }
135 else {
136 $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
137 if ( !$has_filter ) {
138 add_filter( 'pre_option_show_avatars', '__return_true' );
139 }
140 $avatar = get_avatar( $author->user_email, $width );
141 if ( !$has_filter ) {
142 remove_filter( 'pre_option_show_avatars', '__return_true' );
143 }
144
145 if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) {
146 if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) );
147 $image = wp_specialchars_decode( $matches[1], ENT_QUOTES );
148 }
149 }
150 }
151
152 // Fallback to Blavatar if available
153 if ( function_exists( 'blavatar_domain' ) ) {
154 $blavatar_domain = blavatar_domain( site_url() );
155 if ( empty( $image ) && blavatar_exists( $blavatar_domain ) )
156 $image = blavatar_url( $blavatar_domain, 'img', $width );
157 }
158
159 return $image;
160 }
161