nghh / lib-wordpress
正在开发中 – 即将推出完整的 WordPress 工具库和主题开发辅助库
v1.0.1
2022-09-19 09:15 UTC
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2024-09-21 19:47:35 UTC
README
PHP 工具库和 WordPress 辅助类库
目录
WP 路由器
一个基于 WordPress 方式的 MVC 路由器。此路由器工作方式类似于默认 wp 模板层次结构。
@see https://wphierarchy.com
目前您应该有以下控制器和方法
SingularController::post()
SingularController::page()
SingularController::attachment()
ArchiveController::category()
ArchiveController::postTag()
ArchiveController::author()
ErrorController::error404()
IndexController::frontPage()
IndexController::home()
// in your theme e.g. functions.php use Nghh\Lib\Wordpress\Utils\WP_Router; // Optional args. If you use namespace, pass it like this $args = [ 'namespace' => __NAMESPACE__ . '\Controllers\\', 'env' => 'local' ]; (new WP_Router($args))->registerHooks(); // A controller can look like this e.g. Controllers/SingularController.php namespace Nghh\Theme\Controllers; use Nghh\Theme\Models\Post; use Nghh\Theme\Models\Page; use Nghh\Theme\Models\Attachment; class SingularController extends BaseController { public function page() { echo $this->view('pages.singular.page', ['Page' => new Page()]); } public function post() { echo $this->view('pages.singular.post', ['Post' => new Post()]); } public function attachment() { echo $this->view('pages.singular.attachment', ['Attachment' => new Attachment()]); } }
存在一个 wp 过滤器,您可以在此处修改控制器和动作
add_filter('nghh/lib/router', 'nghh_modify_controller', 10, 2); function nghh_modify_controller($controller, $wp_query) { /** * $controller['name] = 'singular' * $controller['action] = 'post' * * => calls SingularController::post() */ return $controller; }
配置
基于 Laravel 配置系统的 Config 类。
// functions.php use Nghh\Lib\Wordpress\Utils\Config; /** * Init config class in your functions.php * and pass full path to config dir and environemt */ Config::instance()->init('/path/to/config/dir', 'local'); // config/app.php return [ 'version' => '0.0.1' ]; // You can then get config data with helper class function like this: \Nghh\Lib\Wordpress\func\config('app.version'); // => 0.0.1