yoast_i18n

This class defines a promo box and checks your translation site's API for stats about it, then shows them to the user.

Defined (1)

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

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