get_active_container_id(); if ( ! $container_id || $this->should_exclude_current_user() ) { return; } // Built from the same source the admin UI displays, so what we show the // user is byte-for-byte what is injected. The dynamic id is escaped inside // head_snippet(); the rest is a static, trusted template. echo self::head_snippet( $container_id ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function inject_body_snippet(): void { $container_id = $this->get_active_container_id(); if ( ! $container_id || $this->should_exclude_current_user() ) { return; } echo self::body_snippet( $container_id ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $this->body_snippet_injected = true; } /** * The exact `
` GTM loader snippet Adbot injects for a container. * Single source of truth shared by the injector and the REST/admin UI so the * code shown to the user always matches what is on the page. */ public static function head_snippet( string $container_id ): string { return "\n" . '\n" . ''; } /** * The exact `` (noscript) GTM snippet Adbot injects for a container. */ public static function body_snippet( string $container_id ): string { return "\n" . '' . "\n" . ''; } public function inject_body_snippet_fallback(): void { if ( $this->body_snippet_injected ) { return; } $this->inject_body_snippet(); } private function get_active_container_id(): ?string { if ( ! get_option( 'adbot_snippet_active' ) ) { return null; } // A matching snippet from the theme / another plugin is already on the // site — never add a second copy (it would double-fire every tag). if ( get_option( 'adbot_snippet_external_detected' ) ) { return null; } $container_id = get_option( 'adbot_snippet_container_id' ); if ( ! $container_id || ! preg_match( '/^GTM-[A-Z0-9]+$/', $container_id ) ) { return null; } return $container_id; } private function should_exclude_current_user(): bool { $settings = get_option( 'adbot_settings', [] ); if ( ! empty( $settings['exclude_admins'] ) && current_user_can( 'manage_options' ) ) { return true; } return false; } }