|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WP-CLI commands for GA4 custom dimension provisioning. |
| 4 | + * |
| 5 | + * @package Newspack |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Newspack\CLI; |
| 9 | + |
| 10 | +use Newspack\GA4_Custom_Dimensions; |
| 11 | +use WP_CLI; |
| 12 | + |
| 13 | +defined( 'ABSPATH' ) || exit; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provisions Newspack's standard GA4 custom dimensions. |
| 17 | + */ |
| 18 | +class GA4_Dimensions { |
| 19 | + |
| 20 | + /** |
| 21 | + * Provision Newspack's GA4 custom dimensions on the connected property. |
| 22 | + * |
| 23 | + * Authenticates via Newspack's Google OAuth (preferred – its tokens carry |
| 24 | + * the `analytics.edit` scope) and falls back to Google Site Kit. Without an |
| 25 | + * explicit `--user`, it authenticates as the Site Kit module owner, so the |
| 26 | + * command works unattended from cron; pass `--user=<admin>` to run as a |
| 27 | + * specific administrator instead. Exits non-zero if any dimension could not |
| 28 | + * be created. |
| 29 | + * |
| 30 | + * ## OPTIONS |
| 31 | + * |
| 32 | + * [--dry-run] |
| 33 | + * : Report connection status and available slots without creating anything. |
| 34 | + * |
| 35 | + * ## EXAMPLES |
| 36 | + * |
| 37 | + * wp newspack ga4-dimensions provision |
| 38 | + * wp newspack ga4-dimensions provision --dry-run |
| 39 | + * wp --user=admin newspack ga4-dimensions provision |
| 40 | + * |
| 41 | + * @param array $args Positional args. |
| 42 | + * @param array $assoc_args Flags. |
| 43 | + */ |
| 44 | + public function provision( $args, $assoc_args ) { |
| 45 | + $dry_run = ! empty( $assoc_args['dry-run'] ); |
| 46 | + |
| 47 | + if ( $dry_run ) { |
| 48 | + $status = GA4_Custom_Dimensions::status(); |
| 49 | + if ( is_wp_error( $status ) ) { |
| 50 | + WP_CLI::error( $status->get_error_message() ); |
| 51 | + } |
| 52 | + WP_CLI::log( 'GA4 dimension provisioning status:' ); |
| 53 | + WP_CLI::log( sprintf( ' Property ID: %s', $status['property_id'] ) ); |
| 54 | + WP_CLI::log( sprintf( ' Site Kit connected: %s', $status['site_kit_connected'] ? 'yes' : 'no' ) ); |
| 55 | + WP_CLI::log( sprintf( ' Auth source: %s', $status['auth_source'] ?? 'unknown' ) ); |
| 56 | + WP_CLI::log( sprintf( ' Event-scoped existing: %d', $status['event_scoped_existing'] ) ); |
| 57 | + WP_CLI::log( sprintf( ' Newspack dimensions: %d total, %d present, %d missing', $status['newspack_total'], count( $status['newspack_present'] ), count( $status['newspack_missing'] ) ) ); |
| 58 | + if ( $status['newspack_missing'] ) { |
| 59 | + WP_CLI::log( ' Missing: ' . implode( ', ', $status['newspack_missing'] ) ); |
| 60 | + } |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $result = GA4_Custom_Dimensions::provision(); |
| 65 | + if ( is_wp_error( $result ) ) { |
| 66 | + WP_CLI::error( $result->get_error_message() ); |
| 67 | + } |
| 68 | + GA4_Custom_Dimensions::maybe_schedule_recheck(); |
| 69 | + WP_CLI::log( sprintf( 'Property ID: %s', $result['property_id'] ) ); |
| 70 | + WP_CLI::log( sprintf( 'Auth source: %s', $result['auth_source'] ?? 'unknown' ) ); |
| 71 | + WP_CLI::log( sprintf( 'Created: %d (%s)', count( $result['created'] ), implode( ', ', $result['created'] ) ) ); |
| 72 | + WP_CLI::log( sprintf( 'Already existed: %d', count( $result['skipped_exists'] ) ) ); |
| 73 | + if ( ! empty( $result['errors'] ) ) { |
| 74 | + WP_CLI::warning( 'Errors:' ); |
| 75 | + foreach ( $result['errors'] as $name => $message ) { |
| 76 | + WP_CLI::log( " $name: $message" ); |
| 77 | + } |
| 78 | + WP_CLI::error( sprintf( '%d dimension(s) could not be created.', count( $result['errors'] ) ) ); |
| 79 | + } |
| 80 | + WP_CLI::success( 'GA4 dimension provisioning complete.' ); |
| 81 | + } |
| 82 | +} |
0 commit comments