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