dev-master
9999999-dev http://www.waga.itAn helper component for Wordpress plugin development
GPL-2.0+
The Requires
by Waga Team
An helper component for Wordpress plugin development
Plugins Framework component is an set of tools and extensible classes to help WordPress plugins developers., (*1)
It supports a "convention over configuration" principle to keep the environment clean and maintainable., (*2)
You start the development of a new plugin either by extending BasePlugin or TemplatePlugin., (*3)
The latter makes available a set of methods to add theme-overridable templates to the plugin., (*4)
You can check out here a barebone plugin., (*5)
BasePlugin takes care of:, (*6)
plugin_dir
, plugin_path
, plugin_relative_dir
, ect...And provides a number of useful functions:, (*7)
WBF Plugin Framework supports an arbitrary standardized structure to enhance plugins maintainability., (*8)
The most basic plugin structure is the shown below:, (*9)
. |-src/ |---includes/ |-----index.php |-----wbf-plugin-check-functions.php |---Plugin.php |-index.php |-waboot-sample.php
If you want to split the code for the frontend from the code for the dashboard, you can use a structure like this:, (*10)
. |-src/ |---includes/ |-----index.php |-----wbf-plugin-check-functions.php |---Admin.php |---Frontend.php |---Plugin.php |-index.php |-waboot-sample.php
The framework automatically recognizes the structure and links Plugin, Admin and Frontend class instances together., (*11)
The Loader instance within Plugin will receive a reference to both Frontend and Admin, and those instances will receive a reference to Plugin in their constructors., (*12)
A practical example can be found here., (*13)
If you have a complex plugin with many classes and files, you can choose to further split the structure:, (*14)
. |-src/ |---includes/ |-----index.php |-----wbf-plugin-check-functions.php |---admin/ |-----Admin.php |-----more-files... |-----more-files... |---frontend/ |-----Frontend.php |-----more-files... |-----more-files... |---Plugin.php |-index.php |-waboot-sample.php
An example can be found here., (*15)
Plugins that extends TemplatePlugin can inject their template into Wordpress "template_include" mechanism., (*16)
Register common templates, (*17)
You can add a common wordpress template (selectable through admin dashboard) with:, (*18)
$this->add_template("Custom Page Template",$this->get_src_dir()."/templates/custom-page-template.php");
Register hierarchy templates, (*19)
The hierarchy templates are loaded automatically. During 'init' the framework register any templates (not previously registered) under /src/templates
as hierarchy template.
Then during 'template_include' it will serve any registered templates not overridden by the theme from the plugin directory., (*20)
You can manually register hierarchy template with:, (*21)
$this->add_hierarchy_template("single-sample-post-type.php", $this->get_src_dir()."/custom_hierarchy_templates/single-sample-post-type.php");
An example con be found here., (*22)
Using built-in cache mechanism, (*23)
BasePlugin has a built-in caching feature based on transients which allows to easily organize them in groups and nodes., (*24)
The two main methods are: maybe_set_transient(<transient_name>)
and maybe_get_transient(<transient_name>)
., (*25)
Any transient name added with set method will be prefixed with plugin name, for example, given "sample-plugin" as plugin name: maybe_set_transient("posts")
will create a transient named: sample-plugin[posts]
; "posts" will
become a new "node type"., (*26)
You can create node part by putting a colon between the node type name and the node part id. For example, let's say you have a gallery post type, and you want to cache the images in each gallery: you can achieve that with something similiar to:, (*27)
//Somewhere in plugin: $this->loader->add_action("save_post", $this, "cache_my_images") //Somewhere else: public function cache_my_images($post_id){ //Checking... checking.... $this->maybe_set_transient("gallery:{$post_id}"); }
Then you can selectively retrieve or delete the cache. For example:, (*28)
//Delete the cache for the gallery with ID 12 $this->clear_transients("gallery",12) //Delete the caches for all galleries $this->clear_transients("gallery") //Delete all transients $this->clear_transients()
Adding custom links for the plugin in Wordpress plugins list, (*29)
From constructor of the class that extends BasePlugin:, (*30)
$this->add_action_links([ [ 'name' => "New link" 'link' => "/path/to/link" ], //more links ]);
Setting up an update server, (*31)
From the constructor of the class that extends BasePlugin: [...], (*32)
$this->set_update_server("my/end/point");
Endpoint must be compatible with CustomUpdater WBF component., (*33)
For more info about CustomUpdater WBF component: click here., (*34)
Register a license, (*35)
From the constructor of the class that extends BasePlugin:, (*36)
$license = new \My\License("my-license"); //a class which extends \WBF\components\license\License and implements \WBF\components\license\Licence_Interface $this->register_license($license);
You can also link a license to the custom updater (the update will be blocked with invalid licenses), (*37)
$license = new \My\License("my-license"); $this->set_update_server("my/end/point",$license);
For more info about License WBF component: click here., (*38)
An helper component for Wordpress plugin development
GPL-2.0+