Noptin_Subscriber_Query::prepare_query( string|array $query = array() )

Prepare the query variables.


Parameters Parameters

$query

(string|array) (Optional) Array or string of Query parameters.

  • 'subscriber_status'
    (string) The susbcriber status to filter by. Can either be all, active or inactive. Default is all.
  • 'email_status'
    (string) The email confirmation status. Can either be any, confirmed or unconfirmed. Default is any.
  • 'date_query'
    (array) An array to pass to WP_Date_Query.
  • 'meta_query'
    (array) An array to pass to WP_Meta_Query.
  • 'meta_key'
    (string) Subscriber meta key.
  • 'meta_value'
    (string) Subscriber meta value.
  • 'meta_compare'
    (string) Comparison operator to test the $meta_value. Accepts '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', or 'RLIKE'. Default '='.
  • 'include'
    (array) An array of subscriber IDs to include.
  • 'exclude'
    (array) An array of subscriber IDs to exclude.
  • 'search'
    (string) Search keyword. Searches for possible string matches on columns.
  • 'search_columns'
    (array) Array of column names to be searched. Accepts 'id', 'first_name', 'second_name', 'email', 'date_created'. Default an array containing all the above.
  • 'orderby'
    (string|array) Field(s) to sort the retrieved subscribers by. May be a single value, an array of values, or a multi-dimensional array with fields as keys and orders ('ASC' or 'DESC') as values. Accepted values are 'id', 'first_name', 'second_name', 'include', 'email, 'active', 'date_created', 'meta_value', 'meta_value_num', the value of $meta_key, or an array key of $meta_query. To use 'meta_value' or 'meta_value_num', $meta_key must be also be defined. Default array( 'date_created', 'id' ).
  • 'order'
    (string) Designates ascending or descending order of subscribers. Order values passed as part of an $orderby array take precedence over this parameter. Accepts 'ASC', 'DESC'. Default 'DESC'.
  • 'offset'
    (int) Number of subscribers to offset in retrieved results. Can be used in conjunction with pagination. Default 0.
  • 'number'
    (int) Number of subscribers to limit the query for. Can be used in conjunction with pagination. Value -1 (all) is supported, but should be used with caution on larger sites. Default -1 (all subscribers).
  • 'paged'
    (int) When used with number, defines the page of results to return. Default 1.
  • 'count_total'
    (bool) Whether to count the total number of subscribers found. If pagination is not needed, setting this to false can improve performance. Default true.
  • 'fields'
    (string|array) Which fields to return. Single or all fields (string), or array of fields. Accepts 'id', 'first_name', 'second_name', 'email', 'confirm_key', 'confirmed', 'date_created', 'active'. Use 'all' for all fields. Default 'all'.

Default value: array()


Top ↑

Source Source

