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

Registers a new closure and creates a type if defined and the type does not exist


Parameters Parameters

$closure

(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 closure( $closure = null, $integration_code = null, $integration = null ) {

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

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

		/**
		 * Use this hook to modify the uap closures before it it error checked and registered
		 */
		$closure = apply_filters_deprecated(
			'uap_register_closure',
			array( $closure, $integration_code, $integration ),
			'3.0',
			'automator_register_closure'
		);
		$closure = apply_filters( 'automator_register_closure', $closure, $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_closure_integration_code',
			array( $integration_code, $closure, $integration ),
			'3.0',
			'automator_register_closure_integration_code'
		);
		$integration_code = apply_filters( 'automator_register_closure_integration_code', $integration_code, $closure, $integration );

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

		// Integration was passed in, lets try to register it
		if ( null !== $integration_code ) {
			if ( ! is_string( $integration_code ) ) {
				throw new Automator_Exception( 'You are trying to register a closure without passing an proper integration code.', 1004 );
			}
			if ( null === $integration && is_array( $integration ) ) {
				throw new Automator_Exception( 'You are trying to register a closure without passing an proper integration object.', 1004 );
			}
			// 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( $closure['integration'] ) ) {
			throw new Automator_Exception( 'You are trying to register a closure without setting its closure_integration.', 1004 );
		}

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

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

		// Sanity check that trigger_validation_function isset
		if ( ! isset( $closure['execution_function'] ) ) {
			throw new Automator_Exception( 'You are trying to register a closure without setting its closure_execution_function.', 1004 );
		}

		// Loop through existing to force only unique values for closure_code and closure_name
		foreach ( Automator()->get_closures() as $existing_closure ) {

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

		Automator()->set_closures( Automator()->utilities->keep_order_of_options( $closure ) );

		return true;
	}