PluginProbe
Embed Privacy / 1.10.7
Embed Privacy v1.10.7
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / class-frontend.php

class-frontend.php in Embed Privacy 1.10.7, at inc/class-frontend.php

106 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy;
3
4 use epiphyt\Embed_Privacy\integration\Amp;
5 use WP_Post;
6
7 /**
8 * Frontend functionality.
9 *
10 * @author Epiphyt
11 * @license GPL2
12 * @package epiphyt\Embed_Privacy
13 * @since 1.10.0
14 */
15 final class Frontend {
16 /**
17 * @var bool Whether the current request has printed Embed Privacy assets.
18 */
19 private $is_printed = false;
20
21 /**
22 * Initialize functionality.
23 */
24 public function init() {
25 \add_action( 'init', [ $this, 'register_assets' ] );
26 }
27
28 /**
29 * Handle printing assets.
30 */
31 public function print_assets() {
32 if ( $this->is_printed ) {
33 return;
34 }
35
36 \wp_enqueue_script( 'embed-privacy' );
37 \wp_enqueue_style( 'embed-privacy' );
38 \wp_localize_script( 'embed-privacy', 'embedPrivacy', [
39 'alwaysActiveProviders' => \array_keys( (array) Embed_Privacy::get_instance()->get_cookie() ), // deprecated
40 ] );
41
42 /**
43 * Fires after assets are printed.
44 *
45 * @since 1.10.0
46 */
47 \do_action( 'embed_privacy_print_assets' );
48
49 $this->is_printed = true;
50 }
51
52 /**
53 * Register our assets for the frontend.
54 */
55 public function register_assets() {
56 if ( \is_admin() || \wp_doing_ajax() || \wp_doing_cron() ) {
57 return;
58 }
59
60 $is_debug = \defined( 'WP_DEBUG' ) && \WP_DEBUG;
61 $suffix = ( $is_debug ? '' : '.min' );
62 $css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/embed-privacy' . $suffix . '.css';
63 $file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/style/embed-privacy' . $suffix . '.css' ) : \EMBED_PRIVACY_VERSION;
64
65 \wp_register_style( 'embed-privacy', $css_file_url, [], $file_version );
66
67 if ( ! Amp::is_amp() ) {
68 $js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/embed-privacy' . $suffix . '.js';
69 $file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/js/embed-privacy' . $suffix . '.js' ) : \EMBED_PRIVACY_VERSION;
70
71 \wp_register_script( 'embed-privacy', $js_file_url, [], $file_version, [ 'strategy' => 'defer' ] );
72 }
73
74 /**
75 * Fires after assets have been registered.
76 *
77 * @since 1.10.0
78 *
79 * @param bool $is_debug Whether debug mode is enabled
80 * @param string $suffix A filename suffix
81 */
82 \do_action( 'embed_privacy_register_assets', $is_debug, $suffix );
83
84 $current_url = \sprintf(
85 'http%1$s://%2$s%3$s',
86 \is_ssl() ? 's' : '',
87 ! empty( $_SERVER['HTTP_HOST'] ) ? \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '',
88 ! empty( $_SERVER['REQUEST_URI'] ) ? \sanitize_text_field( \wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''
89 );
90
91 if ( empty( $_SERVER['HTTP_HOST'] ) ) {
92 return;
93 }
94
95 $post_id = \url_to_postid( $current_url );
96
97 if ( $post_id ) {
98 $post = \get_post( $post_id );
99
100 if ( $post instanceof WP_Post && \has_shortcode( $post->post_content, 'embed_privacy_opt_out' ) ) {
101 $this->print_assets();
102 }
103 }
104 }
105 }
106