pxlrbt/wordpress-notifier

此包已被废弃,不再维护。未建议替代包。

WordPress 包,用于插件或主题中处理 WordPress 管理通知。

1.2.0 2020-11-16 15:51 UTC

This package is auto-updated.

Last update: 2023-01-16 20:39:12 UTC


README

WordPress Notifier 是一个面向对象的助手库,用于处理插件或主题中的 WordPress 管理通知。它默认支持持久通知和可关闭通知,并且可以扩展。

安装

Composer

composer require pxlrbt/wordpress-notifier

手动安装

在项目中下载并解压库。然后包含 bootstrap.php 文件。

require_once '[PATH_TO_FILE]/bootstrap.php';

使用方法

创建通知实例

全局创建一个新的通知实例。这必须在每次页面加载时调用,因为它会自动注册用于打印管理通知的钩子。之后,您可以使用通知器的静态方法创建简单的通知。

Notifier::getInstance();
#or
$notifier = new Notifier()

创建通知

创建通知器实例后,您可以通过它分发新的通知。您可以使用通知器的静态函数进行简单通知,或者自己创建一个通知对象,并使用其链式方法进行配置,然后通过通知器发送。

// Static functions refer to last Notifier created
Notifier::info('An update is available.');
Notifier::error('Oops, something went wrong!');

// Advanced usage
$notifier->dispatch(
    Notification::create('Plugin configuration is missing!')
        ->id('plugin_xy.config.failed')
        ->type('error')
        ->title('ERROR')
        ->dismissible(true)
        ->persistent(true);
)

检查通知是否存在

使用通知 ID 检查通知是否已经发送。

$id = 'plugin_xy.config.failed';
$notifier->containsNotification($id);

删除通知

用户可以手动忽略通知。如果您需要以编程方式删除持久通知,设置一个 ID 并使用它再次删除通知。

$id = 'plugin_xy.config.failed';
$notifier->removeNotification($id);