BP_Reply_By_Email_Parser::init PHP Method

init() public static method

Returns an array of the parsed item on success. WP_Error object on failure. For parameters, see constructor.
public static init ( $args = [], $i = 1 ) : array | object
return array | object Array of the parsed item on success. WP_Error object on failure.
    public static function init($args = array(), $i = 1)
    {
        /**
         * Hook to allow plugins to do something before parser begins.
         *
         * @since 1.0-RC4
         */
        do_action('bp_rbe_before_parser');
        $instance = new self($args, $i);
        // Email header check ******************************************
        if (empty(self::$headers)) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, false, 'no_headers' );
            return new WP_Error('no_headers');
        }
        bp_rbe_log('Message #' . $i . ': email headers successfully parsed');
        // Querystring check *******************************************
        if (empty(self::$querystring)) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, self::$headers, 'no_address_tag' );
            return new WP_Error('no_address_tag', '', $args);
        }
        bp_rbe_log('Message #' . $i . ': address tag successfully parsed');
        // User check **************************************************
        if (empty(self::$user)) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, self::$headers, 'no_user_id' );
            return new WP_Error('no_user_id', '', $args);
        }
        // Spammer check ***********************************************
        $is_spammer = false;
        // Multisite spammer check
        if (!empty(self::$user->spam)) {
            $is_spammer = true;
        }
        // Single site spammer check
        if (1 == self::$user->user_status) {
            $is_spammer = true;
        }
        if ($is_spammer) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_is_spammer' );
            return new WP_Error('user_is_spammer', '', $args);
        }
        // Parameters parser *******************************************
        // Check if we're posting a new item or not
        $params = self::get_parameters();
        if (!$params) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'no_params' );
            return new WP_Error('no_params', '', $args);
        }
        bp_rbe_log('Message #' . $i . ': params = ' . print_r($params, true));
        // Email body parser *******************************************
        self::$content = self::get_body(self::$content, self::$is_html, !self::is_new_item(), $i);
        // If there's no email body and this is a reply, stop!
        if (!self::$content && !self::is_new_item()) {
            //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'no_reply_body' );
            return new WP_Error('no_reply_body', '', $args);
        }
        // log the body for replied items
        if (!self::is_new_item()) {
            bp_rbe_log('Message #' . $i . ': body contents - ' . self::$content);
        }
        // Parsing completed! ******************************************
        $data = array('headers' => self::$headers, 'content' => self::$content, 'subject' => self::$subject, 'user_id' => self::$user->ID, 'is_html' => self::$is_html, 'i' => $i);
        // plugins should use the following hook to do their posting routine
        $retval = apply_filters('bp_rbe_parse_completed', true, $data, $params);
        // clean up after ourselves
        self::clear_properties();
        return $retval;
    }

Usage Example

 /**
  * Webhook parser class method for SendGrid.
  */
 public function webhook_parser()
 {
     if (empty($_SERVER['HTTP_USER_AGENT']) || !empty($_SERVER['HTTP_USER_AGENT']) && 0 !== strpos($_SERVER['HTTP_USER_AGENT'], 'SendGrid')) {
         return;
     }
     if (empty($_POST) || empty($_POST['headers'])) {
         return;
     }
     bp_rbe_log('- SendGrid webhook received -');
     // Format email headers to fit RBE spec.
     $temp = explode("\n", $_POST['headers']);
     $headers = array();
     foreach ($temp as $line) {
         $colun = strpos($line, ':');
         if (false === $colun) {
             continue;
         }
         $key = substr($line, 0, $colun);
         $headers[$key] = stripslashes(trim(substr($line, $colun + 1)));
     }
     $data = array('headers' => $headers, 'to_email' => BP_Reply_By_Email_Parser::get_header($headers, 'To'), 'from_email' => BP_Reply_By_Email_Parser::get_header($headers, 'From'), 'content' => $_POST['text'], 'subject' => $_POST['subject']);
     $parser = BP_Reply_By_Email_Parser::init($data, 1);
     if (is_wp_error($parser)) {
         do_action('bp_rbe_no_match', $parser, $data, 1, false);
     }
     bp_rbe_log('- Webhook parsing completed -');
     die;
 }
All Usage Examples Of BP_Reply_By_Email_Parser::init