[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Widgets Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Display list of the available widgets.
  11   *
  12   * @since 2.5.0
  13   *
  14   * @global array $wp_registered_widgets
  15   * @global array $wp_registered_widget_controls
  16   */
  17  function wp_list_widgets() {
  18      global $wp_registered_widgets, $wp_registered_widget_controls;
  19  
  20      $sort = $wp_registered_widgets;
  21      usort( $sort, '_sort_name_callback' );
  22      $done = array();
  23  
  24      foreach ( $sort as $widget ) {
  25          if ( in_array( $widget['callback'], $done, true ) ) { // We already showed this multi-widget
  26              continue;
  27          }
  28  
  29          $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false );
  30          $done[]  = $widget['callback'];
  31  
  32          if ( ! isset( $widget['params'][0] ) ) {
  33              $widget['params'][0] = array();
  34          }
  35  
  36          $args = array(
  37              'widget_id'   => $widget['id'],
  38              'widget_name' => $widget['name'],
  39              '_display'    => 'template',
  40          );
  41  
  42          if ( isset( $wp_registered_widget_controls[ $widget['id'] ]['id_base'] ) && isset( $widget['params'][0]['number'] ) ) {
  43              $id_base            = $wp_registered_widget_controls[ $widget['id'] ]['id_base'];
  44              $args['_temp_id']   = "$id_base-__i__";
  45              $args['_multi_num'] = next_widget_id_number( $id_base );
  46              $args['_add']       = 'multi';
  47          } else {
  48              $args['_add'] = 'single';
  49              if ( $sidebar ) {
  50                  $args['_hide'] = '1';
  51              }
  52          }
  53  
  54          $control_args = array(
  55              0 => $args,
  56              1 => $widget['params'][0],
  57          );
  58          $sidebar_args = wp_list_widget_controls_dynamic_sidebar( $control_args );
  59  
  60          wp_widget_control( ...$sidebar_args );
  61      }
  62  }
  63  
  64  /**
  65   * Callback to sort array by a 'name' key.
  66   *
  67   * @since 3.1.0
  68   * @access private
  69   *
  70   * @return int
  71   */
  72  function _sort_name_callback( $a, $b ) {
  73      return strnatcasecmp( $a['name'], $b['name'] );
  74  }
  75  
  76  /**
  77   * Show the widgets and their settings for a sidebar.
  78   * Used in the admin widget config screen.
  79   *
  80   * @since 2.5.0
  81   *
  82   * @param string $sidebar      Sidebar ID.
  83   * @param string $sidebar_name Optional. Sidebar name. Default empty.
  84   */
  85  function wp_list_widget_controls( $sidebar, $sidebar_name = '' ) {
  86      add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
  87  
  88      $description = wp_sidebar_description( $sidebar );
  89  
  90      echo '<div id="' . esc_attr( $sidebar ) . '" class="widgets-sortables">';
  91  
  92      if ( $sidebar_name ) {
  93          $add_to = sprintf(
  94              /* translators: %s: Widgets sidebar name. */
  95              __( 'Add to: %s' ),
  96              $sidebar_name
  97          );
  98          ?>
  99          <div class="sidebar-name" data-add-to="<?php echo esc_attr( $add_to ); ?>">
 100              <button type="button" class="handlediv hide-if-no-js" aria-expanded="true">
 101                  <span class="screen-reader-text"><?php echo esc_html( $sidebar_name ); ?></span>
 102                  <span class="toggle-indicator" aria-hidden="true"></span>
 103              </button>
 104              <h2><?php echo esc_html( $sidebar_name ); ?> <span class="spinner"></span></h2>
 105          </div>
 106          <?php
 107      }
 108  
 109      if ( ! empty( $description ) ) {
 110          ?>
 111          <div class="sidebar-description">
 112              <p class="description"><?php echo $description; ?></p>
 113          </div>
 114          <?php
 115      }
 116  
 117      dynamic_sidebar( $sidebar );
 118  
 119      echo '</div>';
 120  }
 121  
 122  /**
 123   * Retrieves the widget control arguments.
 124   *
 125   * @since 2.5.0
 126   *
 127   * @global array $wp_registered_widgets
 128   *
 129   * @staticvar int $i
 130   *
 131   * @param array $params
 132   * @return array
 133   */
 134  function wp_list_widget_controls_dynamic_sidebar( $params ) {
 135      global $wp_registered_widgets;
 136      static $i = 0;
 137      $i++;
 138  
 139      $widget_id = $params[0]['widget_id'];
 140      $id        = isset( $params[0]['_temp_id'] ) ? $params[0]['_temp_id'] : $widget_id;
 141      $hidden    = isset( $params[0]['_hide'] ) ? ' style="display:none;"' : '';
 142  
 143      $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>";
 144      $params[0]['after_widget']  = '</div>';
 145      $params[0]['before_title']  = '%BEG_OF_TITLE%'; // deprecated
 146      $params[0]['after_title']   = '%END_OF_TITLE%'; // deprecated
 147  
 148      if ( is_callable( $wp_registered_widgets[ $widget_id ]['callback'] ) ) {
 149          $wp_registered_widgets[ $widget_id ]['_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
 150          $wp_registered_widgets[ $widget_id ]['callback']  = 'wp_widget_control';
 151      }
 152  
 153      return $params;
 154  }
 155  
 156  /**
 157   * @global array $wp_registered_widgets
 158   *
 159   * @param string $id_base
 160   * @return int
 161   */
 162  function next_widget_id_number( $id_base ) {
 163      global $wp_registered_widgets;
 164      $number = 1;
 165  
 166      foreach ( $wp_registered_widgets as $widget_id => $widget ) {
 167          if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) ) {
 168              $number = max( $number, $matches[1] );
 169          }
 170      }
 171      $number++;
 172  
 173      return $number;
 174  }
 175  
 176  /**
 177   * Meta widget used to display the control form for a widget.
 178   *
 179   * Called from dynamic_sidebar().
 180   *
 181   * @since 2.5.0
 182   *
 183   * @global array $wp_registered_widgets
 184   * @global array $wp_registered_widget_controls
 185   * @global array $sidebars_widgets
 186   *
 187   * @param array $sidebar_args
 188   * @return array
 189   */
 190  function wp_widget_control( $sidebar_args ) {
 191      global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
 192  
 193      $widget_id  = $sidebar_args['widget_id'];
 194      $sidebar_id = isset( $sidebar_args['id'] ) ? $sidebar_args['id'] : false;
 195      $key        = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[ $sidebar_id ] ) : '-1'; // position of widget in sidebar
 196      $control    = isset( $wp_registered_widget_controls[ $widget_id ] ) ? $wp_registered_widget_controls[ $widget_id ] : array();
 197      $widget     = $wp_registered_widgets[ $widget_id ];
 198  
 199      $id_format     = $widget['id'];
 200      $widget_number = isset( $control['params'][0]['number'] ) ? $control['params'][0]['number'] : '';
 201      $id_base       = isset( $control['id_base'] ) ? $control['id_base'] : $widget_id;
 202      $width         = isset( $control['width'] ) ? $control['width'] : '';
 203      $height        = isset( $control['height'] ) ? $control['height'] : '';
 204      $multi_number  = isset( $sidebar_args['_multi_num'] ) ? $sidebar_args['_multi_num'] : '';
 205      $add_new       = isset( $sidebar_args['_add'] ) ? $sidebar_args['_add'] : '';
 206  
 207      $before_form           = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">';
 208      $after_form            = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>';
 209      $before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">';
 210      $after_widget_content  = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>';
 211  
 212      $query_arg = array( 'editwidget' => $widget['id'] );
 213      if ( $add_new ) {
 214          $query_arg['addnew'] = 1;
 215          if ( $multi_number ) {
 216              $query_arg['num']  = $multi_number;
 217              $query_arg['base'] = $id_base;
 218          }
 219      } else {
 220          $query_arg['sidebar'] = $sidebar_id;
 221          $query_arg['key']     = $key;
 222      }
 223  
 224      /*
 225       * We aren't showing a widget control, we're outputting a template
 226       * for a multi-widget control.
 227       */
 228      if ( isset( $sidebar_args['_display'] ) && 'template' == $sidebar_args['_display'] && $widget_number ) {
 229          // number == -1 implies a template where id numbers are replaced by a generic '__i__'
 230          $control['params'][0]['number'] = -1;
 231          // With id_base widget id's are constructed like {$id_base}-{$id_number}.
 232          if ( isset( $control['id_base'] ) ) {
 233              $id_format = $control['id_base'] . '-__i__';
 234          }
 235      }
 236  
 237      $wp_registered_widgets[ $widget_id ]['callback'] = $wp_registered_widgets[ $widget_id ]['_callback'];
 238      unset( $wp_registered_widgets[ $widget_id ]['_callback'] );
 239  
 240      $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
 241      $has_form     = 'noform';
 242  
 243      echo $sidebar_args['before_widget'];
 244      ?>
 245      <div class="widget-top">
 246      <div class="widget-title-action">
 247          <button type="button" class="widget-action hide-if-no-js" aria-expanded="false">
 248              <span class="screen-reader-text edit">
 249                  <?php
 250                  /* translators: %s: Widget title. */
 251                  printf( __( 'Edit widget: %s' ), $widget_title );
 252                  ?>
 253              </span>
 254              <span class="screen-reader-text add">
 255                  <?php
 256                  /* translators: %s: Widget title. */
 257                  printf( __( 'Add widget: %s' ), $widget_title );
 258                  ?>
 259              </span>
 260              <span class="toggle-indicator" aria-hidden="true"></span>
 261          </button>
 262          <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
 263              <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
 264              <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
 265              <span class="screen-reader-text"><?php echo $widget_title; ?></span>
 266          </a>
 267      </div>
 268      <div class="widget-title"><h3><?php echo $widget_title; ?><span class="in-widget-title"></span></h3></div>
 269      </div>
 270  
 271      <div class="widget-inside">
 272      <?php echo $before_form; ?>
 273      <?php echo $before_widget_content; ?>
 274      <?php
 275      if ( isset( $control['callback'] ) ) {
 276          $has_form = call_user_func_array( $control['callback'], $control['params'] );
 277      } else {
 278          echo "\t\t<p>" . __( 'There are no options for this widget.' ) . "</p>\n";
 279      }
 280  
 281      $noform_class = '';
 282      if ( 'noform' === $has_form ) {
 283          $noform_class = ' widget-control-noform';
 284      }
 285      ?>
 286      <?php echo $after_widget_content; ?>
 287      <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr( $id_format ); ?>" />
 288      <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" />
 289      <input type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr( $width ); ?>" />
 290      <input type="hidden" name="widget-height" class="widget-height" value="<?php echo esc_attr( $height ); ?>" />
 291      <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr( $widget_number ); ?>" />
 292      <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" />
 293      <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr( $add_new ); ?>" />
 294  
 295      <div class="widget-control-actions">
 296          <div class="alignleft">
 297              <button type="button" class="button-link button-link-delete widget-control-remove"><?php _e( 'Delete' ); ?></button>
 298              <span class="widget-control-close-wrapper">
 299                  |
 300                  <button type="button" class="button-link widget-control-close"><?php _e( 'Done' ); ?></button>
 301              </span>
 302          </div>
 303          <div class="alignright<?php echo $noform_class; ?>">
 304              <?php submit_button( __( 'Save' ), 'primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?>
 305              <span class="spinner"></span>
 306          </div>
 307          <br class="clear" />
 308      </div>
 309      <?php echo $after_form; ?>
 310      </div>
 311  
 312      <div class="widget-description">
 313      <?php
 314      $widget_description = wp_widget_description( $widget_id );
 315      echo ( $widget_description ) ? "$widget_description\n" : "$widget_title\n";
 316      ?>
 317      </div>
 318      <?php
 319      echo $sidebar_args['after_widget'];
 320  
 321      return $sidebar_args;
 322  }
 323  
 324  /**
 325   * @param string $classes
 326   * @return string
 327   */
 328  function wp_widgets_access_body_class( $classes ) {
 329      return "$classes widgets_access ";
 330  }


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7