queue = $queue; $this->worker = $worker; $this->tester = $tester; } /** * Show how many events are waiting to be delivered. * * ## EXAMPLES * * wp adpix status * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. * @return void */ public function status( $args = array(), $assoc_args = array() ): void { unset( $args, $assoc_args ); WP_CLI::log( sprintf( /* translators: 1: queue name, 2: number of pending events. */ __( 'Queue: %1$s — %2$d event(s) waiting.', 'adpix-analytics' ), $this->queue->label(), $this->queue->size() ) ); } /** * Verify the stored credentials against the collector. * * ## EXAMPLES * * wp adpix verify * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. * @return void */ public function verify( $args = array(), $assoc_args = array() ): void { unset( $args, $assoc_args ); $result = $this->tester->test(); if ( empty( $result['ok'] ) ) { WP_CLI::error( (string) $result['message'] ); } $name = ! empty( $result['property']['name'] ) ? (string) $result['property']['name'] : ''; WP_CLI::success( '' !== $name ? sprintf( /* translators: %s: property name. */ __( 'Connected to %s.', 'adpix-analytics' ), $name ) : (string) $result['message'] ); } /** * Deliver everything that is waiting, without waiting for cron. * * ## OPTIONS * * [--max-passes=] * : Stop after this many batches. Defaults to 20. * * ## EXAMPLES * * wp adpix drain * wp adpix drain --max-passes=100 * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. * @return void */ public function drain( $args = array(), $assoc_args = array() ): void { unset( $args ); $passes = isset( $assoc_args['max-passes'] ) ? max( 1, (int) $assoc_args['max-passes'] ) : 20; $before = $this->queue->size(); // Bounded rather than "until empty": a job that keeps failing is rescheduled rather than // dropped, so an unbounded loop on an unreachable collector would never end. for ( $pass = 0; $pass < $passes; $pass++ ) { $this->worker->drain(); if ( 0 === $this->queue->size() ) { break; } } $after = $this->queue->size(); WP_CLI::success( sprintf( /* translators: 1: number of events delivered, 2: number still waiting. */ __( 'Delivered %1$d event(s). %2$d still waiting.', 'adpix-analytics' ), max( 0, $before - $after ), $after ) ); } /** * Discard every queued event. * * ## OPTIONS * * [--yes] * : Skip the confirmation prompt. * * ## EXAMPLES * * wp adpix purge --yes * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. * @return void */ public function purge( $args = array(), $assoc_args = array() ): void { unset( $args ); WP_CLI::confirm( __( 'Discard every queued event? They cannot be recovered.', 'adpix-analytics' ), $assoc_args ); $removed = $this->queue->purge(); WP_CLI::success( sprintf( /* translators: %d: number of discarded events. */ __( 'Discarded %d event(s).', 'adpix-analytics' ), $removed ) ); } }