Completed
Push — add/delete_theme ( 4f2c5c )
by
unknown
12:42 queued 02:44
created

detect_theme_deletion()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 0
dl 0
loc 15
rs 8.8571
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
		add_action( 'delete_site_transient_update_themes', array($this, 'detect_theme_deletion') );
15
16
		// Sidebar updates.
17
		add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 );
18
		add_action( 'jetpack_widget_added', $callable, 10, 2 );
19
		add_action( 'jetpack_widget_removed', $callable, 10, 2 );
20
		add_action( 'jetpack_widget_moved_to_inactive', $callable );
21
		add_action( 'jetpack_cleared_inactive_widgets', $callable );
22
		add_action( 'jetpack_widget_reordered', $callable );
23
	}
24
25
	public function detect_theme_deletion() {
26
		$backtrace = debug_backtrace();
27
		$delete_theme_call = null;
28
		foreach ( $backtrace as $call ) {
29
			if ( isset( $call['function'] ) && 'delete_theme' === $call['function'] ) {
30
				$delete_theme_call = $call;
31
				break;
32
			}
33
		}
34
		if ( empty( $delete_theme_call ) ) {
35
			return;
36
		}
37
		error_log(print_r($delete_theme_call, true));
38
		$name = $delete_theme_call['args'][0];
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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