Completed
Push — add/activity_theme ( 3c69ba...ac1d2f )
by
unknown
31:26 queued 22:02
created

get_delete_theme_call()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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