Abstract_REST_Endpoint

Abstract REST Endpoint Zapier


Source

File: src/Common/Event_Automator/Zapier/REST/V1/Endpoints/Abstract_REST_Endpoint.php

abstract class Abstract_REST_Endpoint implements READ_Endpoint_Interface, Swagger_Provider_Interface {
	use REST_Namespace;

	/**
	 * The REST API endpoint path.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	protected $path;

	/**
	 * An instance of the Zapier API handler.
	 *
	 * @since 1.0.0
	 *
	 * @var Api
	 */
	protected $api;

	/**
	 * An instance of the Zapier Swagger_Documentation handler.
	 *
	 * @since 1.0.0
	 *
	 * @var Api
	 */
	protected $documentation;

	/**
	 * Abstract_REST_Endpoint constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param Api $api An instance of the Zapier API handler.
	 * @param Swagger_Documentation $documentation An instance of the Zapier Swagger_Documentation handler.
	 */
	public function __construct( Api $api, Swagger_Documentation $documentation ) {
		$this->api           = $api;
		$this->documentation = $documentation;
	}

	/**
	 * Register the actual endpoint on WP Rest API.
	 *
	 * @since 1.0.0
	 */
	abstract public function register();

	/**
	 * Returns an array in the format used by Swagger 2.0.
	 *
	 * While the structure must conform to that used by v2.0 of Swagger the structure can be that of a full document
	 * or that of a document part.
	 * The intelligence lies in the "gatherer" of informations rather than in the single "providers" implementing this
	 * interface.
	 *
	 * @since 1.0.0
	 *
	 * @link http://swagger.io/
	 *
	 * @return array<string|mixed> An array description of a Swagger supported component.
	 */
	abstract public function get_documentation();

	/**
	 * Provides the content of the `args` array to register the endpoint support for GET requests.
	 *
	 * @since 1.0.0
	 *
	 * @return array<string|mixed> An array of read 'args'.
	 */
	abstract public function READ_args();

	/**
	 * Gets the Endpoint path for this route.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_endpoint_path() {
		return $this->path;
	}

	/**
	 * Sanitize a request argument based on details registered to the route.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value Value of the 'filter' argument.
	 *
	 * @return string|array<string|string> A text field sanitized string or array.
	 */
	public function sanitize_callback( $value ) {
		if ( is_array( $value ) ) {
			return array_map( 'sanitize_text_field', $value );
		}

		return sanitize_text_field( $value );
	}

	/**
	 * Converts an array of arguments suitable for the WP REST API to the Swagger format.
	 *
	 * @since 1.0.0
	 *
	 * @param array<string|mixed> $args An array of arguments to swaggerize.
	 * @param array<string|mixed> $defaults A default array of arguments.
	 *
	 * @return array<string|mixed> The converted arguments.
	 */
	public function swaggerize_args( array $args = [], array $defaults = [] ) {
		if ( empty( $args ) ) {
			return $args;
		}

		$no_description = _x( 'No description provided', 'Default description for Zapier Endpoint.', 'event-automator' );
		$defaults = array_merge( [
			'in'          => 'body',
			'schema' => [
				'type'        => 'string',
			],
			'description' => $no_description,
			'required'    => false,
			'items'       => [
				'type' => 'integer',
			],
		], $defaults );


		$swaggerized = [];
		foreach ( $args as $name => $info ) {
			if ( isset( $info['swagger_type'] ) ) {
				$type = $info['swagger_type'];
			} else {
				$type = isset( $info['type'] ) ? $info['type'] : false;
			}

			$type = $this->convert_type( $type );

			$read = [
				'name'             => $name,
				'in'               => isset( $info['in'] ) ? $info['in'] : false,
				'description'      => isset( $info['description'] ) ? $info['description'] : false,
				'schema' => [
					'type'         => $type,
				],
				'required'         => isset( $info['required'] ) ? $info['required'] : false,
			];

			if ( isset( $info['items'] ) ) {
				$read['schema']['items'] = $info['items'];
			}

			if ( isset( $info['collectionFormat'] ) && $info['collectionFormat'] === 'csv' ) {
				$read['style']   = 'form';
				$read['explode'] = false;
			}

			if ( isset( $info['swagger_type'] ) ) {
				$read['schema']['type'] = $info['swagger_type'];
			}

			// Copy in case we need to mutate default values for this field in args
			$defaultsCopy = $defaults;
			unset( $defaultsCopy['default'] );
			unset( $defaultsCopy['items'] );
			unset( $defaultsCopy['type'] );

			$swaggerized[] = array_merge( $defaultsCopy, array_filter( $read ) );
		}

		return $swaggerized;
	}

	/**
	 * Converts REST format type argument to the corresponding Swagger.io definition.
	 *
	 * @since 1.0.0
	 *
	 * @param string $type A type to convert to Swagger.
	 *
	 * @return string The converted type.
	 */
	protected function convert_type( $type ) {
		$rest_to_swagger_type_map = [
			'int'  => 'integer',
			'bool' => 'boolean',
		];

		return Arr::get( $rest_to_swagger_type_map, $type, $type );
	}

	/**
	 * Load the API Key Pair using the consumer id and secret.
	 *
	 * @since 1.0.0
	 *
	 * @param string $consumer_id     The consumer id to get and load an API Key pair.
	 * @param string $consumer_secret The consumer secret used to verify an API Key pair.
	 *
	 * @return bool|WP_Error Whether the API Key pair could load or WP_Error.
	 */
	protected function load_api_key_pair( $consumer_id, $consumer_secret ) {
		$loaded = $this->api->load_api_key_by_id( $consumer_id, $consumer_secret );
		if ( is_wp_error( $loaded ) ) {
			return $loaded;
		}

		$this->api->set_api_key_last_access( $consumer_id );

		return true;
	}

	/**
	 * Verify the access_token for the Zapier request.
	 *
	 * @since 1.0.0
	 *
	 * @param WP_REST_Request $request The request object.
	 *
	 * @return array<string|string>|WP_Error The decoded access token or WP_Error.
	 */
	protected function verify_token( $request ) {
		$access_token = $request->get_param( 'access_token' );
		$key_pair     = $this->api->decode_jwt( $access_token );

		return $key_pair;
	}
}

Top ↑

Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

Methods