首页 > 解决方案 > 插件在激活过程中产生了2651个字符的意外输出,为什么?

问题描述

我正在尝试开发一个插件。当我激活插件时,它向我显示以下错误消息:

该插件在激活期间生成了 2651 个字符的意外输出。如果您注意到“标头已发送”消息、联合提要问题或其他问题,请尝试停用或删除此插件。

我不明白为什么它会显示这个错误。我可以看到没有空格来显示这个错误!

这是我的代码:

<?php
/*
Plugin Name:  Forum Roles
Plugin URI:   http://www.creativeartbd.com
Description:  Generate a google map
Version:      1.0
Author:       Shibbir
Author URI:   http://creativeartbd.com/bio/
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  gmape
Domain Path:  /languages/
*/

// custom forum roles and capabilites class
class BOJ_Forum_Roles {

    function __construct() {
        // register plugin activation hook
        register_activation_hook( __FILE__,  'activation' );
        // register plgin deactivation hook
        register_deactivation_hook( __FILE__, 'deactivation' );
    }

    // plugin activation method
    function activation () {
        // get the default administrator tole
        $role =& get_role('administrator');
        // add forum capabilities to the administrator role
        if( !empty($role) ) {
            $role->add_cap('publish_forum_topics');
            $role->add_cap('edit_others_forum_topics');
            $role->add_cap('delete_forum_topics');
            $role->add_cap('read_forum_topics');
        }

        // create the forum administator tole
        add_role( 'forum_administrator', 'Forum Administrator', array(
            'publish_forum_topics', 'edit_others_forum_topics', 'delete_forum_topics', 'read_forum_topics'
        ) );

        // create the moderator role
        add_role('forum_moderator', 'Forum Moderator', array(
            'publish_forum_topics', 'edit_others_forum_topics','read_forum_topics'
        ));

        // create the forum member role
        add_role('forum_member', 'Forum Member', array(
            'publish_forum_topics', 'read_forum_topics'
        ));

        // create the forum suspended role
        add_role( 'forum_suspended', 'Forum Suspeded', array(
            'read_forum_topics'
        ) );
    }

    // plugin deactivation method
    function deactivation () {
        // get the default administrator role
        $role =& get_role('administrator');

        //remove forum capabilites to the administrator role
        if(!empty($role)) {
            $role->remove_cap('publish_forum_topics');
            $role->remove_cap('edit_others_forum_topics');
            $role->remove_cap('delete_forum_topics');
            $role->remove_cap('read_forum_topics');
        }

        // set up an array of roles to delete
        $roles_to_delete = array(
            'forum_administrator',
            'forum_moderator',
            'forum_member',
            'forum_suspended'
        );

        // loop through each role, deleting the role if necessary
        foreach ($roles_to_delete as $role) {
            // get the users of the role
            $users = get_users(array(
                'role'  =>  $role
            ));
            // check if there are no users for the role
            if( count($users) <= 0 ) {
                //remove the role from the site
                remove_role( $role );
            }

        }
    }
}

$forum_roles = new BOJ_Forum_Roles();

你能告诉我我该如何解决吗?

标签: phpwordpressplugins

解决方案


对于这个错误

警告: call_user_func_array() 期望参数 1 是有效的回调,未找到函数“激活”或无效的函数名..

你不会调用你的对象(类)的方法。为此,您可以将数组传递给回调,第一个元素是类名(在静态调用中)或对象的实例,第二个元素是方法名,例如:

class BOJ_Forum_Roles {

   public function __construct() {
        register_activation_hook( __FILE__, [$this,'activation'] );
        register_deactivation_hook( __FILE__, [$this,'deactivation'] );
   }

   public function activation(){...}

   public function deactivation(){...}
}

在核心 PHPcall_user_func_array()中是这样调用的

 call_user_func_array([$this,'activation'], $args);

http://php.net/manual/en/function.call-user-func-array.php

此错误的输出可能是您看到的原始错误。

静态调用

只是为了完整性,如果你有

public static function activation(){...}

你会这样称呼它(静态)

class BOJ_Forum_Roles {

   public function __construct() {
        register_activation_hook( __FILE__, [__CLASS__,'activation'] );
        register_deactivation_hook( __FILE__, [__CLASS__,'deactivation'] );
   }

   public static function activation(){...}

   public static function deactivation(){...}
}

使用__CLASS__常量比输入类名更可取,在 PHP5.5+ 中您也可以这样做 self::class,尽管我相信常量会更快一些。我认为static::class后期静态绑定也是如此。当然,你总是可以这样做(在 5.5+ 中)BOJ_Forum_Roles::class,这在这个例子中没有什么意义,但它对命名空间很有用。

更新

显示...注意:只能通过引用分配变量

$role =& get_role('administrator');

移除 Amp,因为您通过引用分配函数的结果。我不确定,但如果它是一个对象,它无论如何都是通过引用传递的。没关系,因为你可能不应该在你的函数中修改它,事实上这只是引起了我的注意(在停用时):

$role =& get_role('administrator');
...
foreach ($roles_to_delete as $role) {
  ...
}

您正在循环中重新分配$role变量。这可能会也可能不会导致问题(如果不对其进行测试,我无法确定),但覆盖它可能是您的一个简单的过度站点。如果以前经常声明它不是故意的,那么以这种方式覆盖变量(作为循环的分配)通常是不好的做法。

干杯!


推荐阅读