Nessuna descrizione

I10n.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. class PUM_Utils_I10n {
  9. /**
  10. * Fetches translation status data from WordPress.org API.
  11. *
  12. * Stores it for 1 week.
  13. *
  14. * @return array
  15. */
  16. public static function translation_status() {
  17. $translations = get_transient( 'pum_alerts_translation_status' );
  18. if ( ! $translations ) {
  19. $response = wp_remote_get( 'https://api.wordpress.org/translations/plugins/1.0/?slug=popup-maker&version=' . Popup_Maker::$VER );
  20. $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
  21. $translations = $response_body['translations'];
  22. set_transient( 'pum_alerts_translation_status', $translations, 604800 );
  23. }
  24. $ret = array();
  25. foreach ( $translations as $translation ) {
  26. $ret[ $translation['language'] ] = $translation;
  27. }
  28. return $ret;
  29. }
  30. /**
  31. * Get locales matching the HTTP accept language header.
  32. *
  33. * @return array List of locales.
  34. */
  35. public static function get_non_en_accepted_wp_locales_from_header() {
  36. $res = array();
  37. $http_locales = self::get_http_locales();
  38. if ( empty( $http_locales ) ) {
  39. return $res;
  40. }
  41. if ( is_array( $http_locales ) ) {
  42. foreach ( $http_locales as $http_locale ) {
  43. $http_locale = explode( '-', $http_locale );
  44. $lang = $http_locale[0];
  45. $region = ! empty( $http_locale[1] ) ? $http_locale[1] : null;
  46. if ( is_null( $region ) ) {
  47. $region = $lang;
  48. }
  49. /*
  50. * Discard English -- it's the default for all browsers,
  51. * ergo not very reliable information
  52. */
  53. if ( 'en' === $lang ) {
  54. continue;
  55. }
  56. // Region should be uppercase.
  57. $region = strtoupper( $region );
  58. $mapped = self::map_locale( $lang, $region );
  59. if ( $mapped ) {
  60. $res[] = $mapped;
  61. }
  62. }
  63. $res = array_unique( $res );
  64. }
  65. return $res;
  66. }
  67. /**
  68. * @return array
  69. */
  70. public static function available_locales() {
  71. static $available_locales;
  72. if ( ! isset( $available_locales ) ) {
  73. if ( ! function_exists( 'wp_get_available_translations' ) ) {
  74. require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
  75. }
  76. $available_locales = wp_get_available_translations();
  77. }
  78. return $available_locales;
  79. }
  80. /**
  81. * Tries to map a lang/region pair to one of our locales.
  82. *
  83. * @param string $lang Lang part of the HTTP accept header.
  84. * @param string $region Region part of the HTTP accept header.
  85. *
  86. * @return string|false Our locale matching $lang and $region, false otherwise.
  87. */
  88. public static function map_locale( $lang, $region ) {
  89. $uregion = strtoupper( $region );
  90. $ulang = strtoupper( $lang );
  91. $variants = array(
  92. "$lang-$region",
  93. "{$lang}_$region",
  94. "$lang-$uregion",
  95. "{$lang}_$uregion",
  96. "{$lang}_$ulang",
  97. $lang,
  98. );
  99. $available_locales = self::available_locales();
  100. $available_locales = array_keys( $available_locales );
  101. foreach ( $variants as $variant ) {
  102. if ( in_array( $variant, $available_locales ) ) {
  103. return $variant;
  104. }
  105. }
  106. foreach ( $available_locales as $locale ) {
  107. list( $locale_lang, ) = preg_split( '/[_-]/', $locale );
  108. if ( $lang === $locale_lang ) {
  109. return $locale;
  110. }
  111. }
  112. return false;
  113. }
  114. /**
  115. * Given a HTTP Accept-Language header $header
  116. * returns all the locales in it.
  117. *
  118. * @return array Matched locales.
  119. */
  120. public static function get_http_locales() {
  121. $locale_part_re = '[a-z]{2,}';
  122. $locale_re = "($locale_part_re(\-$locale_part_re)?)";
  123. if ( preg_match_all( "/$locale_re/i", isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '', $matches ) ) {
  124. return $matches[0];
  125. } else {
  126. return array();
  127. }
  128. }
  129. }