Yoast_I18n_v2

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/src/i18n-module.php  
  1. class Yoast_I18n_v2 { 
  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. * The URL to actually do the API request to 
  23. * @var string 
  24. */ 
  25. private $api_url; 
  26.  
  27. /** 
  28. * Hook where you want to show the promo box 
  29. * @var string 
  30. */ 
  31. private $hook; 
  32.  
  33. /** 
  34. * Will contain the site's locale 
  35. * @access private 
  36. * @var string 
  37. */ 
  38. private $locale; 
  39.  
  40. /** 
  41. * Will contain the locale's name, obtained from your translation site 
  42. * @access private 
  43. * @var string 
  44. */ 
  45. private $locale_name; 
  46.  
  47. /** 
  48. * Will contain the percentage translated for the plugin translation project in the locale 
  49. * @access private 
  50. * @var int 
  51. */ 
  52. private $percent_translated; 
  53.  
  54. /** 
  55. * Name of your plugin 
  56. * @var string 
  57. */ 
  58. private $plugin_name; 
  59.  
  60. /** 
  61. * Project slug for the project on your translation site 
  62. * @var string 
  63. */ 
  64. private $project_slug; 
  65.  
  66. /** 
  67. * URL to point to for registration links 
  68. * @var string 
  69. */ 
  70. private $register_url; 
  71.  
  72. /** 
  73. * Your plugins textdomain 
  74. * @var string 
  75. */ 
  76. private $textdomain; 
  77.  
  78. /** 
  79. * Indicates whether there's a translation available at all. 
  80. * @access private 
  81. * @var bool 
  82. */ 
  83. private $translation_exists; 
  84.  
  85. /** 
  86. * Indicates whether the translation's loaded. 
  87. * @access private 
  88. * @var bool 
  89. */ 
  90. private $translation_loaded; 
  91.  
  92. /** 
  93. * Class constructor 
  94. * @param array $args Contains the settings for the class. 
  95. */ 
  96. public function __construct( $args ) { 
  97. if ( ! is_admin() ) { 
  98. return; 
  99.  
  100. $this->locale = $this->get_admin_locale(); 
  101. if ( 'en_US' === $this->locale ) { 
  102. return; 
  103.  
  104. $this->init( $args ); 
  105.  
  106. if ( ! $this->hide_promo() ) { 
  107. add_action( $this->hook, array( $this, 'promo' ) ); 
  108.  
  109. /** 
  110. * Returns the locale used in the admin. 
  111. * WordPress 4.7 introduced the ability for users to specify an Admin language 
  112. * different from the language used on the front end. This checks if the feature 
  113. * is available and returns the user's language, with a fallback to the site's language. 
  114. * Can be removed when support for WordPress 4.6 will be dropped, in favor 
  115. * of WordPress get_user_locale() that already fallbacks to the site*s locale. 
  116. * @returns string The locale. 
  117. */ 
  118. private function get_admin_locale() { 
  119. if ( function_exists( 'get_user_locale' ) ) { 
  120. return get_user_locale(); 
  121.  
  122. return get_locale(); 
  123.  
  124. /** 
  125. * This is where you decide where to display the messages and where you set the plugin specific variables. 
  126. * @access private 
  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. * @access private 
  136. * @return bool 
  137. */ 
  138. private function hide_promo() { 
  139. $hide_promo = get_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide' ); 
  140. if ( ! $hide_promo ) { 
  141. if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) { 
  142. // No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc. 
  143. set_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide', true ); 
  144. $hide_promo = true; 
  145.  
  146. return $hide_promo; 
  147.  
  148. /** 
  149. * Generates a promo message 
  150. * @access private 
  151. * @return bool|string $message 
  152. */ 
  153. private function promo_message() { 
  154. $message = false; 
  155.  
  156. if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) { 
  157. $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 ); 
  158. } else if ( ! $this->translation_loaded && $this->translation_exists ) { 
  159. $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 ); 
  160. } else if ( ! $this->translation_exists ) { 
  161. $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 ); 
  162.  
  163. $registration_link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $this->register_url ), esc_html( $this->glotpress_name ) ); 
  164. $message = sprintf( $message, esc_html( $this->locale_name ), esc_html( $this->plugin_name ), $this->percent_translated, $registration_link ); 
  165.  
  166. return $message; 
  167.  
  168. /** 
  169. * Outputs a promo box 
  170. */ 
  171. public function promo() { 
  172. $this->translation_details(); 
  173.  
  174. $message = $this->promo_message(); 
  175.  
  176. if ( $message ) { 
  177. echo '<div id="i18n_promo_box" style="border:1px solid #ccc;background-color:#fff;padding:10px;max-width:650px; overflow: hidden;">'; 
  178. 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>'; 
  179.  
  180. echo '<div>'; 
  181. echo '<h2>' . sprintf( __( 'Translation of %s', $this->textdomain ), $this->plugin_name ) . '</h2>'; 
  182. if ( isset( $this->glotpress_logo ) && '' != $this->glotpress_logo ) { 
  183. echo '<a href="' . esc_url( $this->register_url ) . '"><img class="alignright" style="margin:0 5px 5px 5px;max-width:200px;" src="' . esc_url( $this->glotpress_logo ) . '" alt="' . esc_attr( $this->glotpress_name ) . '"/></a>'; 
  184. echo '<p>' . $message . '</p>'; 
  185. echo '<p><a href="' . esc_url( $this->register_url ) . '">' . __( 'Register now »', $this->textdomain ) . '</a></p>'; 
  186. echo '</div>'; 
  187. echo '</div>'; 
  188.  
  189. /** 
  190. * Try to find the transient for the translation set or retrieve them. 
  191. * @access private 
  192. * @return object|null 
  193. */ 
  194. private function find_or_initialize_translation_details() { 
  195. $set = get_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale ); 
  196.  
  197. if ( ! $set ) { 
  198. $set = $this->retrieve_translation_details(); 
  199. set_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale, $set, DAY_IN_SECONDS ); 
  200.  
  201. return $set; 
  202.  
  203. /** 
  204. * Try to get translation details from cache, otherwise retrieve them, then parse them. 
  205. * @access private 
  206. */ 
  207. private function translation_details() { 
  208. $set = $this->find_or_initialize_translation_details(); 
  209.  
  210. $this->translation_exists = ! is_null( $set ); 
  211. $this->translation_loaded = is_textdomain_loaded( $this->textdomain ); 
  212.  
  213. $this->parse_translation_set( $set ); 
  214.  
  215. /** 
  216. * The API URL to use when requesting translation information. 
  217. * @param string $api_url The new API URL. 
  218. */ 
  219. public function set_api_url( $api_url ) { 
  220. $this->api_url = $api_url; 
  221.  
  222. /** 
  223. * Returns the API URL to use when requesting translation information. 
  224. * @return string 
  225. */ 
  226. private function get_api_url() { 
  227. if ( empty( $this->api_url ) ) { 
  228. $this->api_url = trailingslashit( $this->glotpress_url ) . 'api/projects/' . $this->project_slug; 
  229.  
  230. return $this->api_url; 
  231.  
  232. /** 
  233. * Retrieve the translation details from Yoast Translate 
  234. * @access private 
  235. * @return object|null 
  236. */ 
  237. private function retrieve_translation_details() { 
  238. $api_url = $this->get_api_url(); 
  239.  
  240. $resp = wp_remote_get( $api_url ); 
  241. if ( is_wp_error( $resp ) || wp_remote_retrieve_response_code( $resp ) !== 200 ) { 
  242. return null; 
  243. $body = wp_remote_retrieve_body( $resp ); 
  244. unset( $resp ); 
  245.  
  246. if ( $body ) { 
  247. $body = json_decode( $body ); 
  248. if ( empty( $body->translation_sets ) ) { 
  249. return null; 
  250. foreach ( $body->translation_sets as $set ) { 
  251. if ( ! property_exists( $set, 'wp_locale' ) ) { 
  252. continue; 
  253.  
  254. if ( $this->locale === $set->wp_locale ) { 
  255. return $set; 
  256.  
  257. return null; 
  258.  
  259. /** 
  260. * Set the needed private variables based on the results from Yoast Translate 
  261. * @param object $set The translation set 
  262. * @access private 
  263. */ 
  264. private function parse_translation_set( $set ) { 
  265. if ( $this->translation_exists && is_object( $set ) ) { 
  266. $this->locale_name = $set->name; 
  267. $this->percent_translated = $set->percent_translated; 
  268. } else { 
  269. $this->locale_name = ''; 
  270. $this->percent_translated = '';