WC_Product_CSV_Importer_Controller::cleanup_after_import │ private static │ WC 1.0

Remove temporary products and mapping data left by the importer.

Method of the class: WC_Product_CSV_Importer_Controller{}

Returns

int|null. ID to resume from, or null once no placeholders are left.

Usage

$result = WC_Product_CSV_Importer_Controller::cleanup_after_import( $cursor ): ?int;
$cursor(int)
Highest placeholder ID earlier cleanup requests have examined.

WC_Product_CSV_Importer_Controller::cleanup_after_import() code WC 11.2.1

private static function cleanup_after_import( int $cursor = 0 ): ?int {
	global $wpdb;

	if ( 0 === $cursor ) {
		self::delete_original_id_markers();
	}

	/** This filter is documented in includes/import/abstract-wc-product-importer.php */
	$time_limit = (int) apply_filters( 'woocommerce_product_importer_default_time_limit', 20 ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
	$deadline   = time() + $time_limit;

	// Page by ID so a placeholder that cannot be deleted is passed over instead of being read again.
	while ( true ) {
		// The claimed status is also selected so a request that died mid-batch does not strand its placeholders.
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$post_ids = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation' ) AND post_status IN ( %s, %s ) AND ID > %d ORDER BY ID ASC LIMIT %d",
				self::CLEANUP_PLACEHOLDER_STATUS,
				self::CLEANUP_CLAIMED_STATUS,
				$cursor,
				self::CLEANUP_BATCH_SIZE
			)
		);

		if ( ! $post_ids ) {
			return null;
		}

		$post_ids = array_map( 'absint', $post_ids );
		$cursor   = (int) end( $post_ids );

		// Ascending IDs delete a placeholder parent before its variations, so WooCommerce removes them through the normal lifecycle.
		foreach ( $post_ids as $post_id ) {
			self::delete_claimed_placeholder( $post_id );

			// One delete lifecycle can take seconds, so check the budget per placeholder rather than per batch.
			if ( time() >= $deadline ) {
				return $post_id;
			}
		}
	}
}