Automator_Functions::get_recipe_data( null $type = null, null $recipe_id = null, array $recipe_children = array(), null $matched_trigger = null )

Get saved data for recipe actions or triggers


Parameters Parameters

$type

(null) (Optional)

Default value: null

$recipe_id

(null) (Optional)

Default value: null

$recipe_children

(array) (Optional)

Default value: array()

$matched_trigger

(null) (Optional)

Default value: null


Top ↑

Return Return

(null)


Source Source

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

	public function get_recipe_data( $type = null, $recipe_id = null, $recipe_children = array(), $matched_trigger = null ) {

		if ( null === $type ) {
			return null;
		}

		if ( ! in_array( $type, array( 'uo-trigger', 'uo-action', 'uo-closure' ), true ) ) {
			return null;
		}

		if ( is_null( $recipe_id ) || ! is_numeric( $recipe_id ) ) {
			Automator()->error->trigger( 'You are trying to get recipe data without providing a recipe_id' );

			return null;
		}

		global $wpdb;
		if ( empty( $recipe_children ) ) {
			$q = $wpdb->prepare( "SELECT ID, post_status FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $recipe_id, $type );
			$q = apply_filters_deprecated(
				'q_get_recipe_data',
				array(
					$q,
					$recipe_id,
					$type,
				),
				'3.0',
				'automator_get_recipe_data_query'
			);
			$q = apply_filters( 'automator_get_recipe_data_query', $q, $recipe_id, $type );

			// All the triggers associated with the recipe
			$recipe_children = $wpdb->get_results( $q, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		}
		// All data for recipe triggers
		$recipe_children_data = array();
		if ( empty( $recipe_children ) ) {
			return $recipe_children_data;
		}
		// Check each trigger for set values
		foreach ( $recipe_children as $key => $child ) {
			// Collect all meta data for this trigger
			if ( ! array_key_exists( 'meta', $child ) ) {
				$child_meta = get_post_custom( $child['ID'] );
			} else {
				$child_meta = $child['meta'];
			}

			if ( ! $child_meta ) {
				continue;
			}
			// Get post custom return an array for each meta_key as there maybe more than one value per key.. we only store and need one value
			$child_meta_single = array();
			foreach ( $child_meta as $meta_key => $meta_value ) {
				$child_meta_single[ $meta_key ] = reset( $meta_value );
			}
			$code = array_key_exists( 'code', $child_meta_single ) ? $child_meta_single['code'] : '';

			/** Fix to show MAGIC BUTTON ID
			 *
			 * @since 3.0
			 * @package Uncanny_Automator
			 */
			if ( 'WPMAGICBUTTON' === (string) $code && ! array_key_exists( 'WPMAGICBUTTON', $child_meta_single ) ) {
				$child_meta_single['WPMAGICBUTTON'] = $child['ID'];
			}

			$item_not_found = true;

			if ( 'uo-trigger' === $type ) {
				$system_triggers = $this->get_triggers();
				if ( ! empty( $system_triggers ) ) {
					foreach ( $system_triggers as $trigger ) {
						if ( $trigger['code'] === $code ) {
							$item_not_found = false;
						}
					}
				} else {
					$item_not_found = false;
				}
			}

			if ( 'uo-action' === $type ) {
				$system_actions = $this->get_actions();
				if ( ! empty( $system_actions ) ) {
					foreach ( $system_actions as $action ) {
						if ( $action['code'] === $code ) {
							$item_not_found = false;
						}
					}
				} else {
					$item_not_found = false;
				}
			}

			if ( 'uo-closure' === $type ) {
				$system_closures = $this->get_closures();
				if ( ! empty( $system_closures ) ) {
					foreach ( $system_closures as $closure ) {
						if ( $closure['code'] === $code ) {
							$item_not_found = false;
						}
					}
				} else {
					$item_not_found = false;
				}
			}

			if ( $item_not_found ) {
				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = 'draft' WHERE ID = %d", absint( $child['ID'] ) ) );
				$child['post_status'] = 'draft';
			}

			// The trigger is create/stored automatically but may not have been saved. Delete if not saved!
			if ( empty( $child_meta ) && isset( $child['ID'] ) ) {
				continue;
			}

			$recipe_children_data[ $key ]['ID']          = absint( $child['ID'] );
			$recipe_children_data[ $key ]['post_status'] = $child['post_status'];
			$recipe_children_data[ $key ]['meta']        = $child_meta_single;

			if ( 'uo-trigger' === $type ) {
				$recipe_children_data[ $key ]['tokens'] = $this->tokens->trigger_tokens( $child_meta_single, $recipe_id );
			}
		}

		return apply_filters(
			'automator_recipe_children_data',
			$recipe_children_data,
			array(
				'type'            => $type,
				'recipe_id'       => $recipe_id,
				'recipe_children' => $recipe_children,
			)
		);
	}