enhanced-open-graph.php
| 1 | <?php |
| 2 | if ( ! class_exists( 'Jetpack_Media_Summary' ) ) { |
| 3 | if ( defined('IS_WPCOM') && IS_WPCOM ) { |
| 4 | include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php'; |
| 5 | } else { |
| 6 | jetpack_require_lib( 'class.media-summary' ); |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Better OG Image Tags for Image Post Formats |
| 12 | */ |
| 13 | function enhanced_og_image( $tags ) { |
| 14 | if ( !is_singular() || post_password_required() ) |
| 15 | return $tags; |
| 16 | |
| 17 | global $post; |
| 18 | |
| 19 | // Always favor featured images. |
| 20 | if ( enhanced_og_has_featured_image( $post->ID ) ) |
| 21 | return $tags; |
| 22 | |
| 23 | $summary = Jetpack_Media_Summary::get( $post->ID ); |
| 24 | |
| 25 | if ( 'image' != $summary['type'] ) |
| 26 | return $tags; |
| 27 | |
| 28 | $tags['og:image'] = $summary['image']; |
| 29 | $tags['og:image:secure_url'] = $summary['secure']['image']; |
| 30 | |
| 31 | return $tags; |
| 32 | } |
| 33 | add_filter( 'jetpack_open_graph_tags', 'enhanced_og_image' ); |
| 34 | |
| 35 | /** |
| 36 | * Better OG Image Tags for Gallery Post Formats |
| 37 | */ |
| 38 | function enhanced_og_gallery( $tags ) { |
| 39 | if ( !is_singular() || post_password_required() ) |
| 40 | return $tags; |
| 41 | |
| 42 | global $post; |
| 43 | |
| 44 | // Always favor featured images. |
| 45 | if ( enhanced_og_has_featured_image( $post->ID ) ) |
| 46 | return $tags; |
| 47 | |
| 48 | $summary = Jetpack_Media_Summary::get( $post->ID ); |
| 49 | |
| 50 | if ( 'gallery' != $summary['type'] ) |
| 51 | return $tags; |
| 52 | |
| 53 | if( !isset( $summary['images'] ) || !is_array( $summary['images'] ) || empty( $summary['images'] ) ) |
| 54 | return $tags; |
| 55 | |
| 56 | $images = $secures = array(); |
| 57 | foreach ( $summary['images'] as $i => $image ) { |
| 58 | $images[] = $image['url']; |
| 59 | $secures[] = $summary['secure']['images'][$i]['url']; |
| 60 | } |
| 61 | |
| 62 | $tags['og:image'] = $images; |
| 63 | $tags['og:image:secure_url'] = $secures; |
| 64 | |
| 65 | return $tags; |
| 66 | } |
| 67 | add_filter( 'jetpack_open_graph_tags', 'enhanced_og_gallery' ); |
| 68 | |
| 69 | /** |
| 70 | * Allows VideoPress, YouTube, and Vimeo videos to play inline on Facebook |
| 71 | */ |
| 72 | function enhanced_og_video( $tags ) { |
| 73 | if ( !is_singular() || post_password_required() ) |
| 74 | return $tags; |
| 75 | |
| 76 | global $post; |
| 77 | |
| 78 | // Always favor featured images. |
| 79 | if ( enhanced_og_has_featured_image( $post->ID ) ) |
| 80 | return $tags; |
| 81 | |
| 82 | $summary = Jetpack_Media_Summary::get( $post->ID ); |
| 83 | |
| 84 | if ( 'video' != $summary['type'] ) { |
| 85 | if ( $summary['count']['video'] > 0 && $summary['count']['image'] < 1 ) { |
| 86 | $tags['og:image'] = $summary['image']; |
| 87 | $tags['og:image:secure_url'] = $summary['secure']['image']; |
| 88 | } |
| 89 | return $tags; |
| 90 | } |
| 91 | |
| 92 | $tags['og:image'] = $summary['image']; |
| 93 | $tags['og:image:secure_url'] = $summary['secure']['image']; |
| 94 | |
| 95 | // This should be html by default for youtube/vimeo, since we're linking to HTML pages. |
| 96 | $tags['og:video:type'] = isset( $summary['video_type'] ) ? $summary['video_type'] : 'text/html'; |
| 97 | |
| 98 | $video_url = $summary['video']; |
| 99 | $secure_video_url = $summary['secure']['video']; |
| 100 | |
| 101 | if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) { |
| 102 | if ( strstr( $video_url, 'youtube' ) ) { |
| 103 | $id = jetpack_get_youtube_id( $video_url ); |
| 104 | $video_url = 'http://www.youtube.com/embed/' . $id; |
| 105 | $secure_video_url = 'https://www.youtube.com/embed/' . $id; |
| 106 | } else if ( strstr( $video_url, 'vimeo' ) ) { |
| 107 | preg_match( '|vimeo\.com/(\d+)/?$|i', $video_url, $match ); |
| 108 | $id = (int) $match[1]; |
| 109 | $video_url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $id; |
| 110 | $secure_video_url = 'https://vimeo.com/moogaloop.swf?clip_id=' . $id; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $tags['og:video'] = $video_url; |
| 115 | $tags['og:video:secure_url'] = $secure_video_url; |
| 116 | |
| 117 | if ( empty( $post->post_title ) ) |
| 118 | $tags['og:title'] = sprintf( __( 'Video on %s', 'jetpack' ), get_option( 'blogname' ) ); |
| 119 | |
| 120 | return $tags; |
| 121 | } |
| 122 | add_filter( 'jetpack_open_graph_tags', 'enhanced_og_video' ); |
| 123 | |
| 124 | function enhanced_og_has_featured_image( $post_id ) { |
| 125 | $featured = Jetpack_PostImages::from_thumbnail( $post_id, 200, 200 ); |
| 126 | if ( !empty( $featured ) && count( $featured ) > 0 ) |
| 127 | return true; |
| 128 | return false; |
| 129 | } |
| 130 |