Noptin_Ajax::import_subscribers()

Imports subscribers


Return Return

(void)


Top ↑

Source Source

File: includes/class-noptin-ajax.php

	public function import_subscribers() {
		global $wpdb;

		// Ensure the nonce is valid...
		check_ajax_referer( 'noptin_subscribers' );

		// ... and that the user can import subscribers.
		if ( ! current_user_can( get_noptin_capability() ) ) {
			wp_die( -1, 403 );
		}

		// Maybe abort early.
		if ( ! isset( $_POST['subscribers'] ) ) {
			wp_die( -1, 400 );
		}

		// Prepare subscribers.
		$subscribers = stripslashes_deep( $_POST['subscribers'] );

		// Are there subscribers?
		if ( empty( $subscribers ) ) {
			wp_send_json_success( array(
				'imported'	=> 0,
				'skipped'	=> 0,
			) );
			exit;
		}

		$table    = get_noptin_subscribers_table_name();
		$imported = 0;
		$skipped  = 0;

		foreach ( $subscribers as $subscriber ) {
			if( ! is_array( $subscriber ) ) {
				$skipped ++;
				log_noptin_message( __( 'Import error: not an array. Skipping.', 'newsletter-optin-box' ) );
				continue;
			}

			$subscriber = apply_filters( 'noptin_format_imported_subscriber_fields', $subscriber );

			// Ensure that there is a unique email address.
			if ( empty( $subscriber['email'] ) ) {
				log_noptin_message( __( 'Import error: email not found. Skipping.', 'newsletter-optin-box' ) );
				$skipped ++;
				continue;
			}

			if ( ! is_email( $subscriber['email'] )  ) {
				log_noptin_message( sprintf(
					__( 'Import skipping %s: Invalid email' ),
					esc_html( $subscriber['email'] )
				));
				$skipped ++;
				continue;
			}

			// Ensure that there is a unique email address.
			if ( noptin_email_exists( $subscriber['email'] ) ) {
				log_noptin_message( sprintf(
					__( 'Import skipping %s: Subscriber already exists' ),
					esc_html( $subscriber['email'] )
				));
				$skipped ++;
				continue;
			}

			// Sanitize email status
			if( ! isset( $subscriber['confirmed'] ) ) {
				$subscriber['confirmed'] = 0;
			}

			if( is_string( $subscriber['confirmed'] ) ) {
				$subscriber['confirmed'] = strtolower( $subscriber['confirmed'] );
			}

			if ( ( is_numeric( $subscriber['confirmed'] ) && 0 === ( int ) $subscriber['confirmed'] ) || 
			    false         === $subscriber['confirmed'] || 
				'false'       === $subscriber['confirmed'] || 
				'unconfirmed' === $subscriber['confirmed'] || 
				''            === $subscriber['confirmed'] || 
				'no'          === $subscriber['confirmed'] ) {
				$subscriber['confirmed'] = 0;
			} else {
				$subscriber['confirmed'] = 1;
			}

			// Sanitize subscriber status

			if( ! isset( $subscriber['active'] ) ) {
				$subscriber['active'] = 1;
			}

			if( is_string( $subscriber['active'] ) ) {
				$subscriber['active'] = strtolower( $subscriber['active'] );
			}
			if ( 
				( is_numeric( $subscriber['active'] ) && 1 === ( int ) $subscriber['active'] )  || 
				true         === $subscriber['active'] ||
				'true'       === $subscriber['active'] ||
				'subscribed' === $subscriber['active'] ||
				'active'     === $subscriber['active'] ||
				'yes'        === $subscriber['active'] ) {
				$subscriber['active'] = 0;
			} else {
				$subscriber['active'] = 1;
			}

			// Maybe split name into first and last.
			if ( isset( $subscriber['name'] ) ) {
				$names = noptin_split_subscriber_name( $subscriber['name'] );

				$subscriber['first_name']   = empty( $subscriber['first_name'] ) ? $names[0] : trim( $subscriber['first_name'] );
				$subscriber['second_name']  = empty( $subscriber['second_name'] ) ? $names[1] : trim( $subscriber['second_name'] );
				unset( $subscriber['name'] );
			}

			// Save the main subscriber fields.
			$database_fields = array(
				'email'        => $subscriber['email'],
				'first_name'   => empty( $subscriber['first_name'] ) ? '' : $subscriber['first_name'],
				'second_name'  => empty( $subscriber['second_name'] ) ? '' : $subscriber['second_name'],
				'confirm_key'  => empty( $subscriber['confirm_key'] ) ? md5( $subscriber['email'] . wp_generate_password( 32 ) ) : $subscriber['confirm_key'],
				'date_created' => empty( $subscriber['date_created'] ) ? date( 'Y-m-d', current_time( 'timestamp' ) ) : $subscriber['date_created'],
				'confirmed'	   => $subscriber['confirmed'],
				'active'	   => $subscriber['active'],
			);

			if ( ! $wpdb->insert( $table, $database_fields, '%s' ) ) {
				$skipped ++;
				log_noptin_message( __( 'Import error:', 'newsletter-optin-box' ) . ' ' . $wpdb->last_error );
				continue;
			}

			$id = $wpdb->insert_id;

			$meta = $subscriber['meta'];
			unset( $subscriber['meta'] );

			$extra_meta = array_diff_key( $subscriber, $database_fields );
			foreach ( $extra_meta as $field => $value ) {

				if ( is_null( $value ) ) {
					continue;
				}
				if ( ! isset( $meta[ $field ] ) ) {
					$meta[ $field ] = array();
				}

				$meta[ $field ][] = $value;

			}

			foreach ( $meta as $field => $value ) {

				if ( ! is_array( $value ) ) {
					$value = array( $value );
				}

				foreach ( $value as $val ) {
					update_noptin_subscriber_meta( $id, $field, $val );
				}
			}

			update_noptin_subscriber_meta( $id, '_subscriber_via', 'import' );
			do_action( 'noptin_after_import_subscriber', $id, $subscriber, $meta );

			$imported += 1;

		}

		wp_send_json_success( array(
			'imported'	=> $imported,
			'skipped'	=> $skipped,
		) );
		exit;

	}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.2.2Introduced.

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