[ Index ]

PHP Cross Reference of WordPress

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 controls (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 IDs.
 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 Registered sidebars.
 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 Registered sidebars.
 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  Registered widgets.
 665   *
 666   * @param int|string $index Optional. Index, name or ID of dynamic sidebar. Default 1.
 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 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 callable|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  Registered widgets.
 901   * @global array $wp_registered_sidebars Registered sidebars.
 902   *
 903   * @return bool True if using widgets, false otherwise.
 904   */
 905  function is_dynamic_sidebar() {
 906      global $wp_registered_widgets, $wp_registered_sidebars;
 907  
 908      $sidebars_widgets = get_option( 'sidebars_widgets' );
 909  
 910      foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
 911          if ( ! empty( $sidebars_widgets[ $index ] ) ) {
 912              foreach ( (array) $sidebars_widgets[ $index ] as $widget ) {
 913                  if ( array_key_exists( $widget, $wp_registered_widgets ) ) {
 914                      return true;
 915                  }
 916              }
 917          }
 918      }
 919  
 920      return false;
 921  }
 922  
 923  /**
 924   * Determines whether a sidebar contains widgets.
 925   *
 926   * For more information on this and similar theme functions, check out
 927   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 928   * Conditional Tags} article in the Theme Developer Handbook.
 929   *
 930   * @since 2.8.0
 931   *
 932   * @param string|int $index Sidebar name, id or number to check.
 933   * @return bool True if the sidebar has widgets, false otherwise.
 934   */
 935  function is_active_sidebar( $index ) {
 936      $index             = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );
 937      $sidebars_widgets  = wp_get_sidebars_widgets();
 938      $is_active_sidebar = ! empty( $sidebars_widgets[ $index ] );
 939  
 940      /**
 941       * Filters whether a dynamic sidebar is considered "active".
 942       *
 943       * @since 3.9.0
 944       *
 945       * @param bool       $is_active_sidebar Whether or not the sidebar should be considered "active".
 946       *                                      In other words, whether the sidebar contains any widgets.
 947       * @param int|string $index             Index, name, or ID of the dynamic sidebar.
 948       */
 949      return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
 950  }
 951  
 952  //
 953  // Internal Functions.
 954  //
 955  
 956  /**
 957   * Retrieve full list of sidebars and their widget instance IDs.
 958   *
 959   * Will upgrade sidebar widget list, if needed. Will also save updated list, if
 960   * needed.
 961   *
 962   * @since 2.2.0
 963   * @access private
 964   *
 965   * @global array $_wp_sidebars_widgets
 966   * @global array $sidebars_widgets
 967   *
 968   * @param bool $deprecated Not used (argument deprecated).
 969   * @return array Upgraded list of widgets to version 3 array format when called from the admin.
 970   */
 971  function wp_get_sidebars_widgets( $deprecated = true ) {
 972      if ( true !== $deprecated ) {
 973          _deprecated_argument( __FUNCTION__, '2.8.1' );
 974      }
 975  
 976      global $_wp_sidebars_widgets, $sidebars_widgets;
 977  
 978      // If loading from front page, consult $_wp_sidebars_widgets rather than options
 979      // to see if wp_convert_widget_settings() has made manipulations in memory.
 980      if ( ! is_admin() ) {
 981          if ( empty( $_wp_sidebars_widgets ) ) {
 982              $_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
 983          }
 984  
 985          $sidebars_widgets = $_wp_sidebars_widgets;
 986      } else {
 987          $sidebars_widgets = get_option( 'sidebars_widgets', array() );
 988      }
 989  
 990      if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) {
 991          unset( $sidebars_widgets['array_version'] );
 992      }
 993  
 994      /**
 995       * Filters the list of sidebars and their widgets.
 996       *
 997       * @since 2.7.0
 998       *
 999       * @param array $sidebars_widgets An associative array of sidebars and their widgets.
1000       */
1001      return apply_filters( 'sidebars_widgets', $sidebars_widgets );
1002  }
1003  
1004  /**
1005   * Set the sidebar widget option to update sidebars.
1006   *
1007   * @since 2.2.0
1008   * @access private
1009   *
1010   * @global array $_wp_sidebars_widgets
1011   * @param array $sidebars_widgets Sidebar widgets and their settings.
1012   */
1013  function wp_set_sidebars_widgets( $sidebars_widgets ) {
1014      global $_wp_sidebars_widgets;
1015  
1016      // Clear cached value used in wp_get_sidebars_widgets().
1017      $_wp_sidebars_widgets = null;
1018  
1019      if ( ! isset( $sidebars_widgets['array_version'] ) ) {
1020          $sidebars_widgets['array_version'] = 3;
1021      }
1022  
1023      update_option( 'sidebars_widgets', $sidebars_widgets );
1024  }
1025  
1026  /**
1027   * Retrieve default registered sidebars list.
1028   *
1029   * @since 2.2.0
1030   * @access private
1031   *
1032   * @global array $wp_registered_sidebars Registered sidebars.
1033   *
1034   * @return array
1035   */
1036  function wp_get_widget_defaults() {
1037      global $wp_registered_sidebars;
1038  
1039      $defaults = array();
1040  
1041      foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
1042          $defaults[ $index ] = array();
1043      }
1044  
1045      return $defaults;
1046  }
1047  
1048  /**
1049   * Converts the widget settings from single to multi-widget format.
1050   *
1051   * @since 2.8.0
1052   *
1053   * @global array $_wp_sidebars_widgets
1054   *
1055   * @param string $base_name   Root ID for all widgets of this type.
1056   * @param string $option_name Option name for this widget type.
1057   * @param array  $settings    The array of widget instance settings.
1058   * @return array The array of widget settings converted to multi-widget format.
1059   */
1060  function wp_convert_widget_settings( $base_name, $option_name, $settings ) {
1061      // This test may need expanding.
1062      $single  = false;
1063      $changed = false;
1064  
1065      if ( empty( $settings ) ) {
1066          $single = true;
1067      } else {
1068          foreach ( array_keys( $settings ) as $number ) {
1069              if ( 'number' === $number ) {
1070                  continue;
1071              }
1072              if ( ! is_numeric( $number ) ) {
1073                  $single = true;
1074                  break;
1075              }
1076          }
1077      }
1078  
1079      if ( $single ) {
1080          $settings = array( 2 => $settings );
1081  
1082          // If loading from the front page, update sidebar in memory but don't save to options.
1083          if ( is_admin() ) {
1084              $sidebars_widgets = get_option( 'sidebars_widgets' );
1085          } else {
1086              if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) {
1087                  $GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() );
1088              }
1089              $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
1090          }
1091  
1092          foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
1093              if ( is_array( $sidebar ) ) {
1094                  foreach ( $sidebar as $i => $name ) {
1095                      if ( $base_name === $name ) {
1096                          $sidebars_widgets[ $index ][ $i ] = "$name-2";
1097                          $changed                          = true;
1098                          break 2;
1099                      }
1100                  }
1101              }
1102          }
1103  
1104          if ( is_admin() && $changed ) {
1105              update_option( 'sidebars_widgets', $sidebars_widgets );
1106          }
1107      }
1108  
1109      $settings['_multiwidget'] = 1;
1110      if ( is_admin() ) {
1111          update_option( $option_name, $settings );
1112      }
1113  
1114      return $settings;
1115  }
1116  
1117  /**
1118   * Output an arbitrary widget as a template tag.
1119   *
1120   * @since 2.8.0
1121   *
1122   * @global WP_Widget_Factory $wp_widget_factory
1123   *
1124   * @param string $widget   The widget's PHP class name (see class-wp-widget.php).
1125   * @param array  $instance Optional. The widget's instance settings. Default empty array.
1126   * @param array  $args {
1127   *     Optional. Array of arguments to configure the display of the widget.
1128   *
1129   *     @type string $before_widget HTML content that will be prepended to the widget's HTML output.
1130   *                                 Default `<div class="widget %s">`, where `%s` is the widget's class name.
1131   *     @type string $after_widget  HTML content that will be appended to the widget's HTML output.
1132   *                                 Default `</div>`.
1133   *     @type string $before_title  HTML content that will be prepended to the widget's title when displayed.
1134   *                                 Default `<h2 class="widgettitle">`.
1135   *     @type string $after_title   HTML content that will be appended to the widget's title when displayed.
1136   *                                 Default `</h2>`.
1137   * }
1138   */
1139  function the_widget( $widget, $instance = array(), $args = array() ) {
1140      global $wp_widget_factory;
1141  
1142      if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
1143          _doing_it_wrong(
1144              __FUNCTION__,
1145              sprintf(
1146                  /* translators: %s: register_widget() */
1147                  __( 'Widgets need to be registered using %s, before they can be displayed.' ),
1148                  '<code>register_widget()</code>'
1149              ),
1150              '4.9.0'
1151          );
1152          return;
1153      }
1154  
1155      $widget_obj = $wp_widget_factory->widgets[ $widget ];
1156      if ( ! ( $widget_obj instanceof WP_Widget ) ) {
1157          return;
1158      }
1159  
1160      $default_args          = array(
1161          'before_widget' => '<div class="widget %s">',
1162          'after_widget'  => '</div>',
1163          'before_title'  => '<h2 class="widgettitle">',
1164          'after_title'   => '</h2>',
1165      );
1166      $args                  = wp_parse_args( $args, $default_args );
1167      $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );
1168  
1169      $instance = wp_parse_args( $instance );
1170  
1171      /** This filter is documented in wp-includes/class-wp-widget.php */
1172      $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args );
1173  
1174      if ( false === $instance ) {
1175          return;
1176      }
1177  
1178      /**
1179       * Fires before rendering the requested widget.
1180       *
1181       * @since 3.0.0
1182       *
1183       * @param string $widget   The widget's class name.
1184       * @param array  $instance The current widget instance's settings.
1185       * @param array  $args     An array of the widget's sidebar arguments.
1186       */
1187      do_action( 'the_widget', $widget, $instance, $args );
1188  
1189      $widget_obj->_set( -1 );
1190      $widget_obj->widget( $args, $instance );
1191  }
1192  
1193  /**
1194   * Retrieves the widget ID base value.
1195   *
1196   * @since 2.8.0
1197   *
1198   * @param string $id Widget ID.
1199   * @return string Widget ID base.
1200   */
1201  function _get_widget_id_base( $id ) {
1202      return preg_replace( '/-[0-9]+$/', '', $id );
1203  }
1204  
1205  /**
1206   * Handle sidebars config after theme change
1207   *
1208   * @access private
1209   * @since 3.3.0
1210   *
1211   * @global array $sidebars_widgets
1212   */
1213  function _wp_sidebars_changed() {
1214      global $sidebars_widgets;
1215  
1216      if ( ! is_array( $sidebars_widgets ) ) {
1217          $sidebars_widgets = wp_get_sidebars_widgets();
1218      }
1219  
1220      retrieve_widgets( true );
1221  }
1222  
1223  /**
1224   * Look for "lost" widgets, this has to run at least on each theme change.
1225   *
1226   * @since 2.8.0
1227   *
1228   * @global array $wp_registered_sidebars Registered sidebars.
1229   * @global array $sidebars_widgets
1230   * @global array $wp_registered_widgets  Registered widgets.
1231   *
1232   * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value
1233   *                                   of 'customize' defers updates for the Customizer.
1234   * @return array Updated sidebars widgets.
1235   */
1236  function retrieve_widgets( $theme_changed = false ) {
1237      global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
1238  
1239      $registered_sidebars_keys = array_keys( $wp_registered_sidebars );
1240      $registered_widgets_ids   = array_keys( $wp_registered_widgets );
1241  
1242      if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) {
1243          if ( empty( $sidebars_widgets ) ) {
1244              return array();
1245          }
1246  
1247          unset( $sidebars_widgets['array_version'] );
1248  
1249          $sidebars_widgets_keys = array_keys( $sidebars_widgets );
1250          sort( $sidebars_widgets_keys );
1251          sort( $registered_sidebars_keys );
1252  
1253          if ( $sidebars_widgets_keys === $registered_sidebars_keys ) {
1254              $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
1255  
1256              return $sidebars_widgets;
1257          }
1258      }
1259  
1260      // Discard invalid, theme-specific widgets from sidebars.
1261      $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
1262      $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets );
1263  
1264      // Find hidden/lost multi-widget instances.
1265      $shown_widgets = array_merge( ...array_values( $sidebars_widgets ) );
1266      $lost_widgets  = array_diff( $registered_widgets_ids, $shown_widgets );
1267  
1268      foreach ( $lost_widgets as $key => $widget_id ) {
1269          $number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id );
1270  
1271          // Only keep active and default widgets.
1272          if ( is_numeric( $number ) && (int) $number < 2 ) {
1273              unset( $lost_widgets[ $key ] );
1274          }
1275      }
1276      $sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] );
1277  
1278      if ( 'customize' !== $theme_changed ) {
1279          wp_set_sidebars_widgets( $sidebars_widgets );
1280      }
1281  
1282      return $sidebars_widgets;
1283  }
1284  
1285  /**
1286   * Compares a list of sidebars with their widgets against an allowed list.
1287   *
1288   * @since 4.9.0
1289   * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.
1290   *
1291   * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs.
1292   * @return array Mapped sidebars widgets.
1293   */
1294  function wp_map_sidebars_widgets( $existing_sidebars_widgets ) {
1295      global $wp_registered_sidebars;
1296  
1297      $new_sidebars_widgets = array(
1298          'wp_inactive_widgets' => array(),
1299      );
1300  
1301      // Short-circuit if there are no sidebars to map.
1302      if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) {
1303          return $new_sidebars_widgets;
1304      }
1305  
1306      foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
1307          if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
1308              $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets );
1309              unset( $existing_sidebars_widgets[ $sidebar ] );
1310          }
1311      }
1312  
1313      // If old and new theme have just one sidebar, map it and we're done.
1314      if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) {
1315          $new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets );
1316  
1317          return $new_sidebars_widgets;
1318      }
1319  
1320      // Map locations with the same slug.
1321      $existing_sidebars = array_keys( $existing_sidebars_widgets );
1322  
1323      foreach ( $wp_registered_sidebars as $sidebar => $name ) {
1324          if ( in_array( $sidebar, $existing_sidebars, true ) ) {
1325              $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];
1326              unset( $existing_sidebars_widgets[ $sidebar ] );
1327          } elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {
1328              $new_sidebars_widgets[ $sidebar ] = array();
1329          }
1330      }
1331  
1332      // If there are more sidebars, try to map them.
1333      if ( ! empty( $existing_sidebars_widgets ) ) {
1334  
1335          /*
1336           * If old and new theme both have sidebars that contain phrases
1337           * from within the same group, make an educated guess and map it.
1338           */
1339          $common_slug_groups = array(
1340              array( 'sidebar', 'primary', 'main', 'right' ),
1341              array( 'second', 'left' ),
1342              array( 'sidebar-2', 'footer', 'bottom' ),
1343              array( 'header', 'top' ),
1344          );
1345  
1346          // Go through each group...
1347          foreach ( $common_slug_groups as $slug_group ) {
1348  
1349              // ...and see if any of these slugs...
1350              foreach ( $slug_group as $slug ) {
1351  
1352                  // ...and any of the new sidebars...
1353                  foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
1354  
1355                      // ...actually match!
1356                      if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
1357                          continue;
1358                      }
1359  
1360                      // Then see if any of the existing sidebars...
1361                      foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
1362  
1363                          // ...and any slug in the same group...
1364                          foreach ( $slug_group as $slug ) {
1365  
1366                              // ... have a match as well.
1367                              if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
1368                                  continue;
1369                              }
1370  
1371                              // Make sure this sidebar wasn't mapped and removed previously.
1372                              if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
1373  
1374                                  // We have a match that can be mapped!
1375                                  $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
1376  
1377                                  // Remove the mapped sidebar so it can't be mapped again.
1378                                  unset( $existing_sidebars_widgets[ $sidebar ] );
1379  
1380                                  // Go back and check the next new sidebar.
1381                                  continue 3;
1382                              }
1383                          } // End foreach ( $slug_group as $slug ).
1384                      } // End foreach ( $existing_sidebars_widgets as $sidebar => $widgets ).
1385                  } // End foreach ( $wp_registered_sidebars as $new_sidebar => $args ).
1386              } // End foreach ( $slug_group as $slug ).
1387          } // End foreach ( $common_slug_groups as $slug_group ).
1388      }
1389  
1390      // Move any left over widgets to inactive sidebar.
1391      foreach ( $existing_sidebars_widgets as $widgets ) {
1392          if ( is_array( $widgets ) && ! empty( $widgets ) ) {
1393              $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], $widgets );
1394          }
1395      }
1396  
1397      // Sidebars_widgets settings from when this theme was previously active.
1398      $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
1399      $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false;
1400  
1401      if ( is_array( $old_sidebars_widgets ) ) {
1402  
1403          // Remove empty sidebars, no need to map those.
1404          $old_sidebars_widgets = array_filter( $old_sidebars_widgets );
1405  
1406          // Only check sidebars that are empty or have not been mapped to yet.
1407          foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
1408              if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) {
1409                  unset( $old_sidebars_widgets[ $new_sidebar ] );
1410              }
1411          }
1412  
1413          // Remove orphaned widgets, we're only interested in previously active sidebars.
1414          foreach ( $old_sidebars_widgets as $sidebar => $widgets ) {
1415              if ( 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
1416                  unset( $old_sidebars_widgets[ $sidebar ] );
1417              }
1418          }
1419  
1420          $old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets );
1421  
1422          if ( ! empty( $old_sidebars_widgets ) ) {
1423  
1424              // Go through each remaining sidebar...
1425              foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) {
1426  
1427                  // ...and check every new sidebar...
1428                  foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
1429  
1430                      // ...for every widget we're trying to revive.
1431                      foreach ( $old_widgets as $key => $widget_id ) {
1432                          $active_key = array_search( $widget_id, $new_widgets, true );
1433  
1434                          // If the widget is used elsewhere...
1435                          if ( false !== $active_key ) {
1436  
1437                              // ...and that elsewhere is inactive widgets...
1438                              if ( 'wp_inactive_widgets' === $new_sidebar ) {
1439  
1440                                  // ...remove it from there and keep the active version...
1441                                  unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] );
1442                              } else {
1443  
1444                                  // ...otherwise remove it from the old sidebar and keep it in the new one.
1445                                  unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] );
1446                              }
1447                          } // End if ( $active_key ).
1448                      } // End foreach ( $old_widgets as $key => $widget_id ).
1449                  } // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ).
1450              } // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ).
1451          } // End if ( ! empty( $old_sidebars_widgets ) ).
1452  
1453          // Restore widget settings from when theme was previously active.
1454          $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets );
1455      }
1456  
1457      return $new_sidebars_widgets;
1458  }
1459  
1460  /**
1461   * Compares a list of sidebars with their widgets against an allowed list.
1462   *
1463   * @since 4.9.0
1464   *
1465   * @param array $sidebars_widgets   List of sidebars and their widget instance IDs.
1466   * @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets.
1467   * @return array Sidebars with allowed widgets.
1468   */
1469  function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) {
1470      if ( empty( $allowed_widget_ids ) ) {
1471          $allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] );
1472      }
1473  
1474      foreach ( $sidebars_widgets as $sidebar => $widgets ) {
1475          if ( is_array( $widgets ) ) {
1476              $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids );
1477          }
1478      }
1479  
1480      return $sidebars_widgets;
1481  }
1482  
1483  /**
1484   * Display the RSS entries in a list.
1485   *
1486   * @since 2.5.0
1487   *
1488   * @param string|array|object $rss  RSS url.
1489   * @param array               $args Widget arguments.
1490   */
1491  function wp_widget_rss_output( $rss, $args = array() ) {
1492      if ( is_string( $rss ) ) {
1493          $rss = fetch_feed( $rss );
1494      } elseif ( is_array( $rss ) && isset( $rss['url'] ) ) {
1495          $args = $rss;
1496          $rss  = fetch_feed( $rss['url'] );
1497      } elseif ( ! is_object( $rss ) ) {
1498          return;
1499      }
1500  
1501      if ( is_wp_error( $rss ) ) {
1502          if ( is_admin() || current_user_can( 'manage_options' ) ) {
1503              echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>';
1504          }
1505          return;
1506      }
1507  
1508      $default_args = array(
1509          'show_author'  => 0,
1510          'show_date'    => 0,
1511          'show_summary' => 0,
1512          'items'        => 0,
1513      );
1514      $args         = wp_parse_args( $args, $default_args );
1515  
1516      $items = (int) $args['items'];
1517      if ( $items < 1 || 20 < $items ) {
1518          $items = 10;
1519      }
1520      $show_summary = (int) $args['show_summary'];
1521      $show_author  = (int) $args['show_author'];
1522      $show_date    = (int) $args['show_date'];
1523  
1524      if ( ! $rss->get_item_quantity() ) {
1525          echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
1526          $rss->__destruct();
1527          unset( $rss );
1528          return;
1529      }
1530  
1531      echo '<ul>';
1532      foreach ( $rss->get_items( 0, $items ) as $item ) {
1533          $link = $item->get_link();
1534          while ( stristr( $link, 'http' ) !== $link ) {
1535              $link = substr( $link, 1 );
1536          }
1537          $link = esc_url( strip_tags( $link ) );
1538  
1539          $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
1540          if ( empty( $title ) ) {
1541              $title = __( 'Untitled' );
1542          }
1543  
1544          $desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
1545          $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
1546  
1547          $summary = '';
1548          if ( $show_summary ) {
1549              $summary = $desc;
1550  
1551              // Change existing [...] to [&hellip;].
1552              if ( '[...]' === substr( $summary, -5 ) ) {
1553                  $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
1554              }
1555  
1556              $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
1557          }
1558  
1559          $date = '';
1560          if ( $show_date ) {
1561              $date = $item->get_date( 'U' );
1562  
1563              if ( $date ) {
1564                  $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
1565              }
1566          }
1567  
1568          $author = '';
1569          if ( $show_author ) {
1570              $author = $item->get_author();
1571              if ( is_object( $author ) ) {
1572                  $author = $author->get_name();
1573                  $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
1574              }
1575          }
1576  
1577          if ( '' === $link ) {
1578              echo "<li>$title{$date}{$summary}{$author}</li>";
1579          } elseif ( $show_summary ) {
1580              echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
1581          } else {
1582              echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
1583          }
1584      }
1585      echo '</ul>';
1586      $rss->__destruct();
1587      unset( $rss );
1588  }
1589  
1590  /**
1591   * Display RSS widget options form.
1592   *
1593   * The options for what fields are displayed for the RSS form are all booleans
1594   * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
1595   * 'show_date'.
1596   *
1597   * @since 2.5.0
1598   *
1599   * @param array|string $args   Values for input fields.
1600   * @param array        $inputs Override default display options.
1601   */
1602  function wp_widget_rss_form( $args, $inputs = null ) {
1603      $default_inputs = array(
1604          'url'          => true,
1605          'title'        => true,
1606          'items'        => true,
1607          'show_summary' => true,
1608          'show_author'  => true,
1609          'show_date'    => true,
1610      );
1611      $inputs         = wp_parse_args( $inputs, $default_inputs );
1612  
1613      $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
1614      $args['url']   = isset( $args['url'] ) ? $args['url'] : '';
1615      $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
1616  
1617      if ( $args['items'] < 1 || 20 < $args['items'] ) {
1618          $args['items'] = 10;
1619      }
1620  
1621      $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
1622      $args['show_author']  = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
1623      $args['show_date']    = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
1624  
1625      if ( ! empty( $args['error'] ) ) {
1626          echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>';
1627      }
1628  
1629      $esc_number = esc_attr( $args['number'] );
1630      if ( $inputs['url'] ) :
1631          ?>
1632      <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
1633      <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>
1634  <?php endif; if ( $inputs['title'] ) : ?>
1635      <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
1636      <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>
1637  <?php endif; if ( $inputs['items'] ) : ?>
1638      <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
1639      <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
1640      <?php
1641      for ( $i = 1; $i <= 20; ++$i ) {
1642          echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
1643      }
1644      ?>
1645      </select></p>
1646  <?php endif; if ( $inputs['show_summary'] || $inputs['show_author'] || $inputs['show_date'] ) : ?>
1647      <p>
1648      <?php if ( $inputs['show_summary'] ) : ?>
1649          <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'] ); ?> />
1650          <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
1651      <?php endif; if ( $inputs['show_author'] ) : ?>
1652          <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'] ); ?> />
1653          <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
1654      <?php endif; if ( $inputs['show_date'] ) : ?>
1655          <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'] ); ?>/>
1656          <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
1657      <?php endif; ?>
1658      </p>
1659      <?php
1660      endif; // End of display options.
1661  foreach ( array_keys( $default_inputs ) as $input ) :
1662      if ( 'hidden' === $inputs[ $input ] ) :
1663          $id = str_replace( '_', '-', $input );
1664          ?>
1665  <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 ] ); ?>" />
1666          <?php
1667      endif;
1668      endforeach;
1669  }
1670  
1671  /**
1672   * Process RSS feed widget data and optionally retrieve feed items.
1673   *
1674   * The feed widget can not have more than 20 items or it will reset back to the
1675   * default, which is 10.
1676   *
1677   * The resulting array has the feed title, feed url, feed link (from channel),
1678   * feed items, error (if any), and whether to show summary, author, and date.
1679   * All respectively in the order of the array elements.
1680   *
1681   * @since 2.5.0
1682   *
1683   * @param array $widget_rss RSS widget feed data. Expects unescaped data.
1684   * @param bool  $check_feed Optional. Whether to check feed for errors. Default true.
1685   * @return array
1686   */
1687  function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1688      $items = (int) $widget_rss['items'];
1689      if ( $items < 1 || 20 < $items ) {
1690          $items = 10;
1691      }
1692      $url          = esc_url_raw( strip_tags( $widget_rss['url'] ) );
1693      $title        = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
1694      $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
1695      $show_author  = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
1696      $show_date    = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
1697  
1698      if ( $check_feed ) {
1699          $rss   = fetch_feed( $url );
1700          $error = false;
1701          $link  = '';
1702          if ( is_wp_error( $rss ) ) {
1703              $error = $rss->get_error_message();
1704          } else {
1705              $link = esc_url( strip_tags( $rss->get_permalink() ) );
1706              while ( stristr( $link, 'http' ) !== $link ) {
1707                  $link = substr( $link, 1 );
1708              }
1709  
1710              $rss->__destruct();
1711              unset( $rss );
1712          }
1713      }
1714  
1715      return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1716  }
1717  
1718  /**
1719   * Registers all of the default WordPress widgets on startup.
1720   *
1721   * Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered.
1722   *
1723   * @since 2.2.0
1724   */
1725  function wp_widgets_init() {
1726      if ( ! is_blog_installed() ) {
1727          return;
1728      }
1729  
1730      register_widget( 'WP_Widget_Pages' );
1731  
1732      register_widget( 'WP_Widget_Calendar' );
1733  
1734      register_widget( 'WP_Widget_Archives' );
1735  
1736      if ( get_option( 'link_manager_enabled' ) ) {
1737          register_widget( 'WP_Widget_Links' );
1738      }
1739  
1740      register_widget( 'WP_Widget_Media_Audio' );
1741  
1742      register_widget( 'WP_Widget_Media_Image' );
1743  
1744      register_widget( 'WP_Widget_Media_Gallery' );
1745  
1746      register_widget( 'WP_Widget_Media_Video' );
1747  
1748      register_widget( 'WP_Widget_Meta' );
1749  
1750      register_widget( 'WP_Widget_Search' );
1751  
1752      register_widget( 'WP_Widget_Text' );
1753  
1754      register_widget( 'WP_Widget_Categories' );
1755  
1756      register_widget( 'WP_Widget_Recent_Posts' );
1757  
1758      register_widget( 'WP_Widget_Recent_Comments' );
1759  
1760      register_widget( 'WP_Widget_RSS' );
1761  
1762      register_widget( 'WP_Widget_Tag_Cloud' );
1763  
1764      register_widget( 'WP_Nav_Menu_Widget' );
1765  
1766      register_widget( 'WP_Widget_Custom_HTML' );
1767  
1768      /**
1769       * Fires after all default WordPress widgets have been registered.
1770       *
1771       * @since 2.2.0
1772       */
1773      do_action( 'widgets_init' );
1774  }


Generated: Mon Sep 28 01:00:02 2020 Cross-referenced by PHPXref 0.7.1