Updated_Events
Class Updated_Events
Source
File: src/Common/Event_Automator/Zapier/REST/V1/Endpoints/Updated_Events.php
class Updated_Events extends Abstract_REST_Endpoint {
use Event;
/**
* @inheritDoc
*/
protected $path = '/updated-events';
/**
* @inheritdoc
*/
protected static $endpoint_id = 'updated_events';
/**
* @inheritdoc
*/
protected static $type = 'queue';
/**
* The trigger accessed with this endpoint.
*
* @since 1.2.0
*
* @var Trigger_Updated_Events
*/
public $trigger;
/**
* Abstract_REST_Endpoint constructor.
*
* @since 1.2.0
*
* @param Api $api An instance of the Zapier API handler.
* @param Swagger_Documentation $documentation An instance of the Zapier Swagger_Documentation handler.
* @param Trigger_Updated_Events $trigger The trigger accessed with this endpoint.
*/
public function __construct( Api $api, Swagger_Documentation $documentation, Trigger_Updated_Events $trigger ) {
parent::__construct( $api, $documentation );
$this->trigger = $trigger;
}
/**
* @inheritdoc
*/
protected function get_display_name() : string {
return _x( 'Updated Events', 'Display name of the Zapier endpoint for updated events.', 'event-automator' );
}
/**
* @inheritDoc
*/
public function register() {
// If disabled, then do not register the route.
if ( ! $this->enabled ) {
return;
}
register_rest_route(
$this->get_events_route_namespace(),
$this->get_endpoint_path(),
[
'methods' => WP_REST_Server::READABLE,
'args' => $this->READ_args(),
'callback' => [ $this, 'get' ],
'permission_callback' => '__return_true',
]
);
$this->documentation->register_documentation_provider( $this->get_endpoint_path(), $this );
}
/**
* Get events from updated event queue.
*
* @since 1.2.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response The response from the updated event queue.
*/
public function get( WP_REST_Request $request ) {
// No cache headers to prevent hosting from caching the endpoint
nocache_headers();
$verified_token = $this->verify_token( $request );
if ( is_wp_error( $verified_token ) ) {
return new WP_REST_Response( $verified_token, 400 );
}
$loaded = $this->load_api_key_pair( $verified_token['consumer_id'], $verified_token['consumer_secret'], $verified_token );
if ( is_wp_error( $loaded ) ) {
return new WP_REST_Response( $loaded, 400 );
}
$current_queue = $this->trigger->get_queue();
if ( empty( $current_queue ) ) {
$data = [
'id' => 'no-updated-events',
];
return new WP_REST_Response( [ $data ] );
}
$next_event_id = (int) array_shift( $current_queue );
if ( empty( $next_event_id ) ) {
$this->trigger->set_queue( $current_queue );
$data = [
'id' => 'no valid event.',
];
return new WP_REST_Response( [ $data ] );
}
$next_event = $this->get_mapped_event( $next_event_id, true );
if ( ! $next_event ) {
$this->trigger->set_queue( $current_queue );
$data = [
'id' => 'not a valid post type.',
];
return new WP_REST_Response( [ $data ] );
}
$this->trigger->set_queue( $current_queue );
return new WP_REST_Response( [ $next_event ] );
}
/**
* @inheritDoc
*/
public function get_documentation() {
$POST_defaults = [
'in' => 'formData',
'default' => '',
'type' => 'string',
];
$post_args = array_merge( $this->READ_args() );
return [
'post' => [
'consumes' => [ 'application/x-www-form-urlencoded' ],
'parameters' => $this->swaggerize_args( $post_args, $POST_defaults ),
'responses' => [
'201' => [
'description' => _x( 'Returns successful checking of the updated event queue.', 'Description for the Zapier Updated Event REST endpoint on a successful return.', 'event-automator' ),
'schema' => [
'$ref' => '#/definitions/Zapier',
],
],
'400' => [
'description' => _x( 'A required parameter is missing or an input parameter is in the wrong format', 'Description for the Zapier Updated Event REST endpoint missing a required parameter.', 'event-automator' ),
],
],
],
];
}
/**
* @inheritDoc
*/
public function READ_args() {
return [
'access_token' => [
'required' => true,
'validate_callback' => [ $this, 'sanitize_callback' ],
'type' => 'string',
'description' => _x( 'The access token to authorize Zapier connection.', 'Description for the Zapier Updated Event REST endpoint required parameter.', 'event-automator' ),
],
];
}
}
Changelog
| Version | Description |
|---|---|
| 6.0.0 | Introduced. |
Methods
- __construct — Abstract_REST_Endpoint constructor.
- get — Get events from updated event queue.
- get_documentation
- READ_args
- register