| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 8 | 12 | define( 'JETPACK_FACEBOOK_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/(posts|photos)/([^/]+)?#' ); |
| 9 | 13 | define( 'JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/permalink.php\?([^\s]+)#' ); |
| 10 | 14 | define( 'JETPACK_FACEBOOK_PHOTO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/photo.php\?([^\s]+)#' ); |
| 11 | 15 | define( 'JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/photos/([^/]+)?#' ); |
| @@ -54,14 +58,22 @@ | ||
| 54 | 58 | * @param string $url Requested URL to be embedded. |
| 55 | 59 | * @return string Facebook embed markup. |
| 56 | 60 | */ |
| 57 | 61 | function 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 | + | |
| 58 | 66 | if ( |
| 59 | 67 | str_contains( $url, 'video.php' ) |
| 60 | 68 | || str_contains( $url, '/videos/' ) |
| 61 | 69 | || str_contains( $url, '/watch' ) |
| 62 | 70 | ) { |
| 63 | - $embed = sprintf( '<div class="fb-video" data-allowfullscreen="true" data-href="%s"></div>', esc_url( $url ) ); | |
| 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 | + ); | |
| 64 | 76 | } else { |
| 65 | 77 | $width = 552; // As of 01/2017, the default width of Facebook embeds when no width attribute provided. |
| 66 | 78 | |
| 67 | 79 | global $content_width; |
| @@ -68,9 +80,14 @@ | ||
| 68 | 80 | if ( is_numeric( $content_width ) && $content_width > 0 ) { |
| 69 | 81 | $width = min( $width, $content_width ); |
| 70 | 82 | } |
| 71 | 83 | |
| 72 | - $embed = sprintf( '<div class="fb-post" data-href="%s" data-width="%s"></div>', esc_url( $url ), esc_attr( $width ) ); | |
| 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 | + ); | |
| 73 | 90 | } |
| 74 | 91 | |
| 75 | 92 | // Skip rendering scripts in an AMP context. |
| 76 | 93 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| @@ -116,4 +133,69 @@ | ||
| 116 | 133 | |
| 117 | 134 | return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 118 | 135 | } |
| 119 | 136 | add_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 | + **/ | |
| 147 | +function 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[] = '#<iframe[^&]+?src="((?:https?:)?//(?:www\.)?facebook\.com/plugins/post\.php\?[^"]+)"[^&]*?>\s*?</iframe>#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 | + */ | |
| 199 | +if ( jetpack_shortcodes_should_hook_pre_kses() ) { | |
| 200 | + add_filter( 'pre_kses', 'jetpack_facebook_embed_reversal' ); | |
| 201 | +} | |