morningtrain/wp-hooks
Composer 安装命令:
composer require morningtrain/wp-hooks
包简介
A system for organizing WordPress hooks
README 文档
README
To let you organize all your WordPress actions and filters.
Table of Contents
Introduction
This tool is made for organizing WordPress hooks.
This tool lets you:
- Load all .php files recursively in a directory
- Add filters and action using a fluid api
- Render Blade views directly on an action (if morningtrain/wp-view is installed)
Getting Started
To get started install the package as described below in Installation.
To use the tool have a look at Usage
Installation
Install with composer
composer require morningtrain/wp-hooks
Dependencies
morningtrain/php-loader
PHP Loader is used to load and initialize all Hooks
morningtrain/wp-view (optional)
WP View is used to load and initialize all Hooks
Usage
To load all Hooks of a given directory
// Load all .php files in ./Hooks and add all found Hooks \Morningtrain\WP\Hooks\Hook::loadDir(__DIR__ . "/App/Hooks");
Multiple Directories
Since this tool uses PHP Loader, you may use multiple directories.
// Load all .php files in ./Hooks and add all found Hooks \Morningtrain\WP\Hooks\Hook::loadDir([__DIR__ . "/App/Hooks",__DIR__ . "/EvenMoreHooks"]);
Creating a Hook
To create a hook first call Hook::action, Hook::filter or Hook::view. Then start a chain to add additional
parameters.
Adding an action
To add an action call Hook::action. You may either add the callback as the second parameter or by using handle()
\Morningtrain\WP\Hooks\Hook::action('init',[Some::class, 'someMethod']); // is the same as \Morningtrain\WP\Hooks\Hook::action('init') ->handle([Some::class, 'someMethod']); // With a priority \Morningtrain\WP\Hooks\Hook::action('init') ->priority(20) ->handle([Some::class, 'someMethod']); // Rendering a view \Morningtrain\WP\Hooks\Hook::action('init') ->view('some_view');
Note that it is not necessary to define the number of args for the callback. The action (or filter) will look at the callback's definition to know how many arguments it takes.
Adding a filter
Adding filters is just like adding action. Call Hook::filter. You may either add the callback as the second parameter
or by using filter()
\Morningtrain\WP\Hooks\Hook::filter('mime_types',[Some::class, 'addSvgMimeType']); // is the same as \Morningtrain\WP\Hooks\Hook::filter('mime_types') ->filter([Some::class, 'addSvgMimeType']); // With a priority \Morningtrain\WP\Hooks\Hook::filter('mime_types') ->priority(20) ->filter([Some::class, 'addSvgMimeType']); // For simple filters that return true or false \Morningtrain\WP\Hooks\Hook::filter('use_some_feature') ->returnTrue(); \Morningtrain\WP\Hooks\Hook::filter('use_some_feature') ->returnFalse();
Note that it is not necessary to define the number of args for the callback. The action (or filter) will look at the callback's definition to know how many arguments it takes.
Adding a view on an action
You may, if the morningtrain/wp-view package is installed, render a blade view directly from an action.
// This will render the footer/copyright view in the footer \Morningtrain\WP\Hooks\Hook::view('footer','footer/copyright');
Note that you MUST define the number of args used in the hook since the hook has no callback method to analyze.
If you need to use the action params in your view you may render your view from another method or use view composing.
// An action that looks something like this: do_action('some_post_action',$postId); \Morningtrain\WP\Hooks\Hook::view('some_post_action','someView'); \Morningtrain\WP\View\View::composer('some_post_action',function($view){ [$postId] = $view; $view->with('postId',$postId); });
Using single use handlers (invokable)
You can use a single use class like so:
class FilterMimeTypes{ public function __invoke(array $mimeTypes) { $mimeTypes['webp'] = 'image/webp'; return $mimeTypes; } } \Morningtrain\WP\Hooks\Hook::filter('mime_types',FilterMimeTypes::class);
Credits
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
morningtrain/wp-hooks 适用场景与选型建议
morningtrain/wp-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.24k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「hooks」 「morningtrain」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 morningtrain/wp-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morningtrain/wp-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 morningtrain/wp-hooks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
A Laravel-like router for the WordPress Rewrite API
A framework
A package containing Data Transfer Object casters, for use with spatie/data-transfer-object
Hooks integrated in Voyager
Illuminate Queue for use in WordPress
统计信息
- 总下载量: 2.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-01