Completed
Push — add/sync-rest-2 ( 589bfc...4837ac )
by
unknown
08:48
created

Jetpack_Sync_Client::do_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
require_once dirname(__FILE__) . '/class.jetpack-sync-deflate-codec.php';
3
4
class Jetpack_Sync_Client {
5
	public $sync_queue = array();
6
	private $codec;
7
	// this is necessary because you can't use "new" when you declare instance properties >:(
8
	function __construct() {
9
		$this->codec = new Jetpack_Sync_Deflate_Codec();
10
	}
11
12
	function init() {
13
		$handler = array( $this, 'action_handler' );
14
		// posts
15
		add_action( 'wp_insert_post', $handler, 10, 3 );
16
		add_action( 'delete_post', $handler, 10 );
17
		// comments
18
		add_action( 'wp_insert_comment', $handler, 10, 2 );
19
		add_action( 'deleted_comment', $handler, 10 );
20
		add_action( 'trashed_comment', $handler, 10 );
21
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
22
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
23
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
24
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
25
			}
26
		}
27
28
		add_filter( 'jetpack_sync_client_add_data_to_sync', array( $this, 'should_sync' ), 10, 3);
29
30
	}
31
32
	function set_codec( iJetpack_Sync_Codec $codec ) {
33
		$this->codec = $codec;
34
	}
35
36
	function action_handler() {
37
		Jetpack_Sync::schedule_sync();
38
		$current_filter     = current_filter();
39
		$args               = func_get_args();
40
41
		if ( apply_filters( 'jetpack_sync_client_add_data_to_sync', true, $current_filter, $args ) ) {
42
			$this->sync_queue[] = array(
43
				$current_filter,
44
				$args
45
			);
46
		}
47
	}
48
49
	function do_sync() {
50
		$data = $this->codec->encode( $this->sync_queue );
51
52
		/**
53
		 * Fires when data is ready to send to the server
54
		 *
55
		 * @since 4.1
56
		 *
57
		 * @param array $data The action buffer
58
		 */
59
		apply_filters( 'jetpack_sync_client_send_data', $data );
60
	}
61
62
	function get_actions() {
63
		return $this->sync_queue;
64
	}
65
66
	function should_sync( $sync, $current_filter, $args ) {
67
68
		switch ( $current_filter ) {
69
			case 'wp_insert_post':
70
				if ( $args[1]->post_type === 'revision' ) {
71
					return false;
72
				}
73
				break;
74
		}
75
		return $sync;
76
	}
77
}
78