Automator_Functions::get_recipes_data( $force_new_data_load = false, null $recipe_id = null )

Get data for all recipe objects


Parameters Parameters

$force_new_data_load

(Optional)

Default value: false

$recipe_id

(null) (Optional)

Default value: null


Top ↑

Return Return

(array)


Source Source

File: src/core/lib/class-automator-functions.php

	public function get_recipes_data( $force_new_data_load = false, $recipe_id = null ) {
		if ( ( false === $force_new_data_load ) && ! empty( $this->recipes_data ) && null === $recipe_id ) {
			return $this->recipes_data;
		}

		// Accidentally sent recipe post instead of id?
		if ( $recipe_id instanceof \WP_Post && 'uo-recipe' === (string) $recipe_id->post_type ) {
			$recipe_id = $recipe_id->ID;
		}

		if ( null !== $recipe_id && is_numeric( $recipe_id ) ) {
			return $this->get_recipe_data_by_recipe_id( $recipe_id );
		}

		global $wpdb;

		$recipes = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title, post_type, post_status, post_parent FROM $wpdb->posts WHERE post_type = %s ORDER BY ID DESC LIMIT 0, 99999", 'uo-recipe' ) );
		if ( empty( $recipes ) ) {
			return array();
		}
		//Extract Recipe IDs
		$recipe_ids = array_column( (array) $recipes, 'ID' );
		//Collective array of recipes triggers, actions, closures
		$recipe_data = $this->pre_fetch_recipe_metas( $recipes );
		//Collective array of users recipes completed status
		$recipes_completed = $this->are_recipes_completed( null, $recipe_ids );
		$recipes_completed = empty( $recipes_completed ) ? array() : $recipes_completed;
		$key               = 0;
		foreach ( $recipes as $recipe ) {
			$recipe_id = $recipe->ID;
			if ( array_key_exists( $recipe_id, $recipe_data ) && is_array( $recipe_data[ $recipe_id ] ) && array_key_exists( 'triggers', $recipe_data[ $recipe_id ] ) ) {
				if ( $recipe_data[ $recipe_id ]['triggers'] ) {
					//Grab tokens for each of trigger
					foreach ( $recipe_data[ $recipe_id ]['triggers'] as $t_id => $tr ) {
						$tokens = $this->tokens->trigger_tokens( $tr['meta'], $recipe_id );

						$recipe_data[ $recipe_id ]['triggers'][ $t_id ]['tokens'] = $tokens;
					}
				}
				$triggers = $recipe_data[ $recipe_id ]['triggers'];
			} else {
				$triggers = array();
			}

			$this->recipes_data[ $key ]['ID']          = $recipe_id;
			$this->recipes_data[ $key ]['post_status'] = $recipe->post_status;
			$this->recipes_data[ $key ]['recipe_type'] = Automator()->utilities->get_recipe_type( $recipe_id );

			$this->recipes_data[ $key ]['triggers'] = $triggers;

			if ( array_key_exists( $recipe_id, $recipe_data ) && is_array( $recipe_data[ $recipe_id ] ) && array_key_exists( 'actions', $recipe_data[ $recipe_id ] ) ) {
				$actions = $recipe_data[ $recipe_id ]['actions'];
			} else {
				$actions = array();
			}
			$this->recipes_data[ $key ]['actions'] = $actions;

			if ( array_key_exists( $recipe_id, $recipe_data ) && is_array( $recipe_data[ $recipe_id ] ) && array_key_exists( 'closures', $recipe_data[ $recipe_id ] ) ) {
				$closures = $recipe_data[ $recipe_id ]['closures'];
			} else {
				$closures = array();
			}
			$this->recipes_data[ $key ]['closures'] = $closures;

			$this->recipes_data[ $key ]['completed_by_current_user'] = array_key_exists( $recipe_id, $recipes_completed ) ? $recipes_completed[ $recipe_id ] : false;
			$key ++;
		}

		return $this->recipes_data;
	}