Completed
Push — add/widget_modified ( 16ccc0 )
by
unknown
10:38
created

Jetpack_Sync_Module_Themes::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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