好的,我在短期内对它进行了一些研究/测试:
Don\'t use uppercase in the first place!
我目前只注意到一个问题,即插件概述中基于ajax的更新(在WP 4.2中介绍)会引发JS错误,但这可能是将来的问题。
这是因为ajax响应使用sanitize_key
在wp_ajax_update_plugin
方法(browse in trac).
The sanitize_key
method 消毒a旁边是否有strtolower
这会导致“myPlugin
“获取”myplugin
“。
这个updateSuccess
方法现在尝试更新slug所在的行“myplugin
“代替”myPlugin
“。
可以通过更改line 694 在里面wp-admin/includes/class-wp-plugins-list-table.php
从…起
printf( "<tr id=\'%s\' class=\'%s\' data-slug=\'%s\'>",
$id,
$class,
$plugin_slug
);
至
printf( "<tr id=\'%s\' class=\'%s\' data-slug=\'%s\'>",
$id,
$class,
sanitize_key($plugin_slug)
);
待办事项:公开票证
Ticket #35032To answer the initial question
您可以在插件更新时更改slug,但必须注意以下事项:
以下代码必须包含在旧版本中(例如\'myPlugin/myPlugin.php)
更新版本must 有一个\'myplugin/myplugin的slug。php不使用delete_plugin()
这可能会触发卸载。php和删除插件数据//hook into when new plugin has been successfully updated
add_filter( \'upgrader_post_install\', \'my_move_plugin_slug\', 10 ,3 );
function my_move_plugin_slug($should_be_true, $hook_extra, $result){
global $wpdb, $wp_filesystem;
$from = \'myPlugin/myPlugin.php\';
$to = \'myplugin/myplugin.php\';
//some general checks
if(!$should_be_true || !isset($hook_extra[\'plugin\']) || $hook_extra[\'plugin\'] != $to) return $should_be_true;
$old_destination = $result[\'local_destination\'].\'/\'.dirname($from).\'/\';
//old location doesn\'t exist (anymore)
if(!is_dir($old_destination)) return $should_be_true;
//new location is the same as the old one
if($old_destination == $result[\'destination\']) return $should_be_true;
//do the magic
//rewrite location in the database
echo \'<p>Moving the plugin to new location… \';
if(false !== $wpdb->query($wpdb->prepare("UPDATE {$wpdb->options} SET `option_value` = replace(option_value, %s, %s)", $from, $to))){
echo \'<strong>done</strong>\';
}
echo \'</p>\';
//delete folder
echo \'<p>Removing the old directory of the plugin… \';
if($wp_filesystem->delete( $old_destination , true )){
echo \'<strong>done</strong>\';
}
echo \'</p>\';
}
Second method
如果你有一个像我这样的插件,专门的批量更新进度,你可以使用这段代码,我认为这段代码更安全,因为它是由显式用户交互触发的,不需要wp\\U文件系统:function do_change_plugin_slug(){
global $wpdb;
$from = \'myMail/myMail.php\';
$to = \'mymail/mymail.php\';
$old_destination = WP_PLUGIN_DIR.\'/\'.$from;
$new_destination = WP_PLUGIN_DIR.\'/\'.$to;
//old location doesn\'t exist (anymore)
if(!file_exists($old_destination)) return true;
//new location is the same as the old one
if($old_destination == $new_destination) return true;
//do the magic
echo \'Removing the old file of the plugin… \';
if(rename( $old_destination , dirname($old_destination).\'/\'.basename($new_destination) )){
echo \'done\';
}else{
echo \'failed\';
}
echo "\\n";
echo \'Removing the old directory of the plugin… \';
if(rename( dirname($old_destination) , dirname($new_destination) )){
echo \'done\';
}else{
echo \'failed\';
}
echo "\\n";
//rewrite location in the database
echo \'Moving the plugin to new location… \';
if(false !== $wpdb->query($wpdb->prepare("UPDATE {$wpdb->options} SET `option_value` = replace(option_value, %s, %s)", $from, $to))){
echo \'done\';
}else{
echo \'failed\';
}
echo "\\n";
return true;
}
最后,您应该始终确保用户了解发生了什么和/或为什么这是一个必要的步骤。