Skip to content

Commit

Permalink
Merge pull request #2101 from the-events-calendar/fix/EVA-157-Conditi…
Browse files Browse the repository at this point in the history
…onal-Endpoint-Disabling

EVA-157 Conditional Endpoint Disabling
  • Loading branch information
pattihis authored Jun 12, 2024
2 parents ac9f3ba + b59a60f commit e1ded06
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 225 deletions.
24 changes: 3 additions & 21 deletions src/Common/Event_Automator/Integrations/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace TEC\Event_Automator\Integrations;

use TEC\Event_Automator\Plugin;

/**
* Class Settings
*
Expand All @@ -26,31 +24,15 @@ class Assets {
* @since TBD Migrated to Common from Event Automator
*/
public function register_admin_assets() {
$admin_helpers = \Tribe__Admin__Helpers::instance();
$plugin = tribe( Plugin::class );

tribe_asset(
$plugin,
'tec-event-automator-css',
'tec-event-automator.css',
[ 'tribe-tooltip' ],
'admin_enqueue_scripts',
[
'conditionals' => [
[ $admin_helpers, 'is_screen' ],
],
]
);

tribe_asset(
$plugin,
\Tribe__Main::instance(),
'tec-event-automator-js',
'tec-event-automator.js',
[ 'jquery', 'tribe-dropdowns', 'tribe-clipboard', 'tribe-tooltip-js' ],
[ 'jquery' ],
'admin_enqueue_scripts',
[
'conditionals' => [
[ $admin_helpers, 'is_screen' ],
[ \Tribe__Admin__Helpers::instance(), 'is_screen' ],
],
'localize' => [
'name' => 'tec_automator',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,37 +234,39 @@ public function swaggerize_args( array $args = [], array $defaults = [] ) {
}

$no_description = _x( 'No description provided', 'Default description for integration endpoint.', 'tribe-common' );
$defaults = array_merge( [
'in' => 'body',
'schema' => [
'type' => 'string',
],
'description' => $no_description,
'required' => false,
'items' => [
'type' => 'integer',
$defaults = array_merge(
[
'in' => 'body',
'schema' => [
'type' => 'string',
],
'description' => $no_description,
'required' => false,
'items' => [
'type' => 'integer',
],
],
], $defaults );

$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 = $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,
'name' => $name,
'in' => $info['in'] ?? false,
'description' => $info['description'] ?? false,
'schema' => [
'type' => $type,
],
'required' => isset( $info['required'] ) ? $info['required'] : false,
'required' => $info['required'] ?? false,
];

if ( isset( $info['items'] ) ) {
Expand All @@ -281,12 +283,12 @@ public function swaggerize_args( array $args = [], array $defaults = [] ) {
}

// 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'] );
$defaults_copy = $defaults;
unset( $defaults_copy['default'] );
unset( $defaults_copy['items'] );
unset( $defaults_copy['type'] );

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

return $swaggerized;
Expand Down Expand Up @@ -344,7 +346,7 @@ protected function load_api_key_pair( $consumer_id, $consumer_secret, $token = '
}

$app_name = empty( $token['app_name'] ) ? '' : $token['app_name'];
$app_name = $app_header_id ? : $app_name;
$app_name = $app_header_id ?: $app_name;
$this->api->set_api_key_last_access( $consumer_id, $app_name );
$this->set_endpoint_last_access( $app_name );

Expand All @@ -369,9 +371,7 @@ protected function verify_token( $request ) {
return new WP_Error( 'missing_access_token', __( 'Missing access token.', 'tribe-common' ), [ 'status' => 401 ] );
}

$key_pair = $this->api->decode_jwt( $access_token );

return $key_pair;
return $this->api->decode_jwt( $access_token );
}

/**
Expand Down Expand Up @@ -440,7 +440,7 @@ abstract protected function get_display_name(): string;
* @since TBD Migrated to Common from Event Automator
*/
public function add_to_dashboard() {
$api_id = $this->api::get_api_id();
$api_id = $this->api::get_api_id();

add_filter( "tec_event_automator_{$api_id}_endpoints", [ $this, 'add_endpoint_details' ], 10, 2 );
}
Expand Down Expand Up @@ -483,33 +483,20 @@ public function get_endpoint_details() {
];

// Setup queue counts only on that endpoint type.
if (
static::$type !== 'queue'
|| ! isset( $this->trigger )
) {
/**
* Filters the integration endpoint details.
*
* @since TBD Migrated to Common from Event Automator
*
* @param array<string,array> $endpoint An array of the integration endpoint details.
* @param Abstract_REST_Endpoint $this An instance of the endpoint.
*/
return apply_filters( "tec_event_automator_{$api_id}_endpoint_details", $endpoint, $this );
if ( static::$type === 'queue' && isset( $this->trigger ) ) {
$endpoint_queue = (array) $this->trigger->get_queue();
$endpoint['count'] = empty( $endpoint_queue ) ? 0 : count( $endpoint_queue );
}

$endpoint_queue = (array) $this->trigger->get_queue();
$endpoint['count'] = empty( $endpoint_queue ) ? 0 : count( $endpoint_queue );

/**
* Filters the integation queue type endpoint details.
* Filters the integration endpoint details.
*
* @since TBD Migrated to Common from Event Automator
*
* @param array<string,array> $endpoint An array of the integration endpoint details.
* @param Abstract_REST_Endpoint $this An instance of the endpoint.
*/
return apply_filters( "tec_event_automator_{$api_id}_queue_endpoint_details", $endpoint, $this );
return apply_filters( "tec_event_automator_{$api_id}_endpoint_details", $endpoint, $this );
}

/**
Expand All @@ -520,7 +507,13 @@ public function get_endpoint_details() {
* @return array<string,array> An array of saved details for an endpoint.
*/
public function get_saved_details() {
return get_option( $this->get_option_id(), [ 'last_access' => '', 'enabled' => true ] );
return get_option(
$this->get_option_id(),
[
'last_access' => '',
'enabled' => true,
]
);
}

/**
Expand Down Expand Up @@ -590,7 +583,7 @@ public function add_to_queue( $post_id, $data ) {
return;
}

$api_id = $this->api::get_api_id();
$api_id = $this->api::get_api_id();

/**
* Filters data passed to the trigger queue for an endpoint.
Expand Down Expand Up @@ -703,8 +696,8 @@ public function pre_dispatch_verification( $result, $server, $request ) {
* @param WP_REST_Response|WP_Error $response Response to replace the requested version with. Can be anything
* a normal endpoint can return, or a WP_Error if replacing the
* response with an error.
* @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
* @param WP_REST_Request $request Request used to generate the response.
* @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
* @param WP_REST_Request $request Request used to generate the response.
*
* @return WP_REST_Response|WP_Error The response.
*/
Expand All @@ -713,7 +706,7 @@ public function modify_rest_api_params_before_validation( $response, $handler, $
return $response;
}

if ( $request->get_route() !== '/' . $this->get_events_route_namespace() . $this->get_endpoint_path() ) {
if ( $request->get_route() !== '/' . $this->get_events_route_namespace() . $this->get_endpoint_path() ) {
return $response;
}

Expand All @@ -724,6 +717,6 @@ public function modify_rest_api_params_before_validation( $response, $handler, $
$request->set_param( 'organizer', $organizer_array );
}

return $response;
return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ class Action_Endpoints {
*
* @since TBD Migrated to Common from Event Automator
*
* @param array<string,array> $endpoint An array of the Zapier endpoint details.
* @param Abstract_REST_Endpoint $this An instance of the endpoint.
* @param array<string,array> $endpoint An array of the Zapier endpoint details.
* @param Abstract_REST_Endpoint $endpoint_obj An instance of the endpoint.
*/
public function filter_details( $endpoint, $endpoint_obj ) {
if ( $endpoint_obj->get_id() !== 'create_events' ) {
return $endpoint;
if ( in_array( $endpoint_obj->get_id(), [ 'new_events', 'updated_events', 'canceled_events', 'create_events' ], true ) ) {
if ( ! class_exists( 'Tribe__Events__REST__V1__Validator__Base', false ) ) {
// Disable if Tribe__Events__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = [ 'tec' ];
}
} elseif ( in_array( $endpoint_obj->get_id(), [ 'attendees', 'updated_attendees', 'checkin', 'orders', 'refunded_orders' ], true ) ) {
if ( ! class_exists( 'Tribe__Tickets__REST__V1__Validator__Base', false ) ) {
// Disable if Tribe__Tickets__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = [ 'et' ];
}
}

if ( class_exists( 'Tribe__Events__REST__V1__Validator__Base', false ) ) {
return $endpoint;
}

// Disable if Tribe__Events__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = $endpoint_obj->get_dependents();

return $endpoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function admin_routes() {
* @since TBD Migrated to Common from Event Automator
*/
protected function add_actions() {
add_action( 'tribe_plugins_loaded', [ $this, 'register_admin_assets' ] );
add_action( 'admin_init', [ $this, 'register_admin_assets' ] );
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );

// Add endpoints to settings dashboard.
Expand Down
24 changes: 3 additions & 21 deletions src/Common/Event_Automator/Zapier/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace TEC\Event_Automator\Zapier;

use TEC\Event_Automator\Plugin;

/**
* Class Settings
*
Expand All @@ -26,31 +24,15 @@ class Assets {
* @since TBD Migrated to Common from Event Automator
*/
public function register_admin_assets() {
$admin_helpers = \Tribe__Admin__Helpers::instance();
$plugin = tribe( Plugin::class );

tribe_asset(
$plugin,
'tec-event-automator-css',
'tec-event-automator.css',
[ 'tribe-tooltip' ],
'admin_enqueue_scripts',
[
'conditionals' => [
[ $admin_helpers, 'is_screen' ],
],
]
);

tribe_asset(
$plugin,
\Tribe__Main::instance(),
'tec-event-automator-js',
'tec-event-automator.js',
[ 'jquery', 'tribe-dropdowns', 'tribe-clipboard', 'tribe-tooltip-js' ],
[ 'jquery' ],
'admin_enqueue_scripts',
[
'conditionals' => [
[ $admin_helpers, 'is_screen' ],
[ \Tribe__Admin__Helpers::instance(), 'is_screen' ],
],
'localize' => [
'name' => 'tec_automator',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace TEC\Event_Automator\Zapier\REST\V1\Utilities;

use Tribe__Events__REST__V1__Validator__Base;

/**
* Class Actions
*
Expand All @@ -25,22 +23,24 @@ class Action_Endpoints {
*
* @since TBD Migrated to Common from Event Automator
*
* @param array<string,array> $endpoint An array of the Zapier endpoint details.
* @param Abstract_REST_Endpoint $this An instance of the endpoint.
* @param array<string,array> $endpoint An array of the Zapier endpoint details.
* @param Abstract_REST_Endpoint $endpoint_obj An instance of the endpoint.
*/
public function filter_details( $endpoint, $endpoint_obj ) {
if ( $endpoint_obj->get_id() !== 'create_events' ) {
return $endpoint;
}

if ( class_exists( 'Tribe__Events__REST__V1__Validator__Base', false ) ) {
return $endpoint;
if ( in_array( $endpoint_obj->get_id(), [ 'new_events', 'updated_events', 'canceled_events', 'create_events' ], true ) ) {
if ( ! class_exists( 'Tribe__Events__REST__V1__Validator__Base', false ) ) {
// Disable if Tribe__Events__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = [ 'tec' ];
}
} elseif ( in_array( $endpoint_obj->get_id(), [ 'attendees', 'updated_attendees', 'checkin', 'orders', 'refunded_orders' ], true ) ) {
if ( ! class_exists( 'Tribe__Tickets__REST__V1__Validator__Base', false ) ) {
// Disable if Tribe__Tickets__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = [ 'et' ];
}
}

// Disable if Tribe__Events__REST__V1__Validator__Base class does not exist.
$endpoint['missing_dependency'] = true;
$endpoint['dependents'] = $endpoint_obj->get_dependents();

return $endpoint;
}
}
Loading

0 comments on commit e1ded06

Please sign in to comment.