AIOSEOP_Translations

Class AIOSEOP_Translations.

Defined (1)

The class is defined in the following location(s).

/inc/translations.php  
  1. class AIOSEOP_Translations { 
  2.  
  3. public $current_locale = ''; 
  4.  
  5. public $url = 'https://translate.wordpress.org/api/projects/wp-plugins/all-in-one-seo-pack/dev'; 
  6.  
  7. public $name = ''; 
  8.  
  9. /** 
  10. * Loop through the locale info. 
  11. * @since 2.3.5 
  12. * @access public 
  13. * @var string $wplocale Information for a particular locale (in loop) 
  14. */ 
  15. public $wplocale = ''; 
  16.  
  17. public $translated_count = 0; 
  18.  
  19. public $translation_url = 'https://translate.wordpress.org/projects/wp-plugins/all-in-one-seo-pack'; 
  20.  
  21. public $slug = ''; 
  22.  
  23. public $percent_translated = ''; 
  24.  
  25. /** 
  26. * AIOSEOP_Translations constructor. 
  27. * @since 2.3.5 
  28. */ 
  29. public function __construct() { 
  30.  
  31. $this->current_locale = get_locale(); 
  32.  
  33. $this->init(); 
  34.  
  35.  
  36. /** 
  37. * Fetch locale data from WP. 
  38. * @since 2.3.5 
  39. * @since 2.3.6 Return FALSE on WP_Error object. 
  40. * @return mixed 
  41. */ 
  42. private function get_locale_data() { 
  43. $response = wp_remote_get( $this->url ); 
  44.  
  45. if ( is_wp_error( $response ) ) { 
  46. return false; 
  47.  
  48. return $response['body']; 
  49.  
  50.  
  51. /** 
  52. * @since 2.3.5 
  53. * @param $locales 
  54. */ 
  55. private function set_current_locale_data( $locales ) { 
  56.  
  57. // Some locales are missing the locale code (wp_locale) so we need to check for that. 
  58.  
  59. foreach ( $locales as $locale ) { 
  60.  
  61. $wplocale = ''; 
  62.  
  63. if ( isset( $locale->wp_locale ) ) { 
  64. $wplocale = $locale->wp_locale; 
  65.  
  66. if ( $wplocale === $this->current_locale ) { 
  67.  
  68. $name = ''; 
  69. $percent_translated = ''; 
  70.  
  71. if ( isset( $locale->name ) ) { 
  72. $name = $locale->name; 
  73.  
  74. if ( isset( $locale->percent_translated ) ) { 
  75. $percent_translated = $locale->percent_translated; 
  76.  
  77. $this->name = $name; 
  78. $this->wplocale = $wplocale; 
  79. $this->percent_translated = $percent_translated; 
  80. $this->slug = $locale->locale; 
  81.  
  82.  
  83.  
  84. /** 
  85. * @since 2.3.5 
  86. * @param $locales 
  87. * @return int 
  88. */ 
  89. private function count_translated_languages( $locales ) { 
  90.  
  91. $count = 0; 
  92.  
  93. foreach ( $locales as $locale ) { 
  94.  
  95. if ( $locale->percent_translated > 0 ) { 
  96. ++ $count; 
  97.  
  98. return $count; 
  99.  
  100. /** 
  101. * @since 2.3.5 
  102. */ 
  103. private function set_translation_url() { 
  104.  
  105. if ( null !== $this->wplocale ) { 
  106.  
  107. $url = "https://translate.wordpress.org/projects/wp-plugins/all-in-one-seo-pack/dev/$this->slug/default"; 
  108.  
  109. $this->translation_url = $url; 
  110.  
  111.  
  112. /** 
  113. * @since 2.3.5 
  114. * @since 2.3.6 Return FALSE on WP_Error object in get_locale_data(). 
  115. */ 
  116. private function init() { 
  117.  
  118. $json = $this->get_locale_data(); 
  119.  
  120. if ( $json === false ) { 
  121. return false; 
  122.  
  123. $translation_data = json_decode( $json ); 
  124.  
  125. $locales = $translation_data->translation_sets; 
  126.  
  127. $this->set_current_locale_data( $locales ); 
  128.  
  129. $this->translated_count = $this->count_translated_languages( $locales ); 
  130.  
  131. $this->set_translation_url();