|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* An implementation of Automattic\Jetpack\Sync\Codec_Interface that uses gzip's DEFLATE |
|
4
|
|
|
* algorithm to compress objects serialized using json_encode. |
|
5
|
|
|
* |
|
6
|
|
|
* @package automattic/jetpack-sync |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Automattic\Jetpack\Sync; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* An implementation of Automattic\Jetpack\Sync\Codec_Interface that uses gzip's DEFLATE |
|
13
|
|
|
* algorithm to compress objects serialized using json_encode |
|
14
|
|
|
*/ |
|
15
|
|
|
class JSON_Deflate_Array_Codec implements Codec_Interface { |
|
16
|
|
|
const CODEC_NAME = 'deflate-json-array'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Return the name of the codec. |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public function name() { |
|
24
|
|
|
return self::CODEC_NAME; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Encodes an object. |
|
29
|
|
|
* |
|
30
|
|
|
* @param object $object Item to encode. |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function encode( $object ) { |
|
34
|
|
|
return base64_encode( gzdeflate( $this->json_serialize( $object ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Decode compressed serialized value. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $input Item to decode. |
|
41
|
|
|
* @return array|mixed|object |
|
42
|
|
|
*/ |
|
43
|
|
|
public function decode( $input ) { |
|
44
|
|
|
return $this->json_unserialize( gzinflate( base64_decode( $input ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Serialize JSON |
|
49
|
|
|
* |
|
50
|
|
|
* @see https://gist.github.com/muhqu/820694 |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $any Value to serialize and wrap. |
|
53
|
|
|
* |
|
54
|
|
|
* @return false|string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function json_serialize( $any ) { |
|
57
|
|
|
return wp_json_encode( Functions::json_wrap( $any ) ); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Unserialize JSON |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $str JSON string. |
|
64
|
|
|
* @return array|object Unwrapped JSON. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function json_unserialize( $str ) { |
|
67
|
|
|
return $this->json_unwrap( json_decode( $str, true ) ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Unwraps a json_decode return. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array|object $any json_decode object. |
|
74
|
|
|
* @return array|object |
|
75
|
|
|
*/ |
|
76
|
|
|
private function json_unwrap( $any ) { |
|
77
|
|
|
if ( is_array( $any ) ) { |
|
78
|
|
|
foreach ( $any as $k => $v ) { |
|
79
|
|
|
if ( '__o' === $k ) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
$any[ $k ] = $this->json_unwrap( $v ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ( isset( $any['__o'] ) ) { |
|
86
|
|
|
unset( $any['__o'] ); |
|
87
|
|
|
$any = (object) $any; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $any; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: