blob: 6ec966e985622e36265896fef80a64cd39279d31 (
plain)
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
|
<?php
require_once dirname( __FILE__ ) . '/interface.jetpack-sync-codec.php';
/**
* An implementation of iJetpack_Sync_Codec that uses gzip's DEFLATE
* algorithm to compress objects serialized using json_encode
*/
class Jetpack_Sync_JSON_Deflate_Codec implements iJetpack_Sync_Codec {
const CODEC_NAME = 'deflate-json';
public function name() {
return self::CODEC_NAME;
}
public function encode( $object ) {
return base64_encode( gzdeflate( $this->json_serialize( unserialize( serialize( $object ) ) ) ) );
}
public function decode( $input ) {
return $this->json_unserialize( gzinflate( base64_decode( $input ) ) );
}
// @see https://gist.github.com/muhqu/820694
private function json_serialize( $any ) {
return json_encode( $this->json_wrap( $any ) );
}
private function json_unserialize( $str ) {
return $this->json_unwrap( json_decode( $str ) );
}
private function json_wrap( $any, $skip_assoc = false ) {
if ( ! $skip_assoc && is_array( $any ) && is_string( key( $any ) ) ) {
return (object) array( '_PHP_ASSOC' => $this->json_wrap( $any, true ) );
}
if ( is_array( $any ) || is_object( $any ) ) {
foreach ( $any as &$v ) {
$v = $this->json_wrap( $v );
}
}
return $any;
}
private function json_unwrap( $any, $skip_assoc = false ) {
if ( ! $skip_assoc && is_object( $any ) && isset( $any->_PHP_ASSOC ) && count( (array) $any ) == 1 ) {
return (array) $this->json_unwrap( $any->_PHP_ASSOC );
}
if ( is_array( $any ) || is_object( $any ) ) {
foreach ( $any as &$v ) {
$v = $this->json_unwrap( $v );
}
}
return $any;
}
}
|