Completed
Push — fix/full-sync-on-connect ( 1def8c )
by
unknown
138:16 queued 128:33
created

Jetpack_Sync_Actions::schedule_full_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
		// cron hooks
19
		add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) );
20
		add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ) );
21
22
		$prevent_sync_loading = 
23
				( !Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() ) 
24
			&& 
25
				!defined( 'PHPUNIT_JETPACK_TESTSUITE' );
26
27
		if ( ! $prevent_sync_loading && ! wp_next_scheduled ( 'jetpack_sync_send_db_checksum' ) ) {
28
			// Schedule a job to send DB checksums once an hour
29
			wp_schedule_event( time(), 'hourly', 'jetpack_sync_send_db_checksum' );
30
		}
31
32
		/**
33
		 * Fires on every request before default loading sync listener code.
34
		 * Return false to not load sync listener code that monitors common
35
		 * WP actions to be serialized.
36
		 * 
37
		 * By default this returns true for non-GET-requests, or requests where the 
38
		 * user is logged-in.
39
		 *
40
		 * @since 4.2.0
41
		 *
42
		 * @param bool should we load sync listener code for this request
43
		 */
44
		if ( !$prevent_sync_loading && apply_filters( 'jetpack_sync_listener_should_load',
45
				(
46
					$_SERVER['REQUEST_METHOD'] !== 'GET'
47
				||
48
					is_user_logged_in()
49
				||
50
					defined( 'PHPUNIT_JETPACK_TESTSUITE' )
51
				||
52
					is_admin()
53
				)
54
			) ) {
55
			self::initialize_listener();
56
		}
57
58
		// Sync connected user role changes to .com
59
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php';
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 ( !$prevent_sync_loading && apply_filters( 'jetpack_sync_sender_should_load',
74
			(
75
				$_SERVER['REQUEST_METHOD'] === 'POST'
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 send_data( $data, $codec_name, $sent_timestamp ) {
91
		Jetpack::load_xml_rpc_client();
92
93
		$url = add_query_arg( array(
94
			'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action
95
			'codec' => $codec_name, // send the name of the codec used to encode the data
96
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
97
		), Jetpack::xmlrpc_api_url() );
98
99
		$rpc = new Jetpack_IXR_Client( array(
100
			'url'     => $url,
101
			'user_id' => JETPACK_MASTER_USER,
102
			'timeout' => 30
103
		) );
104
105
		$result = $rpc->query( 'jetpack.syncActions', $data );
106
107
		if ( ! $result ) {
108
			return $rpc->get_jetpack_error();
109
		}
110
111
		return $rpc->getResponse();
112
	}
113
114
	static function schedule_full_sync() {
115
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
116
	}
117
118
	static function do_full_sync() {
119
		self::initialize_listener();
120
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start();
121
	}
122
123
	static function send_db_checksum() {
124
		self::initialize_sender();
125
		self::$sender->send_checksum();
126
	}
127
128
	static function initialize_listener() {
129
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
130
		self::$listener = Jetpack_Sync_Listener::getInstance();
131
	}
132
133
	static function initialize_sender() {
134
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
135
		self::$sender = Jetpack_Sync_Sender::getInstance();
136
		
137
		// bind the sending process
138
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 3 );
139
	}
140
}
141
142
// Allow other plugins to add filters before we initalize the actions.
143
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
144