Gravity Forms 不在从 WebAPI 发送的通知中执行变量替换
Gravity Forms not performing variable replacement in notification sent from WebAPI
我正在使用 Gravity forms,并且它会发出自动通知。此通知在我的 android 应用程序连接到它并添加条目后发生。
我的问题是它不执行变量替换。
例如 Subject 在表单编辑器中定义为
来自{完整 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 );
}
在你的代码中,你发送给通知函数的 $lead 变量没有在任何地方设置(你只是凭空把它拉出来:-))。这个变量需要保存一个重力形式入口对象,然后通知函数可以使用它来替换变量。
您从 GFAPI::add_entries 获得的 $result 将为您提供一个条目 ID 数组,您可以使用 GFAPI::get_entry 函数从条目 ID 获取条目对象。
我正在使用 Gravity forms,并且它会发出自动通知。此通知在我的 android 应用程序连接到它并添加条目后发生。 我的问题是它不执行变量替换。
例如 Subject 在表单编辑器中定义为 来自{完整 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 );
}
在你的代码中,你发送给通知函数的 $lead 变量没有在任何地方设置(你只是凭空把它拉出来:-))。这个变量需要保存一个重力形式入口对象,然后通知函数可以使用它来替换变量。
您从 GFAPI::add_entries 获得的 $result 将为您提供一个条目 ID 数组,您可以使用 GFAPI::get_entry 函数从条目 ID 获取条目对象。