Completed
Push — fix/inline-docs-410 ( f96891...63b75c )
by
unknown
43:24 queued 33:40
created

Jetpack_Sync_Actions::send_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 22
rs 9.2
1
<?php
2
3
function jetpack_send_db_checksum() {
4
	$sync_sender = Jetpack_Sync_Sender::getInstance();
5
	$sync_sender->send_checksum();
6
}
7
8
class Jetpack_Sync_Actions {
9
	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...
10
	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...
11
12
	static function init() {
13
		/**
14
		 * Fires on every request before default loading sync listener code.
15
		 * Return false to not load sync listener code that monitors common
16
		 * WP actions to be serialized
17
		 *
18
		 * @since 4.2.0
19
		 *
20
		 * @param bool should we load sync listener code for this request
21
		 */
22
		if ( apply_filters( 'jetpack_sync_listener_should_load', 
23
				( 
24
					$_SERVER['REQUEST_METHOD'] !== 'GET' 
25
				||
26
					defined( 'PHPUNIT_JETPACK_TESTSUITE' )
27
				) 
28
			) ) {
29
			require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
30
			self::$listener = Jetpack_Sync_Listener::getInstance();
31
		}
32
33
		/**
34
		 * Fires on every request before default loading sync sender code.
35
		 * Return false to not load sync sender code that serializes pending
36
		 * data and sends it to WPCOM for processing.
37
		 *
38
		 * @since 4.2.0
39
		 *
40
		 * @param bool should we load sync sender code for this request
41
		 */
42
		if ( ! apply_filters( 'jetpack_sync_sender_should_load', 
43
			(
44
				$_SERVER['REQUEST_METHOD'] === 'POST' 
45
			|| 
46
				current_user_can( 'manage_options' ) 
47
			|| 
48
				is_admin() 
49
			||
50
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
51
			)
52
		&&
53
			( Jetpack::is_active() || defined( 'PHPUNIT_JETPACK_TESTSUITE' ) )
54
		&&
55
			!( Jetpack::is_development_mode() || Jetpack::is_staging_site() )
56
		
57
		) ) {
58
			return;
59
		}
60
		
61
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
62
		self::$sender = Jetpack_Sync_Sender::getInstance();
63
64
		// bind the do_sync process to shutdown
65
		add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
66
67
		// bind the sending process
68
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 2 );
69
70
		// On jetpack registration
71
		add_action( 'jetpack_site_registered', array( __CLASS__, 'schedule_full_sync' ) );
72
73
		// Schedule a job to send DB checksums once an hour
74
		if ( ! wp_next_scheduled ( 'jetpack_send_db_checksum' ) ) {
75
			wp_schedule_event( time(), 'hourly', 'jetpack_send_db_checksum' );
76
		}
77
	}
78
79
	static function send_data( $data, $codec_name ) {
80
		Jetpack::load_xml_rpc_client();
81
82
		$url = add_query_arg( array(
83
			'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action
84
			'codec' => $codec_name, // send the name of the codec used to encode the data
85
		), Jetpack::xmlrpc_api_url() );
86
87
		$rpc = new Jetpack_IXR_Client( array(
88
			'url'     => $url,
89
			'user_id' => JETPACK_MASTER_USER,
90
			'timeout' => 30
91
		) );
92
93
		$result = $rpc->query( 'jetpack.syncActions', $data );
94
95
		if ( ! $result ) {
96
			return $rpc->get_jetpack_error();
97
		}
98
99
		return $rpc->getResponse();
100
	}
101
102
	static function schedule_full_sync() {
103
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
104
	}
105
106
}
107
// Allow other plugins to add filters before we initalize the actions.
108
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
109