Completed
Push — add/add_theme_name_to_theme_up... ( 9ba695 )
by
unknown
69:14 queued 59:49
created

Jetpack_Sync_Module_Updates::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
		add_filter( 'jetpack_sync_before_send_jetpack_update_themes_change', array( $this, 'expand_themes' ) );
42
	}
43
44
	public function get_update_checksum( $value ) {
45
		// Create an new array so we don't modify the object passed in.
46
		$a_value = (array) $value;
47
48
		// ignore `last_checked`
49
		unset( $a_value['last_checked'] );
50
		unset( $a_value['checked'] );
51
		if ( empty( $a_value ) ) {
52
			return false;
53
		}
54
		return $this->get_check_sum( $a_value );
55
	}
56
57
	public function validate_update_change( $value, $expiration, $transient ) {
58
59
		$new_checksum = $this->get_update_checksum( $value );
60
		if ( false === $new_checksum  ) {
61
			return;
62
		}
63
64
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
65
66
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
67
			return;
68
		}
69
70
		$checksums[ $transient ] = $new_checksum;
71
72
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
73
		// possible $transient value are update_plugins, update_themes, update_core
74
		do_action( "jetpack_{$transient}_change", $value );
75
	}
76
77
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
78
		/**
79
		 * Tells the client to sync all updates to the server
80
		 *
81
		 * @since 4.2.0
82
		 *
83
		 * @param boolean Whether to expand updates (should always be true)
84
		 */
85
		do_action( 'jetpack_full_sync_updates', true );
86
87
		// The number of actions enqueued, and next module state (true == done)
88
		return array( 1, true );
89
	}
90
91
	public function estimate_full_sync_actions( $config ) {
92
		return 1;
93
	}
94
95
	function get_full_sync_actions() {
96
		return array( 'jetpack_full_sync_updates' );
97
	}
98
99
	public function get_all_updates() {
100
		return array(
101
			'core'    => get_site_transient( 'update_core' ),
102
			'plugins' => get_site_transient( 'update_plugins' ),
103
			'themes'  => get_site_transient( 'update_themes' ),
104
		);
105
	}
106
107
	// removes unnecessary keys from synced updates data
108
	function filter_update_keys( $args ) {
109
		$updates = $args[0];
110
111
		if ( isset( $updates->no_update ) ) {
112
			unset( $updates->no_update );
113
		}
114
115
		return $args;
116
	}
117
118
	function filter_upgrader_process_complete( $args ) {
119
		array_shift( $args );
120
121
		return $args;
122
	}
123
124
	public function expand_updates( $args ) {
125
		if ( $args[0] ) {
126
			return $this->get_all_updates();
127
		}
128
129
		return $args;
130
	}
131
132
	public function expand_themes( $args ) {
133
		error_log(print_r($args, true));
134
	}
135
}
136