File: includes/class-noptin-subscriber-query.php

	public function prepare_query( $query = array() ) {
		global $wpdb;

		if ( empty( $this->query_vars ) || ! empty( $query ) ) {
			$this->query_limit = null;
			$this->query_vars  = $this->fill_query_vars( $query );
		}

		if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) {
			$this->query_vars['fields'] = noptin_parse_list( $this->query_vars['fields'] );
		}

		/**
		 * Fires before the Noptin_Subscriber_Query has been parsed.
		 *
		 * The passed Noptin_Subscriber_Query object contains the query variables, not
		 * yet passed into SQL.
		 *
		 * @since 1.2.7
		 *
		 * @param Noptin_Subscriber_Query $this The current Noptin_Subscriber_Query instance,
		 *                            passed by reference.
		 */
		do_action( 'noptin_pre_get_subscribers', $this );

		// Ensure that query vars are filled after 'noptin_pre_get_subscribers'.
		$qv    =& $this->query_vars;
		$qv    = $this->fill_query_vars( $qv );
		$table = get_noptin_subscribers_table_name();

		if ( is_array( $qv['fields'] ) ) {
			$qv['fields'] = array_unique( $qv['fields'] );

			$this->query_fields = array();
			foreach ( $qv['fields'] as $field ) {
				$field                = 'id' === strtolower( $field ) ? 'id' : sanitize_key( $field );
				$this->query_fields[] = "$table.$field";
			}
			$this->query_fields = implode( ',', $this->query_fields );
		} elseif ( 'all' === $qv['fields'] ) {
			$this->query_fields = "$table.*";
		} else {
			$this->query_fields = "$table.id";
		}

		if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
			$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
		}

		$this->query_from  = "FROM $table";
		$this->query_where = 'WHERE 1=1';

		// Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
		if ( ! empty( $qv['include'] ) ) {
			$include = noptin_parse_int_list( $qv['include'] );
		} else {
			$include = false;
		}

		// Status.
		if ( 'all' !== $qv['subscriber_status'] ) {
			$active = trim( $qv['subscriber_status'] ) === 'active' ? 0 : 1;
			$this->query_where .= $wpdb->prepare( ' AND active = %d', $active );
		}

		// Double optin.
		if ( 'any' !== $qv['email_status'] ) {
			$confirmed = trim( $qv['email_status'] ) === 'confirmed' ? 1 : 0;
			$this->query_where .= $wpdb->prepare( ' AND confirmed = %d', $confirmed );
		}

		// Meta query.
		$this->meta_query = new WP_Meta_Query();
		$this->meta_query->parse_query_vars( $qv );

		if ( ! empty( $this->meta_query->queries ) ) {
			$clauses            = $this->meta_query->get_sql( 'noptin_subscriber', $table, 'id', $this );
			$this->query_from  .= $clauses['join'];
			$this->query_where .= $clauses['where'];

			if ( $this->meta_query->has_or_relation() ) {
				$this->query_fields = 'DISTINCT ' . $this->query_fields;
			}
		}

		// sorting
		$qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : '';
		$order       = $this->parse_order( $qv['order'] );

		if ( empty( $qv['orderby'] ) ) {
			// Default order is by 'date_created id' (latest subscribers).
			$ordersby = array( 'date_created', 'id' );
		} elseif ( is_array( $qv['orderby'] ) ) {
			$ordersby = $qv['orderby'];
		} else {
			// 'orderby' values may be a comma- or space-separated list.
			$ordersby = noptin_parse_list(  $qv['orderby'] );
		}

		$orderby_array = array();
		foreach ( $ordersby as $_key => $_value ) {
			if ( ! $_value ) {
				continue;
			}

			if ( is_int( $_key ) ) {
				// Integer key means this is a flat array of 'orderby' fields.
				$_orderby = $_value;
				$_order   = $order;
			} else {
				// Non-integer key means this the key is the field and the value is ASC/DESC.
				$_orderby = $_key;
				$_order   = $_value;
			}

			$parsed = $this->parse_orderby( $_orderby );

			if ( ! $parsed ) {
				continue;
			}

			$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
		}

		// If no valid clauses were found, order by latest subscribers.
		if ( empty( $orderby_array ) ) {
			$orderby_array[] = "date_created $order id $order";
		}

		$this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array );

		// limit
		if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
			if ( $qv['offset'] ) {
				$this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] );
			} else {
				$this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
			}
		}

		$search = '';
		if ( isset( $qv['search'] ) ) {
			$search = trim( $qv['search'] );
		}

		if ( $search ) {
			trim( $search, '*' );

			$search_columns = array();
			if ( $qv['search_columns'] ) {
				$search_columns = array_intersect( $qv['search_columns'], array( 'id', 'first_name', 'second_name', 'email', 'date_created' ) );
			}
			if ( ! $search_columns ) {
				$search_columns = array( 'id', 'first_name', 'second_name', 'email', 'date_created' );
			}

			$this->query_where .= $this->get_search_sql( $search, $search_columns );
		}

		if ( ! empty( $include ) ) {
			// Sanitized earlier.
			$ids                = implode( ',', $include );
			$this->query_where .= " AND $table.id IN ($ids)";
		} elseif ( ! empty( $qv['exclude'] ) ) {
			$ids                = implode( ',', noptin_parse_int_list( $qv['exclude'] ) );
			$this->query_where .= " AND $table.id NOT IN ($ids)";
		}

		// Date queries are allowed for the date_created field.
		if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) {
			$date_query         = new WP_Date_Query( $qv['date_query'], "$table.date_created" );
			$this->query_where .= $date_query->get_sql();
		}

		/**
		 * Fires after the Noptin_Subscriber_Query has been parsed, and before
		 * the query is executed.
		 *
		 * The passed Noptin_Subscriber_Query object contains SQL parts formed
		 * from parsing the given query.
		 *
		 * @since 1.2.7
		 *
		 * @param Noptin_Subscriber_Query $this The current Noptin_Subscriber_Query instance,
		 *                            passed by reference.
		 */
		do_action_ref_array( 'noptin_pre_subscribers_query', array( &$this ) );
	}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.2.7Introduced.

Top ↑

User Contributed Notes User Contributed Notes

30 day money back guarantee
Safe & Secure online payment
Custom CSS Slide Example
Custom Animation Example
Default Animation