Completed
Push — add/sync-rest-2 ( fa68b0...12974f )
by
unknown
09:23
created

Jetpack_Sync_Client::get_check_sum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php';
4
5
class Jetpack_Sync_Client {
6
	private $sync_queue;
7
	private $codec;
8
	private $options_whitelist = array( 'stylesheet', '/^theme_mods_.*$/' );
9
	private $constants_whitelist = array();
10
	private $meta_types = array( 'post' );
11
	private $previous_filter = array();
12
13
14
	// this is necessary because you can't use "new" when you declare instance properties >:(
15
	function __construct() {
16
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
17
		$this->codec = new Jetpack_Sync_Deflate_Codec();
18
	}
19
20
	function init() {
21
22
		$handler = array( $this, 'action_handler' );
23
		$filter_handler = array( $this, 'filter_handler' );
24
		// posts
25
		add_action( 'wp_insert_post', $handler, 10, 3 );
26
		add_action( 'delete_post', $handler, 10 );
27
		// comments
28
		add_action( 'wp_insert_comment', $handler, 10, 2 );
29
		add_action( 'deleted_comment', $handler, 10 );
30
		add_action( 'trashed_comment', $handler, 10 );
31
		add_action( 'spammed_comment', $handler, 10 );
32
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
33
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
34
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
35
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
36
			}
37
		}
38
39
		// options
40
		add_action( 'added_option', $handler, 10, 2 );
41
		add_action( 'updated_option', $handler, 10, 3 );
42
		add_action( 'deleted_option', $handler, 10, 1 );
43
44
		// themes
45
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
46
		add_action( 'jetpack_sync_current_constants', $handler, 10 );
47
48
		/**
49
		 * Meta-hooks - fire synthetic hooks for all the properties we need to sync, 
50
		 * e.g. when a theme changes
51
		 */
52
53
		// themes
54
		add_action( 'switch_theme', array( $this, 'switch_theme_handler' ) );
55
56
		foreach ( $this->meta_types as $meta_type ) {
57
			add_filter( "add_{$meta_type}_metadata", $filter_handler, 99, 5 );
58
			add_filter( "update_{$meta_type}_metadata", $filter_handler, 99, 5);
59
			add_filter( "delete_{$meta_type}_metadata", $filter_handler, 99, 5 );
60
		}
61
	}
62
63
	function set_options_whitelist( $options ) {
64
		$this->options_whitelist = $options;
65
	}
66
67
	function set_constant_whitelist( $constant ) {
68
		$this->constants_whitelist = $constant;
69
	}
70
71
	function is_whitelisted_option( $option ) {
72
		foreach ( $this->options_whitelist as $whitelisted_option ) {
73
			if ( $whitelisted_option[0] === '/' && preg_match( $whitelisted_option, $option ) ) {
74
				return true;
75
			} elseif ( $whitelisted_option === $option ) {
76
				return true;
77
			}
78
		}
79
		return false;
80
	}
81
82
	function set_codec( iJetpack_Sync_Codec $codec ) {
83
		$this->codec = $codec;
84
	}
85
86
	function set_sync_queue( $queue ) {
87
		$this->sync_queue = $queue;
88
	}
89
90
	function action_handler() {
91
		$current_filter = current_filter();
92
		$args           = func_get_args();
93
94
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
95
			return;
96
		}
97
98
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) 
99
			&& 
100
			! $this->is_whitelisted_option( $args[0] ) ) {
101
			return;
102
		}
103
		Jetpack_Sync::schedule_sync();
104
		$this->sync_queue->add( array(
105
			$current_filter,
106
			$args
107
		) );
108
	}
109
110
	function filter_handler() {
111
		$current_filter = current_filter();
112
		$args           = func_get_args();
113
		$necessary_args = array( $args[1], $args[2], $args[3] );
114
		$return = $args[0];
115
		foreach ( $this->meta_types as $meta_type ) {
116
			if ( $args[0] !== null && ( $current_filter === "add_{$meta_type}_metadata" || $current_filter === "update_{$meta_type}_metadata" || $current_filter === "delete_{$meta_type}_metadata" ) ) {
117
				return $return;
118
			}
119
120
			if ( $current_filter === "add_{$meta_type}_metadata" && serialize( $necessary_args ) === serialize( $this->previous_filter ) ) {
121
				return $return;
122
			}
123
		}
124
125
		Jetpack_Sync::schedule_sync();
126
		$this->sync_queue->add( array(
127
			$current_filter,
128
			$args
129
		) );
130
131
		$this->previous_filter = $necessary_args;
132
133
		return $return;
134
135
	}
136
137
	function switch_theme_handler() {
138
		global $_wp_theme_features;
139
		
140
		do_action( 'jetpack_sync_current_theme_support', $_wp_theme_features );
141
	}
142
143
	function do_sync() {
144
		$this->maybe_sync_constants();
145
		$buffer = $this->sync_queue->checkout();
146
147
		if ( is_wp_error( $buffer) ) {
148
			error_log( "Got error: ".$buffer->get_error_message() );
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<Jetpack_Sync_Queue_Buffer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
			return;
150
		}
151
152
		$data = $this->codec->encode( $buffer->items );
153
154
		/**
155
		 * Fires when data is ready to send to the server
156
		 *
157
		 * @since 4.1
158
		 *
159
		 * @param array $data The action buffer
160
		 */
161
		$result = apply_filters( 'jetpack_sync_client_send_data', $data );
162
163
		if ( !$result || is_wp_error( $result ) ) {
164
			$this->sync_queue->checkin( $buffer );
165
		} else {
166
			$this->sync_queue->close( $buffer );
167
		}
168
	}
169
	
170
	private function maybe_sync_constants() {
171
		$constants = $this->get_all_constants();
172
		$constants_check_sum = $this->get_check_sum( $constants );
173
		$check_sum_option = 'jetpack_constants_sync_checksum';
174
		if ( $constants_check_sum !== get_option( $check_sum_option ) ) {
175
			do_action( 'jetpack_sync_current_constants', $constants );
176
			update_option( $check_sum_option, $constants_check_sum );
177
		}
178
	}
179
180
	private function get_all_constants() {
181
		return array_combine( $this->constants_whitelist, array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) );
182
	}
183
184
	private function get_constant( $constant ) {
185
		if ( defined( $constant ) ) {
186
			return constant( $constant );
187
		}
188
189
		return null;
190
	}
191
192
	private function get_check_sum( $values ) {
193
		return crc32( json_encode( $values ) );
194
	}
195
196
197
	function get_actions() {
198
		// TODO: we should only send a bit at a time, flush_all sends everything
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
199
		return $this->sync_queue->flush_all();
200
	}
201
202
	function get_all_actions() {
203
		return $this->sync_queue->get_all();
204
	}
205
206
	function reset_actions() {
207
		$this->sync_queue->reset();
208
	}
209
}
210