Completed
Push — add/sync-edit-theme ( 9ba294...f03301 )
by
unknown
24:26 queued 16:31
created

Jetpack_Sync_Module_Themes::detect_theme_edit()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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