Zapier_Provider

Class Zapier_Provider


Source

File: src/Common/Event_Automator/Zapier/Zapier_Provider.php

class Zapier_Provider extends \tad_DI52_ServiceProvider {

	use With_Nonce_Routes;

	/**
	 * The constant to disable the event status coding.
	 *
	 * @since 1.0.0
	 */
	const DISABLED = 'TEC_ZAPIER_DISABLED';

	/**
	 * Binds and sets up implementations.
	 *
	 * @since 1.0.0
	 */
	public function register() {
		if ( ! self::is_active() ) {
			return;
		}

		// Register the SP on the container
		$this->container->singleton( 'tec.automator.zapier.provider', $this );

		$this->add_actions();
		$this->add_filters();

		$this->container->singleton( Authorize::class );
		$this->container->singleton( Swagger_Documentation::class, Swagger_Documentation::class );

		/**
		 * Allows filtering of the capability required to use the Zapier integration ajax features.
		 *
		 * @since 1.0.0
		 *
		 * @param string $ajax_capability The capability required to use the ajax features, default manage_options.
		 */
		$ajax_capability = apply_filters( 'tec_event_automator_zapier_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.0.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.0.0
		 *
		 * @param bool $activate Defaults to `true`.
		 */
		return (bool) apply_filters( 'tec_event_automator_zapier_enabled', true );
	}

	/**
	 * Adds the actions required for event status.
	 *
	 * @since 1.0.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' ] );

		// Canceled Events.
		add_action( 'tribe_events_event_status_update_post_meta', [ $this, 'add_canceled_to_queue' ], 10, 2 );
		// 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 );

		// 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 );

		// 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 );

		// 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 );

		//Updated Attendees
		add_action( 'post_updated', [ $this, 'add_updated_attendee_to_queue' ], 10, 3 );
	}

	/**
	 * Adds the filters required by Zapier.
	 *
	 * @since 1.0.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_zapier_settings_fields', [ $this, 'add_dashboard_fields' ] );
		add_filter( 'tec_event_automator_zapier_endpoint_details', [ $this, 'filter_create_event_details' ], 10, 2 );
	}

	/**
	 * Register the Admin Assets for Zapier.
	 *
	 * @since 1.0.0
	 */
	public function register_admin_assets() {
		$this->container->make( Assets::class )->register_admin_assets();
	}

	/**
	 * Registers the REST API endpoints for Zapier
	 *
	 * @since 1.0.0
	 */
	public function register_endpoints() {
		$this->container->make( Swagger_Documentation::class )->register();
		$this->container->make( Authorize::class )->register();
		$this->container->make( Canceled_Events::class )->register();
		$this->container->make( New_Events::class )->register();
		$this->container->make( Updated_Events::class )->register();
		$this->container->make( Attendees::class )->register();
		$this->container->make( Updated_Attendees::class )->register();
		$this->container->make( Orders::class )->register();
		$this->container->make( Refunded_Orders::class )->register();
		$this->container->make( Checkin::class )->register();

		$this->container->make( Create_Events::class )->register();
	}

	/**
	 * Adds the endpoint to the Zapier endpoint dashboard filter.
	 *
	 * @since TBD
	 */
	public function add_endpoints_to_dashboard() {
		$this->container->make( Authorize::class )->add_to_dashboard();
		$this->container->make( New_Events::class )->add_to_dashboard();
		$this->container->make( Canceled_Events::class )->add_to_dashboard();
		$this->container->make( Updated_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 Zapier settings.
	 *
	 * @since 1.0.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 Zapier settings.
	 *
	 * @since 1.0.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 Zapier Endpoint dashboard fields after the Zapier API key settings.
	 *
	 * @since TBD
	 *
	 * @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 Zapier endpoint details.
	 *
	 * @since TBD
	 *
	 * @param array<string,array>    $endpoint An array of the Zapier endpoint details.
	 * @param Abstract_REST_Endpoint $this     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 );
	}

	/**
	 * Provides the routes that should be used to handle Zapier Integration requests.
	 *
	 * The map returned by this method will be used by the `TEC\Event_Automator\Traits\With_Nonce_Routes` trait.
	 *
	 * @since 1.0.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_aki_key_action => $this->container->callback( Api::class, 'ajax_add_api_key' ),
			$actions::$generate_action    => $this->container->callback( Api::class, 'ajax_generate_api_key_pair' ),
			$actions::$revoke_action      => $this->container->callback( Api::class, 'ajax_revoke' ),
			$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' ),
		];
	}

	/**
	 * Add a canceled event post id to a trigger queue.
	 *
	 * @since 1.2.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 a custom post id to a trigger queue.
	 *
	 * @since 1.0.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' ) ) {
			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.2.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' ) ) {
			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.0.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.0.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 Tickets Commerce order to queue.
	 *
	 * @since 1.0.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.0.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.0.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 checkin to the queue.
	 *
	 * @since 1.0.0
	 *
	 * @param int       $attendee_id ID of attendee ticket.
	 * @param bool|null $qr          true if from QR checkin process
	 */
	public function add_checkin_to_queue( $attendee_id, $qr ) {
		$data = [
			'qr' => $qr,
		];

		$this->container->make( Checkin::class )->add_to_queue( $attendee_id, $data );
	}

	/**
	 * Add a custom post id of an attendee that has been updated to a trigger queue.
	 *
	 * @since TBD
	 *
	 * @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' ) ) {
			return;
		}

		$data = [
			'post'        => $post_after,
			'post_before' => $post_before,
		];

		$this->container->make( Updated_Attendees::class )->add_to_queue( $post_id, $data );
	}

	/**
	 * Add Refunded Tickets Commerce order to queue.
	 *
	 * @since TBD
	 *
	 * @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 TBD
	 *
	 * @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 TBD
	 *
	 * @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 );
	}

	/**
	 * Renders the GDPR/CCPA privacy notice.
	 *
	 * @since 1.0.0
	 */
	public function render_privacy_notice() {
		$this->container->make( Privacy_Notice::class )->render();
	}
}

Top ↑

Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

Methods