WC_Product_CSV_Importer_Controller::delete_claimed_placeholder │ private static │ WC 1.0

Claim one importer placeholder and delete it only if the claim succeeded.

Reading the status from the object cache cannot decide this: a process that completes a placeholder does not invalidate this process's cache, so a published product would still look like a placeholder and be force deleted. The guarded UPDATE makes the status check atomic instead, the way ActionScheduler_DBStore::claim_actions() claims actions, and runs per post so a placeholder completed while the batch is being deleted is still protected.

Method of the class: WC_Product_CSV_Importer_Controller{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = WC_Product_CSV_Importer_Controller::delete_claimed_placeholder( $post_id ): void;
$post_id(int) (required)
Candidate post ID.

WC_Product_CSV_Importer_Controller::delete_claimed_placeholder() code WC 11.2.1

private static function delete_claimed_placeholder( int $post_id ): void {
	global $wpdb;

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
	$wpdb->query(
		$wpdb->prepare(
			"UPDATE {$wpdb->posts} SET post_status = %s WHERE ID = %d AND post_type IN ( 'product', 'product_variation' ) AND post_status IN ( %s, %s )",
			self::CLEANUP_CLAIMED_STATUS,
			$post_id,
			self::CLEANUP_PLACEHOLDER_STATUS,
			self::CLEANUP_CLAIMED_STATUS
		)
	);

	// The claim wrote the row behind the object cache's back, so read it back from the database.
	wp_cache_delete( $post_id, 'posts' );

	$post = get_post( $post_id );

	if ( ! $post || self::CLEANUP_CLAIMED_STATUS !== $post->post_status ) {
		return;
	}

	try {
		wp_delete_post( $post_id, true );
	} finally {
		// Release the claim if the post outlived the delete lifecycle, so it stays a placeholder for the next run.
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$released = $wpdb->query(
			$wpdb->prepare(
				"UPDATE {$wpdb->posts} SET post_status = %s WHERE ID = %d AND post_status = %s",
				self::CLEANUP_PLACEHOLDER_STATUS,
				$post_id,
				self::CLEANUP_CLAIMED_STATUS
			)
		);

		if ( $released ) {
			wp_cache_delete( $post_id, 'posts' );
		}
	}
}