Jetpack_Sync_JSON_Deflate_Array_Codec

An implementation of iJetpack_Sync_Codec that uses gzip's DEFLATE algorithm to compress objects serialized using json_encode.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-json-deflate-array-codec.php  
  1. class Jetpack_Sync_JSON_Deflate_Array_Codec implements iJetpack_Sync_Codec { 
  2. const CODEC_NAME = "deflate-json-array"; 
  3.  
  4. public function name() { 
  5. return self::CODEC_NAME; 
  6.  
  7. public function encode( $object ) { 
  8. return base64_encode( gzdeflate( $this->json_serialize( $object ) ) ); 
  9.  
  10. public function decode( $input ) { 
  11. return $this->json_unserialize( gzinflate( base64_decode( $input ) ) ); 
  12.  
  13. // @see https://gist.github.com/muhqu/820694 
  14. private function json_serialize( $any ) { 
  15. return wp_json_encode( $this->json_wrap( $any ) ); 
  16.  
  17. private function json_unserialize( $str ) { 
  18. return $this->json_unwrap( json_decode( $str, true ) ); 
  19.  
  20. private function json_wrap( &$any, $seen_nodes = array() ) { 
  21. if ( is_object( $any ) ) { 
  22. $input = get_object_vars( $any ); 
  23. $input['__o'] = 1; 
  24. } else { 
  25. $input = &$any; 
  26.  
  27. if ( is_array( $input ) ) { 
  28. $seen_nodes[] = &$any; 
  29.  
  30. $return = array(); 
  31.  
  32. foreach ( $input as $k => &$v ) { 
  33. if ( ( is_array( $v ) || is_object( $v ) ) ) { 
  34. if ( in_array( $v, $seen_nodes, true ) ) { 
  35. continue; 
  36. }  
  37. $return[ $k ] = $this->json_wrap( $v, $seen_nodes ); 
  38. } else { 
  39. $return[ $k ] = $v;  
  40.  
  41. return $return; 
  42.  
  43. return $any; 
  44.  
  45. private function json_unwrap( $any ) { 
  46. if ( is_array( $any ) ) { 
  47. foreach ( $any as $k => $v ) { 
  48. if ( '__o' === $k ) { 
  49. continue; 
  50. $any[ $k ] = $this->json_unwrap( $v ); 
  51.  
  52. if ( isset( $any['__o'] ) ) { 
  53. unset( $any['__o'] ); 
  54. $any = (object) $any; 
  55.  
  56. return $any;