PluginProbe
Embed Privacy / 1.10.3
Embed Privacy v1.10.3
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.3, at inc/class-frontend.php

107 lines 2.9 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 'javascriptDetection' => \get_option( 'embed_privacy_javascript_detection' ),
41 ] );
42
43 /**
44 * Fires after assets are printed.
45 *
46 * @since 1.10.0
47 */
48 \do_action( 'embed_privacy_print_assets' );
49
50 $this->is_printed = true;
51 }
52
53 /**
54 * Register our assets for the frontend.
55 */
56 public function register_assets() {
57 if ( \is_admin() || \wp_doing_ajax() || \wp_doing_cron() ) {
58 return;
59 }
60
61 $is_debug = \defined( 'WP_DEBUG' ) && \WP_DEBUG;
62 $suffix = ( $is_debug ? '' : '.min' );
63 $css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/embed-privacy' . $suffix . '.css';
64 $file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/style/embed-privacy' . $suffix . '.css' ) : \EMBED_PRIVACY_VERSION;
65
66 \wp_register_style( 'embed-privacy', $css_file_url, [], $file_version );
67
68 if ( ! Amp::is_amp() ) {
69 $js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/embed-privacy' . $suffix . '.js';
70 $file_version = $is_debug ? \filemtime( \EPI_EMBED_PRIVACY_BASE . 'assets/js/embed-privacy' . $suffix . '.js' ) : \EMBED_PRIVACY_VERSION;
71
72 \wp_register_script( 'embed-privacy', $js_file_url, [], $file_version, [ 'strategy' => 'defer' ] );
73 }
74
75 /**
76 * Fires after assets have been registered.
77 *
78 * @since 1.10.0
79 *
80 * @param bool $is_debug Whether debug mode is enabled
81 * @param string $suffix A filename suffix
82 */
83 \do_action( 'embed_privacy_register_assets', $is_debug, $suffix );
84
85 $current_url = \sprintf(
86 'http%1$s://%2$s%3$s',
87 \is_ssl() ? 's' : '',
88 ! empty( $_SERVER['HTTP_HOST'] ) ? \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '',
89 ! empty( $_SERVER['REQUEST_URI'] ) ? \sanitize_text_field( \wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''
90 );
91
92 if ( empty( $_SERVER['HTTP_HOST'] ) ) {
93 return;
94 }
95
96 $post_id = \url_to_postid( $current_url );
97
98 if ( $post_id ) {
99 $post = \get_post( $post_id );
100
101 if ( $post instanceof WP_Post && \has_shortcode( $post->post_content, 'embed_privacy_opt_out' ) ) {
102 $this->print_assets();
103 }
104 }
105 }
106 }
107