Completed
Push — add/sync-on-plugin-upgrade ( 72e355...a07eed )
by
unknown
313:16 queued 303:44
created

Jetpack_Sync_Actions::schedule_initial_sync()   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
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions,
5
 * when to send, when to perform a full sync, etc.
6
 *
7
 * It also binds the action to send data to WPCOM to Jetpack's XMLRPC client object.
8
 */
9
class Jetpack_Sync_Actions {
10
	static $sender = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sender.

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...
11
	static $listener = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $listener.

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...
12
13
	static function init() {
14
15
		// On jetpack authorization, schedule a full sync
16
		add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ) );
17
		add_action( 'updating_jetpack_version', array( __CLASS__, 'schedule_initial_sync' ) );
18
		
19
		// Sync connected user role changes to .com
20
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php';
21
22
		// everything below this point should only happen if we're a valid sync site
23
		if ( ! self::sync_allowed() ) {
24
			return;
25
		}
26
27
		// cron hooks
28
		add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) );
29
		add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 );
30
		add_action( 'jetpack_sync_send_pending_data', array( __CLASS__, 'do_send_pending_data' ) );
31
32
		if ( ! wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) {
33
			// Schedule a job to send DB checksums once an hour
34
			wp_schedule_event( time(), 'hourly', 'jetpack_sync_send_db_checksum' );
35
		}
36
37
		/**
38
		 * Fires on every request before default loading sync listener code.
39
		 * Return false to not load sync listener code that monitors common
40
		 * WP actions to be serialized.
41
		 *
42
		 * By default this returns true for non-GET-requests, or requests where the
43
		 * user is logged-in.
44
		 *
45
		 * @since 4.2.0
46
		 *
47
		 * @param bool should we load sync listener code for this request
48
		 */
49
		if ( apply_filters( 'jetpack_sync_listener_should_load',
50
			(
51
				'GET' !== $_SERVER['REQUEST_METHOD']
52
				||
53
				is_user_logged_in()
54
				||
55
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
56
			)
57
		) ) {
58
			self::initialize_listener();
59
		}
60
61
		/**
62
		 * Fires on every request before default loading sync sender code.
63
		 * Return false to not load sync sender code that serializes pending
64
		 * data and sends it to WPCOM for processing.
65
		 *
66
		 * By default this returns true for POST requests, admin requests, or requests
67
		 * by users who can manage_options.
68
		 *
69
		 * @since 4.2.0
70
		 *
71
		 * @param bool should we load sync sender code for this request
72
		 */
73
		if ( apply_filters( 'jetpack_sync_sender_should_load',
74
			(
75
				'POST' === $_SERVER['REQUEST_METHOD']
76
				||
77
				current_user_can( 'manage_options' )
78
				||
79
				is_admin()
80
				||
81
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
82
			)
83
		) ) {
84
			self::initialize_sender();
85
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
86
		}
87
88
	}
89
90
	static function sync_allowed() {
91
		return ( Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) )
92
		       || defined( 'PHPUNIT_JETPACK_TESTSUITE' );
93
	}
94
95
	static function send_data( $data, $codec_name, $sent_timestamp ) {
96
		Jetpack::load_xml_rpc_client();
97
98
		$url = add_query_arg( array(
99
			'sync'      => '1', // add an extra parameter to the URL so we can tell it's a sync action
100
			'codec'     => $codec_name, // send the name of the codec used to encode the data
101
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
102
		), Jetpack::xmlrpc_api_url() );
103
104
		$rpc = new Jetpack_IXR_Client( array(
105
			'url'     => $url,
106
			'user_id' => JETPACK_MASTER_USER,
107
			'timeout' => 30,
108
		) );
109
110
		$result = $rpc->query( 'jetpack.syncActions', $data );
111
112
		if ( ! $result ) {
113
			return $rpc->get_jetpack_error();
114
		}
115
116
		return $rpc->getResponse();
117
	}
118
119
	static function schedule_initial_sync() {
120
		self::schedule_full_sync( array( 'options', 'network_options', 'functions', 'constants' ) );
121
	}
122
123
	static function schedule_full_sync( $modules = null ) {
124
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full', array( $modules ) );
125
		spawn_cron();
126
	}
127
128
	static function do_full_sync( $modules = null ) {
129
		if ( ! self::sync_allowed() ) {
130
			return;
131
		}
132
133
		self::initialize_listener();
134
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules );
135
		self::do_send_pending_data(); // try to send at least some of the data
136
	}
137
138
	static function do_send_pending_data() {
139
		self::initialize_sender();
140
		self::$sender->do_sync();
141
	}
142
143
	static function send_db_checksum() {
144
		self::initialize_listener();
145
		self::initialize_sender();
146
		self::$sender->send_checksum();
147
		self::$sender->do_sync();
148
	}
149
150
	static function initialize_listener() {
151
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
152
		self::$listener = Jetpack_Sync_Listener::get_instance();
153
	}
154
155
	static function initialize_sender() {
156
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
157
		self::$sender = Jetpack_Sync_Sender::get_instance();
158
159
		// bind the sending process
160
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 3 );
161
	}
162
}
163
164
// Allow other plugins to add filters before we initialize the actions.
165
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
166