add_noptin_subscriber( $fields )
Inserts a new subscriber into the database
Description Description
This function returns the subscriber id if the subscriber exists. It does not update the subscriber though.
Return Return
(int|string) Subscriber id on success, error on failure.
Source Source
File: includes/subscriber.php
function add_noptin_subscriber( $fields ) { global $wpdb; $table = get_noptin_subscribers_table_name(); $fields = wp_unslash( apply_filters( 'new_noptin_subscriber_fields', $fields ) ); // Ensure an email address is provided and it doesn't exist already. if ( empty( $fields['email'] ) || ! is_email( $fields['email'] ) ) { return __( 'Please provide a valid email address', 'newsletter-optin-box' ); } // Abort if the email is not unique. $fields['email'] = sanitize_email( $fields['email'] ); $subscriber_id = get_noptin_subscriber_id_by_email( $fields['email'] ); if ( ! empty( $subscriber_id ) ) { return (int) $subscriber_id; } // Maybe split name into first and last. if ( isset( $fields['name'] ) ) { $names = noptin_split_subscriber_name( $fields['name'] ); $fields['first_name'] = empty( $fields['first_name'] ) ? $names[0] : trim( $fields['first_name'] ); $fields['last_name'] = empty( $fields['last_name'] ) ? $names[1] : trim( $fields['last_name'] ); } $database_fields = array( 'email' => $fields['email'], 'first_name' => empty( $fields['first_name'] ) ? '' : $fields['first_name'], 'second_name' => empty( $fields['last_name'] ) ? '' : $fields['last_name'], 'confirm_key' => isset( $fields['confirm_key'] ) ? $fields['confirm_key'] : md5( $fields['email'] . wp_generate_password( 32, true, true ) ), 'date_created' => ! empty( $fields['date_created'] ) ? date( 'Y-m-d', strtotime( $fields['date_created'] ) ) : date( 'Y-m-d', current_time( 'timestamp' ) ), 'active' => isset( $fields['active'] ) ? (int) $fields['active'] : ( get_noptin_option( 'double_optin' ) ? 1 : 0 ), ); if ( ! $wpdb->insert( $table, $database_fields, '%s' ) ) { return 'An error occurred. Try again.'; } $id = $wpdb->insert_id; $fields = array_merge( $fields, $database_fields ); unset( $fields['last_name'] ); unset( $fields['name'] ); // Insert additional meta data. foreach ( $fields as $field => $value ) { if ( isset( $database_fields[ $field ] ) || 'name' === $field || 'integration_data' === $field ) { continue; } update_noptin_subscriber_meta( $id, $field, $value ); } setcookie( 'noptin_email_subscribed', $database_fields['confirm_key'], time() + ( 86400 * 30 * 12 ), COOKIEPATH, COOKIE_DOMAIN ); $cookie = get_noptin_option( 'subscribers_cookie' ); if ( ! empty( $cookie ) && is_string( $cookie ) ) { setcookie( $cookie, '1', time() + ( 86400 * 30 * 12 ), COOKIEPATH, COOKIE_DOMAIN ); } do_action( 'noptin_insert_subscriber', $id, $fields ); return $id; }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
1.0.5 | Introduced. |