self::HOOK, 'group' => 'catalogrelay', 'status' => 'pending', // ActionScheduler_Store::STATUS_PENDING. 'per_page' => 1000, ) ); foreach ( (array) $pending as $action ) { $args = is_object( $action ) && method_exists( $action, 'get_args' ) ? array_values( (array) $action->get_args() ) : array(); $jobs[] = (int) ( $args[0] ?? 0 ); } as_unschedule_all_actions( self::HOOK, null, 'catalogrelay' ); // null = any arguments. $jobs = array_values( array_unique( array_filter( array_map( 'intval', $jobs ) ) ) ); if ( $jobs ) { update_option( self::PARKED, $jobs, false ); } } /** * Queue the repairs park() set aside again (self-heal once the plugin runs). * * @return void */ public static function requeueParked(): void { $jobs = get_option( self::PARKED ); if ( ! $jobs || ! function_exists( 'as_enqueue_async_action' ) || ! function_exists( 'as_has_scheduled_action' ) ) { return; } delete_option( self::PARKED ); foreach ( array_map( 'intval', (array) $jobs ) as $job ) { // Not Action Scheduler's unique flag (DEVIATIONS 25), as in ImportJobType::scheduleFinalize(). if ( $job > 0 && ! as_has_scheduled_action( self::HOOK, array( $job ), 'catalogrelay' ) ) { as_enqueue_async_action( self::HOOK, array( $job ), 'catalogrelay', false ); } } } /** * Re-sync the parents of a job's variations, recount the terms of its products and of their parents (a variation row * can add an attribute term to its parent). * * @param int $jobId Job. * @return int Parents synchronised. */ public static function run( int $jobId ): int { global $wpdb; $parents = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT SUBSTRING_INDEX(result_ref, ':', -1) FROM {$wpdb->prefix}catalogrelay_job_items WHERE job_id = %d AND status = 'done' AND result_ref REGEXP '^[cu]:[0-9]+:[0-9]+$'", $jobId ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. $ids = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(result_ref, ':', 2), ':', -1) FROM {$wpdb->prefix}catalogrelay_job_items WHERE job_id = %d AND status = 'done' AND result_ref REGEXP '^[cu]:[0-9]+'", $jobId ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. // A row found written after an interruption (reconcile: applied) has no receipt, and it is the row most likely to have // lost its parent sync and term counts: its products are in the job's ledger batch. $ledger = self::ledgerProducts( $jobId ); $parents = array_values( array_unique( array_filter( array_merge( $parents, self::parentsOf( $ledger ) ) ) ) ); foreach ( $parents as $parent ) { ParentSync::syncNow( $parent ); } self::recount( array_values( array_unique( array_filter( array_merge( $ids, $ledger, $parents ) ) ) ) ); return count( $parents ); } /** * Products of the job's ledger batch. * * @param int $jobId Job. * @return int[] */ private static function ledgerProducts( int $jobId ): array { global $wpdb; $job = JobStore::find( Container::requireCurrent(), $jobId ); $batch = $job ? JobParams::of( $job )->batchId() : 0; if ( $batch < 1 ) { return array(); } return array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT object_ref FROM {$wpdb->prefix}catalogrelay_ledger WHERE batch_id = %d", $batch ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. } /** * Parents of the variations among products. * * @param int[] $ids Products. * @return int[] */ private static function parentsOf( array $ids ): array { global $wpdb; $out = array(); foreach ( array_chunk( array_filter( $ids ), self::PAGE ) as $page ) { $in = implode( ', ', array_fill( 0, count( $page ), '%d' ) ); $out = array_merge( $out, array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_parent FROM {$wpdb->posts} WHERE ID IN ($in) AND post_type = 'product_variation' AND post_parent > 0", $page ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- placeholders generated from a list. } return $out; } /** * Recount the terms the job's products carry (the counts of a chunk are deferred to its end, which a killed process * never reaches) and rebuild the category tree (a category created in that chunk is missing from it). * * @param int[] $ids Products. * @return void */ private static function recount( array $ids ): void { global $wpdb; $terms = array(); foreach ( array_chunk( $ids, self::PAGE ) as $page ) { $in = implode( ', ', array_fill( 0, count( $page ), '%d' ) ); foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT tt.term_taxonomy_id, tt.taxonomy FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id WHERE tr.object_id IN ($in)", $page ), ARRAY_A ) as $r ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- placeholders generated from a list. $terms[ (string) $r['taxonomy'] ][] = (int) $r['term_taxonomy_id']; } } foreach ( $terms as $taxonomy => $tt_ids ) { if ( taxonomy_exists( $taxonomy ) ) { wp_update_term_count_now( array_values( array_unique( $tt_ids ) ), $taxonomy ); } } if ( taxonomy_exists( 'product_cat' ) ) { clean_taxonomy_cache( 'product_cat' ); } } }