PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpml-string-translation/classes/string-translation-ui/class-wpml-string-translation-table.php

https://bitbucket.org/ed47/epfl-smart-move
PHP | 331 lines | 275 code | 42 blank | 14 comment | 40 complexity | 234d576cc12ba919c97af13f7557b485 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. class WPML_String_Translation_Table {
  3. /** @var array $strings */
  4. private $strings;
  5. /** @var array */
  6. private $active_languages;
  7. public function __construct( $strings ) {
  8. global $sitepress;
  9. $this->strings = $strings;
  10. if ( ! empty( $strings ) ) {
  11. $this->strings_in_page = icl_get_strings_tracked_in_pages( $strings );
  12. }
  13. $this->active_languages = $sitepress->get_active_languages();
  14. }
  15. public function render() {
  16. ?>
  17. <table id="icl_string_translations" class="widefat" cellspacing="0">
  18. <?php
  19. $this->render_table_header_or_footer( 'thead' );
  20. $this->render_table_header_or_footer( 'tfoot' );
  21. ?>
  22. <tbody>
  23. <?php if ( empty( $this->strings ) ) {
  24. ?>
  25. <tr>
  26. <td colspan="6" align="center">
  27. <?php esc_html_e( 'No strings found', 'wpml-string-translation' ) ?>
  28. </td>
  29. </tr>
  30. <?php
  31. } else {
  32. foreach ( $this->strings as $string_id => $icl_string ) {
  33. $this->render_string_row( $string_id, $icl_string );
  34. }
  35. }
  36. ?>
  37. </tbody>
  38. </table>
  39. <?php
  40. }
  41. private function render_table_header_or_footer( $tag ) {
  42. ?>
  43. <<?php echo $tag; ?>>
  44. <tr>
  45. <th scope="col" class="manage-column column-cb check-column"><input type="checkbox"/></th>
  46. <th scope="col"><?php esc_html_e( 'Domain', 'wpml-string-translation' ) ?></th>
  47. <th scope="col"><?php esc_html_e( 'Context', 'wpml-string-translation' ) ?></th>
  48. <th scope="col"><?php esc_html_e( 'Name', 'wpml-string-translation' ) ?></th>
  49. <th scope="col"><?php esc_html_e( 'View', 'wpml-string-translation' ) ?></th>
  50. <th scope="col"><?php esc_html_e( 'String', 'wpml-string-translation' ) ?></th>
  51. <th scope="col"><?php esc_html_e( 'Status', 'wpml-string-translation' ) ?></th>
  52. </tr>
  53. <<?php echo $tag; ?>>
  54. <?php
  55. }
  56. public function render_string_row( $string_id, $icl_string ) {
  57. global $wpdb, $sitepress, $WPML_String_Translation;
  58. if ( isset( $icl_string['string_language'] ) && ! isset( $this->active_languages[ $icl_string['string_language'] ] ) ) {
  59. $this->active_languages[ $icl_string['string_language'] ] = $sitepress->get_language_details( $icl_string['string_language'] );
  60. }
  61. if ( isset( $icl_string['translations'] ) ) {
  62. foreach ( $icl_string['translations'] as $target_lang_code => $data ) {
  63. if ( ! isset( $this->active_languages[ $target_lang_code ] ) ) {
  64. $this->active_languages[ $target_lang_code ] = $sitepress->get_language_details( $target_lang_code );
  65. }
  66. }
  67. }
  68. ?>
  69. <tr valign="top">
  70. <?php echo $this->render_checkbox_cell( $icl_string ) ?>
  71. <td class="wpml-st-col-domain"><?php echo esc_html( $icl_string['context'] ) ?></td>
  72. <td><?php echo esc_html( $icl_string['gettext_context'] ) ?></td>
  73. <td class="wpml-st-col-name"><?php echo esc_html( $this->hide_if_md5( $icl_string['name'] ) ); ?></td>
  74. <td nowrap="nowrap">
  75. <?php $this->render_view_column( $string_id ) ?>
  76. </td>
  77. <td class="wpml-st-col-string">
  78. <div class="icl-st-original"<?php _icl_string_translation_rtl_div( $icl_string['string_language'] ) ?>>
  79. <img src="<?php echo esc_url( $sitepress->get_flag_url( $icl_string['string_language'] ) ) ?>"> <?php echo esc_html( $icl_string['value'] ) ?>
  80. </div>
  81. <div style="float:right;">
  82. <a href="#icl-st-toggle-translations" class="js-wpml-st-toggle-translations"><?php esc_html_e( 'translations', 'wpml-string-translation' ) ?></a>
  83. </div>
  84. <br clear="all"/>
  85. <div class="icl-st-inline"
  86. data-original="<?php echo esc_attr( $icl_string['value'] ); ?>"
  87. data-source-lang="<?php echo esc_attr( $icl_string['string_language'] ); ?>">
  88. <?php foreach ( $this->active_languages as $lang ): if ( $lang['code'] === $icl_string['string_language'] ) {
  89. continue;
  90. } ?>
  91. <?php
  92. if ( isset( $icl_string['translations'][ $lang['code'] ] ) && ICL_TM_COMPLETE == $icl_string['translations'][ $lang['code'] ]['status'] ) {
  93. $tr_complete_checked = 'checked="checked"';
  94. } else {
  95. if ( icl_st_is_translator() ) {
  96. $user_lang_pairs = get_user_meta( get_current_user_id(), $wpdb->prefix . 'language_pairs', true );
  97. if ( empty( $user_lang_pairs[ $icl_string['string_language'] ][ $lang['code'] ] ) ) {
  98. continue;
  99. }
  100. }
  101. $tr_complete_checked = '';
  102. }
  103. list( $form_disabled, $form_disabled_reason ) = $this->get_translation_form_status( $icl_string, $lang );
  104. ?>
  105. <form class="icl_st_form"
  106. name="icl_st_form_<?php echo esc_attr( $lang['code'] . '_' . $string_id ) ?>" action="">
  107. <?php wp_nonce_field( 'icl_st_save_translation_nonce', '_icl_nonce' ) ?>
  108. <input type="hidden" name="icl_st_language"
  109. value="<?php echo esc_attr( $lang['code'] ) ?>"/>
  110. <input type="hidden" name="icl_st_string_id" value="<?php echo esc_attr( $string_id ) ?>"/>
  111. <table class="icl-st-table">
  112. <tr>
  113. <td style="border:none">
  114. <?php echo esc_html( $lang['display_name'] ) ?>
  115. <br/>
  116. <?php if ( isset( $icl_string['translations'][ $lang['code'] ]['mo_string'] ) && $icl_string['translations'][ $lang['code'] ]['mo_string'] ) { ?>
  117. <span class="wpml-st-mo-translation" title="<?php esc_attr_e( 'Imported translation', 'wpml-string-translation' ); ?>">
  118. <?php echo esc_html( $icl_string['translations'][ $lang['code'] ]['mo_string'] ); ?>
  119. </span>
  120. <?php } ?>
  121. <img class="icl_ajx_loader"
  122. src="<?php echo WPML_ST_URL ?>/res/img/ajax-loader.gif"
  123. style="float:left;display:none;position:absolute;margin:5px" alt=""/>
  124. <?php
  125. $rows = ceil( strlen( $icl_string['value'] ) / 80 );
  126. $temp_line_array = preg_split( '/\n|\r/', $icl_string['value'] );
  127. $temp_num_lines = count( $temp_line_array );
  128. $rows += $temp_num_lines;
  129. $string_value = '';
  130. $placeholder = $icl_string['value'];
  131. if ( isset( $icl_string['translations'][ $lang['code'] ] ) && null !== $icl_string['translations'][ $lang['code'] ]['value'] ) {
  132. $string_value = $icl_string['translations'][ $lang['code'] ]['value'];
  133. if ( $string_value ) {
  134. $placeholder = $string_value;
  135. } elseif ( $icl_string['translations'][ $lang['code'] ]['mo_string'] ) {
  136. $placeholder = $icl_string['translations'][ $lang['code'] ]['mo_string'];
  137. }
  138. }
  139. ?>
  140. <textarea<?php echo $form_disabled;
  141. _icl_string_translation_rtl_textarea( $lang['code'] ); ?>
  142. rows="<?php echo esc_attr( $rows ) ?>" cols="40"
  143. name="icl_st_translation"
  144. placeholder="<?php echo esc_attr( $placeholder ); ?>"
  145. data-lang="<?php echo $lang['code']; ?>"
  146. <?php if ( isset( $icl_string['translations'][ $lang['code'] ] ) ): ?>id="icl_st_ta_<?php echo esc_attr( $icl_string['translations'][ $lang['code'] ]['id'] ) ?>"<?php endif; ?>
  147. ><?php echo esc_html( $string_value ) ?></textarea>
  148. </td>
  149. </tr>
  150. <tr>
  151. <td align="right" style="border:none">
  152. <?php if ( isset( $icl_string['translations'][ $lang['code'] ]['value'] ) && preg_match( '#<([^>]*)>#im', $icl_string['translations'][ $lang['code'] ]['value'] ) ): ?>
  153. <br clear="all"/>
  154. <div style="text-align:left;display:none" class="icl_html_preview"></div>
  155. <a href="#" class="alignleft icl_htmlpreview_link">HTML preview</a>
  156. <?php endif; ?>
  157. <label>
  158. <input<?php echo $form_disabled ?>
  159. type="checkbox"
  160. name="icl_st_translation_complete"
  161. data-lang="<?php echo $lang['code']; ?>"
  162. value="1"
  163. <?php echo $tr_complete_checked ?>
  164. <?php if ( isset( $icl_string['translations'][ $lang['code'] ] ) ): ?>id="icl_st_cb_<?php echo esc_attr( $icl_string['translations'][ $lang['code'] ]['id'] ) ?>"<?php endif; ?>
  165. />
  166. <?php
  167. if ( isset( $icl_string['translations'][ $lang['code'] ]['mo_string'] ) && $icl_string['translations'][ $lang['code'] ]['mo_string'] ) {
  168. esc_html_e( 'Use my translation', 'wpml-string-translation' );
  169. } else {
  170. esc_html_e( 'Translation is complete', 'wpml-string-translation' );
  171. }
  172. ?>
  173. </label>&nbsp;
  174. <input<?php echo $form_disabled ?> type="submit" class="button-secondary action"
  175. data-lang="<?php echo $lang['code']; ?>"
  176. value="<?php esc_attr_e( 'Save', 'wpml-string-translation' ) ?>"/>
  177. <?php if ( $form_disabled_reason ): ?>
  178. <br clear="all"/>
  179. <p><?php echo esc_html( $form_disabled_reason ) ?></p>
  180. <?php endif; ?>
  181. </td>
  182. </tr>
  183. </table>
  184. </form>
  185. <?php endforeach; ?>
  186. </div>
  187. </td>
  188. <td nowrap="nowrap" id="icl_st_string_status_<?php echo esc_attr( $string_id ) ?>">
  189. <span>
  190. <?php
  191. echo esc_html( apply_filters( 'wpml_string_status_text', WPML_ST_String_Statuses::get_status( (int) $icl_string['status'] ), $string_id ) );
  192. ?>
  193. </span>
  194. <input type="hidden" id="icl_st_wc_<?php echo esc_attr( $string_id ) ?>" value="<?php
  195. echo $WPML_String_Translation->estimate_word_count( $icl_string['value'], $icl_string['string_language'] ) ?>"/>
  196. </td>
  197. </tr>
  198. <?php
  199. }
  200. /**
  201. * @param array $string
  202. *
  203. * @return string html for the checkbox and the table cell it resides in
  204. */
  205. private function render_checkbox_cell( $string ) {
  206. $class = 'icl_st_row_cb' . ( ! empty( $string['string_package_id'] ) ? ' icl_st_row_package' : '' );
  207. return '<td><input class="' . esc_attr( $class ) . '" type="checkbox" value="' . esc_attr( $string['string_id'] )
  208. . '" data-language="' . esc_attr( $string['string_language'] ) .'" /></td>';
  209. }
  210. private function render_view_column( $string_id ) {
  211. if ( isset( $this->strings_in_page[ ICL_STRING_TRANSLATION_STRING_TRACKING_TYPE_SOURCE ][ $string_id ] ) ) {
  212. $thickbox_url = $this->get_thickbox_url( WPML_ST_String_Tracking_AJAX_Factory::ACTION_POSITION_IN_SOURCE, $string_id );
  213. ?>
  214. <a class="thickbox" title="<?php esc_attr_e( 'view in source', 'wpml-string-translation' ) ?>"
  215. href="<?php echo esc_url( $thickbox_url ); ?>">
  216. <img src="<?php echo WPML_ST_URL ?>/res/img/view-in-source.png" width="16" height="16"
  217. alt="<?php esc_attr_e( 'view in page', 'wpml-string-translation' ) ?>"/>
  218. </a>
  219. <?php
  220. }
  221. if ( isset( $this->strings_in_page[ ICL_STRING_TRANSLATION_STRING_TRACKING_TYPE_PAGE ][ $string_id ] ) ) {
  222. $thickbox_url = $this->get_thickbox_url( WPML_ST_String_Tracking_AJAX_Factory::ACTION_POSITION_IN_PAGE, $string_id );
  223. ?>
  224. <a class="thickbox" title="<?php esc_attr_e( 'view in page', 'wpml-string-translation' ) ?>"
  225. href="<?php echo esc_url( $thickbox_url ); ?>">
  226. <img src="<?php echo WPML_ST_URL ?>/res/img/view-in-page.png" width="16" height="16"
  227. alt="<?php esc_attr_e( 'view in page', 'wpml-string-translation' ) ?>"/>
  228. </a>
  229. <?php
  230. }
  231. }
  232. /**
  233. * @param string $action
  234. * @param int $string_id
  235. *
  236. * @return string
  237. */
  238. private function get_thickbox_url( $action, $string_id ) {
  239. return add_query_arg(
  240. array(
  241. 'page' => WPML_ST_FOLDER . '/menu/string-translation.php',
  242. 'action' => $action,
  243. 'nonce' => wp_create_nonce( $action ),
  244. 'string_id' => $string_id,
  245. 'width' => 810,
  246. 'height' => 600,
  247. ),
  248. 'admin-ajax.php'
  249. );
  250. }
  251. private function get_translation_form_status( $icl_string, $lang ) {
  252. global $wpdb;
  253. $form_disabled = '';
  254. $form_disabled_reason = '';
  255. if ( icl_st_is_translator() ) {
  256. // Determine if string is being translated via Translation Proxy
  257. $translation_proxy_status = $wpdb->get_var( $wpdb->prepare( "
  258. SELECT c.status FROM {$wpdb->prefix}icl_core_status c
  259. JOIN {$wpdb->prefix}icl_string_status s ON s.rid = c.rid
  260. WHERE s.string_translation_id = (SELECT id FROM {$wpdb->prefix}icl_string_translations WHERE string_id=%d AND language=%s) AND c.target=%s AND c.status = %d
  261. ORDER BY s.id DESC LIMIT 1
  262. ", $icl_string['string_id'], $lang['code'], $lang['code'], ICL_TM_WAITING_FOR_TRANSLATOR ) );
  263. $can_translate = isset( $icl_string['translations'][ $lang['code'] ] );
  264. if ( ! $can_translate ) {
  265. $form_disabled_reason = __( "You can't translate this string because it hasn't been sent for translation in this language", 'wpml-string-translation' );
  266. }
  267. $translator_id = $can_translate ? $icl_string['translations'][ $lang['code'] ]['translator_id'] : null;
  268. if ( $can_translate && 0 != $translator_id && get_current_user_id() != $translator_id ) {
  269. $can_translate = false;
  270. $form_disabled_reason = __( "You can't translate this string because it's assigned to another translator", 'wpml-string-translation' );
  271. }
  272. if ( $can_translate &&
  273. 0 == $translator_id &&
  274. ICL_TM_WAITING_FOR_TRANSLATOR === (int) $icl_string['translations'][ $lang['code'] ]['status'] &&
  275. $translation_proxy_status
  276. ) {
  277. $can_translate = false;
  278. $form_disabled_reason = __( "You can't translate this string because it's assigned to another translator", 'wpml-string-translation' );
  279. }
  280. if ( ! $can_translate ) {
  281. $form_disabled = ' disabled="disabled" ';
  282. }
  283. }
  284. return array( $form_disabled, $form_disabled_reason );
  285. }
  286. private function hide_if_md5( $str ) {
  287. return preg_replace( '#^((.+)( - ))?([a-z0-9]{32})$#', '$2', $str );
  288. }
  289. }