| 1 |
<?php |
| 2 |
/** |
| 3 |
* Set_Cbid_Ability class. |
| 4 |
* |
| 5 |
* @package cookiebot |
| 6 |
* @subpackage abilities |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace cybot\cookiebot\abilities; |
| 10 |
|
| 11 |
use cybot\cookiebot\lib\Cookiebot_WP; |
| 12 |
|
| 13 |
/** |
| 14 |
* Ability to set the Cookiebot Domain Group ID. |
| 15 |
* |
| 16 |
* @since 4.8.0 |
| 17 |
*/ |
| 18 |
class Set_Cbid_Ability implements Cookiebot_Ability_Interface { |
| 19 |
|
| 20 |
const CBID_PATTERN = '^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}|[a-zA-Z0-9]{9}|[a-zA-Z0-9]{14})$'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Audit logger instance. |
| 24 |
* |
| 25 |
* @var Ability_Audit_Logger |
| 26 |
*/ |
| 27 |
private $logger; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param Ability_Audit_Logger $logger Audit logger instance. |
| 33 |
* |
| 34 |
* @since 4.8.0 |
| 35 |
*/ |
| 36 |
public function __construct( Ability_Audit_Logger $logger ) { |
| 37 |
$this->logger = $logger; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the ability name. |
| 42 |
* |
| 43 |
* @return string |
| 44 |
* |
| 45 |
* @since 4.8.0 |
| 46 |
*/ |
| 47 |
public function get_name() { |
| 48 |
return 'cookiebot/set-cbid'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the ability arguments. |
| 53 |
* |
| 54 |
* @return array |
| 55 |
* |
| 56 |
* @since 4.8.0 |
| 57 |
*/ |
| 58 |
public function get_args() { |
| 59 |
$logger = $this->logger; |
| 60 |
|
| 61 |
return array( |
| 62 |
'label' => __( 'Set Domain Group ID', 'cookiebot' ), |
| 63 |
'description' => __( 'Sets the Domain Group ID (CBID). For Cookiebot accounts this is a UUID (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) found under Implementation in the Cookiebot Admin. For Usercentrics accounts this is a Settings ID (9 or 14 characters) found in the Usercentrics Admin. Changes take effect immediately.', 'cookiebot' ), |
| 64 |
'category' => 'cookiebot', |
| 65 |
'input_schema' => array( |
| 66 |
'type' => 'object', |
| 67 |
'properties' => array( |
| 68 |
'cbid' => array( |
| 69 |
'type' => 'string', |
| 70 |
'pattern' => self::CBID_PATTERN, |
| 71 |
'description' => __( 'The Domain Group ID: a UUID for Cookiebot accounts (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) or a Settings ID for Usercentrics accounts (9 or 14 alphanumeric characters).', 'cookiebot' ), |
| 72 |
), |
| 73 |
), |
| 74 |
'required' => array( 'cbid' ), |
| 75 |
'additionalProperties' => false, |
| 76 |
), |
| 77 |
'output_schema' => array( |
| 78 |
'type' => 'object', |
| 79 |
'properties' => array( |
| 80 |
'old_cbid_set' => array( |
| 81 |
'type' => 'boolean', |
| 82 |
'description' => 'Whether a Domain Group ID was configured before this call.', |
| 83 |
), |
| 84 |
'new_cbid_set' => array( |
| 85 |
'type' => 'boolean', |
| 86 |
'description' => 'Whether a Domain Group ID is now configured.', |
| 87 |
), |
| 88 |
'success' => array( |
| 89 |
'type' => 'boolean', |
| 90 |
'description' => 'Whether the update succeeded.', |
| 91 |
), |
| 92 |
), |
| 93 |
'additionalProperties' => false, |
| 94 |
), |
| 95 |
'execute_callback' => function( $input ) use ( $logger ) { |
| 96 |
$cbid = isset( $input['cbid'] ) ? $input['cbid'] : ''; |
| 97 |
|
| 98 |
if ( empty( $cbid ) || ! preg_match( '/' . self::CBID_PATTERN . '/', $cbid ) ) { |
| 99 |
return new \WP_Error( |
| 100 |
'cookiebot_invalid_cbid', |
| 101 |
__( 'Invalid ID. Provide a Cookiebot Domain Group ID (UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) or a Usercentrics Settings ID (9 or 14 alphanumeric characters).', 'cookiebot' ) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
$old_cbid = (string) Cookiebot_WP::get_cbid(); |
| 106 |
update_option( 'cookiebot-cbid', $cbid ); |
| 107 |
// Log only whether a value existed before/after — never log the CBID value itself. |
| 108 |
$logger->log( 'cookiebot/set-cbid', $old_cbid !== '' ? 'was_set' : 'was_unset', 'set' ); |
| 109 |
|
| 110 |
return array( |
| 111 |
'old_cbid_set' => $old_cbid !== '', |
| 112 |
'new_cbid_set' => true, |
| 113 |
'success' => true, |
| 114 |
); |
| 115 |
}, |
| 116 |
'permission_callback' => function() { |
| 117 |
return current_user_can( 'manage_options' ); |
| 118 |
}, |
| 119 |
'meta' => array( |
| 120 |
'annotations' => array( |
| 121 |
'readonly' => false, |
| 122 |
'destructive' => false, |
| 123 |
'idempotent' => true, |
| 124 |
), |
| 125 |
'show_in_rest' => true, |
| 126 |
), |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|