← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-json-deflate-array-codec.php
+100
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,100 @@ | ||
| 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 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit( 0 ); | |
| 13 | +} | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * An implementation of Automattic\Jetpack\Sync\Codec_Interface that uses gzip's DEFLATE | |
| 17 | + * algorithm to compress objects serialized using json_encode | |
| 18 | + */ | |
| 19 | +class JSON_Deflate_Array_Codec implements Codec_Interface { | |
| 20 | + const CODEC_NAME = 'deflate-json-array'; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Return the name of the codec. | |
| 24 | + * | |
| 25 | + * @return string | |
| 26 | + */ | |
| 27 | + public function name() { | |
| 28 | + return self::CODEC_NAME; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Encodes an object. | |
| 33 | + * | |
| 34 | + * @param object $object Item to encode. | |
| 35 | + * @return string | |
| 36 | + */ | |
| 37 | + public function encode( $object ) { | |
| 38 | + return base64_encode( gzdeflate( $this->json_serialize( $object ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Decode compressed serialized value. | |
| 43 | + * | |
| 44 | + * @param string $input Item to decode. | |
| 45 | + * @return array|mixed|object | |
| 46 | + */ | |
| 47 | + public function decode( $input ) { | |
| 48 | + $decoded = base64_decode( $input ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode | |
| 49 | + $inflated = @gzinflate( $decoded ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 50 | + | |
| 51 | + return is_string( $inflated ) ? $this->json_unserialize( $inflated ) : null; | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Serialize JSON | |
| 56 | + * | |
| 57 | + * @see https://gist.github.com/muhqu/820694 | |
| 58 | + * | |
| 59 | + * @param mixed $any Value to serialize and wrap. | |
| 60 | + * | |
| 61 | + * @return false|string | |
| 62 | + */ | |
| 63 | + protected function json_serialize( $any ) { | |
| 64 | + return wp_json_encode( Functions::json_wrap( $any ), JSON_UNESCAPED_SLASHES ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Unserialize JSON | |
| 69 | + * | |
| 70 | + * @param string $str JSON string. | |
| 71 | + * @return array|object Unwrapped JSON. | |
| 72 | + */ | |
| 73 | + protected function json_unserialize( $str ) { | |
| 74 | + return $this->json_unwrap( json_decode( $str, true ) ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Unwraps a json_decode return. | |
| 79 | + * | |
| 80 | + * @param array|object $any json_decode object. | |
| 81 | + * @return array|object | |
| 82 | + */ | |
| 83 | + private function json_unwrap( $any ) { | |
| 84 | + if ( is_array( $any ) ) { | |
| 85 | + foreach ( $any as $k => $v ) { | |
| 86 | + if ( '__o' === $k ) { | |
| 87 | + continue; | |
| 88 | + } | |
| 89 | + $any[ $k ] = $this->json_unwrap( $v ); | |
| 90 | + } | |
| 91 | + | |
| 92 | + if ( isset( $any['__o'] ) ) { | |
| 93 | + unset( $any['__o'] ); | |
| 94 | + $any = (object) $any; | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $any; | |
| 99 | + } | |
| 100 | +} | |