/**
  * Gets contact history from the database (pageviews, form submissions, shares, and lead details)
  *
  * @param	string
  * @return	object 	$history (pageviews_by_session, submission, lead)
  */
 function get_contact_history()
 {
     global $wpdb;
     $lead = $this->get_contact_details($this->hashkey);
     $pageviews = $this->get_contact_pageviews($this->hashkey, 'ARRAY_A');
     $submissions = $this->get_contact_submissions($this->hashkey, 'ARRAY_A');
     $shares = $this->get_contact_shares($this->hashkey, 'ARRAY_A');
     $lead_lists = $this->get_contact_lead_lists($this->hashkey);
     if (WPInboundRocket::is_power_up_active('people-search')) {
         $lead->social_data = $this->get_social_details($lead);
         $lead->company_data = $this->get_company_details($lead);
     }
     // Merge the page views array and submissions array and reorder by date
     $events_array = array_merge($pageviews, $submissions, $shares);
     usort($events_array, array(&$this, 'sort_by_event_date'));
     $sessions = array();
     $cur_array = '0';
     $first_iteration = TRUE;
     $count = 0;
     $cur_event = 0;
     $prev_form_event = FALSE;
     $total_visits = 0;
     $total_pageviews = 0;
     $total_submissions = 0;
     $total_shares = 0;
     $new_session = TRUE;
     $array_tags = array();
     if (isset($tags)) {
         foreach ($tags as $tag) {
             array_push($array_tags, array('form_hashkey' => $tag->form_hashkey, 'tag_text' => $tag->tag_text, 'tag_slug' => $tag->tag_slug));
         }
     }
     foreach ($events_array as $event_name => $event) {
         // Create a new session array if pageview started a new session
         if ($new_session) {
             $cur_array = $count;
             if (!isset($event['event_date']) || empty($event['event_date'])) {
                 continue;
             }
             $sessions['session_' . $cur_array] = array();
             $sessions['session_' . $cur_array]['session_date'] = $event['event_date'];
             $sessions['session_' . $cur_array]['events'] = array();
             $cur_event = $count;
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event] = array();
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_type'] = 'pageview';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_date'] = $event['event_date'];
             // Set the first submission if it's not set and then leave it alone
             if (!isset($lead->last_visit) && !empty($lead)) {
                 $lead->last_visit = isset($event['event_date']) ? $event['event_date'] : '';
             }
             // Used for $lead->total_visits
             $total_visits++;
             $new_session = FALSE;
         }
         // Pageview activity form submissions
         if (isset($event['pageview_id'])) {
             $cur_event = $count;
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event] = array();
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_type'] = 'pageview';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_date'] = isset($event['event_date']) ? $event['event_date'] : '';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['activities'][] = $event;
             $total_pageviews++;
             // Always overwrite first_visit which will end as last pageview date
             $lead->first_visit = isset($event['event_date']) ? $event['event_date'] : '';
             $lead->lead_source = isset($event['pageview_source']) ? $event['pageview_source'] : '';
         } elseif (isset($event['share_id'])) {
             // Used for $lead->total_shares
             $cur_event = $count;
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event] = array();
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_type'] = 'text-share';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_date'] = isset($event['event_date']) ? $event['event_date'] : '';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['activities'][] = $event;
             // Set the first share if it's not set and then leave it alone
             if (!isset($lead->last_share)) {
                 $lead->last_share = $sessions['session_' . $cur_array]['events']['event_' . $cur_event];
             }
             $total_shares++;
         } else {
             // Always overwrite the last_submission date which will end as last submission date
             $lead->first_submission = isset($event['event_date']) ? $event['event_date'] : '';
             $event['form_name'] = 'form';
             if (isset($event['form_selector_id'])) {
                 $event['form_name'] = '#' . $event['form_selector_id'];
             } else {
                 if (isset($event['form_selector_classes'])) {
                     if (strstr($event['form_selector_classes'], ',')) {
                         $classes = explode(',', $event['form_selector_classes']);
                         $event['form_name'] = isset($classes[0]) ? '.' . $classes[0] : 'form';
                     } else {
                         $event['form_name'] = '.' . $event['form_selector_classes'];
                     }
                 }
             }
             // Run through all the tags and see if the form_hashkey triggered the tag relationship
             $form_tags = array();
             if (count($array_tags)) {
                 foreach ($array_tags as $at) {
                     if ($at['form_hashkey'] == $event['form_hashkey']) {
                         array_push($form_tags, $at);
                     }
                 }
             }
             $cur_event = $count;
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event] = array();
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_type'] = 'form';
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['event_date'] = $event['event_date'];
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['form_name'] = $event['form_name'];
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['form_tags'] = $form_tags;
             $sessions['session_' . $cur_array]['events']['event_' . $cur_event]['activities'][] = $event;
             // Set the first submission if it's not set and then leave it alone
             if (!isset($lead->last_submission)) {
                 $lead->last_submission = $sessions['session_' . $cur_array]['events']['event_' . $cur_event];
             }
             // Used for $lead->total_submissions
             $total_submissions++;
         }
         if (isset($event['pageview_session_start']) && $event['pageview_session_start']) {
             $new_session = TRUE;
         }
         $count++;
     }
     $lead->total_visits = $total_visits;
     $lead->total_pageviews = $total_pageviews;
     $lead->total_submissions = $total_submissions;
     $lead->total_shares = $total_shares;
     $this->history = (object) NULL;
     $this->history->submission = $submissions[0];
     $this->history->sessions = $sessions;
     $this->history->lead = $lead;
     $this->history->lead_lists = isset($lead_lists) ? $lead_lists : null;
     $this->history->shares = isset($shares) ? $shares : null;
     return stripslashes_deep($this->history);
 }
 /**
  * Gets all the contacts in a list given a list_id
  *
  * @param 	int
  * @param 	string 		serialized array
  * @return  object
  */
 function push_contacts_to_lead_list($list_id = 0, $tag_synced_lists = '')
 {
     global $wpdb;
     if (!$tag_synced_lists) {
         $this->get_tag_details($list_id);
         $tag_synced_lists = $this->details->tag_synced_lists;
     }
     $contacts = $this->get_contacts_in_tagged_list($list_id);
     if (count($contacts) && $tag_synced_lists) {
         $synced_lists = unserialize($tag_synced_lists);
         if (count($synced_lists)) {
             foreach ($synced_lists as $synced_list) {
                 $power_up_slug = $synced_list['esp'] . '_connect';
                 // e.g inboundrocket_mailchimp_connect_wp
                 if (WPInboundRocket::is_power_up_active($power_up_slug)) {
                     global ${'inboundrocket_' . $power_up_slug . '_wp'};
                     // e.g inboundrocket_mailchimp_connect_wp
                     ${'inboundrocket_' . $power_up_slug . '_wp'}->bulk_push_contact_to_list($synced_list['list_id'], $contacts);
                 }
             }
         }
     }
 }