em4nl / unplug
WordPress 微型框架
Requires
- php: >=5.6
- em4nl/ucache: ^0.0.4
- em4nl/uresponse: ^0.0.3
- em4nl/urouter: ^0.0.4
Requires (Dev)
- phpunit/phpunit: ^7.2
README
WordPress 微型框架
安装
通过 composer
composer require em4nl/unplug
使用
Unplug 是一个微型框架,用于在 WordPress 主题中需要比 WordPress 默认提供更多对网站前端控制时使用。强烈建议与 Twig 模板引擎一起使用。
基本思想是完全绕过 WordPress 的路由/模板层次结构机制,并自己实现。
我假设您正在使用自动加载,并且您的 composer 供应商目录位于 ./vendor。
functions.php
为了确保 WordPress 不运行默认的查询/模板,您需要在主题的 functions.php 中添加对 Em4nl\Unplug\unplug 的调用。
<?php require_once __DIR__ . '/vendor/autoload.php'; Em4nl\Unplug\unplug();
index.php
对于大多数主题,这可能是您需要的另一个 PHP 文件(尤其是如果使用 Twig)。
<?php // No need to require autoload again if you already did in // `functions.php`, WordPress will always run that before // `index.php`. use Em4nl\Unplug; // Basically, just use the `_use`, `get` and `post` functions to // set up your frontend routes. // _use callbacks will be called on all routes. Use this to add // stuff to your context that you always need. For example a global // image for sharing your website on facebook and twitter, loaded // from an ACF field: Unplug\_use(function(&$context) { $context['share_image'] = get_field('share_image', 'options'); }); // You can have as many _use callbacks as you like. Another example // would be to add a menu Unplug\_use(function(&$context) { $context['menu'] = array(/* TODO load this from WordPress */); }); // If you don't want to mutate the $context array directly, you can // also return a changed copy. // If you want to have the Twig template engine available on every // route, why not store it in the $context Unplug\_use(function($context) { $twig_loader = new Twig_Loader_Filesystem( get_template_directory() . '/templates' ); $twig = new Twig_Environment($twig_loader, [ 'debug' => true, ]); $twig->addExtension(new Twig_Extension_Debug()); $context['twig'] = $twig; return $context; }); // The index route. Return a string to send html. Especially useful // in conjunction with Twig Unplug\get('/', function($context) { return $context['twig']->render('home.twig', $context); }); // Or just echo your response Unplug\get('/hi-world', function($context) { echo "Hello world!"; }); // Return an array to automatically send a json response Unplug\get('/api', function($context) { return ['error' => NULL]; }); // Routes can have parameters; the parameter values are collected // into the $context['params'] array Unplug\get('/:param', function($context) { return "Hi, {$context['params'][0]}!"; }); // Routes can have optional path segments. To find out wether // they're present, examine $context['path'] Unplug\get('/menu/open?', function($context) { return "You're visiting {$context['path']}"; }); // Routes can also have wildcards that match any number of segments // The content of the wildcard is also exposed in // $context['params'] Unplug\get('/test/*', function($context) { return "The wildcard path: {$context['params'][0]}"; }); // You can also have POST routes. Unplug doesn't take care of the // posted data for you; just use the $_POST array for that // (Same goes for query parameters in $_GET) Unplug\post('/form', function($context) { // ... }); // If you want to return something other than a '200 OK' (or a 200 // _explicitly_), you can use the `Unplug\ok`, `Unplug\not_found`, // `Unplug\moved_permanently` and `Unplug\found` functions. function my_404($context) { Unplug\not_found($context['twig']->render('error_404.twig', $context)); } // Sometimes you have a parametrised route where not all // parameter values are valid. This can be handled e.g. like this Unplug\get('/post/:title', function($context) { $context['post'] = my_get_post($context['params'][0]); if ($context['post']) { return $context['twig']->render('post.twig', $context); } else { my_404($context); } }); // You'll almost always want to register a global catchall callback // that will be used if no other route matches where you deliver // your 404 page. Unplug\catchall('my_404'); // After all routes are set up, you still have to call this, or // nothing will be run! Unplug\dispatch();
index.php (WordPress 根目录) (缓存!)
虽然您可以直接在主题中使用 Unplug 的缓存,但最有效的方法是完全绕过 WordPress,并且只在无法或不想从缓存中提供页面时加载它。为了做到这一点,您需要将 WP 的根 index.php 文件替换为您自己的。
将根目录中的 index.php(不是主题中的)重命名为其他名称,例如 wp-index.php。
然后在它的位置创建一个新的 index.php 文件,并放入以下代码
<?php require_once __DIR__ . '/wp-content/themes/<name-of-your-theme>/vendor/autoload.php'; Em4nl\Unplug\front_controller(__DIR__ . '/wp-index.php');
这将提供缓存中的文件,并且只有在找不到文件或通过可选的 `invalidate**` 参数由自定义函数无效化时才会加载 WordPress。
继续阅读,您也需要该文件
unplug-config.php
如果您不使用自定义前端控制器,此文件不会造成伤害,但如果您使用,您必须拥有它。您可以将此文件放在 WordPress 根目录中 index.php 旁边,或者放在任何其他目录中,只要它位于您安装 unplug 的 vendor 目录之上。
目前,您需要在那里有这两个定义
<?php define('UNPLUG_CACHE_DIR', __DIR__ . '/_unplug_cache'); define('UNPLUG_CACHE_ON', TRUE); // set to FALSE to disable the cache
开发
安装依赖项
composer install
运行测试
./vendor/bin/phpunit tests