Completed
Push — fix/compatibility-with-wp-miss... ( f66257...dddf61 )
by
unknown
07:48
created

validate_update_change()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 3
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Updates extends Jetpack_Sync_Module {
4
5
	const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum';
6
7
	function name() {
8
		return 'updates';
9
	}
10
11
	public function init_listeners( $callable ) {
12
13
		add_action( 'set_site_transient_update_plugins', array( $this, 'validate_update_change' ), 10, 3 );
14
		add_action( 'set_site_transient_update_themes', array( $this, 'validate_update_change' ), 10, 3 );
15
		add_action( 'set_site_transient_update_core', array( $this, 'validate_update_change' ), 10, 3 );
16
17
		add_action( 'jetpack_update_plugins_change', $callable );
18
		add_action( 'jetpack_update_themes_change', $callable );
19
		add_action( 'jetpack_update_core_change', $callable );
20
21
		add_filter( 'jetpack_sync_before_enqueue_jetpack_update_plugins_change', array(
22
			$this,
23
			'filter_update_keys',
24
		), 10, 2 );
25
		add_filter( 'jetpack_sync_before_enqueue_upgrader_process_complete', array(
26
			$this,
27
			'filter_upgrader_process_complete',
28
		), 10, 2 );
29
30
		add_action( 'automatic_updates_complete', $callable );
31
	}
32
33
	public function init_full_sync_listeners( $callable ) {
34
		add_action( 'jetpack_full_sync_updates', $callable );
35
	}
36
37
	public function init_before_send() {
38
		// full sync
39
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) );
40
	}
41
42
	public function get_update_checksum( $value ) {
43
		// Create an new array so we don't modify the object passed in.
44
		$a_value = (array) $value;
45
46
		// ignore `last_checked`
47
		unset( $a_value['last_checked'] );
48
		unset( $a_value['checked'] );
49
		if ( empty( $a_value ) ) {
50
			return false;
51
		}
52
		return $this->get_check_sum( $a_value );
53
	}
54
55
	public function validate_update_change( $value, $expiration, $transient ) {
56
57
		$new_checksum = $this->get_update_checksum( $value );
58
		if ( false === $new_checksum  ) {
59
			return;
60
		}
61
62
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
63
64
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
65
			return;
66
		}
67
68
		$checksums[ $transient ] = $new_checksum;
69
70
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
71
		// possible $transient value are update_plugins, update_themes, update_core
72
		do_action( "jetpack_{$transient}_change", $value );
73
	}
74
75
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
76
		/**
77
		 * Tells the client to sync all updates to the server
78
		 *
79
		 * @since 4.2.0
80
		 *
81
		 * @param boolean Whether to expand updates (should always be true)
82
		 */
83
		do_action( 'jetpack_full_sync_updates', true );
84
85
		// The number of actions enqueued, and next module state (true == done)
86
		return array( 1, true );
87
	}
88
89
	public function estimate_full_sync_actions( $config ) {
90
		return 1;
91
	}
92
93
	function get_full_sync_actions() {
94
		return array( 'jetpack_full_sync_updates' );
95
	}
96
97
	public function get_all_updates() {
98
		return array(
99
			'core'    => get_site_transient( 'update_core' ),
100
			'plugins' => get_site_transient( 'update_plugins' ),
101
			'themes'  => get_site_transient( 'update_themes' ),
102
		);
103
	}
104
105
	// removes unnecessary keys from synced updates data
106
	function filter_update_keys( $args ) {
107
		$updates = $args[0];
108
109
		if ( isset( $updates->no_update ) ) {
110
			unset( $updates->no_update );
111
		}
112
113
		return $args;
114
	}
115
116
	function filter_upgrader_process_complete( $args ) {
117
		array_shift( $args );
118
119
		return $args;
120
	}
121
122
	public function expand_updates( $args ) {
123
		if ( $args[0] ) {
124
			return $this->get_all_updates();
125
		}
126
127
		return $args;
128
	}
129
}
130