ignitekit/wp-notices

WordPress的简单易用仪表盘通知管理器

1.0.2 2021-12-05 13:08 UTC

This package is auto-updated.

Last update: 2024-09-05 19:12:03 UTC


README

WordPress简单易用的通知管理库,实现了WordPress通知

该库创建了所有必要的功能,用于处理通知的取消显示,并提供通过代码手动取消显示的功能。

快速入门

1. 如何安装库

composer require ignitekit/wp-notices

2. 如何使用NoticesManager

IgniteKit\WP\Notices\NoticesManager是添加新通知时需要使用的类,如下所示

use IgniteKit\WP\Notices\NoticesManager;

class My_Plugin_Bootstrap() {

    private $notices_manager;
    
    public function __construct() {
        // Create instance of NoticesManager annd pass some custom identifier / prefix here.
        $this->notices_manager = new NoticesManager('myplugin');
        add_action('init', array($this, 'init'));
    }
    
    public function init() {
    
        // Eg. Add error notice if Woocommerce is not installed.
        if( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
            
            // Returns instance of IgniteKit\WP\Notices\Notice
            $notice = $this->notices_manager->add_error( 'missing_wc', '<h3>WooCommerce not installed</h3><p>Please install WooCommerce in order to use My Plugin</p>', NoticesManager::DISMISS_FOREVER );   
            
            // Methods available in Notice
            var_dump($notice->id); // The notice ID
            var_dump($notice->is_dismissed()); // True or false
            var_dump($notice->dismiss()); // Dismisses the notice
            var_dump($notice->reset()); // Resets the notice. Removes the dismissed status.
        }
        
        // Somewhere later you can use this to retrieve the error notice you added before...
        // ...apply some logic.
        $notice = $this->notices_manager->get_notice('missing_wc', 'error');
        $notice->reset(); // Then maybe reset it?
        $notice->dismiss(); // Somewhere later, maybe dismiss again?
    }
}

3. NoticesManager类中可用于添加通知的方法列表。

IgniteKit\WP\Notices\NoticesManager类中提供了几个用于添加通知的方法。

这些方法的每个返回值都是IgniteKit\WP\Notices\Notice实例。这基本上是通知类。

方法add_success()

/**
 * Add success notice. Displayed with greeen border.
 *
 * @param $key
 * @param $message - html of the notice
 * @param string|int $expiry - Specifes how much time the notice stays disabled. 
 *
 * Expiry parameter can be: NoticesManager::DISMISS_FOREVER, NoticesManager::DISMISS_DISABLED or number of seconds)
 *
 * @return Notice
 */
public function add_success( $key, $message, $expiry );

方法add_error()

/**
 * Add error notice. Displayed with red border.
 *
 * @param string $key - unique identifier
 * @param string $message - html of the notice
 * @param string|int $expiry - Specifes how much time the notice stays disabled. 
 *
 * Expiry parameter can be: NoticesManager::DISMISS_FOREVER, NoticesManager::DISMISS_DISABLED or number of seconds)
 *
 * @return Notice
 */
public function add_error( $key, $message, $expiry );

方法add_info()

/**
 * Add info notice. Displayed with blue border.
 *
 * @param string $key - unique identifier
 * @param string $message - html of the notice
 * @param string|int $expiry - Specifes how much time the notice stays disabled. 
 *
 * Expiry parameter can be: NoticesManager::DISMISS_FOREVER, NoticesManager::DISMISS_DISABLED or number of seconds)
 *
 * @return Notice
 */
public function add_info( $key, $message, $expiry );

方法add_warning()

/**
 * Add warning notice. Displayed with orange border.
 *
 * @param string $key - unique identifier
 * @param string $message - html of the notice
 * @param string|int $expiry - Specifes how much time the notice stays disabled. 
 *
 * Expiry parameter can be: NoticesManager::DISMISS_FOREVER, NoticesManager::DISMISS_DISABLED or number of seconds)
 *
 * @return Notice
 */
public function add_warning( $key, $message, $expiry );

方法add_custom()

/**
 * Add custom notice. Displayed with gray border.
 *
 * @param string $key - unique identifier
 * @param string $message - html of the notice
 * @param string|int $expiry - Specifes how much time the notice stays disabled. 
 *
 * Expiry parameter can be: NoticesManager::DISMISS_FOREVER, NoticesManager::DISMISS_DISABLED or number of seconds)
 *
 * @return Notice
 */
public function add_custom( $key, $message, $expiry );

方法get_notice_by_id()

/**
 * Return the notice object
 *
 * @param $id
 *
 * @return Notice|null
 */
public function get_notice_by_id( $id )

方法get_notice()

/**
 * Return notice by key and type
 *
 * @param $key
 * @param $type
 *
 * @return Notice|null
 */
public function get_notice( $key, $type );

4. 添加通知后返回的Notice实例中可用的方法列表。

IgniteKit\WP\Notices\Notice类中提供了几个方法。您可以使用这些方法手动取消通知、重置通知或检查通知是否已取消。

方法is_dismissed()

/**
 * Check if notice is dismissed.
 *
 * @return bool
 */
public function is_dismissed()

方法dismiss()

/**
 * Dismisses the notice
 */
public function dismiss()

方法reset()

/**
 * Removes notice dismissal flag. After this call the notice is not dismissed anymore.
 */
public function reset()

5. 文件模板

插件支持文件模板,而不是简单的字符串消息。

要从文件模板显示通知,您可以提供其路径,如下所示,使用file://前缀

file:///home/site.com/public_html/wp-content/themes/my-theme/templates/my-notice.php

许可证

Copyright (C) 2021 Darko Gjorgjijoski (https://darkog.com)

This file is part of WP Notices

WP Notices is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

WP Notices is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with WP Notices. If not, see <https://gnu.ac.cn/licenses/>.