PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-language-pack-upgrader.php

https://gitlab.com/webkod3r/tripolis
PHP | 273 lines | 152 code | 38 blank | 83 comment | 35 complexity | 7bc8478e2a955242851f4cc9f4edb35a 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.4.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 static $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( __CLASS__, 'has_available_update' ) );
  30. add_action( 'update_option_WPLANG', array( $this, 'updated_language_option' ), 10, 2 );
  31. add_filter( 'admin_init', array( $this, 'manual_language_update' ), 10 );
  32. }
  33. /**
  34. * Get language package URI.
  35. *
  36. * @return string
  37. */
  38. public static function get_language_package_uri( $locale = null ) {
  39. if ( is_null( $locale ) ) {
  40. $locale = get_locale();
  41. }
  42. return self::$repo . WC_VERSION . '/packages/' . $locale . '.zip';
  43. }
  44. /**
  45. * Check for language updates
  46. *
  47. * @param object $data Transient update data
  48. *
  49. * @return object
  50. */
  51. public function check_for_update( $data ) {
  52. if ( self::has_available_update() ) {
  53. $locale = get_locale();
  54. $data->translations[] = array(
  55. 'type' => 'plugin',
  56. 'slug' => 'woocommerce',
  57. 'language' => $locale,
  58. 'version' => WC_VERSION,
  59. 'updated' => date( 'Y-m-d H:i:s' ),
  60. 'package' => self::get_language_package_uri( $locale ),
  61. 'autoupdate' => 1
  62. );
  63. }
  64. return $data;
  65. }
  66. /**
  67. * Triggered when WPLANG is changed
  68. *
  69. * @param string $old
  70. * @param string $new
  71. */
  72. public function updated_language_option( $old, $new ) {
  73. self::has_available_update( $new );
  74. }
  75. /**
  76. * Check if has available translation update
  77. *
  78. * @return bool
  79. */
  80. public static function has_available_update( $locale = null ) {
  81. if ( is_null( $locale ) ) {
  82. $locale = get_locale();
  83. }
  84. if ( 'en_US' === $locale ) {
  85. return false;
  86. }
  87. $version = get_option( 'woocommerce_language_pack_version', array( '0', $locale ) );
  88. if ( ! is_array( $version ) || version_compare( $version[0], WC_VERSION, '<' ) || $version[1] !== $locale ) {
  89. if ( self::check_if_language_pack_exists( $locale ) ) {
  90. self::configure_woocommerce_upgrade_notice();
  91. return true;
  92. } else {
  93. // Updated the woocommerce_language_pack_version to avoid searching translations for this release again
  94. update_option( 'woocommerce_language_pack_version', array( WC_VERSION, $locale ) );
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * Configure the WooCommerce translation upgrade notice
  101. */
  102. public static function configure_woocommerce_upgrade_notice() {
  103. WC_Admin_Notices::add_notice( 'translation_upgrade' );
  104. }
  105. /**
  106. * Check if language pack exists
  107. *
  108. * @return bool
  109. */
  110. public static function check_if_language_pack_exists( $locale ) {
  111. $response = wp_safe_remote_get( self::get_language_package_uri( $locale ), array( 'timeout' => 60 ) );
  112. if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * Update the language version in database
  120. *
  121. * This updates the database while the download the translation package and ensures that not generate download loop
  122. * If the installation fails you can redo it in: WooCommerce > Sistem Status > Tools > Force Translation Upgrade
  123. *
  124. * @param bool $reply Whether to bail without returning the package (default: false)
  125. * @param string $package Package URL
  126. *
  127. * @return bool
  128. */
  129. public function version_update( $reply, $package ) {
  130. if ( $package === self::get_language_package_uri() ) {
  131. $this->save_language_version();
  132. }
  133. return $reply;
  134. }
  135. /**
  136. * Save language version
  137. */
  138. protected function save_language_version() {
  139. // Update the language pack version
  140. update_option( 'woocommerce_language_pack_version', array( WC_VERSION, get_locale() ) );
  141. // Remove the translation upgrade notice
  142. $notices = get_option( 'woocommerce_admin_notices', array() );
  143. $notices = array_diff( $notices, array( 'translation_upgrade' ) );
  144. update_option( 'woocommerce_admin_notices', $notices );
  145. }
  146. /**
  147. * Manual language update
  148. */
  149. public function manual_language_update() {
  150. if (
  151. is_admin()
  152. && current_user_can( 'update_plugins' )
  153. && isset( $_GET['page'] )
  154. && in_array( $_GET['page'], array( 'wc-status', 'wc-setup' ) )
  155. && isset( $_GET['action'] )
  156. && 'translation_upgrade' == $_GET['action']
  157. ) {
  158. $page = 'wc-status&tab=tools';
  159. $wpnonce = 'debug_action';
  160. if ( 'wc-setup' == $_GET['page'] ) {
  161. $page = 'wc-setup';
  162. $wpnonce = 'setup_language';
  163. }
  164. $url = wp_nonce_url( admin_url( 'admin.php?page=' . $page . '&action=translation_upgrade' ), 'language_update' );
  165. $tools_url = admin_url( 'admin.php?page=' . $page );
  166. if ( ! isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], $wpnonce ) ) {
  167. wp_redirect( add_query_arg( array( 'translation_updated' => 2 ), $tools_url ) );
  168. exit;
  169. }
  170. if ( false === ( $creds = request_filesystem_credentials( $url, '', false, false, null ) ) ) {
  171. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  172. exit;
  173. }
  174. if ( ! WP_Filesystem( $creds ) ) {
  175. request_filesystem_credentials( $url, '', true, false, null );
  176. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  177. exit;
  178. }
  179. // Download the language pack
  180. $response = wp_safe_remote_get( self::get_language_package_uri(), array( 'timeout' => 60 ) );
  181. if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  182. global $wp_filesystem;
  183. $upload_dir = wp_upload_dir();
  184. $file = trailingslashit( $upload_dir['path'] ) . get_locale() . '.zip';
  185. // Save the zip file
  186. if ( ! $wp_filesystem->put_contents( $file, $response['body'], FS_CHMOD_FILE ) ) {
  187. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  188. exit;
  189. }
  190. // Unzip the file to wp-content/languages/plugins directory
  191. $dir = trailingslashit( WP_LANG_DIR ) . 'plugins/';
  192. $unzip = unzip_file( $file, $dir );
  193. if ( true !== $unzip ) {
  194. wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
  195. exit;
  196. }
  197. // Delete the package file
  198. $wp_filesystem->delete( $file );
  199. // Update the language pack version
  200. $this->save_language_version();
  201. // Redirect and show a success message
  202. wp_redirect( add_query_arg( array( 'translation_updated' => 1 ), $tools_url ) );
  203. exit;
  204. } else {
  205. // Don't have a valid package for the current language!
  206. wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $tools_url ) );
  207. exit;
  208. }
  209. }
  210. }
  211. /**
  212. * Language update messages
  213. *
  214. * @since 2.4.5
  215. */
  216. public static function language_update_messages() {
  217. switch ( $_GET['translation_updated'] ) {
  218. case 2 :
  219. echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woocommerce' ) . ' ' . __( 'Seems you don\'t have permission to do this!', 'woocommerce' ) . '</p></div>';
  220. break;
  221. case 3 :
  222. echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woocommerce' ) . ' ' . sprintf( __( 'An authentication error occurred while updating the translation. Please try again or configure your %sUpgrade Constants%s.', 'woocommerce' ), '<a href="http://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants">', '</a>' ) . '</p></div>';
  223. break;
  224. case 4 :
  225. echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woocommerce' ) . ' ' . __( 'Sorry but there is no translation available for your language =/', 'woocommerce' ) . '</p></div>';
  226. break;
  227. default :
  228. // Force WordPress find for new updates and hide the WooCommerce translation update
  229. set_site_transient( 'update_plugins', null );
  230. echo '<div class="updated"><p>' . __( 'Translations installed/updated successfully!', 'woocommerce' ) . '</p></div>';
  231. break;
  232. }
  233. }
  234. }
  235. new WC_Language_Pack_Upgrader();