PluginProbe
SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking / trunk
SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking vtrunk
1.5.0 1.4.0 1.3.0 1.3.1 trunk 0.0.0-alpha.1 0.0.0-alpha.2 0.0.0-alpha.3 0.0.1-beta.1 0.0.1-beta.2 0.0.1-beta.3 0.0.1-beta.4 1.0.0 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4
surecookie / inc / modules / services / known-scripts.php

known-scripts.php in SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking trunk, at inc/modules/services/known-scripts.php

124 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Known Scripts Database.
4 *
5 * The blocking view over the unified service catalog. Since 1.3.0 the fetch,
6 * caching and bundled floor live in Services_Source (which reads the unified
7 * dataset/services.json); this class is a thin adapter that projects that
8 * source into the category => slug => {label, scripts[], iframes[]} shape the
9 * Blocker and the `surecookie_known_scripts` filter consumers expect. The public
10 * API is unchanged.
11 *
12 * @package SureCookie\Inc\Modules\Services
13 * @since 0.0.1
14 */
15
16 namespace SureCookie\Inc\Modules\Services;
17
18 use SureCookie\Inc\Traits\GetInstance;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit; // Exit if accessed directly.
22 }
23
24 /**
25 * Known_Scripts
26 *
27 * Manages the database of known third-party scripts that should be blocked.
28 *
29 * @since 0.0.1
30 */
31 class Known_Scripts {
32 use GetInstance;
33
34 /**
35 * Cached blocking view (category => slug => definition).
36 *
37 * @var array<string, array<string, mixed>>
38 */
39 private array $scripts = [];
40
41 /**
42 * Constructor.
43 *
44 * @since 0.0.1
45 */
46 private function __construct() {
47 $this->load_data();
48 }
49
50 /**
51 * Load the blocking view from the unified Services_Source (transient -> file
52 * cache -> bundled floor; the remote is warmed off-request by the cron).
53 *
54 * @since 0.0.1
55 * @return void
56 */
57 public function load_data(): void {
58 $this->scripts = Services_Source::get_instance()->get_blocking_view();
59 }
60
61 /**
62 * Refresh the unified catalog from remote (off the request path) and reload
63 * the projected blocking view.
64 *
65 * @since 1.2.5
66 * @return void
67 */
68 public function refresh_from_remote(): void {
69 Services_Source::get_instance()->refresh_from_remote();
70 $this->load_data();
71 }
72
73 /**
74 * Load the plugin-bundled baseline blocking view (offline floor).
75 *
76 * @since 1.2.3
77 * @return array<string, array<string, mixed>>
78 */
79 public function get_bundled_data(): array {
80 return Services_Source::get_instance()->get_bundled_blocking_view();
81 }
82
83 /**
84 * Get all known scripts grouped by category.
85 *
86 * @since 0.0.1
87 * @return array<string, array<string, mixed>>
88 */
89 public function get_all_scripts(): array {
90 /**
91 * Filter the known scripts database.
92 *
93 * @since 0.0.1
94 * @param array<string, array<string, mixed>> $scripts Known scripts grouped by category.
95 */
96 $filtered = apply_filters( 'surecookie_known_scripts', $this->scripts );
97 return is_array( $filtered ) ? $filtered : $this->scripts;
98 }
99
100 /**
101 * Get scripts for a specific category.
102 *
103 * @param string $category Category name (e.g., 'analytics', 'marketing').
104 * @since 0.0.1
105 * @return array<string, mixed>
106 */
107 public function get_scripts_for_category( string $category ): array {
108 $all_scripts = $this->get_all_scripts();
109 return $all_scripts[ $category ] ?? [];
110 }
111
112 /**
113 * Clear the cached catalog, falling back to the bundled floor so blocking
114 * keeps running until the next request repopulates from cache/remote.
115 *
116 * @since 0.0.1
117 * @return void
118 */
119 public function clear_cache(): void {
120 Services_Source::get_instance()->clear_cache();
121 $this->scripts = $this->get_bundled_data();
122 }
123 }
124