| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\integration; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\data\Providers; |
| 5 |
use epiphyt\Embed_Privacy\embed\Template; |
| 6 |
use epiphyt\Embed_Privacy\Embed_Privacy; |
| 7 |
use epiphyt\Embed_Privacy\handler\Theme; |
| 8 |
|
| 9 |
/** |
| 10 |
* Divi integration for Embed Privacy. |
| 11 |
* |
| 12 |
* @author Epiphyt |
| 13 |
* @license GPL2 |
| 14 |
* @package epiphyt\Embed_Privacy |
| 15 |
* @since 1.10.0 |
| 16 |
*/ |
| 17 |
final class Divi { |
| 18 |
/** |
| 19 |
* Initialize functionality. |
| 20 |
*/ |
| 21 |
public function init() { |
| 22 |
\add_action( 'embed_privacy_register_assets', [ self::class, 'register_assets' ], 10, 2 ); |
| 23 |
\add_filter( 'embed_privacy_print_assets', [ self::class, 'enqueue_assets' ] ); |
| 24 |
\add_filter( 'et_builder_resolve_dynamic_content', [ self::class, 'add_dynamic_content_filter' ], 5 ); |
| 25 |
\add_filter( 'et_builder_resolve_dynamic_content', [ self::class, 'remove_dynamic_content_filter' ], \PHP_INT_MAX ); |
| 26 |
\add_filter( 'et_module_process_display_conditions', [ self::class, 'replace_google_maps' ], 10, 3 ); |
| 27 |
\add_filter( 'et_pb_enqueue_google_maps_script', '__return_false' ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add filter for dynamic content. |
| 32 |
* |
| 33 |
* @since 1.10.9 |
| 34 |
* |
| 35 |
* @param string $content Current dynamic content |
| 36 |
* @return string Current dynamic content |
| 37 |
*/ |
| 38 |
public static function add_dynamic_content_filter( $content ) { |
| 39 |
\add_filter( 'wp_kses_allowed_html', [ self::class, 'allow_script_in_post' ], 10, 2 ); |
| 40 |
|
| 41 |
return $content; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Allow script tags in post, since Divi runs a wp_kses_post over the embed. |
| 46 |
* |
| 47 |
* @since 1.10.9 |
| 48 |
* |
| 49 |
* @param array $html List of allowed HTML tags and attributes |
| 50 |
* @param string $context Current context |
| 51 |
* @return array Updated list of allowed HTML |
| 52 |
*/ |
| 53 |
public static function allow_script_in_post( array $html, $context ) { |
| 54 |
if ( $context === 'post' ) { |
| 55 |
if ( ! isset( $html['input'] ) ) { |
| 56 |
$html['input'] = [ |
| 57 |
'class' => true, |
| 58 |
'date-*' => true, |
| 59 |
'id' => true, |
| 60 |
'type' => true, |
| 61 |
'value' => true, |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
$html['script'] = [ |
| 66 |
'type' => true, |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
return $html; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Enqueue assets. |
| 75 |
*/ |
| 76 |
public static function enqueue_assets() { |
| 77 |
if ( Theme::is( 'Divi' ) ) { |
| 78 |
\wp_enqueue_script( 'embed-privacy-divi' ); |
| 79 |
\wp_enqueue_style( 'embed-privacy-divi' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Replace Google Maps Markup in Divi. |
| 85 |
* |
| 86 |
* @param string $output Current output |
| 87 |
* @param string $render_method Divi render method |
| 88 |
* @param \ET_Builder_Module $module Module instance |
| 89 |
* @return string Updated output |
| 90 |
*/ |
| 91 |
public static function replace_google_maps( $output, $render_method, $module ) { |
| 92 |
if ( $render_method === 'render_as_builder_data' ) { |
| 93 |
return $output; |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! $module instanceof \ET_Builder_Module_Map ) { |
| 97 |
return $output; |
| 98 |
} |
| 99 |
|
| 100 |
global $wp_scripts; |
| 101 |
Embed_Privacy::get_instance()->has_embed = true; |
| 102 |
Embed_Privacy::get_instance()->frontend->print_assets(); |
| 103 |
|
| 104 |
$output .= \sprintf( |
| 105 |
'<script id="%1$s"> |
| 106 |
$.ajax( { |
| 107 |
url: "%2$s", |
| 108 |
dataType: "script", |
| 109 |
success: function() { |
| 110 |
embed_privacy_et_pb_init_maps(); |
| 111 |
}, |
| 112 |
} ); |
| 113 |
</script>', |
| 114 |
\esc_attr( $wp_scripts->registered['google-maps-api']->handle . '-embed-privacy' ), |
| 115 |
\esc_url( |
| 116 |
\add_query_arg( |
| 117 |
[ |
| 118 |
'key' => \et_pb_get_google_api_key(), |
| 119 |
'v' => 3, |
| 120 |
'ver' => $wp_scripts->registered['google-maps-api']->ver, |
| 121 |
], |
| 122 |
$wp_scripts->registered['google-maps-api']->src |
| 123 |
) |
| 124 |
) |
| 125 |
); |
| 126 |
$output .= "<script> |
| 127 |
var embed_privacy_et_pb_map=$('.et_pb_map_container'); |
| 128 |
var embed_privacy_et_pb_init_maps = function embed_privacy_et_pb_init_maps() { |
| 129 |
embed_privacy_et_pb_map.each(function() { |
| 130 |
embed_privacy_et_pb_map_init($(this)); |
| 131 |
}); |
| 132 |
}; |
| 133 |
</script>"; |
| 134 |
|
| 135 |
return Template::get( Providers::get_instance()->get_by_name( 'google-maps' ), $output ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Register assets. |
| 140 |
* |
| 141 |
* @param bool $is_debug Whether debug mode is enabled |
| 142 |
* @param string $suffix A filename suffix |
| 143 |
*/ |
| 144 |
public static function register_assets( $is_debug, $suffix ) { |
| 145 |
$js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/divi' . $suffix . '.js'; |
| 146 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/js/divi' . $suffix . '.js' ) : \EMBED_PRIVACY_VERSION; |
| 147 |
|
| 148 |
\wp_register_script( 'embed-privacy-divi', $js_file_url, [], $file_version, [ 'strategy' => 'defer' ] ); |
| 149 |
|
| 150 |
$css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/divi' . $suffix . '.css'; |
| 151 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/style/divi' . $suffix . '.css' ) : \EMBED_PRIVACY_VERSION; |
| 152 |
|
| 153 |
\wp_register_style( 'embed-privacy-divi', $css_file_url, [], $file_version ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Remove filter for dynamic content. |
| 158 |
* |
| 159 |
* @since 1.10.9 |
| 160 |
* |
| 161 |
* @param string $content Current dynamic content |
| 162 |
* @return string Current dynamic content |
| 163 |
*/ |
| 164 |
public static function remove_dynamic_content_filter( $content ) { |
| 165 |
\remove_filter( 'wp_kses_allowed_html', [ self::class, 'allow_script_in_post' ], 10 ); |
| 166 |
|
| 167 |
return $content; |
| 168 |
} |
| 169 |
} |
| 170 |
|