class-language-pack-upgrader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * Upgrade API: Language_Pack_Upgrader class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for updating/installing language packs (translations)
  11. * for plugins, themes, and core.
  12. *
  13. * @since 3.7.0
  14. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  15. *
  16. * @see WP_Upgrader
  17. */
  18. class Language_Pack_Upgrader extends WP_Upgrader {
  19. /**
  20. * Result of the language pack upgrade.
  21. *
  22. * @since 3.7.0
  23. * @var array|WP_Error $result
  24. * @see WP_Upgrader::$result
  25. */
  26. public $result;
  27. /**
  28. * Whether a bulk upgrade/installation is being performed.
  29. *
  30. * @since 3.7.0
  31. * @var bool $bulk
  32. */
  33. public $bulk = true;
  34. /**
  35. * Asynchronously upgrades language packs after other upgrades have been made.
  36. *
  37. * Hooked to the {@see 'upgrader_process_complete'} action by default.
  38. *
  39. * @since 3.7.0
  40. * @static
  41. *
  42. * @param false|WP_Upgrader $upgrader Optional. WP_Upgrader instance or false. If `$upgrader` is
  43. * a Language_Pack_Upgrader instance, the method will bail to
  44. * avoid recursion. Otherwise unused. Default false.
  45. */
  46. public static function async_upgrade( $upgrader = false ) {
  47. // Avoid recursion.
  48. if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader ) {
  49. return;
  50. }
  51. // Nothing to do?
  52. $language_updates = wp_get_translation_updates();
  53. if ( ! $language_updates ) {
  54. return;
  55. }
  56. /*
  57. * Avoid messing with VCS installations, at least for now.
  58. * Noted: this is not the ideal way to accomplish this.
  59. */
  60. $check_vcs = new WP_Automatic_Updater;
  61. if ( $check_vcs->is_vcs_checkout( WP_CONTENT_DIR ) ) {
  62. return;
  63. }
  64. foreach ( $language_updates as $key => $language_update ) {
  65. $update = ! empty( $language_update->autoupdate );
  66. /**
  67. * Filters whether to asynchronously update translation for core, a plugin, or a theme.
  68. *
  69. * @since 4.0.0
  70. *
  71. * @param bool $update Whether to update.
  72. * @param object $language_update The update offer.
  73. */
  74. $update = apply_filters( 'async_update_translation', $update, $language_update );
  75. if ( ! $update ) {
  76. unset( $language_updates[ $key ] );
  77. }
  78. }
  79. if ( empty( $language_updates ) ) {
  80. return;
  81. }
  82. // Re-use the automatic upgrader skin if the parent upgrader is using it.
  83. if ( $upgrader && $upgrader->skin instanceof Automatic_Upgrader_Skin ) {
  84. $skin = $upgrader->skin;
  85. } else {
  86. $skin = new Language_Pack_Upgrader_Skin( array(
  87. 'skip_header_footer' => true,
  88. ) );
  89. }
  90. $lp_upgrader = new Language_Pack_Upgrader( $skin );
  91. $lp_upgrader->bulk_upgrade( $language_updates );
  92. }
  93. /**
  94. * Initialize the upgrade strings.
  95. *
  96. * @since 3.7.0
  97. */
  98. public function upgrade_strings() {
  99. $this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while we update them as well.' );
  100. $this->strings['up_to_date'] = __( 'The translations are up to date.' );
  101. $this->strings['no_package'] = __( 'Update package not available.' );
  102. /* translators: %s: package URL */
  103. $this->strings['downloading_package'] = sprintf( __( 'Downloading translation from %s&#8230;' ), '<span class="code">%s</span>' );
  104. $this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
  105. $this->strings['process_failed'] = __( 'Translation update failed.' );
  106. $this->strings['process_success'] = __( 'Translation updated successfully.' );
  107. }
  108. /**
  109. * Upgrade a language pack.
  110. *
  111. * @since 3.7.0
  112. *
  113. * @param string|false $update Optional. Whether an update offer is available. Default false.
  114. * @param array $args Optional. Other optional arguments, see
  115. * Language_Pack_Upgrader::bulk_upgrade(). Default empty array.
  116. * @return array|bool|WP_Error The result of the upgrade, or a WP_Error object instead.
  117. */
  118. public function upgrade( $update = false, $args = array() ) {
  119. if ( $update ) {
  120. $update = array( $update );
  121. }
  122. $results = $this->bulk_upgrade( $update, $args );
  123. if ( ! is_array( $results ) ) {
  124. return $results;
  125. }
  126. return $results[0];
  127. }
  128. /**
  129. * Bulk upgrade language packs.
  130. *
  131. * @since 3.7.0
  132. *
  133. * @global WP_Filesystem_Base $wp_filesystem Subclass
  134. *
  135. * @param array $language_updates Optional. Language pack updates. Default empty array.
  136. * @param array $args {
  137. * Optional. Other arguments for upgrading multiple language packs. Default empty array
  138. *
  139. * @type bool $clear_update_cache Whether to clear the update cache when done.
  140. * Default true.
  141. * }
  142. * @return array|bool|WP_Error Will return an array of results, or true if there are no updates,
  143. * false or WP_Error for initial errors.
  144. */
  145. public function bulk_upgrade( $language_updates = array(), $args = array() ) {
  146. global $wp_filesystem;
  147. $defaults = array(
  148. 'clear_update_cache' => true,
  149. );
  150. $parsed_args = wp_parse_args( $args, $defaults );
  151. $this->init();
  152. $this->upgrade_strings();
  153. if ( ! $language_updates )
  154. $language_updates = wp_get_translation_updates();
  155. if ( empty( $language_updates ) ) {
  156. $this->skin->header();
  157. $this->skin->set_result( true );
  158. $this->skin->feedback( 'up_to_date' );
  159. $this->skin->bulk_footer();
  160. $this->skin->footer();
  161. return true;
  162. }
  163. if ( 'upgrader_process_complete' == current_filter() )
  164. $this->skin->feedback( 'starting_upgrade' );
  165. // Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230
  166. remove_all_filters( 'upgrader_pre_install' );
  167. remove_all_filters( 'upgrader_clear_destination' );
  168. remove_all_filters( 'upgrader_post_install' );
  169. remove_all_filters( 'upgrader_source_selection' );
  170. add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 );
  171. $this->skin->header();
  172. // Connect to the Filesystem first.
  173. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
  174. if ( ! $res ) {
  175. $this->skin->footer();
  176. return false;
  177. }
  178. $results = array();
  179. $this->update_count = count( $language_updates );
  180. $this->update_current = 0;
  181. /*
  182. * The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
  183. * as we then may need to create a /plugins or /themes directory inside of it.
  184. */
  185. $remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
  186. if ( ! $wp_filesystem->exists( $remote_destination ) )
  187. if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) )
  188. return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
  189. $language_updates_results = array();
  190. foreach ( $language_updates as $language_update ) {
  191. $this->skin->language_update = $language_update;
  192. $destination = WP_LANG_DIR;
  193. if ( 'plugin' == $language_update->type )
  194. $destination .= '/plugins';
  195. elseif ( 'theme' == $language_update->type )
  196. $destination .= '/themes';
  197. $this->update_current++;
  198. $options = array(
  199. 'package' => $language_update->package,
  200. 'destination' => $destination,
  201. 'clear_destination' => false,
  202. 'abort_if_destination_exists' => false, // We expect the destination to exist.
  203. 'clear_working' => true,
  204. 'is_multi' => true,
  205. 'hook_extra' => array(
  206. 'language_update_type' => $language_update->type,
  207. 'language_update' => $language_update,
  208. )
  209. );
  210. $result = $this->run( $options );
  211. $results[] = $this->result;
  212. // Prevent credentials auth screen from displaying multiple times.
  213. if ( false === $result ) {
  214. break;
  215. }
  216. $language_updates_results[] = array(
  217. 'language' => $language_update->language,
  218. 'type' => $language_update->type,
  219. 'slug' => isset( $language_update->slug ) ? $language_update->slug : 'default',
  220. 'version' => $language_update->version,
  221. );
  222. }
  223. // Remove upgrade hooks which are not required for translation updates.
  224. remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
  225. remove_action( 'upgrader_process_complete', 'wp_version_check' );
  226. remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
  227. remove_action( 'upgrader_process_complete', 'wp_update_themes' );
  228. /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
  229. do_action( 'upgrader_process_complete', $this, array(
  230. 'action' => 'update',
  231. 'type' => 'translation',
  232. 'bulk' => true,
  233. 'translations' => $language_updates_results
  234. ) );
  235. // Re-add upgrade hooks.
  236. add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
  237. add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
  238. add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
  239. add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
  240. $this->skin->bulk_footer();
  241. $this->skin->footer();
  242. // Clean up our hooks, in case something else does an upgrade on this connection.
  243. remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
  244. if ( $parsed_args['clear_update_cache'] ) {
  245. wp_clean_update_cache();
  246. }
  247. return $results;
  248. }
  249. /**
  250. * Check the package source to make sure there are .mo and .po files.
  251. *
  252. * Hooked to the {@see 'upgrader_source_selection'} filter by
  253. * Language_Pack_Upgrader::bulk_upgrade().
  254. *
  255. * @since 3.7.0
  256. *
  257. * @global WP_Filesystem_Base $wp_filesystem Subclass
  258. *
  259. * @param string|WP_Error $source
  260. * @param string $remote_source
  261. */
  262. public function check_package( $source, $remote_source ) {
  263. global $wp_filesystem;
  264. if ( is_wp_error( $source ) )
  265. return $source;
  266. // Check that the folder contains a valid language.
  267. $files = $wp_filesystem->dirlist( $remote_source );
  268. // Check to see if a .po and .mo exist in the folder.
  269. $po = $mo = false;
  270. foreach ( (array) $files as $file => $filedata ) {
  271. if ( '.po' == substr( $file, -3 ) )
  272. $po = true;
  273. elseif ( '.mo' == substr( $file, -3 ) )
  274. $mo = true;
  275. }
  276. if ( ! $mo || ! $po ) {
  277. return new WP_Error( 'incompatible_archive_pomo', $this->strings['incompatible_archive'],
  278. /* translators: 1: .po 2: .mo */
  279. sprintf( __( 'The language pack is missing either the %1$s or %2$s files.' ),
  280. '<code>.po</code>',
  281. '<code>.mo</code>'
  282. )
  283. );
  284. }
  285. return $source;
  286. }
  287. /**
  288. * Get the name of an item being updated.
  289. *
  290. * @since 3.7.0
  291. *
  292. * @param object $update The data for an update.
  293. * @return string The name of the item being updated.
  294. */
  295. public function get_name_for_update( $update ) {
  296. switch ( $update->type ) {
  297. case 'core':
  298. return 'WordPress'; // Not translated
  299. case 'theme':
  300. $theme = wp_get_theme( $update->slug );
  301. if ( $theme->exists() )
  302. return $theme->Get( 'Name' );
  303. break;
  304. case 'plugin':
  305. $plugin_data = get_plugins( '/' . $update->slug );
  306. $plugin_data = reset( $plugin_data );
  307. if ( $plugin_data )
  308. return $plugin_data['Name'];
  309. break;
  310. }
  311. return '';
  312. }
  313. }