为什么我的手写插件安装找不到`REQUEST_FILESYSTEM_Credentials`?

时间:2018-06-01 作者:coolpasta

我试图编写一个插件安装程序,类似于TGMPA,但仅仅是为了它的核心功能,即实际安装插件。

我已经确认了Plugin_Upgrader 是我所需要的,并决定效仿这一点。

$plugin = THEME_DIR . \'/Inc/Plugins/my-shortcodes.zip\';
$options = array(
        \'package\' => $plugin,
        \'destination\' => WP_PLUGIN_DIR,
        \'clear_destination\' => false,
        \'clear_working\' => true,
        \'is_multi\' => true,
        \'hook_extra\' => array(
            \'plugin\' => $plugin,
        ),
);

require_once ABSPATH . \'wp-admin/includes/class-wp-upgrader.php\';

class My_Upgrader extends Plugin_Upgrader
{
    public function run( $options )
    {
        $result = parent::run( $options );
        return $result;
    }
}

$upgrader = new My_Upgrader;
$upgrader->run($options);
$upgrader->install( $plugin );
很遗憾,这会引发以下错误:

Uncaught Error: Call to undefined function request_filesystem_credentials()     
in C:\\xampp\\htdocs\\wordpress\\wp-admin\\includes\\class-wp-upgrader-skin.php:93     
Stack trace: #0 C:\\xampp\\htdocs\\wordpress\\wp-admin\\includes\\class-wp- 
upgrader.php(187): WP_Upgrader_Skin->request_filesystem_credentials(false, 
\'C:\\\\xampp\\\\htdocs...\', false) #1 C:\\xampp\\htdocs\\wordpress\\wp- 
admin\\includes\\class-wp-upgrader.php(693): WP_Upgrader->fs_connect(Array) #2 
C:\\xampp\\htdocs\\wordpress\\wp-content\\themes\\amaranth\\header.php(53): 
WP_Upgrader->run(Array) #3 C:\\xampp\\htdocs\\wordpress\\wp- 
content\\themes\\amaranth\\header.php(59): My_Upgrader->run(Array) #4 
C:\\xampp\\htdocs\\wordpress\\wp-includes\\template.php(688): 
require_once(\'C:\\\\xampp\\\\htdocs...\') #5 C:\\xampp\\htdocs\\wordpress\\wp- 
includes\\template.php(647): load_template(\'C:\\\\xampp\\\\htdocs...\', true) #6 
C:\\xampp\\htdocs\\wordpress\\wp-includes\\general-template.php(41): 
locate_template(Array, true) #7 C:\\xampp\\htdocs\\wordpress\\wp- 
content\\themes\\amaranth\\index.php(15): get_header() #8 C:\\xampp\\htdoc in 
C:\\xampp\\htdocs\\wordpress\\wp-admin\\includes\\class-wp-upgrader-skin.php on 
line 93
这和它的。。。皮我错过什么了吗?

注意事项:

据我所知,如果文件系统模块无法写入,它将默认为FTPwp-content, 因此,可能会弹出此错误,但事实并非如此。WP可以写入wp-content.

Disclaimer: This is a very dumb installer, but I gotta start somewhere. Any suggestions are welcome.

2 个回复
SO网友:Nathan Johnson

错误消息显示request_filesystem_credentials() 未定义。因此,请确保函数是通过在之前包含文件来定义的class-wp-upgrader.php.

include_once ABSPATH . \'/wp-admin/includes/file.php\' );

SO网友:coolpasta

我忘了包括这些功能工作所需的文件。

因此,以下是安装插件的快捷方法:

$plugin = THEME_DIR . \'/Inc/Plugins/my-shortcodes.zip\';
$options = array(
        \'package\' => $plugin,
        \'destination\' => WP_PLUGIN_DIR,
        \'clear_destination\' => false,
        \'clear_working\' => true,
        \'is_multi\' => true,
        \'hook_extra\' => array(
            \'plugin\' => $plugin,
        ),
);

require_once ABSPATH  . \'wp-admin/includes/misc.php\';

if(!function_exists(\'request_filesystem_credentials\')){
    require_once ABSPATH  . \'wp-admin/includes/file.php\';
}

if( !class_exists(\'\\Plugin_Upgrader\')) {
    require_once ABSPATH . \'wp-admin/includes/class-wp-upgrader.php\';
}

class My_Upgrader extends Plugin_Upgrader
{
    public function run( $options )
    {
        $result = parent::run( $options );
        return $result;
    }
}

$upgrader = new My_Upgrader;
$upgrader->run($options);
$upgrader->install( $plugin );

结束