PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.3
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.3
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / ob-callback / editor.php

editor.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.0.3, at inc/ob-callback/editor.php

247 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * wpLingua OB Callback function : On page editor
11 *
12 * @param string $html
13 * @return string
14 */
15 function wplng_ob_callback_editor( $html ) {
16
17 $html = apply_filters( 'wplng_html_intercepted', $html );
18
19 if ( empty( $html ) ) {
20 return $html;
21 }
22
23 /**
24 * Replace excluded HTML part by tag
25 */
26
27 $excluded_elements = array();
28 $html = wplng_html_set_exclude_tag(
29 $html,
30 $excluded_elements
31 );
32
33 /**
34 * Get all texts in HTML
35 */
36
37 $texts = wplng_parse_html( $html );
38
39 /**
40 * Get saved translation
41 */
42
43 $language_target_id = wplng_get_language_current_id();
44 $translations = array();
45
46 if ( ! empty( $texts ) ) {
47 $translations = wplng_get_translations_target( $language_target_id );
48 }
49
50 /**
51 * Get unknow texts
52 */
53
54 $texts_unknow = array();
55
56 foreach ( $texts as $text ) {
57 $is_in = false;
58 foreach ( $translations as $translation ) {
59 if ( $text === $translation['source'] ) {
60 $is_in = true;
61 break;
62 }
63 }
64 if ( ! $is_in ) {
65 $texts_unknow[] = $text;
66 }
67 }
68
69 /**
70 * Get new translated text from API
71 */
72
73 $texts_unknow_translated = wplng_api_call_translate(
74 $texts_unknow,
75 false,
76 $language_target_id
77 );
78
79 $texts_unknow = array_splice(
80 $texts_unknow,
81 0,
82 WPLNG_MAX_TRANSLATIONS + 1
83 );
84
85 /**
86 * Save new translation as wplng_translation CPT
87 */
88
89 $translations_new = array();
90
91 foreach ( $texts_unknow as $key => $text_source ) {
92 if ( isset( $texts_unknow_translated[ $key ] ) ) {
93
94 $translated = $texts_unknow_translated[ $key ];
95 $translated = esc_html( $translated );
96
97 $translations_new[] = array(
98 'source' => $text_source,
99 'translation' => $translated,
100 );
101 }
102 }
103
104 $translations_new = wplng_save_translations(
105 $translations_new,
106 $language_target_id
107 );
108
109 /**
110 * Separate page translations
111 */
112
113 $translations_in_page = array();
114
115 foreach ( $translations as $translation ) {
116 foreach ( $texts as $text ) {
117 $text = wplng_text_esc( $text );
118 if ( isset( $translation['source'] )
119 && $translation['source'] === $text
120 ) {
121 $translations_in_page[] = $translation;
122 }
123 }
124 }
125
126 /**
127 * Merge know and new translations
128 */
129
130 $translations = array_merge(
131 $translations_in_page,
132 $translations_new
133 );
134
135 /**
136 * Replace original texts by translations
137 */
138
139 $html = wplng_translate_html(
140 $html,
141 false,
142 $language_target_id,
143 $translations
144 );
145
146 /**
147 * Replace text by edit link in body
148 */
149
150 $dom = wplng_sdh_str_get_html( $html );
151
152 if ( empty( $dom ) ) {
153 return $html;
154 }
155
156 /**
157 * Replace <a> tags by <span>
158 */
159
160 foreach ( $dom->find( 'a' ) as $element ) {
161
162 $element->setAttribute( 'onclick', 'event.preventDefault()' );
163 $class = 'wplingua-editor-link';
164
165 if ( ! empty( $element->class ) ) {
166 $class = sanitize_html_class( $class . ' ' . $element->class );
167
168 $element->class = $class;
169 } else {
170 $element->setAttribute( 'class', $class );
171 }
172 }
173
174 /**
175 * Add edit links on text
176 */
177
178 $edit_link_excluded = wplng_data_excluded_editor_link();
179 $node_text_excluded = wplng_data_excluded_node_text();
180
181 foreach ( $dom->find( 'body text' ) as $element ) {
182
183 if ( in_array( $element->parent->tag, $edit_link_excluded )
184 || in_array( $element->parent->tag, $node_text_excluded )
185 ) {
186 continue;
187 }
188
189 $text = wplng_text_esc( $element->innertext );
190
191 if ( ! wplng_text_is_translatable( $text ) ) {
192 continue;
193 }
194
195 foreach ( $translations as $translation ) {
196
197 if ( ! isset( $translation['post_id'] )
198 || ! isset( $translation['translation'] )
199 ) {
200 continue;
201 }
202
203 $translated = wplng_text_esc( $translation['translation'] );
204
205 if ( $text !== $translated ) {
206 continue;
207 }
208
209 $edit_link = '';
210 if ( ! empty( $translation['post_id'] ) ) {
211 $edit_link = get_edit_post_link( $translation['post_id'] );
212 } else {
213 continue;
214 }
215
216 $onclick = 'window.open("' . esc_url( $edit_link ) . '", "_blank");';
217
218 $innertext = '<span ';
219 $innertext .= 'class="wplng-edit-link" ';
220 $innertext .= 'onclick="' . esc_js( $onclick ) . '" ';
221 $innertext .= 'title="' . esc_attr__( 'Edit this translation', 'wplingua' ) . '">';
222 $innertext .= esc_html( $text );
223 $innertext .= '</span>';
224
225 $element->innertext = $innertext;
226
227 }
228 }
229
230 $dom->save();
231 $html = (string) wplng_sdh_str_get_html( $dom );
232
233 /**
234 * Replace tag by saved excluded HTML part
235 */
236
237 $html = wplng_html_replace_exclude_tag( $html, $excluded_elements );
238
239 /**
240 * Apply a filter before return $html
241 */
242
243 $html = apply_filters( 'wplng_html_editor', $html );
244
245 return $html;
246 }
247