Automator_Recipe_Process_Complete::recipe( null $recipe_id = null, null $user_id = null, null $recipe_log_id = null, array $args = array() )

Complete a recipe


Parameters Parameters

$recipe_id

(null) (Optional)

Default value: null

$user_id

(null) (Optional)

Default value: null

$recipe_log_id

(null) (Optional)

Default value: null

$args

(array) (Optional)

Default value: array()


Top ↑

Return Return

(null|true)


Source Source

File: src/core/lib/process/class-automator-recipe-process-complete.php

	public function recipe( $recipe_id = null, $user_id = null, $recipe_log_id = null, $args = array() ) {

		/**
		 * @var $completed
		 * Meaning of each number
		 *
		 * 0 = not completed
		 * 1 = completed
		 * 2 = completed with errors, error message provided
		 * 5 = in progress (some actions are scheduled)
		 * 9 = completed, do nothing
		 *
		 */

		$run_number = Automator()->get->next_run_number( $recipe_id, $user_id, true );
		if ( $recipe_log_id && Automator()->db->recipe->get_scheduled_actions_count( $recipe_log_id, $args ) > 0 ) {
			$completed = 5;
		} elseif ( ( is_array( $args ) && key_exists( 'do-nothing', $args ) ) ) {
			$completed  = 9;
			$run_number = 1;
		} else {
			$completed = 1;
		}

		do_action_deprecated(
			'uap_before_recipe_completed',
			array(
				$recipe_id,
				$user_id,
				$recipe_log_id,
				$args,
			),
			'3.0',
			'automator_before_recipe_completed'
		);
		do_action( 'automator_before_recipe_completed', $recipe_id, $user_id, $recipe_log_id, $args );

		if ( null === $recipe_log_id ) {
			if ( null === $recipe_id || ! is_numeric( $recipe_id ) ) {
				Automator()->error->add_error( 'complete_recipe', 'ERROR: You are trying to completed a recipe without providing a recipe_id', $this );

				return null;
			}

			$recipe_log_id = Automator()->db->recipe->add( $user_id, $recipe_id, $completed, $run_number );
		} else {
			Automator()->db->recipe->mark_complete( $recipe_log_id, $completed );
		}

		// If actions error occurred, change the recipe status to 2
		$maybe_error = Automator()->db->action->get_error_message( $recipe_log_id );

		if ( ! empty( $maybe_error ) ) {
			$skip     = false;
			$message  = $maybe_error->error_message;
			$complete = $maybe_error->completed;

			if ( strpos( $message, 'Existing user found matching' ) || strpos( $message, 'User not found matching' ) || strpos( $message, 'User found matching' ) ) {
				$skip = true;
			} elseif ( strpos( $message, 'New user created' ) || strpos( $message, 'Create new user failed' ) ) {
				$skip = true;
			} elseif ( 9 === (int) $complete ) {
				$skip = true;
			}

			if ( ! $skip ) {
				$comp = 9 === absint( $completed ) ? 9 : $complete;
				do_action( 'automator_recipe_completed_with_errors', $recipe_id, $user_id, $recipe_log_id, $args );
				Automator()->db->recipe->mark_complete_with_error( $recipe_id, $recipe_log_id, $comp );
			}
		}

		do_action_deprecated(
			'uap_recipe_completed',
			array(
				$recipe_id,
				$user_id,
				$recipe_log_id,
				$args,
			),
			'3.0',
			'automator_recipe_completed'
		);
		do_action( 'automator_recipe_completed', $recipe_id, $user_id, $recipe_log_id, $args );

		return true;
	}