| 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, 3 ); |
| 10 |
add_action( 'jetpack_sync_current_theme_support', $callable, 10, 2 ); |
| 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_themes', $callable, 10, 2 ); |
| 14 |
add_action( 'delete_site_transient_update_themes', array( $this, 'detect_theme_deletion') ); |
| 15 |
add_action( 'jetpack_deleted_theme', $callable, 10, 2 ); |
| 16 |
add_filter( 'wp_redirect', array( $this, 'detect_theme_edit' ) ); |
| 17 |
add_action( 'jetpack_edited_theme', $callable, 10, 2 ); |
| 18 |
add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'theme_edit_ajax' ), 0 ); |
| 19 |
add_action( 'update_site_option_allowedthemes', array( $this, 'sync_network_allowed_themes_change' ), 10, 4 ); |
| 20 |
add_action( 'jetpack_network_disabled_themes', $callable, 10, 2 ); |
| 21 |
add_action( 'jetpack_network_enabled_themes', $callable, 10, 2 ); |
| 22 |
|
| 23 |
// Sidebar updates. |
| 24 |
add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 ); |
| 25 |
|
| 26 |
add_action( 'jetpack_widget_added', $callable, 10, 4 ); |
| 27 |
add_action( 'jetpack_widget_removed', $callable, 10, 4 ); |
| 28 |
add_action( 'jetpack_widget_moved_to_inactive', $callable, 10, 2 ); |
| 29 |
add_action( 'jetpack_cleared_inactive_widgets', $callable ); |
| 30 |
add_action( 'jetpack_widget_reordered', $callable, 10, 2 ); |
| 31 |
add_filter( 'widget_update_callback', array( $this, 'sync_widget_edit' ), 10, 4 ); |
| 32 |
add_action( 'jetpack_widget_edited', $callable ); |
| 33 |
} |
| 34 |
|
| 35 |
public function sync_widget_edit( $instance, $new_instance, $old_instance, $widget_object ) { |
| 36 |
if ( empty( $old_instance ) ) { |
| 37 |
return $instance; |
| 38 |
} |
| 39 |
|
| 40 |
// Don't trigger sync action if this is an ajax request, because Customizer makes them during preview before saving changes |
| 41 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['customized'] ) ) { |
| 42 |
return $instance; |
| 43 |
} |
| 44 |
|
| 45 |
$widget = array( |
| 46 |
'name' => $widget_object->name, |
| 47 |
'id' => $widget_object->id, |
| 48 |
'title' => isset( $new_instance['title'] ) ? $new_instance['title'] : '', |
| 49 |
); |
| 50 |
/** |
| 51 |
* Trigger action to alert $callable sync listener that a widget was edited |
| 52 |
* |
| 53 |
* @since 5.0.0 |
| 54 |
* |
| 55 |
* @param string $widget_name , Name of edited widget |
| 56 |
*/ |
| 57 |
do_action( 'jetpack_widget_edited', $widget ); |
| 58 |
|
| 59 |
return $instance; |
| 60 |
} |
| 61 |
|
| 62 |
public function sync_network_allowed_themes_change( $option, $value, $old_value, $network_id ) { |
| 63 |
$all_enabled_theme_slugs = array_keys( $value ); |
| 64 |
|
| 65 |
if ( count( $old_value ) > count( $value ) ) { |
| 66 |
|
| 67 |
//Suppress jetpack_network_disabled_themes sync action when theme is deleted |
| 68 |
$delete_theme_call = $this->get_delete_theme_call(); |
| 69 |
if ( ! empty( $delete_theme_call ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$newly_disabled_theme_names = array_keys( array_diff_key( $old_value, $value ) ); |
| 74 |
$newly_disabled_themes = $this->get_theme_details_for_slugs( $newly_disabled_theme_names ); |
| 75 |
/** |
| 76 |
* Trigger action to alert $callable sync listener that network themes were disabled |
| 77 |
* |
| 78 |
* @since 5.0.0 |
| 79 |
* |
| 80 |
* @param mixed $newly_disabled_themes, Array of info about network disabled themes |
| 81 |
* @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes |
| 82 |
*/ |
| 83 |
do_action( 'jetpack_network_disabled_themes', $newly_disabled_themes, $all_enabled_theme_slugs ); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$newly_enabled_theme_names = array_keys( array_diff_key( $value, $old_value ) ); |
| 88 |
$newly_enabled_themes = $this->get_theme_details_for_slugs( $newly_enabled_theme_names ); |
| 89 |
/** |
| 90 |
* Trigger action to alert $callable sync listener that network themes were enabled |
| 91 |
* |
| 92 |
* @since 5.0.0 |
| 93 |
* |
| 94 |
* @param mixed $newly_enabled_themes , Array of info about network enabled themes |
| 95 |
* @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes |
| 96 |
*/ |
| 97 |
do_action( 'jetpack_network_enabled_themes', $newly_enabled_themes, $all_enabled_theme_slugs ); |
| 98 |
} |
| 99 |
|
| 100 |
private function get_theme_details_for_slugs( $theme_slugs ) { |
| 101 |
$theme_data = array(); |
| 102 |
foreach ( $theme_slugs as $slug ) { |
| 103 |
$theme = wp_get_theme( $slug ); |
| 104 |
$theme_data[ $slug ] = array( |
| 105 |
'name' => $theme->get( 'Name' ), |
| 106 |
'version' => $theme->get( 'Version' ), |
| 107 |
'uri' => $theme->get( 'ThemeURI' ), |
| 108 |
'slug' => $slug, |
| 109 |
); |
| 110 |
} |
| 111 |
return $theme_data; |
| 112 |
} |
| 113 |
|
| 114 |
public function detect_theme_edit( $redirect_url ) { |
| 115 |
$url = wp_parse_url( admin_url( $redirect_url ) ); |
| 116 |
$theme_editor_url = wp_parse_url( admin_url( 'theme-editor.php' ) ); |
| 117 |
|
| 118 |
if ( $theme_editor_url['path'] !== $url['path'] ) { |
| 119 |
return $redirect_url; |
| 120 |
} |
| 121 |
|
| 122 |
$query_params = array(); |
| 123 |
wp_parse_str( $url['query'], $query_params ); |
| 124 |
if ( |
| 125 |
! isset( $_POST['newcontent'] ) || |
| 126 |
! isset( $query_params['file'] ) || |
| 127 |
! isset( $query_params['theme'] ) || |
| 128 |
! isset( $query_params['updated'] ) |
| 129 |
) { |
| 130 |
return $redirect_url; |
| 131 |
} |
| 132 |
$theme = wp_get_theme( $query_params['theme'] ); |
| 133 |
$theme_data = array( |
| 134 |
'name' => $theme->get('Name'), |
| 135 |
'version' => $theme->get('Version'), |
| 136 |
'uri' => $theme->get( 'ThemeURI' ), |
| 137 |
); |
| 138 |
|
| 139 |
/** |
| 140 |
* Trigger action to alert $callable sync listener that a theme was edited |
| 141 |
* |
| 142 |
* @since 5.0.0 |
| 143 |
* |
| 144 |
* @param string $query_params['theme'], Slug of edited theme |
| 145 |
* @param string $theme_data, Information about edited them |
| 146 |
*/ |
| 147 |
do_action( 'jetpack_edited_theme', $query_params['theme'], $theme_data ); |
| 148 |
|
| 149 |
return $redirect_url; |
| 150 |
} |
| 151 |
|
| 152 |
public function theme_edit_ajax() { |
| 153 |
$args = wp_unslash( $_POST ); |
| 154 |
|
| 155 |
if ( empty( $args['theme'] ) ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
if ( empty( $args['file'] ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
$file = $args['file']; |
| 163 |
if ( 0 !== validate_file( $file ) ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if ( ! isset( $args['newcontent'] ) ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if ( ! isset( $args['nonce'] ) ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$stylesheet = $args['theme']; |
| 176 |
if ( 0 !== validate_file( $stylesheet ) ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! current_user_can( 'edit_themes' ) ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$theme = wp_get_theme( $stylesheet ); |
| 185 |
if ( ! $theme->exists() ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$real_file = $theme->get_stylesheet_directory() . '/' . $file; |
| 190 |
if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $real_file . $stylesheet ) ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
$editable_extensions = wp_get_theme_file_editable_extensions( $theme ); |
| 199 |
|
| 200 |
$allowed_files = array(); |
| 201 |
foreach ( $editable_extensions as $type ) { |
| 202 |
switch ( $type ) { |
| 203 |
case 'php': |
| 204 |
$allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) ); |
| 205 |
break; |
| 206 |
case 'css': |
| 207 |
$style_files = $theme->get_files( 'css', -1 ); |
| 208 |
$allowed_files['style.css'] = $style_files['style.css']; |
| 209 |
$allowed_files = array_merge( $allowed_files, $style_files ); |
| 210 |
break; |
| 211 |
default: |
| 212 |
$allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) ); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if ( 0 !== validate_file( $real_file, $allowed_files ) ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
// Ensure file is real. |
| 222 |
if ( ! is_file( $real_file ) ) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
// Ensure file extension is allowed. |
| 227 |
$extension = null; |
| 228 |
if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) { |
| 229 |
$extension = strtolower( $matches[1] ); |
| 230 |
if ( ! in_array( $extension, $editable_extensions, true ) ) { |
| 231 |
return; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if ( ! is_writeable( $real_file ) ) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
$file_pointer = fopen( $real_file, 'w+' ); |
| 240 |
if ( false === $file_pointer ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
fclose( $file_pointer ); |
| 244 |
|
| 245 |
$theme_data = array( |
| 246 |
'name' => $theme->get('Name'), |
| 247 |
'version' => $theme->get('Version'), |
| 248 |
'uri' => $theme->get( 'ThemeURI' ), |
| 249 |
); |
| 250 |
|
| 251 |
/** |
| 252 |
* This action is documented already in this file |
| 253 |
*/ |
| 254 |
do_action( 'jetpack_edited_theme', $stylesheet, $theme_data ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
public function detect_theme_deletion() { |
| 259 |
$delete_theme_call = $this->get_delete_theme_call(); |
| 260 |
if ( empty( $delete_theme_call ) ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$slug = $delete_theme_call['args'][0]; |
| 265 |
$theme = wp_get_theme( $slug ); |
| 266 |
$theme_data = array( |
| 267 |
'name' => $theme->get('Name'), |
| 268 |
'version' => $theme->get('Version'), |
| 269 |
'uri' => $theme->get( 'ThemeURI' ), |
| 270 |
'slug' => $slug, |
| 271 |
); |
| 272 |
|
| 273 |
/** |
| 274 |
* Signals to the sync listener that a theme was deleted and a sync action |
| 275 |
* reflecting the deletion and theme slug should be sent |
| 276 |
* |
| 277 |
* @since 5.0.0 |
| 278 |
* |
| 279 |
* @param string $slug Theme slug |
| 280 |
* @param array $theme_data Theme info Since 5.3 |
| 281 |
*/ |
| 282 |
do_action( 'jetpack_deleted_theme', $slug, $theme_data ); |
| 283 |
} |
| 284 |
|
| 285 |
public function check_upgrader( $upgrader, $details ) { |
| 286 |
if ( ! isset( $details['type'] ) || |
| 287 |
'theme' !== $details['type'] || |
| 288 |
is_wp_error( $upgrader->skin->result ) || |
| 289 |
! method_exists( $upgrader, 'theme_info' ) |
| 290 |
) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if ( 'install' === $details['action'] ) { |
| 295 |
$theme = $upgrader->theme_info(); |
| 296 |
if ( ! $theme instanceof WP_Theme ) { |
| 297 |
return; |
| 298 |
} |
| 299 |
$theme_info = array( |
| 300 |
'name' => $theme->get( 'Name' ), |
| 301 |
'version' => $theme->get( 'Version' ), |
| 302 |
'uri' => $theme->get( 'ThemeURI' ), |
| 303 |
); |
| 304 |
|
| 305 |
/** |
| 306 |
* Signals to the sync listener that a theme was installed and a sync action |
| 307 |
* reflecting the installation and the theme info should be sent |
| 308 |
* |
| 309 |
* @since 4.9.0 |
| 310 |
* |
| 311 |
* @param string $theme->theme_root Text domain of the theme |
| 312 |
* @param mixed $theme_info Array of abbreviated theme info |
| 313 |
*/ |
| 314 |
do_action( 'jetpack_installed_theme', $theme->stylesheet, $theme_info ); |
| 315 |
} |
| 316 |
|
| 317 |
if ( 'update' === $details['action'] ) { |
| 318 |
$themes = array(); |
| 319 |
|
| 320 |
if ( empty( $details['themes'] ) && isset ( $details['theme'] ) ) { |
| 321 |
$details['themes'] = array( $details['theme'] ); |
| 322 |
} |
| 323 |
|
| 324 |
foreach ( $details['themes'] as $theme_slug ) { |
| 325 |
$theme = wp_get_theme( $theme_slug ); |
| 326 |
|
| 327 |
if ( ! $theme instanceof WP_Theme ) { |
| 328 |
continue; |
| 329 |
} |
| 330 |
|
| 331 |
$themes[ $theme_slug ] = array( |
| 332 |
'name' => $theme->get( 'Name' ), |
| 333 |
'version' => $theme->get( 'Version' ), |
| 334 |
'uri' => $theme->get( 'ThemeURI' ), |
| 335 |
'stylesheet' => $theme->stylesheet, |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
if ( empty( $themes ) ) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Signals to the sync listener that one or more themes was updated and a sync action |
| 345 |
* reflecting the update and the theme info should be sent |
| 346 |
* |
| 347 |
* @since 6.2.0 |
| 348 |
* |
| 349 |
* @param mixed $themes Array of abbreviated theme info |
| 350 |
*/ |
| 351 |
do_action( 'jetpack_updated_themes', $themes ); |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
public function init_full_sync_listeners( $callable ) { |
| 357 |
add_action( 'jetpack_full_sync_theme_data', $callable ); |
| 358 |
} |
| 359 |
|
| 360 |
public function sync_theme_support( $new_name, $new_theme = null, $old_theme = null ) { |
| 361 |
// Previous theme support got added in WP 4.5 |
| 362 |
$previous_theme = false; |
| 363 |
if ( $old_theme instanceof WP_Theme ) { |
| 364 |
$previous_theme = $this->get_theme_support_info( $old_theme ); |
| 365 |
} |
| 366 |
/** |
| 367 |
* Fires when the client needs to sync theme support info |
| 368 |
* Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist |
| 369 |
* |
| 370 |
* @since 4.2.0 |
| 371 |
* |
| 372 |
* @param array the theme support array |
| 373 |
* @param array the previous theme since Jetpack 6.5.0 |
| 374 |
*/ |
| 375 |
do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info(), $previous_theme ); |
| 376 |
} |
| 377 |
|
| 378 |
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
| 379 |
/** |
| 380 |
* Tells the client to sync all theme data to the server |
| 381 |
* |
| 382 |
* @since 4.2.0 |
| 383 |
* |
| 384 |
* @param boolean Whether to expand theme data (should always be true) |
| 385 |
*/ |
| 386 |
do_action( 'jetpack_full_sync_theme_data', true ); |
| 387 |
|
| 388 |
// The number of actions enqueued, and next module state (true == done) |
| 389 |
return array( 1, true ); |
| 390 |
} |
| 391 |
|
| 392 |
public function estimate_full_sync_actions( $config ) { |
| 393 |
return 1; |
| 394 |
} |
| 395 |
|
| 396 |
public function init_before_send() { |
| 397 |
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) ); |
| 398 |
} |
| 399 |
|
| 400 |
function get_full_sync_actions() { |
| 401 |
return array( 'jetpack_full_sync_theme_data' ); |
| 402 |
} |
| 403 |
|
| 404 |
function expand_theme_data() { |
| 405 |
return array( $this->get_theme_support_info() ); |
| 406 |
} |
| 407 |
|
| 408 |
function get_widget_name( $widget_id ) { |
| 409 |
global $wp_registered_widgets; |
| 410 |
return ( isset( $wp_registered_widgets[ $widget_id ] ) ? $wp_registered_widgets[ $widget_id ]['name'] : null ); |
| 411 |
} |
| 412 |
|
| 413 |
function get_sidebar_name( $sidebar_id ) { |
| 414 |
global $wp_registered_sidebars; |
| 415 |
return ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ? $wp_registered_sidebars[ $sidebar_id ]['name'] : null ); |
| 416 |
} |
| 417 |
|
| 418 |
function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) { |
| 419 |
$added_widgets = array_diff( $new_widgets, $old_widgets ); |
| 420 |
if ( empty( $added_widgets ) ) { |
| 421 |
return array(); |
| 422 |
} |
| 423 |
$moved_to_sidebar = array(); |
| 424 |
$sidebar_name = $this->get_sidebar_name( $sidebar ); |
| 425 |
|
| 426 |
//Don't sync jetpack_widget_added if theme was switched |
| 427 |
if ( $this->is_theme_switch() ) { |
| 428 |
return array(); |
| 429 |
} |
| 430 |
|
| 431 |
foreach ( $added_widgets as $added_widget ) { |
| 432 |
$moved_to_sidebar[] = $added_widget; |
| 433 |
$added_widget_name = $this->get_widget_name( $added_widget ); |
| 434 |
/** |
| 435 |
* Helps Sync log that a widget got added |
| 436 |
* |
| 437 |
* @since 4.9.0 |
| 438 |
* |
| 439 |
* @param string $sidebar, Sidebar id got changed |
| 440 |
* @param string $added_widget, Widget id got added |
| 441 |
* @param string $sidebar_name, Sidebar id got changed Since 5.0.0 |
| 442 |
* @param string $added_widget_name, Widget id got added Since 5.0.0 |
| 443 |
* |
| 444 |
*/ |
| 445 |
do_action( 'jetpack_widget_added', $sidebar, $added_widget, $sidebar_name, $added_widget_name ); |
| 446 |
} |
| 447 |
return $moved_to_sidebar; |
| 448 |
} |
| 449 |
|
| 450 |
function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets ) { |
| 451 |
$removed_widgets = array_diff( $old_widgets, $new_widgets ); |
| 452 |
|
| 453 |
if ( empty( $removed_widgets ) ) { |
| 454 |
return array(); |
| 455 |
} |
| 456 |
|
| 457 |
$moved_to_inactive = array(); |
| 458 |
$sidebar_name = $this->get_sidebar_name( $sidebar ); |
| 459 |
|
| 460 |
foreach( $removed_widgets as $removed_widget ) { |
| 461 |
// Lets check if we didn't move the widget to in_active_widgets |
| 462 |
if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) { |
| 463 |
$removed_widget_name = $this->get_widget_name( $removed_widget ); |
| 464 |
/** |
| 465 |
* Helps Sync log that a widgte got removed |
| 466 |
* |
| 467 |
* @since 4.9.0 |
| 468 |
* |
| 469 |
* @param string $sidebar, Sidebar id got changed |
| 470 |
* @param string $removed_widget, Widget id got removed |
| 471 |
* @param string $sidebar_name, Name of the sidebar that changed Since 5.0.0 |
| 472 |
* @param string $removed_widget_name, Name of the widget that got removed Since 5.0.0 |
| 473 |
*/ |
| 474 |
do_action( 'jetpack_widget_removed', $sidebar, $removed_widget, $sidebar_name, $removed_widget_name ); |
| 475 |
} else { |
| 476 |
$moved_to_inactive[] = $removed_widget; |
| 477 |
} |
| 478 |
} |
| 479 |
return $moved_to_inactive; |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) { |
| 484 |
$added_widgets = array_diff( $new_widgets, $old_widgets ); |
| 485 |
if ( ! empty( $added_widgets ) ) { |
| 486 |
return; |
| 487 |
} |
| 488 |
$removed_widgets = array_diff( $old_widgets, $new_widgets ); |
| 489 |
if ( ! empty( $removed_widgets ) ) { |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) { |
| 494 |
$sidebar_name = $this->get_sidebar_name( $sidebar ); |
| 495 |
/** |
| 496 |
* Helps Sync log that a sidebar id got reordered |
| 497 |
* |
| 498 |
* @since 4.9.0 |
| 499 |
* |
| 500 |
* @param string $sidebar, Sidebar id got changed |
| 501 |
* @param string $sidebar_name, Name of the sidebar that changed Since 5.0.0 |
| 502 |
*/ |
| 503 |
do_action( 'jetpack_widget_reordered', $sidebar, $sidebar_name ); |
| 504 |
} |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
function sync_sidebar_widgets_actions( $old_value, $new_value ) { |
| 509 |
// Don't really know how to deal with different array_values yet. |
| 510 |
if ( |
| 511 |
( isset( $old_value['array_version'] ) && $old_value['array_version'] !== 3 ) || |
| 512 |
( isset( $new_value['array_version'] ) && $new_value['array_version'] !== 3 ) |
| 513 |
) { |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
$moved_to_inactive_ids = array(); |
| 518 |
$moved_to_sidebar = array(); |
| 519 |
|
| 520 |
foreach ( $new_value as $sidebar => $new_widgets ) { |
| 521 |
if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) { |
| 522 |
continue; |
| 523 |
} |
| 524 |
$old_widgets = isset( $old_value[ $sidebar ] ) |
| 525 |
? $old_value[ $sidebar ] |
| 526 |
: array(); |
| 527 |
|
| 528 |
if ( ! is_array( $new_widgets ) ) { |
| 529 |
$new_widgets = array(); |
| 530 |
} |
| 531 |
|
| 532 |
$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] ); |
| 533 |
$moved_to_inactive_ids = array_merge( $moved_to_inactive_ids, $moved_to_inactive_recently ); |
| 534 |
|
| 535 |
$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ); |
| 536 |
$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently ); |
| 537 |
|
| 538 |
$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ); |
| 539 |
|
| 540 |
} |
| 541 |
|
| 542 |
//Don't sync either jetpack_widget_moved_to_inactive or jetpack_cleared_inactive_widgets if theme was switched |
| 543 |
if ( $this->is_theme_switch() ) { |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
// Treat inactive sidebar a bit differently |
| 548 |
if ( ! empty( $moved_to_inactive_ids ) ) { |
| 549 |
$moved_to_inactive_name = array_map( array( $this, 'get_widget_name' ), $moved_to_inactive_ids ); |
| 550 |
/** |
| 551 |
* Helps Sync log that a widgets IDs got moved to in active |
| 552 |
* |
| 553 |
* @since 4.9.0 |
| 554 |
* |
| 555 |
* @param array $moved_to_inactive_ids, Array of widgets id that moved to inactive id got changed |
| 556 |
* @param array $moved_to_inactive_names, Array of widgets names that moved to inactive id got changed Since 5.0.0 |
| 557 |
*/ |
| 558 |
do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive_ids, $moved_to_inactive_name ); |
| 559 |
} elseif ( empty( $moved_to_sidebar ) && |
| 560 |
empty( $new_value['wp_inactive_widgets']) && |
| 561 |
! empty( $old_value['wp_inactive_widgets'] ) ) { |
| 562 |
/** |
| 563 |
* Helps Sync log that a got cleared from inactive. |
| 564 |
* |
| 565 |
* @since 4.9.0 |
| 566 |
*/ |
| 567 |
do_action( 'jetpack_cleared_inactive_widgets' ); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* @param null $theme or the theme object |
| 573 |
* |
| 574 |
* @return array |
| 575 |
*/ |
| 576 |
private function get_theme_support_info( $theme = null ) { |
| 577 |
global $_wp_theme_features; |
| 578 |
|
| 579 |
$theme_support = array(); |
| 580 |
|
| 581 |
// We are trying to get the current theme info. |
| 582 |
if ( $theme === null ) { |
| 583 |
$theme = wp_get_theme(); |
| 584 |
|
| 585 |
foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) { |
| 586 |
$has_support = current_theme_supports( $theme_feature ); |
| 587 |
if ( $has_support ) { |
| 588 |
$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
$theme_support['name'] = $theme->get('Name'); |
| 594 |
$theme_support['version'] = $theme->get('Version'); |
| 595 |
$theme_support['slug'] = $theme->get_stylesheet(); |
| 596 |
$theme_support['uri'] = $theme->get('ThemeURI'); |
| 597 |
|
| 598 |
return $theme_support; |
| 599 |
} |
| 600 |
|
| 601 |
private function get_delete_theme_call() { |
| 602 |
$backtrace = debug_backtrace(); |
| 603 |
$delete_theme_call = null; |
| 604 |
foreach ( $backtrace as $call ) { |
| 605 |
if ( isset( $call['function'] ) && 'delete_theme' === $call['function'] ) { |
| 606 |
$delete_theme_call = $call; |
| 607 |
break; |
| 608 |
} |
| 609 |
} |
| 610 |
return $delete_theme_call; |
| 611 |
} |
| 612 |
|
| 613 |
private function is_theme_switch() { |
| 614 |
return did_action( 'after_switch_theme' ); |
| 615 |
} |
| 616 |
} |