Triggers::validate( mixed $args )

This function will run for each trigger that is using Trait Triggers; Most of the heavy-lifting > 3.0 will be handled by core instead of each individual trigger. We are still going to allow developers to manipulate values or override in a trigger if required.


Parameters Parameters

$args

(mixed) (Required)


Top ↑

Return Return

(bool)


Source Source

File: src/core/lib/recipe-parts/triggers/trait-triggers.php

		 * By default, ...$args contains all the arguments in array. If a developer wants to manipulate the arguments
		 * array to add assign values as key=>value pair, they can do it here.
		 */
		$args = $this->do_action_args( $args );

		/**
		 * Grab user_id using WordPress function.
		 */
		$this->set_user_id( wp_get_current_user()->ID );
		/**
		 * Set conditional triggers to false. Can be overwritten in prepare to run.
		 */
		$this->set_conditional_trigger( false );

		/**
		 * Check if user is logged in.
		 */
		if ( ! is_user_logged_in() ) {
			/**
			 * Allow developers to override and return true to continue running trigger.
			 * use $this->set_is_user_logged_in_required( true|false );
			 */
			if ( true === $this->is_user_logged_in_required( $args ) ) {
				return false;
			}
		}

		if ( is_user_logged_in() ) {
			/**
			 * If this is an anonymous trigger and user is logged in, should it continue running trigger?
			 */
			if ( $this->get_is_anonymous() ) {
				/**
				 * Allow developers to override and return true to continue running trigger.
				 */
				if ( ! $this->do_continue_anon_trigger( $args ) ) {
					return false;
				}
			}
		}

		/**
		 * Should the trigger continue executing? This condition has to be satisfied from within each individual trigger.
		 * Each trigger has its own requirements, i.e., is_page(), $order instanceof WC_Order etc.
		 */
		if ( ! $this->validate_trigger( $args ) ) {
			return false;
		}

		/**
		 * This part of the code prepares the trigger and we can set different settings here. For example, most of Automator
		 * trigger passes `post_id` => X to be validated by the core, this is the place the can be used to set values.
		 * This also allows developer to set "Conditional" trigger option to true where certain conditions has to be met
		 * before a trigger could run. For example, if order contains X product, or user completed X course etc.
		 */
		$this->prepare_to_run( $args );

		/**
		 * In-depth validation of the trigger. Filter recipes based on multiple trigger codes & conditions. By default,
		 * trigger condition is true. Set it to false if required for the trigger.
		 */
		if ( $this->is_conditional_trigger() ) {
			/*
			 * Resetting values of match conditions.
			 */
			$this->find_in   = array();
			$this->find_this = array();

			/**
			 * Filters recipes even further based on the conditions. Multiple conditions can be passed to the function.
			 * See sample integration.
			 */
			$this->trigger_conditions( $args );

			/**
			 * Return us filtered recipe ids after validating trigger conditions.
			 */
			$matched_recipe_ids = $this->validate_conditions( $args );

			/**
			 * Trigger failed to satisfy conditions. bailing...
			 */
			if ( empty( $matched_recipe_ids ) ) {
				return false;
			}

			// Set ignore_post_id to true so that Automator can match passed trigger and recipe IDs.
			$this->set_ignore_post_id( true );

			foreach ( $matched_recipe_ids as $recipe_id => $trigger_id ) {
				$this->set_recipe_to_match( $recipe_id );
				$this->set_trigger_to_match( $trigger_id );

				/*
				 * Process each matched recipe.
				 */
				$this->process_trigger( $args );
			}

			return true;
		}

		// Non-conditional triggers. Complete trigger once.
		$this->process_trigger( $args );