', esc_url( $url ), $extra_styles ); } else { $width = 552; // As of 01/2017, the default width of Facebook embeds when no width attribute provided. global $content_width; if ( is_numeric( $content_width ) && $content_width > 0 ) { $width = min( $width, $content_width ); } $embed = sprintf( '
', esc_url( $url ), esc_attr( $width ), $extra_styles ); } // Skip rendering scripts in an AMP context. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { return $embed; } // since Facebook is a faux embed, we need to load the JS SDK in the wpview embed iframe. if ( defined( 'DOING_AJAX' ) && DOING_AJAX // No need to check for a nonce here, that's already handled by Core further up. && ! empty( $_POST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing && 'parse-embed' === $_POST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Missing ) { ob_start(); wp_scripts()->do_items( array( 'jetpack-facebook-embed' ) ); $scripts = ob_get_clean(); return $embed . $scripts; } else { wp_enqueue_script( 'jetpack-facebook-embed' ); return $embed; } } /** * Shortcode handler. * * @param array $atts Shortcode attributes. */ function jetpack_facebook_shortcode_handler( $atts ) { global $wp_embed; if ( empty( $atts['url'] ) ) { return; } if ( ! preg_match( JETPACK_FACEBOOK_EMBED_REGEX, $atts['url'] ) && ! preg_match( JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, $atts['url'] ) && ! preg_match( JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, $atts['url'] ) && ! preg_match( JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, $atts['url'] ) ) { return; } return $wp_embed->shortcode( $atts, $atts['url'] ); } add_shortcode( 'facebook', 'jetpack_facebook_shortcode_handler' ); /** * Embed Reversal for Facebook * * Hooked to pre_kses, converts an embed code from www.facebook.com to an oEmbeddable URL. * * @param string $content Post content. * * @return string The filtered or the original content. **/ function jetpack_facebook_embed_reversal( $content ) { if ( ! is_string( $content ) || false === stripos( $content, 'https://www.facebook.com/plugins/post.php' ) ) { return $content; } /* * Sample embed code: * */ $regexes = array(); $regexes[] = '##i'; $regexes[] = '#<iframe[^&]+?src="((?:https?:)?//(?:www\.)?facebook\.com/plugins/post\.php\?[^"]+)"[^&]*?>\s*?</iframe>#i'; foreach ( $regexes as $regex ) { if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { continue; } foreach ( $matches as $match ) { if ( ! preg_match( '#(https?:)?//(?:www\.)?facebook\.com/plugins/post.php([^/]*)#i', $match[1], $url_matches ) ) { continue; } $src_url = $url_matches[0]; $parsed_url = wp_parse_url( $src_url ); if ( empty( $parsed_url['query'] ) ) { continue; } $query_args = array(); wp_parse_str( $parsed_url['query'], $query_args ); if ( empty( $query_args['href'] ) ) { continue; } // Since we support Facebook via oEmbed, we simply leave a link on a line by itself. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); $url = esc_url( $query_args['href'] ); $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); /** This action is documented in modules/shortcodes/youtube.php */ do_action( 'jetpack_embed_to_shortcode', 'facebook', $url ); } } return $content; } /** * Embed reversal: Convert an embed code from Facebook.com to an oEmbeddable URL. */ if ( jetpack_shortcodes_should_hook_pre_kses() ) { add_filter( 'pre_kses', 'jetpack_facebook_embed_reversal' ); }