/**
  * Get access to instance or create one
  * @return KWSConstantContact
  */
 static function getInstance()
 {
     if (empty(self::$instance)) {
         self::$instance = new KWSConstantContact();
     }
     return self::$instance;
 }
 public function add_menu()
 {
     // Only add the menu if connected to Constant Contact
     if (is_object($this->cc) && !$this->cc->isConfigured()) {
         return;
     }
     add_submenu_page('constant-contact-api', 'CTCT - ' . htmlentities($this->title), '<span id="menu-' . esc_attr($this->getKey()) . '">' . htmlentities($this->getNavTitle()) . '</span>', $this->permission, $this->key, array(&$this, 'page'));
 }
 /**
  * Process the form for backend and frontend
  *
  * 1. Validates the data
  * 2. Creates a KWSContact contact object
  * 3. Validate the email based on settings
  * 4. If valid, add/update
  *
  * @uses KWSConstantContact::addUpdateContact() To add/update contact
  * @todo Return contact on success.
  * @return WP_Error|KWSContact Returns a WP error if error, otherwise a contact object.
  */
 function process()
 {
     $this->id = NULL;
     // Check that the form was submitted
     if (is_admin()) {
         // Validate nonce from the Profile page
         $valid_nonce = wp_verify_nonce($_POST['_wpnonce'], 'update-user_' . $_POST['user_id']);
         $this->id = !empty($_POST['user_id']) && $valid_nonce ? $_POST['user_id'] : false;
     } else {
         $this->id = isset($_POST['uniqueformid']) ? esc_attr($_POST['uniqueformid']) : false;
     }
     if (empty($this->id)) {
         return false;
     }
     // Validate the POST data
     $this->data = $this->sanitizePost();
     // Create the contact
     $KWSContact = new KWSContact($this->data);
     $this->checkRequired();
     $this->validatePhone($KWSContact);
     // Check If Email Is Real
     $this->validateEmail($KWSContact);
     $this->is_processed = true;
     // If validation failed, stop processing
     if (!empty($this->errors)) {
         return;
     }
     // Otherwise, let's Add/Update
     $result = KWSConstantContact::getInstance()->addUpdateContact($KWSContact);
     if (is_wp_error($result)) {
         $this->errors[] = $result;
         $this->results = false;
     } else {
         $this->results = $result;
     }
     if (empty($this->results)) {
         return;
     }
     $this->maybe_redirect();
 }
 function processAjax()
 {
     global $wpdb;
     // this is how you get access to the database
     // Remove the cache for this whole joint
     add_filter('ctct_cache', '__return_false');
     $id = (int) @$_REQUEST['id'];
     $component = esc_html(@$_REQUEST['component']);
     $field = esc_attr(@$_REQUEST['field']);
     $value = @$_REQUEST['value'];
     $value = is_array($value) ? $value : esc_attr($value);
     $parent = esc_attr(@$_REQUEST['parent']);
     $parent = !empty($parent) ? $parent . '_' : NULL;
     if (!isset($_REQUEST['_wpnonce']) || isset($_REQUEST['_wpnonce']) && !wp_verify_nonce($_REQUEST['_wpnonce'], 'ctct') && !defined('DOING_AJAX')) {
         $response['errors'] = __('You\'re not authorized to be here.', 'ctct');
     } elseif (empty($field)) {
         $response['errors'] = __('There is no field defined.', 'ctct');
     } elseif (!isset($_REQUEST['value'])) {
         $response['errors'] = __('There is no value defined.', 'ctct');
     } else {
         $KWSConstantContact = new KWSConstantContact();
         switch ($component) {
             case 'Contact':
                 try {
                     $KWSContact = new KWSContact($KWSConstantContact->getContact(CTCT_ACCESS_TOKEN, $id));
                     // Did anything change?
                     // Check unformattet, then formatted.
                     $nothingChanged = $value === $KWSContact->get($parent . $field) || $value === $KWSContact->get($parent . $field, true);
                     // Lists need to be handled slightly differently.
                     if ($parent . $field === 'lists') {
                         // Get the lists for the contact
                         $existingLists = $KWSContact->get($parent . $field, true);
                         $items = $value;
                         $value = array();
                         foreach ($items as $key => $item) {
                             $value[] = new KWSContactList(array('id' => $item['value']));
                             $compareLists[] = $item['value'];
                         }
                         // If nothing changed, the arrays should be the same
                         // and the diff should be empty
                         $diff = kws_array_diff($existingLists, $compareLists);
                         $nothingChanged = empty($diff);
                     }
                     if ($nothingChanged) {
                         $response['message'] = __('Nothing changed.', 'ctct');
                         $response['code'] = 204;
                     } else {
                         $updatable = $KWSContact->set($parent . $field, $value);
                         if (!$updatable) {
                             $response['message'] = __('This field is not updatable.', 'ctct');
                             $response['code'] = 400;
                         } else {
                             $fetch = $KWSConstantContact->updateContact(CTCT_ACCESS_TOKEN, $KWSContact);
                             $response['message'] = __('Successfully updated.', 'ctct');
                             $response['code'] = 200;
                             delete_transient('ctct_all_contacts');
                             /**
                              * Set this so that next time the user refreshes the contact page,
                              * CTCT_Admin_Contacts::single() will catch it and force refresh.
                              *
                              * @see CTCT_Admin_Contacts::single()
                              */
                             add_option('ctct_refresh_contact_' . $KWSContact->get('id'), 1);
                         }
                     }
                 } catch (Exception $e) {
                     $response['message'] = $e->getErrors();
                     $response['code'] = 400;
                 }
                 break;
             case 'ContactList':
                 try {
                     $KWSList = new KWSContactList($KWSConstantContact->getList(CTCT_ACCESS_TOKEN, $id));
                     if ($value === $KWSList->get($field)) {
                         $response['message'] = __('Nothing changed.', 'ctct');
                         $response['code'] = 204;
                     } else {
                         $updatable = $KWSList->set($field, $value);
                         if (!$updatable) {
                             $response['message'] = __('This field is not updatable.', 'ctct');
                             $response['code'] = 400;
                         } else {
                             $fetch = $KWSConstantContact->updateList(CTCT_ACCESS_TOKEN, $KWSList);
                             $response['message'] = __('Successfully updated.', 'ctct');
                             $response['code'] = 200;
                             delete_transient('ctct_all_lists');
                         }
                     }
                 } catch (Exception $e) {
                     $response['message'] = $e->getErrors();
                     $response['code'] = 400;
                 }
                 break;
             default:
                 $response['message'] = __('There is no component defined.', 'ctct');
                 $response['code'] = 400;
                 break;
         }
     }
     wp_die(json_encode($response));
 }