Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
39 / 78
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
jetpack_facebook_embed_handler
51.72% covered (warning)
51.72%
15 / 29
0.00% covered (danger)
0.00%
0 / 1
28.20
jetpack_facebook_shortcode_handler
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 1
21.19
jetpack_facebook_embed_reversal
91.67% covered (success)
91.67%
22 / 24
0.00% covered (danger)
0.00%
0 / 1
9.05
1<?php
2/**
3 * Facebook embeds
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12define( 'JETPACK_FACEBOOK_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/(posts|photos)/([^/]+)?#' );
13define( 'JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/permalink.php\?([^\s]+)#' );
14define( 'JETPACK_FACEBOOK_PHOTO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/photo.php\?([^\s]+)#' );
15define( 'JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/photos/([^/]+)?#' );
16define( 'JETPACK_FACEBOOK_VIDEO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/(?:video.php|watch\/?)\?([^\s]+)#' );
17define( 'JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/videos/([^/]+)?#' );
18
19/*
20 * Example URL: https://www.facebook.com/VenusWilliams/posts/10151647007373076
21 */
22wp_embed_register_handler( 'facebook', JETPACK_FACEBOOK_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
23
24/*
25 * Example URL: https://www.facebook.com/permalink.php?id=222622504529111&story_fbid=559431180743788
26 */
27wp_embed_register_handler( 'facebook-alternate', JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
28
29/*
30 * Photos are handled on a different endpoint; e.g. https://www.facebook.com/photo.php?fbid=10151609960150073&set=a.398410140072.163165.106666030072&type=1
31 */
32wp_embed_register_handler( 'facebook-photo', JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
33
34/*
35 * Photos (from pages for example) can be at
36 */
37wp_embed_register_handler( 'facebook-alternate-photo', JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
38
39/*
40 * Videos
41 *
42 * Formats:
43 * https://www.facebook.com/video.php?v=2836814009877992
44 * https://www.facebook.com/watch/?v=2836814009877992
45 */
46wp_embed_register_handler( 'facebook-video', JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
47
48/*
49 * Videos  https://www.facebook.com/WhiteHouse/videos/10153398464269238/
50 */
51wp_embed_register_handler( 'facebook-alternate-video', JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
52
53/**
54 * Callback to modify output of embedded Facebook posts.
55 *
56 * @param array  $matches Regex partial matches against the URL passed.
57 * @param array  $attr    Attributes received in embed response.
58 * @param string $url     Requested URL to be embedded.
59 * @return string Facebook embed markup.
60 */
61function jetpack_facebook_embed_handler( $matches, $attr, $url ) {
62    // This is a stop-gap solution until Facebook hopefully resolves this ticket
63    // https://developers.facebook.com/community/threads/1675075423353415/?post_id=1675075426686748
64    $extra_styles = 'style="background-color: #fff; display: inline-block;"';
65
66    if (
67        str_contains( $url, 'video.php' )
68        || str_contains( $url, '/videos/' )
69        || str_contains( $url, '/watch' )
70    ) {
71        $embed = sprintf(
72            '<div class="fb-video" data-allowfullscreen="true" data-href="%1$s" %2$s></div>',
73            esc_url( $url ),
74            $extra_styles
75        );
76    } else {
77        $width = 552; // As of 01/2017, the default width of Facebook embeds when no width attribute provided.
78
79        global $content_width;
80        if ( is_numeric( $content_width ) && $content_width > 0 ) {
81            $width = min( $width, $content_width );
82        }
83
84        $embed = sprintf(
85            '<div class="fb-post" data-href="%1$s" data-width="%2$s" %3$s></div>',
86            esc_url( $url ),
87            esc_attr( $width ),
88            $extra_styles
89        );
90    }
91
92    // Skip rendering scripts in an AMP context.
93    if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
94        return $embed;
95    }
96
97    // since Facebook is a faux embed, we need to load the JS SDK in the wpview embed iframe.
98    if (
99        defined( 'DOING_AJAX' )
100        && DOING_AJAX
101        // No need to check for a nonce here, that's already handled by Core further up.
102        && ! empty( $_POST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
103        && 'parse-embed' === $_POST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Missing
104    ) {
105        ob_start();
106        wp_scripts()->do_items( array( 'jetpack-facebook-embed' ) );
107        $scripts = ob_get_clean();
108        return $embed . $scripts;
109    } else {
110        wp_enqueue_script( 'jetpack-facebook-embed' );
111        return $embed;
112    }
113}
114
115/**
116 * Shortcode handler.
117 *
118 * @param array $atts Shortcode attributes.
119 */
120function jetpack_facebook_shortcode_handler( $atts ) {
121    global $wp_embed;
122
123    if ( empty( $atts['url'] ) ) {
124        return;
125    }
126
127    if ( ! preg_match( JETPACK_FACEBOOK_EMBED_REGEX, $atts['url'] )
128    && ! preg_match( JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, $atts['url'] )
129    && ! preg_match( JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, $atts['url'] )
130    && ! preg_match( JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, $atts['url'] ) ) {
131        return;
132    }
133
134    return $wp_embed->shortcode( $atts, $atts['url'] );
135}
136add_shortcode( 'facebook', 'jetpack_facebook_shortcode_handler' );
137
138/**
139 * Embed Reversal for Facebook
140 *
141 * Hooked to pre_kses, converts an embed code from www.facebook.com to an oEmbeddable URL.
142 *
143 * @param string $content Post content.
144 *
145 * @return string The filtered or the original content.
146 **/
147function jetpack_facebook_embed_reversal( $content ) {
148    if ( ! is_string( $content ) || false === stripos( $content, 'https://www.facebook.com/plugins/post.php' ) ) {
149        return $content;
150    }
151
152    /*
153     * Sample embed code:
154     * <iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Ftechcrunch%2Fposts%2Fpfbid0997g1PXQKfyFNHNTiCgaCFevt3PRFMaUBBB9eEFPR5NsXCv8EXxBw3p9bBYezWkHl&show_text=true&width=500" width="500" height="504" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"></iframe>
155     */
156
157    $regexes   = array();
158    $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?facebook\.com/plugins/post\.php\?[^"]+)"[^>]*?>\s*?</iframe>#i';
159    $regexes[] = '#&lt;iframe[^&]+?src="((?:https?:)?//(?:www\.)?facebook\.com/plugins/post\.php\?[^"]+)"[^&]*?&gt;\s*?&lt;/iframe&gt;#i';
160
161    foreach ( $regexes as $regex ) {
162        if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
163            continue;
164        }
165
166        foreach ( $matches as $match ) {
167            if ( ! preg_match( '#(https?:)?//(?:www\.)?facebook\.com/plugins/post.php([^/]*)#i', $match[1], $url_matches ) ) {
168                continue;
169            }
170
171            $src_url    = $url_matches[0];
172            $parsed_url = wp_parse_url( $src_url );
173            if ( empty( $parsed_url['query'] ) ) {
174                continue;
175            }
176
177            $query_args = array();
178            wp_parse_str( $parsed_url['query'], $query_args );
179            if ( empty( $query_args['href'] ) ) {
180                continue;
181            }
182
183            // Since we support Facebook via oEmbed, we simply leave a link on a line by itself.
184            $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
185            $url           = esc_url( $query_args['href'] );
186
187            $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
188            /** This action is documented in modules/shortcodes/youtube.php */
189            do_action( 'jetpack_embed_to_shortcode', 'facebook', $url );
190        }
191    }
192
193    return $content;
194}
195
196/**
197 * Embed reversal: Convert an embed code from Facebook.com to an oEmbeddable URL.
198 */
199if ( jetpack_shortcodes_should_hook_pre_kses() ) {
200    add_filter( 'pre_kses', 'jetpack_facebook_embed_reversal' );
201}