Completed
Push — master-stable ( 53f101...a82972 )
by
unknown
86:26 queued 76:28
created

Jetpack_Sync_Module_Themes   C

Complexity

Total Complexity 61

Size/Duplication

Total Lines 412
Duplicated Lines 1.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 412
rs 6.018
c 0
b 0
f 0
wmc 61
lcom 1
cbo 2

22 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
B init_listeners() 0 25 1
A sync_widget_edit() 0 16 1
B sync_network_allowed_themes_change() 0 30 2
A get_theme_details_for_slugs() 0 13 2
B detect_theme_edit() 0 37 6
B detect_theme_deletion() 0 25 5
C check_upgrader() 7 42 7
A init_full_sync_listeners() 0 3 1
A sync_theme_support() 0 11 1
A enqueue_full_sync_actions() 0 13 1
A estimate_full_sync_actions() 0 3 1
A init_before_send() 0 3 1
A get_full_sync_actions() 0 3 1
A expand_theme_data() 0 3 1
A get_widget_name() 0 4 2
A get_sidebar_name() 0 4 2
B sync_add_widgets_to_sidebar() 0 26 3
B sync_remove_widgets_from_sidebar() 0 32 5
B sync_widgets_reordered() 0 24 4
C sync_sidebar_widgets_actions() 0 51 10
A get_theme_support_info() 0 18 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Sync_Module_Themes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Sync_Module_Themes, and based on these observations, apply Extract Interface, too.

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
		add_action( 'delete_site_transient_update_themes', array( $this, 'detect_theme_deletion') );
15
		add_action( 'jetpack_deleted_theme', $callable );
16
		add_filter( 'wp_redirect', array( $this, 'detect_theme_edit' ) );
17
		add_action( 'jetpack_edited_theme', $callable, 10, 2 );
18
		add_action( 'update_site_option_allowedthemes', array( $this, 'sync_network_allowed_themes_change' ), 10, 4 );
19
		add_action( 'jetpack_network_disabled_themes', $callable, 10, 2 );
20
		add_action( 'jetpack_network_enabled_themes', $callable, 10, 2 );
21
22
		// Sidebar updates.
23
		add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 );
24
25
		add_action( 'jetpack_widget_added', $callable, 10, 4 );
26
		add_action( 'jetpack_widget_removed', $callable, 10, 4 );
27
		add_action( 'jetpack_widget_moved_to_inactive', $callable, 10, 2 );
28
		add_action( 'jetpack_cleared_inactive_widgets', $callable );
29
		add_action( 'jetpack_widget_reordered', $callable, 10, 2 );
30
		add_filter( 'widget_update_callback', array( $this, 'sync_widget_edit' ), 10, 4 );
31
		add_action( 'jetpack_widget_edited', $callable );
32
	}
33
34
	public function sync_widget_edit( $instance, $new_instance, $old_instance, $widget_object ) {
35
		$widget = array(
36
			'name' => $widget_object->name,
37
			'id' => $widget_object->id,
38
		);
39
		/**
40
		 * Trigger action to alert $callable sync listener that a widget was edited
41
		 *
42
		 * @since 5.0.0
43
		 *
44
		 * @param string $widget_name, Name of edited widget
45
		 */
46
		do_action( 'jetpack_widget_edited', $widget );
47
48
		return $instance;
49
	}
50
51
	public function sync_network_allowed_themes_change( $option, $value, $old_value, $network_id ) {
52
		$all_enabled_theme_slugs = array_keys( $value );
53
54
		if ( count( $old_value ) > count( $value ) )  {
55
			$newly_disabled_theme_names = array_keys( array_diff_key( $old_value, $value ) );
56
			$newly_disabled_themes = $this->get_theme_details_for_slugs( $newly_disabled_theme_names );
57
			/**
58
			 * Trigger action to alert $callable sync listener that network themes were disabled
59
			 *
60
			 * @since 5.0.0
61
			 *
62
			 * @param mixed $newly_disabled_themes, Array of info about network disabled themes
63
			 * @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes
64
			 */
65
			do_action( 'jetpack_network_disabled_themes', $newly_disabled_themes, $all_enabled_theme_slugs );
66
			return;
67
		}
68
69
		$newly_enabled_theme_names = array_keys( array_diff_key( $value, $old_value ) );
70
		$newly_enabled_themes = $this->get_theme_details_for_slugs( $newly_enabled_theme_names );
71
		/**
72
		 * Trigger action to alert $callable sync listener that network themes were enabled
73
		 *
74
		 * @since 5.0.0
75
		 *
76
		 * @param mixed $newly_enabled_themes , Array of info about network enabled themes
77
		 * @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes
78
		 */
79
		do_action( 'jetpack_network_enabled_themes', $newly_enabled_themes, $all_enabled_theme_slugs );
80
	}
