PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 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 All 508 releases
← All changes | enhanced-open-graph.php +212 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,212 @@
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 + // A gated body's embedded media is withheld along with the body.
36 + if ( \Automattic\Jetpack\SEO\Content_Gate::is_gated( $post ) ) {
37 + return $tags;
38 + }
39 +
40 + // Always favor featured images.
41 + if ( enhanced_og_has_featured_image( $post->ID ) ) {
42 + return $tags;
43 + }
44 +
45 + $summary = Jetpack_Media_Summary::get(
46 + $post->ID,
47 + 0,
48 + array(
49 + 'include_excerpt' => false,
50 + 'include_count' => false,
51 + )
52 + );
53 +
54 + if ( 'image' !== $summary['type'] ) {
55 + return $tags;
56 + }
57 +
58 + $tags['og:image'] = $summary['image'];
59 + $tags['og:image:secure_url'] = $summary['secure']['image'];
60 +
61 + return $tags;
62 +}
63 +add_filter( 'jetpack_open_graph_tags', 'enhanced_og_image' );
64 +
65 +/**
66 + * Better OG Image Tags for Gallery Post Formats
67 + *
68 + * @param array $tags Array of Open Graph tags.
69 + */
70 +function enhanced_og_gallery( $tags ) {
71 + if ( ! is_singular() || post_password_required() ) {
72 + return $tags;
73 + }
74 +
75 + global $post;
76 +
77 + // Bail if we do not have info about the post.
78 + if ( ! $post instanceof WP_Post ) {
79 + return $tags;
80 + }
81 +
82 + // A gated body's embedded media is withheld along with the body.
83 + if ( \Automattic\Jetpack\SEO\Content_Gate::is_gated( $post ) ) {
84 + return $tags;
85 + }
86 +
87 + // Always favor featured images.
88 + if ( enhanced_og_has_featured_image( $post->ID ) ) {
89 + return $tags;
90 + }
91 +
92 + $summary = Jetpack_Media_Summary::get(
93 + $post->ID,
94 + 0,
95 + array(
96 + 'include_excerpt' => false,
97 + 'include_count' => false,
98 + )
99 + );
100 +
101 + if ( 'gallery' !== $summary['type'] ) {
102 + return $tags;
103 + }
104 +
105 + if ( ! isset( $summary['images'] ) || ! is_array( $summary['images'] ) || empty( $summary['images'] ) ) {
106 + return $tags;
107 + }
108 +
109 + $images = array();
110 + $secures = array();
111 +
112 + foreach ( $summary['images'] as $i => $image ) {
113 + $images[] = $image['url'];
114 + $secures[] = $summary['secure']['images'][ $i ]['url'];
115 + }
116 +
117 + $tags['og:image'] = $images;
118 + $tags['og:image:secure_url'] = $secures;
119 +
120 + return $tags;
121 +}
122 +add_filter( 'jetpack_open_graph_tags', 'enhanced_og_gallery' );
123 +
124 +/**
125 + * Allows VideoPress, YouTube, and Vimeo videos to play inline on Facebook
126 + *
127 + * @param array $tags Array of Open Graph tags.
128 + */
129 +function enhanced_og_video( $tags ) {
130 + if ( ! is_singular() || post_password_required() ) {
131 + return $tags;
132 + }
133 +
134 + global $post;
135 +
136 + // Bail if we do not have info about the post.
137 + if ( ! $post instanceof WP_Post ) {
138 + return $tags;
139 + }
140 +
141 + // A gated body's embedded media is withheld along with the body.
142 + if ( \Automattic\Jetpack\SEO\Content_Gate::is_gated( $post ) ) {
143 + return $tags;
144 + }
145 +
146 + // Always favor featured images.
147 + if ( enhanced_og_has_featured_image( $post->ID ) ) {
148 + return $tags;
149 + }
150 +
151 + $summary = Jetpack_Media_Summary::get(
152 + $post->ID,
153 + 0,
154 + array(
155 + 'include_excerpt' => false,
156 + 'include_count' => false,
157 + )
158 + );
159 +
160 + if ( 'video' !== $summary['type'] ) {
161 + if ( $summary['count']['video'] > 0 && $summary['count']['image'] < 1 ) {
162 + $tags['og:image'] = $summary['image'];
163 + $tags['og:image:secure_url'] = $summary['secure']['image'];
164 + }
165 + return $tags;
166 + }
167 +
168 + $tags['og:image'] = $summary['image'];
169 + $tags['og:image:secure_url'] = $summary['secure']['image'];
170 +
171 + // This should be html by default for youtube/vimeo, since we're linking to HTML pages.
172 + $tags['og:video:type'] = $summary['video_type'] ?? 'text/html';
173 +
174 + $video_url = $summary['video'];
175 + $secure_video_url = $summary['secure']['video'];
176 +
177 + if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) {
178 + if ( strstr( $video_url, 'youtube' ) ) {
179 + $id = jetpack_get_youtube_id( $video_url );
180 + $video_url = 'http://www.youtube.com/embed/' . $id;
181 + $secure_video_url = 'https://www.youtube.com/embed/' . $id;
182 + } elseif ( strstr( $video_url, 'vimeo' ) ) {
183 + preg_match( '|vimeo\.com/(\d+)/?$|i', $video_url, $match );
184 + if ( isset( $match[1] ) ) {
185 + $id = (int) $match[1];
186 + $video_url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $id;
187 + $secure_video_url = 'https://vimeo.com/moogaloop.swf?clip_id=' . $id;
188 + }
189 + }
190 + }
191 +
192 + $tags['og:video'] = $video_url;
193 + $tags['og:video:secure_url'] = $secure_video_url;
194 +
195 + if ( empty( $post->post_title ) ) {
196 + /* translators: %s is the name of the site */
197 + $tags['og:title'] = sprintf( __( 'Video on %s', 'jetpack' ), get_option( 'blogname' ) );
198 + }
199 +
200 + return $tags;
201 +}
202 +add_filter( 'jetpack_open_graph_tags', 'enhanced_og_video' );
203 +
204 +/**
205 + * Check if a post has a suitable featured image.
206 + *
207 + * @param int $post_id The post ID to check.
208 + * @return bool True if the post has a suitable featured image, false otherwise.
209 + */
210 +function enhanced_og_has_featured_image( $post_id ) {
211 + return ! empty( Images::from_thumbnail( $post_id ) );
212 +}