Automator_Registration::trigger( null $trigger = null, null|string $integration_code = null, null $integration = null )

Register a new trigger and creates a type if defined and the type does not exist


Parameters Parameters

$trigger

(null) (Optional)

Default value: null

$integration_code

(null|string) (Optional)

Default value: null

$integration

(null) (Optional)

Default value: null


Top ↑

Return Return

(null)


Source Source

File: src/core/lib/utilities/class-automator-registration.php

	public function trigger( $trigger = null, $integration_code = null, $integration = null ) {

		// Sanity check that there was a trigger passed
		if ( null === $trigger || ! is_array( $trigger ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without passing a trigger object.', 1001 );
		}

		/**
		 * Use this hook the stop specific triggers from being registered by returning true
		 */
		$skip_trigger_registration = false;
		$skip_trigger_registration = apply_filters_deprecated(
			'skip_trigger_registration',
			array(
				$skip_trigger_registration,
				$trigger,
				$integration_code,
				$integration,
			),
			'3.0',
			'automator_skip_trigger_registration'
		);
		$skip_trigger_registration = apply_filters( 'automator_skip_trigger_registration', $skip_trigger_registration, $trigger, $integration_code, $integration );
		if ( true === $skip_trigger_registration ) {
			return null;
		}

		/**
		 * Use this hook the override specific triggers type, i.e., utility or user
		 */
		if ( ! key_exists( 'type', $trigger ) ) {
			$trigger_type    = 'user';
			$trigger_type    = apply_filters_deprecated(
				'uap_trigger_type',
				array(
					$trigger_type,
					$trigger,
					$integration_code,
					$integration,
				),
				'3.0',
				'automator_trigger_type'
			);
			$trigger_type    = apply_filters( 'automator_trigger_type', $trigger_type, $trigger, $integration_code, $integration );
			$trigger['type'] = $trigger_type;
		}

		/**
		 * Use this hook to modify the trigger before it it error checked and registered
		 */
		$trigger = apply_filters_deprecated(
			'uap_register_trigger',
			array( $trigger, $integration_code, $integration ),
			'3.0',
			'automator_register_trigger'
		);
		$trigger = apply_filters( 'automator_register_trigger', $trigger, $integration_code, $integration );

		/**
		 * Use this hook to modify the integration_code before it is error checked and registered
		 */
		$integration_code = apply_filters_deprecated(
			'uap_register_trigger_integration_code',
			array( $integration_code, $trigger, $integration ),
			'3.0',
			'automator_register_trigger_integration_code'
		);
		$integration_code = apply_filters( 'automator_register_trigger_integration_code', $integration_code, $trigger, $integration );

		/**
		 * Use this hook to modify the integration_code before it is error checked and registered
		 */
		$integration = apply_filters_deprecated(
			'uap_register_trigger_integration',
			array( $integration, $trigger, $integration_code ),
			'3.0',
			'automator_register_trigger_integration'
		);
		$integration = apply_filters( 'automator_register_trigger_integration', $integration, $trigger, $integration_code );

		// Integration was passed in, lets try to register it
		if ( null !== $integration_code ) {
			if ( ! is_string( $integration_code ) || null === $integration || is_array( $integration ) ) {
				throw new Automator_Exception( 'You are trying to register a trigger without passing integration code.', 1001 );
			}

			// Sanity check that the integration code does not exist already
			if ( ! key_exists( $integration_code, Automator()->get_integrations() ) ) {
				Automator()->register->integration( $integration_code, $integration );
			}
		}

		// Sanity check that trigger_integration isset
		if ( ! isset( $trigger['integration'] ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without setting its trigger_integration', 1001 );
		}

		// Sanity check that the trigger has a integration that is defined
		if ( ! key_exists( $trigger['integration'], Automator()->get_integrations() ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger to an integration that does not exist.', 1001 );
		}

		// Sanity check that trigger_code isset
		if ( ! isset( $trigger['code'] ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without setting its trigger_code', 1001 );
		}

		// Sanity check that trigger_name isset
		if ( ! isset( $trigger['select_option_name'] ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without setting its trigger_name', 1001 );

		}

		// Sanity check that trigger_action isset
		if ( ! isset( $trigger['action'] ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without setting its trigger_action', 1001 );
		}

		// Sanity check that trigger_validation_function isset
		if ( ! isset( $trigger['validation_function'] ) ) {
			throw new Automator_Exception( 'You are trying to register a trigger without setting its trigger_validation_function', 1001 );
		}

		// Loop through existing to force only unique values for trigger_code and trigger_name
		foreach ( Automator()->get_triggers() as $existing_trigger ) {

			// Sanity check that trigger_code is unique
			if ( $existing_trigger['code'] === $trigger['code'] ) {
				// Already exists. Bail.
				return null;
			}
		}

		// Register the trigger into the system
		Automator()->set_triggers( Automator()->utilities->keep_order_of_options( $trigger ) );

		return true;
	}