Please make classes static and accessible
-
First off: what a FANTASTIC plugin! It works like a charm and is truly beautifully coded!
However, I do have one request that will make the plugin even better. Can you please make all classes static and accessible outside of the plugin?
The short version is it’s impossible to make changes to any filters/actions that MMDB adds. For example: the plugin adds the
add_action( 'the_content', array($this, 'post_content_view_hook' ));
hook inMyMovieDatabase\PublicController
. If I need to remove the action and re-add it to a different hook or with a different priority, I need to pass a callable reference to the class.Since the class isn’t static and a reference to the class is never saved anywhere, there’s no way to just run a remove_action() or add_action() without creating an entirely new instance of
new MyMovieDatabase\MyMovieDatabase()
. Which can pose a ton of issues.This could easily be fixed if the plugin implemented a static approach, allowing other devs to for example get the instance of the MyMovieDatabase class by calling something like
MMDB()
, which would returnMyMovieDatabase::instance();
./** * Ensures only one instance of MyMovieDatabase is loaded or can be loaded. * * @return MyMovieDatabase - instance * @static */ public static function instance() { if ( is_null( self::$instance ) ) { self::$instance = new self(); } return self::$instance; }
Subclasses could also be saved and then referenced by either short functions like
/** * @return PublicController */ public function PublicController() { return PublicController::instance(); }
or an array that holds references to all subclass instances.
THE LONG VERSION: The reason this is important:
We’re using the AVADA theme. The way the theme is coded it actually callsthe_content
hook three times: once in the header (for their own header post type), once for the main content, and finally in the footer (for their own footer post type). This means that theadd_action( 'the_content', array($this, 'post_content_view_hook' ));
hook inMyMovieDatabase\PublicController
runs three times on the page, which not only renders 3 wrappers with identical IDs, it also ends up attaching the actual Vue-generated MMDB content to the first one, so in the header.To work around this, there is an additional action hook in Avada’s header and footer before the_content where filters added by various third-party developers (like BBPress or Jetpack) get removed. Then after the_content runs, there’s another action hook, where those filters get added back in. Of course, Avada only took into account really popular plugins and doesn’t include MMDB in that. The good news is I can manually tap into the pre and post action hooks and remove the
post_content_view_hook
added by MMDB, and then add it back in after the header/footer content has been generated. The only problem is there’s no way to reference theMyMovieDatabase\PublicController
class without creating a brand new one each time.Hope this explains the conundrum and how it can easily be fixed. Let me know your thoughts!
- The topic ‘Please make classes static and accessible’ is closed to new replies.