UOA_RECIPECOMPLETED_STATUS::on_completion( $recipe_id,  $user_id,  $recipe_log_id,  $args )

Validation function when the trigger action is hit


Parameters Parameters

$recipe_id

(Required)

$user_id

(Required)

$recipe_log_id

(Required)

$args

(Required)


Source Source

File: src/integrations/uncanny-automator/triggers/uoa-recipecompleted-status.php

	public function on_completion( $recipe_id, $user_id, $recipe_log_id, $args ) {
		$recipes              = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_recipe      = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$required_status      = Automator()->get->meta_from_recipes( $recipes, 'RECIPESTATUS' );
		$required_status_text = Automator()->get->meta_from_recipes( $recipes, 'RECIPESTATUS_readable' );
		$matched_recipe_ids   = array();

		global $wpdb;
		// get recipe actions
		$table_name    = $wpdb->prefix . 'uap_recipe_log';
		$query         = "SELECT `completed` FROM $table_name WHERE ID = $recipe_log_id AND automator_recipe_id = $recipe_id";
		$recipe_status = $wpdb->get_var( $query );

		if ( ! isset( $recipe_status ) ) {
			return;
		}
		//Add where option is set to Any product
		foreach ( $recipes as $_recipe_id => $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				$trigger_id = absint( $trigger['ID'] );
				if ( intval( '-1' ) === intval( $required_recipe[ $_recipe_id ][ $trigger_id ] ) || (int) $recipe_id === (int) $required_recipe[ $_recipe_id ][ $trigger_id ] ) {
					if ( ! isset( $required_status[ $_recipe_id ] ) ) {
						continue;
					}
					if ( ! isset( $required_status[ $_recipe_id ][ $trigger_id ] ) ) {
						continue;
					}
					if ( intval( '-1' ) === intval( $required_status[ $_recipe_id ][ $trigger_id ] ) || (int) $required_status[ $_recipe_id ][ $trigger_id ] === (int) $recipe_status ) {
						$matched_recipe_ids[ $_recipe_id ] = array(
							'recipe_id'  => $_recipe_id,
							'trigger_id' => $trigger_id,
						);
					}
				}
			}
		}

		if ( empty( $matched_recipe_ids ) ) {
			return;
		}

		foreach ( $matched_recipe_ids as $matched_recipe_id ) {
			$pass_args = array(
				'code'             => $this->trigger_code,
				'meta'             => $this->trigger_meta,
				'recipe_to_match'  => $matched_recipe_id['recipe_id'],
				'trigger_to_match' => $matched_recipe_id['trigger_id'],
				'user_id'          => 0,
				'post_id'          => $recipe_id,
			);

			$args = Automator()->maybe_add_trigger_entry( $pass_args, false );
			if ( $args ) {
				foreach ( $args as $result ) {
					if ( true === $result['result'] ) {
						$recipe = get_post( $recipe_id );
						if ( $recipe ) {
							$trigger_meta = array(
								'user_id'        => 0,
								'trigger_id'     => $result['args']['trigger_id'],
								'trigger_log_id' => $result['args']['get_trigger_id'],
								'run_number'     => $result['args']['run_number'],
							);

							$trigger_meta['meta_key']   = 'UOARECIPES_recipe_id';
							$trigger_meta['meta_value'] = $recipe->ID;
							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'UOARECIPES_recipe_title';
							$trigger_meta['meta_value'] = $recipe->post_title;
							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'UOARECIPES_recipe_status';
							$trigger_meta['meta_value'] = $recipe_status;
							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'UOARECIPES_recipe_edit_link';
							$trigger_meta['meta_value'] = get_edit_post_link( $recipe->ID );
							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'UOARECIPES_recipe_log_url';
							$trigger_meta['meta_value'] = "recipe_id=$recipe_id&user_id=$user_id";
							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'UOARECIPES_action_log_url';
							$trigger_meta['meta_value'] = "recipe_id=$recipe_id&user_id=$user_id";
							Automator()->insert_trigger_meta( $trigger_meta );
						}
						Automator()->maybe_trigger_complete( $result['args'] );
					}
				}
			}
		}
	}