| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\integration\Amp; |
| 5 |
|
| 6 |
/** |
| 7 |
* Frontend functionality. |
| 8 |
* |
| 9 |
* @author Epiphyt |
| 10 |
* @license GPL2 |
| 11 |
* @package epiphyt\Embed_Privacy |
| 12 |
* @since 1.10.0 |
| 13 |
*/ |
| 14 |
final class Frontend { |
| 15 |
/** |
| 16 |
* @var bool Whether the current request has printed Embed Privacy assets. |
| 17 |
*/ |
| 18 |
private $is_printed = false; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize functionality. |
| 22 |
*/ |
| 23 |
public function init() { |
| 24 |
\add_action( 'init', [ $this, 'register_assets' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle printing assets. |
| 29 |
*/ |
| 30 |
public function print_assets() { |
| 31 |
if ( $this->is_printed ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
\wp_enqueue_script( 'embed-privacy' ); |
| 36 |
\wp_enqueue_style( 'embed-privacy' ); |
| 37 |
\wp_localize_script( 'embed-privacy', 'embedPrivacy', [ |
| 38 |
'alwaysActiveProviders' => \array_keys( (array) Embed_Privacy::get_instance()->get_cookie() ), // deprecated |
| 39 |
] ); |
| 40 |
|
| 41 |
/** |
| 42 |
* Fires after assets are printed. |
| 43 |
* |
| 44 |
* @since 1.10.0 |
| 45 |
*/ |
| 46 |
\do_action( 'embed_privacy_print_assets' ); |
| 47 |
|
| 48 |
$this->is_printed = true; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register our assets for the frontend. |
| 53 |
*/ |
| 54 |
public function register_assets() { |
| 55 |
if ( \is_admin() || \wp_doing_ajax() || \wp_doing_cron() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$is_debug = \defined( 'WP_DEBUG' ) && \WP_DEBUG; |
| 60 |
$suffix = ( $is_debug ? '' : '.min' ); |
| 61 |
$css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/embed-privacy' . $suffix . '.css'; |
| 62 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/style/embed-privacy' . $suffix . '.css' ) : \EMBED_PRIVACY_VERSION; |
| 63 |
|
| 64 |
\wp_register_style( 'embed-privacy', $css_file_url, [], $file_version ); |
| 65 |
|
| 66 |
if ( ! Amp::is_amp() ) { |
| 67 |
$js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/embed-privacy' . $suffix . '.js'; |
| 68 |
$file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/js/embed-privacy' . $suffix . '.js' ) : \EMBED_PRIVACY_VERSION; |
| 69 |
|
| 70 |
\wp_register_script( 'embed-privacy', $js_file_url, [], $file_version, [ 'strategy' => 'defer' ] ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Fires after assets have been registered. |
| 75 |
* |
| 76 |
* @since 1.10.0 |
| 77 |
* |
| 78 |
* @param bool $is_debug Whether debug mode is enabled |
| 79 |
* @param string $suffix A filename suffix |
| 80 |
*/ |
| 81 |
\do_action( 'embed_privacy_register_assets', $is_debug, $suffix ); |
| 82 |
} |
| 83 |
} |
| 84 |
|