Completed
Push — add/sync-rest-2 ( 3e8de3...589bfc )
by
unknown
09:25
created

Jetpack_Sync_Client::should_sync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
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 get_sync() {
50
		// return $this->sync_queue;
51
		$data = $this->codec->encode( $this->sync_queue );
52
53
		/**
54
		 * Fires when data is ready to send to the server
55
		 *
56
		 * @since 4.1
57
		 *
58
		 * @param array $data The action buffer
59
		 */
60
		return apply_filters( 'jetpack_sync_client_send_data', $data );
61
	}
62
63
	function get_actions() {
64
		return $this->sync_queue;
65
	}
66
67
	function should_sync( $sync, $current_filter, $args ) {
68
69
		switch ( $current_filter ) {
70
			case 'wp_insert_post':
71
				if ( $args[1]->post_type === 'revision' ) {
72
					return false;
73
				}
74
				break;
75
		}
76
		return $sync;
77
	}
78
}
79