| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\integration; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\handler\Theme; |
| 5 |
|
| 6 |
/** |
| 7 |
* Divi integration for Embed Privacy. |
| 8 |
* |
| 9 |
* @author Epiphyt |
| 10 |
* @license GPL2 |
| 11 |
* @package epiphyt\Embed_Privacy |
| 12 |
* @since 1.10.0 |
| 13 |
*/ |
| 14 |
final class Divi { |
| 15 |
/** |
| 16 |
* Initialize functionality. |
| 17 |
*/ |
| 18 |
public function init() { |
| 19 |
\add_action( 'embed_privacy_register_assets', [ self::class, 'register_assets' ], 10, 2 ); |
| 20 |
\add_filter( 'embed_privacy_print_assets', [ self::class, 'enqueue_assets' ] ); |
| 21 |
\add_filter( 'et_builder_resolve_dynamic_content', [ self::class, 'add_dynamic_content_filter' ], 5 ); |
| 22 |
\add_filter( 'et_builder_resolve_dynamic_content', [ self::class, 'remove_dynamic_content_filter' ], \PHP_INT_MAX ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add filter for dynamic content. |
| 27 |
* |
| 28 |
* @since 1.10.9 |
| 29 |
* |
| 30 |
* @param string $content Current dynamic content |
| 31 |
* @return string Current dynamic content |
| 32 |
*/ |
| 33 |
public static function add_dynamic_content_filter( $content ) { |
| 34 |
\add_filter( 'wp_kses_allowed_html', [ self::class, 'allow_script_in_post' ], 10, 2 ); |
| 35 |
|
| 36 |
return $content; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Allow script tags in post, since Divi runs a wp_kses_post over the embed. |
| 41 |
* |
| 42 |
* @since 1.10.9 |
| 43 |
* |
| 44 |
* @param array $html List of allowed HTML tags and attributes |
| 45 |
* @param string $context Current context |
| 46 |
* @return array Updated list of allowed HTML |
| 47 |
*/ |
| 48 |
public static function allow_script_in_post( array $html, $context ) { |
| 49 |
if ( $context === 'post' ) { |
| 50 |
if ( ! isset( $html['input'] ) ) { |
| 51 |
$html['input'] = [ |
| 52 |
'class' => true, |
| 53 |
'date-*' => true, |
| 54 |
'id' => true, |
| 55 |
'type' => true, |
| 56 |
'value' => true, |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
$html['script'] = [ |
| 61 |
'type' => true, |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
return $html; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue assets. |
| 70 |
*/ |
| 71 |
public static function enqueue_assets() { |
| 72 |
if ( Theme::is( 'Divi' ) ) { |
| 73 |
\wp_enqueue_script( 'embed-privacy-divi' ); |
| 74 |
\wp_enqueue_style( 'embed-privacy-divi' ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register assets. |
| 80 |
* |
| 81 |
* @param bool $is_debug Whether debug mode is enabled |
| 82 |
* @param string $suffix A filename suffix |
| 83 |
*/ |
| 84 |
public static function register_assets( $is_debug, $suffix ) { |
| 85 |
$js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/divi' . $suffix . '.js'; |
| 86 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/js/divi' . $suffix . '.js' ) : \EMBED_PRIVACY_VERSION; |
| 87 |
|
| 88 |
\wp_register_script( 'embed-privacy-divi', $js_file_url, [], $file_version, [ 'strategy' => 'defer' ] ); |
| 89 |
|
| 90 |
$css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/divi' . $suffix . '.css'; |
| 91 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/style/divi' . $suffix . '.css' ) : \EMBED_PRIVACY_VERSION; |
| 92 |
|
| 93 |
\wp_register_style( 'embed-privacy-divi', $css_file_url, [], $file_version ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Remove filter for dynamic content. |
| 98 |
* |
| 99 |
* @since 1.10.9 |
| 100 |
* |
| 101 |
* @param string $content Current dynamic content |
| 102 |
* @return string Current dynamic content |
| 103 |
*/ |
| 104 |
public static function remove_dynamic_content_filter( $content ) { |
| 105 |
\remove_filter( 'wp_kses_allowed_html', [ self::class, 'allow_script_in_post' ], 10 ); |
| 106 |
|
| 107 |
return $content; |
| 108 |
} |
| 109 |
} |
| 110 |
|