WC_Product_CSV_Importer_Controller::dispatch_ajax │ public static │ WC 9.3.0

Processes AJAX requests related to a product CSV import.

Method of the class: WC_Product_CSV_Importer_Controller{}

Returns

null. Nothing (null).

Usage

$result = WC_Product_CSV_Importer_Controller::dispatch_ajax();

Changelog

Since 9.3.0 Introduced.

WC_Product_CSV_Importer_Controller::dispatch_ajax() code WC 11.2.1

public static function dispatch_ajax() {
	check_ajax_referer( 'wc-product-import', 'security' );

	try {
		$position = wc_clean( wp_unslash( $_POST['position'] ?? '' ) );

		// Cleanup runs in requests of its own so it cannot time out behind the last import batch.
		if ( is_string( $position ) && preg_match( '/^' . preg_quote( self::CLEANUP_POSITION_PREFIX, '/' ) . '(\\d+)$/', $position, $matches ) ) {
			self::dispatch_ajax_cleanup( (int) $matches[1] );

			return;
		}

		$file = wc_clean( wp_unslash( $_POST['file'] ?? '' ) ); // PHPCS: input var ok.
		self::validate_file_path( $file );

		$params = array(
			'delimiter'          => ! empty( $_POST['delimiter'] ) ? wc_clean( wp_unslash( $_POST['delimiter'] ) ) : ',', // PHPCS: input var ok.
			'start_pos'          => absint( $position ),
			'mapping'            => isset( $_POST['mapping'] ) ? (array) wc_clean( wp_unslash( $_POST['mapping'] ) ) : array(), // PHPCS: input var ok.
			'update_existing'    => isset( $_POST['update_existing'] ) ? (bool) $_POST['update_existing'] : false, // PHPCS: input var ok.
			'character_encoding' => isset( $_POST['character_encoding'] ) ? wc_clean( wp_unslash( $_POST['character_encoding'] ) ) : '',

			/**
			 * Batch size for the product import process.
			 *
			 * @param int $size Batch size.
			 *
			 * @since 3.1.0
			 */
			'lines'              => apply_filters( 'woocommerce_product_import_batch_size', 30 ),
			'parse'              => true,
		);

		// Log failures.
		if ( 0 !== $params['start_pos'] ) {
			$error_log = array_filter( (array) get_user_option( 'product_import_error_log' ) );
		} else {
			$error_log = array();
		}

		if ( 0 === $params['start_pos'] ) {
			self::release_stranded_cleanup_claims();
		}

		include_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';

		$importer         = self::get_importer( $file, $params );
		$results          = $importer->import();
		$percent_complete = $importer->get_percent_complete();
		$error_log        = array_merge( $error_log, $results['failed'], $results['skipped'] );

		if ( 100 === $percent_complete ) {
			self::delete_original_id_markers();
		}

		update_user_option( get_current_user_id(), 'product_import_error_log', $error_log );

		// The last row hands over to the cleanup phase rather than running it in this request.
		$next_position = 100 === $percent_complete ? self::CLEANUP_POSITION_PREFIX . '0' : $importer->get_file_position();

		wp_send_json_success(
			array(
				'position'            => $next_position,
				'percentage'          => $percent_complete,
				'imported'            => is_countable( $results['imported'] ) ? count( $results['imported'] ) : 0,
				'imported_variations' => is_countable( $results['imported_variations'] ) ? count( $results['imported_variations'] ) : 0,
				'failed'              => is_countable( $results['failed'] ) ? count( $results['failed'] ) : 0,
				'updated'             => is_countable( $results['updated'] ) ? count( $results['updated'] ) : 0,
				'skipped'             => is_countable( $results['skipped'] ) ? count( $results['skipped'] ) : 0,
			)
		);
	} catch ( \Exception $e ) {
		wp_send_json_error( array( 'message' => $e->getMessage() ) );
	}
}