Completed
Push — sync/sync-after-connection ( 26183c )
by
unknown
196:44 queued 186:10
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
class Jetpack_Sync_Actions {
4
	static $client = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $client.

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...
5
6
	static function init() {
7
		/**
8
		 * Fires on every request before default loading sync code.
9
		 * Return false to not load sync code and hook sync actions.
10
		 *
11
		 * @since 4.2.0
12
		 *
13
		 * @param bool should we load sync code for this request
14
		 */
15
		if ( ! apply_filters( 'jetpack_sync_should_load', 
16
			(
17
				$_SERVER['REQUEST_METHOD'] === 'POST' 
18
			|| 
19
				current_user_can( 'manage_options' ) 
20
			|| 
21
				is_admin() 
22
			||
23
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
24
			)
25
		&&
26
			( Jetpack::is_active() || defined( 'PHPUNIT_JETPACK_TESTSUITE' ) )
27
		&&
28
			!( Jetpack::is_development_mode() || Jetpack::is_staging_site() )
29
		
30
		) ) {
31
			return;
32
		}
33
		
34
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-client.php';
35
36
		self::$client = Jetpack_Sync_Client::getInstance();
37
38
		// bind the do_sync process to shutdown
39
		add_action( 'shutdown', array( self::$client, 'do_sync' ) );
40
41
		// bind the sending process
42
		add_filter( 'jetpack_sync_client_send_data', array( __CLASS__, 'send_data' ), 10, 2 );
43
44
		// On jetpack registration
45
		add_action( 'jetpack_site_registered', array( __CLASS__, 'schedule_full_sync' ) );
46
	}
47
48
	static function send_data( $data, $codec_name ) {
49
		Jetpack::load_xml_rpc_client();
50
51
		$url = add_query_arg( array(
52
			'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action
53
			'codec' => $codec_name, // send the name of the codec used to encode the data
54
		), Jetpack::xmlrpc_api_url() );
55
56
		$rpc = new Jetpack_IXR_Client( array(
57
			'url'     => $url,
58
			'user_id' => JETPACK_MASTER_USER,
59
			'timeout' => 30
60
		) );
61
62
		$result = $rpc->query( 'jetpack.syncActions', $data );
63
64
		if ( ! $result ) {
65
			return $rpc->get_jetpack_error();
66
		}
67
68
		return $rpc->getResponse();
69
	}
70
71
	static function schedule_full_sync() {
72
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
73
	}
74
75
}
76
// Allow other plugins to add filters before we initalize the actions.
77
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
78