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.

3 s
3

Short 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.

Leave a Reply

Your email address will not be published. Required fields are marked *