Completed
Push — add/sync-rest-2 ( 7deea7...41eee2 )
by
unknown
09:44
created

Jetpack_Sync_Client::action_handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
class Jetpack_Sync_Client {
4
	private $sync_queue = array();
5
	private $codec;
6
	// this is necessary because you can't use "new" when you declare instance properties >:(
7
	function __construct() {
8
		$this->codec = new Jetpack_Sync_Deflate_Codec();
9
	}
10
11
	function init() {
12
		$handler = array( $this, 'action_handler' );
13
		// posts
14
		add_action( 'wp_insert_post', $handler, 10, 3 );
15
		add_action( 'delete_post', $handler, 10 );
16
		// comments
17
		add_action( 'wp_insert_comment', $handler, 10, 2 );
18
		add_action( 'deleted_comment', $handler, 10 );
19
		add_action( 'trashed_comment', $handler, 10 );
20
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
21
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
22
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
23
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
24
			}
25
		}
26
	}
27
28
	function set_codec( iJetpack_Sync_Codec $codec ) {
29
		$this->codec = $codec;
30
	}
31
32
	function action_handler() {
33
		$current_filter     = current_filter();
34
		$args               = func_get_args();
35
		$this->sync_queue[] = array(
36
			$current_filter,
37
			$args
38
		);
39
	}
40
41
	function do_sync() {
42
		$data = $this->codec->encode( $this->sync_queue );
43
44
		/**
45
		 * Fires when data is ready to send to the server
46
		 *
47
		 * @since 4.1
48
		 *
49
		 * @param array $data The action buffer
50
		 */
51
		do_action( 'jetpack_sync_client_send_data', $data );
52
	}
53
54
55
}
56
57
/**
58
 * Very simple interface for encoding and decoding input
59
 * This is used to provide compression and serialization to messages
60
 **/
61
interface iJetpack_Sync_Codec {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
62
	public function encode( $object );
63
	public function decode( $input );
64
}
65
/**
66
 * An implementation of iJetpack_Sync_Codec that uses gzip's DEFLATE
67
 * algorithm to compress objects serialized using PHP's default
68
 * serializer
69
 */
70
class Jetpack_Sync_Deflate_Codec implements iJetpack_Sync_Codec {
0 ignored issues
show
Coding Style Compatibility introduced by
Each interface must be in a file by itself

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
71
	public function encode( $object ) {
72
		return gzdeflate( serialize( $object ) );
73
	}
74
	public function decode( $input ) {
75
		return unserialize( gzinflate( $input ) );
76
	}
77
}
78