/**
  * Convert an array of List objects into HTML output
  *
  * @param  array $passed_items List array
  * @param  array $atts Settings; `fill`, `selected`, `format`; `format` should use replacement tags with the tag being the name of the var of the List object you want to replace. For example, `%%name%% (%%contact_count%% Contacts)` will return an item with the content "List Name (140 Contacts)"
  *
  * `showhidden` If true, will exclude lists that have a status of "hidden" in http://dotcms.constantcontact.com/docs/contact-list-api/contactlist-collection.html
  *
  * @return [type]        [description]
  */
 static function outputHTML($passed_items = array(), $atts = array())
 {
     $settings = wp_parse_args($atts, array('type' => 'checkboxes', 'fill' => true, 'format' => '<span>%%name%%</span>', 'id_attr' => 'ctct-%%id%%', 'name_attr' => 'lists', 'checked' => array(), 'include' => array(), 'showhidden' => true, 'class' => '', 'blank' => ''));
     extract($settings);
     $items = array();
     // Tell the cache that if the current requests are forced to be
     // refreshed, the cache should also reset this key.
     // See Cache_WP_HTTP
     add_filter('flush_key', function () {
         return 'ctct_all_lists';
     });
     if ($passed_items === 'all') {
         $items = WP_CTCT::getInstance()->cc->getAllLists();
     } elseif (!empty($passed_items) && is_array($passed_items)) {
         foreach ($passed_items as $item) {
             global $list_id;
             if ($fill) {
                 $list_id = is_object($item) ? $item->id : $item;
                 $list_id = esc_attr($list_id);
                 // Tell Cache_WP_HTTP to use the following key
                 // as the transient name
                 add_filter('ctct_cachekey', function () {
                     global $list_id;
                     return 'ctct_list_' . $list_id;
                 });
                 $item = WP_CTCT::getInstance()->cc->getList(CTCT_ACCESS_TOKEN, $list_id);
             }
             $items[] = $item;
         }
     }
     $before = $before_item = $after_item = $after = '';
     switch ($type) {
         case 'hidden':
             $format = '<input type="hidden" value="%%id%%" name="%%name_attr%%[]" />';
             break;
         case 'ul':
             $before = '<ul class="ul-square">';
             $before_item = '<li>';
             $after_item = '</li>';
             $after = '</ul>';
             break;
         case 'dropdown':
         case 'select':
         case 'multiselect':
             $multiple = '';
             // Even though the multiselect option is no longer available
             // in the settings, keep this around for backward compatibility.
             // And if crazy people want multi-selects
             if ($type === 'select' || $type === 'multiselect') {
                 $multiple = ' multiple="multiple"';
             }
             $before = '<select name="%%name_attr%%"' . $multiple . ' class="select2 ctct-lists">';
             $before_item = '<option value="%%id%%">';
             $after_item = '</option>';
             $after = '</select>';
             // Allow passing a blank item title
             if (!empty($blank)) {
                 $before .= '<option value="">' . esc_html($blank) . '</option>';
             }
             break;
         case 'checkbox':
         case 'checkboxes':
             $before = '<ul class="ctct-lists ctct-checkboxes ' . esc_attr($class) . '">';
             $before_item = '<li><label for="%%id_attr%%"><input type="checkbox" id="%%id_attr%%" value="%%id%%" name="%%name_attr%%[]" %%checked%% /> ';
             $after_item = '</label></li>';
             $after = '</ul>';
             break;
     }
     $output = $before;
     $items_output = '';
     foreach ($items as &$item) {
         // If include was specified, then we need to skip lists not included
         if (is_array($passed_items) && (!empty($include) && !in_array($item->id, $include)) || $item->status === 'HIDDEN' && !$showhidden) {
             #continue;
         }
         $item = new KWSContactList($item);
         $item_content = !empty($format) || is_null($format) ? $format : $item->name;
         $tmp_output = $before_item . $item_content . $after_item . "\n";
         $tmp_output = str_replace('%%id%%', sanitize_title($item->get('id')), $tmp_output);
         $tmp_output = str_replace('%%name%%', $item->get('name', false), $tmp_output);
         $tmp_output = str_replace('%%status%%', $item->get('status', false), $tmp_output);
         $tmp_output = str_replace('%%contact_count%%', $item->get('contact_count', true), $tmp_output);
         $tmp_output = str_replace('%%checked%%', checked(in_array($item->get('id'), (array) $checked) || is_null($checked) && $item->get('status') === 'ACTIVE', true, false), $tmp_output);
         $items_output .= $tmp_output;
     }
     $output .= $items_output;
     $output .= $after;
     $output = str_replace('%%name_attr%%', $name_attr, $output);
     $output = str_replace('%%id_attr%%', $id_attr, $output);
     return $output;
 }
 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));
 }
		</tr>
	</thead>
	<tbody>

<?php 
foreach ((array) $Lists as $List) {
    $List = new KWSContactList($List);
    $alt = empty($alt) ? 'class="alt"' : '';
    ?>
		<tr <?php 
    echo $alt;
    ?>
>
			<td class="column-id">
				<?php 
    echo $List->get('id');
    ?>
			</td>
			<td class="column-name">
				<?php 
    echo $List->get('name', true);
    ?>
			</td>
			<td class="column-name">
				<?php 
    echo $List->get('contact_count');
    ?>
			</td>
			<td class="column-name">
				<a href="<?php 
    echo esc_url(add_query_arg(array('view' => $List->id), remove_query_arg('add')));