[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Customize Widgets classes 4 * 5 * @package WordPress 6 * @subpackage Customize 7 * @since 3.9.0 8 */ 9 10 /** 11 * Customize Widgets class. 12 * 13 * Implements widget management in the Customizer. 14 * 15 * @since 3.9.0 16 * 17 * @see WP_Customize_Manager 18 */ 19 final class WP_Customize_Widgets { 20 21 /** 22 * WP_Customize_Manager instance. 23 * 24 * @since 3.9.0 25 * @var WP_Customize_Manager 26 */ 27 public $manager; 28 29 /** 30 * All id_bases for widgets defined in core. 31 * 32 * @since 3.9.0 33 * @var array 34 */ 35 protected $core_widget_id_bases = array( 36 'archives', 37 'calendar', 38 'categories', 39 'custom_html', 40 'links', 41 'media_audio', 42 'media_image', 43 'media_video', 44 'meta', 45 'nav_menu', 46 'pages', 47 'recent-comments', 48 'recent-posts', 49 'rss', 50 'search', 51 'tag_cloud', 52 'text', 53 ); 54 55 /** 56 * @since 3.9.0 57 * @var array 58 */ 59 protected $rendered_sidebars = array(); 60 61 /** 62 * @since 3.9.0 63 * @var array 64 */ 65 protected $rendered_widgets = array(); 66 67 /** 68 * @since 3.9.0 69 * @var array 70 */ 71 protected $old_sidebars_widgets = array(); 72 73 /** 74 * Mapping of widget ID base to whether it supports selective refresh. 75 * 76 * @since 4.5.0 77 * @var array 78 */ 79 protected $selective_refreshable_widgets; 80 81 /** 82 * Mapping of setting type to setting ID pattern. 83 * 84 * @since 4.2.0 85 * @var array 86 */ 87 protected $setting_id_patterns = array( 88 'widget_instance' => '/^widget_(?P<id_base>.+?)(?:\[(?P<widget_number>\d+)\])?$/', 89 'sidebar_widgets' => '/^sidebars_widgets\[(?P<sidebar_id>.+?)\]$/', 90 ); 91 92 /** 93 * Initial loader. 94 * 95 * @since 3.9.0 96 * 97 * @param WP_Customize_Manager $manager Customizer bootstrap instance. 98 */ 99 public function __construct( $manager ) { 100 $this->manager = $manager; 101 102 // See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L420-L449 103 add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 ); 104 add_action( 'widgets_init', array( $this, 'register_settings' ), 95 ); 105 add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); 106 107 // Skip remaining hooks when the user can't manage widgets anyway. 108 if ( ! current_user_can( 'edit_theme_options' ) ) { 109 return; 110 } 111 112 add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); 113 add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); 114 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 115 add_action( 'customize_controls_print_styles', array( $this, 'print_styles' ) ); 116 add_action( 'customize_controls_print_scripts', array( $this, 'print_scripts' ) ); 117 add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_footer_scripts' ) ); 118 add_action( 'customize_controls_print_footer_scripts', array( $this, 'output_widget_control_templates' ) ); 119 add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); 120 add_filter( 'customize_refresh_nonces', array( $this, 'refresh_nonces' ) ); 121 add_filter( 'should_load_block_editor_scripts_and_styles', array( $this, 'should_load_block_editor_scripts_and_styles' ) ); 122 123 add_action( 'dynamic_sidebar', array( $this, 'tally_rendered_widgets' ) ); 124 add_filter( 'is_active_sidebar', array( $this, 'tally_sidebars_via_is_active_sidebar_calls' ), 10, 2 ); 125 add_filter( 'dynamic_sidebar_has_widgets', array( $this, 'tally_sidebars_via_dynamic_sidebar_calls' ), 10, 2 ); 126 127 // Selective Refresh. 128 add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); 129 add_action( 'customize_preview_init', array( $this, 'selective_refresh_init' ) ); 130 } 131 132 /** 133 * List whether each registered widget can be use selective refresh. 134 * 135 * If the theme does not support the customize-selective-refresh-widgets feature, 136 * then this will always return an empty array. 137 * 138 * @since 4.5.0 139 * 140 * @global WP_Widget_Factory $wp_widget_factory 141 * 142 * @return array Mapping of id_base to support. If theme doesn't support 143 * selective refresh, an empty array is returned. 144 */ 145 public function get_selective_refreshable_widgets() { 146 global $wp_widget_factory; 147 if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 148 return array(); 149 } 150 if ( ! isset( $this->selective_refreshable_widgets ) ) { 151 $this->selective_refreshable_widgets = array(); 152 foreach ( $wp_widget_factory->widgets as $wp_widget ) { 153 $this->selective_refreshable_widgets[ $wp_widget->id_base ] = ! empty( $wp_widget->widget_options['customize_selective_refresh'] ); 154 } 155 } 156 return $this->selective_refreshable_widgets; 157 } 158 159 /** 160 * Determines if a widget supports selective refresh. 161 * 162 * @since 4.5.0 163 * 164 * @param string $id_base Widget ID Base. 165 * @return bool Whether the widget can be selective refreshed. 166 */ 167 public function is_widget_selective_refreshable( $id_base ) { 168 $selective_refreshable_widgets = $this->get_selective_refreshable_widgets(); 169 return ! empty( $selective_refreshable_widgets[ $id_base ] ); 170 } 171 172 /** 173 * Retrieves the widget setting type given a setting ID. 174 * 175 * @since 4.2.0 176 * 177 * @param string $setting_id Setting ID. 178 * @return string|void Setting type. 179 */ 180 protected function get_setting_type( $setting_id ) { 181 static $cache = array(); 182 if ( isset( $cache[ $setting_id ] ) ) { 183 return $cache[ $setting_id ]; 184 } 185 foreach ( $this->setting_id_patterns as $type => $pattern ) { 186 if ( preg_match( $pattern, $setting_id ) ) { 187 $cache[ $setting_id ] = $type; 188 return $type; 189 } 190 } 191 } 192 193 /** 194 * Inspects the incoming customized data for any widget settings, and dynamically adds 195 * them up-front so widgets will be initialized properly. 196 * 197 * @since 4.2.0 198 */ 199 public function register_settings() { 200 $widget_setting_ids = array(); 201 $incoming_setting_ids = array_keys( $this->manager->unsanitized_post_values() ); 202 foreach ( $incoming_setting_ids as $setting_id ) { 203 if ( ! is_null( $this->get_setting_type( $setting_id ) ) ) { 204 $widget_setting_ids[] = $setting_id; 205 } 206 } 207 if ( $this->manager->doing_ajax( 'update-widget' ) && isset( $_REQUEST['widget-id'] ) ) { 208 $widget_setting_ids[] = $this->get_setting_id( wp_unslash( $_REQUEST['widget-id'] ) ); 209 } 210 211 $settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) ); 212 213 if ( $this->manager->settings_previewed() ) { 214 foreach ( $settings as $setting ) { 215 $setting->preview(); 216 } 217 } 218 } 219 220 /** 221 * Determines the arguments for a dynamically-created setting. 222 * 223 * @since 4.2.0 224 * 225 * @param false|array $args The arguments to the WP_Customize_Setting constructor. 226 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. 227 * @return array|false Setting arguments, false otherwise. 228 */ 229 public function filter_customize_dynamic_setting_args( $args, $setting_id ) { 230 if ( $this->get_setting_type( $setting_id ) ) { 231 $args = $this->get_setting_args( $setting_id ); 232 } 233 return $args; 234 } 235 236 /** 237 * Retrieves an unslashed post value or return a default. 238 * 239 * @since 3.9.0 240 * 241 * @param string $name Post value. 242 * @param mixed $default_value Default post value. 243 * @return mixed Unslashed post value or default value. 244 */ 245 protected function get_post_value( $name, $default_value = null ) { 246 if ( ! isset( $_POST[ $name ] ) ) { 247 return $default_value; 248 } 249 250 return wp_unslash( $_POST[ $name ] ); 251 } 252 253 /** 254 * Override sidebars_widgets for theme switch. 255 * 256 * When switching a theme via the Customizer, supply any previously-configured 257 * sidebars_widgets from the target theme as the initial sidebars_widgets 258 * setting. Also store the old theme's existing settings so that they can 259 * be passed along for storing in the sidebars_widgets theme_mod when the 260 * theme gets switched. 261 * 262 * @since 3.9.0 263 * 264 * @global array $sidebars_widgets 265 * @global array $_wp_sidebars_widgets 266 */ 267 public function override_sidebars_widgets_for_theme_switch() { 268 global $sidebars_widgets; 269 270 if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) { 271 return; 272 } 273 274 $this->old_sidebars_widgets = wp_get_sidebars_widgets(); 275 add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) ); 276 $this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset. 277 278 // retrieve_widgets() looks at the global $sidebars_widgets. 279 $sidebars_widgets = $this->old_sidebars_widgets; 280 $sidebars_widgets = retrieve_widgets( 'customize' ); 281 add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 ); 282 // Reset global cache var used by wp_get_sidebars_widgets(). 283 unset( $GLOBALS['_wp_sidebars_widgets'] ); 284 } 285 286 /** 287 * Filters old_sidebars_widgets_data Customizer setting. 288 * 289 * When switching themes, filter the Customizer setting old_sidebars_widgets_data 290 * to supply initial $sidebars_widgets before they were overridden by retrieve_widgets(). 291 * The value for old_sidebars_widgets_data gets set in the old theme's sidebars_widgets 292 * theme_mod. 293 * 294 * @since 3.9.0 295 * 296 * @see WP_Customize_Widgets::handle_theme_switch() 297 * 298 * @param array $old_sidebars_widgets 299 * @return array 300 */ 301 public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) { 302 return $this->old_sidebars_widgets; 303 } 304 305 /** 306 * Filters sidebars_widgets option for theme switch. 307 * 308 * When switching themes, the retrieve_widgets() function is run when the Customizer initializes, 309 * and then the new sidebars_widgets here get supplied as the default value for the sidebars_widgets 310 * option. 311 * 312 * @since 3.9.0 313 * 314 * @see WP_Customize_Widgets::handle_theme_switch() 315 * @global array $sidebars_widgets 316 * 317 * @param array $sidebars_widgets 318 * @return array 319 */ 320 public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) { 321 $sidebars_widgets = $GLOBALS['sidebars_widgets']; 322 $sidebars_widgets['array_version'] = 3; 323 return $sidebars_widgets; 324 } 325 326 /** 327 * Ensures all widgets get loaded into the Customizer. 328 * 329 * Note: these actions are also fired in wp_ajax_update_widget(). 330 * 331 * @since 3.9.0 332 */ 333 public function customize_controls_init() { 334 /** This action is documented in wp-admin/includes/ajax-actions.php */ 335 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 336 337 /** This action is documented in wp-admin/includes/ajax-actions.php */ 338 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 339 340 /** This action is documented in wp-admin/widgets.php */ 341 do_action( 'sidebar_admin_setup' ); 342 } 343 344 /** 345 * Ensures widgets are available for all types of previews. 346 * 347 * When in preview, hook to {@see 'customize_register'} for settings after WordPress is loaded 348 * so that all filters have been initialized (e.g. Widget Visibility). 349 * 350 * @since 3.9.0 351 */ 352 public function schedule_customize_register() { 353 if ( is_admin() ) { 354 $this->customize_register(); 355 } else { 356 add_action( 'wp', array( $this, 'customize_register' ) ); 357 } 358 } 359 360 /** 361 * Registers Customizer settings and controls for all sidebars and widgets. 362 * 363 * @since 3.9.0 364 * 365 * @global array $wp_registered_widgets 366 * @global array $wp_registered_widget_controls 367 * @global array $wp_registered_sidebars 368 */ 369 public function customize_register() { 370 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars; 371 372 $use_widgets_block_editor = wp_use_widgets_block_editor(); 373 374 add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); 375 376 $sidebars_widgets = array_merge( 377 array( 'wp_inactive_widgets' => array() ), 378 array_fill_keys( array_keys( $wp_registered_sidebars ), array() ), 379 wp_get_sidebars_widgets() 380 ); 381 382 $new_setting_ids = array(); 383 384 /* 385 * Register a setting for all widgets, including those which are active, 386 * inactive, and orphaned since a widget may get suppressed from a sidebar 387 * via a plugin (like Widget Visibility). 388 */ 389 foreach ( array_keys( $wp_registered_widgets ) as $widget_id ) { 390 $setting_id = $this->get_setting_id( $widget_id ); 391 $setting_args = $this->get_setting_args( $setting_id ); 392 if ( ! $this->manager->get_setting( $setting_id ) ) { 393 $this->manager->add_setting( $setting_id, $setting_args ); 394 } 395 $new_setting_ids[] = $setting_id; 396 } 397 398 /* 399 * Add a setting which will be supplied for the theme's sidebars_widgets 400 * theme_mod when the theme is switched. 401 */ 402 if ( ! $this->manager->is_theme_active() ) { 403 $setting_id = 'old_sidebars_widgets_data'; 404 $setting_args = $this->get_setting_args( 405 $setting_id, 406 array( 407 'type' => 'global_variable', 408 'dirty' => true, 409 ) 410 ); 411 $this->manager->add_setting( $setting_id, $setting_args ); 412 } 413 414 $this->manager->add_panel( 415 'widgets', 416 array( 417 'type' => 'widgets', 418 'title' => __( 'Widgets' ), 419 'description' => __( 'Widgets are independent sections of content that can be placed into widgetized areas provided by your theme (commonly called sidebars).' ), 420 'priority' => 110, 421 'active_callback' => array( $this, 'is_panel_active' ), 422 'auto_expand_sole_section' => true, 423 'theme_supports' => 'widgets', 424 ) 425 ); 426 427 foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) { 428 if ( empty( $sidebar_widget_ids ) ) { 429 $sidebar_widget_ids = array(); 430 } 431 432 $is_registered_sidebar = is_registered_sidebar( $sidebar_id ); 433 $is_inactive_widgets = ( 'wp_inactive_widgets' === $sidebar_id ); 434 $is_active_sidebar = ( $is_registered_sidebar && ! $is_inactive_widgets ); 435 436 // Add setting for managing the sidebar's widgets. 437 if ( $is_registered_sidebar || $is_inactive_widgets ) { 438 $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); 439 $setting_args = $this->get_setting_args( $setting_id ); 440 if ( ! $this->manager->get_setting( $setting_id ) ) { 441 if ( ! $this->manager->is_theme_active() ) { 442 $setting_args['dirty'] = true; 443 } 444 $this->manager->add_setting( $setting_id, $setting_args ); 445 } 446 $new_setting_ids[] = $setting_id; 447 448 // Add section to contain controls. 449 $section_id = sprintf( 'sidebar-widgets-%s', $sidebar_id ); 450 if ( $is_active_sidebar ) { 451 452 $section_args = array( 453 'title' => $wp_registered_sidebars[ $sidebar_id ]['name'], 454 'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ), true ), 455 'panel' => 'widgets', 456 'sidebar_id' => $sidebar_id, 457 ); 458 459 if ( $use_widgets_block_editor ) { 460 $section_args['description'] = ''; 461 } else { 462 $section_args['description'] = $wp_registered_sidebars[ $sidebar_id ]['description']; 463 } 464 465 /** 466 * Filters Customizer widget section arguments for a given sidebar. 467 * 468 * @since 3.9.0 469 * 470 * @param array $section_args Array of Customizer widget section arguments. 471 * @param string $section_id Customizer section ID. 472 * @param int|string $sidebar_id Sidebar ID. 473 */ 474 $section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id ); 475 476 $section = new WP_Customize_Sidebar_Section( $this->manager, $section_id, $section_args ); 477 $this->manager->add_section( $section ); 478 479 if ( $use_widgets_block_editor ) { 480 $control = new WP_Sidebar_Block_Editor_Control( 481 $this->manager, 482 $setting_id, 483 array( 484 'section' => $section_id, 485 'sidebar_id' => $sidebar_id, 486 'label' => $section_args['title'], 487 'description' => $section_args['description'], 488 ) 489 ); 490 } else { 491 $control = new WP_Widget_Area_Customize_Control( 492 $this->manager, 493 $setting_id, 494 array( 495 'section' => $section_id, 496 'sidebar_id' => $sidebar_id, 497 'priority' => count( $sidebar_widget_ids ), // place 'Add Widget' and 'Reorder' buttons at end. 498 ) 499 ); 500 } 501 502 $this->manager->add_control( $control ); 503 504 $new_setting_ids[] = $setting_id; 505 } 506 } 507 508 if ( ! $use_widgets_block_editor ) { 509 // Add a control for each active widget (located in a sidebar). 510 foreach ( $sidebar_widget_ids as $i => $widget_id ) { 511 512 // Skip widgets that may have gone away due to a plugin being deactivated. 513 if ( ! $is_active_sidebar || ! isset( $wp_registered_widgets[ $widget_id ] ) ) { 514 continue; 515 } 516 517 $registered_widget = $wp_registered_widgets[ $widget_id ]; 518 $setting_id = $this->get_setting_id( $widget_id ); 519 $id_base = $wp_registered_widget_controls[ $widget_id ]['id_base']; 520 521 $control = new WP_Widget_Form_Customize_Control( 522 $this->manager, 523 $setting_id, 524 array( 525 'label' => $registered_widget['name'], 526 'section' => $section_id, 527 'sidebar_id' => $sidebar_id, 528 'widget_id' => $widget_id, 529 'widget_id_base' => $id_base, 530 'priority' => $i, 531 'width' => $wp_registered_widget_controls[ $widget_id ]['width'], 532 'height' => $wp_registered_widget_controls[ $widget_id ]['height'], 533 'is_wide' => $this->is_wide_widget( $widget_id ), 534 ) 535 ); 536 $this->manager->add_control( $control ); 537 } 538 } 539 } 540 541 if ( $this->manager->settings_previewed() ) { 542 foreach ( $new_setting_ids as $new_setting_id ) { 543 $this->manager->get_setting( $new_setting_id )->preview(); 544 } 545 } 546 } 547 548 /** 549 * Determines whether the widgets panel is active, based on whether there are sidebars registered. 550 * 551 * @since 4.4.0 552 * 553 * @see WP_Customize_Panel::$active_callback 554 * 555 * @global array $wp_registered_sidebars 556 * @return bool Active. 557 */ 558 public function is_panel_active() { 559 global $wp_registered_sidebars; 560 return ! empty( $wp_registered_sidebars ); 561 } 562 563 /** 564 * Converts a widget_id into its corresponding Customizer setting ID (option name). 565 * 566 * @since 3.9.0 567 * 568 * @param string $widget_id Widget ID. 569 * @return string Maybe-parsed widget ID. 570 */ 571 public function get_setting_id( $widget_id ) { 572 $parsed_widget_id = $this->parse_widget_id( $widget_id ); 573 $setting_id = sprintf( 'widget_%s', $parsed_widget_id['id_base'] ); 574 575 if ( ! is_null( $parsed_widget_id['number'] ) ) { 576 $setting_id .= sprintf( '[%d]', $parsed_widget_id['number'] ); 577 } 578 return $setting_id; 579 } 580 581 /** 582 * Determines whether the widget is considered "wide". 583 * 584 * Core widgets which may have controls wider than 250, but can still be shown 585 * in the narrow Customizer panel. The RSS and Text widgets in Core, for example, 586 * have widths of 400 and yet they still render fine in the Customizer panel. 587 * 588 * This method will return all Core widgets as being not wide, but this can be 589 * overridden with the {@see 'is_wide_widget_in_customizer'} filter. 590 * 591 * @since 3.9.0 592 * 593 * @global array $wp_registered_widget_controls 594 * 595 * @param string $widget_id Widget ID. 596 * @return bool Whether or not the widget is a "wide" widget. 597 */ 598 public function is_wide_widget( $widget_id ) { 599 global $wp_registered_widget_controls; 600 601 $parsed_widget_id = $this->parse_widget_id( $widget_id ); 602 $width = $wp_registered_widget_controls[ $widget_id ]['width']; 603 $is_core = in_array( $parsed_widget_id['id_base'], $this->core_widget_id_bases, true ); 604 $is_wide = ( $width > 250 && ! $is_core ); 605 606 /** 607 * Filters whether the given widget is considered "wide". 608 * 609 * @since 3.9.0 610 * 611 * @param bool $is_wide Whether the widget is wide, Default false. 612 * @param string $widget_id Widget ID. 613 */ 614 return apply_filters( 'is_wide_widget_in_customizer', $is_wide, $widget_id ); 615 } 616 617 /** 618 * Converts a widget ID into its id_base and number components. 619 * 620 * @since 3.9.0 621 * 622 * @param string $widget_id Widget ID. 623 * @return array Array containing a widget's id_base and number components. 624 */ 625 public function parse_widget_id( $widget_id ) { 626 $parsed = array( 627 'number' => null, 628 'id_base' => null, 629 ); 630 631 if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $matches ) ) { 632 $parsed['id_base'] = $matches[1]; 633 $parsed['number'] = (int) $matches[2]; 634 } else { 635 // Likely an old single widget. 636 $parsed['id_base'] = $widget_id; 637 } 638 return $parsed; 639 } 640 641 /** 642 * Converts a widget setting ID (option path) to its id_base and number components. 643 * 644 * @since 3.9.0 645 * 646 * @param string $setting_id Widget setting ID. 647 * @return array|WP_Error Array containing a widget's id_base and number components, 648 * or a WP_Error object. 649 */ 650 public function parse_widget_setting_id( $setting_id ) { 651 if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { 652 return new WP_Error( 'widget_setting_invalid_id' ); 653 } 654 655 $id_base = $matches[2]; 656 $number = isset( $matches[3] ) ? (int) $matches[3] : null; 657 658 return compact( 'id_base', 'number' ); 659 } 660 661 /** 662 * Calls admin_print_styles-widgets.php and admin_print_styles hooks to 663 * allow custom styles from plugins. 664 * 665 * @since 3.9.0 666 */ 667 public function print_styles() { 668 /** This action is documented in wp-admin/admin-header.php */ 669 do_action( 'admin_print_styles-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 670 671 /** This action is documented in wp-admin/admin-header.php */ 672 do_action( 'admin_print_styles' ); 673 } 674 675 /** 676 * Calls admin_print_scripts-widgets.php and admin_print_scripts hooks to 677 * allow custom scripts from plugins. 678 * 679 * @since 3.9.0 680 */ 681 public function print_scripts() { 682 /** This action is documented in wp-admin/admin-header.php */ 683 do_action( 'admin_print_scripts-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 684 685 /** This action is documented in wp-admin/admin-header.php */ 686 do_action( 'admin_print_scripts' ); 687 } 688 689 /** 690 * Enqueues scripts and styles for Customizer panel and export data to JavaScript. 691 * 692 * @since 3.9.0 693 * 694 * @global WP_Scripts $wp_scripts 695 * @global array $wp_registered_sidebars 696 * @global array $wp_registered_widgets 697 */ 698 public function enqueue_scripts() { 699 global $wp_scripts, $wp_registered_sidebars, $wp_registered_widgets; 700 701 wp_enqueue_style( 'customize-widgets' ); 702 wp_enqueue_script( 'customize-widgets' ); 703 704 /** This action is documented in wp-admin/admin-header.php */ 705 do_action( 'admin_enqueue_scripts', 'widgets.php' ); 706 707 /* 708 * Export available widgets with control_tpl removed from model 709 * since plugins need templates to be in the DOM. 710 */ 711 $available_widgets = array(); 712 713 foreach ( $this->get_available_widgets() as $available_widget ) { 714 unset( $available_widget['control_tpl'] ); 715 $available_widgets[] = $available_widget; 716 } 717 718 $widget_reorder_nav_tpl = sprintf( 719 '<div class="widget-reorder-nav"><span class="move-widget" tabindex="0">%1$s</span><span class="move-widget-down" tabindex="0">%2$s</span><span class="move-widget-up" tabindex="0">%3$s</span></div>', 720 __( 'Move to another area…' ), 721 __( 'Move down' ), 722 __( 'Move up' ) 723 ); 724 725 $move_widget_area_tpl = str_replace( 726 array( '{description}', '{btn}' ), 727 array( 728 __( 'Select an area to move this widget into:' ), 729 _x( 'Move', 'Move widget' ), 730 ), 731 '<div class="move-widget-area"> 732 <p class="description">{description}</p> 733 <ul class="widget-area-select"> 734 <% _.each( sidebars, function ( sidebar ){ %> 735 <li class="" data-id="<%- sidebar.id %>" title="<%- sidebar.description %>" tabindex="0"><%- sidebar.name %></li> 736 <% }); %> 737 </ul> 738 <div class="move-widget-actions"> 739 <button class="move-widget-btn button" type="button">{btn}</button> 740 </div> 741 </div>' 742 ); 743 744 /* 745 * Gather all strings in PHP that may be needed by JS on the client. 746 * Once JS i18n is implemented (in #20491), this can be removed. 747 */ 748 $some_non_rendered_areas_messages = array(); 749 $some_non_rendered_areas_messages[1] = html_entity_decode( 750 __( 'Your theme has 1 other widget area, but this particular page does not display it.' ), 751 ENT_QUOTES, 752 get_bloginfo( 'charset' ) 753 ); 754 $registered_sidebar_count = count( $wp_registered_sidebars ); 755 for ( $non_rendered_count = 2; $non_rendered_count < $registered_sidebar_count; $non_rendered_count++ ) { 756 $some_non_rendered_areas_messages[ $non_rendered_count ] = html_entity_decode( 757 sprintf( 758 /* translators: %s: The number of other widget areas registered but not rendered. */ 759 _n( 760 'Your theme has %s other widget area, but this particular page does not display it.', 761 'Your theme has %s other widget areas, but this particular page does not display them.', 762 $non_rendered_count 763 ), 764 number_format_i18n( $non_rendered_count ) 765 ), 766 ENT_QUOTES, 767 get_bloginfo( 'charset' ) 768 ); 769 } 770 771 if ( 1 === $registered_sidebar_count ) { 772 $no_areas_shown_message = html_entity_decode( 773 sprintf( 774 __( 'Your theme has 1 widget area, but this particular page does not display it.' ) 775 ), 776 ENT_QUOTES, 777 get_bloginfo( 'charset' ) 778 ); 779 } else { 780 $no_areas_shown_message = html_entity_decode( 781 sprintf( 782 /* translators: %s: The total number of widget areas registered. */ 783 _n( 784 'Your theme has %s widget area, but this particular page does not display it.', 785 'Your theme has %s widget areas, but this particular page does not display them.', 786 $registered_sidebar_count 787 ), 788 number_format_i18n( $registered_sidebar_count ) 789 ), 790 ENT_QUOTES, 791 get_bloginfo( 'charset' ) 792 ); 793 } 794 795 $settings = array( 796 'registeredSidebars' => array_values( $wp_registered_sidebars ), 797 'registeredWidgets' => $wp_registered_widgets, 798 'availableWidgets' => $available_widgets, // @todo Merge this with registered_widgets. 799 'l10n' => array( 800 'saveBtnLabel' => __( 'Apply' ), 801 'saveBtnTooltip' => __( 'Save and preview changes before publishing them.' ), 802 'removeBtnLabel' => __( 'Remove' ), 803 'removeBtnTooltip' => __( 'Keep widget settings and move it to the inactive widgets' ), 804 'error' => __( 'An error has occurred. Please reload the page and try again.' ), 805 'widgetMovedUp' => __( 'Widget moved up' ), 806 'widgetMovedDown' => __( 'Widget moved down' ), 807 'navigatePreview' => __( 'You can navigate to other pages on your site while using the Customizer to view and edit the widgets displayed on those pages.' ), 808 'someAreasShown' => $some_non_rendered_areas_messages, 809 'noAreasShown' => $no_areas_shown_message, 810 'reorderModeOn' => __( 'Reorder mode enabled' ), 811 'reorderModeOff' => __( 'Reorder mode closed' ), 812 'reorderLabelOn' => esc_attr__( 'Reorder widgets' ), 813 /* translators: %d: The number of widgets found. */ 814 'widgetsFound' => __( 'Number of widgets found: %d' ), 815 'noWidgetsFound' => __( 'No widgets found.' ), 816 ), 817 'tpl' => array( 818 'widgetReorderNav' => $widget_reorder_nav_tpl, 819 'moveWidgetArea' => $move_widget_area_tpl, 820 ), 821 'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(), 822 ); 823 824 foreach ( $settings['registeredWidgets'] as &$registered_widget ) { 825 unset( $registered_widget['callback'] ); // May not be JSON-serializeable. 826 } 827 828 $wp_scripts->add_data( 829 'customize-widgets', 830 'data', 831 sprintf( 'var _wpCustomizeWidgetsSettings = %s;', wp_json_encode( $settings ) ) 832 ); 833 834 /* 835 * TODO: Update 'wp-customize-widgets' to not rely so much on things in 836 * 'customize-widgets'. This will let us skip most of the above and not 837 * enqueue 'customize-widgets' which saves bytes. 838 */ 839 840 if ( wp_use_widgets_block_editor() ) { 841 $block_editor_context = new WP_Block_Editor_Context( 842 array( 843 'name' => 'core/customize-widgets', 844 ) 845 ); 846 847 $editor_settings = get_block_editor_settings( 848 get_legacy_widget_block_editor_settings(), 849 $block_editor_context 850 ); 851 852 wp_add_inline_script( 853 'wp-customize-widgets', 854 sprintf( 855 'wp.domReady( function() { 856 wp.customizeWidgets.initialize( "widgets-customizer", %s ); 857 } );', 858 wp_json_encode( $editor_settings ) 859 ) 860 ); 861 862 // Preload server-registered block schemas. 863 wp_add_inline_script( 864 'wp-blocks', 865 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' 866 ); 867 868 wp_add_inline_script( 869 'wp-blocks', 870 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), 871 'after' 872 ); 873 874 wp_enqueue_script( 'wp-customize-widgets' ); 875 wp_enqueue_style( 'wp-customize-widgets' ); 876 877 /** This action is documented in edit-form-blocks.php */ 878 do_action( 'enqueue_block_editor_assets' ); 879 } 880 } 881 882 /** 883 * Renders the widget form control templates into the DOM. 884 * 885 * @since 3.9.0 886 */ 887 public function output_widget_control_templates() { 888 ?> 889 <div id="widgets-left"><!-- compatibility with JS which looks for widget templates here --> 890 <div id="available-widgets"> 891 <div class="customize-section-title"> 892 <button class="customize-section-back" tabindex="-1"> 893 <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> 894 </button> 895 <h3> 896 <span class="customize-action"> 897 <?php 898 /* translators: ▸ is the unicode right-pointing triangle. %s: Section title in the Customizer. */ 899 printf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'widgets' )->title ) ); 900 ?> 901 </span> 902 <?php _e( 'Add a Widget' ); ?> 903 </h3> 904 </div> 905 <div id="available-widgets-filter"> 906 <label class="screen-reader-text" for="widgets-search"><?php _e( 'Search Widgets' ); ?></label> 907 <input type="text" id="widgets-search" placeholder="<?php esc_attr_e( 'Search widgets…' ); ?>" aria-describedby="widgets-search-desc" /> 908 <div class="search-icon" aria-hidden="true"></div> 909 <button type="button" class="clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results' ); ?></span></button> 910 <p class="screen-reader-text" id="widgets-search-desc"><?php _e( 'The search results will be updated as you type.' ); ?></p> 911 </div> 912 <div id="available-widgets-list"> 913 <?php foreach ( $this->get_available_widgets() as $available_widget ) : ?> 914 <div id="widget-tpl-<?php echo esc_attr( $available_widget['id'] ); ?>" data-widget-id="<?php echo esc_attr( $available_widget['id'] ); ?>" class="widget-tpl <?php echo esc_attr( $available_widget['id'] ); ?>" tabindex="0"> 915 <?php echo $available_widget['control_tpl']; ?> 916 </div> 917 <?php endforeach; ?> 918 <p class="no-widgets-found-message"><?php _e( 'No widgets found.' ); ?></p> 919 </div><!-- #available-widgets-list --> 920 </div><!-- #available-widgets --> 921 </div><!-- #widgets-left --> 922 <?php 923 } 924 925 /** 926 * Calls admin_print_footer_scripts and admin_print_scripts hooks to 927 * allow custom scripts from plugins. 928 * 929 * @since 3.9.0 930 */ 931 public function print_footer_scripts() { 932 /** This action is documented in wp-admin/admin-footer.php */ 933 do_action( 'admin_print_footer_scripts-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 934 935 /** This action is documented in wp-admin/admin-footer.php */ 936 do_action( 'admin_print_footer_scripts' ); 937 938 /** This action is documented in wp-admin/admin-footer.php */ 939 do_action( 'admin_footer-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 940 } 941 942 /** 943 * Retrieves common arguments to supply when constructing a Customizer setting. 944 * 945 * @since 3.9.0 946 * 947 * @param string $id Widget setting ID. 948 * @param array $overrides Array of setting overrides. 949 * @return array Possibly modified setting arguments. 950 */ 951 public function get_setting_args( $id, $overrides = array() ) { 952 $args = array( 953 'type' => 'option', 954 'capability' => 'edit_theme_options', 955 'default' => array(), 956 ); 957 958 if ( preg_match( $this->setting_id_patterns['sidebar_widgets'], $id, $matches ) ) { 959 $args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' ); 960 $args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' ); 961 $args['transport'] = current_theme_supports( 'customize-selective-refresh-widgets' ) ? 'postMessage' : 'refresh'; 962 } elseif ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) { 963 $id_base = $matches['id_base']; 964 $args['sanitize_callback'] = function( $value ) use ( $id_base ) { 965 return $this->sanitize_widget_instance( $value, $id_base ); 966 }; 967 $args['sanitize_js_callback'] = function( $value ) use ( $id_base ) { 968 return $this->sanitize_widget_js_instance( $value, $id_base ); 969 }; 970 $args['transport'] = $this->is_widget_selective_refreshable( $matches['id_base'] ) ? 'postMessage' : 'refresh'; 971 } 972 973 $args = array_merge( $args, $overrides ); 974 975 /** 976 * Filters the common arguments supplied when constructing a Customizer setting. 977 * 978 * @since 3.9.0 979 * 980 * @see WP_Customize_Setting 981 * 982 * @param array $args Array of Customizer setting arguments. 983 * @param string $id Widget setting ID. 984 */ 985 return apply_filters( 'widget_customizer_setting_args', $args, $id ); 986 } 987 988 /** 989 * Ensures sidebar widget arrays only ever contain widget IDS. 990 * 991 * Used as the 'sanitize_callback' for each $sidebars_widgets setting. 992 * 993 * @since 3.9.0 994 * 995 * @param string[] $widget_ids Array of widget IDs. 996 * @return string[] Array of sanitized widget IDs. 997 */ 998 public function sanitize_sidebar_widgets( $widget_ids ) { 999 $widget_ids = array_map( 'strval', (array) $widget_ids ); 1000 $sanitized_widget_ids = array(); 1001 foreach ( $widget_ids as $widget_id ) { 1002 $sanitized_widget_ids[] = preg_replace( '/[^a-z0-9_\-]/', '', $widget_id ); 1003 } 1004 return $sanitized_widget_ids; 1005 } 1006 1007 /** 1008 * Builds up an index of all available widgets for use in Backbone models. 1009 * 1010 * @since 3.9.0 1011 * 1012 * @global array $wp_registered_widgets 1013 * @global array $wp_registered_widget_controls 1014 * 1015 * @see wp_list_widgets() 1016 * 1017 * @return array List of available widgets. 1018 */ 1019 public function get_available_widgets() { 1020 static $available_widgets = array(); 1021 if ( ! empty( $available_widgets ) ) { 1022 return $available_widgets; 1023 } 1024 1025 global $wp_registered_widgets, $wp_registered_widget_controls; 1026 require_once ABSPATH . 'wp-admin/includes/widgets.php'; // For next_widget_id_number(). 1027 1028 $sort = $wp_registered_widgets; 1029 usort( $sort, array( $this, '_sort_name_callback' ) ); 1030 $done = array(); 1031 1032 foreach ( $sort as $widget ) { 1033 if ( in_array( $widget['callback'], $done, true ) ) { // We already showed this multi-widget. 1034 continue; 1035 } 1036 1037 $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false ); 1038 $done[] = $widget['callback']; 1039 1040 if ( ! isset( $widget['params'][0] ) ) { 1041 $widget['params'][0] = array(); 1042 } 1043 1044 $available_widget = $widget; 1045 unset( $available_widget['callback'] ); // Not serializable to JSON. 1046 1047 $args = array( 1048 'widget_id' => $widget['id'], 1049 'widget_name' => $widget['name'], 1050 '_display' => 'template', 1051 ); 1052 1053 $is_disabled = false; 1054 $is_multi_widget = ( isset( $wp_registered_widget_controls[ $widget['id'] ]['id_base'] ) && isset( $widget['params'][0]['number'] ) ); 1055 if ( $is_multi_widget ) { 1056 $id_base = $wp_registered_widget_controls[ $widget['id'] ]['id_base']; 1057 $args['_temp_id'] = "$id_base-__i__"; 1058 $args['_multi_num'] = next_widget_id_number( $id_base ); 1059 $args['_add'] = 'multi'; 1060 } else { 1061 $args['_add'] = 'single'; 1062 1063 if ( $sidebar && 'wp_inactive_widgets' !== $sidebar ) { 1064 $is_disabled = true; 1065 } 1066 $id_base = $widget['id']; 1067 } 1068 1069 $list_widget_controls_args = wp_list_widget_controls_dynamic_sidebar( 1070 array( 1071 0 => $args, 1072 1 => $widget['params'][0], 1073 ) 1074 ); 1075 $control_tpl = $this->get_widget_control( $list_widget_controls_args ); 1076 1077 // The properties here are mapped to the Backbone Widget model. 1078 $available_widget = array_merge( 1079 $available_widget, 1080 array( 1081 'temp_id' => isset( $args['_temp_id'] ) ? $args['_temp_id'] : null, 1082 'is_multi' => $is_multi_widget, 1083 'control_tpl' => $control_tpl, 1084 'multi_number' => ( 'multi' === $args['_add'] ) ? $args['_multi_num'] : false, 1085 'is_disabled' => $is_disabled, 1086 'id_base' => $id_base, 1087 'transport' => $this->is_widget_selective_refreshable( $id_base ) ? 'postMessage' : 'refresh', 1088 'width' => $wp_registered_widget_controls[ $widget['id'] ]['width'], 1089 'height' => $wp_registered_widget_controls[ $widget['id'] ]['height'], 1090 'is_wide' => $this->is_wide_widget( $widget['id'] ), 1091 ) 1092 ); 1093 1094 $available_widgets[] = $available_widget; 1095 } 1096 1097 return $available_widgets; 1098 } 1099 1100 /** 1101 * Naturally orders available widgets by name. 1102 * 1103 * @since 3.9.0 1104 * 1105 * @param array $widget_a The first widget to compare. 1106 * @param array $widget_b The second widget to compare. 1107 * @return int Reorder position for the current widget comparison. 1108 */ 1109 protected function _sort_name_callback( $widget_a, $widget_b ) { 1110 return strnatcasecmp( $widget_a['name'], $widget_b['name'] ); 1111 } 1112 1113 /** 1114 * Retrieves the widget control markup. 1115 * 1116 * @since 3.9.0 1117 * 1118 * @param array $args Widget control arguments. 1119 * @return string Widget control form HTML markup. 1120 */ 1121 public function get_widget_control( $args ) { 1122 $args[0]['before_form'] = '<div class="form">'; 1123 $args[0]['after_form'] = '</div><!-- .form -->'; 1124 $args[0]['before_widget_content'] = '<div class="widget-content">'; 1125 $args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; 1126 ob_start(); 1127 wp_widget_control( ...$args ); 1128 $control_tpl = ob_get_clean(); 1129 return $control_tpl; 1130 } 1131 1132 /** 1133 * Retrieves the widget control markup parts. 1134 * 1135 * @since 4.4.0 1136 * 1137 * @param array $args Widget control arguments. 1138 * @return array { 1139 * @type string $control Markup for widget control wrapping form. 1140 * @type string $content The contents of the widget form itself. 1141 * } 1142 */ 1143 public function get_widget_control_parts( $args ) { 1144 $args[0]['before_widget_content'] = '<div class="widget-content">'; 1145 $args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; 1146 $control_markup = $this->get_widget_control( $args ); 1147 1148 $content_start_pos = strpos( $control_markup, $args[0]['before_widget_content'] ); 1149 $content_end_pos = strrpos( $control_markup, $args[0]['after_widget_content'] ); 1150 1151 $control = substr( $control_markup, 0, $content_start_pos + strlen( $args[0]['before_widget_content'] ) ); 1152 $control .= substr( $control_markup, $content_end_pos ); 1153 $content = trim( 1154 substr( 1155 $control_markup, 1156 $content_start_pos + strlen( $args[0]['before_widget_content'] ), 1157 $content_end_pos - $content_start_pos - strlen( $args[0]['before_widget_content'] ) 1158 ) 1159 ); 1160 1161 return compact( 'control', 'content' ); 1162 } 1163 1164 /** 1165 * Adds hooks for the Customizer preview. 1166 * 1167 * @since 3.9.0 1168 */ 1169 public function customize_preview_init() { 1170 add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue' ) ); 1171 add_action( 'wp_print_styles', array( $this, 'print_preview_css' ), 1 ); 1172 add_action( 'wp_footer', array( $this, 'export_preview_data' ), 20 ); 1173 } 1174 1175 /** 1176 * Refreshes the nonce for widget updates. 1177 * 1178 * @since 4.2.0 1179 * 1180 * @param array $nonces Array of nonces. 1181 * @return array Array of nonces. 1182 */ 1183 public function refresh_nonces( $nonces ) { 1184 $nonces['update-widget'] = wp_create_nonce( 'update-widget' ); 1185 return $nonces; 1186 } 1187 1188 /** 1189 * Tells the script loader to load the scripts and styles of custom blocks 1190 * if the widgets block editor is enabled. 1191 * 1192 * @since 5.8.0 1193 * 1194 * @param bool $is_block_editor_screen Current decision about loading block assets. 1195 * @return bool Filtered decision about loading block assets. 1196 */ 1197 public function should_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { 1198 if ( wp_use_widgets_block_editor() ) { 1199 return true; 1200 } 1201 1202 return $is_block_editor_screen; 1203 } 1204 1205 /** 1206 * When previewing, ensures the proper previewing widgets are used. 1207 * 1208 * Because wp_get_sidebars_widgets() gets called early at {@see 'init' } (via 1209 * wp_convert_widget_settings()) and can set global variable `$_wp_sidebars_widgets` 1210 * to the value of `get_option( 'sidebars_widgets' )` before the Customizer preview 1211 * filter is added, it has to be reset after the filter has been added. 1212 * 1213 * @since 3.9.0 1214 * 1215 * @param array $sidebars_widgets List of widgets for the current sidebar. 1216 * @return array 1217 */ 1218 public function preview_sidebars_widgets( $sidebars_widgets ) { 1219 $sidebars_widgets = get_option( 'sidebars_widgets', array() ); 1220 1221 unset( $sidebars_widgets['array_version'] ); 1222 return $sidebars_widgets; 1223 } 1224 1225 /** 1226 * Enqueues scripts for the Customizer preview. 1227 * 1228 * @since 3.9.0 1229 */ 1230 public function customize_preview_enqueue() { 1231 wp_enqueue_script( 'customize-preview-widgets' ); 1232 } 1233 1234 /** 1235 * Inserts default style for highlighted widget at early point so theme 1236 * stylesheet can override. 1237 * 1238 * @since 3.9.0 1239 */ 1240 public function print_preview_css() { 1241 ?> 1242 <style> 1243 .widget-customizer-highlighted-widget { 1244 outline: none; 1245 -webkit-box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); 1246 box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); 1247 position: relative; 1248 z-index: 1; 1249 } 1250 </style> 1251 <?php 1252 } 1253 1254 /** 1255 * Communicates the sidebars that appeared on the page at the very end of the page, 1256 * and at the very end of the wp_footer, 1257 * 1258 * @since 3.9.0 1259 * 1260 * @global array $wp_registered_sidebars 1261 * @global array $wp_registered_widgets 1262 */ 1263 public function export_preview_data() { 1264 global $wp_registered_sidebars, $wp_registered_widgets; 1265 1266 $switched_locale = switch_to_locale( get_user_locale() ); 1267 1268 $l10n = array( 1269 'widgetTooltip' => __( 'Shift-click to edit this widget.' ), 1270 ); 1271 1272 if ( $switched_locale ) { 1273 restore_previous_locale(); 1274 } 1275 1276 $rendered_sidebars = array_filter( $this->rendered_sidebars ); 1277 $rendered_widgets = array_filter( $this->rendered_widgets ); 1278 1279 // Prepare Customizer settings to pass to JavaScript. 1280 $settings = array( 1281 'renderedSidebars' => array_fill_keys( array_keys( $rendered_sidebars ), true ), 1282 'renderedWidgets' => array_fill_keys( array_keys( $rendered_widgets ), true ), 1283 'registeredSidebars' => array_values( $wp_registered_sidebars ), 1284 'registeredWidgets' => $wp_registered_widgets, 1285 'l10n' => $l10n, 1286 'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(), 1287 ); 1288 1289 foreach ( $settings['registeredWidgets'] as &$registered_widget ) { 1290 unset( $registered_widget['callback'] ); // May not be JSON-serializeable. 1291 } 1292 1293 ?> 1294 <script type="text/javascript"> 1295 var _wpWidgetCustomizerPreviewSettings = <?php echo wp_json_encode( $settings ); ?>; 1296 </script> 1297 <?php 1298 } 1299 1300 /** 1301 * Tracks the widgets that were rendered. 1302 * 1303 * @since 3.9.0 1304 * 1305 * @param array $widget Rendered widget to tally. 1306 */ 1307 public function tally_rendered_widgets( $widget ) { 1308 $this->rendered_widgets[ $widget['id'] ] = true; 1309 } 1310 1311 /** 1312 * Determine if a widget is rendered on the page. 1313 * 1314 * @since 4.0.0 1315 * 1316 * @param string $widget_id Widget ID to check. 1317 * @return bool Whether the widget is rendered. 1318 */ 1319 public function is_widget_rendered( $widget_id ) { 1320 return ! empty( $this->rendered_widgets[ $widget_id ] ); 1321 } 1322 1323 /** 1324 * Determines if a sidebar is rendered on the page. 1325 * 1326 * @since 4.0.0 1327 * 1328 * @param string $sidebar_id Sidebar ID to check. 1329 * @return bool Whether the sidebar is rendered. 1330 */ 1331 public function is_sidebar_rendered( $sidebar_id ) { 1332 return ! empty( $this->rendered_sidebars[ $sidebar_id ] ); 1333 } 1334 1335 /** 1336 * Tallies the sidebars rendered via is_active_sidebar(). 1337 * 1338 * Keep track of the times that is_active_sidebar() is called in the template, 1339 * and assume that this means that the sidebar would be rendered on the template 1340 * if there were widgets populating it. 1341 * 1342 * @since 3.9.0 1343 * 1344 * @param bool $is_active Whether the sidebar is active. 1345 * @param string $sidebar_id Sidebar ID. 1346 * @return bool Whether the sidebar is active. 1347 */ 1348 public function tally_sidebars_via_is_active_sidebar_calls( $is_active, $sidebar_id ) { 1349 if ( is_registered_sidebar( $sidebar_id ) ) { 1350 $this->rendered_sidebars[ $sidebar_id ] = true; 1351 } 1352 1353 /* 1354 * We may need to force this to true, and also force-true the value 1355 * for 'dynamic_sidebar_has_widgets' if we want to ensure that there 1356 * is an area to drop widgets into, if the sidebar is empty. 1357 */ 1358 return $is_active; 1359 } 1360 1361 /** 1362 * Tallies the sidebars rendered via dynamic_sidebar(). 1363 * 1364 * Keep track of the times that dynamic_sidebar() is called in the template, 1365 * and assume this means the sidebar would be rendered on the template if 1366 * there were widgets populating it. 1367 * 1368 * @since 3.9.0 1369 * 1370 * @param bool $has_widgets Whether the current sidebar has widgets. 1371 * @param string $sidebar_id Sidebar ID. 1372 * @return bool Whether the current sidebar has widgets. 1373 */ 1374 public function tally_sidebars_via_dynamic_sidebar_calls( $has_widgets, $sidebar_id ) { 1375 if ( is_registered_sidebar( $sidebar_id ) ) { 1376 $this->rendered_sidebars[ $sidebar_id ] = true; 1377 } 1378 1379 /* 1380 * We may need to force this to true, and also force-true the value 1381 * for 'is_active_sidebar' if we want to ensure there is an area to 1382 * drop widgets into, if the sidebar is empty. 1383 */ 1384 return $has_widgets; 1385 } 1386 1387 /** 1388 * Retrieves MAC for a serialized widget instance string. 1389 * 1390 * Allows values posted back from JS to be rejected if any tampering of the 1391 * data has occurred. 1392 * 1393 * @since 3.9.0 1394 * 1395 * @param string $serialized_instance Widget instance. 1396 * @return string MAC for serialized widget instance. 1397 */ 1398 protected function get_instance_hash_key( $serialized_instance ) { 1399 return wp_hash( $serialized_instance ); 1400 } 1401 1402 /** 1403 * Sanitizes a widget instance. 1404 * 1405 * Unserialize the JS-instance for storing in the options. It's important that this filter 1406 * only get applied to an instance *once*. 1407 * 1408 * @since 3.9.0 1409 * @since 5.8.0 Added the `$id_base` parameter. 1410 * 1411 * @global WP_Widget_Factory $wp_widget_factory 1412 * 1413 * @param array $value Widget instance to sanitize. 1414 * @param string $id_base Optional. Base of the ID of the widget being sanitized. Default null. 1415 * @return array|void Sanitized widget instance. 1416 */ 1417 public function sanitize_widget_instance( $value, $id_base = null ) { 1418 global $wp_widget_factory; 1419 1420 if ( array() === $value ) { 1421 return $value; 1422 } 1423 1424 if ( isset( $value['raw_instance'] ) && $id_base && wp_use_widgets_block_editor() ) { 1425 $widget_object = $wp_widget_factory->get_widget_object( $id_base ); 1426 if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { 1427 if ( 'block' === $id_base && ! current_user_can( 'unfiltered_html' ) ) { 1428 /* 1429 * The content of the 'block' widget is not filtered on the fly while editing. 1430 * Filter the content here to prevent vulnerabilities. 1431 */ 1432 $value['raw_instance']['content'] = wp_kses_post( $value['raw_instance']['content'] ); 1433 } 1434 1435 return $value['raw_instance']; 1436 } 1437 } 1438 1439 if ( 1440 empty( $value['is_widget_customizer_js_value'] ) || 1441 empty( $value['instance_hash_key'] ) || 1442 empty( $value['encoded_serialized_instance'] ) 1443 ) { 1444 return; 1445 } 1446 1447 $decoded = base64_decode( $value['encoded_serialized_instance'], true ); 1448 if ( false === $decoded ) { 1449 return; 1450 } 1451 1452 if ( ! hash_equals( $this->get_instance_hash_key( $decoded ), $value['instance_hash_key'] ) ) { 1453 return; 1454 } 1455 1456 $instance = unserialize( $decoded ); 1457 if ( false === $instance ) { 1458 return; 1459 } 1460 1461 return $instance; 1462 } 1463 1464 /** 1465 * Converts a widget instance into JSON-representable format. 1466 * 1467 * @since 3.9.0 1468 * @since 5.8.0 Added the `$id_base` parameter. 1469 * 1470 * @global WP_Widget_Factory $wp_widget_factory 1471 * 1472 * @param array $value Widget instance to convert to JSON. 1473 * @param string $id_base Optional. Base of the ID of the widget being sanitized. Default null. 1474 * @return array JSON-converted widget instance. 1475 */ 1476 public function sanitize_widget_js_instance( $value, $id_base = null ) { 1477 global $wp_widget_factory; 1478 1479 if ( empty( $value['is_widget_customizer_js_value'] ) ) { 1480 $serialized = serialize( $value ); 1481 1482 $js_value = array( 1483 'encoded_serialized_instance' => base64_encode( $serialized ), 1484 'title' => empty( $value['title'] ) ? '' : $value['title'], 1485 'is_widget_customizer_js_value' => true, 1486 'instance_hash_key' => $this->get_instance_hash_key( $serialized ), 1487 ); 1488 1489 if ( $id_base && wp_use_widgets_block_editor() ) { 1490 $widget_object = $wp_widget_factory->get_widget_object( $id_base ); 1491 if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { 1492 $js_value['raw_instance'] = (object) $value; 1493 } 1494 } 1495 1496 return $js_value; 1497 } 1498 1499 return $value; 1500 } 1501 1502 /** 1503 * Strips out widget IDs for widgets which are no longer registered. 1504 * 1505 * One example where this might happen is when a plugin orphans a widget 1506 * in a sidebar upon deactivation. 1507 * 1508 * @since 3.9.0 1509 * 1510 * @global array $wp_registered_widgets 1511 * 1512 * @param array $widget_ids List of widget IDs. 1513 * @return array Parsed list of widget IDs. 1514 */ 1515 public function sanitize_sidebar_widgets_js_instance( $widget_ids ) { 1516 global $wp_registered_widgets; 1517 $widget_ids = array_values( array_intersect( $widget_ids, array_keys( $wp_registered_widgets ) ) ); 1518 return $widget_ids; 1519 } 1520 1521 /** 1522 * Finds and invokes the widget update and control callbacks. 1523 * 1524 * Requires that `$_POST` be populated with the instance data. 1525 * 1526 * @since 3.9.0 1527 * 1528 * @global array $wp_registered_widget_updates 1529 * @global array $wp_registered_widget_controls 1530 * 1531 * @param string $widget_id Widget ID. 1532 * @return array|WP_Error Array containing the updated widget information. 1533 * A WP_Error object, otherwise. 1534 */ 1535 public function call_widget_update( $widget_id ) { 1536 global $wp_registered_widget_updates, $wp_registered_widget_controls; 1537 1538 $setting_id = $this->get_setting_id( $widget_id ); 1539 1540 /* 1541 * Make sure that other setting changes have previewed since this widget 1542 * may depend on them (e.g. Menus being present for Navigation Menu widget). 1543 */ 1544 if ( ! did_action( 'customize_preview_init' ) ) { 1545 foreach ( $this->manager->settings() as $setting ) { 1546 if ( $setting->id !== $setting_id ) { 1547 $setting->preview(); 1548 } 1549 } 1550 } 1551 1552 $this->start_capturing_option_updates(); 1553 $parsed_id = $this->parse_widget_id( $widget_id ); 1554 $option_name = 'widget_' . $parsed_id['id_base']; 1555 1556 /* 1557 * If a previously-sanitized instance is provided, populate the input vars 1558 * with its values so that the widget update callback will read this instance 1559 */ 1560 $added_input_vars = array(); 1561 if ( ! empty( $_POST['sanitized_widget_setting'] ) ) { 1562 $sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true ); 1563 if ( false === $sanitized_widget_setting ) { 1564 $this->stop_capturing_option_updates(); 1565 return new WP_Error( 'widget_setting_malformed' ); 1566 } 1567 1568 $instance = $this->sanitize_widget_instance( $sanitized_widget_setting, $parsed_id['id_base'] ); 1569 if ( is_null( $instance ) ) { 1570 $this->stop_capturing_option_updates(); 1571 return new WP_Error( 'widget_setting_unsanitized' ); 1572 } 1573 1574 if ( ! is_null( $parsed_id['number'] ) ) { 1575 $value = array(); 1576 $value[ $parsed_id['number'] ] = $instance; 1577 $key = 'widget-' . $parsed_id['id_base']; 1578 $_REQUEST[ $key ] = wp_slash( $value ); 1579 $_POST[ $key ] = $_REQUEST[ $key ]; 1580 $added_input_vars[] = $key; 1581 } else { 1582 foreach ( $instance as $key => $value ) { 1583 $_REQUEST[ $key ] = wp_slash( $value ); 1584 $_POST[ $key ] = $_REQUEST[ $key ]; 1585 $added_input_vars[] = $key; 1586 } 1587 } 1588 } 1589 1590 // Invoke the widget update callback. 1591 foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 1592 if ( $name === $parsed_id['id_base'] && is_callable( $control['callback'] ) ) { 1593 ob_start(); 1594 call_user_func_array( $control['callback'], $control['params'] ); 1595 ob_end_clean(); 1596 break; 1597 } 1598 } 1599 1600 // Clean up any input vars that were manually added. 1601 foreach ( $added_input_vars as $key ) { 1602 unset( $_POST[ $key ] ); 1603 unset( $_REQUEST[ $key ] ); 1604 } 1605 1606 // Make sure the expected option was updated. 1607 if ( 0 !== $this->count_captured_options() ) { 1608 if ( $this->count_captured_options() > 1 ) { 1609 $this->stop_capturing_option_updates(); 1610 return new WP_Error( 'widget_setting_too_many_options' ); 1611 } 1612 1613 $updated_option_name = key( $this->get_captured_options() ); 1614 if ( $updated_option_name !== $option_name ) { 1615 $this->stop_capturing_option_updates(); 1616 return new WP_Error( 'widget_setting_unexpected_option' ); 1617 } 1618 } 1619 1620 // Obtain the widget instance. 1621 $option = $this->get_captured_option( $option_name ); 1622 if ( null !== $parsed_id['number'] ) { 1623 $instance = $option[ $parsed_id['number'] ]; 1624 } else { 1625 $instance = $option; 1626 } 1627 1628 /* 1629 * Override the incoming $_POST['customized'] for a newly-created widget's 1630 * setting with the new $instance so that the preview filter currently 1631 * in place from WP_Customize_Setting::preview() will use this value 1632 * instead of the default widget instance value (an empty array). 1633 */ 1634 $this->manager->set_post_value( $setting_id, $this->sanitize_widget_js_instance( $instance, $parsed_id['id_base'] ) ); 1635 1636 // Obtain the widget control with the updated instance in place. 1637 ob_start(); 1638 $form = $wp_registered_widget_controls[ $widget_id ]; 1639 if ( $form ) { 1640 call_user_func_array( $form['callback'], $form['params'] ); 1641 } 1642 $form = ob_get_clean(); 1643 1644 $this->stop_capturing_option_updates(); 1645 1646 return compact( 'instance', 'form' ); 1647 } 1648 1649 /** 1650 * Updates widget settings asynchronously. 1651 * 1652 * Allows the Customizer to update a widget using its form, but return the new 1653 * instance info via Ajax instead of saving it to the options table. 1654 * 1655 * Most code here copied from wp_ajax_save_widget(). 1656 * 1657 * @since 3.9.0 1658 * 1659 * @see wp_ajax_save_widget() 1660 */ 1661 public function wp_ajax_update_widget() { 1662 1663 if ( ! is_user_logged_in() ) { 1664 wp_die( 0 ); 1665 } 1666 1667 check_ajax_referer( 'update-widget', 'nonce' ); 1668 1669 if ( ! current_user_can( 'edit_theme_options' ) ) { 1670 wp_die( -1 ); 1671 } 1672 1673 if ( empty( $_POST['widget-id'] ) ) { 1674 wp_send_json_error( 'missing_widget-id' ); 1675 } 1676 1677 /** This action is documented in wp-admin/includes/ajax-actions.php */ 1678 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 1679 1680 /** This action is documented in wp-admin/includes/ajax-actions.php */ 1681 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 1682 1683 /** This action is documented in wp-admin/widgets.php */ 1684 do_action( 'sidebar_admin_setup' ); 1685 1686 $widget_id = $this->get_post_value( 'widget-id' ); 1687 $parsed_id = $this->parse_widget_id( $widget_id ); 1688 $id_base = $parsed_id['id_base']; 1689 1690 $is_updating_widget_template = ( 1691 isset( $_POST[ 'widget-' . $id_base ] ) 1692 && 1693 is_array( $_POST[ 'widget-' . $id_base ] ) 1694 && 1695 preg_match( '/__i__|%i%/', key( $_POST[ 'widget-' . $id_base ] ) ) 1696 ); 1697 if ( $is_updating_widget_template ) { 1698 wp_send_json_error( 'template_widget_not_updatable' ); 1699 } 1700 1701 $updated_widget = $this->call_widget_update( $widget_id ); // => {instance,form} 1702 if ( is_wp_error( $updated_widget ) ) { 1703 wp_send_json_error( $updated_widget->get_error_code() ); 1704 } 1705 1706 $form = $updated_widget['form']; 1707 $instance = $this->sanitize_widget_js_instance( $updated_widget['instance'], $id_base ); 1708 1709 wp_send_json_success( compact( 'form', 'instance' ) ); 1710 } 1711 1712 /* 1713 * Selective Refresh Methods 1714 */ 1715 1716 /** 1717 * Filters arguments for dynamic widget partials. 1718 * 1719 * @since 4.5.0 1720 * 1721 * @param array|false $partial_args Partial arguments. 1722 * @param string $partial_id Partial ID. 1723 * @return array (Maybe) modified partial arguments. 1724 */ 1725 public function customize_dynamic_partial_args( $partial_args, $partial_id ) { 1726 if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 1727 return $partial_args; 1728 } 1729 1730 if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) { 1731 if ( false === $partial_args ) { 1732 $partial_args = array(); 1733 } 1734 $partial_args = array_merge( 1735 $partial_args, 1736 array( 1737 'type' => 'widget', 1738 'render_callback' => array( $this, 'render_widget_partial' ), 1739 'container_inclusive' => true, 1740 'settings' => array( $this->get_setting_id( $matches['widget_id'] ) ), 1741 'capability' => 'edit_theme_options', 1742 ) 1743 ); 1744 } 1745 1746 return $partial_args; 1747 } 1748 1749 /** 1750 * Adds hooks for selective refresh. 1751 * 1752 * @since 4.5.0 1753 */ 1754 public function selective_refresh_init() { 1755 if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 1756 return; 1757 } 1758 add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) ); 1759 add_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_allowed_data_attributes' ) ); 1760 add_action( 'dynamic_sidebar_before', array( $this, 'start_dynamic_sidebar' ) ); 1761 add_action( 'dynamic_sidebar_after', array( $this, 'end_dynamic_sidebar' ) ); 1762 } 1763 1764 /** 1765 * Inject selective refresh data attributes into widget container elements. 1766 * 1767 * @since 4.5.0 1768 * 1769 * @param array $params { 1770 * Dynamic sidebar params. 1771 * 1772 * @type array $args Sidebar args. 1773 * @type array $widget_args Widget args. 1774 * } 1775 * @see WP_Customize_Nav_Menus::filter_wp_nav_menu_args() 1776 * 1777 * @return array Params. 1778 */ 1779 public function filter_dynamic_sidebar_params( $params ) { 1780 $sidebar_args = array_merge( 1781 array( 1782 'before_widget' => '', 1783 'after_widget' => '', 1784 ), 1785 $params[0] 1786 ); 1787 1788 // Skip widgets not in a registered sidebar or ones which lack a proper wrapper element to attach the data-* attributes to. 1789 $matches = array(); 1790 $is_valid = ( 1791 isset( $sidebar_args['id'] ) 1792 && 1793 is_registered_sidebar( $sidebar_args['id'] ) 1794 && 1795 ( isset( $this->current_dynamic_sidebar_id_stack[0] ) && $this->current_dynamic_sidebar_id_stack[0] === $sidebar_args['id'] ) 1796 && 1797 preg_match( '#^<(?P<tag_name>\w+)#', $sidebar_args['before_widget'], $matches ) 1798 ); 1799 if ( ! $is_valid ) { 1800 return $params; 1801 } 1802 $this->before_widget_tags_seen[ $matches['tag_name'] ] = true; 1803 1804 $context = array( 1805 'sidebar_id' => $sidebar_args['id'], 1806 ); 1807 if ( isset( $this->context_sidebar_instance_number ) ) { 1808 $context['sidebar_instance_number'] = $this->context_sidebar_instance_number; 1809 } elseif ( isset( $sidebar_args['id'] ) && isset( $this->sidebar_instance_count[ $sidebar_args['id'] ] ) ) { 1810 $context['sidebar_instance_number'] = $this->sidebar_instance_count[ $sidebar_args['id'] ]; 1811 } 1812 1813 $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'widget[' . $sidebar_args['widget_id'] . ']' ) ); 1814 $attributes .= ' data-customize-partial-type="widget"'; 1815 $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $context ) ) ); 1816 $attributes .= sprintf( ' data-customize-widget-id="%s"', esc_attr( $sidebar_args['widget_id'] ) ); 1817 $sidebar_args['before_widget'] = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $sidebar_args['before_widget'] ); 1818 1819 $params[0] = $sidebar_args; 1820 return $params; 1821 } 1822 1823 /** 1824 * List of the tag names seen for before_widget strings. 1825 * 1826 * This is used in the {@see 'filter_wp_kses_allowed_html'} filter to ensure that the 1827 * data-* attributes can be allowed. 1828 * 1829 * @since 4.5.0 1830 * @var array 1831 */ 1832 protected $before_widget_tags_seen = array(); 1833 1834 /** 1835 * Ensures the HTML data-* attributes for selective refresh are allowed by kses. 1836 * 1837 * This is needed in case the `$before_widget` is run through wp_kses() when printed. 1838 * 1839 * @since 4.5.0 1840 * 1841 * @param array $allowed_html Allowed HTML. 1842 * @return array (Maybe) modified allowed HTML. 1843 */ 1844 public function filter_wp_kses_allowed_data_attributes( $allowed_html ) { 1845 foreach ( array_keys( $this->before_widget_tags_seen ) as $tag_name ) { 1846 if ( ! isset( $allowed_html[ $tag_name ] ) ) { 1847 $allowed_html[ $tag_name ] = array(); 1848 } 1849 $allowed_html[ $tag_name ] = array_merge( 1850 $allowed_html[ $tag_name ], 1851 array_fill_keys( 1852 array( 1853 'data-customize-partial-id', 1854 'data-customize-partial-type', 1855 'data-customize-partial-placement-context', 1856 'data-customize-partial-widget-id', 1857 'data-customize-partial-options', 1858 ), 1859 true 1860 ) 1861 ); 1862 } 1863 return $allowed_html; 1864 } 1865 1866 /** 1867 * Keep track of the number of times that dynamic_sidebar() was called for a given sidebar index. 1868 * 1869 * This helps facilitate the uncommon scenario where a single sidebar is rendered multiple times on a template. 1870 * 1871 * @since 4.5.0 1872 * @var array 1873 */ 1874 protected $sidebar_instance_count = array(); 1875 1876 /** 1877 * The current request's sidebar_instance_number context. 1878 * 1879 * @since 4.5.0 1880 * @var int|null 1881 */ 1882 protected $context_sidebar_instance_number; 1883 1884 /** 1885 * Current sidebar ID being rendered. 1886 * 1887 * @since 4.5.0 1888 * @var array 1889 */ 1890 protected $current_dynamic_sidebar_id_stack = array(); 1891 1892 /** 1893 * Begins keeping track of the current sidebar being rendered. 1894 * 1895 * Insert marker before widgets are rendered in a dynamic sidebar. 1896 * 1897 * @since 4.5.0 1898 * 1899 * @param int|string $index Index, name, or ID of the dynamic sidebar. 1900 */ 1901 public function start_dynamic_sidebar( $index ) { 1902 array_unshift( $this->current_dynamic_sidebar_id_stack, $index ); 1903 if ( ! isset( $this->sidebar_instance_count[ $index ] ) ) { 1904 $this->sidebar_instance_count[ $index ] = 0; 1905 } 1906 $this->sidebar_instance_count[ $index ] += 1; 1907 if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { 1908 printf( "\n<!--dynamic_sidebar_before:%s:%d-->\n", esc_html( $index ), (int) $this->sidebar_instance_count[ $index ] ); 1909 } 1910 } 1911 1912 /** 1913 * Finishes keeping track of the current sidebar being rendered. 1914 * 1915 * Inserts a marker after widgets are rendered in a dynamic sidebar. 1916 * 1917 * @since 4.5.0 1918 * 1919 * @param int|string $index Index, name, or ID of the dynamic sidebar. 1920 */ 1921 public function end_dynamic_sidebar( $index ) { 1922 array_shift( $this->current_dynamic_sidebar_id_stack ); 1923 if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { 1924 printf( "\n<!--dynamic_sidebar_after:%s:%d-->\n", esc_html( $index ), (int) $this->sidebar_instance_count[ $index ] ); 1925 } 1926 } 1927 1928 /** 1929 * Current sidebar being rendered. 1930 * 1931 * @since 4.5.0 1932 * @var string|null 1933 */ 1934 protected $rendering_widget_id; 1935 1936 /** 1937 * Current widget being rendered. 1938 * 1939 * @since 4.5.0 1940 * @var string|null 1941 */ 1942 protected $rendering_sidebar_id; 1943 1944 /** 1945 * Filters sidebars_widgets to ensure the currently-rendered widget is the only widget in the current sidebar. 1946 * 1947 * @since 4.5.0 1948 * 1949 * @param array $sidebars_widgets Sidebars widgets. 1950 * @return array Filtered sidebars widgets. 1951 */ 1952 public function filter_sidebars_widgets_for_rendering_widget( $sidebars_widgets ) { 1953 $sidebars_widgets[ $this->rendering_sidebar_id ] = array( $this->rendering_widget_id ); 1954 return $sidebars_widgets; 1955 } 1956 1957 /** 1958 * Renders a specific widget using the supplied sidebar arguments. 1959 * 1960 * @since 4.5.0 1961 * 1962 * @see dynamic_sidebar() 1963 * 1964 * @param WP_Customize_Partial $partial Partial. 1965 * @param array $context { 1966 * Sidebar args supplied as container context. 1967 * 1968 * @type string $sidebar_id ID for sidebar for widget to render into. 1969 * @type int $sidebar_instance_number Disambiguating instance number. 1970 * } 1971 * @return string|false 1972 */ 1973 public function render_widget_partial( $partial, $context ) { 1974 $id_data = $partial->id_data(); 1975 $widget_id = array_shift( $id_data['keys'] ); 1976 1977 if ( ! is_array( $context ) 1978 || empty( $context['sidebar_id'] ) 1979 || ! is_registered_sidebar( $context['sidebar_id'] ) 1980 ) { 1981 return false; 1982 } 1983 1984 $this->rendering_sidebar_id = $context['sidebar_id']; 1985 1986 if ( isset( $context['sidebar_instance_number'] ) ) { 1987 $this->context_sidebar_instance_number = (int) $context['sidebar_instance_number']; 1988 } 1989 1990 // Filter sidebars_widgets so that only the queried widget is in the sidebar. 1991 $this->rendering_widget_id = $widget_id; 1992 1993 $filter_callback = array( $this, 'filter_sidebars_widgets_for_rendering_widget' ); 1994 add_filter( 'sidebars_widgets', $filter_callback, 1000 ); 1995 1996 // Render the widget. 1997 ob_start(); 1998 $this->rendering_sidebar_id = $context['sidebar_id']; 1999 dynamic_sidebar( $this->rendering_sidebar_id ); 2000 $container = ob_get_clean(); 2001 2002 // Reset variables for next partial render. 2003 remove_filter( 'sidebars_widgets', $filter_callback, 1000 ); 2004 2005 $this->context_sidebar_instance_number = null; 2006 $this->rendering_sidebar_id = null; 2007 $this->rendering_widget_id = null; 2008 2009 return $container; 2010 } 2011 2012 // 2013 // Option Update Capturing. 2014 // 2015 2016 /** 2017 * List of captured widget option updates. 2018 * 2019 * @since 3.9.0 2020 * @var array $_captured_options Values updated while option capture is happening. 2021 */ 2022 protected $_captured_options = array(); 2023 2024 /** 2025 * Whether option capture is currently happening. 2026 * 2027 * @since 3.9.0 2028 * @var bool $_is_current Whether option capture is currently happening or not. 2029 */ 2030 protected $_is_capturing_option_updates = false; 2031 2032 /** 2033 * Determines whether the captured option update should be ignored. 2034 * 2035 * @since 3.9.0 2036 * 2037 * @param string $option_name Option name. 2038 * @return bool Whether the option capture is ignored. 2039 */ 2040 protected function is_option_capture_ignored( $option_name ) { 2041 return ( 0 === strpos( $option_name, '_transient_' ) ); 2042 } 2043 2044 /** 2045 * Retrieves captured widget option updates. 2046 * 2047 * @since 3.9.0 2048 * 2049 * @return array Array of captured options. 2050 */ 2051 protected function get_captured_options() { 2052 return $this->_captured_options; 2053 } 2054 2055 /** 2056 * Retrieves the option that was captured from being saved. 2057 * 2058 * @since 4.2.0 2059 * 2060 * @param string $option_name Option name. 2061 * @param mixed $default_value Optional. Default value to return if the option does not exist. Default false. 2062 * @return mixed Value set for the option. 2063 */ 2064 protected function get_captured_option( $option_name, $default_value = false ) { 2065 if ( array_key_exists( $option_name, $this->_captured_options ) ) { 2066 $value = $this->_captured_options[ $option_name ]; 2067 } else { 2068 $value = $default_value; 2069 } 2070 return $value; 2071 } 2072 2073 /** 2074 * Retrieves the number of captured widget option updates. 2075 * 2076 * @since 3.9.0 2077 * 2078 * @return int Number of updated options. 2079 */ 2080 protected function count_captured_options() { 2081 return count( $this->_captured_options ); 2082 } 2083 2084 /** 2085 * Begins keeping track of changes to widget options, caching new values. 2086 * 2087 * @since 3.9.0 2088 */ 2089 protected function start_capturing_option_updates() { 2090 if ( $this->_is_capturing_option_updates ) { 2091 return; 2092 } 2093 2094 $this->_is_capturing_option_updates = true; 2095 2096 add_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10, 3 ); 2097 } 2098 2099 /** 2100 * Pre-filters captured option values before updating. 2101 * 2102 * @since 3.9.0 2103 * 2104 * @param mixed $new_value The new option value. 2105 * @param string $option_name Name of the option. 2106 * @param mixed $old_value The old option value. 2107 * @return mixed Filtered option value. 2108 */ 2109 public function capture_filter_pre_update_option( $new_value, $option_name, $old_value ) { 2110 if ( $this->is_option_capture_ignored( $option_name ) ) { 2111 return $new_value; 2112 } 2113 2114 if ( ! isset( $this->_captured_options[ $option_name ] ) ) { 2115 add_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); 2116 } 2117 2118 $this->_captured_options[ $option_name ] = $new_value; 2119 2120 return $old_value; 2121 } 2122 2123 /** 2124 * Pre-filters captured option values before retrieving. 2125 * 2126 * @since 3.9.0 2127 * 2128 * @param mixed $value Value to return instead of the option value. 2129 * @return mixed Filtered option value. 2130 */ 2131 public function capture_filter_pre_get_option( $value ) { 2132 $option_name = preg_replace( '/^pre_option_/', '', current_filter() ); 2133 2134 if ( isset( $this->_captured_options[ $option_name ] ) ) { 2135 $value = $this->_captured_options[ $option_name ]; 2136 2137 /** This filter is documented in wp-includes/option.php */ 2138 $value = apply_filters( 'option_' . $option_name, $value, $option_name ); 2139 } 2140 2141 return $value; 2142 } 2143 2144 /** 2145 * Undoes any changes to the options since options capture began. 2146 * 2147 * @since 3.9.0 2148 */ 2149 protected function stop_capturing_option_updates() { 2150 if ( ! $this->_is_capturing_option_updates ) { 2151 return; 2152 } 2153 2154 remove_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10 ); 2155 2156 foreach ( array_keys( $this->_captured_options ) as $option_name ) { 2157 remove_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); 2158 } 2159 2160 $this->_captured_options = array(); 2161 $this->_is_capturing_option_updates = false; 2162 } 2163 2164 /** 2165 * {@internal Missing Summary} 2166 * 2167 * See the {@see 'customize_dynamic_setting_args'} filter. 2168 * 2169 * @since 3.9.0 2170 * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 2171 */ 2172 public function setup_widget_addition_previews() { 2173 _deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); 2174 } 2175 2176 /** 2177 * {@internal Missing Summary} 2178 * 2179 * See the {@see 'customize_dynamic_setting_args'} filter. 2180 * 2181 * @since 3.9.0 2182 * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 2183 */ 2184 public function prepreview_added_sidebars_widgets() { 2185 _deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); 2186 } 2187 2188 /** 2189 * {@internal Missing Summary} 2190 * 2191 * See the {@see 'customize_dynamic_setting_args'} filter. 2192 * 2193 * @since 3.9.0 2194 * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 2195 */ 2196 public function prepreview_added_widget_instance() { 2197 _deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); 2198 } 2199 2200 /** 2201 * {@internal Missing Summary} 2202 * 2203 * See the {@see 'customize_dynamic_setting_args'} filter. 2204 * 2205 * @since 3.9.0 2206 * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 2207 */ 2208 public function remove_prepreview_filters() { 2209 _deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); 2210 } 2211 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |