pinkcrab / perique-admin-menu
使用继承从插件和主题中非常容易地创建管理页面/组的PinkCrab框架包。
Requires
- php: >=7.4.0
- pinkcrab/perique-framework-core: 2.0.*
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: <=1.0.0
- gin0115/wpunit-helpers: 1.1.*
- php-stubs/wordpress-stubs: 6.1.* || 5.9.*
- phpstan/phpstan: 1.*
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: 6.1.*
- symfony/var-dumper: <=6.2.7
- szepeviktor/phpstan-wordpress: <=1.1.7
- vlucas/phpdotenv: <=5.5.0
- wp-coding-standards/wpcs: <=2.3.0
- wp-phpunit/wp-phpunit: 6.1.*
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
- dev-master
- 2.0.0
- 1.0.1
- 1.0.0
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- 0.1.0-beta2
- dev-dependabot/composer/yoast/phpunit-polyfills-tw-0.2.0or-tw-1.0.0or-tw-2.0.0
- dev-dependabot/composer/szepeviktor/phpstan-wordpress-lte-1.3.1
- dev-dependabot/composer/phpunit/phpunit-tw-7.0or-tw-8.0or-tw-9.0
- dev-dependabot/composer/php-stubs/wordpress-stubs-6.1.staror-5.9.staror-6.2.star
- dev-develop
- dev-feature/gh35-prep-for-perique-v2
- dev-feature/gh27-hold-page-slug-after-registered
- dev-feature/gh34-seperate-docs
- dev-feature/gh28-readme-incorrect-for-override-view
- dev-feature/gh30-update-deps-to-wp6_1
- dev-feature/gh21-allow-pages-extending-menu-page-to-be-registtered
- dev-feature/gh22-update-dev-deps
- dev-feature/docs
- dev-feature/add-missin-tests-for-standalong-page-registration
- dev-feature/gh12-reintroduce_before_load_hooks
- dev-feature/gh12-reintroduce-page-hooks
- dev-feature/gh7-hardcoded-page-slug-when-changing-page-titles
- dev-feature/gh8-update-dev-deps
- dev-bump-collection-to-v0.2
- dev-feature/issue/gh1-allow-extension-of-page-types
- dev-settings-page-temp
This package is auto-updated.
Last update: 2024-09-07 01:50:33 UTC
README
Perique Admin Menu
Perique插件框架的一个模块,用于使用WordPress渲染和处理管理菜单页面。
为什么?
WordPress管理页面可以通过简单的函数添加,但这很容易导致代码结构混乱,包含模板的包括和模板中逻辑和表现的不可避免地混合。
Admin Menu模块完全访问Perique DI容器,以实现逻辑和表现的更多分离,同时所有服务都易于测试和推理。
需要 Perique - 更多信息请访问我们的文档。 https://perique.info
设置
使用composer(通过CLI)包含模块
$ composer require pinkcrab/perique-admin-menu
一旦包含,我们就可以将模块添加到Perique,其底层中间件将添加到注册过程。
$app = ( new PinkCrab\Perique\Application\App_Factory() ) // Perique bootstrapping as normal. ->module( Admin_Menu::class ) ->boot();
一旦包含中间件,我们就可以在常规 注册 过程中将Page & Group模型用作一部分
使用
可以注册单个页面或一组页面。
组
一个页面可以用来注册一组页面,这些页面可以注册为顶级菜单项,或作为另一个页面的子菜单项。
class My_Group extends Abstract_Group{ // Required protected string $group_title = 'My Page Group'; protected string $primary_page = 'Acme\My_Plugin\Page\Primary_Page'; protected array $pages = array( 'Acme\My_Plugin\Page\Secondary_Page', 'Acme\My_Plugin\Page\Tertiary_Page', ); // Optional protected string $capability = 'edit_posts'; // Defaults to manage_options protected string $icon = 'dashicons-chart-pie'; // Defaults to dashicons-admin-generic protected int $position = 24; // Defaults to 65 }
可以使用 enqueue方法 明确为此组加载脚本和样式。这将使这些脚本和样式仅在该组内的页面上加载。
有关更多详细信息,请参阅 组文档
页面
页面是一个单个菜单项,可以注册为顶级菜单项,或作为另一个页面的子菜单项。
class My_Page extends Menu_Page{ // Required protected string $page_slug = 'acme_pages'; protected string $page_title = 'Acme Pages'; protected string $menu_title = 'Acme Pages'; // Optional protected ?string $parent_slug = null; // If null, will be a top level menu item. protected string $capability = 'edit_post'; // Default capability for page. protected ?int $position = 12; // View to render protected string $view_template = 'my-page.php'; protected array $view_data = array('key' => 'value'); }
可以使用 enqueue方法 明确为此页面加载脚本和样式。
您还可以使用 load方法 在页面加载时触发回调。
有关Page模型的更多详细信息,请参阅 页面文档
页面表单处理示例
class Settings_Page extends Menu_page{ // Page definitions. protected string $page_slug = 'acme_pages'; protected string $page_title = 'Acme Pages'; protected string $menu_title = 'Acme Pages'; protected string $view_template = 'my-page.php'; // Custom form handler service. private Form_Handler $form_handler; // Injected settings service and the form handler. public __construct( Settings $settings, Form_Handler $form_handler ){ $this->form_handler = $form_handler; $this->view_data = $settings->as_array(); } // On page load, check if form has been submitted, and if so, handle it. public function load( Page $page ): void{ if( $this->form_handler->is_submitted() ){ $new_settings = $this->form_handler->handle(); $this->view_data = $new_settings; } } }
在上面的示例中,页面将使用来自Settings
服务的设置,如果表单已被提交,它将使用Form_Handler
服务处理表单并更新视图数据。
示例
请参阅 示例插件 了解如何将此模块作为插件的一部分使用的基本示例。
许可证
MIT许可证
https://open-source.org.cn/licenses/mit-license.html
贡献
如果您想为此或任何其他Perique模块做出贡献,请随时提交带有您更改的PR。
所有代码都必须提供相应的测试,并且必须通过PHPUNIT、PHPStan和PHPCS的检查,且覆盖率没有大幅下降。
有关测试和代码风格检查命令的详细信息,请参阅composer.json文件。其中composer all
是最重要的。
变更日志
- 2.0.0 - 迁移到Perique 2.0.0
- 1.0.1 - 将文档分离出来,并在页面模型中添加了
page_hook()
方法,并在注册后设置钩子。 - 1.0.0 - 完成了Perique 1.4.*的API
- 0.2.1 - 允许扩展Menu_Page的页面同时注册为父页面和子页面。
- 0.2.0 - 重新引入了Groups和Pages的
register()
和load()
方法。 - 0.1.2 - 在Page_Dispatcher中修复了硬编码的主页面slug,并更新了开发依赖项。
- 0.1.1 - Collection版本升级。
- 0.1.0 - 从https://github.com/Pink-Crab/Module__Admin_Pages迁移过来。