'The data move of Phase 3: the pop-ups\' codes and the workflow steps\' coupons become offers. status (reads only), shadow (make each one\'s offer, switched off: nothing a customer meets changes; an item changed since its shadow is shadowed again; Phase 3 on), compare (does each offer give what the coupon or the step gives today? reads only), switch (customers get the offers; refused while compare reports a blocking difference not left out with --skip; Phase 3 on), undo (back to the state before the switch; also with Phase 3 off). Each can run twice; a list that names an unknown item is refused whole.', 'synopsis' => array( array( 'type' => 'positional', 'name' => 'action', 'description' => 'status, shadow, compare, switch or undo.', 'optional' => false, 'options' => self::ACTIONS ), array( 'type' => 'assoc', 'name' => 'skip', 'description' => 'switch: the pop-ups (their post ids) and steps (wf::) to leave as they are, comma-separated.', 'optional' => true ), array( 'type' => 'assoc', 'name' => 'only', 'description' => 'undo: only these pop-ups and steps, as --skip names them.', 'optional' => true ), array( 'type' => 'assoc', 'name' => 'format', 'description' => 'text (the default) or json.', 'optional' => true ), ), ) ); } public static function move( $args, $assoc ) { $answer = self::action( isset( $args[0] ) ? (string) $args[0] : '', (array) $assoc ); if ( isset( $assoc['format'] ) && 'json' === $assoc['format'] ) { WP_CLI::line( (string) wp_json_encode( $answer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); if ( ! $answer['ok'] ) { WP_CLI::halt( 1 ); } return; } foreach ( self::lines( $answer ) as $line ) { WP_CLI::line( $line ); } if ( ! $answer['ok'] ) { WP_CLI::error( $answer['message'] ); } WP_CLI::success( $answer['message'] ); } public static function action( $action, array $assoc = array() ) { $action = (string) $action; if ( ! in_array( $action, self::ACTIONS, true ) ) { return self::answer( false, 'Say what to do: ' . implode( ', ', self::ACTIONS ) . '.' ); } if ( ! class_exists( 'WooCommerce' ) && ! class_exists( 'WC_Coupon' ) ) { return self::answer( false, 'WooCommerce is not active on this site.' ); } switch ( $action ) { case 'status': $status = AIEO_DMM_Offer_Access_Move::status(); return self::answer( true, sprintf( '%d pop-ups with a code, %d steps with a coupon; %d moved, %d shadowed, %d cut short; %d blocking differences at the last comparison.', $status['popups_with_code'], $status['steps_with_offer'], $status['moved']['popups'] + $status['moved']['steps'], $status['shadowed']['popups'] + $status['shadowed']['steps'], $status['pending'], (int) $status['compare']['blocking'] ), $status ); case 'shadow': $done = AIEO_DMM_Offer_Access_Move::shadow(); if ( is_wp_error( $done ) ) { return self::refused( $done ); } return self::answer( true, sprintf( '%d made, %d made before, %d shadowed again, %d let go (no longer the move\'s), %d could not be made. Nothing a customer meets has changed: compare, then switch.', count( $done['made'] ), count( $done['kept'] ), count( $done['redone'] ), count( $done['dropped'] ), count( $done['failed'] ) ), $done ); case 'compare': $report = AIEO_DMM_Offer_Access_Move::compare(); if ( is_wp_error( $report ) ) { return self::refused( $report ); } return self::answer( true, sprintf( '%d pop-ups and steps compared: %d differences that keep them out of the switch, %d noted.', count( $report['items'] ), (int) $report['blocking'], count( $report['differences'] ) - (int) $report['blocking'] ), $report ); case 'switch': $done = AIEO_DMM_Offer_Access_Move::switch_now( self::list_of( $assoc['skip'] ?? '' ) ); if ( is_wp_error( $done ) ) { return self::refused( $done ); } return self::answer( empty( $done['failed'] ), sprintf( '%d switched, %d switched before, %d left as they are, %d failed.', count( $done['switched'] ), count( $done['already'] ), count( $done['skipped'] ), count( $done['failed'] ) ), $done ); case 'undo': $done = AIEO_DMM_Offer_Access_Move::undo( self::list_of( $assoc['only'] ?? '' ) ); if ( is_wp_error( $done ) ) { return self::refused( $done ); } return self::answer( empty( $done['failed'] ), sprintf( '%d taken back; %d with something left as it is; %d still moved (run undo again).', count( $done['undone'] ), count( $done['left'] ), count( $done['failed'] ) ), $done ); } return self::answer( false, 'Say what to do.' ); } public static function lines( array $answer ): array { $data = (array) $answer['data']; $lines = array(); foreach ( (array) ( $data['differences'] ?? array() ) as $difference ) { $lines[] = sprintf( '%s [%s%s] %s', $difference['item'], $difference['kind'], empty( $difference['blocking'] ) ? ', noted' : '', $difference['words'] ); } foreach ( array( 'redone' => 'shadowed again', 'dropped' => 'let go' ) as $part => $said ) { foreach ( (array) ( $data[ $part ] ?? array() ) as $key => $why ) { $lines[] = sprintf( '%s %s: %s', $key, $said, $why ); } } foreach ( (array) ( $data['failed'] ?? array() ) as $key => $why ) { $lines[] = sprintf( '%s failed: %s', $key, $why ); } foreach ( (array) ( $data['left'] ?? array() ) as $key => $why ) { $lines[] = sprintf( '%s: %s', $key, $why ); } if ( isset( $data['phase_3'] ) ) { $lines[] = sprintf( 'Phase 3: %s (store %s).', $data['phase_3'] ? 'on' : 'off', '' !== (string) $data['store'] ? $data['store'] : 'legacy' ); $lines[] = sprintf( 'Pop-ups with a code: %d (%d naming no coupon, %d naming an offer).', $data['popups_with_code'], $data['popups_without_coupon'], $data['popups_with_offer'] ); $lines[] = sprintf( 'Steps with a coupon of their own: %d (%d naming an offer).', $data['steps_with_offer'], $data['steps_with_offer_id'] ); $lines[] = sprintf( 'Shadowed: %d pop-ups, %d steps. Moved: %d pop-ups, %d steps. Cut short: %d. Offers made: %d. Could not be made: %d.', $data['shadowed']['popups'], $data['shadowed']['steps'], $data['moved']['popups'], $data['moved']['steps'], $data['pending'], $data['offers'], $data['failed'] ); if ( $data['skipped'] ) { $lines[] = 'Left out at the last switch: ' . implode( ', ', $data['skipped'] ) . '.'; } $compare = (array) $data['compare']; if ( '' !== (string) $compare['at_gmt'] ) { $lines[] = sprintf( 'The last comparison (%s GMT): %d blocking differences, %d noted.', $compare['at_gmt'], (int) $compare['blocking'], count( (array) $compare['differences'] ) - (int) $compare['blocking'] ); foreach ( (array) $compare['differences'] as $difference ) { $lines[] = sprintf( ' %s [%s%s] %s', $difference['item'], $difference['kind'], empty( $difference['blocking'] ) ? ', noted' : '', $difference['words'] ); } } } return $lines; } private static function answer( bool $ok, string $message, array $data = array() ): array { return array( 'ok' => $ok, 'message' => $message, 'data' => $data, ); } private static function refused( WP_Error $error ): array { $data = $error->get_error_data(); return self::answer( false, $error->get_error_message(), array( 'code' => $error->get_error_code() ) + ( is_array( $data ) ? $data : array() ) ); } private static function list_of( $value ): array { return '' === trim( (string) $value ) ? array() : array_map( 'trim', explode( ',', (string) $value ) ); } } }