Completed
Push — add/sync-rest-2 ( 5c8420...066938 )
by
unknown
28:02 queued 18:44
created

Jetpack_Sync_Client::do_sync()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 11
nc 3
nop 0
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
10
	// this is necessary because you can't use "new" when you declare instance properties >:(
11
	function __construct() {
12
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
13
		$this->codec = new Jetpack_Sync_Deflate_Codec();
14
	}
15
16
	function init() {
17
		$handler = array( $this, 'action_handler' );
18
		// posts
19
		add_action( 'wp_insert_post', $handler, 10, 3 );
20
		add_action( 'delete_post', $handler, 10 );
21
		// comments
22
		add_action( 'wp_insert_comment', $handler, 10, 2 );
23
		add_action( 'deleted_comment', $handler, 10 );
24
		add_action( 'trashed_comment', $handler, 10 );
25
		add_action( 'spammed_comment', $handler, 10 );
26
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
27
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
28
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
29
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
30
			}
31
		}
32
33
		// options
34
		add_action( 'added_option', $handler, 10, 2 );
35
		add_action( 'updated_option', $handler, 10, 3 );
36
		add_action( 'deleted_option', $handler, 10, 1 );
37
38
		// themes
39
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
40
41
		/**
42
		 * Meta-hooks - fire synthetic hooks for all the properties we need to sync, 
43
		 * e.g. when a theme changes
44
		 */
45
46
		// themes
47
		add_action( 'switch_theme', array( $this, 'switch_theme_handler' ) );
48
	}
49
50
	function set_options_whitelist( $options ) {
51
		$this->options_whitelist = $options;
52
	}
53
54
	function is_whitelisted_option( $option ) {
55
		foreach ( $this->options_whitelist as $whitelisted_option ) {
56
			if ( $whitelisted_option[0] === '/' && preg_match( $whitelisted_option, $option ) ) {
57
				return true;
58
			} elseif ( $whitelisted_option === $option ) {
59
				return true;
60
			}
61
		}
62
		return false;
63
	}
64
65
	function set_codec( iJetpack_Sync_Codec $codec ) {
66
		$this->codec = $codec;
67
	}
68
69
	function action_handler() {
70
		$current_filter = current_filter();
71
		$args           = func_get_args();
72
73
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
74
			return;
75
		}
76
77
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) 
78
			&& 
79
			! $this->is_whitelisted_option( $args[0] ) ) {
80
			return;
81
		}
82
		
83
		Jetpack_Sync::schedule_sync();
84
		$this->sync_queue->add( array(
85
			$current_filter,
86
			$args
87
		) );
88
	}
89
90
	function switch_theme_handler() {
91
		global $_wp_theme_features;
92
		
93
		do_action( 'jetpack_sync_current_theme_support', $_wp_theme_features );
94
	}
95
96
	function do_sync() {
97
		$buffer = $this->sync_queue->checkout();
98
99
		if ( is_wp_error( $buffer) ) {
100
			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...
101
			return;
102
		}
103
104
		$data = $this->codec->encode( $buffer->items );
105
106
		/**
107
		 * Fires when data is ready to send to the server
108
		 *
109
		 * @since 4.1
110
		 *
111
		 * @param array $data The action buffer
112
		 */
113
		$result = apply_filters( 'jetpack_sync_client_send_data', $data );
114
115
		if ( !$result || is_wp_error( $result ) ) {
116
			$this->sync_queue->checkin( $buffer );
117
		} else {
118
			$this->sync_queue->close( $buffer );
119
		}
120
	}
121
122
	function get_actions() {
123
		// 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...
124
		return $this->sync_queue->flush_all();
125
	}
126
127
	function reset_actions() {
128
		$this->sync_queue->reset();
0 ignored issues
show
Bug introduced by
The method reset() does not seem to exist on object<Jetpack_Sync_Queue>.

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...
129
	}
130
}
131