从静态方法调用“ADMIN_NOTICES”挂钩

时间:2022-01-05 作者:Pijush Gupta

正在尝试呼叫;admin\\u通知”;从静态函数挂钩。没有得到任何输出。而对同一钩子的正常调用正在工作。

use Awraq\\Init;
use Awraq\\Notice;

/**
 * Initialize the activation works of the plugin.
 */
function awraq_activate_plugin()
{

    /* NOT WORKING */
    Notice::error(\'Activation failed\');

    
    /* WORKING */

    //add_action(\'admin_notices\', \'simplefunction\');
}

register_activation_hook(__FILE__, \'awraq_activate_plugin\');



function simplefunction()
{
    echo \'<div class="notice notice-error is-dismissible">
        <p>Activation failed</p></div>\';
}

class

namespace Awraq;

if (!defined(\'ABSPATH\')) exit;

class Notice
{
    private static $class = \'\';
    private static $msg = \'\';

    public static function error($msg = \'error\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-error is-dismissible\' : \'notice notice-error\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(__CLASS__, \'awraq_notice\'));
    }

    public static function warning($msg = \'warning\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-warning is-dismissible\' : \'notice notice-warning\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(__CLASS__, \'awraq_notice\'));
    }

    public static function success($msg = \'success\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-success is-dismissible\' : \'notice notice-success\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(__CLASS__, \'awraq_notice\'));
    }

    public static function info($msg = \'info\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-info is-dismissible\' : \'notice notice-info\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(__CLASS__, \'awraq_notice\'));
    }

    public static function  awraq_notice()
    {

        $class  = self::$class;
        $msg    = self::$msg;

        if ($class == \'\' and $msg == \'\') {
            return;
        }

        printf(\'<div class="%1$s"><p>%2$s</p></div>\', esc_attr($class), esc_html($msg));
    }
}

1 个回复
SO网友:Pijush Gupta

通过做两件事解决了问题

已从中删除主呼叫

/**
 * Initialize the activation works of the plugin.
 */
function awraq_activate_plugin()
{
    //removed 
}

register_activation_hook(__FILE__, \'awraq_activate_plugin\');
已将呼叫添加到;plugins\\u loaded“插件加载”;


/**
 * Initialize the plugin.
 */
function awraq_init_plugin()
{
    if (Init::activate() != TRUE) {
        deactivate_plugins(plugin_basename(__FILE__));
        //added below
        Notice::error(\'Plugin got Deactivated. Please check the dependencies.\', true);
    }
}
add_action(\'plugins_loaded\', \'awraq_init_plugin\');
在移除的类上__CLASS__ 具有完整的命名空间和类-private static $globalScopeName = \'Awraq\\Base\\Notice\';, 因为它将从外部调用
namespace Awraq\\Base;

if (!defined(\'ABSPATH\')) exit;

class Notice
{
    private static $globalScopeName = \'Awraq\\Base\\Notice\';
    private static $class = \'\';
    private static $msg = \'\';

    public static function error($msg = \'error\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-error is-dismissible\' : \'notice notice-error\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(self::$globalScopeName, \'awraq_notice\'));
    }

    public static function warning($msg = \'warning\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-warning is-dismissible\' : \'notice notice-warning\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(self::$globalScopeName, \'awraq_notice\'));
    }

    public static function success($msg = \'success\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-success is-dismissible\' : \'notice notice-success\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(self::$globalScopeName, \'awraq_notice\'));
    }

    public static function info($msg = \'info\', $is_dismissible = TRUE)
    {
        self::$class = ($is_dismissible == TRUE) ? \'notice notice-info is-dismissible\' : \'notice notice-info\';
        self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
        add_action(\'admin_notices\', array(self::$globalScopeName, \'awraq_notice\'));
    }

    public static function  awraq_notice()
    {

        $class  = self::$class;
        $msg    = self::$msg;

        if ($class == \'\' and $msg == \'\') {
            return;
        }

        printf(\'<div class="%1$s"><p>%2$s</p></div>\', esc_attr($class), esc_html($msg));
    }
}

相关推荐

连接到php:8-fpm容器和MariaDB容器的nginx docker容器中的WordPress不会在插件激活时创建任何表

我搜索了整个网络和stackexchange,寻找原因和解决方案,但没有找到任何答案。关于自己的插件以及如何在激活时创建表的代码,有很多问题(和答案)。但这不适用于这里,因为我对wordpress插件库中的知名插件和维护插件有这个问题。So, the situation:一个拥有约20个docker堆栈的kvm来宾。每个堆栈由一个nginx、一个php:8-fpm和一个mariadb容器组成。nginx容器中有一个web目录作为共享卷,其中包含wordpress安装。(所以我没有使用wordpress d