插件在激活期间生成了2651个字符的意外输出,为什么?

时间:2018-10-02 03:06:51

标签: php wordpress plugins

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

  

该插件在生成期间产生了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();

您能告诉我如何解决吗?

1 个答案:

答案 0 :(得分:1)

针对此错误

  

警告: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(){...}
}

在核心PHP call_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变量。这可能会也可能不会导致问题(如果不进行测试就无法确定),但是覆盖它可能对您而言是一个简单的过度使用。如果以前经常声明变量,这不是故意的,那么通常以这种方式覆盖变量(作为循环的赋值)。

干杯!