Completed
Push — add/sync-rest-2 ( b696cb...bb3b5b )
by
unknown
09:27 queued 52s
created

Jetpack_Sync_Client::set_sync_queue()   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
	static $default_options_whitelist = array( 'stylesheet', '/^theme_mods_.*$/' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $default_options_whitelist.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

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