/** * Receives and processes email registrations sent via AJAX. * * @return void * @since 1.0 */ function ql_register_email() { check_ajax_referer('quicklaunch-register-email', '_wpnonce'); $email = trim($_POST['email']); // Create our email object $email_obj = new QL_Email(); $email_obj->email = $email; $email_obj->ip = $_SERVER['REMOTE_ADDR']; // Validate $errors = $email_obj->validate(); if (!empty($errors)) { if (isset($errors['email'])) { $msg = '- ' . implode('\\n - ', $errors['email']); $data = array('msg' => $msg); $response = new QL_JSONResponse($data, false); $response->output(); } } else { $email_obj->save(); $response = new QL_JSONResponse(); $response->output(); } }
/** * Checks and validates the fields of this email record. Returns a * multi-dimensional array of validation errors: * array[field][] = 'Error'; * array[field2][] = 'Error #2'; * * @return array * @since 1.0 */ public function validate() { $errors = array(); if (empty($this->email)) { $errors['email'][] = 'Email address is empty'; } else { if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) { $errors['email'][] = 'Invalid email address.'; } else { if (QL_Email::is_registered($this->email)) { $errors['email'][] = 'Email address has already been subscribed.'; } } } if (empty($this->ip)) { $errors['ip'][] = 'IP Address is empty'; } else { if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) { $errors['email'][] = 'Invalid IP address.'; } } return $errors; }
/** * Generates the admin page that lists the email addresses that subscribed * in the current WP blog. * * @return void * @since 1.1 */ function ql_email_list() { if (isset($_POST['delete']) && !empty($_POST['email'])) { call_user_func_array(array('QL_Email', 'delete'), $_POST['email']); $msg = 'Emails removed from the subscription list.'; } $emails = QL_Email::get_all(); ?> <div class="wrap"> <div id="icon-users" class="icon32"></div> <h2>Email List</h2> <?php $ql_widgets = get_option('ql_widgets'); if ($ql_widgets['mailchimp']) { ?> <div class="error"> <p>You are using Mailchimp to collect email subscriptions.</p> </div> <?php } ?> <?php if ($msg) { ?> <div id="setting-error-settings_updated" class="updated settings-error"> <p><strong><?php echo $msg; ?> </strong></p> </div> <?php } ?> <br> <form action="<?php admin_url('admin.php'); ?> ?page=ql-email-list" method="post" id="emails-form"> <table class="widefat fixed emails" cellspacing="0"> <thead> <tr> <th scope="col" id="cb" class="manage-column column-cb check-column"><input type="checkbox"></th> <th scope="col" id="email" class="manage-column column-email">Email</th> <th scope="col" id="date" class="manage-column column-date" style="width: 30%">Date</th> <th scope="col" id="ip" class="manage-column column-ip" style="width: 20%">IP Address</th> </tr> </thead> <tbody> <?php if (empty($emails)) { ?> <tr> <td colspan="4">No email addresses yet. Don't worry someone will register their email soon :)</td> </tr> <?php } else { foreach ($emails as $email) { ?> <tr> <th scope="row" class="check-column"><input type="checkbox" name="email[]" value="<?php echo $email->id; ?> "></th> <td><a href="mailto:<?php echo $email->email; ?> "><?php echo $email->email; ?> </a></td> <td><?php echo $email->registered_on->format('Y/m/d'); ?> </td> <td><?php echo $email->ip; ?> </td> </tr> <?php } } ?> </tbody> <tfoot> <tr> <th scope="col" class="manage-column column-cb check-column"><input type="checkbox"></th> <th scope="col" class="manage-column column-email">Email</th> <th scope="col" class="manage-column column-date">Date</th> <th scope="col" class="manage-column column-ip">IP Address</th> </tr> </tfoot> </table> <div class="tablenav bottom"> <p> <input type="submit" name="delete" value="Delete Selected" class="button"> <a href="<?php admin_url('admin.php'); ?> ?page=ql-email-list&export=csv">Export as CSV</a> </p> </div> <script> jQuery('#emails-form').submit(function(evt) { if (!confirm('Are you sure you want to delete the selected email addresses?') ) { evt.preventDefault(); } }); </script> </form> </div> <?php }