advancedforms / advanced-forms

WordPress plugin to create forms using Advanced Custom Fields
75 stars 14 forks source link

Bug: Unable to access classes after they've been instantiated #27

Closed angusdowling closed 6 years ago

angusdowling commented 6 years ago

Hi @Fabianlindfors , loving the plugin so far. Great alternative to the bigger names like CF7.

Summary

Several classes are being declared in external files, and then included in the AF class, under the initialize_plugin function.

Unfortunately, these classes are being instantiated without being assigned to any variables.

This means that it's impossible without a large amount of hacking WP global variables, to remove any default filters or actions that are applied by these classes.

These classes are:

Steps to Reproduce

Add to anywhere in functions.php remove_action( 'init', array( 'AF_Core_Forms', 'pre_form' ), 10 );

Expected behavior

Action is removed.

Actual behavior

Action is not removed.

Fix

Whenever a class is instantiated, return it, like this: return new AF_Core_Forms();

Inside the AF class assign it to a variable, like this: $this->core_forms = include( $this->path . 'core/core-forms.php' );

That way, you can remove an action like this: remove_action('init', array(AF()->core_forms, 'pre_form'), 10);

I'm happy to create a pull request that reflects this.

Version: 1.3.5

fabianlindfors commented 6 years ago

Hi!

This is a great suggestion and I really like your solution. I know WooCommerce does something similar for some classes but they use an array instead of instance variables. Would you prefer a solution with instance variables (like your proposed solution) or one with a single array (something like `AF()->classes['core_forms'])?

Thank you!

angusdowling commented 6 years ago

Good idea! an array sounds like a much cleaner implementation. I would be happy to go with that.

fabianlindfors commented 6 years ago

Wonderful! I've pushed a commit which implements your suggestions, 86c58ebd027c257c3c598b67e0f7c7db999a2dc9. All the classes should be available through AF()->classes.

Hope this works! Thanks for your help.