PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.4
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.4
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 / html-updater.php

html-updater.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.0.4, at inc/html-updater.php

246 lines 5.6 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 * Print HTML of alternate link with hreflang for each available languages
11 *
12 * @return void
13 */
14 function wplng_link_alternate_hreflang() {
15
16 if ( ! wplng_url_is_translatable() ) {
17 return;
18 }
19
20 $html = PHP_EOL . PHP_EOL;
21 $language_website = wplng_get_language_website();
22 $languages_target = wplng_get_languages_target();
23
24 if ( empty( $language_website ) || empty( $languages_target ) ) {
25 return;
26 }
27
28 // Create the starting comment
29 $html .= '<!-- This site is made multilingual with the wpLingua plugin -->';
30 $html .= PHP_EOL;
31
32 // Create meta generator
33 $html .= '<meta ';
34 $html .= 'name="generator" ';
35 $html .= 'content="wpLingua ' . esc_attr( WPLNG_PLUGIN_VERSION ) . '" />';
36 $html .= PHP_EOL;
37
38 // Create alternate link for website language
39 $html .= '<link ';
40 $html .= 'rel="alternate" ';
41 $html .= 'hreflang="' . esc_attr( $language_website['id'] ) . '" ';
42 $html .= 'href="' . esc_url( wplng_get_url_original() ) . '" />';
43 $html .= PHP_EOL;
44
45 // Create alternate link for each target languages
46 foreach ( $languages_target as $language_target ) {
47
48 $url = wplng_get_url_current_for_language( $language_target['id'] );
49
50 $html .= '<link ';
51 $html .= 'rel="alternate" ';
52 $html .= 'hreflang="' . esc_attr( $language_target['id'] ) . '" ';
53 $html .= 'href="' . esc_url( $url ) . '" />';
54 $html .= PHP_EOL;
55 }
56
57 // Create the ending comment
58 $html .= '<!-- / wpLingua plugin. -->' . PHP_EOL . PHP_EOL;
59
60 echo $html;
61 }
62
63
64 /**
65 * Get wpLingua excluded selectors
66 *
67 * @return array
68 */
69 function wplng_get_selector_exclude() {
70
71 $selector_exclude = array();
72
73 // Get, check and sanitize excluded selector as string
74 $option = get_option( 'wplng_excluded_selectors' );
75 if ( empty( $option ) || ! is_string( $option ) ) {
76 wplng_data_excluded_selector_default();
77 }
78 $option = sanitize_textarea_field( $option );
79
80 // Get exclude selector as array
81 $option = explode( PHP_EOL, $option );
82
83 // Sanitize each selectors
84 foreach ( $option as $selector ) {
85 $selector = esc_attr( trim( $selector ) );
86 if ( ! empty( $selector ) ) {
87 $selector_exclude[] = $selector;
88 }
89 }
90
91 // Add default selectors
92 $selector_exclude = array_merge(
93 wplng_data_excluded_selector_default(), // Make HTML parsed smaller
94 $selector_exclude
95 );
96
97 // Remove duplicate
98 $selector_exclude = array_unique( $selector_exclude );
99
100 // Apply wplng_selector_exclude filters
101 $selector_exclude = apply_filters(
102 'wplng_selector_exclude',
103 $selector_exclude
104 );
105
106 return $selector_exclude;
107 }
108
109
110 /**
111 * Replace all excluded elements by empty tags with wplng-tag-exclude attribute
112 *
113 * @param string $html
114 * @param array $excluded_elements
115 * @return string HTML
116 */
117 function wplng_html_set_exclude_tag( $html, &$excluded_elements ) {
118
119 $selector_exclude = wplng_get_selector_exclude();
120
121 if ( empty( $selector_exclude ) ) {
122 return $html;
123 }
124
125 $dom = wplng_sdh_str_get_html( $html );
126
127 if ( false === $dom ) {
128 return $html;
129 }
130
131 foreach ( $selector_exclude as $selector ) {
132 foreach ( $dom->find( $selector ) as $element ) {
133 $excluded_elements[] = $element->outertext;
134 $attr = count( $excluded_elements ) - 1;
135 $element->outertext = '<div wplng-tag-exclude="' . esc_attr( $attr ) . '"></div>';
136 }
137 }
138
139 $dom->save();
140
141 return (string) wplng_sdh_str_get_html( $dom );
142 }
143
144
145 /**
146 * Replace all tags with wplng-tag-exclude attribute by value in $excluded_elements
147 *
148 * @param string $html
149 * @param array $excluded_elements
150 * @return string
151 */
152 function wplng_html_replace_exclude_tag( $html, $excluded_elements ) {
153
154 if ( empty( $excluded_elements ) ) {
155 return $html;
156 }
157
158 $dom = wplng_sdh_str_get_html( $html );
159
160 if ( false === $dom ) {
161 return $html;
162 }
163
164 foreach ( $dom->find( '[wplng-tag-exclude]' ) as $element ) {
165
166 if ( isset( $element->attr['wplng-tag-exclude'] ) ) {
167
168 $exclude_index = (int) $element->attr['wplng-tag-exclude'];
169
170 if ( isset( $excluded_elements[ $exclude_index ] ) ) {
171 $element->outertext = $excluded_elements[ $exclude_index ];
172 }
173 }
174 }
175
176 $dom->save();
177
178 return (string) wplng_sdh_str_get_html( $dom );
179 }
180
181
182 /**
183 * wpLingua init function
184 *
185 * @return void
186 */
187 function wplng_ob_start() {
188
189 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
190 ob_start( 'wplng_ob_callback_ajax' );
191 return;
192 }
193
194 if ( wplng_get_language_website_id() === wplng_get_language_current_id() ) {
195 return;
196 }
197
198 global $wplng_request_uri;
199 $current_path = $wplng_request_uri;
200
201 if ( ! is_string( $current_path ) ) {
202 return;
203 }
204
205 $origin_path = '/' . substr( $current_path, 4, strlen( $current_path ) - 1 );
206 $origin_path = sanitize_url( $origin_path );
207
208 if ( ! wplng_url_is_translatable( $origin_path ) ) {
209 wp_redirect( $origin_path );
210 exit;
211 }
212
213 $_SERVER['REQUEST_URI'] = $origin_path;
214
215 if ( current_user_can( 'edit_posts' ) ) {
216 if ( isset( $_GET['wplingua-editor'] ) ) {
217
218 ob_start( 'wplng_ob_callback_editor' );
219
220 } elseif ( isset( $_GET['wplingua-list'] ) ) {
221
222 add_filter(
223 'body_class', function( $classes ) {
224 return array_merge( $classes, array( 'wplingua-list' ) );
225 }
226 );
227
228 ob_start( 'wplng_ob_callback_list' );
229
230 } else {
231
232 add_filter(
233 'body_class', function( $classes ) {
234 return array_merge( $classes, array( 'wplingua-editor' ) );
235 }
236 );
237
238 ob_start( 'wplng_ob_callback_translate' );
239
240 }
241 } else {
242 ob_start( 'wplng_ob_callback_translate' );
243 }
244
245 }
246