[ Index ]

PHP Cross Reference of WordPress

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   * @param array $params
 130   * @return array
 131   */
 132  function wp_list_widget_controls_dynamic_sidebar( $params ) {
 133      global $wp_registered_widgets;
 134      static $i = 0;
 135      $i++;
 136  
 137      $widget_id = $params[0]['widget_id'];
 138      $id        = isset( $params[0]['_temp_id'] ) ? $params[0]['_temp_id'] : $widget_id;
 139      $hidden    = isset( $params[0]['_hide'] ) ? ' style="display:none;"' : '';
 140  
 141      $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>";
 142      $params[0]['after_widget']  = '</div>';
 143      $params[0]['before_title']  = '%BEG_OF_TITLE%'; // Deprecated.
 144      $params[0]['after_title']   = '%END_OF_TITLE%'; // Deprecated.
 145  
 146      if ( is_callable( $wp_registered_widgets[ $widget_id ]['callback'] ) ) {
 147          $wp_registered_widgets[ $widget_id ]['_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
 148          $wp_registered_widgets[ $widget_id ]['callback']  = 'wp_widget_control';
 149      }
 150  
 151      return $params;
 152  }
 153  
 154  /**
 155   * @global array $wp_registered_widgets
 156   *
 157   * @param string $id_base
 158   * @return int
 159   */
 160  function next_widget_id_number( $id_base ) {
 161      global $wp_registered_widgets;
 162      $number = 1;
 163  
 164      foreach ( $wp_registered_widgets as $widget_id => $widget ) {
 165          if ( preg_match( '/' . preg_quote( $id_base, '/' ) . '-([0-9]+)$/', $widget_id, $matches ) ) {
 166              $number = max( $number, $matches[1] );
 167          }
 168      }
 169      $number++;
 170  
 171      return $number;
 172  }
 173  
 174  /**
 175   * Meta widget used to display the control form for a widget.
 176   *
 177   * Called from dynamic_sidebar().
 178   *
 179   * @since 2.5.0
 180   *
 181   * @global array $wp_registered_widgets
 182   * @global array $wp_registered_widget_controls
 183   * @global array $sidebars_widgets
 184   *
 185   * @param array $sidebar_args
 186   * @return array
 187   */
 188  function wp_widget_control( $sidebar_args ) {
 189      global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
 190  
 191      $widget_id  = $sidebar_args['widget_id'];
 192      $sidebar_id = isset( $sidebar_args['id'] ) ? $sidebar_args['id'] : false;
 193      $key        = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[ $sidebar_id ], true ) : '-1'; // Position of widget in sidebar.
 194      $control    = isset( $wp_registered_widget_controls[ $widget_id ] ) ? $wp_registered_widget_controls[ $widget_id ] : array();
 195      $widget     = $wp_registered_widgets[ $widget_id ];
 196  
 197      $id_format     = $widget['id'];
 198      $widget_number = isset( $control['params'][0]['number'] ) ? $control['params'][0]['number'] : '';
 199      $id_base       = isset( $control['id_base'] ) ? $control['id_base'] : $widget_id;
 200      $width         = isset( $control['width'] ) ? $control['width'] : '';
 201      $height        = isset( $control['height'] ) ? $control['height'] : '';
 202      $multi_number  = isset( $sidebar_args['_multi_num'] ) ? $sidebar_args['_multi_num'] : '';
 203      $add_new       = isset( $sidebar_args['_add'] ) ? $sidebar_args['_add'] : '';
 204  
 205      $before_form           = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">';
 206      $after_form            = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>';
 207      $before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">';
 208      $after_widget_content  = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>';
 209  
 210      $query_arg = array( 'editwidget' => $widget['id'] );
 211      if ( $add_new ) {
 212          $query_arg['addnew'] = 1;
 213          if ( $multi_number ) {
 214              $query_arg['num']  = $multi_number;
 215              $query_arg['base'] = $id_base;
 216          }
 217      } else {
 218          $query_arg['sidebar'] = $sidebar_id;
 219          $query_arg['key']     = $key;
 220      }
 221  
 222      /*
 223       * We aren't showing a widget control, we're outputting a template
 224       * for a multi-widget control.
 225       */
 226      if ( isset( $sidebar_args['_display'] ) && 'template' === $sidebar_args['_display'] && $widget_number ) {
 227          // number == -1 implies a template where id numbers are replaced by a generic '__i__'.
 228          $control['params'][0]['number'] = -1;
 229          // With id_base widget ID's are constructed like {$id_base}-{$id_number}.
 230          if ( isset( $control['id_base'] ) ) {
 231              $id_format = $control['id_base'] . '-__i__';
 232          }
 233      }
 234  
 235      $wp_registered_widgets[ $widget_id ]['callback'] = $wp_registered_widgets[ $widget_id ]['_callback'];
 236      unset( $wp_registered_widgets[ $widget_id ]['_callback'] );
 237  
 238      $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
 239      $has_form     = 'noform';
 240  
 241      echo $sidebar_args['before_widget'];
 242      ?>
 243      <div class="widget-top">
 244      <div class="widget-title-action">
 245          <button type="button" class="widget-action hide-if-no-js" aria-expanded="false">
 246              <span class="screen-reader-text edit">
 247                  <?php
 248                  /* translators: %s: Widget title. */
 249                  printf( __( 'Edit widget: %s' ), $widget_title );
 250                  ?>
 251              </span>
 252              <span class="screen-reader-text add">
 253                  <?php
 254                  /* translators: %s: Widget title. */
 255                  printf( __( 'Add widget: %s' ), $widget_title );
 256                  ?>
 257              </span>
 258              <span class="toggle-indicator" aria-hidden="true"></span>
 259          </button>
 260          <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
 261              <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
 262              <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
 263              <span class="screen-reader-text"><?php echo $widget_title; ?></span>
 264          </a>
 265      </div>
 266      <div class="widget-title"><h3><?php echo $widget_title; ?><span class="in-widget-title"></span></h3></div>
 267      </div>
 268  
 269      <div class="widget-inside">
 270      <?php echo $before_form; ?>
 271      <?php echo $before_widget_content; ?>
 272      <?php
 273      if ( isset( $control['callback'] ) ) {
 274          $has_form = call_user_func_array( $control['callback'], $control['params'] );
 275      } else {
 276          echo "\t\t<p>" . __( 'There are no options for this widget.' ) . "</p>\n";
 277      }
 278  
 279      $noform_class = '';
 280      if ( 'noform' === $has_form ) {
 281          $noform_class = ' widget-control-noform';
 282      }
 283      ?>
 284      <?php echo $after_widget_content; ?>
 285      <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr( $id_format ); ?>" />
 286      <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" />
 287      <input type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr( $width ); ?>" />
 288      <input type="hidden" name="widget-height" class="widget-height" value="<?php echo esc_attr( $height ); ?>" />
 289      <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr( $widget_number ); ?>" />
 290      <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" />
 291      <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr( $add_new ); ?>" />
 292  
 293      <div class="widget-control-actions">
 294          <div class="alignleft">
 295              <button type="button" class="button-link button-link-delete widget-control-remove"><?php _e( 'Delete' ); ?></button>
 296              <span class="widget-control-close-wrapper">
 297                  | <button type="button" class="button-link widget-control-close"><?php _e( 'Done' ); ?></button>
 298              </span>
 299          </div>
 300          <div class="alignright<?php echo $noform_class; ?>">
 301              <?php submit_button( __( 'Save' ), 'primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?>
 302              <span class="spinner"></span>
 303          </div>
 304          <br class="clear" />
 305      </div>
 306      <?php echo $after_form; ?>
 307      </div>
 308  
 309      <div class="widget-description">
 310      <?php
 311      $widget_description = wp_widget_description( $widget_id );
 312      echo ( $widget_description ) ? "$widget_description\n" : "$widget_title\n";
 313      ?>
 314      </div>
 315      <?php
 316      echo $sidebar_args['after_widget'];
 317  
 318      return $sidebar_args;
 319  }
 320  
 321  /**
 322   * @param string $classes
 323   * @return string
 324   */
 325  function wp_widgets_access_body_class( $classes ) {
 326      return "$classes widgets_access ";
 327  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1