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