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