Refactor create_function

时间:2019-06-13 作者:Jobbie Daddy

由于最近WP升级到5.2,我从插件中得到了几个create\\u函数被贬低的错误。插件不再受支持,这意味着我需要自己重构代码。有人能帮我指出正确的方向吗?

    function _options_page(){
    if($this->args[\'page_type\'] == \'submenu\'){
        if(!isset($this->args[\'page_parent\']) || empty($this->args[\'page_parent\'])){
            $this->args[\'page_parent\'] = \'themes.php\';
        }
        $this->page = add_theme_page(
                        $this->args[\'page_parent\'],
                        $this->args[\'page_title\'], 
                        $this->args[\'menu_title\'], 
                        $this->args[\'page_cap\'], 
                        $this->args[\'page_slug\'], 
                        array(&$this, \'_options_page_html\')
                    );
    }else{
        $this->page = add_theme_page(
                        $this->args[\'page_title\'], 
                        $this->args[\'menu_title\'], 
                        $this->args[\'page_cap\'], 
                        $this->args[\'page_slug\'], 
                        array(&$this, \'_options_page_html\'),
                        $this->args[\'menu_icon\'],
                        $this->args[\'page_position\']
                    );

    if(true === $this->args[\'allow_sub_menu\']){

        //this is needed to remove the top level menu item from showing in the submenu
        add_theme_page($this->args[\'page_slug\'],$this->args[\'page_title\'],\'\',$this->args[\'page_cap\'],$this->args[\'page_slug\'],create_function( \'$a\', "return null;" ));


        foreach($this->sections as $k => $section){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    $section[\'title\'], 
                    $section[\'title\'], 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=\'.$k, 
                    create_function( \'$a\', "return null;" )
            );

        }

        if(true === $this->args[\'show_import_export\']){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    __(\'Import / Export\', \'nhp-opts\'), 
                    __(\'Import / Export\', \'nhp-opts\'), 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=import_export_default\', 
                    create_function( \'$a\', "return null;" )
            );

        }//if

        foreach($this->extra_tabs as $k => $tab){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    $tab[\'title\'], 
                    $tab[\'title\'], 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=\'.$k, 
                    create_function( \'$a\', "return null;" )
            );

        }
        if(true === $this->args[\'dev_mode\']){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    __(\'Dev Mode Info\', \'nhp-opts\'), 
                    __(\'Dev Mode Info\', \'nhp-opts\'), 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=dev_mode_default\', 
                    create_function( \'$a\', "return null;" )
            );

        }//if
    }//if           


    }//else
    add_action(\'admin_print_styles-\'.$this->page, array(&$this, \'_enqueue\'));
    add_action(\'load-\'.$this->page, array(&$this, \'_load_page\'));
}//function 

2 个回复
SO网友:majick

有一个用于返回的内置函数null: __return_null.

所以你可以替换create_function(\'$a\', \'return null;\'); 仅使用\'__return_null\' (注意引号)看起来$a 无论如何都不使用。

也可以直接使用匿名函数作为参数:function($a) {return null;} (无引号)。无论哪种方式,因为参数需要函数calback。

SO网友:Adnane Zarrouk

您应该能够更改create\\u函数(“$a”,“return null;”)到Anonymous Function (又名闭包):

From :

    add_theme_page(
            $this->args[\'page_slug\'],
            __(\'Dev Mode Info\', \'nhp-opts\'), 
            __(\'Dev Mode Info\', \'nhp-opts\'), 
            $this->args[\'page_cap\'], 
            $this->args[\'page_slug\'].\'&tab=dev_mode_default\', 
            create_function( \'$a\', "return null;" )
    );
To :

        add_theme_page(
                $this->args[\'page_slug\'],
                __(\'Dev Mode Info\', \'nhp-opts\'), 
                __(\'Dev Mode Info\', \'nhp-opts\'), 
                $this->args[\'page_cap\'], 
                $this->args[\'page_slug\'].\'&tab=dev_mode_default\', 
                function($a){ return null; }
        );
完整代码更改:

function _options_page(){
    if($this->args[\'page_type\'] == \'submenu\'){
        if(!isset($this->args[\'page_parent\']) || empty($this->args[\'page_parent\'])){
            $this->args[\'page_parent\'] = \'themes.php\';
        }
        $this->page = add_theme_page(
                        $this->args[\'page_parent\'],
                        $this->args[\'page_title\'], 
                        $this->args[\'menu_title\'], 
                        $this->args[\'page_cap\'], 
                        $this->args[\'page_slug\'], 
                        array(&$this, \'_options_page_html\')
                    );
    }else{
        $this->page = add_theme_page(
                        $this->args[\'page_title\'], 
                        $this->args[\'menu_title\'], 
                        $this->args[\'page_cap\'], 
                        $this->args[\'page_slug\'], 
                        array(&$this, \'_options_page_html\'),
                        $this->args[\'menu_icon\'],
                        $this->args[\'page_position\']
                    );

    if(true === $this->args[\'allow_sub_menu\']){

        //this is needed to remove the top level menu item from showing in the submenu
        add_theme_page(
          $this->args[\'page_slug\'],
          $this->args[\'page_title\'],
          \'\',
          $this->args[\'page_cap\'],
          $this->args[\'page_slug\'],
          function($a){ return null; }
        );


        foreach($this->sections as $k => $section){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    $section[\'title\'], 
                    $section[\'title\'], 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=\'.$k, 
                    function($a){ return null; }
            );

        }

        if(true === $this->args[\'show_import_export\']){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    __(\'Import / Export\', \'nhp-opts\'), 
                    __(\'Import / Export\', \'nhp-opts\'), 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=import_export_default\', 
                    function($a){ return null; }
            );

        }//if

        foreach($this->extra_tabs as $k => $tab){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    $tab[\'title\'], 
                    $tab[\'title\'], 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=\'.$k, 
                    function($a){ return null; }
            );

        }
        if(true === $this->args[\'dev_mode\']){

            add_theme_page(
                    $this->args[\'page_slug\'],
                    __(\'Dev Mode Info\', \'nhp-opts\'), 
                    __(\'Dev Mode Info\', \'nhp-opts\'), 
                    $this->args[\'page_cap\'], 
                    $this->args[\'page_slug\'].\'&tab=dev_mode_default\', 
                    function($a){ return null; }
            );

        }//if
    }//if           


    }//else
    add_action(\'admin_print_styles-\'.$this->page, array(&$this, \'_enqueue\'));
    add_action(\'load-\'.$this->page, array(&$this, \'_load_page\'));
}//function

相关推荐

初学者问题:通过管理Web界面访问Functions.php以导入自定义帖子类型?

是否可以访问这些功能。php文件仅仅使用管理web界面?我正在尝试访问以前创建的(手动编码的)自定义帖子类型,我不得不跳过很多障碍,因为我无法访问函数中的代码。php文件。我已经浏览了很多帮助页面,但建议的步骤似乎总是涉及到函数。php文件(我无法访问)或使用插件中的导入/导出工具,该插件首先创建了自定义帖子类型(据我所知,没有使用任何插件)。这似乎是一个非常基本的问题,但我一辈子都想不出来。任何帮助都将不胜感激!