Completed
Push — add/theme_update ( 13ed5e )
by
unknown
450:23 queued 436:51
created

Jetpack_Sync_Module_Themes::check_upgrader()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 49
Code Lines 20

Duplication

Lines 45
Ratio 91.84 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 5
nop 2
dl 45
loc 49
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Themes extends Jetpack_Sync_Module {
4
	function name() {
5
		return 'themes';
6
	}
7
8
	public function init_listeners( $callable ) {
9
		add_action( 'switch_theme', array( $this, 'sync_theme_support' ) );
10
		add_action( 'jetpack_sync_current_theme_support', $callable );
11
		add_action( 'upgrader_process_complete', array( $this, 'check_upgrader'), 10, 2 );
12
		add_action( 'jetpack_installed_theme', $callable, 10, 2 );
13
		add_action( 'jetpack_updated_theme', $callable, 10, 2 );
14
15
		// Sidebar updates.
16
		add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 );
17
		add_action( 'jetpack_widget_added', $callable, 10, 2 );
18
		add_action( 'jetpack_widget_removed', $callable, 10, 2 );
19
		add_action( 'jetpack_widget_moved_to_inactive', $callable );
20
		add_action( 'jetpack_cleared_inactive_widgets', $callable );
21
		add_action( 'jetpack_widget_reordered', $callable );
22
	}
23
24
	public function check_upgrader( $upgrader, $details) {
25 View Code Duplication
		if ( ! isset( $details['type'] ) ||
26
			'theme' !== $details['type'] ||
27
			is_wp_error( $upgrader->skin->result ) ||
28
			! method_exists( $upgrader, 'theme_info' )
29
		) {
30
			return;
31
		}
32
33 View Code Duplication
		if ( 'install' === $details['action'] ) {
34
			$theme = $upgrader->theme_info();
35
			$theme_info = array(
36
				'name' => $theme->get('Name'),
37
				'version' => $theme->get('Version'),
38
				'uri' => $theme->get('ThemeURI'),
39
			);
40
41
			/**
42
			 * Signals to the sync listener that a theme was installed and a sync action
43
			 * reflecting the installation and the theme info should be sent
44
			 *
45
			 * @since 4.9.0
46
			 *
47
			 * @param string $theme->theme_root Text domain of the theme
48
			 * @param mixed $theme_info Array of abbreviated theme info
49
			 */
50
			do_action( 'jetpack_installed_theme', $theme->stylesheet, $theme_info );
51
		}
52
53 View Code Duplication
		if ( 'update' === $details['action'] ) {
54
			$theme = $upgrader->theme_info();
55
			$theme_info = array(
56
				'name' => $theme->get('Name'),
57
				'version' => $theme->get('Version'),
58
				'uri' => $theme->get('ThemeURI'),
59
			);
60
61
			/**
62
			 * Signals to the sync listener that a theme was updated and a sync action
63
			 * reflecting the update and the theme info should be sent
64
			 *
65
			 * @since 4.9.0
66
			 *
67
			 * @param string $theme->theme_root Text domain of the theme
68
			 * @param mixed $theme_info Array of abbreviated theme info
69
			 */
70
			do_action( 'jetpack_updated_theme', $theme->stylesheet, $theme_info );
71
		}
72
	}
73
74
	public function init_full_sync_listeners( $callable ) {
75
		add_action( 'jetpack_full_sync_theme_data', $callable );
76
	}
77
78
	public function sync_theme_support() {
79
		/**
80
		 * Fires when the client needs to sync theme support info
81
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
82
		 *
83
		 * @since 4.2.0
84
		 *
85
		 * @param object the theme support hash
86
		 */
87
		do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() );
88
	}
89
90
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
91
		/**
92
		 * Tells the client to sync all theme data to the server
93
		 *
94
		 * @since 4.2.0
95
		 *
96
		 * @param boolean Whether to expand theme data (should always be true)
97
		 */
98
		do_action( 'jetpack_full_sync_theme_data', true );
99
100
		// The number of actions enqueued, and next module state (true == done)
101
		return array( 1, true );
102
	}
103
104
	public function estimate_full_sync_actions( $config ) {
105
		return 1;
106
	}
107
	
108
	public function init_before_send() {
109
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) );
110
	}
111
112
	function get_full_sync_actions() {
113
		return array( 'jetpack_full_sync_theme_data' );
114
	}
