Completed
Push — branch-4.2 ( 75dc85...f982fc )
by Jeremy
17:06 queued 07:07
created

Jetpack_Sync_Actions::init()   D

Complexity

Conditions 10
Paths 3

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 10
eloc 21
c 5
b 0
f 1
nc 3
nop 0
dl 0
loc 46
rs 4.983

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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