Completed
Push — add/sync-rest-2 ( 7a9b36...5ec2d9 )
by
unknown
08:20
created

Jetpack_Sync::schedule_full_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
require_once 'class.jetpack-sync-posts.php';
4
require_once 'class.jetpack-sync-post-meta.php';
5
require_once 'class.jetpack-sync-comments.php';
6
require_once 'class.jetpack-sync-options.php';
7
require_once 'class.jetpack-sync-network-options.php';
8
require_once 'class.jetpack-sync-functions.php';
9
require_once 'class.jetpack-sync-constants.php';
10
require_once 'class.jetpack-sync-users.php';
11
require_once 'class.jetpack-sync-updates.php';
12
require_once 'class.jetpack-sync-reindex.php';
13
require_once 'class.jetpack-sync-themes.php';
14
if ( is_multisite() ) {
15
	require_once 'class.jetpack-sync-network-options.php';
16
}
17
18
class Jetpack_Sync {
19
	static $do_shutdown = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $do_shutdown.

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...
20
21
	static $cron_name = 'jetpack_sync_next_beat';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $cron_name.

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...
22
23
24
	static function init() {
25
		Jetpack_Sync_Posts::init();
26
		Jetpack_Sync_Post_Meta::init();
27
		Jetpack_Sync_Comments::init();
28
		Jetpack_Sync_Options::init();
29
		Jetpack_Sync_Users::init();
30
		Jetpack_Sync_Network_Options::init();
31
		Jetpack_Sync_Updates::init();
32
		Jetpack_Sync_Reindex::init();
33
34
		// On jetpack version bump
35
		add_action( 'updating_jetpack_version', array( __CLASS__, 'schedule_full_sync' ) );
36
		// On jetpack registration
37
		add_action( 'jetpack_site_registered', array( __CLASS__, 'schedule_full_sync' ) );
38
		// Add on cron
39
		add_action( self::$cron_name, array( __CLASS__, 'cron_exec' ) );
40
	}
41
42
	static function schedule_sync() {
43
		if ( ! self::$do_shutdown ) {
44
			self::$do_shutdown = true;
45
			if ( function_exists( 'ignore_user_abort' ) ) {
46
				ignore_user_abort( true );
47
			}
48
			add_action( 'shutdown', array( __CLASS__, 'sync_partial_on_shutdown' ), 9 );
49
		}
50
	}
51
52
	static function cron_exec() {
53
		Jetpack::xmlrpc_async_call( 'jetpack.sync_v2', self::get_data_to_sync() );
54
	}
55
56
	static function schedule_next_cron() {
57
		if ( ! wp_next_scheduled( self::$cron_name ) ) {
58
			$next_minute = time() + 60;
59
			wp_schedule_single_event( $next_minute, self::$cron_name );
60
		}
61
	}
62
63
	static function remove_cron() {
64
		$timestamp = wp_next_scheduled( self::$cron_name );
65
		wp_unschedule_event( $timestamp, self::$cron_name );
66
	}
67
68
	static function slice_ids( $ids, $max, $option_name ) {
69
		$lock_name    = $option_name . '_lock';
70
		$is_locked    = get_option( $lock_name );
71
		$post_ids_que = get_option( $option_name );
72
73
		if ( ! empty( $post_ids_que ) ) {
74
			$ids = array_unique( array_merge( $ids, $post_ids_que ) );
75
		}
76
		$pid = getmypid();
77
		if ( ! $is_locked || $pid === $is_locked ) {
78
			update_option( $lock_name, $pid );
79
80
			if ( sizeof( $ids ) <= $max ) {
81
				delete_option( $option_name );
82
				delete_option( $lock_name );
83
84
				return $ids;
85
			}
86
			$to_save = array_splice( $ids, $max );
87
			update_option( $option_name, $to_save );
88
			delete_option( $lock_name );
89
90
			Jetpack_Sync::schedule_next_cron();
91
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
92
93
		}
94
95
		return $ids;
96
	}
97
98
	static function schedule_full_sync() {
99
		add_action( 'shutdown', array( __CLASS__, 'sync_full_on_shutdown' ), 9 );
100
	}
101
102
	static function sync_partial_on_shutdown() {
103
		if ( ! self::should_sync() ) {
104
			return;
105
		}
106
		Jetpack::xmlrpc_async_call( 'jetpack.sync_v2', self::get_data_to_sync() );
107
	}
108
109
	static function sync_full_on_shutdown() {
110
		if ( ! self::should_sync() ) {
111
			return;
112
		}
113
		Jetpack::xmlrpc_async_call( 'jetpack.sync_v2', self::get_all_data_() );
0 ignored issues
show
Bug introduced by
The method get_all_data_() does not exist on Jetpack_Sync. Did you maybe mean get_all_data()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
	}
115
116
	static function should_sync() {
117
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
118
			return false;
119
		}
120
		if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) {
121
			return false;
122
		}
