Automattic\WooCommerce\Admin\Features\Blueprint

RestApi::import()publicWC 1.0

Handle the import request.

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

Хуков нет.

Возвращает

\WP_HTTP_Response. The response object.

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

$RestApi = new RestApi();
$RestApi->import();

Код RestApi::import() WC 9.7.1

public function import() {

	// Check for nonce to prevent CSRF.
	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
	if ( ! isset( $_POST['blueprint_upload_nonce'] ) || ! \wp_verify_nonce( $_POST['blueprint_upload_nonce'], 'blueprint_upload_nonce' ) ) {
		return new \WP_HTTP_Response(
			array(
				'status'  => 'error',
				'message' => __( 'Invalid nonce', 'woocommerce' ),
			),
			400
		);
	}

	// phpcs:ignore
	if ( ! empty( $_FILES['file'] ) && $_FILES['file']['error'] === UPLOAD_ERR_OK ) {
		// phpcs:ignore
		$uploaded_file = $_FILES['file']['tmp_name'];
		// phpcs:ignore
		$mime_type     = $_FILES['file']['type'];

		if ( 'application/json' !== $mime_type && 'application/zip' !== $mime_type ) {
			return new \WP_HTTP_Response(
				array(
					'status'  => 'error',
					'message' => __( 'Invalid file type', 'woocommerce' ),
				),
				400
			);
		}

		try {
			// phpcs:ignore
			if ( $mime_type === 'application/zip' ) {
				// phpcs:ignore
				if ( ! function_exists( 'wp_handle_upload' ) ) {
					require_once ABSPATH . 'wp-admin/includes/file.php';
				}

				$movefile = \wp_handle_upload( $_FILES['file'], array( 'test_form' => false ) );

				if ( $movefile && ! isset( $movefile['error'] ) ) {
					$blueprint = ImportSchema::create_from_zip( $movefile['file'] );
				} else {
					throw new InvalidArgumentException( $movefile['error'] );
				}
			} else {
				$blueprint = ImportSchema::create_from_json( $uploaded_file );
			}
		} catch ( \Exception $e ) {
			return new \WP_HTTP_Response(
				array(
					'status'  => 'error',
					'message' => $e->getMessage(),
				),
				400
			);
		}

		$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() ? 'success' : 'error';

		return new \WP_HTTP_Response(
			array(
				'status'  => $is_success,
				'message' => 'error' === $is_success ? __( 'There was an error while processing your schema', 'woocommerce' ) : 'success',
				'data'    => array(
					'redirect' => admin_url( $redirect_url ),
					'result'   => $result_formatter->format(),
				),
			),
			200
		);
	}

	return new \WP_HTTP_Response(
		array(
			'status'  => 'error',
			'message' => __( 'No file uploaded', 'woocommerce' ),
		),
		400
	);
}