Completed
Push — add/sync-rest-2 ( 563c8b...120bfd )
by
unknown
10:08
created

Jetpack_Sync_Client::set_options_whitelist()   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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
	private $options_whitelist = array();
8
	private $options_actions = array( 'deleted_options', 'added_option', 'updated_option' );
9
10
	// this is necessary because you can't use "new" when you declare instance properties >:(
11
	function __construct() {
12
		$this->codec = new Jetpack_Sync_Deflate_Codec();
13
	}
14
15
	function init() {
16
		$handler = array( $this, 'action_handler' );
17
		// posts
18
		add_action( 'wp_insert_post', $handler, 10, 3 );
19
		add_action( 'delete_post', $handler, 10 );
20
		// comments
21
		add_action( 'wp_insert_comment', $handler, 10, 2 );
22
		add_action( 'deleted_comment', $handler, 10 );
23
		add_action( 'trashed_comment', $handler, 10 );
24
		add_action( 'spammed_comment', $handler, 10 );
25
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
26
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
27
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
28
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
29
			}
30
		}
31
32
		// options
33
		add_action( 'added_option', $handler, 10, 2 );
34
		add_action( 'updated_option', $handler, 10, 3 );
35
		add_action( 'deleted_option', $handler, 10, 1 );
36
	}
37
38
	function set_options_whitelist( $options ) {
39
		$this->options_whitelist = $options;
40
	}
41
42
	function set_codec( iJetpack_Sync_Codec $codec ) {
43
		$this->codec = $codec;
44
	}
45
46
	function action_handler() {
47
		$current_filter = current_filter();
48
		$args           = func_get_args();
49
50
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
51
			return;
52
		}
53
54
		if ( in_array( $current_filter, $this->options_actions ) && ! in_array($args[0], $this->options_whitelist ) ) {
55
			return;
56
		}
57
		
58
		Jetpack_Sync::schedule_sync();
59
		$this->sync_queue[] = array(
60
			$current_filter,
61
			apply_filters( 'jetpack_sync_client_add_data_to_sync', $args, $current_filter )
62
		);
63
64
	}
65
66
	function do_sync() {
67
		$data = $this->codec->encode( $this->sync_queue );
68
69
		/**
70
		 * Fires when data is ready to send to the server
71
		 *
72
		 * @since 4.1
73
		 *
74
		 * @param array $data The action buffer
75
		 */
76
		apply_filters( 'jetpack_sync_client_send_data', $data );
77
	}
78
79
	function get_actions() {
80
		return $this->sync_queue;
81
	}
82
83
	function reset_actions() {
84
		$this->sync_queue = array();
85
	}
86
}
87