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


Generated: Fri Jul 19 01:00:04 2019 Cross-referenced by PHPXref 0.7.1