jetpack
Last commit date
3rd-party
1 week ago
_inc
2 days ago
css
2 weeks ago
extensions
2 days ago
images
1 month ago
jetpack_vendor
2 days ago
json-endpoints
1 week ago
modules
2 days ago
sal
1 week ago
src
2 days ago
vendor
2 days ago
views
1 month ago
CHANGELOG.md
2 days ago
LICENSE.txt
5 months ago
SECURITY.md
2 days ago
class-jetpack-connection-status.php
2 years ago
class-jetpack-gallery-settings.php
6 months ago
class-jetpack-newsletter-dashboard-widget.php
6 months ago
class-jetpack-pre-connection-jitms.php
2 years ago
class-jetpack-stats-dashboard-widget.php
3 months ago
class-jetpack-xmlrpc-methods.php
1 week ago
class.frame-nonce-preview.php
6 months ago
class.jetpack-admin.php
2 days ago
class.jetpack-autoupdate.php
6 months ago
class.jetpack-cli.php
2 days ago
class.jetpack-client-server.php
2 years ago
class.jetpack-gutenberg.php
1 week ago
class.jetpack-heartbeat.php
3 months ago
class.jetpack-modules-list-table.php
6 months ago
class.jetpack-network-sites-list-table.php
6 months ago
class.jetpack-network.php
1 month ago
class.jetpack-plan.php
2 years ago
class.jetpack-post-images.php
2 months ago
class.jetpack-twitter-cards.php
3 months ago
class.jetpack-user-agent.php
2 years ago
class.jetpack.php
2 days ago
class.json-api-endpoints.php
1 week ago
class.json-api.php
2 weeks ago
class.photon.php
3 years ago
composer.json
2 days ago
enhanced-open-graph.php
1 week ago
functions.compat.php
3 months ago
functions.cookies.php
2 years ago
functions.global.php
2 days ago
functions.is-mobile.php
2 years ago
functions.opengraph.php
2 months ago
functions.photon.php
2 years ago
jetpack.php
2 days ago
json-api-config.php
3 years ago
json-endpoints.php
2 years ago
load-jetpack.php
1 week ago
locales.php
6 months ago
readme.txt
2 days ago
unauth-file-upload.php
6 months ago
uninstall.php
6 months ago
wpml-config.xml
3 years ago
enhanced-open-graph.php
198 lines
| 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 | // 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 | } |
| 58 | add_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 | */ |
| 65 | function 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 | } |
| 112 | add_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 | */ |
| 119 | function 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 | } |
| 187 | add_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 | */ |
| 195 | function enhanced_og_has_featured_image( $post_id ) { |
| 196 | return ! empty( Images::from_thumbnail( $post_id ) ); |
| 197 | } |
| 198 |