Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
enhanced_og_image
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
enhanced_og_gallery
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
110
enhanced_og_video
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
182
enhanced_og_has_featured_image
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Enhanced Open Graph for Jetpack.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Post_Media\Images;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14if ( ! 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 */
23function 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}
58add_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 */
65function 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}
112add_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 */
119function 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}
187add_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 */
195function enhanced_og_has_featured_image( $post_id ) {
196    return ! empty( Images::from_thumbnail( $post_id ) );
197}