81
82
	private function get_theme_details_for_slugs( $theme_slugs ) {
83
		$theme_data = array();
84
		foreach ( $theme_slugs as $slug ) {
85
			$theme = wp_get_theme( $slug );
86
			$theme_data[ $slug ] = array(
87
				'name' => $theme->get( 'Name' ),
88
				'version' => $theme->get( 'Version' ),
89
				'uri' => $theme->get( 'ThemeURI' ),
90
				'slug' => $slug,
91
			);
92
		}
93
		return $theme_data;
94
	}
95
96
	public function detect_theme_edit( $redirect_url ) {
97
		$url = wp_parse_url( admin_url( $redirect_url ) );
98
		$theme_editor_url = wp_parse_url( admin_url( 'theme-editor.php' ) );
99
100
		if ( $theme_editor_url['path'] !== $url['path'] ) {
101
			return $redirect_url;
102
		}
103
104
		$query_params = array();
105
		wp_parse_str( $url['query'], $query_params );
106
		if (
107
			! isset( $_POST['newcontent'] ) ||
108
			! isset( $query_params['file'] ) ||
109
			! isset( $query_params['theme'] ) ||
110
			! isset( $query_params['updated'] )
111
		) {
112
			return $redirect_url;
113
		}
114
		$theme = wp_get_theme( $query_params['theme'] );
115
		$theme_data = array(
116
			'name' => $theme->get('Name'),
117
			'version' => $theme->get('Version'),
118
			'uri' => $theme->get( 'ThemeURI' ),
119
		);
120
121
		/**
122
		 * Trigger action to alert $callable sync listener that a theme was edited
123
		 *
124
		 * @since 5.0.0
125
		 *
126
		 * @param string $query_params['theme'], Slug of edited theme
127
		 * @param string $theme_data, Information about edited them
128
		 */
129
		do_action( 'jetpack_edited_theme', $query_params['theme'], $theme_data );
130
131
		return $redirect_url;
132
	}
133
134
	public function detect_theme_deletion() {
135
		$backtrace = debug_backtrace();
136
		$delete_theme_call = null;
137
		foreach ( $backtrace as $call ) {
138
			if ( isset( $call['function'] ) && 'delete_theme' === $call['function'] ) {
139
				$delete_theme_call = $call;
140
				break;
141
			}
142
		}
143
		if ( empty( $delete_theme_call ) ) {
144
			return;
145
		}
146
147
		$slug = $delete_theme_call['args'][0];
148
149
		/**
150
		 * Signals to the sync listener that a theme was deleted and a sync action
151
		 * reflecting the deletion and theme slug should be sent
152
		 *
153
		 * @since 5.0.0
154
		 *
155
		 * @param string $slug Theme slug
156
		 */
157
		do_action( 'jetpack_deleted_theme', $slug );
158
	}
