Automator_Functions::pre_fetch_recipe_metas( array $recipes = array() )


Parameters Parameters

$recipes

(Optional)

Default value: array()


Top ↑

Return Return

(array)


Source Source

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

	public function pre_fetch_recipe_metas( $recipes = array() ) {
		$metas    = array();
		$triggers = array();
		$actions  = array();
		$closures = array();
		if ( ! empty( $recipes ) ) {

			global $wpdb;
			// Fetch uo-trigger, uo-action, uo-closure.
			$recipe_children = $wpdb->get_results( "SELECT ID, post_status, post_type
													FROM $wpdb->posts
													WHERE post_parent IN (SELECT ID
													FROM $wpdb->posts
													WHERE post_type = 'uo-recipe')" );

			if ( $recipe_children ) {
				foreach ( $recipe_children as $p ) {
					$ID  = $p->ID;
					$p_t = $p->post_type;
					$p_s = $p->post_status;
					switch ( $p_t ) {
						case 'uo-trigger':
							$triggers[ $ID ] = [ 'ID' => $ID, 'post_status' => $p_s, ];
							break;
						case 'uo-action':
							$actions[ $ID ] = [ 'ID' => $ID, 'post_status' => $p_s, ];
							break;
						case 'uo-closure':
							$closures[ $ID ] = [ 'ID' => $ID, 'post_status' => $p_s, ];
							break;
					}
				}
			}

			///END
			//////////////////////
			//////////////////////

			//////////////////////
			//////////////////////
			//////////////////////
			/////Fetch metas for uo-trigger, uo-action, uo-closure
			$q             = $wpdb->prepare( "SELECT pm.post_id, pm.meta_key, pm.meta_value, p.post_parent, p.post_type
														FROM $wpdb->postmeta pm
														LEFT JOIN $wpdb->posts p
														ON p.ID = pm.post_id
														WHERE pm.post_id IN (SELECT ID
														FROM $wpdb->posts
														WHERE post_parent IN (SELECT ID
														FROM $wpdb->posts
														WHERE post_type = %s))", 'uo-recipe' );
			$related_metas = $wpdb->get_results( $q );

			if ( $related_metas ) {
				foreach ( $related_metas as $p ) {
					$ID  = $p->post_id;
					$m_k = $p->meta_key;
					$m_v = $p->meta_value;
					if ( array_key_exists( $ID, $triggers ) ) {
						$triggers[ $ID ]['meta'][ $m_k ] = $m_v;
					} elseif ( array_key_exists( $ID, $actions ) ) {
						$actions[ $ID ]['meta'][ $m_k ] = $m_v;
					} elseif ( array_key_exists( $ID, $closures ) ) {
						$closures[ $ID ]['meta'][ $m_k ] = $m_v;
					}
				}
			}
			//Fix missing metas!
			if ( $triggers ) {
				foreach ( $triggers as $trigger_ID => $array ) {
					if ( ! array_key_exists( 'meta', $array ) ) {
						$triggers[ $trigger_ID ]['meta'] = [ 'code' => '' ];
					} elseif ( array_key_exists( 'meta', $array ) ) {
						//Attempt to return Trigger ID for magic button
						foreach ( $array['meta'] as $mk => $mv ) {
							if ( 'code' === (string) trim( $mk ) && 'WPMAGICBUTTON' === (string) trim( $mv ) ) {
								$triggers[ $trigger_ID ]['meta']['WPMAGICBUTTON'] = $trigger_ID;
							}
						}
					}
				}
			}

			///END
			//////////////////////
			//////////////////////

			/////Build old recipe array style
			foreach ( $related_metas as $r ) {
				$recipe_id     = absint( $r->post_parent );
				$non_recipe_id = absint( $r->post_id );
				switch ( $r->post_type ) {
					case 'uo-trigger':
						if ( array_key_exists( $non_recipe_id, $triggers ) ) {
							$metas[ $recipe_id ]['triggers'][] = $triggers[ $non_recipe_id ];
							unset( $triggers[ $non_recipe_id ] );
						}
						break;
					case 'uo-action':
						if ( array_key_exists( $non_recipe_id, $actions ) ) {
							$metas[ $recipe_id ]['actions'][] = $actions[ $non_recipe_id ];
							unset( $actions[ $non_recipe_id ] );
						}
						break;
					case 'uo-closure':
						if ( array_key_exists( $non_recipe_id, $closures ) ) {
							$metas[ $recipe_id ]['closures'][] = $closures[ $non_recipe_id ];
							unset( $closures[ $non_recipe_id ] );
						}
						break;
				}
			}
		}

		return $metas;
	}