PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.7.4
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.7.4
4.7.4 4.7.5 4.7.3 4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 All 95 releases
cookiebot / src / cli / Cookiebot_CLI_Command.php

Cookiebot_CLI_Command.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 4.7.4, at src/cli/Cookiebot_CLI_Command.php

333 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_format 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_format = 'table' ) {
46 $allowed = array( 'table', 'json', 'yaml', 'csv' );
47 $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : $default_format;
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 */
235 public function set_cbid( $args ) {
236 if ( empty( $args[0] ) ) {
237 $this->output->error( 'CBID is required. Usage: wp cookiebot set-cbid <cbid>' );
238 return;
239 }
240 $ability = $this->get_ability( 'cookiebot/set-cbid' );
241 if ( ! $ability ) {
242 return;
243 }
244 $result = $ability->execute( array( 'cbid' => $args[0] ) );
245 if ( is_wp_error( $result ) ) {
246 $this->output->error( $result->get_error_message() );
247 return;
248 }
249 $this->output->success(
250 sprintf(
251 'CBID updated: %s -> set',
252 $result['old_cbid_set'] ? 'previous value' : '(unset)'
253 )
254 );
255 }
256
257 /**
258 * Enable or disable Google Consent Mode v2.
259 *
260 * GCM v2 is enabled by default and required for Google Ads/Analytics in EU/EEA.
261 * Only disable if explicitly requested.
262 *
263 * ## OPTIONS
264 *
265 * --enabled=<bool>
266 * : true to enable, false to disable.
267 *
268 * ## EXAMPLES
269 *
270 * wp cookiebot toggle-gcm --enabled=false
271 * wp cookiebot toggle-gcm --enabled=true
272 *
273 * @subcommand toggle-gcm
274 * @param array $args Positional arguments.
275 * @param array $assoc_args Named arguments.
276 */
277 public function toggle_gcm( $args, $assoc_args ) {
278 if ( ! isset( $assoc_args['enabled'] ) || '' === $assoc_args['enabled'] ) {
279 $this->output->error( '--enabled flag is required (true|false).' );
280 return;
281 }
282 $enabled = filter_var( $assoc_args['enabled'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
283 if ( null === $enabled ) {
284 $this->output->error( '--enabled must be exactly "true" or "false".' );
285 return;
286 }
287 $ability = $this->get_ability( 'cookiebot/toggle-gcm' );
288 if ( ! $ability ) {
289 return;
290 }
291 $result = $ability->execute( array( 'enabled' => $enabled ) );
292 if ( is_wp_error( $result ) ) {
293 $this->output->error( $result->get_error_message() );
294 return;
295 }
296 $this->output->success(
297 sprintf(
298 'Google Consent Mode v2: %s -> %s',
299 $result['old_value'] ? 'enabled' : 'disabled',
300 $result['new_value'] ? 'enabled' : 'disabled'
301 )
302 );
303 }
304
305 /**
306 * Install and activate the Privacy Policy Generator plugin (idempotent).
307 *
308 * Returns immediately if the plugin is already active.
309 *
310 * ## EXAMPLES
311 *
312 * wp cookiebot install-ppg
313 *
314 * @subcommand install-ppg
315 */
316 public function install_ppg() {
317 $ability = $this->get_ability( 'cookiebot/install-ppg' );
318 if ( ! $ability ) {
319 return;
320 }
321 $result = $ability->execute( null );
322 if ( is_wp_error( $result ) ) {
323 $this->output->error( $result->get_error_message() );
324 return;
325 }
326 if ( ! empty( $result['was_already_active'] ) ) {
327 $this->output->success( sprintf( 'PPG was already active. Configure at: %s', $result['admin_url'] ) );
328 return;
329 }
330 $this->output->success( sprintf( 'PPG installed and activated. Configure at: %s', $result['admin_url'] ) );
331 }
332 }
333