PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.0
Jetpack – WP Security, Backup, Speed, & Growth v16.0
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 / enhanced-open-graph.php
enhanced-open-graph.php
198 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Enhanced Open Graph for Jetpack.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Post_Media\Images;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit( 0 );
12 }
13
14 if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
15 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media-summary.php';
16 }
17
18 /**
19 * Better OG Image Tags for Image Post Formats
20 *
21 * @param array $tags Array of Open Graph tags.
22 */
23 function enhanced_og_image( $tags ) {
24 if ( ! is_singular() || post_password_required() ) {
25 return $tags;
26 }
27
28 global $post;
29
30 // Bail if we do not have info about the post.
31 if ( ! $post instanceof WP_Post ) {
32 return $tags;
33 }
34
35 // Always favor featured images.
36 if ( enhanced_og_has_featured_image( $post->ID ) ) {
37 return $tags;
38 }
39
40 $summary = Jetpack_Media_Summary::get(
41 $post->ID,
42 0,
43 array(
44 'include_excerpt' => false,
45 'include_count' => false,
46 )
47 );
48
49 if ( 'image' !== $summary['type'] ) {
50 return $tags;
51 }
52
53 $tags['og:image'] = $summary['image'];
54 $tags['og:image:secure_url'] = $summary['secure']['image'];
55
56 return $tags;
57 }
58 add_filter( 'jetpack_open_graph_tags', 'enhanced_og_image' );
59
60 /**
61 * Better OG Image Tags for Gallery Post Formats
62 *
63 * @param array $tags Array of Open Graph tags.
64 */
65 function enhanced_og_gallery( $tags ) {
66 if ( ! is_singular() || post_password_required() ) {
67 return $tags;
68 }
69
70 global $post;
71
72 // Bail if we do not have info about the post.
73 if ( ! $post instanceof WP_Post ) {
74 return $tags;
75 }
76
77 // Always favor featured images.
78 if ( enhanced_og_has_featured_image( $post->ID ) ) {
79 return $tags;
80 }
81
82 $summary = Jetpack_Media_Summary::get(
83 $post->ID,
84 0,
85 array(
86 'include_excerpt' => false,
87 'include_count' => false,
88 )
89 );
90
91 if ( 'gallery' !== $summary['type'] ) {
92 return $tags;
93 }
94
95 if ( ! isset( $summary['images'] ) || ! is_array( $summary['images'] ) || empty( $summary['images'] ) ) {
96 return $tags;
97 }
98
99 $images = array();
100 $secures = array();
101
102 foreach ( $summary['images'] as $i => $image ) {
103 $images[] = $image['url'];
104 $secures[] = $summary['secure']['images'][ $i ]['url'];
105 }
106
107 $tags['og:image'] = $images;
108 $tags['og:image:secure_url'] = $secures;
109
110 return $tags;
111 }
112 add_filter( 'jetpack_open_graph_tags', 'enhanced_og_gallery' );
113
114 /**
115 * Allows VideoPress, YouTube, and Vimeo videos to play inline on Facebook
116 *
117 * @param array $tags Array of Open Graph tags.
118 */
119 function enhanced_og_video( $tags ) {
120 if ( ! is_singular() || post_password_required() ) {
121 return $tags;
122 }
123
124 global $post;
125
126 // Bail if we do not have info about the post.
127 if ( ! $post instanceof WP_Post ) {
128 return $tags;
129 }
130
131 // Always favor featured images.
132 if ( enhanced_og_has_featured_image( $post->ID ) ) {
133 return $tags;
134 }
135
136 $summary = Jetpack_Media_Summary::get(
137 $post->ID,
138 0,
139 array(
140 'include_excerpt' => false,
141 'include_count' => false,
142 )
143 );
144
145 if ( 'video' !== $summary['type'] ) {
146 if ( $summary['count']['video'] > 0 && $summary['count']['image'] < 1 ) {
147 $tags['og:image'] = $summary['image'];
148 $tags['og:image:secure_url'] = $summary['secure']['image'];
149 }
150 return $tags;
151 }
152
153 $tags['og:image'] = $summary['image'];
154 $tags['og:image:secure_url'] = $summary['secure']['image'];
155
156 // This should be html by default for youtube/vimeo, since we're linking to HTML pages.
157 $tags['og:video:type'] = $summary['video_type'] ?? 'text/html';
158
159 $video_url = $summary['video'];
160 $secure_video_url = $summary['secure']['video'];
161
162 if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) {
163 if ( strstr( $video_url, 'youtube' ) ) {
164 $id = jetpack_get_youtube_id( $video_url );
165 $video_url = 'http://www.youtube.com/embed/' . $id;
166 $secure_video_url = 'https://www.youtube.com/embed/' . $id;
167 } elseif ( strstr( $video_url, 'vimeo' ) ) {
168 preg_match( '|vimeo\.com/(\d+)/?$|i', $video_url, $match );
169 if ( isset( $match[1] ) ) {
170 $id = (int) $match[1];
171 $video_url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $id;
172 $secure_video_url = 'https://vimeo.com/moogaloop.swf?clip_id=' . $id;
173 }
174 }
175 }
176
177 $tags['og:video'] = $video_url;
178 $tags['og:video:secure_url'] = $secure_video_url;
179
180 if ( empty( $post->post_title ) ) {
181 /* translators: %s is the name of the site */
182 $tags['og:title'] = sprintf( __( 'Video on %s', 'jetpack' ), get_option( 'blogname' ) );
183 }
184
185 return $tags;
186 }
187 add_filter( 'jetpack_open_graph_tags', 'enhanced_og_video' );
188
189 /**
190 * Check if a post has a suitable featured image.
191 *
192 * @param int $post_id The post ID to check.
193 * @return bool True if the post has a suitable featured image, false otherwise.
194 */
195 function enhanced_og_has_featured_image( $post_id ) {
196 return ! empty( Images::from_thumbnail( $post_id ) );
197 }
198