tombroucke / wp-sidewheels
此包的最新版本(3.2.1)没有可用的许可证信息。
用于在您的WP安装中开发应用的框架。
3.2.1
2023-01-17 14:00 UTC
Requires
- johnbillion/extended-cpts: ^5.0
- squizlabs/php_codesniffer: ^3.6
- twig/twig: ^3.0
Requires (Dev)
- 10up/wp_mock: ^0.4.2
- phpunit/phpunit: ^9.5
- spatie/ray: ^1.36
- szepeviktor/phpstan-wordpress: ^1.1
Suggests
- tombroucke/wp-models: Interact with posts, terms & users in an eloquent way
This package is auto-updated.
Last update: 2024-09-17 17:38:15 UTC
README
此包提供了一个通过配置文件创建自定义路由(MVC)、角色、自定义文章类型等内容的简单方法。
安装
将此包添加到您的插件依赖中
composer require tombroucke/wp-sidewheels
初始化您的插件sidewheels
dirname(__FILE__)
是您的config.php文件所在的位置。
use Otomaties\Sidewheels\Sidewheels; add_action('init', function(){ Sidewheels::init(dirname(__FILE__)); });
添加到您的插件激活钩子
use Otomaties\Sidewheels\Sidewheels; register_activation_hook(__FILE__, function(){ Sidewheels::install(dirname(__FILE__)); });
添加到您的插件停用钩子
use Otomaties\Sidewheels\Sidewheels; register_deactivation_hook(__FILE__, function(){ Sidewheels::uninstall(); });
将config.php添加到您的插件根目录
重要提示
添加新路由后,您需要手动刷新重写规则。
示例配置文件
<?php use Namespace\Models\Order; return [ 'templatePath' => __DIR__ . '/views', // This is the default template directory, so this could be omitted 'textDomain' => 'plugin-textdomain', // Text domain for admin strings translation 'routes' => [ [ 'path' => 'public-page', 'callback' => ['Namespace\Controllers\Frontend', 'index'], // Or string: 'Namespace\Controllers\Frontend@index' 'title' => __('Public page', 'plugin-textdomain'), ], [ 'path' => 'private-page', 'callback' => ['Namespace\Controllers\Admin', 'index'], 'title' => __('Private page', 'plugin-textdomain'), 'capability' => 'manage_my_plugin', ], [ 'path' => 'private-page', 'callback' => ['Namespace\Controllers\Admin', 'create'], 'capability' => 'manage_my_plugin', 'method' => 'POST', ], [ 'path' => 'private-page/orders/{order_id}', 'callback' => 'Namespaces\Controllers\Admin\Order@index', 'method' => 'GET', 'title' => __('Order #{order_id}', 'plugin-textdomain'), ], [ 'path' => 'private-page/orders/{order_id}/{order_item}', 'callback' => 'Namespaces\Controllers\Admin\OrderItem@index', 'method' => 'GET', 'title' => __('Order item #{order_item}', 'plugin-textdomain'), ] ], 'postTypes' => [ // See https://github.com/johnbillion/extended-cpts 'shop_order' => array( 'args' => array( 'labels' => array( 'singular_name' => __('Order', 'plugin-textdomain'), 'plural_name' => __('Orders', 'plugin-textdomain'), ), 'menu_icon' => 'dashicons-cart', 'supports' => array( 'title', 'author' ), 'show_in_menu' => true, 'public' => false, 'publicly_queryable' => false, 'has_archive' => false, 'admin_cols' => array( 'name' => array( 'title' => __('Name', 'plugin-textdomain'), 'meta_key' => 'name', ), 'total' => array( 'title' => __('Total', 'plugin-textdomain'), 'function' => function () { $order = new Order(get_the_ID()); echo $order->total(); }, ), ), ), ), ], 'roles' => [ 'administrator' => array( 'label' => __('Administrator', 'plugin-textdomain'), 'capabilities' => array( 'manage_my_plugin' => true, ), ), ] ];
MVC
控制器
对于每个路由,您需要定义一个回调。这可以是一个内联函数或一个控制器。
<?php namespace Namespace\Controllers; use Otomaties\Sidewheels\Abstracts\Controller; class Admin extends Controller { public function index() { $this->route()->setTitle('Dashboard'); // Optional, only for rendering complete pages $this->render('admin/dash.html', [ // render() : complete page, renderContent() : render during the_content(), renderShortcode() : returns html instead of rendering 'website' => 'https://tombroucke.be' ]); } public function create() { // Create a post or perform other action on POST } public function shout(string $string) { return strtoupper($string); } }
视图
此包使用twig作为其模板引擎。您可以将变量传递到模板中,例如'website'。路由对象也作为变量传递,因此您可以使用例如{{ route.title }}。您可以从控制器调用公共方法。
<!DOCTYPE html> <html lang="en"> <head> <title>{{ route.title }}</title> </head> <body> <h1>{{ route.title }}</h1> <p>Check out this website: {{ website }}</p> <p>{{ shout('I\'m shouting'); }}</p> </body> </html>
默认情况下,非常基本的vendor/tombroucke/wp-sidewheels/templates/sidewheels.php
模板将加载到所有端点。您可以在视图目录或主题根目录中添加sidewheels.php
模板。您可以使用sidewheels_template_paths
过滤器根据特定路由或其他逻辑加载自定义模板。
过滤器
拒绝访问某些路由
use Namespace\Models\Order; add_filter('sidewheels_user_has_access', function ($access, $route) { if (current_user_can('manage_options')) { $access = true; } // Only give access if current user is author of {order_id} if ($route->path() == 'private-page/orders/{order_id}') { $order = new Order($route->parameter('order_id')); return $order->author() == get_current_user_id(); } return $access; }, 10, 2);
修改标题,将覆盖配置文件中的标题
标题也可以从控制器$this->route()->setTitle('title');
设置,但此过滤器将覆盖所有内容。
add_filter('sidewheels_route_title', function ($title, $route) { if ($route->path() == 'public-page') { $title = 'Custom public page title'; } return $title; }, 10, 2);
添加用于在您的twig模板中使用的自定义函数。
use \Twig\TwigFunction; add_filter('sidewheels_twig_functions', function ($functions) { $functions = new TwigFunction( 'customFunction', function ($argument) { // Modify $argument return $argument; } ); return $functions; });
添加路由
您可以在配置文件之外添加路由。
Route::get('public-page', 'Namespace\Controllers\Frontend@index');