| 1 |
<?php |
| 2 |
|
| 3 |
namespace cybot\cookiebot\cli; |
| 4 |
|
| 5 |
/** |
| 6 |
* Manage Cookiebot CMP from the command line. |
| 7 |
* |
| 8 |
* Wraps the WordPress Abilities API surface (Phase 2) so that all configuration |
| 9 |
* available to AI agents over MCP / REST is also available from the shell. |
| 10 |
* |
| 11 |
* ## EXAMPLES |
| 12 |
* |
| 13 |
* # Read current status |
| 14 |
* wp cookiebot status |
| 15 |
* |
| 16 |
* # Set the Domain Group ID |
| 17 |
* wp cookiebot set-cbid 12345678-1234-1234-1234-123456789abc |
| 18 |
* |
| 19 |
* # Verify setup is complete |
| 20 |
* wp cookiebot verify --format=json |
| 21 |
* |
| 22 |
* @since 4.8.0 |
| 23 |
*/ |
| 24 |
class Cookiebot_CLI_Command { |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Output_Adapter |
| 28 |
*/ |
| 29 |
private $output; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param Output_Adapter|null $output Adapter for output. Defaults to WP_CLI. |
| 33 |
*/ |
| 34 |
public function __construct( $output = null ) { |
| 35 |
$this->output = $output ? $output : new WP_CLI_Output_Adapter(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Validate and return the requested output format, or emit an error and return null. |
| 40 |
* |
| 41 |
* @param array $assoc_args WP-CLI named arguments. |
| 42 |
* @param string $default Default format when not specified. |
| 43 |
* @return string|null One of table|json|yaml|csv, or null on invalid input. |
| 44 |
*/ |
| 45 |
private function get_format( $assoc_args, $default = 'table' ) { |
| 46 |
$allowed = array( 'table', 'json', 'yaml', 'csv' ); |
| 47 |
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : $default; |
| 48 |
if ( ! in_array( $format, $allowed, true ) ) { |
| 49 |
$this->output->error( sprintf( 'Invalid --format "%s". Allowed: table, json, yaml, csv.', $format ) ); |
| 50 |
return null; |
| 51 |
} |
| 52 |
return $format; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Resolve a registered ability or fail fast. |
| 57 |
* |
| 58 |
* @param string $name e.g. 'cookiebot/get-status' |
| 59 |
* @return object|null WP_Ability instance, or null on failure. |
| 60 |
*/ |
| 61 |
private function get_ability( $name ) { |
| 62 |
if ( ! function_exists( 'wp_get_ability' ) ) { |
| 63 |
$this->output->error( 'Cookiebot abilities are not registered. Is the Abilities API plugin active?' ); |
| 64 |
return null; |
| 65 |
} |
| 66 |
$ability = wp_get_ability( $name ); |
| 67 |
if ( ! $ability ) { |
| 68 |
$this->output->error( sprintf( 'Ability "%s" is not registered.', $name ) ); |
| 69 |
return null; |
| 70 |
} |
| 71 |
return $ability; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Show current Cookiebot CMP configuration. |
| 76 |
* |
| 77 |
* ## OPTIONS |
| 78 |
* |
| 79 |
* [--format=<format>] |
| 80 |
* : Render format. |
| 81 |
* --- |
| 82 |
* default: table |
| 83 |
* options: |
| 84 |
* - table |
| 85 |
* - json |
| 86 |
* - yaml |
| 87 |
* - csv |
| 88 |
* --- |
| 89 |
* |
| 90 |
* ## EXAMPLES |
| 91 |
* |
| 92 |
* wp cookiebot status |
| 93 |
* wp cookiebot status --format=json |
| 94 |
* |
| 95 |
* @param array $args Positional arguments. |
| 96 |
* @param array $assoc_args Named arguments. |
| 97 |
*/ |
| 98 |
public function status( $args, $assoc_args ) { |
| 99 |
$ability = $this->get_ability( 'cookiebot/get-status' ); |
| 100 |
if ( ! $ability ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
$result = $ability->execute( null ); |
| 104 |
if ( is_wp_error( $result ) ) { |
| 105 |
$this->output->error( $result->get_error_message() ); |
| 106 |
return; |
| 107 |
} |
| 108 |
$format = $this->get_format( $assoc_args ); |
| 109 |
if ( null === $format ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
$this->output->format_items( |
| 113 |
array( $result ), |
| 114 |
array_keys( $result ), |
| 115 |
$format |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Verify that Cookiebot CMP is correctly configured. |
| 121 |
* |
| 122 |
* Returns issues[] — empty means setup is complete. |
| 123 |
* |
| 124 |
* ## OPTIONS |
| 125 |
* |
| 126 |
* [--format=<format>] |
| 127 |
* : Render format. |
| 128 |
* --- |
| 129 |
* default: table |
| 130 |
* options: |
| 131 |
* - table |
| 132 |
* - json |
| 133 |
* - yaml |
| 134 |
* - csv |
| 135 |
* --- |
| 136 |
* |
| 137 |
* ## EXAMPLES |
| 138 |
* |
| 139 |
* wp cookiebot verify |
| 140 |
* wp cookiebot verify --format=json | jq '.issues' |
| 141 |
* |
| 142 |
* @param array $args Positional arguments. |
| 143 |
* @param array $assoc_args Named arguments. |
| 144 |
*/ |
| 145 |
public function verify( $args, $assoc_args ) { |
| 146 |
$ability = $this->get_ability( 'cookiebot/verify-setup' ); |
| 147 |
if ( ! $ability ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
$result = $ability->execute( null ); |
| 151 |
if ( is_wp_error( $result ) ) { |
| 152 |
$this->output->error( $result->get_error_message() ); |
| 153 |
return; |
| 154 |
} |
| 155 |
$format = $this->get_format( $assoc_args ); |
| 156 |
if ( null === $format ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
if ( 'table' === $format && isset( $result['issues'] ) ) { |
| 160 |
$result['issues'] = implode( '; ', $result['issues'] ); |
| 161 |
} |
| 162 |
$this->output->format_items( |
| 163 |
array( $result ), |
| 164 |
array_keys( $result ), |
| 165 |
$format |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Show the privacy regulations covered by Cookiebot CMP on this site. |
| 171 |
* |
| 172 |
* ## OPTIONS |
| 173 |
* |
| 174 |
* [--format=<format>] |
| 175 |
* : Render format. |
| 176 |
* --- |
| 177 |
* default: table |
| 178 |
* options: |
| 179 |
* - table |
| 180 |
* - json |
| 181 |
* - yaml |
| 182 |
* - csv |
| 183 |
* --- |
| 184 |
* |
| 185 |
* ## EXAMPLES |
| 186 |
* |
| 187 |
* wp cookiebot compliance |
| 188 |
* |
| 189 |
* @param array $args Positional arguments. |
| 190 |
* @param array $assoc_args Named arguments. |
| 191 |
*/ |
| 192 |
public function compliance( $args, $assoc_args ) { |
| 193 |
$ability = $this->get_ability( 'cookiebot/get-compliance-summary' ); |
| 194 |
if ( ! $ability ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
$result = $ability->execute( null ); |
| 198 |
if ( is_wp_error( $result ) ) { |
| 199 |
$this->output->error( $result->get_error_message() ); |
| 200 |
return; |
| 201 |
} |
| 202 |
$format = $this->get_format( $assoc_args ); |
| 203 |
if ( null === $format ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
if ( 'table' === $format && isset( $result['regulations_covered'] ) ) { |
| 207 |
$result['regulations_covered'] = implode( ', ', $result['regulations_covered'] ); |
| 208 |
} |
| 209 |
$this->output->format_items( |
| 210 |
array( $result ), |
| 211 |
array_keys( $result ), |
| 212 |
$format |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Set the Domain Group ID (CBID). |
| 218 |
* |
| 219 |
* Accepts a Cookiebot UUID (36 chars) or a Usercentrics Settings ID (9 or 14 alphanumeric chars). |
| 220 |
* |
| 221 |
* ## OPTIONS |
| 222 |
* |
| 223 |
* <cbid> |
| 224 |
* : The Domain Group ID (UUID for Cookiebot, Settings ID for Usercentrics). |
| 225 |
* |
| 226 |
* ## EXAMPLES |
| 227 |
* |
| 228 |
* wp cookiebot set-cbid 12345678-1234-1234-1234-123456789abc |
| 229 |
* wp cookiebot set-cbid AbCdEfGhI |
| 230 |
* wp cookiebot set-cbid AbCdEfGhIjKlMn |
| 231 |
* |
| 232 |
* @subcommand set-cbid |
| 233 |
* @param array $args Positional arguments. |
| 234 |
* @param array $assoc_args Named arguments. |
| 235 |
*/ |
| 236 |
public function set_cbid( $args, $assoc_args ) { |
| 237 |
if ( empty( $args[0] ) ) { |
| 238 |
$this->output->error( 'CBID is required. Usage: wp cookiebot set-cbid <cbid>' ); |
| 239 |
return; |
| 240 |
} |
| 241 |
$ability = $this->get_ability( 'cookiebot/set-cbid' ); |
| 242 |
if ( ! $ability ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
$result = $ability->execute( array( 'cbid' => $args[0] ) ); |
| 246 |
if ( is_wp_error( $result ) ) { |
| 247 |
$this->output->error( $result->get_error_message() ); |
| 248 |
return; |
| 249 |
} |
| 250 |
$this->output->success( |
| 251 |
sprintf( |
| 252 |
'CBID updated: %s -> set', |
| 253 |
$result['old_cbid_set'] ? 'previous value' : '(unset)' |
| 254 |
) |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Enable or disable Google Consent Mode v2. |
| 260 |
* |
| 261 |
* GCM v2 is enabled by default and required for Google Ads/Analytics in EU/EEA. |
| 262 |
* Only disable if explicitly requested. |
| 263 |
* |
| 264 |
* ## OPTIONS |
| 265 |
* |
| 266 |
* --enabled=<bool> |
| 267 |
* : true to enable, false to disable. |
| 268 |
* |
| 269 |
* ## EXAMPLES |
| 270 |
* |
| 271 |
* wp cookiebot toggle-gcm --enabled=false |
| 272 |
* wp cookiebot toggle-gcm --enabled=true |
| 273 |
* |
| 274 |
* @subcommand toggle-gcm |
| 275 |
* @param array $args Positional arguments. |
| 276 |
* @param array $assoc_args Named arguments. |
| 277 |
*/ |
| 278 |
public function toggle_gcm( $args, $assoc_args ) { |
| 279 |
if ( ! isset( $assoc_args['enabled'] ) || '' === $assoc_args['enabled'] ) { |
| 280 |
$this->output->error( '--enabled flag is required (true|false).' ); |
| 281 |
return; |
| 282 |
} |
| 283 |
$enabled = filter_var( $assoc_args['enabled'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); |
| 284 |
if ( null === $enabled ) { |
| 285 |
$this->output->error( '--enabled must be exactly "true" or "false".' ); |
| 286 |
return; |
| 287 |
} |
| 288 |
$ability = $this->get_ability( 'cookiebot/toggle-gcm' ); |
| 289 |
if ( ! $ability ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
$result = $ability->execute( array( 'enabled' => $enabled ) ); |
| 293 |
if ( is_wp_error( $result ) ) { |
| 294 |
$this->output->error( $result->get_error_message() ); |
| 295 |
return; |
| 296 |
} |
| 297 |
$this->output->success( |
| 298 |
sprintf( |
| 299 |
'Google Consent Mode v2: %s -> %s', |
| 300 |
$result['old_value'] ? 'enabled' : 'disabled', |
| 301 |
$result['new_value'] ? 'enabled' : 'disabled' |
| 302 |
) |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Install and activate the Privacy Policy Generator plugin (idempotent). |
| 308 |
* |
| 309 |
* Returns immediately if the plugin is already active. |
| 310 |
* |
| 311 |
* ## EXAMPLES |
| 312 |
* |
| 313 |
* wp cookiebot install-ppg |
| 314 |
* |
| 315 |
* @subcommand install-ppg |
| 316 |
* @param array $args Positional arguments. |
| 317 |
* @param array $assoc_args Named arguments. |
| 318 |
*/ |
| 319 |
public function install_ppg( $args, $assoc_args ) { |
| 320 |
$ability = $this->get_ability( 'cookiebot/install-ppg' ); |
| 321 |
if ( ! $ability ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
$result = $ability->execute( null ); |
| 325 |
if ( is_wp_error( $result ) ) { |
| 326 |
$this->output->error( $result->get_error_message() ); |
| 327 |
return; |
| 328 |
} |
| 329 |
if ( ! empty( $result['was_already_active'] ) ) { |
| 330 |
$this->output->success( sprintf( 'PPG was already active. Configure at: %s', $result['admin_url'] ) ); |
| 331 |
return; |
| 332 |
} |
| 333 |
$this->output->success( sprintf( 'PPG installed and activated. Configure at: %s', $result['admin_url'] ) ); |
| 334 |
} |
| 335 |
} |
| 336 |
|