BP_Optout::get_where_sql( array $args = array() )

Assemble the WHERE clause of a get() SQL statement.


Description Description

Used by BP_optout::get() to create its WHERE clause.


Top ↑

Parameters Parameters

$args

(array) (Optional) See BP_optout::get() for more details.

Default value: array()


Top ↑

Return Return

(string) WHERE clause.


Source Source

File: bp-core/classes/class-bp-optout.php

	protected static function get_where_sql( $args = array() ) {
		global $wpdb;

		$where_conditions = array();
		$where            = '';

		// id.
		if ( false !== $args['id'] ) {
			$id_in                  = implode( ',', wp_parse_id_list( $args['id'] ) );
			$where_conditions['id'] = "id IN ({$id_in})";
		}

		// email_address.
		if ( ! empty( $args['email_address'] ) ) {
			if ( ! is_array( $args['email_address'] ) ) {
				$emails = explode( ',', $args['email_address'] );
			} else {
				$emails = $args['email_address'];
			}

			$email_clean = array();
			foreach ( $emails as $email ) {
				$email         = strtolower( $email );
				$email_hash    = wp_hash( $email );
				$email_clean[] = $wpdb->prepare( '%s', $email_hash );
			}

			$email_in                          = implode( ',', $email_clean );
			$where_conditions['email_address'] = "email_address_hash IN ({$email_in})";
		}

		// user_id.
		if ( ! empty( $args['user_id'] ) ) {
			$user_id_in                  = implode( ',', wp_parse_id_list( $args['user_id'] ) );
			$where_conditions['user_id'] = "user_id IN ({$user_id_in})";
		}

		// email_type.
		if ( ! empty( $args['email_type'] ) ) {
			if ( ! is_array( $args['email_type'] ) ) {
				$email_types = explode( ',', $args['email_type'] );
			} else {
				$email_types = $args['email_type'];
			}

			$et_clean = array();
			foreach ( $email_types as $et ) {
				$et_clean[] = $wpdb->prepare( '%s', sanitize_key( $et ) );
			}

			$et_in                          = implode( ',', $et_clean );
			$where_conditions['email_type'] = "email_type IN ({$et_in})";
		}

		// search_terms.
		if ( ! empty( $args['search_terms'] ) ) {
			// Matching email_address is an exact match because of the hashing.
			$args['search_terms']             = strtolower( $args['search_terms'] );
			$search_terms_like                = wp_hash( $args['search_terms'] );
			$where_conditions['search_terms'] = $wpdb->prepare( '( email_address_hash LIKE %s )', $search_terms_like );
		}

		// Custom WHERE.
		if ( ! empty( $where_conditions ) ) {
			$where = 'WHERE ' . implode( ' AND ', $where_conditions );
		}

		return $where;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
8.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.