public/edition/wp-includes/widgets.php line 845

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