Completed
Push — add/widget_modified ( 8f0fab...27417a )
by
unknown
12:27
created

Jetpack_Sync_Module_Themes   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 387
Duplicated Lines 1.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 387
rs 6.433
c 0
b 0
f 0
wmc 57
lcom 1
cbo 2

20 Methods

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