UOA_RECIPECOMPLETEDTIMES::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-recipecompletedtimes.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 );
		$num_times          = Automator()->get->meta_from_recipes( $recipes, $this->num_times );
		$matched_recipe_ids = array();

		if ( empty( $recipes ) ) {
			return;
		}
		if ( empty( $required_recipe ) ) {
			return;
		}
		if ( empty( $num_times ) ) {
			return;
		}

		global $wpdb;
		$table_name      = $wpdb->prefix . Automator()->db->tables->recipe;
		$completed_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(ID) FROM $table_name WHERE automator_recipe_id = %d", $recipe_id ) ); //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		if ( 0 === $completed_count ) {
			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( $num_times[ $_recipe_id ] ) ) {
						continue;
					}
					if ( ! isset( $num_times[ $_recipe_id ][ $trigger_id ] ) ) {
						continue;
					}
					if ( 0 === absint( $completed_count ) % absint( $num_times[ $_recipe_id ][ $trigger_id ] ) ) {
						$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,
				'user_id'          => 0,
				'recipe_to_match'  => $matched_recipe_id['recipe_id'],
				'trigger_to_match' => $matched_recipe_id['trigger_id'],
				'ignore_post_id'   => true,
			);
			$args      = Automator()->maybe_add_trigger_entry( $pass_args, false );
			if ( empty( $args ) ) {
				continue;
			}
			foreach ( $args as $result ) {
				if ( false === $result['result'] ) {
					continue;
				}
				$recipe = get_post( $recipe_id );

				if ( ! $recipe ) {
					continue;
				}

				Automator()->insert_trigger_meta(
					array(
						'user_id'        => $user_id,
						'trigger_id'     => $result['args']['trigger_id'],
						'meta_key'       => 'UOARECIPES_recipe_id',
						'meta_value'     => $recipe->ID,
						'trigger_log_id' => $result['args']['get_trigger_id'],
						'run_number'     => $result['args']['run_number'],
					)
				);
				Automator()->insert_trigger_meta(
					array(
						'user_id'        => $user_id,
						'trigger_id'     => $result['args']['trigger_id'],
						'meta_key'       => 'UOARECIPES_recipe_title',
						'meta_value'     => $recipe->post_title,
						'trigger_log_id' => $result['args']['get_trigger_id'],
						'run_number'     => $result['args']['run_number'],
					)
				);
				Automator()->insert_trigger_meta(
					array(
						'user_id'        => $user_id,
						'trigger_id'     => $result['args']['trigger_id'],
						'meta_key'       => 'UOARECIPES_recipe_edit_link',
						'meta_value'     => get_edit_post_link( $recipe->ID ),
						'trigger_log_id' => $result['args']['get_trigger_id'],
						'run_number'     => $result['args']['run_number'],
					)
				);
				Automator()->insert_trigger_meta(
					array(
						'user_id'        => $user_id,
						'trigger_id'     => $result['args']['trigger_id'],
						'meta_key'       => 'UOARECIPES_recipe_log_url',
						'meta_value'     => "recipe_id=$recipe_id&user_id=$user_id",
						'trigger_log_id' => $result['args']['get_trigger_id'],
						'run_number'     => $result['args']['run_number'],
					)
				);
				Automator()->insert_trigger_meta(
					array(
						'user_id'        => $user_id,
						'trigger_id'     => $result['args']['trigger_id'],
						'meta_key'       => 'UOARECIPES_action_log_url',
						'meta_value'     => "recipe_id=$recipe_id&user_id=$user_id",
						'trigger_log_id' => $result['args']['get_trigger_id'],
						'run_number'     => $result['args']['run_number'],
					)
				);

				Automator()->maybe_trigger_complete( $result['args'] );
			}
		}
	}