PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.1
Jetpack – WP Security, Backup, Speed, & Growth v12.1
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 in Jetpack – WP Security, Backup, Speed, & Growth 12.1, at enhanced-open-graph.php

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