client = $client; } public function register() { add_action( self::CRON, array( $this, 'run' ) ); } public function set_source( callable $source ) { $this->source = $source; } public function available() { return null !== $this->source; } /** * @return array{sized: int, review: int, refused: int, unread: int}|WP_Error */ public function run() { if ( null === $this->source ) { return new WP_Error( 'thisbig_no_catalogue', __( 'Sync needs WooCommerce.', 'thisbig' ), array( 'status' => 409 ) ); } $totals = array( 'sized' => 0, 'review' => 0, 'refused' => 0, 'unread' => 0 ); foreach ( array_chunk( call_user_func( $this->source ), ThisBig_Client::BATCH ) as $batch ) { $result = $this->client->import( $batch ); if ( is_wp_error( $result ) ) { return $result; } foreach ( $totals as $key => $count ) { $totals[ $key ] = $count + (int) ( $result[ $key ] ?? 0 ); } } return $totals; } /** Daily, from the first sync on. */ public function schedule() { ThisBig_Plugin::daily( self::CRON ); } }