Automator_Helpers_Recipe::wp_query( array $args, bool $add_any_option = false, string $add_any_option_label = null, bool $is_all_label = false )


Parameters Parameters

$args

(array) (Required)

$add_any_option

(bool) (Optional)

Default value: false

$add_any_option_label

(string) (Optional)

Default value: null

$is_all_label

(bool) (Optional)

Default value: false


Top ↑

Return Return

(array)


Source Source

File: src/core/lib/helpers/class-automator-recipe-helpers.php

	public function wp_query( $args, $add_any_option = false, $add_any_option_label = null, $is_all_label = false ) {

		// set up a default label.
		if ( is_null( $add_any_option_label ) ) {
			$add_any_option_label = esc_attr__( 'Any page', 'uncanny-automator' );
		}

		// bail, if we aren't supposed to do this at all.
		if ( ! $this->load_helpers ) {
			return array();
		}

		// bail if no arguments are supplied.
		if ( empty( $args ) ) {
			return array();
		}

		/**
		 * Allow developers to modify $args
		 *
		 * @author  Saad
		 * @version 2.6
		 */
		$args = apply_filters( 'automator_wp_query_args', $args );

		extract( $args ); //phpcs:ignore WordPress.PHP.DontExtract.extract_extract

		// prepare transient key.
		$transient_key = apply_filters( 'automator_transient_name', 'automator_transient', $args );

		// suffix post type is needed.
		if ( isset( $args['post_type'] ) ) {
			$transient_key .= md5( wp_json_encode( $args ) );
		}

		// attempt fetching options from transient.
		$options = apply_filters( 'automator_modify_transient_options', get_transient( $transient_key ), $args );

		// if meta query is set, its better to re-run query instead of transient
		if ( isset( $args['meta_query'] ) && ! empty( $args['meta_query'] ) ) {
			$options = array();
		}
		// if the transient is empty, generate options afresh.
		if ( empty( $options ) ) {
			// fetch all the posts.
			global $wpdb;
			if ( isset( $args['meta_query'] ) && ( isset( $args['meta_query']['relation'] ) || count( $args['meta_query'] ) > 1 ) ) {
				$posts = get_posts( $args );
			} else {
				$join = '';
				// basic query begins
				$query = "SELECT p.ID, p.post_title
						FROM $wpdb->posts p";

				// check if there's meta query.. which means
				// we have to join postmeta table
				if ( isset( $meta_query ) ) {
					$mq         = array_shift( $meta_query );
					$meta_key   = sanitize_text_field( $mq['key'] );
					$meta_value = sanitize_text_field( $mq['value'] );
					$compare    = isset( $mq['compare'] ) ? $mq['compare'] : 'LIKE';
					$join       .= "INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '{$meta_key}' AND pm.meta_value {$compare} '{$meta_value}'"; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
				}

				// Join tables
				$query .= $join;

				// basic where 1=1 so all other
				// where clauses can be joined via AND
				$query .= ' WHERE 1=1 ';

				// include post_type with fallback to publish
				if ( isset( $post_status ) ) {
					if ( ! empty( $post_status ) && ! is_array( $post_status ) ) {
						$query .= " AND p.post_status = '$post_status' ";
					} elseif ( ! empty( $post_status ) && is_array( $post_status ) ) {
						$comma_separated = implode( "','", $post_status );
						$comma_separated = "'" . $comma_separated . "'";
						$query           .= "AND p.post_status IN ({$comma_separated}) "; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
					} else {
						$query .= " AND p.post_status = 'publish' ";
					}
				} else {
					$query .= " AND p.post_status = 'publish' ";
				}

				// filter by post_type with fallback to 'page' only
				if ( isset( $post_type ) && ! is_array( $post_type ) ) {
					$query .= " AND p.post_type = '{$post_type}'";
				} elseif ( isset( $post_type ) && is_array( $post_type ) ) {
					$comma_separated = implode( "','", $post_type );
					$comma_separated = "'" . $comma_separated . "'";
					$query           .= " AND p.post_type = '{$comma_separated}'"; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
				} else {
					$query .= " AND p.post_type = 'page'";
				}

				// order by provided argument, fallback to title
				if ( isset( $orderby ) && ! empty( $orderby ) ) {
					switch ( $orderby ) {
						case 'ID':
							$order_by = 'p.ID';
							break;
						case 'title':
						default:
							$order_by = 'p.post_title';
							break;

					}
					$query .= " ORDER BY $order_by";
				} else {
					$query .= ' ORDER BY p.post_title';
				}

				if ( isset( $order ) && empty( $order ) ) {
					$query .= " $order";
				} else {
					$query .= ' ASC';
				}

				if ( isset( $posts_per_page ) ) {
					$query .= " LIMIT 0, $posts_per_page";
				}

				/**
				 * dropped get_posts() and used direct query to reduce load time
				 *
				 * @version 2.6
				 * @author  Saad
				 *
				 * @var string $query MySQL query
				 * @var array $args of arguments passed to function
				 */
				$query = apply_filters( 'automator_maybe_modify_wp_query', $query, $args );

				$posts = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			}
			// type set to array.
			$options = array();

			// if posts were found.
			if ( $posts ) {

				// loop through each post to set up individual options.
				foreach ( $posts as $post ) {
					$title = $post->post_title;

					// set up a descriptive title for posts with no title.
					if ( empty( $title ) ) {
						/* translators: ID of recipe, trigger or action  */
						$title = sprintf( esc_attr__( 'ID: %1$s (no title)', 'uncanny-automator' ), $post->ID );
					}

					// add post as an option.
					$options[ $post->ID ] = $title;
				}

				// save fetched posts in a transient for 5 minutes for performance gains.
				/**
				 * Allow developers to modify transient times
				 *
				 * @author  Saad
				 * @version 2.6
				 */
				$transient_time = apply_filters( 'automator_transient_time', 5 * MINUTE_IN_SECONDS );
				set_transient( $transient_key, $options, $transient_time );
			}
		}

		// do we need to add an any/all posts option
		if ( $add_any_option ) {

			// get extra option.
			$any_option = $this->maybe_add_any_option( $add_any_option_label, $is_all_label );
			$options    = $any_option + $options;
		}

		return apply_filters( 'automator_modify_option_results', $options, $args );
	}