class-wpml-encoding.php
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
class WPML_Encoding {
/**
* @param string $string The string to decode.
* @param string $encodings A comma separated list of encodings in the order that the data was encoded
*
* @return mixed
*/
public static function decode( $string, $encodings ) {
$decoded_data = $string;
// NOTE: We decode in the reverse order of the encodings given
foreach ( array_reverse( explode( ',', $encodings ) ) as $encoding ) {
switch ( $encoding ) {
case 'json':
$decoded_data = json_decode( $decoded_data, true );
break;
case 'base64':
$decoded_data = base64_decode( $decoded_data );
break;
case 'urlencode':
$decoded_data = urldecode( $decoded_data );
break;
}
}
/**
* @since 4.1.0
*/
return apply_filters( 'wpml_decode_string', $decoded_data, $string, $encodings );
}
/**
* @param mixed $data The data to encode.
* @param string $encodings A comma separated list of encodings in the order that the data was encoded
*
* @return string
*/
public static function encode( $data, $encodings ) {
$encoded_data = $data;
foreach ( explode( ',', $encodings ) as $encoding ) {
switch ( $encoding ) {
case 'json':
$encoded_data = wp_json_encode( $encoded_data );
break;
case 'base64':
$encoded_data = base64_encode( $encoded_data );
break;
case 'urlencode':
$encoded_data = urlencode( $encoded_data );
break;
}
}
/**
* @since 4.1.0
*/
return apply_filters( 'wpml_encode_string', $encoded_data, $data, $encodings );
}
}