facebook.php
| 1 | <?php |
| 2 | /** |
| 3 | * Facebook embeds |
| 4 | */ |
| 5 | |
| 6 | define( 'JETPACK_FACEBOOK_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/(posts|photos)/([^/]+)?#' ); |
| 7 | define( 'JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/permalink.php\?([^\s]+)#' ); |
| 8 | define( 'JETPACK_FACEBOOK_PHOTO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/photo.php\?([^\s]+)#' ); |
| 9 | define( 'JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/photos/([^/]+)?#' ); |
| 10 | define( 'JETPACK_FACEBOOK_VIDEO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/video.php\?([^\s]+)#' ); |
| 11 | define( 'JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/videos/([^/]+)?#' ); |
| 12 | |
| 13 | |
| 14 | // Example URL: https://www.facebook.com/VenusWilliams/posts/10151647007373076 |
| 15 | wp_embed_register_handler( 'facebook', JETPACK_FACEBOOK_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 16 | |
| 17 | // Example URL: https://www.facebook.com/permalink.php?id=222622504529111&story_fbid=559431180743788 |
| 18 | wp_embed_register_handler( 'facebook-alternate', JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 19 | |
| 20 | // Photos are handled on a different endpoint; e.g. https://www.facebook.com/photo.php?fbid=10151609960150073&set=a.398410140072.163165.106666030072&type=1 |
| 21 | wp_embed_register_handler( 'facebook-photo', JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 22 | |
| 23 | // Photos (from pages for example) can be at |
| 24 | wp_embed_register_handler( 'facebook-alternate-photo', JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 25 | |
| 26 | // Videos e.g. https://www.facebook.com/video.php?v=772471122790796 |
| 27 | wp_embed_register_handler( 'facebook-video', JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 28 | // Videos https://www.facebook.com/WhiteHouse/videos/10153398464269238/ |
| 29 | wp_embed_register_handler( 'facebook-alternate-video', JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); |
| 30 | |
| 31 | function jetpack_facebook_embed_handler( $matches, $attr, $url ) { |
| 32 | if ( false !== strpos( $url, 'video.php' ) || false !== strpos( $url, '/videos/' ) ) { |
| 33 | $embed = sprintf( '<div class="fb-video" data-allowfullscreen="true" data-href="%s"></div>', esc_url( $url ) ); |
| 34 | } else { |
| 35 | $width = 552; // As of 01/2017, the default width of Facebook embeds when no width attribute provided |
| 36 | |
| 37 | global $content_width; |
| 38 | if ( isset( $content_width ) ) { |
| 39 | $width = min( $width, $content_width ); |
| 40 | } |
| 41 | |
| 42 | $embed = sprintf( '<fb:post href="%s" data-width="%s"></fb:post>', esc_url( $url ), esc_attr( $width ) ); |
| 43 | } |
| 44 | |
| 45 | // since Facebook is a faux embed, we need to load the JS SDK in the wpview embed iframe |
| 46 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST['action'] ) && 'parse-embed' == $_POST['action'] ) { |
| 47 | ob_start(); |
| 48 | wp_scripts()->do_items( array( 'jetpack-facebook-embed' ) ); |
| 49 | $scripts = ob_get_clean(); |
| 50 | return $embed . $scripts; |
| 51 | } else { |
| 52 | wp_enqueue_script( 'jetpack-facebook-embed' ); |
| 53 | return $embed; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | add_shortcode( 'facebook', 'jetpack_facebook_shortcode_handler' ); |
| 58 | |
| 59 | function jetpack_facebook_shortcode_handler( $atts ) { |
| 60 | global $wp_embed; |
| 61 | |
| 62 | if ( empty( $atts['url'] ) ) |
| 63 | return; |
| 64 | |
| 65 | if ( ! preg_match( JETPACK_FACEBOOK_EMBED_REGEX, $atts['url'] ) |
| 66 | && ! preg_match( JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, $atts['url'] ) |
| 67 | && ! preg_match( JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, $atts['url'] ) |
| 68 | && ! preg_match( JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, $atts['url'] ) ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 73 | } |
| 74 |