Completed
Push — add/activity_theme ( 0b81a9...3fa738 )
by
unknown
44:52 queued 35:12
created

Jetpack_Sync_Module_Themes::get_widget_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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