PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.7
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
← All changes | inc/parser/js.php +227 -227 2.12.32.16.7 View file →
@@ -1,227 +1,227 @@
1 -<?php
2 -
3 -// If this file is called directly, abort.
4 -if ( ! defined( 'WPINC' ) ) {
5 - die;
6 -}
7 -
8 -
9 -/**
10 - * wpLingua Parser : Get texts in a JS script
11 - *
12 - * @param string $js
13 - * @return array Texts
14 - */
15 -function wplng_parse_js( $js ) {
16 -
17 - $js = trim( $js );
18 -
19 - // Check if $JS is empty or is wp emoji script
20 - if ( '' === $js
21 - || wplng_str_contains( $js, '_wpemojiSettings' )
22 - ) {
23 - return array();
24 - }
25 -
26 - $texts = array_merge(
27 - wplng_parse_js_json_in_var( $js ),
28 - wplng_parse_js_json_encoded_as_url( $js ),
29 - wplng_parse_js_json_in_function_call( $js ),
30 - );
31 -
32 - if ( wplng_str_is_script_i18n( $js ) ) {
33 - $texts = array_merge(
34 - $texts,
35 - wplng_parse_js_json_in_i18n_script( $js )
36 - );
37 - }
38 -
39 - return $texts;
40 -}
41 -
42 -
43 -/**
44 - * Parse JSON contained in 'var', 'let' or 'window._'
45 - *
46 - * @param string $js
47 - * @return array Texts
48 - */
49 -function wplng_parse_js_json_in_var( $js ) {
50 -
51 - $texts = array();
52 - $json = array();
53 -
54 - preg_match_all(
55 - '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\}|\[(?:[^\[\]"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\])\s*;#Us',
56 - $js,
57 - $json
58 - );
59 -
60 - if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
61 - foreach ( $json[2] as $key => $var_name ) {
62 -
63 - $var_name = trim( $var_name );
64 -
65 - if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
66 - continue;
67 - }
68 -
69 - $var_json = trim( $json[3][ $key ] );
70 -
71 - $texts = array_merge(
72 - $texts,
73 - wplng_parse_json(
74 - $var_json,
75 - array( $var_name )
76 - )
77 - );
78 -
79 - }
80 - }
81 -
82 - return $texts;
83 -}
84 -
85 -
86 -/**
87 - * Parse JSON in i18n script
88 - *
89 - * You can use wplng_parse_js_json_in_i18n_script( $js )
90 - * to check if the string is a i18n script
91 - *
92 - * @param string $js
93 - * @return array Texts
94 - */
95 -function wplng_parse_js_json_in_i18n_script( $js ) {
96 -
97 - $texts = array();
98 - $json = array();
99 -
100 - preg_match_all(
101 - '#\(\s?["|\'](.*)["|\'],\s?(.*)\s?\);#Ui',
102 - $js,
103 - $json
104 - );
105 -
106 - if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
107 - foreach ( $json[1] as $key => $var_name ) {
108 -
109 - $var_name = trim( $var_name );
110 -
111 - if ( empty( $var_name ) || empty( $json[2][ $key ] ) ) {
112 - continue;
113 - }
114 -
115 - $var_json = trim( $json[2][ $key ] );
116 -
117 - $texts = array_merge(
118 - $texts,
119 - wplng_parse_json(
120 - $var_json,
121 - array( 'i18n_script', $var_name )
122 - )
123 - );
124 -
125 - }
126 - }
127 -
128 - return $texts;
129 -}
130 -
131 -
132 -/**
133 - * Parse JSON encoded as URL
134 - *
135 - * @param string $js
136 - * @return array Texts
137 - */
138 -function wplng_parse_js_json_encoded_as_url( $js ) {
139 -
140 - $texts = array();
141 - $json = array();
142 -
143 - preg_match_all(
144 - '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?JSON\.parse\(\sdecodeURIComponent\(\s\'(.*)\'\s\)\s\)#Ui',
145 - $js,
146 - $json
147 - );
148 -
149 - if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
150 - foreach ( $json[2] as $key => $var_name ) {
151 -
152 - $var_name = trim( $var_name );
153 -
154 - if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
155 - continue;
156 - }
157 -
158 - $encoded_json = trim( $json[3][ $key ] );
159 - $var_json = urldecode( $encoded_json );
160 -
161 - $texts = array_merge(
162 - $texts,
163 - wplng_parse_json(
164 - $var_json,
165 - array( 'encoded_as_url', $var_name )
166 - )
167 - );
168 - }
169 - }
170 -
171 - return $texts;
172 -}
173 -
174 -
175 -/**
176 - * Parse JSON passed as argument to function calls like jQuery.datepicker.setDefaults({...})
177 - *
178 - * @param string $js
179 - * @return array Texts
180 - */
181 -function wplng_parse_js_json_in_function_call( $js ) {
182 -
183 - $texts = array();
184 - $json = array();
185 -
186 - // Whitelist of function calls that contain translatable JSON
187 - $allowed_functions = wplng_data_json_in_js_functions();
188 -
189 - if ( empty( $allowed_functions ) ) {
190 - return array();
191 - }
192 -
193 - preg_match_all(
194 - '#([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\s*\(\s*(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?2))*\})\s*\)#Us',
195 - $js,
196 - $json
197 - );
198 -
199 - if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
200 - foreach ( $json[1] as $key => $func_name ) {
201 -
202 - $func_name = trim( $func_name );
203 -
204 - // Skip if not in whitelist
205 - if ( ! in_array( $func_name, $allowed_functions, true ) ) {
206 - continue;
207 - }
208 -
209 - if ( empty( $json[2][ $key ] ) ) {
210 - continue;
211 - }
212 -
213 - $var_json = trim( $json[2][ $key ] );
214 -
215 - $texts = array_merge(
216 - $texts,
217 - wplng_parse_json(
218 - $var_json,
219 - array( $func_name )
220 - )
221 - );
222 -
223 - }
224 - }
225 -
226 - return $texts;
227 -}
1 +<?php
2 +
3 +// If this file is called directly, abort.
4 +if ( ! defined( 'WPINC' ) ) {
5 + die;
6 +}
7 +
8 +
9 +/**
10 + * wpLingua Parser : Get texts in a JS script
11 + *
12 + * @param string $js
13 + * @return array Texts
14 + */
15 +function wplng_parse_js( $js ) {
16 +
17 + $js = trim( $js );
18 +
19 + // Check if $JS is empty or is wp emoji script
20 + if ( '' === $js
21 + || wplng_str_contains( $js, '_wpemojiSettings' )
22 + ) {
23 + return array();
24 + }
25 +
26 + $texts = array_merge(
27 + wplng_parse_js_json_in_var( $js ),
28 + wplng_parse_js_json_encoded_as_url( $js ),
29 + wplng_parse_js_json_in_function_call( $js ),
30 + );
31 +
32 + if ( wplng_str_is_script_i18n( $js ) ) {
33 + $texts = array_merge(
34 + $texts,
35 + wplng_parse_js_json_in_i18n_script( $js )
36 + );
37 + }
38 +
39 + return $texts;
40 +}
41 +
42 +
43 +/**
44 + * Parse JSON contained in 'var', 'let' or 'window._'
45 + *
46 + * @param string $js
47 + * @return array Texts
48 + */
49 +function wplng_parse_js_json_in_var( $js ) {
50 +
51 + $texts = array();
52 + $json = array();
53 +
54 + preg_match_all(
55 + '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\}|\[(?:[^\[\]"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\])\s*;#Us',
56 + $js,
57 + $json
58 + );
59 +
60 + if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
61 + foreach ( $json[2] as $key => $var_name ) {
62 +
63 + $var_name = trim( $var_name );
64 +
65 + if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
66 + continue;
67 + }
68 +
69 + $var_json = trim( $json[3][ $key ] );
70 +
71 + $texts = array_merge(
72 + $texts,
73 + wplng_parse_json(
74 + $var_json,
75 + array( $var_name )
76 + )
77 + );
78 +
79 + }
80 + }
81 +
82 + return $texts;
83 +}
84 +
85 +
86 +/**
87 + * Parse JSON in i18n script
88 + *
89 + * You can use wplng_parse_js_json_in_i18n_script( $js )
90 + * to check if the string is a i18n script
91 + *
92 + * @param string $js
93 + * @return array Texts
94 + */
95 +function wplng_parse_js_json_in_i18n_script( $js ) {
96 +
97 + $texts = array();
98 + $json = array();
99 +
100 + preg_match_all(
101 + '#\(\s?["\'](.*)["\'],\s?(.*)\s?\);#Ui',
102 + $js,
103 + $json
104 + );
105 +
106 + if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
107 + foreach ( $json[1] as $key => $var_name ) {
108 +
109 + $var_name = trim( $var_name );
110 +
111 + if ( empty( $var_name ) || empty( $json[2][ $key ] ) ) {
112 + continue;
113 + }
114 +
115 + $var_json = trim( $json[2][ $key ] );
116 +
117 + $texts = array_merge(
118 + $texts,
119 + wplng_parse_json(
120 + $var_json,
121 + array( 'i18n_script', $var_name )
122 + )
123 + );
124 +
125 + }
126 + }
127 +
128 + return $texts;
129 +}
130 +
131 +
132 +/**
133 + * Parse JSON encoded as URL
134 + *
135 + * @param string $js
136 + * @return array Texts
137 + */
138 +function wplng_parse_js_json_encoded_as_url( $js ) {
139 +
140 + $texts = array();
141 + $json = array();
142 +
143 + preg_match_all(
144 + '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?JSON\.parse\(\sdecodeURIComponent\(\s\'(.*)\'\s\)\s\)#Ui',
145 + $js,
146 + $json
147 + );
148 +
149 + if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
150 + foreach ( $json[2] as $key => $var_name ) {
151 +
152 + $var_name = trim( $var_name );
153 +
154 + if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
155 + continue;
156 + }
157 +
158 + $encoded_json = trim( $json[3][ $key ] );
159 + $var_json = urldecode( $encoded_json );
160 +
161 + $texts = array_merge(
162 + $texts,
163 + wplng_parse_json(
164 + $var_json,
165 + array( 'encoded_as_url', $var_name )
166 + )
167 + );
168 + }
169 + }
170 +
171 + return $texts;
172 +}
173 +
174 +
175 +/**
176 + * Parse JSON passed as argument to function calls like jQuery.datepicker.setDefaults({...})
177 + *
178 + * @param string $js
179 + * @return array Texts
180 + */
181 +function wplng_parse_js_json_in_function_call( $js ) {
182 +
183 + $texts = array();
184 + $json = array();
185 +
186 + // Whitelist of function calls that contain translatable JSON
187 + $allowed_functions = wplng_data_json_in_js_functions();
188 +
189 + if ( empty( $allowed_functions ) ) {
190 + return array();
191 + }
192 +
193 + preg_match_all(
194 + '#([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\s*\(\s*(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?2))*\})\s*\)#Us',
195 + $js,
196 + $json
197 + );
198 +
199 + if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
200 + foreach ( $json[1] as $key => $func_name ) {
201 +
202 + $func_name = trim( $func_name );
203 +
204 + // Skip if not in whitelist
205 + if ( ! in_array( $func_name, $allowed_functions, true ) ) {
206 + continue;
207 + }
208 +
209 + if ( empty( $json[2][ $key ] ) ) {
210 + continue;
211 + }
212 +
213 + $var_json = trim( $json[2][ $key ] );
214 +
215 + $texts = array_merge(
216 + $texts,
217 + wplng_parse_json(
218 + $var_json,
219 + array( $func_name )
220 + )
221 + );
222 +
223 + }
224 + }
225 +
226 + return $texts;
227 +}