/includes/class-wc-language-pack-upgrader.php

https://github.com/crowdfavorite/woocommerce · PHP · 231 lines · 119 code · 35 blank · 77 comment · 31 complexity · 31997a8457dc11be168488a5d9bf5741 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WooCommerce Language Pack Upgrader class
  7. *
  8. * Downloads the last language pack.
  9. *
  10. * @class WC_Language_Pack_Upgrader
  11. * @version 2.2.0
  12. * @package WooCommerce/Classes/Language
  13. * @category Class
  14. * @author WooThemes
  15. */
  16. class WC_Language_Pack_Upgrader {
  17. /**
  18. * Languages repository
  19. *
  20. * @var string
  21. */
  22. protected $repo = 'https://github.com/woothemes/woocommerce-language-packs/raw/v';
  23. /**
  24. * Initialize the language pack upgrader
  25. */
  26. public function __construct() {
  27. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
  28. add_filter( 'upgrader_pre_download', array( $this, 'version_update' ), 10, 2 );
  29. add_action( 'woocommerce_installed', array( $this, 'has_available_update' ) );
  30. add_filter( 'admin_init', array( $this, 'manual_language_update' ), 999 );
  31. }
  32. /**
  33. * Get language package URI.
  34. *
  35. * @return string
  36. */
  37. public function get_language_package_uri() {
  38. return $this->repo . WC_VERSION . '/packages/' . get_locale() . '.zip';
  39. }
  40. /**
  41. * Check for language updates
  42. *
  43. * @param object $data Transient update data
  44. *
  45. * @return object
  46. */
  47. public function check_for_update( $data ) {
  48. if ( $this->has_available_update() ) {
  49. $data->translations[] = array(
  50. 'type' => 'plugin',
  51. 'slug' => 'woocommerce',
  52. 'language' => get_locale(),
  53. 'version' => WC_VERSION,
  54. 'updated' => date( 'Y-m-d H:i:s' ),
  55. 'package' => $this->get_language_package_uri(),
  56. 'autoupdate' => 1
  57. );
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Check if has available translation update
  63. *
  64. * @return bool
  65. */
  66. public function has_available_update() {
  67. $locale = get_locale();
  68. if ( 'en_US' !== $locale ) {
  69. return false;
  70. }
  71. $version = get_option( 'woocommerce_language_pack_version', array( '0', $locale ) );
  72. if ( ! is_array( $version ) || version_compare( $version[0], WC_VERSION, '<' ) || $version[1] !== $locale ) {
  73. if ( $this->check_if_language_pack_exists() ) {
  74. $this->configure_woocommerce_upgrade_notice();
  75. return true;
  76. } else {
  77. // Updated the woocommerce_language_pack_version to avoid searching translations for this release again
  78. update_option( 'woocommerce_language_pack_version', array( WC_VERSION , get_locale() ) );
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * Configure the WooCommerce translation upgrade notice
  85. *
  86. * @return void
  87. */
  88. public function configure_woocommerce_upgrade_notice() {
  89. WC_Admin_Notices::add_notice( 'translation_upgrade' );
  90. }
  91. /**
  92. * Check if language pack exists
  93. *
  94. * @return bool
  95. */
  96. public function check_if_language_pack_exists() {
  97. $response = wp_remote_get( $this->get_language_package_uri(), array( 'sslverify' => false, 'timeout' => 60 ) );
  98. if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104. /**
  105. * Update the language version in database
  106. *
  107. * This updates the database while the download the translation package and ensures that not generate download loop
  108. * If the installation fails you can redo it in: WooCommerce > Sistem Status > Tools > Force Translation Upgrade
  109. *
  110. * @param bool $reply Whether to bail without returning the package (default: false)
  111. * @param string $package Package URL
  112. *
  113. * @return bool
  114. */
  115. public function version_update( $reply, $package ) {
  116. if ( $package === $this->get_language_package_uri() ) {
  117. $this->save_language_version();
  118. }
  119. return $reply;
  120. }
  121. /**
  122. * Save language version
  123. *
  124. * @return void
  125. */
  126. protected function save_language_version() {
  127. // Update the language pack version
  128. update_option( 'woocommerce_language_pack_version', array( WC_VERSION , get_locale() ) );
  129. // Remove the translation upgrade notice
  130. $notices = get_option( 'woocommerce_admin_notices', array() );
  131. $notices = array_diff( $notices, array( 'translation_upgrade' ) );
  132. update_option( 'woocommerce_admin_notices', $notices );
  133. }
  134. /**
  135. * Manual language update
  136. *
  137. * @return void
  138. */
  139. public function manual_language_update() {
  140. if (
  141. is_admin()
  142. && current_user_can( 'update_plugins' )
  143. && isset( $_GET['page'] )
  144. && 'wc-status' == $_GET['page']
  145. && isset( $_GET['action'] )
  146. && 'translation_upgrade' == $_GET['action']
  147. ) {
  148. $url = wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=translation_upgrade' ), 'language_update' );
  149. $tools_url = admin_url( 'admin.php?page=wc-status&tab=tools' );
  150. if ( ! isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) ) {
  151. wp_redirect( add_query_arg( array( 'translation_updated' => 2 ), $tools_url ) );
  152. exit;
  153. }
  154. if ( false === ( $creds = request_filesystem_credentials( $url, '', false, false, null ) ) ) {
  155. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  156. exit;
  157. }
  158. if ( ! WP_Filesystem( $creds ) ) {
  159. request_filesystem_credentials( $url, '', true, false, null );
  160. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  161. exit;
  162. }
  163. // Download the language pack
  164. $response = wp_remote_get( $this->get_language_package_uri(), array( 'sslverify' => false, 'timeout' => 60 ) );
  165. if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  166. global $wp_filesystem;
  167. $upload_dir = wp_upload_dir();
  168. $file = trailingslashit( $upload_dir['path'] ) . get_locale() . '.zip';
  169. // Save the zip file
  170. if ( ! $wp_filesystem->put_contents( $file, $response['body'], FS_CHMOD_FILE ) ) {
  171. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  172. exit;
  173. }
  174. // Unzip the file to wp-content/languages/plugins directory
  175. $dir = trailingslashit( WP_LANG_DIR ) . 'plugins/';
  176. $unzip = unzip_file( $file, $dir );
  177. if ( true !== $unzip ) {
  178. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  179. exit;
  180. }
  181. // Delete the package file
  182. $wp_filesystem->delete( $file );
  183. // Update the language pack version
  184. $this->save_language_version();
  185. // Redirect and show a success message
  186. wp_redirect( add_query_arg( array( 'translation_updated' => 1 ), $tools_url ) );
  187. exit;
  188. } else {
  189. // Don't have a valid package for the current language!
  190. wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $tools_url ) );
  191. exit;
  192. }
  193. }
  194. }
  195. }
  196. new WC_Language_Pack_Upgrader();