| 1 |
<?php |
| 2 |
/** |
| 3 |
* Nudges API class |
| 4 |
* |
| 5 |
* Handles pro nudges related REST API endpoints. |
| 6 |
* |
| 7 |
* @package SureCookie\Inc\Modules\Nudges |
| 8 |
* @since 0.0.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SureCookie\Inc\Modules\Nudges; |
| 12 |
|
| 13 |
use SureCookie\Inc\API\Base; |
| 14 |
use SureCookie\Inc\Functions\SendJson; |
| 15 |
use SureCookie\Inc\Functions\Update; |
| 16 |
use SureCookie\Inc\Traits\GetInstance; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Server; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; // Exit if accessed directly. |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Class Api |
| 26 |
* |
| 27 |
* @package SureCookie\Inc\Modules\Nudges |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
class Api extends Base { |
| 31 |
use GetInstance; |
| 32 |
|
| 33 |
/** |
| 34 |
* Register API routes. |
| 35 |
* |
| 36 |
* @since 0.0.1 |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_routes(): void { |
| 40 |
// Register a single POST endpoint to disable pro nudges. |
| 41 |
register_rest_route( |
| 42 |
$this->get_api_namespace(), |
| 43 |
'/nudges/disable', |
| 44 |
[ |
| 45 |
'methods' => WP_REST_Server::CREATABLE, |
| 46 |
'callback' => [ $this, 'disable_nudge' ], |
| 47 |
'permission_callback' => [ $this, 'validate_permission' ], |
| 48 |
'args' => [ |
| 49 |
'type' => [ |
| 50 |
'required' => true, |
| 51 |
'type' => 'string', |
| 52 |
'sanitize_callback' => 'sanitize_text_field', |
| 53 |
'enum' => [ 'upgrade_banner', 'consent_log_report' ], |
| 54 |
], |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Disable a pro nudge (persist the disabled type with count and next display time). |
| 62 |
* |
| 63 |
* @param WP_REST_Request<array<string, mixed>> $request The request object. |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function disable_nudge( $request ): void { |
| 67 |
$type = $request->get_param( 'type' ); |
| 68 |
|
| 69 |
if ( ! in_array( $type, [ 'upgrade_banner', 'consent_log_report' ], true ) ) { |
| 70 |
SendJson::error( [ 'message' => __( 'Invalid type provided.', 'surecookie' ) ] ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$nudges = (array) get_option( SURECOOKIE_NUDGES, [] ); |
| 75 |
|
| 76 |
// Initialize if not set. |
| 77 |
if ( ! isset( $nudges[ $type ] ) ) { |
| 78 |
$nudges[ $type ] = [ |
| 79 |
'count' => 0, |
| 80 |
'next_time_to_display' => 0, |
| 81 |
'display' => true, |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
// Increment count and set next display time. |
| 86 |
$nudges[ $type ]['count']++; |
| 87 |
$nudges[ $type ]['next_time_to_display'] = time() + ( 7 * DAY_IN_SECONDS ); |
| 88 |
$nudges[ $type ]['display'] = $nudges[ $type ]['count'] < 2; |
| 89 |
|
| 90 |
// Non-autoloaded - dismissal state is only read by the admin app (via |
| 91 |
// LocalizeData::get_admin_data()) and the analytics collector, never by the |
| 92 |
// front-end banner, so it has no business in the alloptions cache. |
| 93 |
if ( ! Update::option( SURECOOKIE_NUDGES, $nudges ) ) { |
| 94 |
SendJson::error( [ 'message' => __( 'Could not disable the nudge. Please try again.', 'surecookie' ) ] ); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
SendJson::success( |
| 99 |
[ |
| 100 |
'message' => sprintf( /* translators: %s: nudge type */__( '"%s" has been disabled.', 'surecookie' ), $type ), |
| 101 |
'count' => $nudges[ $type ]['count'], |
| 102 |
'next_time_to_display' => $nudges[ $type ]['next_time_to_display'], |
| 103 |
] |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|