asas-virtuais / asas-virtuais-wp
Library for coding WordPress plugins
This package is auto-updated.
Last update: 2021-09-17 15:29:19 UTC
README
This framework gained a lot of functionalities as time went by and since most plugins don't need all that I am starting to take things appart. I am making a new framework called asas-virtuais-wp-core which will have just the basic stuff for WP plugins, I also plan to move the generic PHP functions to a library called asas-virtuais-php.
Asas Virtuais WP Framework
Asas Virtuais means Virtual Wings.
Goals
- Gather code that is very common among Wordpress plugins.
Benefits
- Eliminates the practice of copying and pasting the same classes and methods from one plugin to the other.
- Keeps only what is specific to each plugin in the plugin.
Requiring the Framework:
composer require asas-virtuais/asas-virtuais-wp
Loading the Framework:
Make sure dependencies were properly installed and the framework is under vendor
1 - Require TakeOff.php
$loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/asas-virtuais/asas-virtuais-wp/TakeOff.php' );
2 - Call the fly method, passing the composer autoload and the plugin file path.
- It is better to do this under the plugins_loaded hook
$autoload = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' ); $framework_instance = $loader->fly( $autoload, __FILE__ );
Using the Framework
Once the fly method was called you can call for any function in the files under the lib directory, and the framework instance can be retrieved like in the examples below:
$framework_instnace = asas_virtuais( 'my-plugin-name' );
- my-plugin-name is always the basename of the main plugin file, without the extension of course.
The framework also has a default instance which can be used in certain cases.
$framework_default_instance = asas_virtuais();
Using the Manager classes
Beware of the args of manager constructors and the inner workings of each manager, some will require input to be instantiated and others will require that another manager is instantiated first.
Managers are not instantiated automatically, they will be instantiated as you use them.
The manager classes can be either extended or called directly from the framework instance.
Extending the a Manager
class MyAssetsManager extends \AsasVirtuaisWP\Assets\AssetsManager
The manager classes can be either extended or called directly from the framework instance.
$my_assets_manager = asas_virtuais('my-plugin-name')->assets_manager( $args );
Best Practices
- Only call the
$loader->fly
under the plugins_loaded hook. - Save your framework instance in a class variable instead of calling
asas_virtuais
multiple times. - Save your manager instances in a class variable instead of relying on the
asas_virtuais()->x_manager();
methods multiple times.
What happens behind the hood
1 - When you require TakeOff.php
- The class \AsasVirtuais\WP\Framework\TakeOff is instantiated if it hasn't been already. This class is only instnatiated once, regardless of how many plugins use the framework.
- TakeOff will register all the plugins that required the framework but will only load the latest version of the framework.
2 - When you call the method TakeOff->fly
- This will trigger the framework loading, assuming all plugins required TakeOff.php before the plugins_loaded hook and only called the Fly method after the plugins_loaded hook, the latest version available will be loaded.
- If a plugin calls the fly method before the plugins_loaded hook, the framework version on that plugin will be used regardless of other plugins having a more recent version of the framework.
Problems and Solutions
- The main problem we face is having the framework availalbe for multiple plugins but only loading it once. This problem is currently solved by using the TakeOff class/file to load the latest version of the framework registered.