Completed
Push — fix/refactor-full-sync-status-... ( bcba1f...91ea1c )
by
unknown
88:05 queued 78:09
created

Jetpack_Sync_Module_Full_Sync::get_sender()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
/**
4
 * This class does a full resync of the database by
5
 * enqueuing an outbound action for every single object
6
 * that we care about.
7
 *
8
 * This class contains a few non-obvious optimisations that should be explained:
9
 * - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database
10
 * - for each object type, we obtain a full list of object IDs to sync via a single API call (hoping that since they're ints, they can all fit in RAM)
11
 * - we load the full objects for those IDs in chunks of Jetpack_Sync_Full::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls)
12
 * - we fire a trigger for the entire array which the Jetpack_Sync_Sender then serializes and queues.
13
 */
14
15
require_once 'class.jetpack-sync-wp-replicastore.php';
16
17
class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module {
18
	static $status_option = 'jetpack_full_sync_status';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $status_option.

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...
19
	static $transient_timeout = 3600; // an hour
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $transient_timeout.

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
	private $sender;
0 ignored issues
show
Unused Code introduced by
The property $sender is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
	public function name() {
24
		return 'full-sync';
25
	}
26
27
	function init_listeners( $callable ) {
28
		// synthetic actions for full sync
29
		add_action( 'jetpack_full_sync_start', $callable );
30
		add_action( 'jetpack_full_sync_end', $callable );
31
	}
32
33
	function init_before_send() {
34
		// this is triggered after actions have been processed on the server
35
		add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) );
36
	}
37
38
	function start() {
39
		if( ! $this->should_start_full_sync() ) {
40
			return false;
41
		}
42
		/**
43
		 * Fires when a full sync begins. This action is serialized
44
		 * and sent to the server so that it can clear the replica storage,
45
		 * and/or reset other data.
46
		 *
47
		 * @since 4.2.0
48
		 */
49
		do_action( 'jetpack_full_sync_start' );
50
		$this->set_status_queuing_started();
51
52
		foreach( Jetpack_Sync_Modules::get_modules() as $module ) {
53
			$module->full_sync();
54
		}
55
56
		$this->set_status_queuing_finished();
57
58
		$store = new Jetpack_Sync_WP_Replicastore();
59
		do_action( 'jetpack_full_sync_end', $store->checksum_all() );
60
		return true;
61
	}
62
63
	private function should_start_full_sync() {
64
		$status = $this->get_status();
65
		// We should try sync if we haven't started it yet or if we have finished it.
66
		if( is_null( $status['started'] ) || is_integer( $status['finished'] ) ) {
67
			return true;
68
		}
69
		return false;
70
	}
71
72
	function update_sent_progress_action( $actions ) {
73
		// quick way to map to first items with an array of arrays
74
		$actions_with_counts = array_count_values( array_map( 'reset', $actions ) );
75
76
		$status = $this->get_status();
77
		if ( is_null( $status['started'] ) || $status['finished'] ) {
78
			return;
79
		}
80
81
		if ( isset( $actions_with_counts[ 'jetpack_full_sync_start' ] ) ) {
82
			$status['sent_started'] = time();
83
		}
84
85
		foreach( Jetpack_Sync_Modules::get_modules() as $module ) {
86
			$module_name = $module->name();
87
			$module_actions = $module->get_full_sync_actions();
88
			foreach( $module_actions as $module_action ) {
89
				if ( isset( $actions_with_counts[ $module_action ] ) ) {
90
					if ( ! isset( $status[ 'sent' ][ $module_name ] ) ) {
91
						$status['sent'][ $module_name ] = 0;	
92
					}
93
					$status['sent'][ $module_name ] += $actions_with_counts[ $module_action ];	
94
				}
95
			}
96
		}
97
98
		if ( isset( $actions_with_counts[ 'jetpack_full_sync_end' ] ) ) {
99
			$status['finished'] = time();
100
		}
101
102
		$this->update_status( $status );
103
	}
104
105
	private function set_status_queuing_started() {
106
		$status = $this->initial_status;
107
		$status[ 'started' ] = time();
108
		$this->update_status( $status );
109
	}
110
111
	private function set_status_queuing_finished() {
112
		$this->update_status( array( 'queue_finished' => time() ) );
113
	}
114
115
	private $initial_status = array(
116
		'started' => null,
117
		'queue_finished' => null,
118
		'sent_started' => null,
119
		'finished' => null,
120
		'sent' => array(),
121
		'queue' => array(),
122
	);
123
124
	public function get_status() {
125
		return get_option( self::$status_option, $this->initial_status );
126
	}
127
128
	public function update_status( $status ) {
129
		return update_option(
130
			self::$status_option,
131
			array_merge( $this->get_status(), $status )
132
		);
133
	}
134
135
	private function clear_status() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
136
		delete_option( self::$status_option );
137
	}
138
}
139