wbf/pluginsframework

WordPress插件开发辅助组件

dev-master 2017-09-28 09:33 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:39:01 UTC


README

插件框架组件是一组工具和可扩展类,用于帮助WordPress插件开发者。

它支持“约定优于配置”原则,以保持环境清洁和可维护。

入门

您可以通过扩展BasePlugin或TemplatePlugin来开始新插件的开发。

后者提供了一组方法,可以将主题可覆盖的模板添加到插件中。

简单插件

您可以在以下链接中查看一个基本插件:这里

BasePlugin负责

  • 初始化有用的变量,如 plugin_dirplugin_pathplugin_relative_dir 等...
  • 设置本地化域名
  • 建立一个标准的“约定优于配置”结构(见下文)
  • 存储插件添加的动作和过滤器

并提供许多有用的函数

  • 基于transients的完整缓存机制
  • 设置自定义更新服务器的功能
  • 等等...

插件目录结构

WBF插件框架支持任意标准化的结构来增强插件的可维护性。

最基本的插件结构如下所示

.
|-src/
|---includes/
|-----index.php
|-----wbf-plugin-check-functions.php
|---Plugin.php
|-index.php
|-waboot-sample.php

如果您想将前端代码与仪表盘代码分开,可以使用如下结构

.
|-src/
|---includes/
|-----index.php
|-----wbf-plugin-check-functions.php
|---Admin.php
|---Frontend.php
|---Plugin.php
|-index.php
|-waboot-sample.php

框架会自动识别结构,并将插件、管理后台和前端类实例连接在一起。

插件内的Loader实例将接收到前端和管理后台的引用,而那些实例在其构造函数中也将接收插件的引用。

一个实际例子可以在以下链接中找到:这里

如果您有一个具有许多类和文件复杂插件,您可以进一步选择拆分结构

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

一个例子可以在以下链接中找到:这里

模板插件

扩展TemplatePlugin的插件可以将它们的模板注入到WordPress的“template_include”机制中。

注册通用模板

您可以使用以下方式添加一个通用的WordPress模板(可通过管理仪表盘选择)

$this->add_template("Custom Page Template",$this->get_src_dir()."/templates/custom-page-template.php");

注册层次模板

层次模板将自动加载。在'init'期间,框架将注册任何在 /src/templates 下(尚未注册)的模板作为层次模板。然后在'template_include'期间,它将提供任何未由主题覆盖的注册模板。

您可以使用以下方式手动注册层次模板

$this->add_hierarchy_template("single-sample-post-type.php", $this->get_src_dir()."/custom_hierarchy_templates/single-sample-post-type.php");

一个例子可以在以下链接中找到:这里

更多功能

使用内置的缓存机制

BasePlugin具有基于transients的内置缓存功能,这使得它们可以轻松地在组和节点中组织。

两种主要方法是: maybe_set_transient(<transient_name>)maybe_get_transient(<transient_name>)

使用set方法添加的任何transient名称都将以插件名称为前缀,例如,给定“sample-plugin”作为插件名称: maybe_set_transient("posts") 将创建一个名为 sample-plugin[posts] 的transient;“posts”将成为一个新的“节点类型”。

您可以通过在节点类型名称和节点部分ID之间放置冒号来创建节点部分。例如,假设您有一个相册帖子类型,并且想缓存每个相册中的图片:您可以使用类似的方法实现。

//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}");
}

然后您可以有选择地检索或删除缓存。例如

//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()

在Wordpress插件列表中添加插件的自定义链接

从扩展BasePlugin的类的构造函数中

$this->add_action_links([
    [
        'name' => "New link"
        'link' => "/path/to/link"
    ],
    //more links
]);

设置更新服务器

从扩展BasePlugin的类的构造函数中:[...]

$this->set_update_server("my/end/point");

端点必须与CustomUpdater WBF组件兼容。

有关CustomUpdater WBF组件的更多信息:[点击这里](https://github.com/wagaweb/wbf/tree/master/src/components/customupdater)。

注册许可证

从扩展BasePlugin的类的构造函数中

$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);

您还可以将许可证链接到自定义更新器(更新将因无效许可证而被阻止)

$license = new \My\License("my-license");
$this->set_update_server("my/end/point",$license);

有关License WBF组件的更多信息:[点击这里](https://github.com/wagaweb/wbf/tree/master/src/components/license)。