| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\integration; |
| 3 |
|
| 4 |
use Automattic\Jetpack\Assets; |
| 5 |
use epiphyt\Embed_Privacy\embed\Replacement; |
| 6 |
use epiphyt\Embed_Privacy\Embed_Privacy; |
| 7 |
|
| 8 |
/** |
| 9 |
* Jetpack integration for Embed Privacy. |
| 10 |
* |
| 11 |
* @author Epiphyt |
| 12 |
* @license GPL2 |
| 13 |
* @package epiphyt\Embed_Privacy |
| 14 |
* @since 1.10.0 |
| 15 |
*/ |
| 16 |
final class Jetpack { |
| 17 |
/** |
| 18 |
* Initialize functionality. |
| 19 |
*/ |
| 20 |
public static function init() { |
| 21 |
if ( ! \defined( 'JETPACK__VERSION' ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
\add_action( 'wp_enqueue_scripts', [ self::class, 'deregister_assets' ], 100 ); |
| 26 |
\add_filter( 'embed_privacy_overlay_replaced_content', [ self::class, 'replace_facebook_posts' ] ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Deregister assets. |
| 31 |
*/ |
| 32 |
public static function deregister_assets() { |
| 33 |
\wp_deregister_script( 'jetpack-facebook-embed' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Replace Facebook posts. |
| 38 |
* |
| 39 |
* @param string $content Current replaced content |
| 40 |
* @return string Updated replaced content |
| 41 |
*/ |
| 42 |
public static function replace_facebook_posts( $content ) { |
| 43 |
if ( ! \str_contains( $content, 'class="fb-post"' ) ) { |
| 44 |
return $content; |
| 45 |
} |
| 46 |
|
| 47 |
\remove_filter( 'embed_privacy_overlay_replaced_content', [ self::class, 'replace_facebook_posts' ] ); |
| 48 |
|
| 49 |
$attributes = [ |
| 50 |
'additional_checks' => [ |
| 51 |
[ |
| 52 |
'attribute' => 'class', |
| 53 |
'compare' => '===', |
| 54 |
'type' => 'attribute', |
| 55 |
'value' => 'fb-post', |
| 56 |
], |
| 57 |
], |
| 58 |
'assets' => [], |
| 59 |
'elements' => [ |
| 60 |
'div', |
| 61 |
], |
| 62 |
'element_attribute' => 'data-href', |
| 63 |
]; |
| 64 |
|
| 65 |
// register jetpack script if available |
| 66 |
if ( \class_exists( '\Automattic\Jetpack\Assets' ) ) { |
| 67 |
/** @disregard P1009 */ |
| 68 |
$jetpack = \Jetpack::init(); |
| 69 |
$attributes['assets'][] = [ |
| 70 |
'data' => [ // phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder |
| 71 |
/** |
| 72 |
* Filter the Jetpack sharing Facebook app ID. |
| 73 |
* |
| 74 |
* @since 1.4.5 |
| 75 |
* |
| 76 |
* @param string $app_id The current app ID |
| 77 |
*/ |
| 78 |
'appid' => \apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ), |
| 79 |
'locale' => $jetpack->get_locale(), |
| 80 |
], |
| 81 |
'object_name' => 'jpfbembed', |
| 82 |
'type' => 'inline', |
| 83 |
]; |
| 84 |
$attributes['assets'][] = [ |
| 85 |
'handle' => 'jetpack-facebook-embed', |
| 86 |
'src' => Assets::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
| 87 |
'type' => 'script', |
| 88 |
'version' => \JETPACK__VERSION, |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
$overlay = new Replacement( $content ); |
| 93 |
$new_content = $overlay->get( $attributes ); |
| 94 |
|
| 95 |
if ( $new_content !== $content ) { |
| 96 |
Embed_Privacy::get_instance()->has_embed = true; |
| 97 |
$content = $new_content; |
| 98 |
} |
| 99 |
|
| 100 |
\add_filter( 'embed_privacy_overlay_replaced_content', [ self::class, 'replace_facebook_posts' ] ); |
| 101 |
|
| 102 |
return $content; |
| 103 |
} |
| 104 |
} |
| 105 |
|