Completed
Push — update/non-admin-view ( b1ee9d...762240 )
by
unknown
10:19
created

Jetpack_Sync_Actions::sync_allowed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 4
rs 9.2
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' ) );
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
					$_SERVER['REQUEST_METHOD'] !== 'GET'
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
				$_SERVER['REQUEST_METHOD'] === 'POST'
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_full_sync() {
119
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
120
	}
121
122
	static function do_full_sync() {
123
		if ( ! self::sync_allowed() ) {
124
			return;
125
		}
126
127
		self::initialize_listener();
128
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start();
129
		self::do_send_pending_data(); // try to send at least some of the data
130
	}
131
132
	static function do_send_pending_data() {
133
		self::initialize_sender();
134
		self::$sender->do_sync();
135
	}
136
137
	static function send_db_checksum() {
138
		self::initialize_listener();
139
		self::initialize_sender();
140
		self::$sender->send_checksum();
141
		self::$sender->do_sync();
142
	}
143
144
	static function initialize_listener() {
145
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
146
		self::$listener = Jetpack_Sync_Listener::getInstance();
147
	}
148
149
	static function initialize_sender() {
150
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
151
		self::$sender = Jetpack_Sync_Sender::getInstance();
152
		
153
		// bind the sending process
154
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 3 );
155
	}
156
}
157
158
// Allow other plugins to add filters before we initalize the actions.
159
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
160