Completed
Push — add/sync-rest-2 ( df1b9f...967bcd )
by
unknown
08:38
created

Jetpack_Sync_Updates   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 2
Metric Value
wmc 26
c 8
b 0
f 2
lcom 1
cbo 1
dl 0
loc 149
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A update_get_wp_version() 0 6 3
A refresh_core_update_data() 0 9 3
A get_wp_version() 0 5 1
A init() 0 10 1
B refresh_update_data() 0 22 5
A get_to_sync() 0 3 1
A get_all() 0 7 1
D get_count() 0 44 9
A get_update_details() 0 19 2
1
<?php
2
3
class Jetpack_Sync_Updates {
4
5
	static $check_sum_id = 'updates_check_sum';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $check_sum_id.

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...
6
7
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

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...
8
9
	static function init() {
10
		/** Trigger a wp_version sync when updating WP versions
11
		 **/
12
		add_action( 'upgrader_process_complete', array( __CLASS__, 'update_get_wp_version' ), 10, 2 );
13
14
		// Anytime WordPress saves update data, we'll want to sync update data
15
		add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'refresh_update_data' ), 10, 2 );
16
		add_action( 'set_site_transient_update_themes', array( __CLASS__, 'refresh_update_data' ), 10, 2 );
17
		add_action( 'set_site_transient_update_core', array( __CLASS__, 'refresh_core_update_data' ), 10, 2 );
18
	}
19
20
	/**
21
	 * Triggers a sync of update counts and update details
22
	 */
23
	/**
24
	 * Keeps wp_version in sync with .com when WordPress core updates
25
	 **/
26
	static function update_get_wp_version( $update, $meta_data ) {
27
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
28
			self::$sync['wp_version'] = self::get_wp_version();
29
			Jetpack_Sync::schedule_shutdown();
30
		}
31
	}
32
33
	static function refresh_core_update_data( $value ) {
34
		if ( empty( $value->updates ) && empty( $value->translations ) ) {
35
			return;
36
		}
37
38
		self::$sync['updates']        = self::get_count( 'update_core' );
39
		self::$sync['update_details'] = self::get_update_details( 'update_core' );
40
		Jetpack_Sync::schedule_shutdown();
41
	}
42
43
	static function refresh_update_data( $value, $expiration ) {
0 ignored issues
show
Unused Code introduced by
The parameter $expiration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
45
46
		$prefix = 'pre_set_site_transient_';
47
		$transient = substr( current_filter(), strlen( $prefix ) );
48
		if ( current_filter() === $transient ) {
49
			return;
50
		}
51
52
		if ( ! in_array( $transient, array( 'update_themes', 'update_plugins' ) ) ) {
53
			return;
54
		}
55
56
		if ( empty( $value->response ) && empty( $value->translations ) ) {
57
			return;
58
		}
59
60
		self::$sync['updates']        = self::get_count( $transient );
61
		self::$sync['update_details'] = self::get_update_details( $transient );
62
		Jetpack_Sync::schedule_shutdown();
63
64
	}
65
66
	static function get_to_sync() {
67
		return self::$sync;
68
	}
69
70
	static function get_all() {
71
		return array(
72
			'updates'        => self::get_count(),
73
			'update_details' => self::get_update_details(),
74
			'wp_version'     => self::get_wp_version(),
75
		);
76
	}
77
78
	static function get_count( $key = null ) {
79
		$counts         = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
80
		$update_details = self::get_update_details( $key );
81
		$translations   = array();
82
83
		foreach ( $update_details as $key => $update_detail ) {
84
			if ( ! $update_detail ) {
85
				continue;
86
			}
87
			if ( 'wordpress' === $key ) {
88
				if ( ! empty( $update_detail->updates ) ) {
89
					// Don't set the update to be true if the update is a core autoupdate.
90
					if ( ! in_array( $update_detail->updates[0]->response, array(
91
						'development',
92
						'latest'
93
					) )
94
					) {
95
						$counts['wordpress']         = 1;
96
						$counts['wp_update_version'] = $update_detail->updates[0]->current;
97
					}
98
				}
99
100
			} else {
101
				// Themes and Plugins
102
				if ( ! empty( $update_detail->response ) ) {
103
					$counts[ $key ] = count( $update_detail->response );
104
				}
105
			}
106
107
			if ( isset( $update_detail->translations ) ) {
108
				foreach ( $update_detail->translations as $translation ) {
109
					$translations[] = (object) $translation;
110
				}
111
			}
112
113
		}
114
115
		$counts['translations'] = count( $translations );
116
		// calculate total
117
		$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
118
119
		return $counts;
120
121
	}
122
123
	static function get_update_details( $key = null ) {
124
		if ( in_array( $key, array( 'update_core', 'update_plugins', 'update_themes' ) ) ) {
125
126
			$map = array(
127
				'update_core'    => 'wordpress',
128
				'update_plugins' => 'plugins',
129
				'update_themes'  => 'themes',
130
			);
131
132
			return array( $map[ $key ] => get_site_transient( $key ) );
133
		}
134
		$update_details = array(
135
			'wordpress' => get_site_transient( 'update_core' ),
136
			'plugins'   => get_site_transient( 'update_plugins' ),
137
			'themes'    => get_site_transient( 'update_themes' ),
138
		);
139
140
		return $update_details;
141
	}
142
143
	/*
144
	* Sync back wp_version
145
	*/
146
	static function get_wp_version() {
147
		global $wp_version;
148
149
		return $wp_version;
150
	}
151
}
152