Language packs: Remove translations when deleting a theme or a plugin.

This is for translation files in WP_LANG_DIR which are installed through a language pack.
Change `wp_get_installed_translations()` to only return a translation if the .mo file also exists.

fixes #29860.
Built from https://develop.svn.wordpress.org/trunk@29856


git-svn-id: http://core.svn.wordpress.org/trunk@29619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2014-10-08 19:05:20 +00:00
parent 5f17e0952d
commit 57c41263d7
5 changed files with 134 additions and 45 deletions

View File

@ -797,29 +797,50 @@ function delete_plugins( $plugins, $deprecated = '' ) {
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
//Get the base plugin folder // Get the base plugin folder.
$plugins_dir = $wp_filesystem->wp_plugins_dir(); $plugins_dir = $wp_filesystem->wp_plugins_dir();
if ( empty($plugins_dir) ) if ( empty( $plugins_dir ) ) {
return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.')); return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress Plugin directory.' ) );
}
$plugins_dir = trailingslashit( $plugins_dir ); $plugins_dir = trailingslashit( $plugins_dir );
$translations_dir = $wp_filesystem->wp_lang_dir();
$translations_dir = trailingslashit( $translations_dir );
$plugin_translations = wp_get_installed_translations( 'plugins' );
$errors = array(); $errors = array();
foreach( $plugins as $plugin_file ) { foreach( $plugins as $plugin_file ) {
// Run Uninstall hook // Run Uninstall hook.
if ( is_uninstallable_plugin( $plugin_file ) ) if ( is_uninstallable_plugin( $plugin_file ) ) {
uninstall_plugin($plugin_file); uninstall_plugin($plugin_file);
}
$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) ); $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
// If plugin is in its own directory, recursively delete the directory. // If plugin is in its own directory, recursively delete the directory.
if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
$deleted = $wp_filesystem->delete($this_plugin_dir, true); $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
else } else {
$deleted = $wp_filesystem->delete($plugins_dir . $plugin_file); $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
}
if ( ! $deleted ) if ( ! $deleted ) {
$errors[] = $plugin_file; $errors[] = $plugin_file;
continue;
}
// Remove language files, silently.
$plugin_slug = dirname( $plugin_file );
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
$translations = $plugin_translations[ $plugin_slug ];
foreach ( $translations as $translation => $data ) {
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
}
}
} }
// Remove deleted plugins from the plugin updates list. // Remove deleted plugins from the plugin updates list.

View File

@ -55,20 +55,37 @@ function delete_theme($stylesheet, $redirect = '') {
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
//Get the base plugin folder // Get the base plugin folder.
$themes_dir = $wp_filesystem->wp_themes_dir(); $themes_dir = $wp_filesystem->wp_themes_dir();
if ( empty($themes_dir) ) if ( empty( $themes_dir ) ) {
return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.')); return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
}
$themes_dir = trailingslashit( $themes_dir ); $themes_dir = trailingslashit( $themes_dir );
$theme_dir = trailingslashit($themes_dir . $stylesheet); $theme_dir = trailingslashit( $themes_dir . $stylesheet );
$deleted = $wp_filesystem->delete($theme_dir, true); $deleted = $wp_filesystem->delete( $theme_dir, true );
if ( ! $deleted ) if ( ! $deleted ) {
return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $stylesheet) ); return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
}
// Force refresh of theme update information $translations_dir = $wp_filesystem->wp_lang_dir();
delete_site_transient('update_themes'); $translations_dir = trailingslashit( $translations_dir );
$theme_translations = wp_get_installed_translations( 'themes' );
// Remove language files, silently.
if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
$translations = $theme_translations[ $stylesheet ];
foreach ( $translations as $translation => $data ) {
$wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
$wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
}
}
// Force refresh of theme update information.
delete_site_transient( 'update_themes' );
return true; return true;
} }

View File

