| 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 an array decoded from a JSON |
| 11 |
* |
| 12 |
* @param array $json_decoded |
| 13 |
* @param array $parents |
| 14 |
* @return array Texts |
| 15 |
*/ |
| 16 |
function wplng_parse_json_array( $json_decoded, $parents = array() ) { |
| 17 |
|
| 18 |
$texts = array(); |
| 19 |
$json_excluded = wplng_data_excluded_json(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Don't parse JSON if it's exclude |
| 23 |
*/ |
| 24 |
|
| 25 |
if ( in_array( $parents, $json_excluded ) ) { |
| 26 |
return array(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Parse each JSON elements |
| 31 |
*/ |
| 32 |
|
| 33 |
foreach ( $json_decoded as $key => $value ) { |
| 34 |
|
| 35 |
/** |
| 36 |
* Don't parse element if it's exclude |
| 37 |
*/ |
| 38 |
|
| 39 |
if ( in_array( array_merge( $parents, array( $key ) ), $json_excluded ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
if ( is_array( $value ) ) { |
| 44 |
|
| 45 |
/** |
| 46 |
* If element is an array, parse it |
| 47 |
*/ |
| 48 |
|
| 49 |
$texts = array_merge( |
| 50 |
$texts, |
| 51 |
wplng_parse_json_array( $value, array_merge( $parents, array( $key ) ) ) |
| 52 |
); |
| 53 |
|
| 54 |
} elseif ( is_string( $value ) ) { |
| 55 |
|
| 56 |
/** |
| 57 |
* If element is a string |
| 58 |
*/ |
| 59 |
|
| 60 |
// Ignore if is an URL or a local ID (fr_FR, fr, FR, ...) |
| 61 |
if ( wplng_str_is_url( $value ) |
| 62 |
|| wplng_str_is_locale_id( $value ) |
| 63 |
) { |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
if ( wplng_str_is_html( $value ) ) { |
| 68 |
|
| 69 |
/** |
| 70 |
* If element is a HTML, parse it |
| 71 |
*/ |
| 72 |
|
| 73 |
$texts = array_merge( |
| 74 |
$texts, |
| 75 |
wplng_parse_html( $value, array_merge( $parents, array( $key ) ) ) |
| 76 |
); |
| 77 |
|
| 78 |
} elseif ( wplng_str_is_json( $value ) ) { |
| 79 |
|
| 80 |
/** |
| 81 |
* If element is a JSON, parse it |
| 82 |
*/ |
| 83 |
|
| 84 |
$texts = array_merge( |
| 85 |
$texts, |
| 86 |
wplng_parse_json( $value, array_merge( $parents, array( $key ) ) ) |
| 87 |
); |
| 88 |
|
| 89 |
} else { |
| 90 |
|
| 91 |
/** |
| 92 |
* Element is a unknow string, check if it's translatable |
| 93 |
*/ |
| 94 |
|
| 95 |
$is_translatable = wplng_json_element_is_translatable( |
| 96 |
$value, |
| 97 |
array_merge( $parents, array( $key ) ) |
| 98 |
); |
| 99 |
|
| 100 |
if ( ! $is_translatable ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
$texts[] = $value; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$texts = array_unique( $texts ); // Remove duplicate |
| 110 |
|
| 111 |
return $texts; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* wpLingua Parser : Get texts in an JSON |
| 117 |
* |
| 118 |
* @param string $json |
| 119 |
* @param array $parents |
| 120 |
* @return array Texts |
| 121 |
*/ |
| 122 |
function wplng_parse_json( $json, $parents = array() ) { |
| 123 |
|
| 124 |
$json_decoded = json_decode( $json, true ); |
| 125 |
|
| 126 |
if ( empty( $json_decoded ) || ! is_array( $json_decoded ) ) { |
| 127 |
return array(); |
| 128 |
} |
| 129 |
|
| 130 |
$texts = wplng_parse_json_array( $json_decoded, $parents ); |
| 131 |
|
| 132 |
$texts = array_unique( $texts ); // Remove duplicate |
| 133 |
|
| 134 |
return $texts; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* wpLingua Parser : Get texts in an JS |
| 140 |
* |
| 141 |
* @param string $js |
| 142 |
* @return array Texts |
| 143 |
*/ |
| 144 |
function wplng_parse_js( $js ) { |
| 145 |
|
| 146 |
$texts = array(); |
| 147 |
$json = array(); |
| 148 |
|
| 149 |
if ( empty( trim( $js ) ) ) { |
| 150 |
return array(); |
| 151 |
} |
| 152 |
|
| 153 |
preg_match_all( |
| 154 |
'#var\s(.*)\s?=\s?(\{.*\});#Ui', |
| 155 |
$js, |
| 156 |
$json |
| 157 |
); |
| 158 |
|
| 159 |
if ( ! empty( $json[1][0] ) && ! empty( $json[2][0] ) ) { |
| 160 |
|
| 161 |
$var_name = $json[1][0]; |
| 162 |
$var_json = $json[2][0]; |
| 163 |
|
| 164 |
$texts = wplng_parse_json( |
| 165 |
$var_json, |
| 166 |
array( $var_name ) |
| 167 |
); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
$texts = array_unique( $texts ); // Remove duplicate |
| 172 |
|
| 173 |
return $texts; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* wpLingua Parser : Get texts in an HTML |
| 179 |
* |
| 180 |
* @param string $html |
| 181 |
* @return array Texts |
| 182 |
*/ |
| 183 |
function wplng_parse_html( $html ) { |
| 184 |
|
| 185 |
$texts = array(); |
| 186 |
$dom = wplng_sdh_str_get_html( $html ); |
| 187 |
|
| 188 |
if ( empty( $dom ) ) { |
| 189 |
return array(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Find and parse JSON |
| 194 |
*/ |
| 195 |
|
| 196 |
foreach ( $dom->find( 'script[type="application/ld+json"]' ) as $element ) { |
| 197 |
$texts = array_merge( |
| 198 |
$texts, |
| 199 |
wplng_parse_json( $element->innertext ) |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Find and translate JS |
| 205 |
*/ |
| 206 |
|
| 207 |
foreach ( $dom->find( 'script' ) as $element ) { |
| 208 |
$texts = array_merge( |
| 209 |
$texts, |
| 210 |
wplng_parse_js( $element->innertext ) |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Parse Node text |
| 216 |
*/ |
| 217 |
|
| 218 |
$node_text_excluded = wplng_data_excluded_node_text(); |
| 219 |
|
| 220 |
foreach ( $dom->find( 'text' ) as $element ) { |
| 221 |
|
| 222 |
$text = wplng_text_esc( $element->innertext ); |
| 223 |
|
| 224 |
if ( in_array( $element->parent->tag, $node_text_excluded ) |
| 225 |
|| ! wplng_text_is_translatable( $text ) |
| 226 |
) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
$texts[] = $text; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Parse attr |
| 235 |
*/ |
| 236 |
|
| 237 |
$attr_to_translate = wplng_data_attr_text_to_translate(); |
| 238 |
|
| 239 |
foreach ( $attr_to_translate as $attr ) { |
| 240 |
foreach ( $dom->find( $attr['selector'] ) as $element ) { |
| 241 |
|
| 242 |
if ( empty( $element->attr[ $attr['attr'] ] ) ) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
$text = wplng_text_esc( $element->attr[ $attr['attr'] ] ); |
| 247 |
|
| 248 |
if ( ! wplng_text_is_translatable( $text ) ) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
$texts[] = $text; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
$texts = array_unique( $texts ); // Remove duplicate |
| 257 |
|
| 258 |
return $texts; |
| 259 |
} |
| 260 |
|