admin管理员组文章数量:1305918
I'm working on a plugin that interacts with Gravity Forms, and under certain conditions I want to prevent a form from being deleted. Here's the method in Gravity Forms that handles deleting a form:
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);
}
Is it possible to hook into gform_before_delete_form
and then do something to make delete_form() return at that point without continuing? e.g.,
public function preventGravityFormDeletion()
{
if( $someCondition )
{
// do something that forces delete_form() to stop
}
}
add_action( 'gform_before_delete_form', array( $this, 'preventGravityFormDeletion' ) );
I know that I could call wp_die()
and just stop everything, but that's not very elegant. Is there a better way? It doesn't seem possible because of scope limitations, but I wanted to check in case there's some WP/PHP magic that I'm not aware of.
I'm working on a plugin that interacts with Gravity Forms, and under certain conditions I want to prevent a form from being deleted. Here's the method in Gravity Forms that handles deleting a form:
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);
}
Is it possible to hook into gform_before_delete_form
and then do something to make delete_form() return at that point without continuing? e.g.,
public function preventGravityFormDeletion()
{
if( $someCondition )
{
// do something that forces delete_form() to stop
}
}
add_action( 'gform_before_delete_form', array( $this, 'preventGravityFormDeletion' ) );
I know that I could call wp_die()
and just stop everything, but that's not very elegant. Is there a better way? It doesn't seem possible because of scope limitations, but I wanted to check in case there's some WP/PHP magic that I'm not aware of.
- Would this question be better-asked at the Gravity Forms support forum? Or is the GF part just to give context to the question regarding removing/preventing actions from firing? – Chip Bennett Commented Feb 10, 2012 at 14:42
- Yeah, it was just to give a concrete example, but I think the question could apply to lots of other situations as well. – Ian Dunn Commented Feb 10, 2012 at 15:20
3 Answers
Reset to default 4Short answer: no.
Long answer: also no. Actions don't work that way.
Edit:
To elaborate and make your question totally generic:
function foo() {
bar();
return 1;
}
function bar() {
// stuff
}
There is nothing you can put in stuff that will prevent a call to foo()
from returning 1, other than halting script execution entirely with die or exit.
Note: Throwing exceptions won't help either, because this has the same effect as halting script execution unless some previous caller catches the exeception.
Now, if this function is in a class somewhere, then you can define a subclass of it and replace this function with one of your own, then potentially use that, thus modifying the way this function works. That's about the best you can do, since PHP doesn't have any sort of aspect-oriented-programming mechanisms in it that would allow you to change function behavior at runtime.
A creative solution would be to modify the actual form_id in the database during gform_before_delete_form so none of the proceeding actions will modify the form.
Then you can hook into gform_after_delete_form and modify the form_id back.
gform_before_delete_form
form_id = form_id + 1000000
gform_after_delete_form
form_id = form_id - 1000000
(assuming you have less than a million forms!)
rough code:
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' ) );
For those looking, Gravity Forms now provides this solution:
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/gform_form_trash_link/
本文标签: plugin gravity formsCan an action callback prevent the parent from continuing execution
版权声明:本文标题:plugin gravity forms - Can an action callback prevent the parent from continuing execution? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741777249a2397097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论