PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.1
Jetpack – WP Security, Backup, Speed, & Growth v7.2.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sync / class.jetpack-sync-json-deflate-array-codec.php

class.jetpack-sync-json-deflate-array-codec.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.1, at sync/class.jetpack-sync-json-deflate-array-codec.php

85 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once dirname( __FILE__ ) . '/interface.jetpack-sync-codec.php';
4
5 /**
6 * An implementation of iJetpack_Sync_Codec that uses gzip's DEFLATE
7 * algorithm to compress objects serialized using json_encode
8 */
9 class Jetpack_Sync_JSON_Deflate_Array_Codec implements iJetpack_Sync_Codec {
10 const CODEC_NAME = 'deflate-json-array';
11
12 public function name() {
13 return self::CODEC_NAME;
14 }
15
16 public function encode( $object ) {
17 return base64_encode( gzdeflate( $this->json_serialize( $object ) ) );
18 }
19
20 public function decode( $input ) {
21 return $this->json_unserialize( gzinflate( base64_decode( $input ) ) );
22 }
23
24 // @see https://gist.github.com/muhqu/820694
25 protected function json_serialize( $any ) {
26 if ( function_exists( 'jetpack_json_wrap' ) ) {
27 return wp_json_encode( jetpack_json_wrap( $any ) );
28 }
29 // This prevents fatal error when updating pre 6.0 via the cli command
30 return wp_json_encode( $this->json_wrap( $any ) );
31 }
32
33 protected function json_unserialize( $str ) {
34 return $this->json_unwrap( json_decode( $str, true ) );
35 }
36
37 private function json_wrap( &$any, $seen_nodes = array() ) {
38 if ( is_object( $any ) ) {
39 $input = get_object_vars( $any );
40 $input['__o'] = 1;
41 } else {
42 $input = &$any;
43 }
44
45 if ( is_array( $input ) ) {
46 $seen_nodes[] = &$any;
47
48 $return = array();
49
50 foreach ( $input as $k => &$v ) {
51 if ( ( is_array( $v ) || is_object( $v ) ) ) {
52 if ( in_array( $v, $seen_nodes, true ) ) {
53 continue;
54 }
55 $return[ $k ] = $this->json_wrap( $v, $seen_nodes );
56 } else {
57 $return[ $k ] = $v;
58 }
59 }
60
61 return $return;
62 }
63
64 return $any;
65 }
66
67 private function json_unwrap( $any ) {
68 if ( is_array( $any ) ) {
69 foreach ( $any as $k => $v ) {
70 if ( '__o' === $k ) {
71 continue;
72 }
73 $any[ $k ] = $this->json_unwrap( $v );
74 }
75
76 if ( isset( $any['__o'] ) ) {
77 unset( $any['__o'] );
78 $any = (object) $any;
79 }
80 }
81
82 return $any;
83 }
84 }
85