115
116
	function expand_theme_data() {
117
		return array( $this->get_theme_support_info() );
118
	}
119
120
	function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) {
121
		$added_widgets = array_diff( $new_widgets, $old_widgets );
122
		if ( empty( $added_widgets ) ) {
123
			return array();
124
		}
125
		$moved_to_sidebar = array();
126
		foreach ( $added_widgets as $added_widget ) {
127
			$moved_to_sidebar[] = $added_widget;
128
			/**
129
			 * Helps Sync log that a widget got added
130
			 *
131
			 * @since 4.9.0
132
			 *
133
			 * @param string $sidebar, Sidebar id got changed
134
			 * @param string $added_widget, Widget id got added
135
			 */
136
			do_action( 'jetpack_widget_added', $sidebar, $added_widget );
137
		}
138
		return $moved_to_sidebar;
139
	}
140
141
	function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets  ) {
142
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
143
144
		if ( empty( $removed_widgets ) ) {
145
			return array();
146
		}
147
148
		$moved_to_inactive = array();
149
150
		foreach( $removed_widgets as $removed_widget ) {
151
			// Lets check if we didn't move the widget to in_active_widgets
152
			if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) {
153
				/**
154
				 * Helps Sync log that a widgte got removed
155
				 *
156
				 * @since 4.9.0
157
				 *
158
				 * @param string $sidebar, Sidebar id got changed
159
				 * @param string $removed_widget, Widget id got removed
160
				 */
161
				do_action( 'jetpack_widget_removed', $sidebar, $removed_widget );
162
			} else {
163
				$moved_to_inactive[] = $removed_widget;
164
			}
165
		}
166
		return $moved_to_inactive;
167
168
	}
169
170
	function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) {
171
		$added_widgets = array_diff( $new_widgets, $old_widgets );
172
		if ( ! empty( $added_widgets ) ) {
173
			return;
174
		}
175
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
176
		if ( ! empty( $removed_widgets ) ) {
177
			return;
178
		}
179
180
		if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) {
181
			/**
182
			 * Helps Sync log that a sidebar id got reordered
183
			 *
184
			 * @since 4.9.0
185
			 *
186
			 * @param string $sidebar, Sidebar id got changed
187
			 */
188
			do_action( 'jetpack_widget_reordered', $sidebar );
189
		}
190
191
	}
192
193
	function sync_sidebar_widgets_actions( $old_value, $new_value ) {
194
195
		// Don't really know how to deal with different array_values yet.
196
		if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) {
197
			return;
198
		}
199
200
		$moved_to_inactive = array();
201
		$moved_to_sidebar = array();
202
203
		foreach ( $new_value as $sidebar => $new_widgets ) {
204
			if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) {
205
				continue;
206
			}
207
			$old_widgets = $old_value[ $sidebar ];
208
209
			$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] );
210
			$moved_to_inactive = array_merge( $moved_to_inactive, $moved_to_inactive_recently );
211
212
213
			$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar );
214
			$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently );
215
216
			$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar );
217
218
		}
219
220
		// Treat inactive sidebar a bit differently
221
		if ( ! empty( $moved_to_inactive ) ) {
222
			/**
223
			 * Helps Sync log that a widgets IDs got moved to in active
224
			 *
225
			 * @since 4.9.0
226
			 *
227
			 * @param array $sidebar, Sidebar id got changed
228
			 */
229
			do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive );
230
		} elseif ( empty( $moved_to_sidebar ) &&
231
		           empty( $new_value['wp_inactive_widgets']) &&
232
		           ! empty( $old_value['wp_inactive_widgets'] ) ) {
233
			/**
234
			 * Helps Sync log that a got cleared from inactive.
235
			 *
236
			 * @since 4.9.0
237
			 */
238
			do_action( 'jetpack_cleared_inactive_widgets' );
239
		} 
240
	}
241
242
	private function get_theme_support_info() {
243
		global $_wp_theme_features;
244
245
		$theme_support = array();
246
247
		foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) {
0 ignored issues
show
Bug introduced by
The property default_theme_support_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

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...
248
			$has_support = current_theme_supports( $theme_feature );
249
			if ( $has_support ) {
250
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
251
			}
252
		}
253
254
		$theme = wp_get_theme();
255
		$theme_support['name'] = $theme->name;
256
		$theme_support['version'] =  $theme->version;
257
258
		return $theme_support;
259
	}
260
}
261