@ -96,8 +96,10 @@ if ( $action ) {
require_once(ABSPATH . 'wp-admin/admin-footer.php'); require_once(ABSPATH . 'wp-admin/admin-footer.php');
exit; exit;
case 'delete-selected': case 'delete-selected':
if ( ! current_user_can( 'delete_themes' ) ) if ( ! current_user_can( 'delete_themes' ) ) {
wp_die( __('You do not have sufficient permissions to delete themes for this site.') ); wp_die( __('You do not have sufficient permissions to delete themes for this site.') );
}
check_admin_referer( 'bulk-themes' ); check_admin_referer( 'bulk-themes' );
$themes = isset( $_REQUEST['checked'] ) ? (array) $_REQUEST['checked'] : array(); $themes = isset( $_REQUEST['checked'] ) ? (array) $_REQUEST['checked'] : array();
@ -115,9 +117,26 @@ if ( $action ) {
} }
$files_to_delete = $theme_info = array(); $files_to_delete = $theme_info = array();
$theme_translations = wp_get_installed_translations( 'themes' );
foreach ( $themes as $key => $theme ) { foreach ( $themes as $key => $theme ) {
$theme_info[ $theme ] = wp_get_theme( $theme ); $theme_info[ $theme ] = wp_get_theme( $theme );
$files_to_delete = array_merge( $files_to_delete, list_files( $theme_info[ $theme ]->get_stylesheet_directory() ) );
// Locate all the files in that folder.
$files = list_files( $theme_info[ $theme ]->get_stylesheet_directory() );
if ( $files ) {
$files_to_delete = array_merge( $files_to_delete, $files );
}
// Add translation files.
$theme_slug = $theme_info[ $theme ]->get_stylesheet();
if ( ! empty( $theme_translations[ $theme_slug ] ) ) {
$translations = $theme_translations[ $theme_slug ];
foreach ( $translations as $translation => $data ) {
$files_to_delete[] = $theme_slug . '-' . $translation . '.po';
$files_to_delete[] = $theme_slug . '-' . $translation . '.mo';
}
}
} }
include(ABSPATH . 'wp-admin/update.php'); include(ABSPATH . 'wp-admin/update.php');
@ -136,16 +155,21 @@ if ( $action ) {
<div class="error"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php echo _n( 'This theme may be active on other sites in the network.', 'These themes may be active on other sites in the network.', $themes_to_delete ); ?></p></div> <div class="error"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php echo _n( 'This theme may be active on other sites in the network.', 'These themes may be active on other sites in the network.', $themes_to_delete ); ?></p></div>
<p><?php echo _n( 'You are about to remove the following theme:', 'You are about to remove the following themes:', $themes_to_delete ); ?></p> <p><?php echo _n( 'You are about to remove the following theme:', 'You are about to remove the following themes:', $themes_to_delete ); ?></p>
<ul class="ul-disc"> <ul class="ul-disc">
<?php foreach ( $theme_info as $theme ) <?php
echo '<li>', sprintf( __('<strong>%1$s</strong> by <em>%2$s</em>' ), $theme->display('Name'), $theme->display('Author') ), '</li>'; /* translators: 1: theme name, 2: theme author */ ?> foreach ( $theme_info as $theme ) {
/* translators: 1: theme name, 2: theme author */
echo '<li>', sprintf( __('<strong>%1$s</strong> by <em>%2$s</em>' ), $theme->display('Name'), $theme->display('Author') ), '</li>';
}
?>
</ul> </ul>
<p><?php _e('Are you sure you wish to delete these themes?'); ?></p> <p><?php _e('Are you sure you wish to delete these themes?'); ?></p>
<form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" style="display:inline;"> <form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" style="display:inline;">
<input type="hidden" name="verify-delete" value="1" /> <input type="hidden" name="verify-delete" value="1" />
<input type="hidden" name="action" value="delete-selected" /> <input type="hidden" name="action" value="delete-selected" />
<?php <?php
foreach ( (array) $themes as $theme ) foreach ( (array) $themes as $theme ) {
echo '<input type="hidden" name="checked[]" value="' . esc_attr($theme) . '" />'; echo '<input type="hidden" name="checked[]" value="' . esc_attr($theme) . '" />';
}
?> ?>
<?php wp_nonce_field('bulk-themes') ?> <?php wp_nonce_field('bulk-themes') ?>
<?php submit_button( _n( 'Yes, Delete this theme', 'Yes, Delete these themes', $themes_to_delete ), 'button', 'submit', false ); ?> <?php submit_button( _n( 'Yes, Delete this theme', 'Yes, Delete these themes', $themes_to_delete ), 'button', 'submit', false ); ?>
@ -158,8 +182,9 @@ if ( $action ) {
<div id="files-list" style="display:none;"> <div id="files-list" style="display:none;">
<ul class="code"> <ul class="code">
<?php <?php
foreach ( (array) $files_to_delete as $file ) foreach ( (array) $files_to_delete as $file ) {
echo '<li>' . esc_html( str_replace( WP_CONTENT_DIR . "/themes", '', $file) ) . '</li>'; echo '<li>' . esc_html( str_replace( WP_CONTENT_DIR . '/themes', '', $file ) ) . '</li>';
}
?> ?>
</ul> </ul>
</div> </div>

View File

@ -207,8 +207,9 @@ if ( $action ) {
exit; exit;
case 'delete-selected': case 'delete-selected':
if ( ! current_user_can('delete_plugins') ) if ( ! current_user_can('delete_plugins') ) {
wp_die(__('You do not have sufficient permissions to delete plugins for this site.')); wp_die(__('You do not have sufficient permissions to delete plugins for this site.'));
}
check_admin_referer('bulk-plugins'); check_admin_referer('bulk-plugins');
@ -237,28 +238,44 @@ if ( $action ) {
<?php <?php
$files_to_delete = $plugin_info = array(); $files_to_delete = $plugin_info = array();
$have_non_network_plugins = false; $have_non_network_plugins = false;
$plugin_translations = wp_get_installed_translations( 'plugins' );
foreach ( (array) $plugins as $plugin ) { foreach ( (array) $plugins as $plugin ) {
if ( '.' == dirname($plugin) ) { $plugin_slug = dirname( $plugin );
if ( '.' == $plugin_slug ) {
$files_to_delete[] = WP_PLUGIN_DIR . '/' . $plugin; $files_to_delete[] = WP_PLUGIN_DIR . '/' . $plugin;
if( $data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin) ) { if ( $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) ) {
$plugin_info[ $plugin ] = $data; $plugin_info[ $plugin ] = $data;
$plugin_info[ $plugin ]['is_uninstallable'] = is_uninstallable_plugin( $plugin ); $plugin_info[ $plugin ]['is_uninstallable'] = is_uninstallable_plugin( $plugin );
if ( ! $plugin_info[ $plugin ]['Network'] ) if ( ! $plugin_info[ $plugin ]['Network'] ) {
$have_non_network_plugins = true; $have_non_network_plugins = true;
}
} }
} else { } else {
// Locate all the files in that folder // Locate all the files in that folder.
$files = list_files( WP_PLUGIN_DIR . '/' . dirname($plugin) ); $files = list_files( WP_PLUGIN_DIR . '/' . $plugin_slug );
if ( $files ) { if ( $files ) {
$files_to_delete = array_merge($files_to_delete, $files); $files_to_delete = array_merge( $files_to_delete, $files );
} }
// Get plugins list from that folder
if ( $folder_plugins = get_plugins( '/' . dirname($plugin)) ) { // Get plugins list from that folder.
if ( $folder_plugins = get_plugins( '/' . $plugin_slug ) ) {
foreach( $folder_plugins as $plugin_file => $data ) { foreach( $folder_plugins as $plugin_file => $data ) {
$plugin_info[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $data ); $plugin_info[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $data );
$plugin_info[ $plugin_file ]['is_uninstallable'] = is_uninstallable_plugin( $plugin ); $plugin_info[ $plugin_file ]['is_uninstallable'] = is_uninstallable_plugin( $plugin );
if ( ! $plugin_info[ $plugin_file ]['Network'] ) if ( ! $plugin_info[ $plugin_file ]['Network'] ) {
$have_non_network_plugins = true; $have_non_network_plugins = true;
}
}
}
// Add translation files.
if ( ! empty( $plugin_translations[ $plugin_slug ] ) ) {
$translations = $plugin_translations[ $plugin_slug ];
foreach ( $translations as $translation => $data ) {
$files_to_delete[] = $plugin_slug . '-' . $translation . '.po';
$files_to_delete[] = $plugin_slug . '-' . $translation . '.mo';
} }
} }
} }
@ -295,8 +312,9 @@ if ( $action ) {
<input type="hidden" name="verify-delete" value="1" /> <input type="hidden" name="verify-delete" value="1" />
<input type="hidden" name="action" value="delete-selected" /> <input type="hidden" name="action" value="delete-selected" />
<?php <?php
foreach ( (array) $plugins as $plugin ) foreach ( (array) $plugins as $plugin ) {
echo '<input type="hidden" name="checked[]" value="' . esc_attr($plugin) . '" />'; echo '<input type="hidden" name="checked[]" value="' . esc_attr( $plugin ) . '" />';
}
?> ?>
<?php wp_nonce_field('bulk-plugins') ?> <?php wp_nonce_field('bulk-plugins') ?>
<?php submit_button( $data_to_delete ? __( 'Yes, Delete these files and data' ) : __( 'Yes, Delete these files' ), 'button', 'submit', false ); ?> <?php submit_button( $data_to_delete ? __( 'Yes, Delete these files and data' ) : __( 'Yes, Delete these files' ), 'button', 'submit', false ); ?>
@ -309,8 +327,9 @@ if ( $action ) {
<div id="files-list" style="display:none;"> <div id="files-list" style="display:none;">
<ul class="code"> <ul class="code">
<?php <?php
foreach ( (array)$files_to_delete as $file ) foreach ( (array) $files_to_delete as $file ) {
echo '<li>' . esc_html(str_replace(WP_PLUGIN_DIR, '', $file)) . '</li>'; echo '<li>' . esc_html( str_replace( WP_PLUGIN_DIR, '', $file ) ) . '</li>';
}
?> ?>
</ul> </ul>
</div> </div>

View File

@ -797,16 +797,23 @@ function wp_get_installed_translations( $type ) {
$language_data = array(); $language_data = array();
foreach ( $files as $file ) { foreach ( $files as $file ) {
if ( '.' === $file[0] || is_dir( $file ) ) if ( '.' === $file[0] || is_dir( $file ) ) {
continue; continue;
if ( substr( $file, -3 ) !== '.po' ) }
if ( substr( $file, -3 ) !== '.po' ) {
continue; continue;
if ( ! preg_match( '/(?:(.+)-)?([A-Za-z_]{2,6}).po/', $file, $match ) ) }
if ( ! preg_match( '/(?:(.+)-)?([A-Za-z_]{2,6}).po/', $file, $match ) ) {
continue; continue;
}
if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) {
continue;
}
list( , $textdomain, $language ) = $match; list( , $textdomain, $language ) = $match;
if ( '' === $textdomain ) if ( '' === $textdomain ) {
$textdomain = 'default'; $textdomain = 'default';
}
$language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" ); $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
} }
return $language_data; return $language_data;