Completed
Push — add/sync-rest-2 ( 28e1b3...b696cb )
by
unknown
09:04
created

Jetpack_Sync_Client   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 15
Bugs 1 Features 2
Metric Value
wmc 36
c 15
b 1
f 2
lcom 3
cbo 4
dl 0
loc 213
rs 8.8

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 2
A __construct() 0 5 1
B init() 0 48 4
A set_options_whitelist() 0 3 1
A set_constant_whitelist() 0 3 1
B is_whitelisted_option() 0 10 5
A set_codec() 0 3 1
A set_sync_queue() 0 3 1
B action_handler() 0 19 5
A switch_theme_handler() 0 5 1
B do_sync() 0 42 5
A maybe_sync_constants() 0 9 2
A get_all_constants() 0 6 1
A get_constant() 0 7 2
A get_check_sum() 0 3 1
A get_actions() 0 4 1
A get_all_actions() 0 3 1
A reset_state() 0 3 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
12
	// singleton functions
13
	private static $instance;
14
15
	public static function getInstance() {
16
		if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
17
			static::$instance = new static();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
18
		}
19
		
20
		return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
21
	}
22
23
	// this is necessary because you can't use "new" when you declare instance properties >:(
24
	protected function __construct() {
25
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync', 10000 );
26
		$this->codec = new Jetpack_Sync_Deflate_Codec();
27
		$this->init();
28
	}
29
30
	private function init() {
31
		$handler = array( $this, 'action_handler' );
32
33
		// posts
34
		add_action( 'wp_insert_post', $handler, 10, 3 );
35
		add_action( 'deleted_post', $handler, 10 );
36
37
		// comments
38
		add_action( 'wp_insert_comment', $handler, 10, 2 );
39
		add_action( 'deleted_comment', $handler, 10 );
40
		add_action( 'trashed_comment', $handler, 10 );
41
		add_action( 'spammed_comment', $handler, 10 );
42
43
		// even though it's messy, we implement these hooks because 
44
		// the edit_comment hook doesn't include the data
45
		// so this saves us a DB read for every comment event
46
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
47
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
48
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
49
			}
50
		}
51
52
		// options
53
		add_action( 'added_option', $handler, 10, 2 );
54
		add_action( 'updated_option', $handler, 10, 3 );
55
		add_action( 'deleted_option', $handler, 10, 1 );
56
57
		// themes
58
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
59
		// add_action( 'jetpack_sync_current_constants', $handler, 10 );
60
61
		// post-meta, and in the future - other meta?
62
		foreach ( $this->meta_types as $meta_type ) {
63
			// we need to make sure we don't commit before we receive these,
64
			// because they're invoked after meta changes are saved to the DB
65
			add_action( "added_{$meta_type}_meta", $handler, 99, 4 );
66
			add_action( "updated_{$meta_type}_meta", $handler, 99, 4 );
67
			add_action( "deleted_{$meta_type}_meta", $handler, 99, 4 );
68
		}
69
70
		/**
71
		 * Meta-hooks - fire synthetic hooks for all the properties we need to sync, 
72
		 * e.g. when a theme changes
73
		 */
74
75
		// themes
76
		add_action( 'switch_theme', array( $this, 'switch_theme_handler' ) );
77
	}
78
79
	function set_options_whitelist( $options ) {
80
		$this->options_whitelist = $options;
81
	}
82
83
	function set_constant_whitelist( $constant ) {
84
		$this->constants_whitelist = $constant;
85
	}
86
87
	function is_whitelisted_option( $option ) {
88
		foreach ( $this->options_whitelist as $whitelisted_option ) {
89
			if ( $whitelisted_option[0] === '/' && preg_match( $whitelisted_option, $option ) ) {
90
				return true;
91
			} elseif ( $whitelisted_option === $option ) {
92
				return true;
93
			}
94
		}
95
		return false;
96
	}
97
98
	function set_codec( iJetpack_Sync_Codec $codec ) {
99
		$this->codec = $codec;
100
	}
101
102
	function set_sync_queue( $queue ) {
103
		$this->sync_queue = $queue;
104
	}
105
106
	function action_handler() {
107
		$current_filter = current_filter();
108
		$args           = func_get_args();
109
110
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
111
			return;
112
		}
113
114
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) 
115
			&& 
116
			! $this->is_whitelisted_option( $args[0] ) ) {
117
			return;
118
		}
119
		Jetpack_Sync::schedule_sync();
120
		$this->sync_queue->add( array(
121
			$current_filter,
122
			$args
123
		) );
124
	}
125
126
	function switch_theme_handler() {
127
		global $_wp_theme_features;
128
		
129
		do_action( 'jetpack_sync_current_theme_support', $_wp_theme_features );
130
	}
131
132
	function do_sync() {
133
		$this->maybe_sync_constants();
134
		// TODO: only send buffer once, then do the rest in a cron job
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...
135
		// $iters = 0;
136
		$buffer = $this->sync_queue->checkout();
137
		// while ( ($buffer = $this->sync_queue->checkout()) && $iters < 100 ) {
138
139
			if ( !$buffer ) {
140
				// buffer has no items
141
				return;
142
			}
143
144
			if ( is_wp_error( $buffer) ) {
145
				error_log("Error fetching buffer: ".$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...
146
				return;
147
			}
148
149
			// error_log(print_r($buffer->get_items(), true));
150
151
			$data = $this->codec->encode( $buffer->get_items() );
152
153
			/**
154
			 * Fires when data is ready to send to the server.
155
			 * Return false or WP_Error to abort the sync (e.g. if there's an error)
156
			 * The items will be automatically re-sent later
157
			 *
158
			 * @since 4.1
159
			 *
160
			 * @param array $data The action buffer
161
			 */
162
			$result = apply_filters( 'jetpack_sync_client_send_data', $data );
163
164
			if ( !$result || is_wp_error( $result ) ) {
165
				$this->sync_queue->checkin( $buffer );
166
			} else {
167
				$this->sync_queue->close( $buffer );
168
			}
169
			// $iters += 1;
170
		// }
171
172
		// $this->sync_queue->checkin( $buffer );
173
	}
174
	
175
	private function maybe_sync_constants() {
176
		$constants = $this->get_all_constants();
177
		$constants_check_sum = $this->get_check_sum( $constants );
178
		$check_sum_option = 'jetpack_constants_sync_checksum';
179
		if ( $constants_check_sum !== get_option( $check_sum_option ) ) {
180
			do_action( 'jetpack_sync_current_constants', $constants );
181
			update_option( $check_sum_option, $constants_check_sum );
182
		}
183
	}
184
185
	private function get_all_constants() {
186
		return 	array_combine( 
187
					$this->constants_whitelist, 
188
					array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) 
189
				);
190
	}
191
192
	private function get_constant( $constant ) {
193
		if ( defined( $constant ) ) {
194
			return constant( $constant );
195
		}
196
197
		return null;
198
	}
199
200
	private function get_check_sum( $values ) {
201
		return crc32( json_encode( $values ) );
202
	}
203
204
205
	function get_actions() {
206
		// 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...
207
		return $this->sync_queue->flush_all();
208
	}
209
210
	function get_all_actions() {
211
		return $this->sync_queue->get_all();
212
	}
213
214
	function reset_state() {
215
		$this->sync_queue->reset();
216
	}
217
}
218