[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> widgets.php (source)

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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7