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