Completed
Push — test-create-new ( 4242ab )
by
unknown
09:14
created

Jetpack_Sync_Module_Themes   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 251
Duplicated Lines 2.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 251
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 15 1
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 48 9
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
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
		$theme = $upgrader->theme_info();
34
		$theme_info = array(
35
			'name' => $theme->get( 'Name' ),
36
			'version' => $theme->get( 'Version' ),
37
			'uri' => $theme->get( 'ThemeURI' ),
38
		);
39
40
		if ( 'install' === $details['action'] ) {
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
		if ( 'update' === $details['action'] ) {
54
			/**
55
			 * Signals to the sync listener that a theme was updated and a sync action
56
			 * reflecting the update and the theme info should be sent
57
			 *
58
			 * @since 4.9.0
59
			 *
60
			 * @param string $theme->theme_root Text domain of the theme
61
			 * @param mixed $theme_info Array of abbreviated theme info
62
			 */
63
			do_action( 'jetpack_updated_theme', $theme->stylesheet, $theme_info );
64
		}
65
	}
66
67
	public function init_full_sync_listeners( $callable ) {
68
		add_action( 'jetpack_full_sync_theme_data', $callable );
69
	}
70
71
	public function sync_theme_support() {
72
		/**
73
		 * Fires when the client needs to sync theme support info
74
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
75
		 *
76
		 * @since 4.2.0
77
		 *
78
		 * @param object the theme support hash
79
		 */
80
		do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() );
81
	}
82
83
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
84
		/**
85
		 * Tells the client to sync all theme data to the server
86
		 *
87
		 * @since 4.2.0
88
		 *
89
		 * @param boolean Whether to expand theme data (should always be true)
90
		 */
91
		do_action( 'jetpack_full_sync_theme_data', true );
92
93
		// The number of actions enqueued, and next module state (true == done)
94
		return array( 1, true );
95
	}
96
97
	public function estimate_full_sync_actions( $config ) {
98
		return 1;
99
	}
100
	
101
	public function init_before_send() {
102
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) );
103
	}
104
105
	function get_full_sync_actions() {
106
		return array( 'jetpack_full_sync_theme_data' );
107
	}
108
109
	function expand_theme_data() {
110
		return array( $this->get_theme_support_info() );
111
	}
112
113
	function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) {
114
		$added_widgets = array_diff( $new_widgets, $old_widgets );
115
		if ( empty( $added_widgets ) ) {
116
			return array();
117
		}
118
		$moved_to_sidebar = array();
119
		foreach ( $added_widgets as $added_widget ) {
120
			$moved_to_sidebar[] = $added_widget;
121
			/**
122
			 * Helps Sync log that a widget got added
123
			 *
124
			 * @since 4.9.0
125
			 *
126
			 * @param string $sidebar, Sidebar id got changed
127
			 * @param string $added_widget, Widget id got added
128
			 */
129
			do_action( 'jetpack_widget_added', $sidebar, $added_widget );
130
		}
131
		return $moved_to_sidebar;
132
	}
133
134
	function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets  ) {
135
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
136
137
		if ( empty( $removed_widgets ) ) {
138
			return array();
139
		}
140
141
		$moved_to_inactive = array();
142
143
		foreach( $removed_widgets as $removed_widget ) {
144
			// Lets check if we didn't move the widget to in_active_widgets
145
			if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) {
146
				/**
147
				 * Helps Sync log that a widgte got removed
148
				 *
149
				 * @since 4.9.0
150
				 *
151
				 * @param string $sidebar, Sidebar id got changed
152
				 * @param string $removed_widget, Widget id got removed
153
				 */
154
				do_action( 'jetpack_widget_removed', $sidebar, $removed_widget );
155
			} else {
156
				$moved_to_inactive[] = $removed_widget;
157
			}
158
		}
159
		return $moved_to_inactive;
160
161
	}
162
163
	function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) {
164
		$added_widgets = array_diff( $new_widgets, $old_widgets );
165
		if ( ! empty( $added_widgets ) ) {
166
			return;
167
		}
168
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
169
		if ( ! empty( $removed_widgets ) ) {
170
			return;
171
		}
172
173
		if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) {
174
			/**
175
			 * Helps Sync log that a sidebar id got reordered
176
			 *
177
			 * @since 4.9.0
178
			 *
179
			 * @param string $sidebar, Sidebar id got changed
180
			 */
181
			do_action( 'jetpack_widget_reordered', $sidebar );
182
		}
183
184
	}
185
186
	function sync_sidebar_widgets_actions( $old_value, $new_value ) {
187
188
		// Don't really know how to deal with different array_values yet.
189
		if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) {
190
			return;
191
		}
192
193
		$moved_to_inactive = array();
194
		$moved_to_sidebar = array();
195
196
		foreach ( $new_value as $sidebar => $new_widgets ) {
197
			if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) {
198
				continue;
199
			}
200
			$old_widgets = $old_value[ $sidebar ];
201
202
			$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] );
203
			$moved_to_inactive = array_merge( $moved_to_inactive, $moved_to_inactive_recently );
204
205
206
			$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar );
207
			$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently );
208
209
			$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar );
210
211
		}
212
213
		// Treat inactive sidebar a bit differently
214
		if ( ! empty( $moved_to_inactive ) ) {
215
			/**
216
			 * Helps Sync log that a widgets IDs got moved to in active
217
			 *
218
			 * @since 4.9.0
219
			 *
220
			 * @param array $sidebar, Sidebar id got changed
221
			 */
222
			do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive );
223
		} elseif ( empty( $moved_to_sidebar ) &&
224
		           empty( $new_value['wp_inactive_widgets']) &&
225
		           ! empty( $old_value['wp_inactive_widgets'] ) ) {
226
			/**
227
			 * Helps Sync log that a got cleared from inactive.
228
			 *
229
			 * @since 4.9.0
230
			 */
231
			do_action( 'jetpack_cleared_inactive_widgets' );
232
		} 
233
	}
234
235
	private function get_theme_support_info() {
236
		global $_wp_theme_features;
237
238
		$theme_support = array();
239
240
		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...
241
			$has_support = current_theme_supports( $theme_feature );
242
			if ( $has_support ) {
243
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
244
			}
245
		}
246
247
		$theme = wp_get_theme();
248
		$theme_support['name'] = $theme->name;
249
		$theme_support['version'] =  $theme->version;
250
251
		return $theme_support;
252
	}
253
}
254