【问题标题】:Gravity Forms not performing variable replacement in notification sent from WebAPIGravity Forms 未在从 WebAPI 发送的通知中执行变量替换
【发布时间】:2015-04-07 13:54:17
【问题描述】:

我正在使用 Gravity 表单,并且我已经让它发出自动通知。此通知发生在我的 android 应用程序连接到它并添加一个条目之后。 我的问题是它不执行变量替换。

例如,表单编辑器中的 Subject 定义为 来自 {Full Name:2}

的新搬家应用程序提交

但电子邮件最终以 来自

的新搬家应用程序提交

电子邮件中的所有表单变量都被替换为任何内容。但如果我在网站上查看条目,它就在那里。

我已将代码添加到 Gravity Forms WebApi 中的 post_entries 函数中,如下所示:

public function post_entries($data, $form_id = null) {

    $this->authorize('gravityforms_edit_entries');

    $result = GFAPI::add_entries($data, $form_id);

    if (is_wp_error($result)) {
        $response = $this->get_error_response($result);
        $status = $this->get_error_status($result);
    } else {
        $status = 201;
        $response = $result;

        // This is the form object from Gravity Forms.
        $form = \GFAPI::get_form($form_id);

        $event = 'form_submission';

        $notifications = GFCommon::get_notifications_to_send($event, $form, $lead[0]);
        $notifications_to_send = array();

        //running through filters that disable form submission notifications
        foreach ($notifications as $notification) {
            if (apply_filters("gform_disable_notification_{$form['id']}", apply_filters('gform_disable_notification', false, $notification, $form, $lead), $notification, $form, $lead)) {
                //skip notifications if it has been disabled by a hook
                continue;
            }

            $notifications_to_send[] = $notification['id'];
        }

        GFCommon::send_notifications($notifications_to_send, $form, $lead, true, $event);
    }

    $this->end($status, $response);
}

编辑:这是在 Naomi 的帮助下的工作代码

public function post_entries( $data, $form_id = null ) {

    $this->authorize( 'gravityforms_edit_entries' );
    $result = GFAPI::add_entries( $data, $form_id );

    if ( is_wp_error( $result ) ) {
        $response = $this->get_error_response( $result );
        $status   = $this->get_error_status( $result );
    } else {
        $status   = 201;
        $response = $result;

       $lead = \GFAPI::get_entry($result);
       // This is the form object from Gravity Forms.
       $form = \GFAPI::get_form($form_id);
       $event ='form_submission';

        $notifications         = GFCommon::get_notifications_to_send( $event, $form, $lead );
        $notifications_to_send = array();

        //running through filters that disable form submission notifications
        foreach ( $notifications as $notification ) {
            if ( apply_filters( "gform_disable_notification_{$form['id']}", apply_filters( 'gform_disable_notification', false, $notification, $form, $lead ), $notification, $form, $lead ) ) {
                //skip notifications if it has been disabled by a hook
                continue;
            }
            $notifications_to_send[] = $notification['id'];
        }
        GFCommon::send_notifications( $notifications_to_send, $form, $lead, true, $event );
    }

    $this->end( $status, $response );
}

【问题讨论】:

    标签: notifications asp.net-web-api gravity-forms-plugin


    【解决方案1】:

    在您的代码中,您发送给通知函数的 $lead 变量尚未在任何地方设置(您只是凭空取出它 :-))。这个变量需要保存一个重力形式条目对象,然后通知函数将能够使用它来替换变量。

    来自 GFAPI::add_entries 的 $result 将为您提供一组条目 ID,您可以使用 GFAPI::get_entry 函数从条目 ID 中获取条目对象。

    【讨论】:

    • $notifications = GFCommon::get_notifications_to_send($event, $form, $result[0]);
    • 这里的新评论不允许我编辑:谢谢你看到,我的坏!,这是有道理的。我的变量应该是 $result[0] ,所以该行应该是: $notifications = GFCommon::get_notifications_to_send($event, $form, $result[0]);我将尝试使用 GFAPI::get_entry 方法来获取入口对象。谢谢!
    • 非常感谢!我欠你 1!,这是代码,以防其他人需要这样做:
    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2018-09-11
    • 2020-09-06
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2021-05-03
    • 2012-12-14
    相关资源
    最近更新 更多