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.
Short answer: no.
Long answer: also no. Actions don’t work that way.
Edit:
To elaborate and make your question totally generic:
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
gform_after_delete_form
(assuming you have less than a million forms!)
rough code:
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.com/gform_form_trash_link/