Completed
Push — branch-4.9-built ( f63861...2af77d )
by
unknown
751:45 queued 732:48
created

Jetpack_Sync_Module_Themes   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 237
Duplicated Lines 2.95 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 237
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 14 1
B check_upgrader() 7 29 6
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   

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:

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