| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Get dictionary entries |
| 11 |
* |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
function wplng_dictionary_get_entries() { |
| 15 |
|
| 16 |
$entries_clear = array(); |
| 17 |
$entries_json = get_option( 'wplng_dictionary_entries' ); |
| 18 |
$entries = json_decode( $entries_json, true ); |
| 19 |
|
| 20 |
if ( empty( $entries ) || ! is_array( $entries ) ) { |
| 21 |
return array(); |
| 22 |
} |
| 23 |
|
| 24 |
foreach ( $entries as $entry ) { |
| 25 |
|
| 26 |
/** |
| 27 |
* Get and check the source |
| 28 |
*/ |
| 29 |
|
| 30 |
if ( ! isset( $entry['source'] ) |
| 31 |
|| ! is_string( $entry['source'] ) |
| 32 |
|| strlen( $entry['source'] ) >= 256 |
| 33 |
) { |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
$source_clear = wplng_text_esc( $entry['source'] ); |
| 38 |
$source_clear = str_replace( '⊕', '', $source_clear ); |
| 39 |
$source_clear = str_replace( '⊖', '', $source_clear ); |
| 40 |
$source_clear = preg_replace( '#\[wplng_dictionary.*\]#', '', $source_clear ); |
| 41 |
$source_clear = preg_replace( '#\[\/wplng_dictionary\]#', '', $source_clear ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Check if rule already exist |
| 45 |
*/ |
| 46 |
|
| 47 |
$already_in = false; |
| 48 |
foreach ( $entries_clear as $entry_clear ) { |
| 49 |
if ( $source_clear === $entry_clear['source'] ) { |
| 50 |
$already_in = true; |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( $already_in ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get and check the rules |
| 61 |
*/ |
| 62 |
|
| 63 |
if ( ! isset( $entry['rules'] ) |
| 64 |
|| ! is_array( $entry['rules'] ) |
| 65 |
) { |
| 66 |
/** |
| 67 |
* Create the clear entries |
| 68 |
*/ |
| 69 |
|
| 70 |
$entries_clear[] = array( |
| 71 |
'source' => $source_clear, |
| 72 |
); |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$rules_clear = array(); |
| 77 |
|
| 78 |
foreach ( $entry['rules'] as $language_id => $rule ) { |
| 79 |
if ( ! wplng_is_valid_language_id( $language_id ) |
| 80 |
|| ! is_string( $rule ) |
| 81 |
|| '' === trim( $rule ) |
| 82 |
|| $rule === $source_clear |
| 83 |
|| strlen( $rule ) >= 256 |
| 84 |
) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
$rules_clear[ $language_id ] = wplng_text_esc( $rule ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Create the clear entries |
| 93 |
*/ |
| 94 |
|
| 95 |
if ( empty( $rules_clear ) ) { |
| 96 |
$entries_clear[] = array( |
| 97 |
'source' => $source_clear, |
| 98 |
); |
| 99 |
} else { |
| 100 |
$entries_clear[] = array( |
| 101 |
'source' => $source_clear, |
| 102 |
'rules' => $rules_clear, |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Sort dictionary entries by sources length |
| 109 |
*/ |
| 110 |
|
| 111 |
usort( |
| 112 |
$entries_clear, |
| 113 |
function ( $a, $b ) { |
| 114 |
return strlen( $b['source'] ) - strlen( $a['source'] ); |
| 115 |
} |
| 116 |
); |
| 117 |
|
| 118 |
/** |
| 119 |
* Apply wplng_dictionary_entries filter |
| 120 |
*/ |
| 121 |
|
| 122 |
$entries_clear = apply_filters( |
| 123 |
'wplng_dictionary_entries', |
| 124 |
$entries_clear |
| 125 |
); |
| 126 |
|
| 127 |
return $entries_clear; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Get dictionary entries in JSON format |
| 133 |
* |
| 134 |
* @return string JSON |
| 135 |
*/ |
| 136 |
function wplng_dictionary_get_entries_json() { |
| 137 |
return wp_json_encode( |
| 138 |
wplng_dictionary_get_entries(), |
| 139 |
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Add dictionary tag on a texts list |
| 146 |
* |
| 147 |
* @param array $texts |
| 148 |
* @param array $dictionary_entries |
| 149 |
* @return array Texts tagged |
| 150 |
*/ |
| 151 |
function wplng_dictionary_add_tags( $texts, $dictionary_entries = false ) { |
| 152 |
|
| 153 |
if ( false === $dictionary_entries ) { |
| 154 |
$dictionary_entries = wplng_dictionary_get_entries(); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Preg quote sources texts |
| 159 |
*/ |
| 160 |
|
| 161 |
foreach ( $dictionary_entries as $entry_key => $entry ) { |
| 162 |
$dictionary_entries[ $entry_key ]['source'] = preg_quote( $entry['source'] ); |
| 163 |
} |
| 164 |
|
| 165 |
foreach ( $texts as $text_key => $text ) { |
| 166 |
|
| 167 |
/** |
| 168 |
* Get used dictionary entry in current text |
| 169 |
*/ |
| 170 |
|
| 171 |
$entries_used = array(); |
| 172 |
|
| 173 |
foreach ( $dictionary_entries as $entry_key => $entry ) { |
| 174 |
|
| 175 |
$preg_match = array(); |
| 176 |
|
| 177 |
preg_match_all( |
| 178 |
'#' . $entry['source'] . '#i', |
| 179 |
$text, |
| 180 |
$preg_match |
| 181 |
); |
| 182 |
|
| 183 |
$preg_match = $preg_match[0]; |
| 184 |
|
| 185 |
foreach ( $preg_match as $key => $match ) { |
| 186 |
|
| 187 |
$upper = 'none'; |
| 188 |
if ( $match === strtoupper( $match ) ) { |
| 189 |
// Check uppercase |
| 190 |
$upper = 'all'; |
| 191 |
} elseif ( $match === ucfirst( $match ) ) { |
| 192 |
// Check capitalize |
| 193 |
$upper = 'first'; |
| 194 |
} |
| 195 |
|
| 196 |
$entry_current = $entry; |
| 197 |
$entry_current['source'] = $match; |
| 198 |
$entry_current['key'] = $entry_key; |
| 199 |
$entry_current['upper'] = $upper; |
| 200 |
$entries_used[] = $entry_current; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Remove dupicate in used entries |
| 206 |
*/ |
| 207 |
|
| 208 |
$entries_used = array_map( 'serialize', $entries_used ); |
| 209 |
$entries_used = array_unique( $entries_used ); |
| 210 |
$entries_used = array_map( 'unserialize', $entries_used ); |
| 211 |
|
| 212 |
/** |
| 213 |
* Put tempory tag in current text |
| 214 |
*/ |
| 215 |
|
| 216 |
foreach ( $entries_used as $entry_key => $entry_used ) { |
| 217 |
$text = preg_replace( |
| 218 |
'#\b' . $entry_used['source'] . '\b#', |
| 219 |
'⊕' . str_repeat( '⊖', $entry_key + 1 ) . '⊕', |
| 220 |
$text |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Repace tempory tag by final tag |
| 226 |
*/ |
| 227 |
|
| 228 |
foreach ( $entries_used as $entry_key => $entry ) { |
| 229 |
|
| 230 |
$search = '⊕' . str_repeat( '⊖', $entry_key + 1 ) . '⊕'; |
| 231 |
|
| 232 |
$replace = '[wplng_dictionary '; |
| 233 |
$replace .= 'key="' . $entry['key'] . '" '; |
| 234 |
$replace .= 'upper="' . $entry['upper'] . '"]'; |
| 235 |
$replace .= $entry['source']; |
| 236 |
$replace .= '[/wplng_dictionary]'; |
| 237 |
|
| 238 |
$text = str_replace( |
| 239 |
$search, |
| 240 |
$replace, |
| 241 |
$text |
| 242 |
); |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Set updated text in text array |
| 248 |
*/ |
| 249 |
|
| 250 |
$texts[ $text_key ] = $text; |
| 251 |
} |
| 252 |
|
| 253 |
return $texts; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* Transform dictionary tags to translated text |
| 259 |
* |
| 260 |
* @param array $texts |
| 261 |
* @param array $dictionary_entries |
| 262 |
* @return array Texts untagged |
| 263 |
*/ |
| 264 |
function wplng_dictionary_replace_tags( $texts, $dictionary_entries = false, $language_id = false ) { |
| 265 |
|
| 266 |
if ( false === $language_id ) { |
| 267 |
$language_id = wplng_get_language_current_id(); |
| 268 |
} |
| 269 |
|
| 270 |
if ( false === $dictionary_entries ) { |
| 271 |
$dictionary_entries = wplng_dictionary_get_entries(); |
| 272 |
} |
| 273 |
|
| 274 |
foreach ( $texts as $text_key => $text ) { |
| 275 |
foreach ( $dictionary_entries as $key => $entry ) { |
| 276 |
|
| 277 |
// For ruls "Do not translate |
| 278 |
$replacement = $entry['source']; |
| 279 |
|
| 280 |
// For specific rule by language |
| 281 |
if ( ! empty( $entry['rules'][ $language_id ] ) ) { |
| 282 |
$replacement = $entry['rules'][ $language_id ]; |
| 283 |
} |
| 284 |
|
| 285 |
// Replacement for text in uppercase |
| 286 |
$text = preg_replace( |
| 287 |
'#\[wplng_dictionary key="' . $key . '" upper="all"\].+\[\/wplng_dictionary\]#U', |
| 288 |
strtoupper( $replacement ), |
| 289 |
$text |
| 290 |
); |
| 291 |
|
| 292 |
// Replacement for text when only the first letter is uppercase |
| 293 |
$text = preg_replace( |
| 294 |
'#\[wplng_dictionary key="' . $key . '" upper="first"\].+\[\/wplng_dictionary\]#U', |
| 295 |
ucfirst( $replacement ), |
| 296 |
$text |
| 297 |
); |
| 298 |
|
| 299 |
// Replacement for other case |
| 300 |
$text = preg_replace( |
| 301 |
'#\[wplng_dictionary key="' . $key . '" upper="none"\].+\[\/wplng_dictionary\]#U', |
| 302 |
$replacement, |
| 303 |
$text |
| 304 |
); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
// Cleaning of any residual rules |
| 309 |
$text = preg_replace( |
| 310 |
'#\[wplng_dictionary key=".*" upper=".*"\](.+)\[\/wplng_dictionary\]#U', |
| 311 |
'${1}', |
| 312 |
$text |
| 313 |
); |
| 314 |
|
| 315 |
$texts[ $text_key ] = $text; |
| 316 |
} |
| 317 |
|
| 318 |
return $texts; |
| 319 |
} |
| 320 |
|