我正在开发一个与重力表单交互的插件,在某些情况下,我想防止表单被删除。以下是重力表单中处理删除表单的方法:
public static function delete_form($form_id){
global $wpdb;
if(!GFCommon::current_user_can_any("gravityforms_delete_forms"))
die(__("You don\'t have adequate permission to delete forms.", "gravityforms"));
do_action("gform_before_delete_form", $form_id);
$form_meta_table = self::get_meta_table_name();
$form_table = self::get_form_table_name();
//Deleting form Entries
self::delete_leads_by_form($form_id);
//Delete form meta
$sql = $wpdb->prepare("DELETE FROM $form_meta_table WHERE form_id=%d", $form_id);
$wpdb->query($sql);
//Deleting form Views
self::delete_views($form_id);
//Delete form
$sql = $wpdb->prepare("DELETE FROM $form_table WHERE id=%d", $form_id);
$wpdb->query($sql);
do_action("gform_after_delete_form", $form_id);
}
有可能钩住
gform_before_delete_form
然后执行一些操作,使delete\\u form()在该点返回,而不继续?例如。,
public function preventGravityFormDeletion()
{
if( $someCondition )
{
// do something that forces delete_form() to stop
}
}
add_action( \'gform_before_delete_form\', array( $this, \'preventGravityFormDeletion\' ) );
我知道我可以打电话
wp_die()
停止一切,但这不是很优雅。有更好的方法吗?由于范围的限制,这似乎不可能实现,但我想检查一下是否有一些我不知道的WP/PHP魔术。
SO网友:Jamie
一个创造性的解决方案是在gform\\U期间修改数据库中的实际表单id,然后再删除表单,这样就不会有任何后续操作修改表单。
然后,您可以在\\u delete\\u form之后挂接到gform\\u并修改form\\u id。
gform\\u before\\u delete\\u表单
form_id = form_id + 1000000
gform\\u after\\u delete\\u表单
form_id = form_id - 1000000
(假设您的表单少于一百万张!)
粗略代码:
public function preventGravityFormDeletion()
{
if( $someCondition )
{
global $wpdb;
$temp_form_id = 1000000+$form_id;
$sql = $wpdb->prepare("UPDATE $form_meta_table SET form_id = $temp_form_id WHERE form_id=$form_id");
$wpdb->query($sql);
$sql = $wpdb->prepare("UPDATE $form_table SET id = $temp_form_id WHERE id=$form_id");
$wpdb->query($sql);
}
}
add_action( \'gform_before_delete_form\', array( $this, \'preventGravityFormDeletion\' ) );
SO网友:Jeremy Caris
对于那些正在寻找的人,重力形态现在提供了以下解决方案:
add_filter( \'gform_form_trash_link\', \'prevent_form_deletion\', 10, 2 );
function prevent_form_deletion( $trash_link, $form_id ){ if ( $form_id == 75 ) { $trash_link = \'<a class="submitdelete" onclick="alert(\\\'This form cannot be deleted.\\\');" onkeypress="alert(\\\'This form cannot be deleted.\\\')">Remove this Form</a>\'; } return $trash_link; }
https://docs.gravityforms.com/gform_form_trash_link/