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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7