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