159
160
	public function check_upgrader( $upgrader, $details) {
161 View Code Duplication
		if ( ! isset( $details['type'] ) ||
162
			'theme' !== $details['type'] ||
163
			is_wp_error( $upgrader->skin->result ) ||
164
			! method_exists( $upgrader, 'theme_info' )
165
		) {
166
			return;
167
		}
168
169
		$theme = $upgrader->theme_info();
170
		$theme_info = array(
171
			'name' => $theme->get( 'Name' ),
172
			'version' => $theme->get( 'Version' ),
173
			'uri' => $theme->get( 'ThemeURI' ),
174
		);
175
176
		if ( 'install' === $details['action'] ) {
177
			/**
178
			 * Signals to the sync listener that a theme was installed and a sync action
179
			 * reflecting the installation and the theme info should be sent
180
			 *
181
			 * @since 4.9.0
182
			 *
183
			 * @param string $theme->theme_root Text domain of the theme
184
			 * @param mixed $theme_info Array of abbreviated theme info
185
			 */
186
			do_action( 'jetpack_installed_theme', $theme->stylesheet, $theme_info );
187
		}
188
189
		if ( 'update' === $details['action'] ) {
190
			/**
191
			 * Signals to the sync listener that a theme was updated and a sync action
192
			 * reflecting the update and the theme info should be sent
193
			 *
194
			 * @since 4.9.0
195
			 *
196
			 * @param string $theme->theme_root Text domain of the theme
197
			 * @param mixed $theme_info Array of abbreviated theme info
198
			 */
199
			do_action( 'jetpack_updated_theme', $theme->stylesheet, $theme_info );
200
		}
201
	}
202
203
	public function init_full_sync_listeners( $callable ) {
204
		add_action( 'jetpack_full_sync_theme_data', $callable );
205
	}
206
207
	public function sync_theme_support() {
208
		/**
209
		 * Fires when the client needs to sync theme support info
210
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
211
		 *
212
		 * @since 4.2.0
213
		 *
214
		 * @param object the theme support hash
215
		 */
216
		do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() );
217
	}
218
219
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
220
		/**
221
		 * Tells the client to sync all theme data to the server
222
		 *
223
		 * @since 4.2.0
224
		 *
225
		 * @param boolean Whether to expand theme data (should always be true)
226
		 */
227
		do_action( 'jetpack_full_sync_theme_data', true );
228
229
		// The number of actions enqueued, and next module state (true == done)
230
		return array( 1, true );
231
	}
232
233
	public function estimate_full_sync_actions( $config ) {
234
		return 1;
235
	}
236
	
237
	public function init_before_send() {
238
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) );
239
	}
240
241
	function get_full_sync_actions() {
242
		return array( 'jetpack_full_sync_theme_data' );
243
	}
244
245
	function expand_theme_data() {
246
		return array( $this->get_theme_support_info() );
247
	}
248
249
	function get_widget_name( $widget_id ) {
250
		global $wp_registered_widgets;
251
		return ( isset( $wp_registered_widgets[ $widget_id ] ) ? $wp_registered_widgets[ $widget_id ]['name'] : null );
252
	}
253
254
	function get_sidebar_name( $sidebar_id ) {
255
		global $wp_registered_sidebars;
256
		return ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ? $wp_registered_sidebars[ $sidebar_id ]['name'] : null );
257
	}
258
259
	function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) {
260
		$added_widgets = array_diff( $new_widgets, $old_widgets );
261
		if ( empty( $added_widgets ) ) {
262
			return array();
263
		}
264
		$moved_to_sidebar = array();
265
		$sidebar_name = $this->get_sidebar_name( $sidebar );
266
267
		foreach ( $added_widgets as $added_widget ) {
268
			$moved_to_sidebar[] = $added_widget;
269
			$added_widget_name = $this->get_widget_name( $added_widget );
270
			/**
271
			 * Helps Sync log that a widget got added
272
			 *
273
			 * @since 4.9.0
274
			 *
275
			 * @param string $sidebar, Sidebar id got changed
276
			 * @param string $added_widget, Widget id got added
277
			 * @param string $sidebar_name, Sidebar id got changed Since 5.0.0
278
			 * @param string $added_widget_name, Widget id got added Since 5.0.0
279
			 *
280
			 */
281
			do_action( 'jetpack_widget_added', $sidebar, $added_widget,  $sidebar_name, $added_widget_name );
282
		}
283
		return $moved_to_sidebar;
284
	}
