/**
  * Process feed, create conversation.
  * 
  * @access public
  * @param array $feed The feed object to be processed.
  * @param array $entry The entry object currently being processed.
  * @param array $form The form object currently being processed.
  * @return void
  */
 public function process_feed($feed, $entry, $form)
 {
     /* If Help Scout instance is not initialized, exit. */
     if (!$this->initialize_api()) {
         $this->log_error(__METHOD__ . '(): Failed to set up the API.');
         return;
     }
     /* If this entry already has a Help Scout conversation, exit. */
     if (gform_get_meta($entry['id'], 'helpscout_conversation_id')) {
         $this->log_debug(__METHOD__ . '(): Entry already has a Help Scout conversation associated to it. Skipping processing.');
         return;
     }
     /* Prepare needed information. */
     $data = array('email' => $this->get_field_value($form, $entry, $feed['meta']['customer_email']), 'first_name' => $this->get_field_value($form, $entry, $feed['meta']['customer_first_name']), 'last_name' => $this->get_field_value($form, $entry, $feed['meta']['customer_last_name']), 'subject' => GFCommon::replace_variables($feed['meta']['subject'], $form, $entry, false, false, false, 'text'), 'body' => GFCommon::replace_variables($feed['meta']['body'], $form, $entry), 'attachments' => array(), 'tags' => GFCommon::replace_variables($feed['meta']['tags'], $form, $entry));
     /* If the email address is empty, exit. */
     if (GFCommon::is_invalid_or_empty_email($data['email'])) {
         $this->log_error(__METHOD__ . "(): Email address must be provided.");
         return false;
     }
     /* Setup the mailbox for this conversation */
     $mailbox = new \HelpScout\model\ref\MailboxRef();
     $mailbox->setId($feed['meta']['mailbox']);
     /* Create the customer object */
     $customer = $this->api->getCustomerRefProxy(null, $data['email']);
     $customer->setFirstName($data['first_name']);
     $customer->setLastName($data['last_name']);
     /* Create the conversation object */
     $conversation = new \HelpScout\model\Conversation();
     $conversation->setSubject($data['subject']);
     $conversation->setMailbox($mailbox);
     $conversation->setCustomer($customer);
     $conversation->setCreatedBy($customer);
     $conversation->setType($feed['meta']['type']);
     /* Create the message thread */
     if (gf_apply_filters('gform_helpscout_process_body_shortcodes', $form['id'], false, $form, $feed)) {
         $data['body'] = do_shortcode($data['body']);
     }
     $thread = new \HelpScout\model\thread\Customer();
     $thread->setCreatedBy($customer);
     $thread->setBody($data['body']);
     $thread->setStatus($feed['meta']['status']);
     /* Assign this conversation to user if set */
     if (!rgempty('user', $feed['meta'])) {
         $user = new \HelpScout\model\ref\PersonRef();
         $user->setId($feed['meta']['user']);
         $user->setType('user');
         $thread->setAssignedTo($user);
     }
     /* If feed has an attachments field assign, process the attachments. */
     if (!empty($feed['meta']['attachments'])) {
         $attachment_fields = array_keys($feed['meta']['attachments']);
         $attachment_files = array();
         foreach ($attachment_fields as $attachment_field) {
             $field_value = $this->get_field_value($form, $entry, $attachment_field);
             $field_value = $this->is_json($field_value) ? json_decode($field_value, true) : $field_value;
             $field_value = strpos($field_value, ' , ') !== FALSE ? explode(' , ', $field_value) : $field_value;
             if (empty($field_value)) {
                 continue;
             }
             if (is_array($field_value)) {
                 $attachment_files = array_merge($attachment_files, $field_value);
             } else {
                 $attachment_files[] = $field_value;
             }
         }
         if (!empty($attachment_files)) {
             $attachments = $this->process_feed_attachments($attachment_files);
             $thread->setAttachments($attachments);
         }
     }
     /* Add tags to conversation */
     $tags = !empty($data['tags']) ? array_map('trim', explode(',', $data['tags'])) : array();
     $tags = gf_apply_filters('gform_helpscout_tags', $form['id'], $tags, $feed, $entry, $form);
     if (!empty($tags)) {
         $conversation->setTags($tags);
     }
     /* Add CC and BCC support if set. */
     if (isset($feed['meta']['cc'])) {
         $data['cc'] = GFCommon::replace_variables($feed['meta']['cc'], $form, $entry);
         $data['cc'] = is_array($data['cc']) ? $data['cc'] : explode(',', $data['cc']);
         if (!empty($data['cc'])) {
             $thread->setCcList($data['cc']);
         }
     }
     if (isset($feed['meta']['bcc'])) {
         $data['bcc'] = GFCommon::replace_variables($feed['meta']['bcc'], $form, $entry);
         $data['bcc'] = is_array($data['bcc']) ? $data['bcc'] : explode(',', $data['bcc']);
         if (!empty($data['bcc'])) {
             $thread->setCcList($data['bcc']);
         }
     }
     /* Assign the message thread to the conversation */
     $conversation->setThreads(array($thread));
     /* Set thread count to 1 so Help Scout will include the conversation in the mailbox folder count */
     $conversation->setThreadCount(1);
     $this->log_debug(__METHOD__ . "(): Conversation to be created => " . print_r($conversation, true));
     try {
         $auto_reply = rgars($feed, 'meta/auto_reply') == '1';
         /* Create the conversation. */
         $this->api->createConversation($conversation, false, $auto_reply, true);
         gform_update_meta($entry['id'], 'helpscout_conversation_id', $conversation->getId());
         /* Log that conversation was created. */
         $this->log_debug(__METHOD__ . "(): Conversation has been created.");
     } catch (Exception $e) {
         /* Log that conversation was not created. */
         $this->log_error(__METHOD__ . "(): Conversation was not created; {$e->getMessage()}");
         return;
     }
     /* Add conversation note if set. */
     if (rgars($feed, 'meta/note')) {
         /* Replace variables for note. */
         $note_text = GFCommon::replace_variables($feed['meta']['note'], $form, $entry);
         /* Get API user. */
         $api_user = $this->api->getUserMe();
         /* Create note object. */
         $note = new \HelpScout\model\thread\Message();
         $note->setCreatedBy($this->api->getUserRefProxy($api_user->getId()));
         $note->setBody($note_text);
         $note->setType('note');
         try {
             /* Post note to conversation. */
             $this->api->createThread($conversation->getId(), $note);
             /* Log that note was added. */
             $this->log_debug(__METHOD__ . '(): Note was successfully added to conversation.');
         } catch (Exception $e) {
             /* Log that note was not added. */
             $this->log_error(__METHOD__ . '(): Note was not added to conversation; ' . $e->getMessage());
             return;
         }
     }
 }
 $customer->setId(null);
 $customer->setEmail($row['email']);
 $customer->setFirstName($row['f_name']);
 $customer->setLastName($row['l_name']);
 // Create the conversation
 $conversation = new \HelpScout\model\Conversation();
 $conversation->setSubject($row['reason'] . ': ' . $row['subject']);
 $conversation->setCreatedAt(format_api_date($row['date']));
 $conversation->setMailbox($mailbox);
 $conversation->setCustomer($customer);
 $conversation->setType('email');
 $tags = array('backlog', strtolower($row['reason']));
 if ($row['extension'] == 'Yes') {
     $tags[] = strtolower($row['extension_name']);
 }
 $conversation->setTags($tags);
 // A conversation must have at least one thread
 $thread = new \HelpScout\model\thread\Customer();
 $thread->setCreatedAt(format_api_date($row['date']));
 $body = '<strong>Website: </strong>' . $row['website'] . "\r\n";
 if ($row['extension'] == 'Yes') {
     $body .= '<strong>Extension: </strong>' . $row['extension_name'] . "\r\n";
 }
 $body .= "\r\n\r\n" . $row['body'];
 $thread->setBody($body);
 $thread->setStatus('active');
 // Add attachments if they exist.
 if (!empty($attachments)) {
     $thread->setAttachments($attachments);
 }
 // Create by: required