/vendor/yoast/i18n-module/i18n-module.php

  1. <?php 
  2.  
  3. /** 
  4. * This class defines a promo box and checks your translation site's API for stats about it, then shows them to the user. 
  5. */ 
  6. class yoast_i18n { 
  7.  
  8. /** 
  9. * Your translation site's logo 
  10. * 
  11. * @var string 
  12. */ 
  13. private $glotpress_logo; 
  14.  
  15. /** 
  16. * Your translation site's name 
  17. * 
  18. * @var string 
  19. */ 
  20. private $glotpress_name; 
  21.  
  22. /** 
  23. * Your translation site's URL 
  24. * 
  25. * @var string 
  26. */ 
  27. private $glotpress_url; 
  28.  
  29. /** 
  30. * Hook where you want to show the promo box 
  31. * 
  32. * @var string 
  33. */ 
  34. private $hook; 
  35.  
  36. /** 
  37. * Will contain the site's locale 
  38. * 
  39. * @access private 
  40. * @var string 
  41. */ 
  42. private $locale; 
  43.  
  44. /** 
  45. * Will contain the locale's name, obtained from yoru translation site 
  46. * 
  47. * @access private 
  48. * @var string 
  49. */ 
  50. private $locale_name; 
  51.  
  52. /** 
  53. * Will contain the percentage translated for the plugin translation project in the locale 
  54. * 
  55. * @access private 
  56. * @var int 
  57. */ 
  58. private $percent_translated; 
  59.  
  60. /** 
  61. * Name of your plugin 
  62. * 
  63. * @var string 
  64. */ 
  65. private $plugin_name; 
  66.  
  67. /** 
  68. * Project slug for the project on your translation site 
  69. * 
  70. * @var string 
  71. */ 
  72. private $project_slug; 
  73.  
  74. /** 
  75. * URL to point to for registration links 
  76. * 
  77. * @var string 
  78. */ 
  79. private $register_url; 
  80.  
  81. /** 
  82. * Your plugins textdomain 
  83. * 
  84. * @var string 
  85. */ 
  86. private $textdomain; 
  87.  
  88. /** 
  89. * Indicates whether there's a translation available at all. 
  90. * 
  91. * @access private 
  92. * @var bool 
  93. */ 
  94. private $translation_exists; 
  95.  
  96. /** 
  97. * Indicates whether the translation's loaded. 
  98. * 
  99. * @access private 
  100. * @var bool 
  101. */ 
  102. private $translation_loaded; 
  103.  
  104. /** 
  105. * Class constructor 
  106. * 
  107. * @param array $args Contains the settings for the class. 
  108. */ 
  109. public function __construct( $args ) { 
  110. if ( ! is_admin() ) { 
  111. return; 
  112.  
  113. $this->locale = get_locale(); 
  114. if ( 'en_US' === $this->locale ) { 
  115. return; 
  116.  
  117. $this->init( $args ); 
  118.  
  119. if ( ! $this->hide_promo() ) { 
  120. add_action( $this->hook, array( $this, 'promo' ) ); 
  121.  
  122. /** 
  123. * This is where you decide where to display the messages and where you set the plugin specific variables. 
  124. * 
  125. * @access private 
  126. * 
  127. * @param array $args 
  128. */ 
  129. private function init( $args ) { 
  130. foreach ( $args as $key => $arg ) { 
  131. $this->$key = $arg; 
  132.  
  133. /** 
  134. * Check whether the promo should be hidden or not 
  135. * 
  136. * @access private 
  137. * 
  138. * @return bool 
  139. */ 
  140. private function hide_promo() { 
  141. $hide_promo = get_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide' ); 
  142. if ( ! $hide_promo ) { 
  143. if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) { 
  144. // No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc. 
  145. set_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide', true ); 
  146. $hide_promo = true; 
  147.  
  148. return $hide_promo; 
  149.  
  150. /** 
  151. * Generates a promo message 
  152. * 
  153. * @access private 
  154. * 
  155. * @return bool|string $message 
  156. */ 
  157. private function promo_message() { 
  158. $message = false; 
  159.  
  160. if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) { 
  161. $message = __( 'As you can see, there is a translation of this plugin in %1$s. This translation is currently %3$d%% complete. We need your help to make it complete and to fix any errors. Please register at %4$s to help complete the translation to %1$s!', $this->textdomain ); 
  162. } else if ( ! $this->translation_loaded && $this->translation_exists ) { 
  163. $message = __( 'You\'re using WordPress in %1$s. While %2$s has been translated to %1$s for %3$d%%, it\'s not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', $this->textdomain ); 
  164. } else if ( ! $this->translation_exists ) { 
  165. $message = __( 'You\'re using WordPress in a language we don\'t support yet. We\'d love for %2$s to be translated in that language too, but unfortunately, it isn\'t right now. You can change that! Register at %4$s to help translate it!', $this->textdomain ); 
  166.  
  167. $registration_link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $this->register_url ), esc_html( $this->glotpress_name ) ); 
  168. $message = sprintf( $message, esc_html( $this->locale_name ), esc_html( $this->plugin_name ), $this->percent_translated, $registration_link ); 
  169.  
  170. return $message; 
  171.  
  172. /** 
  173. * Outputs a promo box 
  174. */ 
  175. public function promo() { 
  176. $this->translation_details(); 
  177.  
  178. $message = $this->promo_message(); 
  179.  
  180. if ( $message ) { 
  181. echo '<div id="i18n_promo_box" style="border:1px solid #ccc;background-color:#fff;padding:10px;max-width:650px;">'; 
  182. echo '<a href="' . esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ) . '" style="color:#333;text-decoration:none;font-weight:bold;font-size:16px;border:1px solid #ccc;padding:1px 4px;" class="alignright">X</a>'; 
  183. echo '<h2>' . sprintf( __( 'Translation of %s', $this->textdomain ), $this->plugin_name ) . '</h2>'; 
  184. if ( isset( $this->glotpress_logo ) && '' != $this->glotpress_logo ) { 
  185. echo '<a href="' . $this->register_url . '"><img class="alignright" style="margin:15px 5px 5px 5px;width:200px;" src="' . $this->glotpress_logo . '" alt="' . $this->glotpress_name . '"/></a>'; 
  186. echo '<p>' . $message . '</p>'; 
  187. echo '<p><a href="' . $this->register_url . '">' . __( 'Register now »', $this->textdomain ) . '</a></p>'; 
  188. echo '</div>'; 
  189.  
  190. /** 
  191. * Try to find the transient for the translation set or retrieve them. 
  192. * 
  193. * @access private 
  194. * 
  195. * @return object|null 
  196. */ 
  197. private function find_or_initialize_translation_details() { 
  198. $set = get_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale ); 
  199.  
  200. if ( ! $set ) { 
  201. $set = $this->retrieve_translation_details(); 
  202. set_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale, $set, DAY_IN_SECONDS ); 
  203.  
  204. return $set; 
  205.  
  206. /** 
  207. * Try to get translation details from cache, otherwise retrieve them, then parse them. 
  208. * 
  209. * @access private 
  210. */ 
  211. private function translation_details() { 
  212. $set = $this->find_or_initialize_translation_details(); 
  213.  
  214. $this->translation_exists = ! is_null( $set ); 
  215. $this->translation_loaded = is_textdomain_loaded( $this->textdomain ); 
  216.  
  217. $this->parse_translation_set( $set ); 
  218.  
  219. /** 
  220. * Retrieve the translation details from Yoast Translate 
  221. * 
  222. * @access private 
  223. * 
  224. * @return object|null 
  225. */ 
  226. private function retrieve_translation_details() { 
  227. $api_url = trailingslashit( $this->glotpress_url ) . 'api/projects/' . $this->project_slug; 
  228.  
  229. $resp = wp_remote_get( $api_url ); 
  230. $body = wp_remote_retrieve_body( $resp ); 
  231. unset( $resp ); 
  232.  
  233. if ( $body ) { 
  234. $body = json_decode( $body ); 
  235. foreach ( $body->translation_sets as $set ) { 
  236. if ( $this->locale == $set->wp_locale ) { 
  237. return $set; 
  238.  
  239. return null; 
  240.  
  241. /** 
  242. * Set the needed private variables based on the results from Yoast Translate 
  243. * 
  244. * @param object $set The translation set 
  245. * 
  246. * @access private 
  247. */ 
  248. private function parse_translation_set( $set ) { 
  249. if ( $this->translation_exists && is_object( $set ) ) { 
  250. $this->locale_name = $set->name; 
  251. $this->percent_translated = $set->percent_translated; 
  252. } else { 
  253. $this->locale_name = ''; 
  254. $this->percent_translated = ''; 
.