285
286
	function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets  ) {
287
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
288
289
		if ( empty( $removed_widgets ) ) {
290
			return array();
291
		}
292
293
		$moved_to_inactive = array();
294
		$sidebar_name = $this->get_sidebar_name( $sidebar );
295
296
		foreach( $removed_widgets as $removed_widget ) {
297
			// Lets check if we didn't move the widget to in_active_widgets
298
			if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) {
299
				$removed_widget_name = $this->get_widget_name( $removed_widget );
300
				/**
301
				 * Helps Sync log that a widgte got removed
302
				 *
303
				 * @since 4.9.0
304
				 *
305
				 * @param string $sidebar, Sidebar id got changed
306
				 * @param string $removed_widget, Widget id got removed
307
				 * @param string $sidebar_name, Name of the sidebar that changed  Since 5.0.0
308
				 * @param string $removed_widget_name, Name of the widget that got removed Since 5.0.0
309
				 */
310
				do_action( 'jetpack_widget_removed', $sidebar, $removed_widget, $sidebar_name, $removed_widget_name );
311
			} else {
312
				$moved_to_inactive[] = $removed_widget;
313
			}
314
		}
315
		return $moved_to_inactive;
316
317
	}
318
319
	function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) {
320
		$added_widgets = array_diff( $new_widgets, $old_widgets );
321
		if ( ! empty( $added_widgets ) ) {
322
			return;
323
		}
324
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
325
		if ( ! empty( $removed_widgets ) ) {
326
			return;
327
		}
328
329
		if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) {
330
			$sidebar_name = $this->get_sidebar_name( $sidebar );
331
			/**
332
			 * Helps Sync log that a sidebar id got reordered
333
			 *
334
			 * @since 4.9.0
335
			 *
336
			 * @param string $sidebar, Sidebar id got changed
337
			 * @param string $sidebar_name, Name of the sidebar that changed  Since 5.0.0
338
			 */
339
			do_action( 'jetpack_widget_reordered', $sidebar, $sidebar_name );
340
		}
341
342
	}
343
344
	function sync_sidebar_widgets_actions( $old_value, $new_value ) {
345
346
		// Don't really know how to deal with different array_values yet.
347
		if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) {
348
			return;
349
		}
350
351
		$moved_to_inactive = array();
352
		$moved_to_sidebar = array();
353
354
		foreach ( $new_value as $sidebar => $new_widgets ) {
355
			if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) {
356
				continue;
357
			}
358
			$old_widgets = isset( $old_value[ $sidebar ] )
359
				? $old_value[ $sidebar ]
360
				: array();
361
362
			$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] );
363
			$moved_to_inactive_ids = array_merge( $moved_to_inactive, $moved_to_inactive_recently );
364
365
			$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar );
366
			$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently );
367
368
			$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar );
369
370
		}
371
372
		// Treat inactive sidebar a bit differently
373
		if ( ! empty( $moved_to_inactive_ids ) ) {
374
			$moved_to_inactive_name = array_map( array( $this, 'get_widget_name' ), $moved_to_inactive_ids );
375
			/**
376
			 * Helps Sync log that a widgets IDs got moved to in active
377
			 *
378
			 * @since 4.9.0
379
			 *
380
			 * @param array $moved_to_inactive_ids, Array of widgets id that moved to inactive id got changed
381
			 * @param array $moved_to_inactive_names, Array of widgets names that moved to inactive id got changed Since 5.0.0
382
			 */
383
			do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive_ids, $moved_to_inactive_name );
384
		} elseif ( empty( $moved_to_sidebar ) &&
385
		           empty( $new_value['wp_inactive_widgets']) &&
386
		           ! empty( $old_value['wp_inactive_widgets'] ) ) {
387
			/**
388
			 * Helps Sync log that a got cleared from inactive.
389
			 *
390
			 * @since 4.9.0
391
			 */
392
			do_action( 'jetpack_cleared_inactive_widgets' );
393
		} 
394
	}
395
396
	private function get_theme_support_info() {
397
		global $_wp_theme_features;
398
399
		$theme_support = array();
400
401
		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...
402
			$has_support = current_theme_supports( $theme_feature );
403
			if ( $has_support ) {
404
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
405
			}
406
		}
407
408
		$theme = wp_get_theme();
409
		$theme_support['name'] = $theme->name;
410
		$theme_support['version'] =  $theme->version;
411
412
		return $theme_support;
413
	}
414
}
415