PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / 0.0.3
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager v0.0.3
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / modules / settings / module.php

module.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager 0.0.3, at modules/settings/module.php

420 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Modules\Settings;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 use Cookiez\Classes\Logger;
10 use Cookiez\Classes\Module_Base;
11 use Cookiez\Classes\Utils;
12 use Cookiez\Modules\Connect\Classes\Config;
13 use Cookiez\Modules\Connect\Module as Connect;
14 use Cookiez\Modules\Core\Components\Notices;
15 use Cookiez\Modules\Settings\Classes\Settings;
16 use Throwable;
17 use Exception;
18 use WP_Error;
19
20 /**
21 * Module `Settings`
22 */
23 class Module extends Module_Base {
24 const SETTING_BASE_SLUG = 'cookiez-settings';
25 const SETTING_CAPABILITY = 'manage_options';
26
27 public function get_name(): string {
28 return 'Settings';
29 }
30
31 /**
32 * Declare components list
33 *
34 * @return array
35 */
36 public static function component_list(): array {
37 return [
38 'Settings_Pointer',
39 ];
40 }
41
42 /**
43 * Get Plugin ENV
44 * @return string
45 */
46 private static function get_plugin_env(): string {
47 return apply_filters( 'cookiez_settings_plugin_env', 'production' );
48 }
49
50 public function render_app() {
51 ?>
52 <!-- The hack required to wrap WP notifications -->
53 <div class="wrap">
54 <h1 style="display: none;" role="presentation"></h1>
55 </div>
56
57 <div id="cookiez-app"></div>
58 <?php
59 }
60
61 public function register_page() {
62 add_submenu_page(
63 'elementor-home',
64 __( 'Cookie Consent', 'cookiez' ),
65 __( 'Cookie Consent', 'cookiez' ),
66 self::SETTING_CAPABILITY,
67 self::SETTING_BASE_SLUG,
68 [ $this, 'render_app' ],
69 30
70 );
71 }
72
73 /**
74 * Enqueue Scripts and Styles
75 */
76 public function enqueue_scripts(): void {
77 if ( ! Utils::is_plugin_page() ) {
78 return;
79 }
80
81 /**
82 * These styles are braking MUI component styling.
83 * Re-registering as empty so wp-admin's dependency doesn't break
84 */
85 wp_deregister_style( 'forms' );
86 wp_register_style( 'forms', false );
87
88 self::refresh_plan_data();
89
90 wp_enqueue_style(
91 'cookiez-admin-fonts',
92 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
93 [],
94 COOKIEZ_VERSION
95 );
96
97 Utils\Assets::enqueue_app_assets( 'settings' );
98
99 $settings_data = [
100 'isConnected' => Connect::is_connected(),
101 'isUrlMismatch' => ! Connect::get_connect()->utils()->is_valid_home_url(),
102 'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
103 'restRoot' => rest_url(),
104 'pluginEnv' => self::get_plugin_env(),
105 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
106 'appSlug' => Config::PLUGIN_SLUG,
107 'appVersion' => COOKIEZ_VERSION,
108 'isRTL' => is_rtl(),
109 'dateFormat' => get_option( 'date_format', 'F j, Y' ),
110 'timeFormat' => get_option( 'time_format', 'g:i a' ),
111 'translations' => Utils::get_translations(),
112 'isElementorOne' => self::is_elementor_one(),
113 'settings' => Settings::get( Settings::COOKIEZ_SETTINGS ),
114 'content' => Settings::get( Settings::COOKIEZ_CONTENT ),
115 'planData' => get_option( Settings::PLAN_DATA ),
116 'planScope' => get_option( Settings::PLAN_SCOPE ),
117 'isElementorActive' => defined( 'ELEMENTOR_VERSION' ),
118 'isElementorProActive' => defined( 'ELEMENTOR_PRO_VERSION' ),
119 ];
120
121 $settings_data = apply_filters( 'cookiez/settings/data', $settings_data );
122
123 wp_localize_script(
124 'settings',
125 'cookiezSettingsData',
126 $settings_data
127 );
128 }
129
130 /**
131 * Check if elementor one
132 * @return bool
133 */
134 public static function is_elementor_one(): bool {
135 return Connect::get_connect()->get_config( 'app_type' ) !== Config::APP_TYPE;
136 }
137
138 /**
139 * Get all plugin settings data
140 * @return array
141 * @throws Throwable
142 */
143 public static function get_plugin_settings(): array {
144 $content = Settings::get( Settings::COOKIEZ_CONTENT );
145
146 return array_merge(
147 [
148 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
149 'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
150 'settings' => Settings::get( Settings::COOKIEZ_SETTINGS ),
151 ],
152 is_array( $content ) ? $content : []
153 );
154 }
155
156 public static function routes_list(): array {
157 return [
158 'Get_Settings',
159 'Update_Settings',
160 'Get_Consent_Logs',
161 'Export_Consent_Logs',
162 ];
163 }
164
165 /**
166 * @throws Exception
167 */
168 public function on_connect(): void {
169 if ( ! Connect::is_connected() ) {
170 return;
171 }
172
173 self::register_site_with_data();
174 }
175
176 /**
177 * Fetch and save plan data on Elementor One app_cookie connection.
178 * @return void
179 */
180 public function on_app_cookie_connected(): void {
181 if ( ! Connect::is_connected() ) {
182 return;
183 }
184
185 $plan_data = get_option( Settings::PLAN_DATA );
186
187 $response = Utils::get_api_client()->make_request(
188 'POST',
189 'sites/info'
190 );
191
192 self::save_plan_data( $response );
193 }
194
195 /**
196 * On disconnect
197 * @return void
198 */
199 public function on_disconnect() {
200 delete_option( Settings::SUBSCRIPTION_ID );
201 }
202
203 /**
204 * Register or update site data for One connect
205 * @throws Exception
206 */
207 public function on_migration_run() {
208 if ( ! Connect::is_connected() ) {
209 return;
210 }
211
212 $client_id = Settings::get( Settings::CLIENT_ID );
213
214 if ( $client_id ) {
215 try {
216 $migration_response = Utils::get_api_client()->make_request(
217 'POST',
218 'site/migration',
219 [ 'old_client_id' => $client_id ],
220 );
221
222 self::save_plan_data( $migration_response );
223
224 $old_options = [
225 'cookiez_client_secret',
226 'cookiez_home_url',
227 'cookiez_access_token',
228 'cookiez_token_id',
229 'cookiez_refresh_token',
230 'cookiez_user_access_token',
231 'cookiez_owner_user_id',
232 Settings::SUBSCRIPTION_ID,
233 Settings::CLIENT_ID,
234 ];
235
236 foreach ( $old_options as $option ) {
237 delete_option( $option );
238 }
239 } catch ( Throwable $t ) {
240 Logger::error( esc_html( $t->getMessage() ) );
241 }
242 } else {
243 $this->on_connect();
244 }
245 }
246
247 /**
248 * Register the website and save the plan data.
249 * @return void
250 */
251 public static function register_site_with_data(): void {
252 $register_response = Utils::get_api_client()->make_request(
253 'POST',
254 'site/register'
255 );
256
257 if ( is_wp_error( $register_response ) ) {
258 Logger::error( esc_html( $register_response->get_error_message() ) );
259 } else {
260 self::save_plan_data( $register_response );
261 if ( isset( $register_response->scopes ) ) {
262 Settings::set( Settings::PLAN_SCOPE, $register_response->scopes );
263 }
264 }
265 }
266
267 /**
268 * Refresh the plan data if the refresh transient has expired.
269 * @return void
270 */
271 public static function refresh_plan_data(): void {
272 if ( ! Connect::is_connected() ) {
273 return;
274 }
275
276 if ( self::get_plan_data_refresh_transient() ) {
277 return;
278 }
279
280 $plan_data = get_option( Settings::PLAN_DATA );
281
282 if ( empty( $plan_data->public_api_key ) ) {
283 Logger::error( 'Cannot refresh the plan data. No public API key found.' );
284 self::register_site_with_data();
285 return;
286 }
287
288 $response = Utils::get_api_client()->make_request(
289 'POST',
290 'site/info'
291 );
292
293 self::save_plan_data( $response );
294 }
295
296 public static function set_plan_data_refresh_transient(): void {
297 set_transient( Settings::PLAN_DATA_REFRESH_TRANSIENT, true, MINUTE_IN_SECONDS * 15 );
298 }
299
300 public static function get_plan_data_refresh_transient(): bool {
301 return get_transient( Settings::PLAN_DATA_REFRESH_TRANSIENT );
302 }
303
304 public static function delete_plan_data_refresh_transient(): bool {
305 return delete_transient( Settings::PLAN_DATA_REFRESH_TRANSIENT );
306 }
307
308 /**
309 * Save plan data to plan_data option
310 * @param $register_response
311 *
312 * @return void
313 */
314 public static function save_plan_data( $register_response ): void {
315 if ( $register_response && ! is_wp_error( $register_response ) ) {
316 $decoded_response = $register_response;
317
318 update_option( Settings::SUBSCRIPTION_ID, $decoded_response->plan->subscription_id );
319 update_option( Settings::PLAN_DATA, $decoded_response );
320 update_option( Settings::IS_VALID_PLAN_DATA, true );
321
322 self::set_plan_data_refresh_transient();
323 } else {
324 Logger::error( $register_response instanceof WP_Error ? esc_html( $register_response->get_error_message() ) : $register_response );
325 update_option( Settings::IS_VALID_PLAN_DATA, false );
326 }
327 }
328
329 /**
330 * Get upgrade link with UTM parameters
331 *
332 * @param string $campaign Campaign identifier for tracking.
333 * @return string
334 */
335 public static function get_upgrade_link( string $campaign ): string {
336 $subscription_id = get_option( Settings::SUBSCRIPTION_ID );
337
338 if ( $subscription_id ) {
339 return add_query_arg(
340 [
341 'utm_source' => $campaign . '-upgrade',
342 'utm_medium' => 'wp-dash',
343 'subscription_id' => $subscription_id,
344 ],
345 'https://go.elementor.com/' . $campaign
346 );
347 }
348
349 return add_query_arg(
350 [
351 'utm_source' => $campaign . '-upgrade',
352 'utm_medium' => 'wp-dash',
353 ],
354 'https://go.elementor.com/' . $campaign
355 );
356 }
357
358 /**
359 * Cookie consent quota usage percentage (cookie_consents only).
360 *
361 * @return float Percent used (0–100+), or 0 if unavailable.
362 */
363 public static function get_consent_quota_usage_percent(): float {
364 $plan_data = get_option( Settings::PLAN_DATA );
365
366 if ( ! $plan_data || ! isset( $plan_data->quota->cookie_consents ) ) {
367 return 0;
368 }
369
370 $consents = $plan_data->quota->cookie_consents;
371
372 if ( ! isset( $consents->allowed, $consents->used ) || $consents->allowed <= 0 ) {
373 return 0;
374 }
375
376 return round( $consents->used / $consents->allowed * 100, 2 );
377 }
378
379 /**
380 * Register quota notices with the notice manager.
381 *
382 * @param Notices $notice_manager The notice manager instance.
383 * @return void
384 */
385 public function register_notices( Notices $notice_manager ): void {
386 if ( self::is_elementor_one() ) {
387 return;
388 }
389
390 if ( ! Connect::is_connected() && ! get_option( Settings::PLAN_DATA ) ) {
391 return;
392 }
393
394 $notices = [
395 'Quota_80',
396 'Quota_100',
397 ];
398
399 foreach ( $notices as $notice ) {
400 $class_name = 'Cookiez\Modules\Settings\Notices\\' . $notice;
401 $notice_manager->register_notice( new $class_name() );
402 }
403 }
404
405 public function __construct() {
406 $this->register_components();
407 $this->register_routes();
408
409 add_action( 'admin_menu', [ $this, 'register_page' ] );
410 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
411
412 add_action( 'elementor_one/' . Config::APP_PREFIX . '_connected', [ $this, 'on_connect' ] );
413 add_action( 'elementor_one/' . Config::APP_PREFIX . '_disconnected', [ $this, 'on_disconnect' ] );
414 add_action( 'elementor_one/' . Config::APP_PREFIX . '_migration_run', [ $this, 'on_migration_run' ] );
415 add_action( 'elementor_one/' . Config::APP_TYPE . '_connected', [ $this, 'on_app_cookie_connected' ] );
416
417 add_action( 'cookiez_register_notices', [ $this, 'register_notices' ] );
418 }
419 }
420