PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.10.0
Translate and Go multilingual – Automatic AI translation – wpLingua v2.10.0
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 1.1.1 All 111 releases
wplingua / inc / parser / js.php

js.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.10.0, at inc/parser/js.php

171 lines 3.0 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 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 );
30
31 if ( wplng_str_is_script_i18n( $js ) ) {
32 $texts = array_merge(
33 $texts,
34 wplng_parse_js_json_in_i18n_script( $js )
35 );
36 }
37
38 return $texts;
39 }
40
41
42 /**
43 * Parse JSON contained in 'var', 'let' or 'window._'
44 *
45 * @param string $js
46 * @return array Texts
47 */
48 function wplng_parse_js_json_in_var( $js ) {
49
50 $texts = array();
51 $json = array();
52
53 preg_match_all(
54 '#(var\s|let\s|window\._)([A-Za-z0-9_]+)\s?=\s?(\{(?:[^{}"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\}|\[(?:[^\[\]"\'\\\\]+|"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|(?3))*\])\s*;#Us',
55 $js,
56 $json
57 );
58
59 if ( ! empty( $json[2] ) && is_array( $json[2] ) ) {
60 foreach ( $json[2] as $key => $var_name ) {
61
62 $var_name = trim( $var_name );
63
64 if ( empty( $var_name ) || empty( $json[3][ $key ] ) ) {
65 continue;
66 }
67
68 $var_json = trim( $json[3][ $key ] );
69
70 $texts = array_merge(
71 $texts,
72 wplng_parse_json(
73 $var_json,
74 array( $var_name )
75 )
76 );
77
78 }
79 }
80
81 return $texts;
82 }
83
84
85 /**
86 * Parse JSON in i18n script
87 *
88 * You can use wplng_parse_js_json_in_i18n_script( $js )
89 * to check if the string is a i18n script
90 *
91 * @param string $js
92 * @return array Texts
93 */
94 function wplng_parse_js_json_in_i18n_script( $js ) {
95
96 $texts = array();
97 $json = array();
98
99 preg_match_all(
100 '#\(\s?["|\'](.*)["|\'],\s?(.*)\s?\);#Ui',
101 $js,
102 $json
103 );
104
105 if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
106 foreach ( $json[1] as $key => $var_name ) {
107
108 $var_name = trim( $var_name );
109
110 if ( empty( $var_name ) || empty( $json[2][ $key ] ) ) {
111 continue;
112 }
113
114 $var_json = trim( $json[2][ $key ] );
115
116 $texts = array_merge(
117 $texts,
118 wplng_parse_json(
119 $var_json,
120 array( $var_name )
121 )
122 );
123
124 }
125 }
126
127 return $texts;
128 }
129
130
131 /**
132 * Parse JSON encoded as URL
133 *
134 * @param string $js
135 * @return array Texts
136 */
137 function wplng_parse_js_json_encoded_as_url( $js ) {
138
139 $texts = array();
140 $json = array();
141
142 /**
143 * URL encoded JSON
144 */
145
146 preg_match_all(
147 '#JSON\.parse\(\sdecodeURIComponent\(\s\'(.*)\'\s\)\s\)#Ui',
148 $js,
149 $json
150 );
151
152 if ( ! empty( $json[1] ) && is_array( $json[1] ) ) {
153
154 foreach ( $json[1] as $key => $encoded_json ) {
155
156 $var_json = urldecode( $encoded_json );
157
158 $texts = array_merge(
159 $texts,
160 wplng_parse_json(
161 $var_json,
162 array( 'EncodedAsURL' )
163 )
164 );
165
166 }
167 }
168
169 return $texts;
170 }
171