carries no data-* attribute. */ class Adpriva_Tag { const HANDLE = 'adpriva-tag'; /** * Register hooks. * * @return void */ public function hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); } /** * Enqueue the CDN tag when the site is connected. * * @return void */ public function enqueue() { if ( ! $this->should_load() ) { return; } wp_enqueue_script( self::HANDLE, Adpriva_Config::tag_url(), array(), ADPRIVA_VERSION, true ); } /** * Whether the tag should load. Connected + provisioned, and not vetoed by a * consent/privacy plugin via the adpriva_should_load_tag filter. * * @return bool */ private function should_load() { $load = Adpriva_Tokens::is_connected() && '' !== Adpriva_Tokens::placement_key(); if ( $load ) { $load = $this->passes_targeting(); } /** * Filter whether the AdPriva tag should load on this request. Consent or * privacy plugins can return false to suppress it until consent is granted. * This filter is the final override and runs after the Settings targeting * rules, so a consent plugin can always veto a tag the rules would allow. * * @param bool $load Whether to load the tag. */ return (bool) apply_filters( 'adpriva_should_load_tag', $load ); } /** * Whether the Settings → Tag targeting rules allow the tag on this request. * Each rule is only evaluated when it is enabled, so the default configuration * touches no conditional-tag functions. * * @return bool */ private function passes_targeting() { if ( Adpriva_Config::exclude_logged_in() && is_user_logged_in() ) { return false; } if ( Adpriva_Config::exclude_admins() && current_user_can( 'manage_options' ) ) { return false; } if ( ! $this->post_type_allowed() ) { return false; } if ( $this->url_excluded() ) { return false; } return true; } /** * Whether the configured post-type targeting allows this request. 'all' allows * every request; a specific list allows only singular views of a listed type. * * @return bool */ private function post_type_allowed() { $types = Adpriva_Config::tag_post_types(); if ( 'all' === $types || ! is_array( $types ) ) { return true; } if ( ! is_singular() ) { return false; } return in_array( (string) get_post_type(), $types, true ); } /** * Whether the current request URI contains any configured excluded fragment. * * @return bool */ private function url_excluded() { $excludes = Adpriva_Config::exclude_urls(); if ( empty( $excludes ) ) { return false; } $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( '' === $uri ) { return false; } foreach ( $excludes as $fragment ) { if ( '' !== $fragment && false !== strpos( $uri, $fragment ) ) { return true; } } return false; } }