Completed
Push — branch-4.2 ( 75fbec...061803 )
by
unknown
294:23 queued 285:31
created

Jetpack_Sync_Actions::schedule_full_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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