Completed
Push — master-stable ( ea10e6...53f101 )
by
unknown
26:27 queued 18:04
created

detect_theme_deletion()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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