123
124
		return true;
125
	}
126
127
	static function get_data_to_sync() {
128
		$send['options']        = Jetpack_Sync_Options::get_to_sync();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$send was never initialized. Although not strictly required by PHP, it is generally a good practice to add $send = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
129
		$send['options_delete'] = Jetpack_Sync_Options::get_to_delete();
130
		$send['constants']      = self::sync_if_has_changed( Jetpack_Sync_Constants::$check_sum_id, Jetpack_Sync_Constants::get_all() );
0 ignored issues
show
Bug introduced by
The property check_sum_id cannot be accessed from this context as it is declared private in class Jetpack_Sync_Constants.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
131
		$send['posts']          = Jetpack_Sync_Posts::posts_to_sync();
132
		$send['posts_delete']   = Jetpack_Sync_Posts::posts_to_delete();
133
134
		$send['post_meta']        = Jetpack_Sync_Post_Meta::post_meta_to_sync();
135
		$send['post_meta_delete'] = Jetpack_Sync_Post_Meta::post_meta_to_delete();
136
137
		$send['comments']        = Jetpack_Sync_Comments::comments_to_sync();
138
		$send['delete_comments'] = Jetpack_Sync_Comments::comments_to_delete();
139
		$send['updates']         = Jetpack_Sync_Updates::get_to_sync();
140
		$send['themes']          = Jetpack_Sync_Themes::get_to_sync();
141
142
		if ( false === ( $do_check = get_transient( 'jetpack_sync_functions' ) ) ) {
143
			$send['functions'] = self::sync_if_has_changed( Jetpack_Sync_Functions::$check_sum_id, Jetpack_Sync_Functions::get_all() );
0 ignored issues
show
Bug introduced by
The property check_sum_id cannot be accessed from this context as it is declared private in class Jetpack_Sync_Functions.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
144
			set_transient( 'jetpack_sync_functions', true, MINUTE_IN_SECONDS );
145
		}
146
		if ( is_multisite() ) {
147
			$send['network_options']        = Jetpack_Sync_Network_Options::get_to_sync();
148
			$send['network_options_delete'] = Jetpack_Sync_Network_Options::get_to_delete();
149
		}
150
151
		return array_filter( $send );
152
	}
153
154
	static function get_all_data() {
155
		$send['options']   = Jetpack_Sync_Options::get_all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$send was never initialized. Although not strictly required by PHP, it is generally a good practice to add $send = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
156
		$send['constants'] = Jetpack_Sync_Constants::get_all();
157
		$send['functions'] = Jetpack_Sync_Functions::get_all();
158
		$send['updates']   = Jetpack_Sync_Updates::get_all();
159
		$send['themes']    = Jetpack_Sync_Themes::get_all();
160
		if ( is_multisite() ) {
161
			$send['network_options'] = Jetpack_Sync_Network_Options::get_all();
162
		}
163
164
		return $send;
165
	}
166
167
	static function sync_if_has_changed( $check_sum_id, $values ) {
168
		$current_check_sum = self::get_check_sum( $values );
169
		if ( Jetpack_Options::get_option( $check_sum_id ) !== $current_check_sum ) {
170
			Jetpack_Options::update_option( $check_sum_id, $current_check_sum );
171
172
			return $values;
173
		}
174
175
		return null;
176
	}
177
178
	static function get_check_sum( $values ) {
179
		return crc32( json_encode( $values ) );
180
	}
181
182
}
183
184
Jetpack_Sync::init();
185