Completed
Push — add/sync-rest-2 ( 4837ac...19933d )
by
unknown
09:55
created

Jetpack_Sync_Client::reset_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
require_once dirname(__FILE__) . '/class.jetpack-sync-deflate-codec.php';
3
4
class Jetpack_Sync_Client {
5
	private $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
	}
29
30
	function set_codec( iJetpack_Sync_Codec $codec ) {
31
		$this->codec = $codec;
32
	}
33
34
	function action_handler() {
35
		$current_filter     = current_filter();
36
		$args               = func_get_args();
37
38
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
39
			return;
40
		}
41
		
42
		Jetpack_Sync::schedule_sync();
43
		$this->sync_queue[] = array(
44
			$current_filter,
45
			apply_filters( 'jetpack_sync_client_add_data_to_sync', $args, $current_filter )
46
		);
47
48
	}
49
50
	function do_sync() {
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
		apply_filters( 'jetpack_sync_client_send_data', $data );
61
	}
62
63
	function get_actions() {
64
		return $this->sync_queue;
65
	}
66
67
	function reset_actions() {
68
		$this->sync_queue = array();
69
	}
70
}
71