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