[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Core Widgets API 4 * 5 * This API is used for creating dynamic sidebar without hardcoding functionality into 6 * themes 7 * 8 * Includes both internal WordPress routines and theme-use routines. 9 * 10 * This functionality was found in a plugin before the WordPress 2.2 release, which 11 * included it in the core from that point on. 12 * 13 * @link https://wordpress.org/documentation/article/manage-wordpress-widgets/ 14 * @link https://developer.wordpress.org/themes/functionality/widgets/ 15 * 16 * @package WordPress 17 * @subpackage Widgets 18 * @since 2.2.0 19 */ 20 21 // 22 // Global Variables. 23 // 24 25 /** @ignore */ 26 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; 27 28 /** 29 * Stores the sidebars, since many themes can have more than one. 30 * 31 * @global array $wp_registered_sidebars The registered sidebars. 32 * @since 2.2.0 33 */ 34 $wp_registered_sidebars = array(); 35 36 /** 37 * Stores the registered widgets. 38 * 39 * @global array $wp_registered_widgets The registered widgets. 40 * @since 2.2.0 41 */ 42 $wp_registered_widgets = array(); 43 44 /** 45 * Stores the registered widget controls (options). 46 * 47 * @global array $wp_registered_widget_controls The registered widget controls. 48 * @since 2.2.0 49 */ 50 $wp_registered_widget_controls = array(); 51 /** 52 * @global array $wp_registered_widget_updates The registered widget updates. 53 */ 54 $wp_registered_widget_updates = array(); 55 56 /** 57 * Private 58 * 59 * @global array $_wp_sidebars_widgets 60 */ 61 $_wp_sidebars_widgets = array(); 62 63 /** 64 * Private 65 * 66 * @global array $_wp_deprecated_widgets_callbacks 67 */ 68 $GLOBALS['_wp_deprecated_widgets_callbacks'] = array( 69 'wp_widget_pages', 70 'wp_widget_pages_control', 71 'wp_widget_calendar', 72 'wp_widget_calendar_control', 73 'wp_widget_archives', 74 'wp_widget_archives_control', 75 'wp_widget_links', 76 'wp_widget_meta', 77 'wp_widget_meta_control', 78 'wp_widget_search', 79 'wp_widget_recent_entries', 80 'wp_widget_recent_entries_control', 81 'wp_widget_tag_cloud', 82 'wp_widget_tag_cloud_control', 83 'wp_widget_categories', 84 'wp_widget_categories_control', 85 'wp_widget_text', 86 'wp_widget_text_control', 87 'wp_widget_rss', 88 'wp_widget_rss_control', 89 'wp_widget_recent_comments', 90 'wp_widget_recent_comments_control', 91 ); 92 93 // 94 // Template tags & API functions. 95 // 96 97 /** 98 * Register a widget 99 * 100 * Registers a WP_Widget widget 101 * 102 * @since 2.8.0 103 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 104 * instead of simply a `WP_Widget` subclass name. 105 * 106 * @see WP_Widget 107 * 108 * @global WP_Widget_Factory $wp_widget_factory 109 * 110 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 111 */ 112 function register_widget( $widget ) { 113 global $wp_widget_factory; 114 115 $wp_widget_factory->register( $widget ); 116 } 117 118 /** 119 * Unregisters a widget. 120 * 121 * Unregisters a WP_Widget widget. Useful for un-registering default widgets. 122 * Run within a function hooked to the {@see 'widgets_init'} action. 123 * 124 * @since 2.8.0 125 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 126 * instead of simply a `WP_Widget` subclass name. 127 * 128 * @see WP_Widget 129 * 130 * @global WP_Widget_Factory $wp_widget_factory 131 * 132 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 133 */ 134 function unregister_widget( $widget ) { 135 global $wp_widget_factory; 136 137 $wp_widget_factory->unregister( $widget ); 138 } 139 140 /** 141 * Creates multiple sidebars. 142 * 143 * If you wanted to quickly create multiple sidebars for a theme or internally. 144 * This function will allow you to do so. If you don't pass the 'name' and/or 145 * 'id' in `$args`, then they will be built for you. 146 * 147 * @since 2.2.0 148 * 149 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here. 150 * 151 * @global array $wp_registered_sidebars The new sidebars are stored in this array by sidebar ID. 152 * 153 * @param int $number Optional. Number of sidebars to create. Default 1. 154 * @param array|string $args { 155 * Optional. Array or string of arguments for building a sidebar. 156 * 157 * @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple 158 * sidebars are being defined, the ID will have "-2" appended, and so on. 159 * Default 'sidebar-' followed by the number the sidebar creation is currently at. 160 * @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering 161 * more than one sidebar, include '%d' in the string as a placeholder for the uniquely 162 * assigned number for each sidebar. 163 * Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'. 164 * } 165 */ 166 function register_sidebars( $number = 1, $args = array() ) { 167 global $wp_registered_sidebars; 168 $number = (int) $number; 169 170 if ( is_string( $args ) ) { 171 parse_str( $args, $args ); 172 } 173 174 for ( $i = 1; $i <= $number; $i++ ) { 175 $_args = $args; 176 177 if ( $number > 1 ) { 178 if ( isset( $args['name'] ) ) { 179 $_args['name'] = sprintf( $args['name'], $i ); 180 } else { 181 /* translators: %d: Sidebar number. */ 182 $_args['name'] = sprintf( __( 'Sidebar %d' ), $i ); 183 } 184 } else { 185 $_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' ); 186 } 187 188 /* 189 * Custom specified ID's are suffixed if they exist already. 190 * Automatically generated sidebar names need to be suffixed regardless starting at -0. 191 */ 192 if ( isset( $args['id'] ) ) { 193 $_args['id'] = $args['id']; 194 $n = 2; // Start at -2 for conflicting custom IDs. 195 while ( is_registered_sidebar( $_args['id'] ) ) { 196 $_args['id'] = $args['id'] . '-' . $n++; 197 } 198 } else { 199 $n = count( $wp_registered_sidebars ); 200 do { 201 $_args['id'] = 'sidebar-' . ++$n; 202 } while ( is_registered_sidebar( $_args['id'] ) ); 203 } 204 register_sidebar( $_args ); 205 } 206 } 207 208 /** 209 * Builds the definition for a single sidebar and returns the ID. 210 * 211 * Accepts either a string or an array and then parses that against a set 212 * of default arguments for the new sidebar. WordPress will automatically 213 * generate a sidebar ID and name based on the current number of registered 214 * sidebars if those arguments are not included. 215 * 216 * When allowing for automatic generation of the name and ID parameters, keep 217 * in mind that the incrementor for your sidebar can change over time depending 218 * on what other plugins and themes are installed. 219 * 220 * If theme support for 'widgets' has not yet been added when this function is 221 * called, it will be automatically enabled through the use of add_theme_support() 222 * 223 * @since 2.2.0 224 * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments. 225 * @since 5.9.0 Added the `show_in_rest` argument. 226 * 227 * @global array $wp_registered_sidebars The registered sidebars. 228 * 229 * @param array|string $args { 230 * Optional. Array or string of arguments for the sidebar being registered. 231 * 232 * @type string $name The name or title of the sidebar displayed in the Widgets 233 * interface. Default 'Sidebar $instance'. 234 * @type string $id The unique identifier by which the sidebar will be called. 235 * Default 'sidebar-$instance'. 236 * @type string $description Description of the sidebar, displayed in the Widgets interface. 237 * Default empty string. 238 * @type string $class Extra CSS class to assign to the sidebar in the Widgets interface. 239 * Default empty. 240 * @type string $before_widget HTML content to prepend to each widget's HTML output when assigned 241 * to this sidebar. Receives the widget's ID attribute as `%1$s` 242 * and class name as `%2$s`. Default is an opening list item element. 243 * @type string $after_widget HTML content to append to each widget's HTML output when assigned 244 * to this sidebar. Default is a closing list item element. 245 * @type string $before_title HTML content to prepend to the sidebar title when displayed. 246 * Default is an opening h2 element. 247 * @type string $after_title HTML content to append to the sidebar title when displayed. 248 * Default is a closing h2 element. 249 * @type string $before_sidebar HTML content to prepend to the sidebar when displayed. 250 * Receives the `$id` argument as `%1$s` and `$class` as `%2$s`. 251 * Outputs after the {@see 'dynamic_sidebar_before'} action. 252 * Default empty string. 253 * @type string $after_sidebar HTML content to append to the sidebar when displayed. 254 * Outputs before the {@see 'dynamic_sidebar_after'} action. 255 * Default empty string. 256 * @type bool $show_in_rest Whether to show this sidebar publicly in the REST API. 257 * Defaults to only showing the sidebar to administrator users. 258 * } 259 * @return string Sidebar ID added to $wp_registered_sidebars global. 260 */ 261 function register_sidebar( $args = array() ) { 262 global $wp_registered_sidebars; 263 264 $i = count( $wp_registered_sidebars ) + 1; 265 266 $id_is_empty = empty( $args['id'] ); 267 268 $defaults = array( 269 /* translators: %d: Sidebar number. */ 270 'name' => sprintf( __( 'Sidebar %d' ), $i ), 271 'id' => "sidebar-$i", 272 'description' => '', 273 'class' => '', 274 'before_widget' => '<li id="%1$s" class="widget %2$s">', 275 'after_widget' => "</li>\n", 276 'before_title' => '<h2 class="widgettitle">', 277 'after_title' => "</h2>\n", 278 'before_sidebar' => '', 279 'after_sidebar' => '', 280 'show_in_rest' => false, 281 ); 282 283 /** 284 * Filters the sidebar default arguments. 285 * 286 * @since 5.3.0 287 * 288 * @see register_sidebar() 289 * 290 * @param array $defaults The default sidebar arguments. 291 */ 292 $sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) ); 293 294 if ( $id_is_empty ) { 295 _doing_it_wrong( 296 __FUNCTION__, 297 sprintf( 298 /* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */ 299 __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), 300 '<code>id</code>', 301 $sidebar['name'], 302 $sidebar['id'] 303 ), 304 '4.2.0' 305 ); 306 } 307 308 $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; 309 310 add_theme_support( 'widgets' ); 311 312 /** 313 * Fires once a sidebar has been registered. 314 * 315 * @since 3.0.0 316 * 317 * @param array $sidebar Parsed arguments for the registered sidebar. 318 */ 319 do_action( 'register_sidebar', $sidebar ); 320 321 return $sidebar['id']; 322 } 323 324 /** 325 * Removes a sidebar from the list. 326 * 327 * @since 2.2.0 328 * 329 * @global array $wp_registered_sidebars The registered sidebars. 330 * 331 * @param string|int $sidebar_id The ID of the sidebar when it was registered. 332 */ 333 function unregister_sidebar( $sidebar_id ) { 334 global $wp_registered_sidebars; 335 336 unset( $wp_registered_sidebars[ $sidebar_id ] ); 337 } 338 339 /** 340 * Checks if a sidebar is registered. 341 * 342 * @since 4.4.0 343 * 344 * @global array $wp_registered_sidebars The registered sidebars. 345 * 346 * @param string|int $sidebar_id The ID of the sidebar when it was registered. 347 * @return bool True if the sidebar is registered, false otherwise. 348 */ 349 function is_registered_sidebar( $sidebar_id ) { 350 global $wp_registered_sidebars; 351 352 return isset( $wp_registered_sidebars[ $sidebar_id ] ); 353 } 354 355 /** 356 * Register an instance of a widget. 357 * 358 * The default widget option is 'classname' that can be overridden. 359 * 360 * The function can also be used to un-register widgets when `$output_callback` 361 * parameter is an empty string. 362 * 363 * @since 2.2.0 364 * @since 5.3.0 Formalized the existing and already documented `...$params` parameter 365 * by adding it to the function signature. 366 * @since 5.8.0 Added show_instance_in_rest option. 367 * 368 * @global array $wp_registered_widgets Uses stored registered widgets. 369 * @global array $wp_registered_widget_controls Stores the registered widget controls (options). 370 * @global array $wp_registered_widget_updates The registered widget updates. 371 * @global array $_wp_deprecated_widgets_callbacks 372 * 373 * @param int|string $id Widget ID. 374 * @param string $name Widget display title. 375 * @param callable $output_callback Run when widget is called. 376 * @param array $options { 377 * Optional. An array of supplementary widget options for the instance. 378 * 379 * @type string $classname Class name for the widget's HTML container. Default is a shortened 380 * version of the output callback name. 381 * @type string $description Widget description for display in the widget administration 382 * panel and/or theme. 383 * @type bool $show_instance_in_rest Whether to show the widget's instance settings in the REST API. 384 * Only available for WP_Widget based widgets. 385 * } 386 * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. 387 */ 388 function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array(), ...$params ) { 389 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; 390 391 $id = strtolower( $id ); 392 393 if ( empty( $output_callback ) ) { 394 unset( $wp_registered_widgets[ $id ] ); 395 return; 396 } 397 398 $id_base = _get_widget_id_base( $id ); 399 if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) { 400 unset( $wp_registered_widget_controls[ $id ] ); 401 unset( $wp_registered_widget_updates[ $id_base ] ); 402 return; 403 } 404 405 $defaults = array( 'classname' => $output_callback ); 406 $options = wp_parse_args( $options, $defaults ); 407 $widget = array( 408 'name' => $name, 409 'id' => $id, 410 'callback' => $output_callback, 411 'params' => $params, 412 ); 413 $widget = array_merge( $widget, $options ); 414 415 if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) { 416 417 /** 418 * Fires once for each registered widget. 419 * 420 * @since 3.0.0 421 * 422 * @param array $widget An array of default widget arguments. 423 */ 424 do_action( 'wp_register_sidebar_widget', $widget ); 425 $wp_registered_widgets[ $id ] = $widget; 426 } 427 } 428 429 /** 430 * Retrieve description for widget. 431 * 432 * When registering widgets, the options can also include 'description' that 433 * describes the widget for display on the widget administration panel or 434 * in the theme. 435 * 436 * @since 2.5.0 437 * 438 * @global array $wp_registered_widgets The registered widgets. 439 * 440 * @param int|string $id Widget ID. 441 * @return string|void Widget description, if available. 442 */ 443 function wp_widget_description( $id ) { 444 if ( ! is_scalar( $id ) ) { 445 return; 446 } 447 448 global $wp_registered_widgets; 449 450 if ( isset( $wp_registered_widgets[ $id ]['description'] ) ) { 451 return esc_html( $wp_registered_widgets[ $id ]['description'] ); 452 } 453 } 454 455 /** 456 * Retrieve description for a sidebar. 457 * 458 * When registering sidebars a 'description' parameter can be included that 459 * describes the sidebar for display on the widget administration panel. 460 * 461 * @since 2.9.0 462 * 463 * @global array $wp_registered_sidebars The registered sidebars. 464 * 465 * @param string $id sidebar ID. 466 * @return string|void Sidebar description, if available. 467 */ 468 function wp_sidebar_description( $id ) { 469 if ( ! is_scalar( $id ) ) { 470 return; 471 } 472 473 global $wp_registered_sidebars; 474 475 if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) { 476 return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' ); 477 } 478 } 479 480 /** 481 * Remove widget from sidebar. 482 * 483 * @since 2.2.0 484 * 485 * @param int|string $id Widget ID. 486 */ 487 function wp_unregister_sidebar_widget( $id ) { 488 489 /** 490 * Fires just before a widget is removed from a sidebar. 491 * 492 * @since 3.0.0 493 * 494 * @param int|string $id The widget ID. 495 */ 496 do_action( 'wp_unregister_sidebar_widget', $id ); 497 498 wp_register_sidebar_widget( $id, '', '' ); 499 wp_unregister_widget_control( $id ); 500 } 501 502 /** 503 * Registers widget control callback for customizing options. 504 * 505 * @since 2.2.0 506 * @since 5.3.0 Formalized the existing and already documented `...$params` parameter 507 * by adding it to the function signature. 508 * 509 * @global array $wp_registered_widget_controls The registered widget controls. 510 * @global array $wp_registered_widget_updates The registered widget updates. 511 * @global array $wp_registered_widgets The registered widgets. 512 * @global array $_wp_deprecated_widgets_callbacks 513 * 514 * @param int|string $id Sidebar ID. 515 * @param string $name Sidebar display name. 516 * @param callable $control_callback Run when sidebar is displayed. 517 * @param array $options { 518 * Optional. Array or string of control options. Default empty array. 519 * 520 * @type int $height Never used. Default 200. 521 * @type int $width Width of the fully expanded control form (but try hard to use the default width). 522 * Default 250. 523 * @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the 524 * text widget. The widget ID will end up looking like `{$id_base}-{$unique_number}`. 525 * } 526 * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. 527 */ 528 function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) { 529 global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks; 530 531 $id = strtolower( $id ); 532 $id_base = _get_widget_id_base( $id ); 533 534 if ( empty( $control_callback ) ) { 535 unset( $wp_registered_widget_controls[ $id ] ); 536 unset( $wp_registered_widget_updates[ $id_base ] ); 537 return; 538 } 539 540 if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) { 541 unset( $wp_registered_widgets[ $id ] ); 542 return; 543 } 544 545 if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { 546 return; 547 } 548 549 $defaults = array( 550 'width' => 250, 551 'height' => 200, 552 ); // Height is never used. 553 $options = wp_parse_args( $options, $defaults ); 554 $options['width'] = (int) $options['width']; 555 $options['height'] = (int) $options['height']; 556 557 $widget = array( 558 'name' => $name, 559 'id' => $id, 560 'callback' => $control_callback, 561 'params' => $params, 562 ); 563 $widget = array_merge( $widget, $options ); 564 565 $wp_registered_widget_controls[ $id ] = $widget; 566 567 if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { 568 return; 569 } 570 571 if ( isset( $widget['params'][0]['number'] ) ) { 572 $widget['params'][0]['number'] = -1; 573 } 574 575 unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] ); 576 $wp_registered_widget_updates[ $id_base ] = $widget; 577 } 578 579 /** 580 * Registers the update callback for a widget. 581 * 582 * @since 2.8.0 583 * @since 5.3.0 Formalized the existing and already documented `...$params` parameter 584 * by adding it to the function signature. 585 * 586 * @global array $wp_registered_widget_updates The registered widget updates. 587 * 588 * @param string $id_base The base ID of a widget created by extending WP_Widget. 589 * @param callable $update_callback Update callback method for the widget. 590 * @param array $options Optional. Widget control options. See wp_register_widget_control(). 591 * Default empty array. 592 * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. 593 */ 594 function _register_widget_update_callback( $id_base, $update_callback, $options = array(), ...$params ) { 595 global $wp_registered_widget_updates; 596 597 if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { 598 if ( empty( $update_callback ) ) { 599 unset( $wp_registered_widget_updates[ $id_base ] ); 600 } 601 return; 602 } 603 604 $widget = array( 605 'callback' => $update_callback, 606 'params' => $params, 607 ); 608 609 $widget = array_merge( $widget, $options ); 610 $wp_registered_widget_updates[ $id_base ] = $widget; 611 } 612 613 /** 614 * Registers the form callback for a widget. 615 * 616 * @since 2.8.0 617 * @since 5.3.0 Formalized the existing and already documented `...$params` parameter 618 * by adding it to the function signature. 619 * 620 * @global array $wp_registered_widget_controls The registered widget controls. 621 * 622 * @param int|string $id Widget ID. 623 * @param string $name Name attribute for the widget. 624 * @param callable $form_callback Form callback. 625 * @param array $options Optional. Widget control options. See wp_register_widget_control(). 626 * Default empty array. 627 * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. 628 */ 629 630 function _register_widget_form_callback( $id, $name, $form_callback, $options = array(), ...$params ) { 631 global $wp_registered_widget_controls; 632 633 $id = strtolower( $id ); 634 635 if ( empty( $form_callback ) ) { 636 unset( $wp_registered_widget_controls[ $id ] ); 637 return; 638 } 639 640 if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { 641 return; 642 } 643 644 $defaults = array( 645 'width' => 250, 646 'height' => 200, 647 ); 648 $options = wp_parse_args( $options, $defaults ); 649 $options['width'] = (int) $options['width']; 650 $options['height'] = (int) $options['height']; 651 652 $widget = array( 653 'name' => $name, 654 'id' => $id, 655 'callback' => $form_callback, 656 'params' => $params, 657 ); 658 $widget = array_merge( $widget, $options ); 659 660 $wp_registered_widget_controls[ $id ] = $widget; 661 } 662 663 /** 664 * Remove control callback for widget. 665 * 666 * @since 2.2.0 667 * 668 * @param int|string $id Widget ID. 669 */ 670 function wp_unregister_widget_control( $id ) { 671 wp_register_widget_control( $id, '', '' ); 672 } 673 674 /** 675 * Display dynamic sidebar. 676 * 677 * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or 678 * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter. 679 * Otherwise, you can pass in a numerical index to display the sidebar at that index. 680 * 681 * @since 2.2.0 682 * 683 * @global array $wp_registered_sidebars The registered sidebars. 684 * @global array $wp_registered_widgets The registered widgets. 685 * 686 * @param int|string $index Optional. Index, name or ID of dynamic sidebar. Default 1. 687 * @return bool True, if widget sidebar was found and called. False if not found or not called. 688 */ 689 function dynamic_sidebar( $index = 1 ) { 690 global $wp_registered_sidebars, $wp_registered_widgets; 691 692 if ( is_int( $index ) ) { 693 $index = "sidebar-$index"; 694 } else { 695 $index = sanitize_title( $index ); 696 foreach ( (array) $wp_registered_sidebars as $key => $value ) { 697 if ( sanitize_title( $value['name'] ) === $index ) { 698 $index = $key; 699 break; 700 } 701 } 702 } 703 704 $sidebars_widgets = wp_get_sidebars_widgets(); 705 if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) { 706 /** This action is documented in wp-includes/widget.php */ 707 do_action( 'dynamic_sidebar_before', $index, false ); 708 /** This action is documented in wp-includes/widget.php */ 709 do_action( 'dynamic_sidebar_after', $index, false ); 710 /** This filter is documented in wp-includes/widget.php */ 711 return apply_filters( 'dynamic_sidebar_has_widgets', false, $index ); 712 } 713 714 $sidebar = $wp_registered_sidebars[ $index ]; 715 716 $sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] ); 717 718 /** 719 * Fires before widgets are rendered in a dynamic sidebar. 720 * 721 * Note: The action also fires for empty sidebars, and on both the front end 722 * and back end, including the Inactive Widgets sidebar on the Widgets screen. 723 * 724 * @since 3.9.0 725 * 726 * @param int|string $index Index, name, or ID of the dynamic sidebar. 727 * @param bool $has_widgets Whether the sidebar is populated with widgets. 728 * Default true. 729 */ 730 do_action( 'dynamic_sidebar_before', $index, true ); 731 732 if ( ! is_admin() && ! empty( $sidebar['before_sidebar'] ) ) { 733 echo $sidebar['before_sidebar']; 734 } 735 736 $did_one = false; 737 foreach ( (array) $sidebars_widgets[ $index ] as $id ) { 738 739 if ( ! isset( $wp_registered_widgets[ $id ] ) ) { 740 continue; 741 } 742 743 $params = array_merge( 744 array( 745 array_merge( 746 $sidebar, 747 array( 748 'widget_id' => $id, 749 'widget_name' => $wp_registered_widgets[ $id ]['name'], 750 ) 751 ), 752 ), 753 (array) $wp_registered_widgets[ $id ]['params'] 754 ); 755 756 // Substitute HTML `id` and `class` attributes into `before_widget`. 757 $classname_ = ''; 758 foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) { 759 if ( is_string( $cn ) ) { 760 $classname_ .= '_' . $cn; 761 } elseif ( is_object( $cn ) ) { 762 $classname_ .= '_' . get_class( $cn ); 763 } 764 } 765 $classname_ = ltrim( $classname_, '_' ); 766 767 $params[0]['before_widget'] = sprintf( 768 $params[0]['before_widget'], 769 str_replace( '\\', '_', $id ), 770 $classname_ 771 ); 772 773 /** 774 * Filters the parameters passed to a widget's display callback. 775 * 776 * Note: The filter is evaluated on both the front end and back end, 777 * including for the Inactive Widgets sidebar on the Widgets screen. 778 * 779 * @since 2.5.0 780 * 781 * @see register_sidebar() 782 * 783 * @param array $params { 784 * @type array $args { 785 * An array of widget display arguments. 786 * 787 * @type string $name Name of the sidebar the widget is assigned to. 788 * @type string $id ID of the sidebar the widget is assigned to. 789 * @type string $description The sidebar description. 790 * @type string $class CSS class applied to the sidebar container. 791 * @type string $before_widget HTML markup to prepend to each widget in the sidebar. 792 * @type string $after_widget HTML markup to append to each widget in the sidebar. 793 * @type string $before_title HTML markup to prepend to the widget title when displayed. 794 * @type string $after_title HTML markup to append to the widget title when displayed. 795 * @type string $widget_id ID of the widget. 796 * @type string $widget_name Name of the widget. 797 * } 798 * @type array $widget_args { 799 * An array of multi-widget arguments. 800 * 801 * @type int $number Number increment used for multiples of the same widget. 802 * } 803 * } 804 */ 805 $params = apply_filters( 'dynamic_sidebar_params', $params ); 806 807 $callback = $wp_registered_widgets[ $id ]['callback']; 808 809 /** 810 * Fires before a widget's display callback is called. 811 * 812 * Note: The action fires on both the front end and back end, including 813 * for widgets in the Inactive Widgets sidebar on the Widgets screen. 814 * 815 * The action is not fired for empty sidebars. 816 * 817 * @since 3.0.0 818 * 819 * @param array $widget { 820 * An associative array of widget arguments. 821 * 822 * @type string $name Name of the widget. 823 * @type string $id Widget ID. 824 * @type callable $callback When the hook is fired on the front end, `$callback` is an array 825 * containing the widget object. Fired on the back end, `$callback` 826 * is 'wp_widget_control', see `$_callback`. 827 * @type array $params An associative array of multi-widget arguments. 828 * @type string $classname CSS class applied to the widget container. 829 * @type string $description The widget description. 830 * @type array $_callback When the hook is fired on the back end, `$_callback` is populated 831 * with an array containing the widget object, see `$callback`. 832 * } 833 */ 834 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] ); 835 836 if ( is_callable( $callback ) ) { 837 call_user_func_array( $callback, $params ); 838 $did_one = true; 839 } 840 } 841 842 if ( ! is_admin() && ! empty( $sidebar['after_sidebar'] ) ) { 843 echo $sidebar['after_sidebar']; 844 } 845 846 /** 847 * Fires after widgets are rendered in a dynamic sidebar. 848 * 849 * Note: The action also fires for empty sidebars, and on both the front end 850 * and back end, including the Inactive Widgets sidebar on the Widgets screen. 851 * 852 * @since 3.9.0 853 * 854 * @param int|string $index Index, name, or ID of the dynamic sidebar. 855 * @param bool $has_widgets Whether the sidebar is populated with widgets. 856 * Default true. 857 */ 858 do_action( 'dynamic_sidebar_after', $index, true ); 859 860 /** 861 * Filters whether a sidebar has widgets. 862 * 863 * Note: The filter is also evaluated for empty sidebars, and on both the front end 864 * and back end, including the Inactive Widgets sidebar on the Widgets screen. 865 * 866 * @since 3.9.0 867 * 868 * @param bool $did_one Whether at least one widget was rendered in the sidebar. 869 * Default false. 870 * @param int|string $index Index, name, or ID of the dynamic sidebar. 871 */ 872 return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index ); 873 } 874 875 /** 876 * Determines whether a given widget is displayed on the front end. 877 * 878 * Either $callback or $id_base can be used 879 * $id_base is the first argument when extending WP_Widget class 880 * Without the optional $widget_id parameter, returns the ID of the first sidebar 881 * in which the first instance of the widget with the given callback or $id_base is found. 882 * With the $widget_id parameter, returns the ID of the sidebar where 883 * the widget with that callback/$id_base AND that ID is found. 884 * 885 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective 886 * this function has to run after widgets have initialized, at action {@see 'init'} or later. 887 * 888 * For more information on this and similar theme functions, check out 889 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 890 * Conditional Tags} article in the Theme Developer Handbook. 891 * 892 * @since 2.2.0 893 * 894 * @global array $wp_registered_widgets The registered widgets. 895 * 896 * @param callable|false $callback Optional. Widget callback to check. Default false. 897 * @param string|false $widget_id Optional. Widget ID. Optional, but needed for checking. 898 * Default false. 899 * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget. 900 * Default false. 901 * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'. 902 * Default true. 903 * @return string|false ID of the sidebar in which the widget is active, 904 * false if the widget is not active. 905 */ 906 function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { 907 global $wp_registered_widgets; 908 909 $sidebars_widgets = wp_get_sidebars_widgets(); 910 911 if ( is_array( $sidebars_widgets ) ) { 912 foreach ( $sidebars_widgets as $sidebar => $widgets ) { 913 if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) ) { 914 continue; 915 } 916 917 if ( is_array( $widgets ) ) { 918 foreach ( $widgets as $widget ) { 919 if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) { 920 if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) { 921 return $sidebar; 922 } 923 } 924 } 925 } 926 } 927 } 928 return false; 929 } 930 931 /** 932 * Determines whether the dynamic sidebar is enabled and used by the theme. 933 * 934 * For more information on this and similar theme functions, check out 935 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 936 * Conditional Tags} article in the Theme Developer Handbook. 937 * 938 * @since 2.2.0 939 * 940 * @global array $wp_registered_widgets The registered widgets. 941 * @global array $wp_registered_sidebars The registered sidebars. 942 * 943 * @return bool True if using widgets, false otherwise. 944 */ 945 function is_dynamic_sidebar() { 946 global $wp_registered_widgets, $wp_registered_sidebars; 947 948 $sidebars_widgets = get_option( 'sidebars_widgets' ); 949 950 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { 951 if ( ! empty( $sidebars_widgets[ $index ] ) ) { 952 foreach ( (array) $sidebars_widgets[ $index ] as $widget ) { 953 if ( array_key_exists( $widget, $wp_registered_widgets ) ) { 954 return true; 955 } 956 } 957 } 958 } 959 960 return false; 961 } 962 963 /** 964 * Determines whether a sidebar contains widgets. 965 * 966 * For more information on this and similar theme functions, check out 967 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 968 * Conditional Tags} article in the Theme Developer Handbook. 969 * 970 * @since 2.8.0 971 * 972 * @param string|int $index Sidebar name, id or number to check. 973 * @return bool True if the sidebar has widgets, false otherwise. 974 */ 975 function is_active_sidebar( $index ) { 976 $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index ); 977 $sidebars_widgets = wp_get_sidebars_widgets(); 978 $is_active_sidebar = ! empty( $sidebars_widgets[ $index ] ); 979 980 /** 981 * Filters whether a dynamic sidebar is considered "active". 982 * 983 * @since 3.9.0 984 * 985 * @param bool $is_active_sidebar Whether or not the sidebar should be considered "active". 986 * In other words, whether the sidebar contains any widgets. 987 * @param int|string $index Index, name, or ID of the dynamic sidebar. 988 */ 989 return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index ); 990 } 991 992 // 993 // Internal Functions. 994 // 995 996 /** 997 * Retrieve full list of sidebars and their widget instance IDs. 998 * 999 * Will upgrade sidebar widget list, if needed. Will also save updated list, if 1000 * needed. 1001 * 1002 * @since 2.2.0 1003 * @access private 1004 * 1005 * @global array $_wp_sidebars_widgets 1006 * @global array $sidebars_widgets 1007 * 1008 * @param bool $deprecated Not used (argument deprecated). 1009 * @return array Upgraded list of widgets to version 3 array format when called from the admin. 1010 */ 1011 function wp_get_sidebars_widgets( $deprecated = true ) { 1012 if ( true !== $deprecated ) { 1013 _deprecated_argument( __FUNCTION__, '2.8.1' ); 1014 } 1015 1016 global $_wp_sidebars_widgets, $sidebars_widgets; 1017 1018 /* 1019 * If loading from front page, consult $_wp_sidebars_widgets rather than options 1020 * to see if wp_convert_widget_settings() has made manipulations in memory. 1021 */ 1022 if ( ! is_admin() ) { 1023 if ( empty( $_wp_sidebars_widgets ) ) { 1024 $_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() ); 1025 } 1026 1027 $sidebars_widgets = $_wp_sidebars_widgets; 1028 } else { 1029 $sidebars_widgets = get_option( 'sidebars_widgets', array() ); 1030 } 1031 1032 if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) { 1033 unset( $sidebars_widgets['array_version'] ); 1034 } 1035 1036 /** 1037 * Filters the list of sidebars and their widgets. 1038 * 1039 * @since 2.7.0 1040 * 1041 * @param array $sidebars_widgets An associative array of sidebars and their widgets. 1042 */ 1043 return apply_filters( 'sidebars_widgets', $sidebars_widgets ); 1044 } 1045 1046 /** 1047 * Retrieves the registered sidebar with the given ID. 1048 * 1049 * @since 5.9.0 1050 * 1051 * @global array $wp_registered_sidebars The registered sidebars. 1052 * 1053 * @param string $id The sidebar ID. 1054 * @return array|null The discovered sidebar, or null if it is not registered. 1055 */ 1056 function wp_get_sidebar( $id ) { 1057 global $wp_registered_sidebars; 1058 1059 foreach ( (array) $wp_registered_sidebars as $sidebar ) { 1060 if ( $sidebar['id'] === $id ) { 1061 return $sidebar; 1062 } 1063 } 1064 1065 if ( 'wp_inactive_widgets' === $id ) { 1066 return array( 1067 'id' => 'wp_inactive_widgets', 1068 'name' => __( 'Inactive widgets' ), 1069 ); 1070 } 1071 1072 return null; 1073 } 1074 1075 /** 1076 * Set the sidebar widget option to update sidebars. 1077 * 1078 * @since 2.2.0 1079 * @access private 1080 * 1081 * @global array $_wp_sidebars_widgets 1082 * @param array $sidebars_widgets Sidebar widgets and their settings. 1083 */ 1084 function wp_set_sidebars_widgets( $sidebars_widgets ) { 1085 global $_wp_sidebars_widgets; 1086 1087 // Clear cached value used in wp_get_sidebars_widgets(). 1088 $_wp_sidebars_widgets = null; 1089 1090 if ( ! isset( $sidebars_widgets['array_version'] ) ) { 1091 $sidebars_widgets['array_version'] = 3; 1092 } 1093 1094 update_option( 'sidebars_widgets', $sidebars_widgets ); 1095 } 1096 1097 /** 1098 * Retrieve default registered sidebars list. 1099 * 1100 * @since 2.2.0 1101 * @access private 1102 * 1103 * @global array $wp_registered_sidebars The registered sidebars. 1104 * 1105 * @return array 1106 */ 1107 function wp_get_widget_defaults() { 1108 global $wp_registered_sidebars; 1109 1110 $defaults = array(); 1111 1112 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { 1113 $defaults[ $index ] = array(); 1114 } 1115 1116 return $defaults; 1117 } 1118 1119 /** 1120 * Converts the widget settings from single to multi-widget format. 1121 * 1122 * @since 2.8.0 1123 * 1124 * @global array $_wp_sidebars_widgets 1125 * 1126 * @param string $base_name Root ID for all widgets of this type. 1127 * @param string $option_name Option name for this widget type. 1128 * @param array $settings The array of widget instance settings. 1129 * @return array The array of widget settings converted to multi-widget format. 1130 */ 1131 function wp_convert_widget_settings( $base_name, $option_name, $settings ) { 1132 // This test may need expanding. 1133 $single = false; 1134 $changed = false; 1135 1136 if ( empty( $settings ) ) { 1137 $single = true; 1138 } else { 1139 foreach ( array_keys( $settings ) as $number ) { 1140 if ( 'number' === $number ) { 1141 continue; 1142 } 1143 if ( ! is_numeric( $number ) ) { 1144 $single = true; 1145 break; 1146 } 1147 } 1148 } 1149 1150 if ( $single ) { 1151 $settings = array( 2 => $settings ); 1152 1153 // If loading from the front page, update sidebar in memory but don't save to options. 1154 if ( is_admin() ) { 1155 $sidebars_widgets = get_option( 'sidebars_widgets' ); 1156 } else { 1157 if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) { 1158 $GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() ); 1159 } 1160 $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets']; 1161 } 1162 1163 foreach ( (array) $sidebars_widgets as $index => $sidebar ) { 1164 if ( is_array( $sidebar ) ) { 1165 foreach ( $sidebar as $i => $name ) { 1166 if ( $base_name === $name ) { 1167 $sidebars_widgets[ $index ][ $i ] = "$name-2"; 1168 $changed = true; 1169 break 2; 1170 } 1171 } 1172 } 1173 } 1174 1175 if ( is_admin() && $changed ) { 1176 update_option( 'sidebars_widgets', $sidebars_widgets ); 1177 } 1178 } 1179 1180 $settings['_multiwidget'] = 1; 1181 if ( is_admin() ) { 1182 update_option( $option_name, $settings ); 1183 } 1184 1185 return $settings; 1186 } 1187 1188 /** 1189 * Output an arbitrary widget as a template tag. 1190 * 1191 * @since 2.8.0 1192 * 1193 * @global WP_Widget_Factory $wp_widget_factory 1194 * 1195 * @param string $widget The widget's PHP class name (see class-wp-widget.php). 1196 * @param array $instance Optional. The widget's instance settings. Default empty array. 1197 * @param array $args { 1198 * Optional. Array of arguments to configure the display of the widget. 1199 * 1200 * @type string $before_widget HTML content that will be prepended to the widget's HTML output. 1201 * Default `<div class="widget %s">`, where `%s` is the widget's class name. 1202 * @type string $after_widget HTML content that will be appended to the widget's HTML output. 1203 * Default `</div>`. 1204 * @type string $before_title HTML content that will be prepended to the widget's title when displayed. 1205 * Default `<h2 class="widgettitle">`. 1206 * @type string $after_title HTML content that will be appended to the widget's title when displayed. 1207 * Default `</h2>`. 1208 * } 1209 */ 1210 function the_widget( $widget, $instance = array(), $args = array() ) { 1211 global $wp_widget_factory; 1212 1213 if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { 1214 _doing_it_wrong( 1215 __FUNCTION__, 1216 sprintf( 1217 /* translators: %s: register_widget() */ 1218 __( 'Widgets need to be registered using %s, before they can be displayed.' ), 1219 '<code>register_widget()</code>' 1220 ), 1221 '4.9.0' 1222 ); 1223 return; 1224 } 1225 1226 $widget_obj = $wp_widget_factory->widgets[ $widget ]; 1227 if ( ! ( $widget_obj instanceof WP_Widget ) ) { 1228 return; 1229 } 1230 1231 $default_args = array( 1232 'before_widget' => '<div class="widget %s">', 1233 'after_widget' => '</div>', 1234 'before_title' => '<h2 class="widgettitle">', 1235 'after_title' => '</h2>', 1236 ); 1237 $args = wp_parse_args( $args, $default_args ); 1238 $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); 1239 1240 $instance = wp_parse_args( $instance ); 1241 1242 /** This filter is documented in wp-includes/class-wp-widget.php */ 1243 $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args ); 1244 1245 if ( false === $instance ) { 1246 return; 1247 } 1248 1249 /** 1250 * Fires before rendering the requested widget. 1251 * 1252 * @since 3.0.0 1253 * 1254 * @param string $widget The widget's class name. 1255 * @param array $instance The current widget instance's settings. 1256 * @param array $args An array of the widget's sidebar arguments. 1257 */ 1258 do_action( 'the_widget', $widget, $instance, $args ); 1259 1260 $widget_obj->_set( -1 ); 1261 $widget_obj->widget( $args, $instance ); 1262 } 1263 1264 /** 1265 * Retrieves the widget ID base value. 1266 * 1267 * @since 2.8.0 1268 * 1269 * @param string $id Widget ID. 1270 * @return string Widget ID base. 1271 */ 1272 function _get_widget_id_base( $id ) { 1273 return preg_replace( '/-[0-9]+$/', '', $id ); 1274 } 1275 1276 /** 1277 * Handle sidebars config after theme change 1278 * 1279 * @access private 1280 * @since 3.3.0 1281 * 1282 * @global array $sidebars_widgets 1283 */ 1284 function _wp_sidebars_changed() { 1285 global $sidebars_widgets; 1286 1287 if ( ! is_array( $sidebars_widgets ) ) { 1288 $sidebars_widgets = wp_get_sidebars_widgets(); 1289 } 1290 1291 retrieve_widgets( true ); 1292 } 1293 1294 /** 1295 * Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar, 1296 * and saves the widget settings. This has to run at least on each theme change. 1297 * 1298 * For example, let's say theme A has a "footer" sidebar, and theme B doesn't have one. 1299 * After switching from theme A to theme B, all the widgets previously assigned 1300 * to the footer would be inaccessible. This function detects this scenario, and 1301 * moves all the widgets previously assigned to the footer under wp_inactive_widgets. 1302 * 1303 * Despite the word "retrieve" in the name, this function actually updates the database 1304 * and the global `$sidebars_widgets`. For that reason it should not be run on front end, 1305 * unless the `$theme_changed` value is 'customize' (to bypass the database write). 1306 * 1307 * @since 2.8.0 1308 * 1309 * @global array $wp_registered_sidebars The registered sidebars. 1310 * @global array $sidebars_widgets 1311 * @global array $wp_registered_widgets The registered widgets. 1312 * 1313 * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value 1314 * of 'customize' defers updates for the Customizer. 1315 * @return array Updated sidebars widgets. 1316 */ 1317 function retrieve_widgets( $theme_changed = false ) { 1318 global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets; 1319 1320 $registered_sidebars_keys = array_keys( $wp_registered_sidebars ); 1321 $registered_widgets_ids = array_keys( $wp_registered_widgets ); 1322 1323 if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) { 1324 if ( empty( $sidebars_widgets ) ) { 1325 return array(); 1326 } 1327 1328 unset( $sidebars_widgets['array_version'] ); 1329 1330 $sidebars_widgets_keys = array_keys( $sidebars_widgets ); 1331 sort( $sidebars_widgets_keys ); 1332 sort( $registered_sidebars_keys ); 1333 1334 if ( $sidebars_widgets_keys === $registered_sidebars_keys ) { 1335 $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); 1336 1337 return $sidebars_widgets; 1338 } 1339 } 1340 1341 // Discard invalid, theme-specific widgets from sidebars. 1342 $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); 1343 $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets ); 1344 1345 // Find hidden/lost multi-widget instances. 1346 $shown_widgets = array_merge( ...array_values( $sidebars_widgets ) ); 1347 $lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets ); 1348 1349 foreach ( $lost_widgets as $key => $widget_id ) { 1350 $number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id ); 1351 1352 // Only keep active and default widgets. 1353 if ( is_numeric( $number ) && (int) $number < 2 ) { 1354 unset( $lost_widgets[ $key ] ); 1355 } 1356 } 1357 $sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] ); 1358 1359 if ( 'customize' !== $theme_changed ) { 1360 // Update the widgets settings in the database. 1361 wp_set_sidebars_widgets( $sidebars_widgets ); 1362 } 1363 1364 return $sidebars_widgets; 1365 } 1366 1367 /** 1368 * Compares a list of sidebars with their widgets against an allowed list. 1369 * 1370 * @since 4.9.0 1371 * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping. 1372 * 1373 * @global array $wp_registered_sidebars The registered sidebars. 1374 * 1375 * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs. 1376 * @return array Mapped sidebars widgets. 1377 */ 1378 function wp_map_sidebars_widgets( $existing_sidebars_widgets ) { 1379 global $wp_registered_sidebars; 1380 1381 $new_sidebars_widgets = array( 1382 'wp_inactive_widgets' => array(), 1383 ); 1384 1385 // Short-circuit if there are no sidebars to map. 1386 if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) { 1387 return $new_sidebars_widgets; 1388 } 1389 1390 foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) { 1391 if ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) { 1392 $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets ); 1393 unset( $existing_sidebars_widgets[ $sidebar ] ); 1394 } 1395 } 1396 1397 // If old and new theme have just one sidebar, map it and we're done. 1398 if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) { 1399 $new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets ); 1400 1401 return $new_sidebars_widgets; 1402 } 1403 1404 // Map locations with the same slug. 1405 $existing_sidebars = array_keys( $existing_sidebars_widgets ); 1406 1407 foreach ( $wp_registered_sidebars as $sidebar => $name ) { 1408 if ( in_array( $sidebar, $existing_sidebars, true ) ) { 1409 $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ]; 1410 unset( $existing_sidebars_widgets[ $sidebar ] ); 1411 } elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) { 1412 $new_sidebars_widgets[ $sidebar ] = array(); 1413 } 1414 } 1415 1416 // If there are more sidebars, try to map them. 1417 if ( ! empty( $existing_sidebars_widgets ) ) { 1418 1419 /* 1420 * If old and new theme both have sidebars that contain phrases 1421 * from within the same group, make an educated guess and map it. 1422 */ 1423 $common_slug_groups = array( 1424 array( 'sidebar', 'primary', 'main', 'right' ), 1425 array( 'second', 'left' ), 1426 array( 'sidebar-2', 'footer', 'bottom' ), 1427 array( 'header', 'top' ), 1428 ); 1429 1430 // Go through each group... 1431 foreach ( $common_slug_groups as $slug_group ) { 1432 1433 // ...and see if any of these slugs... 1434 foreach ( $slug_group as $slug ) { 1435 1436 // ...and any of the new sidebars... 1437 foreach ( $wp_registered_sidebars as $new_sidebar => $args ) { 1438 1439 // ...actually match! 1440 if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) { 1441 continue; 1442 } 1443 1444 // Then see if any of the existing sidebars... 1445 foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) { 1446 1447 // ...and any slug in the same group... 1448 foreach ( $slug_group as $slug ) { 1449 1450 // ... have a match as well. 1451 if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) { 1452 continue; 1453 } 1454 1455 // Make sure this sidebar wasn't mapped and removed previously. 1456 if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) { 1457 1458 // We have a match that can be mapped! 1459 $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] ); 1460 1461 // Remove the mapped sidebar so it can't be mapped again. 1462 unset( $existing_sidebars_widgets[ $sidebar ] ); 1463 1464 // Go back and check the next new sidebar. 1465 continue 3; 1466 } 1467 } // End foreach ( $slug_group as $slug ). 1468 } // End foreach ( $existing_sidebars_widgets as $sidebar => $widgets ). 1469 } // End foreach ( $wp_registered_sidebars as $new_sidebar => $args ). 1470 } // End foreach ( $slug_group as $slug ). 1471 } // End foreach ( $common_slug_groups as $slug_group ). 1472 } 1473 1474 // Move any left over widgets to inactive sidebar. 1475 foreach ( $existing_sidebars_widgets as $widgets ) { 1476 if ( is_array( $widgets ) && ! empty( $widgets ) ) { 1477 $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], $widgets ); 1478 } 1479 } 1480 1481 // Sidebars_widgets settings from when this theme was previously active. 1482 $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' ); 1483 $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false; 1484 1485 if ( is_array( $old_sidebars_widgets ) ) { 1486 1487 // Remove empty sidebars, no need to map those. 1488 $old_sidebars_widgets = array_filter( $old_sidebars_widgets ); 1489 1490 // Only check sidebars that are empty or have not been mapped to yet. 1491 foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) { 1492 if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) { 1493 unset( $old_sidebars_widgets[ $new_sidebar ] ); 1494 } 1495 } 1496 1497 // Remove orphaned widgets, we're only interested in previously active sidebars. 1498 foreach ( $old_sidebars_widgets as $sidebar => $widgets ) { 1499 if ( str_starts_with( $sidebar, 'orphaned_widgets' ) ) { 1500 unset( $old_sidebars_widgets[ $sidebar ] ); 1501 } 1502 } 1503 1504 $old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets ); 1505 1506 if ( ! empty( $old_sidebars_widgets ) ) { 1507 1508 // Go through each remaining sidebar... 1509 foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) { 1510 1511 // ...and check every new sidebar... 1512 foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) { 1513 1514 // ...for every widget we're trying to revive. 1515 foreach ( $old_widgets as $key => $widget_id ) { 1516 $active_key = array_search( $widget_id, $new_widgets, true ); 1517 1518 // If the widget is used elsewhere... 1519 if ( false !== $active_key ) { 1520 1521 // ...and that elsewhere is inactive widgets... 1522 if ( 'wp_inactive_widgets' === $new_sidebar ) { 1523 1524 // ...remove it from there and keep the active version... 1525 unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] ); 1526 } else { 1527 1528 // ...otherwise remove it from the old sidebar and keep it in the new one. 1529 unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] ); 1530 } 1531 } // End if ( $active_key ). 1532 } // End foreach ( $old_widgets as $key => $widget_id ). 1533 } // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ). 1534 } // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ). 1535 } // End if ( ! empty( $old_sidebars_widgets ) ). 1536 1537 // Restore widget settings from when theme was previously active. 1538 $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets ); 1539 } 1540 1541 return $new_sidebars_widgets; 1542 } 1543 1544 /** 1545 * Compares a list of sidebars with their widgets against an allowed list. 1546 * 1547 * @since 4.9.0 1548 * 1549 * @global array $wp_registered_widgets The registered widgets. 1550 * 1551 * @param array $sidebars_widgets List of sidebars and their widget instance IDs. 1552 * @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets. 1553 * @return array Sidebars with allowed widgets. 1554 */ 1555 function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) { 1556 if ( empty( $allowed_widget_ids ) ) { 1557 $allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] ); 1558 } 1559 1560 foreach ( $sidebars_widgets as $sidebar => $widgets ) { 1561 if ( is_array( $widgets ) ) { 1562 $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids ); 1563 } 1564 } 1565 1566 return $sidebars_widgets; 1567 } 1568 1569 /** 1570 * Display the RSS entries in a list. 1571 * 1572 * @since 2.5.0 1573 * 1574 * @param string|array|object $rss RSS url. 1575 * @param array $args Widget arguments. 1576 */ 1577 function wp_widget_rss_output( $rss, $args = array() ) { 1578 if ( is_string( $rss ) ) { 1579 $rss = fetch_feed( $rss ); 1580 } elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { 1581 $args = $rss; 1582 $rss = fetch_feed( $rss['url'] ); 1583 } elseif ( ! is_object( $rss ) ) { 1584 return; 1585 } 1586 1587 if ( is_wp_error( $rss ) ) { 1588 if ( is_admin() || current_user_can( 'manage_options' ) ) { 1589 echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</p>'; 1590 } 1591 return; 1592 } 1593 1594 $default_args = array( 1595 'show_author' => 0, 1596 'show_date' => 0, 1597 'show_summary' => 0, 1598 'items' => 0, 1599 ); 1600 $args = wp_parse_args( $args, $default_args ); 1601 1602 $items = (int) $args['items']; 1603 if ( $items < 1 || 20 < $items ) { 1604 $items = 10; 1605 } 1606 $show_summary = (int) $args['show_summary']; 1607 $show_author = (int) $args['show_author']; 1608 $show_date = (int) $args['show_date']; 1609 1610 if ( ! $rss->get_item_quantity() ) { 1611 echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; 1612 $rss->__destruct(); 1613 unset( $rss ); 1614 return; 1615 } 1616 1617 echo '<ul>'; 1618 foreach ( $rss->get_items( 0, $items ) as $item ) { 1619 $link = $item->get_link(); 1620 while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) { 1621 $link = substr( $link, 1 ); 1622 } 1623 $link = esc_url( strip_tags( $link ) ); 1624 1625 $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); 1626 if ( empty( $title ) ) { 1627 $title = __( 'Untitled' ); 1628 } 1629 1630 $desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); 1631 $desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); 1632 1633 $summary = ''; 1634 if ( $show_summary ) { 1635 $summary = $desc; 1636 1637 // Change existing [...] to […]. 1638 if ( str_ends_with( $summary, '[...]' ) ) { 1639 $summary = substr( $summary, 0, -5 ) . '[…]'; 1640 } 1641 1642 $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; 1643 } 1644 1645 $date = ''; 1646 if ( $show_date ) { 1647 $date = $item->get_date( 'U' ); 1648 1649 if ( $date ) { 1650 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>'; 1651 } 1652 } 1653 1654 $author = ''; 1655 if ( $show_author ) { 1656 $author = $item->get_author(); 1657 if ( is_object( $author ) ) { 1658 $author = $author->get_name(); 1659 $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>'; 1660 } 1661 } 1662 1663 if ( '' === $link ) { 1664 echo "<li>$title{$date}{$summary}{$author}</li>"; 1665 } elseif ( $show_summary ) { 1666 echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>"; 1667 } else { 1668 echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>"; 1669 } 1670 } 1671 echo '</ul>'; 1672 $rss->__destruct(); 1673 unset( $rss ); 1674 } 1675 1676 /** 1677 * Display RSS widget options form. 1678 * 1679 * The options for what fields are displayed for the RSS form are all booleans 1680 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author', 1681 * 'show_date'. 1682 * 1683 * @since 2.5.0 1684 * 1685 * @param array|string $args Values for input fields. 1686 * @param array $inputs Override default display options. 1687 */ 1688 function wp_widget_rss_form( $args, $inputs = null ) { 1689 $default_inputs = array( 1690 'url' => true, 1691 'title' => true, 1692 'items' => true, 1693 'show_summary' => true, 1694 'show_author' => true, 1695 'show_date' => true, 1696 ); 1697 $inputs = wp_parse_args( $inputs, $default_inputs ); 1698 1699 $args['title'] = isset( $args['title'] ) ? $args['title'] : ''; 1700 $args['url'] = isset( $args['url'] ) ? $args['url'] : ''; 1701 $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0; 1702 1703 if ( $args['items'] < 1 || 20 < $args['items'] ) { 1704 $args['items'] = 10; 1705 } 1706 1707 $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary']; 1708 $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author']; 1709 $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; 1710 1711 if ( ! empty( $args['error'] ) ) { 1712 echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $args['error'] ) . '</p>'; 1713 } 1714 1715 $esc_number = esc_attr( $args['number'] ); 1716 if ( $inputs['url'] ) : 1717 ?> 1718 <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label> 1719 <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p> 1720 <?php endif; if ( $inputs['title'] ) : ?> 1721 <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label> 1722 <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p> 1723 <?php endif; if ( $inputs['items'] ) : ?> 1724 <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label> 1725 <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]"> 1726 <?php 1727 for ( $i = 1; $i <= 20; ++$i ) { 1728 echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>"; 1729 } 1730 ?> 1731 </select></p> 1732 <?php endif; if ( $inputs['show_summary'] || $inputs['show_author'] || $inputs['show_date'] ) : ?> 1733 <p> 1734 <?php if ( $inputs['show_summary'] ) : ?> 1735 <input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> /> 1736 <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br /> 1737 <?php endif; if ( $inputs['show_author'] ) : ?> 1738 <input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> /> 1739 <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br /> 1740 <?php endif; if ( $inputs['show_date'] ) : ?> 1741 <input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/> 1742 <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br /> 1743 <?php endif; ?> 1744 </p> 1745 <?php 1746 endif; // End of display options. 1747 foreach ( array_keys( $default_inputs ) as $input ) : 1748 if ( 'hidden' === $inputs[ $input ] ) : 1749 $id = str_replace( '_', '-', $input ); 1750 ?> 1751 <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" /> 1752 <?php 1753 endif; 1754 endforeach; 1755 } 1756 1757 /** 1758 * Process RSS feed widget data and optionally retrieve feed items. 1759 * 1760 * The feed widget can not have more than 20 items or it will reset back to the 1761 * default, which is 10. 1762 * 1763 * The resulting array has the feed title, feed url, feed link (from channel), 1764 * feed items, error (if any), and whether to show summary, author, and date. 1765 * All respectively in the order of the array elements. 1766 * 1767 * @since 2.5.0 1768 * 1769 * @param array $widget_rss RSS widget feed data. Expects unescaped data. 1770 * @param bool $check_feed Optional. Whether to check feed for errors. Default true. 1771 * @return array 1772 */ 1773 function wp_widget_rss_process( $widget_rss, $check_feed = true ) { 1774 $items = (int) $widget_rss['items']; 1775 if ( $items < 1 || 20 < $items ) { 1776 $items = 10; 1777 } 1778 $url = sanitize_url( strip_tags( $widget_rss['url'] ) ); 1779 $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : ''; 1780 $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0; 1781 $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0; 1782 $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0; 1783 $error = false; 1784 $link = ''; 1785 1786 if ( $check_feed ) { 1787 $rss = fetch_feed( $url ); 1788 1789 if ( is_wp_error( $rss ) ) { 1790 $error = $rss->get_error_message(); 1791 } else { 1792 $link = esc_url( strip_tags( $rss->get_permalink() ) ); 1793 while ( stristr( $link, 'http' ) !== $link ) { 1794 $link = substr( $link, 1 ); 1795 } 1796 1797 $rss->__destruct(); 1798 unset( $rss ); 1799 } 1800 } 1801 1802 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' ); 1803 } 1804 1805 /** 1806 * Registers all of the default WordPress widgets on startup. 1807 * 1808 * Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered. 1809 * 1810 * @since 2.2.0 1811 */ 1812 function wp_widgets_init() { 1813 if ( ! is_blog_installed() ) { 1814 return; 1815 } 1816 1817 register_widget( 'WP_Widget_Pages' ); 1818 1819 register_widget( 'WP_Widget_Calendar' ); 1820 1821 register_widget( 'WP_Widget_Archives' ); 1822 1823 if ( get_option( 'link_manager_enabled' ) ) { 1824 register_widget( 'WP_Widget_Links' ); 1825 } 1826 1827 register_widget( 'WP_Widget_Media_Audio' ); 1828 1829 register_widget( 'WP_Widget_Media_Image' ); 1830 1831 register_widget( 'WP_Widget_Media_Gallery' ); 1832 1833 register_widget( 'WP_Widget_Media_Video' ); 1834 1835 register_widget( 'WP_Widget_Meta' ); 1836 1837 register_widget( 'WP_Widget_Search' ); 1838 1839 register_widget( 'WP_Widget_Text' ); 1840 1841 register_widget( 'WP_Widget_Categories' ); 1842 1843 register_widget( 'WP_Widget_Recent_Posts' ); 1844 1845 register_widget( 'WP_Widget_Recent_Comments' ); 1846 1847 register_widget( 'WP_Widget_RSS' ); 1848 1849 register_widget( 'WP_Widget_Tag_Cloud' ); 1850 1851 register_widget( 'WP_Nav_Menu_Widget' ); 1852 1853 register_widget( 'WP_Widget_Custom_HTML' ); 1854 1855 register_widget( 'WP_Widget_Block' ); 1856 1857 /** 1858 * Fires after all default WordPress widgets have been registered. 1859 * 1860 * @since 2.2.0 1861 */ 1862 do_action( 'widgets_init' ); 1863 } 1864 1865 /** 1866 * Enables the widgets block editor. This is hooked into 'after_setup_theme' so 1867 * that the block editor is enabled by default but can be disabled by themes. 1868 * 1869 * @since 5.8.0 1870 * 1871 * @access private 1872 */ 1873 function wp_setup_widgets_block_editor() { 1874 add_theme_support( 'widgets-block-editor' ); 1875 } 1876 1877 /** 1878 * Whether or not to use the block editor to manage widgets. Defaults to true 1879 * unless a theme has removed support for widgets-block-editor or a plugin has 1880 * filtered the return value of this function. 1881 * 1882 * @since 5.8.0 1883 * 1884 * @return bool Whether to use the block editor to manage widgets. 1885 */ 1886 function wp_use_widgets_block_editor() { 1887 /** 1888 * Filters whether to use the block editor to manage widgets. 1889 * 1890 * @since 5.8.0 1891 * 1892 * @param bool $use_widgets_block_editor Whether to use the block editor to manage widgets. 1893 */ 1894 return apply_filters( 1895 'use_widgets_block_editor', 1896 get_theme_support( 'widgets-block-editor' ) 1897 ); 1898 } 1899 1900 /** 1901 * Converts a widget ID into its id_base and number components. 1902 * 1903 * @since 5.8.0 1904 * 1905 * @param string $id Widget ID. 1906 * @return array Array containing a widget's id_base and number components. 1907 */ 1908 function wp_parse_widget_id( $id ) { 1909 $parsed = array(); 1910 1911 if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) { 1912 $parsed['id_base'] = $matches[1]; 1913 $parsed['number'] = (int) $matches[2]; 1914 } else { 1915 // Likely an old single widget. 1916 $parsed['id_base'] = $id; 1917 } 1918 1919 return $parsed; 1920 } 1921 1922 /** 1923 * Finds the sidebar that a given widget belongs to. 1924 * 1925 * @since 5.8.0 1926 * 1927 * @param string $widget_id The widget ID to look for. 1928 * @return string|null The found sidebar's ID, or null if it was not found. 1929 */ 1930 function wp_find_widgets_sidebar( $widget_id ) { 1931 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) { 1932 foreach ( $widget_ids as $maybe_widget_id ) { 1933 if ( $maybe_widget_id === $widget_id ) { 1934 return (string) $sidebar_id; 1935 } 1936 } 1937 } 1938 1939 return null; 1940 } 1941 1942 /** 1943 * Assigns a widget to the given sidebar. 1944 * 1945 * @since 5.8.0 1946 * 1947 * @param string $widget_id The widget ID to assign. 1948 * @param string $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar. 1949 */ 1950 function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) { 1951 $sidebars = wp_get_sidebars_widgets(); 1952 1953 foreach ( $sidebars as $maybe_sidebar_id => $widgets ) { 1954 foreach ( $widgets as $i => $maybe_widget_id ) { 1955 if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) { 1956 unset( $sidebars[ $maybe_sidebar_id ][ $i ] ); 1957 // We could technically break 2 here, but continue looping in case the ID is duplicated. 1958 continue 2; 1959 } 1960 } 1961 } 1962 1963 if ( $sidebar_id ) { 1964 $sidebars[ $sidebar_id ][] = $widget_id; 1965 } 1966 1967 wp_set_sidebars_widgets( $sidebars ); 1968 } 1969 1970 /** 1971 * Calls the render callback of a widget and returns the output. 1972 * 1973 * @since 5.8.0 1974 * 1975 * @global array $wp_registered_widgets The registered widgets. 1976 * @global array $wp_registered_sidebars The registered sidebars. 1977 * 1978 * @param string $widget_id Widget ID. 1979 * @param string $sidebar_id Sidebar ID. 1980 * @return string 1981 */ 1982 function wp_render_widget( $widget_id, $sidebar_id ) { 1983 global $wp_registered_widgets, $wp_registered_sidebars; 1984 1985 if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) { 1986 return ''; 1987 } 1988 1989 if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) { 1990 $sidebar = $wp_registered_sidebars[ $sidebar_id ]; 1991 } elseif ( 'wp_inactive_widgets' === $sidebar_id ) { 1992 $sidebar = array(); 1993 } else { 1994 return ''; 1995 } 1996 1997 $params = array_merge( 1998 array( 1999 array_merge( 2000 $sidebar, 2001 array( 2002 'widget_id' => $widget_id, 2003 'widget_name' => $wp_registered_widgets[ $widget_id ]['name'], 2004 ) 2005 ), 2006 ), 2007 (array) $wp_registered_widgets[ $widget_id ]['params'] 2008 ); 2009 2010 // Substitute HTML `id` and `class` attributes into `before_widget`. 2011 $classname_ = ''; 2012 foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) { 2013 if ( is_string( $cn ) ) { 2014 $classname_ .= '_' . $cn; 2015 } elseif ( is_object( $cn ) ) { 2016 $classname_ .= '_' . get_class( $cn ); 2017 } 2018 } 2019 $classname_ = ltrim( $classname_, '_' ); 2020 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ ); 2021 2022 /** This filter is documented in wp-includes/widgets.php */ 2023 $params = apply_filters( 'dynamic_sidebar_params', $params ); 2024 2025 $callback = $wp_registered_widgets[ $widget_id ]['callback']; 2026 2027 ob_start(); 2028 2029 /** This filter is documented in wp-includes/widgets.php */ 2030 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] ); 2031 2032 if ( is_callable( $callback ) ) { 2033 call_user_func_array( $callback, $params ); 2034 } 2035 2036 return ob_get_clean(); 2037 } 2038 2039 /** 2040 * Calls the control callback of a widget and returns the output. 2041 * 2042 * @since 5.8.0 2043 * 2044 * @global array $wp_registered_widget_controls The registered widget controls. 2045 * 2046 * @param string $id Widget ID. 2047 * @return string|null 2048 */ 2049 function wp_render_widget_control( $id ) { 2050 global $wp_registered_widget_controls; 2051 2052 if ( ! isset( $wp_registered_widget_controls[ $id ]['callback'] ) ) { 2053 return null; 2054 } 2055 2056 $callback = $wp_registered_widget_controls[ $id ]['callback']; 2057 $params = $wp_registered_widget_controls[ $id ]['params']; 2058 2059 ob_start(); 2060 2061 if ( is_callable( $callback ) ) { 2062 call_user_func_array( $callback, $params ); 2063 } 2064 2065 return ob_get_clean(); 2066 } 2067 2068 /** 2069 * Displays a _doing_it_wrong() message for conflicting widget editor scripts. 2070 * 2071 * The 'wp-editor' script module is exposed as window.wp.editor. This overrides 2072 * the legacy TinyMCE editor module which is required by the widgets editor. 2073 * Because of that conflict, these two shouldn't be enqueued together. 2074 * See https://core.trac.wordpress.org/ticket/53569. 2075 * 2076 * There is also another conflict related to styles where the block widgets 2077 * editor is hidden if a block enqueues 'wp-edit-post' stylesheet. 2078 * See https://core.trac.wordpress.org/ticket/53569. 2079 * 2080 * @since 5.8.0 2081 * @access private 2082 * 2083 * @global WP_Scripts $wp_scripts 2084 * @global WP_Styles $wp_styles 2085 */ 2086 function wp_check_widget_editor_deps() { 2087 global $wp_scripts, $wp_styles; 2088 2089 if ( 2090 $wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) || 2091 $wp_scripts->query( 'wp-customize-widgets', 'enqueued' ) 2092 ) { 2093 if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) { 2094 _doing_it_wrong( 2095 'wp_enqueue_script()', 2096 sprintf( 2097 /* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */ 2098 __( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ), 2099 'wp-editor', 2100 'wp-edit-widgets', 2101 'wp-customize-widgets' 2102 ), 2103 '5.8.0' 2104 ); 2105 } 2106 if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) { 2107 _doing_it_wrong( 2108 'wp_enqueue_style()', 2109 sprintf( 2110 /* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */ 2111 __( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ), 2112 'wp-edit-post', 2113 'wp-edit-widgets', 2114 'wp-customize-widgets' 2115 ), 2116 '5.8.0' 2117 ); 2118 } 2119 } 2120 } 2121 2122 /** 2123 * Registers the previous theme's sidebars for the block themes. 2124 * 2125 * @since 6.2.0 2126 * @access private 2127 * 2128 * @global array $wp_registered_sidebars The registered sidebars. 2129 */ 2130 function _wp_block_theme_register_classic_sidebars() { 2131 global $wp_registered_sidebars; 2132 2133 if ( ! wp_is_block_theme() ) { 2134 return; 2135 } 2136 2137 $classic_sidebars = get_theme_mod( 'wp_classic_sidebars' ); 2138 if ( empty( $classic_sidebars ) ) { 2139 return; 2140 } 2141 2142 // Don't use `register_sidebar` since it will enable the `widgets` support for a theme. 2143 foreach ( $classic_sidebars as $sidebar ) { 2144 $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; 2145 } 2146 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Tue Mar 19 08:20:01 2024 | Cross-referenced by PHPXref |