Noptin_Subscriber::get_data_by( string $field, string|int $value )

Returns only the main subscriber fields


Parameters Parameters

$field

(string) (Required) The field to query against: 'id', 'email' or 'confirm_key'.

$value

(string|int) (Required) The field value


Top ↑

Return Return

(object|false) Raw subscriber object


Top ↑

Source Source

File: includes/class-noptin-subscriber.php

	public static function get_data_by( $field, $value ) {
		global $wpdb;

		$field = strtolower( trim( $field ) );

		if ( 'id' === $field ) {

			// Make sure the value is numeric to avoid casting objects, for example, to int 1.
			if ( ! is_numeric( $value ) ) {
				return false;
			}

			$value = intval( $value );
			if ( $value < 1 ) {
				return false;
			}

		} else {
			$value = trim( $value );
		}

		if ( ! $value ) {
			return false;
		}

		switch ( $field ) {
			case 'id':
				$subscriber_id  = $value;
				$db_field       = 'id';
				break;
			case 'email':
				$subscriber_id  = wp_cache_get( $value, 'noptin_subscriber_emails' );
				$db_field       = 'email';
				break;
			case 'confirm_key':
				$subscriber_id  = wp_cache_get( $value, 'noptin_subscriber_keys' );
				$db_field       = 'confirm_key';
				break;
			default:
				return false;
		}

		// Try retrieving the subscriber from cache.
		if ( false !== $subscriber_id ) {
			$subscriber = wp_cache_get( $subscriber_id, 'noptin_subscribers' );
			if ( $subscriber ) {
				return $subscriber;
			}
		}

		// If that fails, retrieve from the db...
		$table       = get_noptin_subscribers_table_name();
		$subscriber  = $wpdb->get_row(
			$wpdb->prepare(
				"SELECT * FROM $table WHERE $db_field = %s LIMIT 1",
				$value
			)
		);

		if ( ! $subscriber ) {
			return false;
		}

		// ... then cache the results.
		wp_cache_set( $subscriber->id, $subscriber, 'noptin_subscribers' );
		wp_cache_set( $subscriber->confirm_key, $subscriber->id, 'noptin_subscriber_keys' );
		wp_cache_set( $subscriber->email, $subscriber->id, 'noptin_subscriber_emails' );

		return $subscriber;
	}


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