rumur / wp-notice
用于处理WordPress通知的面向对象包
v1.1.0
2021-07-04 21:45 UTC
Requires
- php: >=7.1.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-05 04:40:29 UTC
README
这是一个小的面向对象包装器,允许以整洁的方式处理WordPress通知。
最低要求
- PHP: 7.2+
- WordPress: 5.3+
安装
composer require rumur/wp-notice
Themosis 2.x
php console vendor:publish --provider='Rumur\WordPress\Notice\NoticeServiceProvider'
Sage 10.x
wp acorn vendor:publish --provider='Rumur\WordPress\Notice\NoticeServiceProvider'
注册到WordPress
它注册了渲染功能,将显示所有由该包添加的通知。
<?php // functions.php // Clean WordPress installation. \Rumur\WordPress\Notice\Notice::registerIntoWordPress(); // Alternative way of doing this. add_action('admin_notices', [\Rumur\WordPress\Notice\Notice::class, 'render']); // Themosis 2.x or Sage 10.x Installation, after you've used `vendor:publish` command. \Notice::registerIntoWordPress(); // Or there is another alternative way. // Themosis 2.x or Sage 10.x Only \Rumur\WordPress\Notice\Facades\Notice::registerIntoWordPress(); // Or even as a function // Themosis 2.x or Sage 10.x Only notice()->registerIntoWordPress(); /** * The notice function that just need to return a string. * * @return string */ function rmr_notice_as_function() { return __('Hello From Function', 'text-domain'); }
<?php // App/Notices/ExampleNotification.php namespace App\Notices; use Rumur\WordPress\Notice\Noticeable; class ExampleNotification extends Noticeable { public function message(): string { return __('I can change the info here', 'text-domain'); } }
如何使用?
可以使用几种类型。例如Notice::info(...)、Notice::error(...)、Notice::warning(...)、Notice::success(...)。
这些方法可以接受一个string、Noticeable或\WP_Error实例,或者返回字符串的常规可调用函数rmr_notice_as_function作为参数。
<?php use App\Notices\ExampleNotification; use App\Domain\Orders\OrderRepository; use Rumur\WordPress\Notice\Notice; $order = OrderRepository::find(2020); if ($order->isPayed()) { // Simple way. Notice::info(__('Congrats', 'text-domain')); // Makes a success message from the Noticeable instance Notice::success(new ExampleNotification) // ⚠️ OPTIONAL ⚠️ // Will be always displaying the notice, // unless `DISABLE_NAG_NOTICES` defined and it's set as `true` // @link https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices#Disable_Nag_Notices ->nag() // ⚠️ OPTIONAL ⚠️ // Makes a notice be dismissible. Adds a close button to the notice. ->dismissible() // ⚠️ OPTIONAL ⚠️ // Makes a notice display it in an alternative way, // by adding a background color that corresponds to its type. ->asAlternative() // ⚠️ OPTIONAL ⚠️ // This options tells to a notice to postpone itself and show only when time. ->showLater('tomorrow') // ⚠️ OPTIONAL ⚠️ // This options tells to a notice to always show up until time. ->showUntil('next friday') // ⚠️ OPTIONAL ⚠️ // Tells to a notice to show up only on this pages. ->showWhenPage('themes', 'tools' /*,...*/ ) // ⚠️ OPTIONAL ⚠️ // Tells to a notice to show up only if the logged in user has specific role. ->showWhenRole('subscriber', 'author' /*,...*/ ) // ⚠️ OPTIONAL ⚠️ // Tells to a notice to show up only if the logged in user has specific id. ->showWhenUser(1, get_user_by('id', 25) /*,...*/) // ⚠️ OPTIONAL ⚠️ // Tells to a notice to show up only if the current screen is for specific taxonomies. ->showWhenTaxonomy('category', 'post_tag' /*,...*/ ) // ⚠️ OPTIONAL ⚠️ // Tells to a notice to show up only if the current screen is for specific post type. ->showWhenPostType('post', 'page' /*,...*/ ); }
可用方法
注意,所有条件都使用
OR操作符进行检查,但如果存在时间条件,则将切换到AND。
为了在卸载使用该包的插件/主题时删除所有痕迹。
为了删除所有痕迹,只需调用flush方法,如Notice::flush()。
许可证
此软件包根据MIT许可证授权 - 有关详细信息,请参阅LICENSE.md文件。