/**
  * Ingest emails from a SMTP account
  *
  * @subcommand ingest-emails
  */
 public function ingest_emails($args, $assoc_args)
 {
     $defaults = array('host' => '', 'username' => '', 'password' => '', 'inbox' => 'INBOX', 'archive' => 'P2BE_ARCHIVE');
     $connection_details = array_merge($defaults, $assoc_args);
     $connection_details = apply_filters('p2be_imap_connection_details', $connection_details);
     $ret = P2_By_Email()->extend->email_replies->ingest_emails($connection_details);
     if (is_wp_error($ret)) {
         WP_CLI::error($ret->get_error_message());
     } else {
         WP_CLI::success($ret);
     }
 }
 /**
  * Queue notifications for a comment
  */
 public function queue_comment_notifications($comment_id)
 {
     $comment = get_comment($comment_id);
     if (1 != $comment->comment_approved) {
         return;
     }
     $users = get_users();
     foreach ($users as $user) {
         if ($comment->user_id == $user->ID) {
             if (!apply_filters('p2be_emails_send_notif_to_author', false, 'comment', $user)) {
                 continue;
             }
         }
         $user_options = P2_By_Email()->extend->settings->get_user_notification_options($user->ID);
         if ('all' == $user_options['comments'] || 'yes' == $user_options['mentions'] && $this->is_user_mentioned($user, $comment->comment_content)) {
             $this->send_comment_notification($comment_id, $user);
         }
     }
 }
    public function user_profile_fields($user)
    {
        $user_options = $this->get_user_notification_options($user->ID);
        ?>
<h3>P2 By Email</h3>
	<table class="form-table">
		<tr>
			<th><label for="p2be-posts">Posts</label></th>
			<td>
				<select id="p2be-posts" name="p2be-posts">
				<?php 
        foreach (array('all' => 'Send me an email for every new post', 'none' => "Don't send me new post emails") as $key => $label) {
            ?>
					<option value="<?php 
            echo esc_attr($key);
            ?>
" <?php 
            selected($key, $user_options['posts']);
            ?>
><?php 
            echo esc_attr($label);
            ?>
</option>
				<?php 
        }
        ?>
				</select>
				<?php 
        if (P2_By_Email()->extend->email_replies->is_enabled()) {
            ?>
				<?php 
            $user_secret_email = apply_filters('p2be_emails_reply_to_email', '', 'user', $user->ID);
            ?>
				<p class="description">Tip: Create new posts by emailing this secret address: <a href="<?php 
            echo esc_url('mailto:' . $user_secret_email);
            ?>
"><?php 
            echo esc_html($user_secret_email);
            ?>
</a>
				<?php 
        }
        ?>
			</td>
		</tr>
		<tr>
			<th><label for="p2be-comments">Comments</label></th>
			<td>
				<select id="p2be-comments" name="p2be-comments">
				<?php 
        foreach (array('all' => 'Send me an email for every new comment', 'none' => "Don't send me new comment emails") as $key => $label) {
            ?>
					<option value="<?php 
            echo esc_attr($key);
            ?>
" <?php 
            selected($key, $user_options['comments']);
            ?>
><?php 
            echo esc_attr($label);
            ?>
</option>
				<?php 
        }
        ?>
				</select>
			</td>
		</tr>
		<tr>
			<th><label for="p2be-mentions">Mentions</label></th>
			<td>
				<select id="p2be-mentions" name="p2be-mentions">
				<?php 
        foreach (array('yes' => 'Make sure I get an email if someone @mentions my username', 'no' => "Respect my post and comment notification settings") as $key => $label) {
            ?>
					<option value="<?php 
            echo esc_attr($key);
            ?>
" <?php 
            selected($key, $user_options['mentions']);
            ?>
><?php 
            echo esc_attr($label);
            ?>
</option>
				<?php 
        }
        ?>
				</select>
			</td>
		</tr>
	</table>
<?php 
    }
            $sender = array_shift($headers->sender);
        }
        if (empty($sender->mailbox) || empty($sender->host)) {
            return new WP_Error('invalid-sender', 'Sender from headers are missing.');
        }
        return sanitize_email($sender->mailbox . '@' . $sender->host);
    }
    /**
     * Parse the reply from the email
     */
    private function get_reply_from_email($email)
    {
        $what_the_email = new What_The_Email($email);
        $message = quoted_printable_decode($what_the_email->get_reply());
        return $message;
    }
    /**
     * Get the email text body and/or attachments given an IMAP resource
     */
    private function get_body_from_connection($connection, $num, $type = 'text/plain')
    {
        // Hacky way to get the email body. We should support more MIME types in the future
        $body = imap_fetchbody($connection, $num, 1.1, FT_UID);
        if (empty($body)) {
            $body = imap_fetchbody($connection, $num, 1, FT_UID);
        }
        return $body;
    }
}
P2_By_Email()->extend->email_replies = new P2BE_Email_Replies();