Power_Automate_Provider
Class Power_Automate_Provider
Source
File: src/Common/Event_Automator/Power_Automate/Power_Automate_Provider.php
class Power_Automate_Provider extends Service_Provider {
use With_Nonce_Routes;
/**
* The constant to disable the event status coding.
*
* @since 1.4.0
*/
const DISABLED = 'TEC_POWER_AUTOMATE_DISABLED';
/**
* The constant to enable add to queue.
*
* @since 1.4.0
*/
const ENABLE_ADD_TO_QUEUE = 'TEC_POWER_AUTOMATE_ENABLE_ADD_TO_QUEUE';
/**
* Binds and sets up implementations.
*
* @since 1.4.0
*/
public function register() {
if ( ! self::is_active() ) {
return;
}
// Register the SP on the container
$this->container->singleton( 'tec.automator.power.automate.provider', $this );
$this->add_actions();
$this->add_filters();
$this->container->singleton( Api::class );
$this->container->singleton( Swagger_Documentation::class );
/**
* Allows filtering of the capability required to use the Power Automate integration ajax features.
*
* @since 1.4.0
*
* @param string $ajax_capability The capability required to use the ajax features, default manage_options.
*/
$ajax_capability = apply_filters( 'tec_event_automator_power_automate_admin_ajax_capability', 'manage_options' );
$this->route_admin_by_nonce( $this->admin_routes(), $ajax_capability );
}
/**
* Returns whether the event status should register, thus activate, or not.
*
* @since 1.4.0
*
* @return bool Whether the event status should register or not.
*/
public static function is_active() {
if ( defined( self::DISABLED ) && constant( self::DISABLED ) ) {
// The disable constant is defined and it's truthy.
return false;
}
if ( getenv( self::DISABLED ) ) {
// The disable env var is defined and it's truthy.
return false;
}
/**
* Allows filtering whether the event status should be activated or not.
*
* Note: this filter will only apply if the disable constant or env var
* are not set or are set to falsy values.
*
* @since 1.4.0
*
* @param bool $activate Defaults to `true`.
*/
return (bool) apply_filters( 'tec_event_automator_power_automate_enabled', true );
}
/**
* Provides the routes that should be used to handle Power Automate Integration requests.
*
* The map returned by this method will be used by the `TEC\Event_Automator\Traits\With_Nonce_Routes` trait.
*
* @since 1.4.0
*
* @return array<string,callable> A map from the nonce actions to the corresponding handlers.
*/
public function admin_routes() {
$actions = tribe( Actions::class );
return [
$actions::$add_connection => $this->container->callback( Api::class, 'ajax_add_connection' ),
$actions::$create_access => $this->container->callback( Api::class, 'ajax_create_connection_access' ),
$actions::$delete_connection => $this->container->callback( Api::class, 'ajax_delete_connection' ),
$actions::$clear_action => $this->container->callback( Endpoints_Manager::class, 'ajax_clear' ),
$actions::$disable_action => $this->container->callback( Endpoints_Manager::class, 'ajax_disable' ),
$actions::$enable_action => $this->container->callback( Endpoints_Manager::class, 'ajax_enable' ),
];
}
/**
* Adds the actions required for event status.
*
* @since 1.4.0
*/
protected function add_actions() {
add_action( 'tribe_plugins_loaded', [ $this, 'register_admin_assets' ] );
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
// Add endpoints to settings dashboard.
add_action( 'admin_init', [ $this, 'add_endpoints_to_dashboard' ] );
$this->setup_add_to_queues();
}
/**
* Adds the actions to add to the queues.
*
* @since 1.4.0
*/
protected function setup_add_to_queues() {
// New Events.
add_action( 'wp_insert_post', [ $this, 'add_to_queue' ], 10, 3 );
// Updated Events.
add_action( 'post_updated', [ $this, 'add_updated_to_queue' ], 10, 3 );
// Canceled Events.
add_action( 'tribe_events_event_status_update_post_meta', [ $this, 'add_canceled_to_queue' ], 10, 2 );
// Attendees.
add_action( 'event_tickets_rsvp_attendee_created', [ $this, 'add_rsvp_attendee_to_queue' ], 10, 4 );
add_action( 'tec_tickets_commerce_attendee_after_create', [ $this, 'add_tc_attendee_to_queue' ], 10, 4 );
add_action( 'event_ticket_edd_attendee_created', [ $this, 'add_edd_attendee_to_queue' ], 10, 4 );
add_action( 'event_ticket_woo_attendee_created', [ $this, 'add_woo_attendee_to_queue' ], 10, 4 );
//Updated Attendees
add_action( 'post_updated', [ $this, 'add_updated_attendee_to_queue' ], 10, 3 );
// Checkin.
add_action( 'rsvp_checkin', [ $this, 'add_checkin_to_queue' ], 10, 2 );
add_action( 'event_tickets_checkin', [ $this, 'add_checkin_to_queue' ], 10, 2 );
add_action( 'eddtickets_checkin', [ $this, 'add_checkin_to_queue' ], 10, 2 );
add_action( 'wootickets_checkin', [ $this, 'add_checkin_to_queue' ], 10, 2 );
// Ticket Orders.
add_action( 'tec_tickets_commerce_attendee_after_create', [ $this, 'add_tc_order_to_queue' ], 10, 4 );
add_action( 'event_tickets_edd_ticket_created', [ $this, 'add_edd_order_to_queue' ], 10, 4 );
add_action( 'event_tickets_woocommerce_ticket_created', [ $this, 'add_woo_order_to_queue' ], 10, 4 );
// Refunded Ticket Orders.
add_action( 'tec_tickets_commerce_order_status_refunded', [ $this, 'add_refunded_tc_order_to_queue' ], 10, 3 );
add_action( 'edd_refund_order', [ $this, 'add_refunded_edd_order_to_queue' ], 10, 3 );
add_action( 'woocommerce_order_status_changed', [ $this, 'add_refunded_woo_order_to_queue' ], 10, 4 );
}
/**
* Adds the filters required by Power Automate.
*
* @since 1.4.0
*/
protected function add_filters() {
add_filter( 'tribe_addons_tab_fields', [ $this, 'filter_tec_integrations_tab_fields' ] );
add_filter( 'tec_tickets_plus_integrations_tab_fields', [ $this, 'filter_et_integrations_tab_fields' ], 30 );
add_filter( 'tec_event_automator_power-automate_settings_fields', [ $this, 'add_dashboard_fields' ] );
add_filter( 'tec_event_automator_power-automate_endpoint_details', [ $this, 'filter_create_event_details' ], 10, 2 );
add_filter( 'tec_event_automator_power-automate_enable_add_to_queues', [ $this, 'filter_enable_add_to_queues' ], 10 );
add_filter( 'rest_pre_dispatch', [ $this, 'pre_dispatch_verification' ], 10, 3 );
add_filter( 'rest_request_before_callbacks', [ $this, 'modify_rest_api_params_before_validation' ], 1, 3 );
}
/**
* Register the Admin Assets for Power Automate.
*
* @since 1.4.0
*/
public function register_admin_assets() {
$this->container->make( Assets::class )->register_admin_assets();
}
/**
* Registers the REST API endpoints.
*
* @since 1.4.0
*/
public function register_endpoints() {
$this->container->make( Swagger_Documentation::class )->register();
$this->container->make( New_Events::class )->register();
$this->container->make( Updated_Events::class )->register();
$this->container->make( Canceled_Events::class )->register();
$this->container->make( Attendees::class )->register();
$this->container->make( Updated_Attendees::class )->register();
$this->container->make( Checkin::class )->register();
$this->container->make( Orders::class )->register();
$this->container->make( Refunded_Orders::class )->register();
$this->container->make( Create_Events::class )->register();
}
/**
* Adds the endpoint to the endpoint dashboard filter.
*
* @since 1.4.0
*/
public function add_endpoints_to_dashboard() {
$this->container->make( New_Events::class )->add_to_dashboard();
$this->container->make( Updated_Events::class )->add_to_dashboard();
$this->container->make( Canceled_Events::class )->add_to_dashboard();
$this->container->make( Attendees::class )->add_to_dashboard();
$this->container->make( Updated_Attendees::class )->add_to_dashboard();
$this->container->make( Checkin::class )->add_to_dashboard();
$this->container->make( Orders::class )->add_to_dashboard();
$this->container->make( Refunded_Orders::class )->add_to_dashboard();
$this->container->make( Create_Events::class )->add_to_dashboard();
}
/**
* Filters the fields in the Events > Settings > Integrations tab to Power Automate settings.
*
* @since 1.4.0
*
* @param array<string,array> $fields The current fields.
*
* @return array<string,array> The fields, as updated by the settings.
*/
public function filter_tec_integrations_tab_fields( $fields ) {
if ( ! is_array( $fields ) ) {
return $fields;
}
return tribe( Settings::class )->add_fields_tec( $fields );
}
/**
* Filters the fields in the Tickets > Settings > Integrations tab to Power Automate settings.
*
* @since 1.4.0
*
* @param array<string,array> $fields The current fields.
*
* @return array<string,array> The fields, as updated by the settings.
*/
public function filter_et_integrations_tab_fields( $fields ) {
if ( ! is_array( $fields ) ) {
return $fields;
}
return tribe( Settings::class )->add_fields_et( $fields );
}
/**
* Adds the Endpoint dashboard fields after the connection settings.
*
* @since 1.4.0
*
* @param array<string,array> $fields The current fields.
*
* @return array<string,array> The fields, with the added endpoint dashboard fields.
*/
public function add_dashboard_fields( $fields ) {
if ( ! is_array( $fields ) ) {
return $fields;
}
return tribe( Dashboard::class )->add_fields( $fields );
}
/**
* Filters the Power Automate endpoint details.
*
* @since 1.4.0
*
* @param array<string,array> $endpoint An array of the Power Automate endpoint details.
* @param Abstract_REST_Endpoint $endpoint_obj An instance of the endpoint.
*/
public function filter_create_event_details( $endpoint, $endpoint_obj ) {
return tribe( Action_Endpoints_Utilities::class )->filter_details( $endpoint, $endpoint_obj );
}
/**
* Filter to enable adding to the queues for Power Automate.
*
* @since 1.4.0
*
* @param boolean $enable_add_to_queue Whether to enable adding to the queues for Power Automate, default to false.
*
* @return boolean Whether adding to the queues is enabled Power Automate.
*/
public function filter_enable_add_to_queues( $enable_add_to_queue ) {
if ( defined( self::ENABLE_ADD_TO_QUEUE ) && constant( self::ENABLE_ADD_TO_QUEUE ) ) {
return true;
}
if ( getenv( self::ENABLE_ADD_TO_QUEUE ) ) {
// The enable env var is defined and it's truthy.
return true;
}
// Setup queues if there is a connection setup.
$access_keys = get_option( 'tec_power_automate_connections' );
if ( ! empty( $access_keys ) ) {
$enable_add_to_queue = true;
}
/**
* Allows filtering whether to add items to Power Automate queues.
*
* Note: this filter will only apply if the enable constant or env var
* are not set or are set to true values.
*
* @since 1.4.0
*
* @param bool $enable_add_to_queue Defaults to `false`.
* @param array<string|mixed> $access_keys An array of the Zapier access keys.
*/
return (bool) apply_filters( 'tec_event_automator_power_automate_enable_add_to_queue', $enable_add_to_queue, $access_keys );
}
/**
* Verify token and login user before dispatching the request.
* Done on `rest_pre_dispatch` to be able to set current user to pass validation capability checks.
*
* @since 1.4.0
*
* @param mixed $result Response to replace the requested version with. Can be anything
* a normal endpoint can return, or null to not hijack the request.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
*
* @return null With always return null, failure will happen on the can_create permission check.
*/
public function pre_dispatch_verification( $result, $server, $request ) {
return $this->container->make( Create_Events::class )->pre_dispatch_verification( $result, $server, $request );
}
/**
* Modifies REST API comma seperated parameters before validation.
*
* @since 1.4.0
*
* @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.
*
* @return WP_REST_Response|WP_Error The response.
*/
public function modify_rest_api_params_before_validation( $result, $server, $request ) {
return $this->container->make( Create_Events::class )->modify_rest_api_params_before_validation( $result, $server, $request );
}
/**
* Add a custom post id to a trigger queue.
*
* @since 1.4.0
*
* @param int $post_id A WordPress custom post id.
* @param WP_Post $post A WordPress custom post object.
* @param boolean $update Whether this is an update to a custom post or new. Unreliable and not used.
*/
public function add_to_queue( $post_id, $post, $update ) {
// TEC is not available return to prevent errors.
if ( ! class_exists('Tribe__Events__Main', false ) ) {
return;
}
$data = [
'post' => $post,
'update' => $update,
];
$this->container->make( New_Events::class )->add_to_queue( $post_id, $data );
}
/**
* Add a custom post id of an event that has been updated to a trigger queue.
*
* @since 1.4.0
*
* @param int $post_id A WordPress custom post id.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
public function add_updated_to_queue( $post_id, $post_after, $post_before ) {
// TEC is not available return to prevent errors.
if ( ! class_exists('Tribe__Events__Main', false ) ) {
return;
}
$data = [
'post' => $post_after,
'post_before' => $post_before,
];
$this->container->make( Updated_Events::class )->add_to_queue( $post_id, $data );
}
/**
* Add RSVP attendee to queue.
*
* @since 1.4.0
*
* @param integer $attendee_id An attendee id.
* @param integer $post_id A WordPress custom post id.
* @param integer $product_id A WordPress custom post object.
* @param integer $order_attendee_id Whether this is an update to a custom post or new. Unreliable and not used.
*/
public function add_rsvp_attendee_to_queue( $attendee_id, $post_id, $product_id, $order_attendee_id ) {
$data = [
'post_id' => $post_id,
'product_id' => $product_id,
'order_attendee_id' => $order_attendee_id,
];
$this->container->make( Attendees::class )->add_to_queue( $attendee_id, $data );
}
/**
* Add Tickets Commerce attendee to queue.
*
* @since 1.4.0
*
* @param WP_Post $attendee Post object for the attendee.
* @param WP_Post $order Which order generated this attendee.
* @param Ticket_Object $ticket Which ticket generated this Attendee.
* @param array $args Set of extra arguments used to populate the data for the attendee.
*/
public function add_tc_attendee_to_queue( $attendee, $order, $ticket, $args ) {
$data = [
'attendee' => $attendee,
'order' => $order,
'ticket' => $ticket,
'attendee_args' => $args,
];
$this->container->make( Attendees::class )->add_to_queue( $attendee->ID, $data );
}
/**
* Add EDD attendee to queue.
*
* @param int $attendee_id ID of attendee ticket.
* @param int $post_id ID of event.
* @param int $order_id Easy Digital Downloads order ID.
* @param int $product_id Easy Digital Downloads product ID.
*/
public function add_edd_attendee_to_queue( $attendee_id, $post_id, $order_id, $product_id ) {
$data = [
'post_id' => $post_id,
'product_id' => $product_id,
'order_id' => $order_id,
];
$this->container->make( Attendees::class )->add_to_queue( $attendee_id, $data );
}
/**
* Add Woo attendee to queue.
*
* @param int $attendee_id ID of attendee ticket.
* @param int $post_id ID of event.
* @param WC_Order $order WooCommerce order.
* @param int $product_id WooCommerce product ID.
*/
public function add_woo_attendee_to_queue( $attendee_id, $post_id, $order, $product_id ) {
$data = [
'post_id' => $post_id,
'order' => $order,
'product_id' => $product_id,
];
$this->container->make( Attendees::class )->add_to_queue( $attendee_id, $data );
}
/**
* Add a custom post id of an attendee that has been updated to a trigger queue.
*
* @since 1.4.0
*
* @param int $post_id A WordPress custom post id.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
public function add_updated_attendee_to_queue( $post_id, $post_after, $post_before ) {
// ET is not available return to prevent errors.
if ( ! class_exists('Tribe__Tickets__Main', false ) ) {
return;
}
$data = [
'post' => $post_after,
'post_before' => $post_before,
];
$this->container->make( Updated_Attendees::class )->add_to_queue( $post_id, $data );
}
/**
* Add a canceled event post id to a trigger queue.
*
* @since 1.4.0
*
* @param int $post_id ID of the post we're saving.
* @param array<string> $data The meta data we're trying to save.
*/
public function add_canceled_to_queue( $post_id, $data ) {
$this->container->make( Canceled_Events::class )->add_to_queue( $post_id, $data );
}
/**
* Add checkin to the queue.
*
* @since 1.4.0
*
* @param int $attendee_id ID of attendee ticket.
* @param bool|null $is_qr_checkin True if from QR checkin process.
*/
public function add_checkin_to_queue( $attendee_id, $is_qr_checkin ) {
$data = [
'is_qr' => boolval( $is_qr_checkin ),
];
$this->container->make( Checkin::class )->add_to_queue( $attendee_id, $data );
}
/**
* Add Tickets Commerce order to queue.
*
* @since 1.4.0
*
* @param WP_Post $attendee Post object for the attendee.
* @param WP_Post $order Which order generated this attendee.
* @param Ticket_Object $ticket Which ticket generated this Attendee.
* @param array $args Set of extra arguments used to populate the data for the attendee.
*/
public function add_tc_order_to_queue( $attendee, $order, $ticket, $args ) {
$data = [
'provider' => tribe_tickets_get_ticket_provider( $attendee->ID ),
'order' => $order,
'ticket' => $ticket,
'attendee_args' => $args,
];
$this->container->make( Orders::class )->add_to_queue( $order->ID, $data );
}
/**
* Add EDD order to queue.
*
* @since 1.4.0
*
* @param int $attendee_id ID of attendee ticket.
* @param int $order_id Easy Digital Downloads order ID.
* @param int $product_id Easy Digital Downloads product ID.
* @param int $order_attendee_id Attendee # for order.
*/
public function add_edd_order_to_queue( $attendee_id, $order_id, $product_id, $order_attendee_id ) {
$data = [
'provider' => tribe_tickets_get_ticket_provider( $attendee_id ),
'attendee_id' => $attendee_id,
'product_id' => $product_id,
'order_attendee_id' => $order_attendee_id,
];
$this->container->make( Orders::class )->add_to_queue( $order_id, $data );
}
/**
* Add Woo order to queue.
*
* @since 1.4.0
*
* @param int $attendee_id ID of attendee ticket.
* @param int $order_id WooCommerce order ID.
* @param int $product_id WooCommerce product ID.
* @param int $order_attendee_id Attendee # for order.
*/
public function add_woo_order_to_queue( $attendee_id, $order_id, $product_id, $order_attendee_id ) {
$data = [
'provider' => tribe_tickets_get_ticket_provider( $attendee_id ),
'attendee_id' => $attendee_id,
'product_id' => $product_id,
'order_attendee_id' => $order_attendee_id,
];
$this->container->make( Orders::class )->add_to_queue( $order_id, $data );
}
/**
* Add Refunded Tickets Commerce order to queue.
*
* @since 1.4.0
*
* @param Status_Interface $new_status New post status.
* @param Status_Interface|null $old_status Old post status.
* @param \WP_Post $order Order Post object.
*/
public function add_refunded_tc_order_to_queue( $new_status, $old_status, $order ) {
$data = [
'provider' => tribe_tickets_get_ticket_provider( $order->ID ),
'order_id' => $order->ID,
'order' => $order,
'old_status' => $old_status,
'new_status' => $new_status,
];
$this->container->make( Refunded_Orders::class )->add_to_queue( $order->ID, $data );
}
/**
* Add Refunded EDD order to queue.
*
* @since 1.4.0
*
* @param int $order_id The ID number of the order.
* @param int $refund_id The ID number of the refund order.
* @param bool $all_refunded The status of the order prior to this change.
*/
public function add_refunded_edd_order_to_queue( $order_id, $refund_id, $all_refunded ) {
// EDD does not get the provider by id for EDD 3.0.0
/** @var \Tribe__Tickets_Plus__Commerce__EDD__Main $commerce_edd */
$provider = tribe( 'tickets-plus.commerce.edd' );
$payment = new EDD_Payment( $order_id );
$new_status = $payment->__get( 'status' );
$data = [
'provider' => $provider,
'order_id' => $order_id,
'new_status' => $new_status,
];
$this->container->make( Refunded_Orders::class )->add_to_queue( $order_id, $data );
}
/**
* Add Refunded Woo order to queue.
*
* @since 1.4.0
*
* @param int $order_id WooCommerce order ID.
* @param string $old_status The status of the order prior to this change.
* @param string $new_status The new order status.
* @param WC_Order $order The instance of the order object.
*/
public function add_refunded_woo_order_to_queue( $order_id, $old_status, $new_status, $order ) {
$data = [
'provider' => tribe_tickets_get_ticket_provider( $order_id ),
'order_id' => $order_id,
'old_status' => $old_status,
'new_status' => $new_status,
];
$this->container->make( Refunded_Orders::class )->add_to_queue( $order_id, $data );
}
}
Changelog
| Version | Description |
|---|---|
| 6.0.0 | Introduced. |
Methods
- add_canceled_to_queue — Add a canceled event post id to a trigger queue.
- add_checkin_to_queue — Add checkin to the queue.
- add_dashboard_fields — Adds the Endpoint dashboard fields after the connection settings.
- add_edd_attendee_to_queue — Add EDD attendee to queue.
- add_edd_order_to_queue — Add EDD order to queue.
- add_endpoints_to_dashboard — Adds the endpoint to the endpoint dashboard filter.
- add_refunded_edd_order_to_queue — Add Refunded EDD order to queue.
- add_refunded_tc_order_to_queue — Add Refunded Tickets Commerce order to queue.
- add_refunded_woo_order_to_queue — Add Refunded Woo order to queue.
- add_rsvp_attendee_to_queue — Add RSVP attendee to queue.
- add_tc_attendee_to_queue — Add Tickets Commerce attendee to queue.
- add_tc_order_to_queue — Add Tickets Commerce order to queue.
- add_to_queue — Add a custom post id to a trigger queue.
- add_updated_attendee_to_queue — Add a custom post id of an attendee that has been updated to a trigger queue.
- add_updated_to_queue — Add a custom post id of an event that has been updated to a trigger queue.
- add_woo_attendee_to_queue — Add Woo attendee to queue.
- add_woo_order_to_queue — Add Woo order to queue.
- admin_routes — Provides the routes that should be used to handle Power Automate Integration requests.
- filter_create_event_details — Filters the Power Automate endpoint details.
- filter_enable_add_to_queues — Filter to enable adding to the queues for Power Automate.
- filter_et_integrations_tab_fields — Filters the fields in the Tickets > Settings > Integrations tab to Power Automate settings.
- filter_tec_integrations_tab_fields — Filters the fields in the Events > Settings > Integrations tab to Power Automate settings.
- is_active — Returns whether the event status should register, thus activate, or not.
- modify_rest_api_params_before_validation — Modifies REST API comma seperated parameters before validation.
- pre_dispatch_verification — Verify token and login user before dispatching the request.
- register — Binds and sets up implementations.
- register_admin_assets — Register the Admin Assets for Power Automate.
- register_endpoints — Registers the REST API endpoints.
- setup_add_to_queues — Adds the actions to add to the queues.