Automattic\WooCommerce\Admin\Features\Blueprint

RestApi::process()publicWC 1.0

Process the uploaded file.

Метод класса: RestApi{}

Хуков нет.

Возвращает

Массив.

Использование

$RestApi = new RestApi();
$RestApi->process( $request );
$request(\WP_REST_Request) (обязательный)
request object.

Код RestApi::process() WC 9.7.1

public function process( \WP_REST_Request $request ) {
	$response = array(
		'processed' => false,
		'message'   => '',
		'data'      => array(
			'redirect' => '',
			'result'   => array(),
		),
	);

	$ref   = $request->get_param( 'reference' );
	$nonce = $request->get_param( 'process_nonce' );

	if ( ! \wp_verify_nonce( $nonce, $ref ) ) {
		$response['message'] = __( 'Invalid nonce', 'woocommerce' );
		return $response;
	}

	$fullpath  = get_temp_dir() . $ref;
	$extension = pathinfo( $fullpath, PATHINFO_EXTENSION );

	// Process the uploaded file.
	try {
		if ( 'zip' === $extension ) {
			$blueprint = ImportSchema::create_from_zip( $fullpath );
		} else {
			$blueprint = ImportSchema::create_from_json( $fullpath );
		}
	} catch ( \Exception $e ) {
		$response['message'] = $e->getMessage();
		return $response;
	}

	$results          = $blueprint->import();
	$result_formatter = new JsonResultFormatter( $results );
	$redirect         = $blueprint->get_schema()->landingPage ?? null;
	$redirect_url     = $redirect->url ?? 'admin.php?page=wc-admin';

	$is_success = $result_formatter->is_success();

	$response['processed'] = $is_success;
	$response['message']   = false === $is_success ? __( 'There was an error while processing your schema', 'woocommerce' ) : 'success';
	$response['data']      = array(
		'redirect' => admin_url( $redirect_url ),
		'result'   => $result_formatter->format(),
	);

	return $response;
}