在第三方API响应后发送Gravit Forms通知

时间:2018-09-14 作者:Tyler Robinson

我有一个重力表单,在提交时将数据发送给第三方API。它与以下代码连接并通过API保存数据。然而,我现在正试图弄清楚,只有当API响应返回为失败或状态代码201以外的任何内容时,才如何发送管理员通知电子邮件。

add_action( \'gform_after_submission_1\', \'post_to_third_party\', 10, 2 );
function post_to_third_party( $entry, $form ) {

    $post_url = \'http://thirdpartyapiurl.com\';

    $headers = array(
      \'Cache-Control\' => \'no-cache\',
      \'session-id\' => \'123456789101112\'
    );

    $body = \'{
      "Sales_Doc_Type": "QUOTE", 
      "Sales_Doc_ID": "CGRV",  
      "Sales_Batch": "WEB QUOTES",  
      "Customer_Num": "00",  
      "Customer_Name": "\'.rgar( $entry, \'1\' ).\'", 
      "Price_Level": "SHOP 3",  
      "Shipping_Method": "GROUND FREIGHT",  
      "Tax_Schedule": "TBD",  
      "Warehouse_Code": "CG1",  
      "Sales_Person_ID": "HOUSE ACCOUNT",  
      "Sales_Territory": "RV1",  
      "Payment_Terms": "PREPAYMENT",  
      "Created_By": "API USER",
      "Web_Site": "", 
      "UserFieldData": [
        "WEB FORM",
        "CERT FUNDS",
        "\'.rgar( $entry, \'1\' ).\'",
        "\'.rgar( $entry, \'98.1\' ).\'",
        "\'.rgar( $entry, \'98.2\' ).\'",
        "\'.rgar( $entry, \'98.3\' ).\'",
        "\'.rgar( $entry, \'98.4\' ).\'",
        "\'.rgar( $entry, \'98.5\' ).\'",
        "\'.rgar( $entry, \'98.6\' ).\'",
        "\'.rgar( $entry, \'98.7\' ).\'",
        "\'.rgar( $entry, \'3\' ).\'",
        "\'.rgar( $entry, \'6\' ).\'",  
        "\'.rgar( $entry, \'8\' ).\'",
        "\'.rgar( $entry, \'9\' ).\'",
        "\'.rgar( $entry, \'11.1\' ).\'",  
        "\'.rgar( $entry, \'99.1\' ).\'",
        "\'.rgar( $entry, \'99.2\' ).\'",
        "\'.rgar( $entry, \'99.3\' ).\'",
        "\'.rgar( $entry, \'99.4\' ).\'",
        "\'.rgar( $entry, \'99.5\' ).\'",
        "\'.rgar( $entry, \'99.6\' ).\'",
        "\'.rgar( $entry, \'99.7\' ).\'",
        "\'.rgar( $entry, \'16\' ).\'",
        "\'.rgar( $entry, \'18\' ).\'",
        "\'.rgar( $entry, \'19\' ).\'",
        "\'.rgar( $entry, \'20\' ).\'",
        "\'.rgar( $entry, \'22\' ).\'",
        "\'.rgar( $entry, \'90\' ).\'",
        "\'.rgar( $entry, \'24\' ).\'",
        "\'.rgar( $entry, \'25\' ).\'",
        "\'.rgar( $entry, \'94\' ).\'",
        "\'.rgar( $entry, \'27\' ).\'",
        "\'.rgar( $entry, \'28\' ).\'",
        "\'.rgar( $entry, \'93\' ).\'",
        "\'.rgar( $entry, \'95\' ).\'",
        "\'.rgar( $entry, \'51\' ).\'",
        "\'.rgar( $entry, \'115\' ).\'",
        "\'.rgar( $entry, \'77\' ).\'",
        "\'.rgar( $entry, \'76\' ).\'",
        "\'.rgar( $entry, \'49\' ).\'",  
        "",  
        "\'.rgar( $entry, \'114\' ).\'",  
        "\'.rgar( $entry, \'50\' ).\'",
        "\'.rgar( $entry, \'69\' ).\'",
        "\'.rgar( $entry, \'70\' ).\'",
        "\'.rgar( $entry, \'73\' ).\'",
        "\'.rgar( $entry, \'55\' ).\'",
        "\'.rgar( $entry, \'56\' ).\'",
        "\'.rgar( $entry, \'46\' ).\'",
        "\'.rgar( $entry, \'48\' ).\'",
        "\'.rgar( $entry, \'57\' ).\'",
        "\'.rgar( $entry, \'58\' ).\'",
        "\'.rgar( $entry, \'62\' ).\'",  
        "\'.rgar( $entry, \'64\' ).\'",
        "\'.rgar( $entry, \'65\' ).\'",
        "\'.rgar( $entry, \'66\' ).\'",
        "\'.rgar( $entry, \'80\' ).\'",
        "\'.base64_encode(rgar( $entry, \'82\' )).\'",
        "\'.base64_encode(rgar( $entry, \'85\' )).\'",
        "\'.base64_encode(rgar( $entry, \'86\' )).\'",
        "\'.base64_encode(rgar( $entry, \'89\' )).\'",
        "\'.base64_encode(rgar( $entry, \'88\' )).\'",
        "\'.base64_encode(rgar( $entry, \'87\' )).\'",
        "\'.rgar( $entry, \'91\' ).\'",  
        ""
      ],
      "UserFieldNames": [
        "CGRV_Sales_Originating_Source",
        "CGRV_Payment_Type",
        "CGRV_QTE_Contact",
        "JAJ_Ship_Address_Line_1",
        "JAJ_Ship_Address_Line_2",
        "JAJ_Ship_Address_Line_3",
        "JAJ_Ship_City",
        "JAJ_Ship_State",
        "JAJ_Ship_Zip",
        "JAJ_Ship_Country",
        "JAJ_Ship_Phone_1",
        "JAJ_Ship_Email_To",
        "JAJ_Ship_Contact_Person",
        "JAJ_Customer_Website",
        "JAJ_Residential",
        "JAJ_Bill_Address_Line_1",
        "JAJ_Bill_Address_Line_2",
        "JAJ_Bill_Address_Line_3",
        "JAJ_Bill_City",
        "JAJ_Bill_State",
        "JAJ_Bill_Zip",
        "JAJ_Bill_Country",
        "JAJ_Bill_Phone_1",
        "JAJ_Bill_Fax",
        "JAJ_Bill_Email_To",
        "JAJ_Bill_Contact_Person",
        "CGRV_QTE_Application_Inquiry",
        "CGRV_QTE_Year",
        "CGRV_QTE_Make",
        "CGRV_QTE_Model",
        "CGRV_QTE_Part_Number",
        "CGRV_QTE_Qty_Needed",
        "CGRV_QTE_SideNeeded",
        "JAJ_VIN",
        "CGRV_QTE_WshldNums",
        "CGRV_QTE_Wshld_Mfg",
        "CGRV_QTE_DOT_Number",
        "CGRV_QTE_Height",
        "CGRV_QTE_Width",
        "CGRV_QTE_Ceramicv1",
        "",
        "CGRV_QTE_Color",
        "CGRV_QTE_Installation",
        "CGRV_QTE_SG_Window_Side",
        "CGRV_QTE_SG_Window_Loc",
        "CGRV_QTE_SG_Quote_Needed_For",
        "CGRV_QTE_SG_Frame_Color",
        "CGRV_QTE_SG_Glass_Color",
        "CGRV_QTE_SG_Frame_Height",
        "CGRV_QTE_SG_Frame_Width",
        "CGRV_QTE_SG_Glass_Height",
        "CGRV_QTE_SG_Glass_Width",
        "CGRV_QTE_SG_Emergency_Exit",
        "CGRV_QTE_SG_Window_Movement",
        "CGRV_QTE_SG_Glass_Pane",
        "CGRV_QTE_SG_Frame_Corners",
        "CGRV_QTE_SG_Additional_Info",
        "JAJ_File_Attachment_01",
        "JAJ_File_Attachment_02",
        "JAJ_File_Attachment_03",
        "JAJ_File_Attachment_04",
        "JAJ_File_Attachment_05",
        "JAJ_File_Attachment_06",
        "JAJ_Referred_By",
        "JAJ_Referred_By_Other_Defined"
      ]
    }\';
    GFCommon::log_debug( \'gform_after_submission: body => \' . print_r( $body, true ) );
    $request = new WP_Http();
    $response = $request->post( $post_url, array( \'headers\' => $headers, \'body\' => $body ) );
    GFCommon::log_debug( \'gform_after_submission: response => \' . print_r( $response, true ) );
}
我还有一个Gravity表单插件,我一直在开发它,它在通知区域有一个选项,可以选择“第三方”api并控制电子邮件通知,而不是Gravity表单自动发送。我只是不知道如何根据API的响应来控制管理员通知电子邮件。

GFForms::include_addon_framework();

class GFApiNotificationAddOn extends GFAddOn {

    protected $_version = GF_API_NOTIFICATION_ADDON_VERSION;
    protected $_min_gravityforms_version = \'1.9\';
    protected $_slug = \'apinotificationaddon\';
    protected $_path = \'apinotificationaddon/apinotificationaddon.php\';
    protected $_full_path = __FILE__;
    protected $_title = \'Gravity Forms Admin Notification Add-On\';
    protected $_short_title = \'Admin Notification Add-On\';

    /**
     * @var object $_instance If available, contains an instance of this class.
     */
    private static $_instance = null;

    /**
     * Returns an instance of this class, and stores it in the $_instance property.
     *
     * @return object $_instance An instance of this class.
     */
    public static function get_instance() {
        if ( self::$_instance == null ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * Plugin starting point. Handles hooks, loading of language files and PayPal delayed payment support.
     */
    public function init() {

        parent::init();

        add_filter( \'gform_notification_services\', array( $this, \'add_notification_service\' ) );
        add_filter( \'gform_pre_send_email\', array( $this, \'api_fail_send_email\' ), 19, 3 );

    }

    /**
     * Add the new notification service.
     *
     * @param array $services The notification services.
     *
     * @return array
     */
    public function add_notification_service( $services ) {


        $services[\'api_fail_email\'] = array(
            \'label\' => esc_html__( \'Third Party API Notice\', \'apinotificationaddon\' ),
            \'image\' => $this->get_base_url() . \'/images/icon.png\',
        );

        return $services;
    }

    public function api_fail_send_email( $email, $message_format, $notification ) {
        global $response;

    // If the notification is not assigned to this service, return the email.
        if ( rgar( $notification, \'service\' ) !== \'api_fail_email\' ) {
            return $email;
        }

        // If the email has already been aborted, return the email.
        if ( $email[\'abort_email\'] ) {
            $this->log_debug( __METHOD__ . \'(): Not sending email because the notification has already been aborted by another Add-On.\' );

            return $email;
        }


        $result = $this->send_email( $email, $message_format, $notification );


        if ( $result ) {
            // The service successfully sent the email; prevent WordPress and other add-ons from also sending the email.
            $email[\'abort_email\'] = true;
        }

        return $email;
    }

    /**
     * Send the email via the new service.
     *
     * @param array $email The email properties.
     * @param string $message_format The message format, html or text.
     * @param array $notification The Notification object which produced the current email.
     *
     * @return bool
     */
    public function send_email( $email, $message_format, $notification ) {

        // pass the email to the service

        return $result;
    }

    public function init_admin() {
        parent::init_admin();

        add_filter( \'gform_tooltips\', array( $this, \'tooltips\' ) );
        add_action( \'gform_field_appearance_settings\', array( $this, \'field_appearance_settings\' ), 10, 2 );
    }


    // # SCRIPTS & STYLES -----------------------------------------------------------------------------------------------

    /**
     * Include my_script.js when the form contains a \'simple\' type field.
     *
     * @return array
     */
    public function scripts() {
        $scripts = array(
            array(
                \'handle\'  => \'my_script_js\',
                \'src\'     => $this->get_base_url() . \'/js/my_script.js\',
                \'version\' => $this->_version,
                \'deps\'    => array( \'jquery\' ),
                \'enqueue\' => array(
                    array( \'field_types\' => array( \'simple\' ) ),
                ),
            ),

        );

        return array_merge( parent::scripts(), $scripts );
    }

    /**
     * Include my_styles.css when the form contains a \'simple\' type field.
     *
     * @return array
     */
    public function styles() {
        $styles = array(
            array(
                \'handle\'  => \'my_styles_css\',
                \'src\'     => $this->get_base_url() . \'/css/my_styles.css\',
                \'version\' => $this->_version,
                \'enqueue\' => array(
                    array( \'field_types\' => array( \'simple\' ) )
                )
            )
        );

        return array_merge( parent::styles(), $styles );
    }

}
提交后,我可以使用以下代码获取响应代码

$status = wp_remote_retrieve_response_code( $response );

2 个回复
最合适的回答,由SO网友:Tyler Robinson 整理而成

对于那些可能也在寻找这个的人,我有这个答案。

add_action( \'gform_after_submission_1\', \'post_to_third_party\', 10, 2 );
function post_to_third_party( $entry, $form ) {

    $post_url = \'http://thirdpartyapiurl.com\';

    $headers = array(
      \'Cache-Control\' => \'no-cache\',
      \'session-id\' => \'123456789101112\'
    );

    $body = \'{
      "Sales_Doc_Type": "QUOTE", 
      "Sales_Doc_ID": "CGRV",  
      "Sales_Batch": "WEB QUOTES",  
      "Customer_Num": "00",  
      "Customer_Name": "\'.rgar( $entry, \'1\' ).\'", 
      "Price_Level": "SHOP 3",  
      "Shipping_Method": "GROUND FREIGHT",  
      "Tax_Schedule": "TBD",  
      "Warehouse_Code": "CG1",  
      "Sales_Person_ID": "HOUSE ACCOUNT",  
      "Sales_Territory": "RV1",  
      "Payment_Terms": "PREPAYMENT",  
      "Created_By": "API USER",
      "Web_Site": "", 
      "UserFieldData": [
        "WEB FORM",
        "CERT FUNDS",
        "\'.rgar( $entry, \'1\' ).\'",
        "\'.rgar( $entry, \'98.1\' ).\'",
        "\'.rgar( $entry, \'98.2\' ).\'",
        "\'.rgar( $entry, \'98.3\' ).\'",
        "\'.rgar( $entry, \'98.4\' ).\'",
        "\'.rgar( $entry, \'98.5\' ).\'",
        "\'.rgar( $entry, \'98.6\' ).\'",
        "\'.rgar( $entry, \'98.7\' ).\'",
        "\'.rgar( $entry, \'3\' ).\'",
        "\'.rgar( $entry, \'6\' ).\'",  
        "\'.rgar( $entry, \'8\' ).\'",
        "\'.rgar( $entry, \'9\' ).\'",
        "\'.rgar( $entry, \'11.1\' ).\'",  
        "\'.rgar( $entry, \'99.1\' ).\'",
        "\'.rgar( $entry, \'99.2\' ).\'",
        "\'.rgar( $entry, \'99.3\' ).\'",
        "\'.rgar( $entry, \'99.4\' ).\'",
        "\'.rgar( $entry, \'99.5\' ).\'",
        "\'.rgar( $entry, \'99.6\' ).\'",
        "\'.rgar( $entry, \'99.7\' ).\'",
        "\'.rgar( $entry, \'16\' ).\'",
        "\'.rgar( $entry, \'18\' ).\'",
        "\'.rgar( $entry, \'19\' ).\'",
        "\'.rgar( $entry, \'20\' ).\'",
        "\'.rgar( $entry, \'22\' ).\'",
        "\'.rgar( $entry, \'90\' ).\'",
        "\'.rgar( $entry, \'24\' ).\'",
        "\'.rgar( $entry, \'25\' ).\'",
        "\'.rgar( $entry, \'94\' ).\'",
        "\'.rgar( $entry, \'27\' ).\'",
        "\'.rgar( $entry, \'28\' ).\'",
        "\'.rgar( $entry, \'93\' ).\'",
        "\'.rgar( $entry, \'95\' ).\'",
        "\'.rgar( $entry, \'51\' ).\'",
        "\'.rgar( $entry, \'115\' ).\'",
        "\'.rgar( $entry, \'77\' ).\'",
        "\'.rgar( $entry, \'76\' ).\'",
        "\'.rgar( $entry, \'49\' ).\'",  
        "",  
        "\'.rgar( $entry, \'114\' ).\'",  
        "\'.rgar( $entry, \'50\' ).\'",
        "\'.rgar( $entry, \'69\' ).\'",
        "\'.rgar( $entry, \'70\' ).\'",
        "\'.rgar( $entry, \'73\' ).\'",
        "\'.rgar( $entry, \'55\' ).\'",
        "\'.rgar( $entry, \'56\' ).\'",
        "\'.rgar( $entry, \'46\' ).\'",
        "\'.rgar( $entry, \'48\' ).\'",
        "\'.rgar( $entry, \'57\' ).\'",
        "\'.rgar( $entry, \'58\' ).\'",
        "\'.rgar( $entry, \'62\' ).\'",  
        "\'.rgar( $entry, \'64\' ).\'",
        "\'.rgar( $entry, \'65\' ).\'",
        "\'.rgar( $entry, \'66\' ).\'",
        "\'.rgar( $entry, \'80\' ).\'",
        "\'.base64_encode(rgar( $entry, \'82\' )).\'",
        "\'.base64_encode(rgar( $entry, \'85\' )).\'",
        "\'.base64_encode(rgar( $entry, \'86\' )).\'",
        "\'.base64_encode(rgar( $entry, \'89\' )).\'",
        "\'.base64_encode(rgar( $entry, \'88\' )).\'",
        "\'.base64_encode(rgar( $entry, \'87\' )).\'",
        "\'.rgar( $entry, \'91\' ).\'",  
        ""
      ],
      "UserFieldNames": [
        "CGRV_Sales_Originating_Source",
        "CGRV_Payment_Type",
        "CGRV_QTE_Contact",
        "JAJ_Ship_Address_Line_1",
        "JAJ_Ship_Address_Line_2",
        "JAJ_Ship_Address_Line_3",
        "JAJ_Ship_City",
        "JAJ_Ship_State",
        "JAJ_Ship_Zip",
        "JAJ_Ship_Country",
        "JAJ_Ship_Phone_1",
        "JAJ_Ship_Email_To",
        "JAJ_Ship_Contact_Person",
        "JAJ_Customer_Website",
        "JAJ_Residential",
        "JAJ_Bill_Address_Line_1",
        "JAJ_Bill_Address_Line_2",
        "JAJ_Bill_Address_Line_3",
        "JAJ_Bill_City",
        "JAJ_Bill_State",
        "JAJ_Bill_Zip",
        "JAJ_Bill_Country",
        "JAJ_Bill_Phone_1",
        "JAJ_Bill_Fax",
        "JAJ_Bill_Email_To",
        "JAJ_Bill_Contact_Person",
        "CGRV_QTE_Application_Inquiry",
        "CGRV_QTE_Year",
        "CGRV_QTE_Make",
        "CGRV_QTE_Model",
        "CGRV_QTE_Part_Number",
        "CGRV_QTE_Qty_Needed",
        "CGRV_QTE_SideNeeded",
        "JAJ_VIN",
        "CGRV_QTE_WshldNums",
        "CGRV_QTE_Wshld_Mfg",
        "CGRV_QTE_DOT_Number",
        "CGRV_QTE_Height",
        "CGRV_QTE_Width",
        "CGRV_QTE_Ceramicv1",
        "",
        "CGRV_QTE_Color",
        "CGRV_QTE_Installation",
        "CGRV_QTE_SG_Window_Side",
        "CGRV_QTE_SG_Window_Loc",
        "CGRV_QTE_SG_Quote_Needed_For",
        "CGRV_QTE_SG_Frame_Color",
        "CGRV_QTE_SG_Glass_Color",
        "CGRV_QTE_SG_Frame_Height",
        "CGRV_QTE_SG_Frame_Width",
        "CGRV_QTE_SG_Glass_Height",
        "CGRV_QTE_SG_Glass_Width",
        "CGRV_QTE_SG_Emergency_Exit",
        "CGRV_QTE_SG_Window_Movement",
        "CGRV_QTE_SG_Glass_Pane",
        "CGRV_QTE_SG_Frame_Corners",
        "CGRV_QTE_SG_Additional_Info",
        "JAJ_File_Attachment_01",
        "JAJ_File_Attachment_02",
        "JAJ_File_Attachment_03",
        "JAJ_File_Attachment_04",
        "JAJ_File_Attachment_05",
        "JAJ_File_Attachment_06",
        "JAJ_Referred_By",
        "JAJ_Referred_By_Other_Defined"
      ]
    }\';
    GFCommon::log_debug( \'gform_after_submission: body => \' . print_r( $body, true ) );
    $request = new WP_Http();
    $response = $request->post( $post_url, array( \'headers\' => $headers, \'body\' => $body ) );
    GFCommon::log_debug( \'gform_after_submission: response => \' . print_r( $response, true ) );

    $code = wp_remote_retrieve_response_code($response);
    $apiresults = wp_remote_retrieve_body($response);

    if($code != 201)
        GFAPI::send_notifications( $form, $entry, \'api_connection_fail\', $data );

    $data = array(\'webhook\' => array(
    \'status\' => $code,
    \'results\' => print_r( $response, true )
    ));

    GFAPI::send_notifications( $form, $entry, \'api_connection_fail\', $data );
}

add_filter( \'gform_notification_events\', function( $notification_events ) {
    $notification_events[\'api_connection_fail\'] = \'API Connection Failed\';
    return $notification_events;
} );

SO网友:Hans

我想gform_after_submission 是在这种情况下使用的错误操作。你看过吗gform_pre_send_email? 它有一个abort_email 参数,这可能很有用。

结束