[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0

Provided by Yoast

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Widgets administration panel.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once ( './admin.php' );
  11  
  12  /** WordPress Administration Widgets API */
  13  require_once (ABSPATH . 'wp-admin/includes/widgets.php');
  14  
  15  if ( ! current_user_can('edit_theme_options') )
  16      wp_die( __( 'Cheatin&#8217; uh?' ));
  17  
  18  wp_admin_css( 'widgets' );
  19  
  20  $widgets_access = get_user_setting( 'widgets_access' );
  21  if ( isset($_GET['widgets-access']) ) {
  22      $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
  23      set_user_setting( 'widgets_access', $widgets_access );
  24  }
  25  
  26  if ( 'on' == $widgets_access )
  27      add_filter( 'admin_body_class', create_function('', '{return " widgets_access ";}') );
  28  else
  29      wp_enqueue_script('admin-widgets');
  30  
  31  do_action( 'sidebar_admin_setup' );
  32  
  33  $title = __( 'Widgets' );
  34  $parent_file = 'themes.php';
  35  
  36  $help = '
  37      <p>' . __('Widgets are independent sections of content that can be placed into any widgetized area provided by your theme (commonly called sidebars). To populate your sidebars/widget areas with individual widgets, drag and drop the title bars into the desired area. By default, only the first widget area is expanded. To populate additional widget areas, click on their title bars to expand them.') . '</p>
  38      <p>' . __('Available Widgets section contains all the widgets you can choose from. Once you drag a widget into a sidebar, it will open to allow you to configure its settings. When you are happy with the widget settings, click the Save button and the widget will go live on your site. If you click Delete, it will remove the widget.') . '</p>
  39      <p>' . __('If you want to remove the widget but save its setting for possible future use, just drag it into the Inactive Widgets area. You can add them back anytime from there. This is especially helpful when you switch to a theme with fewer or different widget areas.') . '</p>
  40      <p>' . __('Widgets may be used multiple times. You can give each widget a title, to display on your site, but it&#8217;s not required.') . '</p>
  41      <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>
  42      <p>' . __('Many themes show some sidebar widgets by default until you edit your sidebars, but they are not automatically displayed in your sidebar management tool. After you make your first widget change, you can re-add the default widgets by adding them from the Available Widgets area.') . '</p>
  43  ';
  44  $help .= '<p><strong>' . __('For more information:') . '</strong></p>';
  45  $help .= '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Widgets_SubPanel" target="_blank">Widgets Documentation</a>') . '</p>';
  46  $help .= '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>';
  47  add_contextual_help($current_screen, $help);
  48  
  49  // register the inactive_widgets area as sidebar
  50  register_sidebar(array(
  51      'name' => __('Inactive Widgets'),
  52      'id' => 'wp_inactive_widgets',
  53      'description' => '',
  54      'before_widget' => '',
  55      'after_widget' => '',
  56      'before_title' => '',
  57      'after_title' => '',
  58  ));
  59  
  60  // These are the widgets grouped by sidebar
  61  $sidebars_widgets = wp_get_sidebars_widgets();
  62  if ( empty( $sidebars_widgets ) )
  63      $sidebars_widgets = wp_get_widget_defaults();
  64  
  65  // look for "lost" widgets, this has to run at least on each theme change
  66  function retrieve_widgets() {
  67      global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
  68  
  69      $_sidebars_widgets = array();
  70      $sidebars = array_keys($wp_registered_sidebars);
  71  
  72      unset( $sidebars_widgets['array_version'] );
  73  
  74      $old = array_keys($sidebars_widgets);
  75      sort($old);
  76      sort($sidebars);
  77  
  78      if ( $old == $sidebars )
  79          return;
  80  
  81      // Move the known-good ones first
  82      foreach ( $sidebars as $id ) {
  83          if ( array_key_exists( $id, $sidebars_widgets ) ) {
  84              $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  85              unset($sidebars_widgets[$id], $sidebars[$id]);
  86          }
  87      }
  88  
  89      // if new theme has less sidebars than the old theme
  90      if ( !empty($sidebars_widgets) ) {
  91          foreach ( $sidebars_widgets as $lost => $val ) {
  92              if ( is_array($val) )
  93                  $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
  94          }
  95      }
  96  
  97      // discard invalid, theme-specific widgets from sidebars
  98      $shown_widgets = array();
  99      foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
 100          if ( !is_array($widgets) )
 101              continue;
 102  
 103          $_widgets = array();
 104          foreach ( $widgets as $widget ) {
 105              if ( isset($wp_registered_widgets[$widget]) )
 106                  $_widgets[] = $widget;
 107          }
 108          $_sidebars_widgets[$sidebar] = $_widgets;
 109          $shown_widgets = array_merge($shown_widgets, $_widgets);
 110      }
 111  
 112      $sidebars_widgets = $_sidebars_widgets;
 113      unset($_sidebars_widgets, $_widgets);
 114  
 115      // find hidden/lost multi-widget instances
 116      $lost_widgets = array();
 117      foreach ( $wp_registered_widgets as $key => $val ) {
 118          if ( in_array($key, $shown_widgets, true) )
 119              continue;
 120  
 121          $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
 122  
 123          if ( 2 > (int) $number )
 124              continue;
 125  
 126          $lost_widgets[] = $key;
 127      }
 128  
 129      $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
 130      wp_set_sidebars_widgets($sidebars_widgets);
 131  }
 132  retrieve_widgets();
 133  
 134  if ( count($wp_registered_sidebars) == 1 ) {
 135      // If only "wp_inactive_widgets" is defined the theme has no sidebars, die.
 136      require_once ( './admin-header.php' );
 137  ?>
 138  
 139      <div class="wrap">
 140      <?php screen_icon(); ?>
 141      <h2><?php echo esc_html( $title ); ?></h2>
 142          <div class="error">
 143              <p><?php _e( 'No Sidebars Defined' ); ?></p>
 144          </div>
 145          <p><?php _e( 'The theme you are currently using isn&#8217;t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="http://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ); ?></p>
 146      </div>
 147  
 148  <?php
 149      require_once ( './admin-footer.php' );
 150      exit;
 151  }
 152  
 153  // We're saving a widget without js
 154  if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
 155      $widget_id = $_POST['widget-id'];
 156      check_admin_referer("save-delete-widget-$widget_id");
 157  
 158      $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
 159      if ( $number ) {
 160          foreach ( $_POST as $key => $val ) {
 161              if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
 162                  $_POST[$key] = array( $number => array_shift($val) );
 163                  break;
 164              }
 165          }
 166      }
 167  
 168      $sidebar_id = $_POST['sidebar'];
 169      $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
 170  
 171      $id_base = $_POST['id_base'];
 172      $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
 173  
 174      // delete
 175      if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
 176  
 177          if ( !in_array($widget_id, $sidebar, true) ) {
 178              wp_redirect('widgets.php?error=0');
 179              exit;
 180          }
 181  
 182          $sidebar = array_diff( $sidebar, array($widget_id) );
 183          $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
 184      }
 185  
 186      $_POST['widget-id'] = $sidebar;
 187  
 188      foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
 189          if ( $name != $id_base || !is_callable($control['callback']) )
 190              continue;
 191  
 192          ob_start();
 193              call_user_func_array( $control['callback'], $control['params'] );
 194          ob_end_clean();
 195  
 196          break;
 197      }
 198  
 199      $sidebars_widgets[$sidebar_id] = $sidebar;
 200  
 201      // remove old position
 202      if ( !isset($_POST['delete_widget']) ) {
 203          foreach ( $sidebars_widgets as $key => $sb ) {
 204              if ( is_array($sb) )
 205                  $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
 206          }
 207          array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
 208      }
 209  
 210      wp_set_sidebars_widgets($sidebars_widgets);
 211      wp_redirect('widgets.php?message=0');
 212      exit;
 213  }
 214  
 215  // Output the widget form without js
 216  if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
 217      $widget_id = $_GET['editwidget'];
 218  
 219      if ( isset($_GET['addnew']) ) {
 220          // Default to the first sidebar
 221          $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
 222  
 223          if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
 224              // Copy minimal info from an existing instance of this widget to a new instance
 225              foreach ( $wp_registered_widget_controls as $control ) {
 226                  if ( $_GET['base'] === $control['id_base'] ) {
 227                      $control_callback = $control['callback'];
 228                      $multi_number = (int) $_GET['num'];
 229                      $control['params'][0]['number'] = -1;
 230                      $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
 231                      $wp_registered_widget_controls[$control['id']] = $control;
 232                      break;
 233                  }
 234              }
 235          }
 236      }
 237  
 238      if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
 239          $control = $wp_registered_widget_controls[$widget_id];
 240          $control_callback = $control['callback'];
 241      } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
 242          $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
 243      }
 244  
 245      if ( !isset($name) )
 246          $name = esc_html( strip_tags($control['name']) );
 247  
 248      if ( !isset($sidebar) )
 249          $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
 250  
 251      if ( !isset($multi_number) )
 252          $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
 253  
 254      $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
 255  
 256      // show the widget form
 257      $width = ' style="width:' . max($control['width'], 350) . 'px"';
 258      $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
 259  
 260      require_once ( './admin-header.php' ); ?>
 261      <div class="wrap">
 262      <?php screen_icon(); ?>
 263      <h2><?php echo esc_html( $title ); ?></h2>
 264      <div class="editwidget"<?php echo $width; ?>>
 265      <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
 266  
 267      <form action="widgets.php" method="post">
 268      <div class="widget-inside">
 269  <?php
 270      if ( is_callable( $control_callback ) )
 271          call_user_func_array( $control_callback, $control['params'] );
 272      else
 273          echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
 274      </div>
 275  
 276      <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
 277      <div class="widget-position">
 278      <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
 279  <?php
 280      foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
 281          echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
 282          if ( 'wp_inactive_widgets' == $sbname ) {
 283              echo '&nbsp;';
 284          } else {
 285              if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
 286                  $j = 1;
 287                  $sidebars_widgets[$sbname] = array();
 288              } else {
 289                  $j = count($sidebars_widgets[$sbname]);
 290                  if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
 291                      $j++;
 292              }
 293              $selected = '';
 294              echo "\t\t<select name='{$sbname}_position'>\n";
 295              echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
 296              for ( $i = 1; $i <= $j; $i++ ) {
 297                  if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
 298                      $selected = selected( $i, $key + 1, false );
 299                  echo "\t\t<option value='$i'$selected> $i </option>\n";
 300              }
 301              echo "\t\t</select>\n";
 302          }
 303          echo "</td></tr>\n";
 304      } ?>
 305      </tbody></table>
 306      </div>
 307  
 308      <div class="widget-control-actions">
 309  <?php    if ( isset($_GET['addnew']) ) { ?>
 310      <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
 311  <?php    } else { ?>
 312      <input type="submit" name="removewidget" class="button alignleft" value="<?php esc_attr_e('Delete'); ?>" />
 313  <?php    } ?>
 314      <input type="submit" name="savewidget" class="button-primary alignright" value="<?php esc_attr_e('Save Widget'); ?>" />
 315      <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
 316      <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
 317      <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
 318  <?php    wp_nonce_field("save-delete-widget-$widget_id"); ?>
 319      <br class="clear" />
 320      </div>
 321      </form>
 322      </div>
 323      </div>
 324  <?php
 325      require_once ( './admin-footer.php' );
 326      exit;
 327  }
 328  
 329  $messages = array(
 330      __('Changes saved.')
 331  );
 332  
 333  $errors = array(
 334      __('Error while saving.'),
 335      __('Error in displaying the widget settings form.')
 336  );
 337  
 338  require_once ( './admin-header.php' ); ?>
 339  
 340  <div class="wrap">
 341  <?php screen_icon(); ?>
 342  <h2><?php echo esc_html( $title ); ?></h2>
 343  
 344  <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
 345  <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
 346  <?php } ?>
 347  <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
 348  <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
 349  <?php } ?>
 350  
 351  <?php do_action( 'widgets_admin_page' ); ?>
 352  
 353  <div class="widget-liquid-left">
 354  <div id="widgets-left">
 355      <div id="available-widgets" class="widgets-holder-wrap">
 356          <div class="sidebar-name">
 357          <div class="sidebar-name-arrow"><br /></div>
 358          <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _e('Deactivate'); ?> <span></span></span></h3></div>
 359          <div class="widget-holder">
 360          <p class="description"><?php _e('Drag widgets from here to a sidebar on the right to activate them. Drag widgets back here to deactivate them and delete their settings.'); ?></p>
 361          <div id="widget-list">
 362          <?php wp_list_widgets(); ?>
 363          </div>
 364          <br class='clear' />
 365          </div>
 366          <br class="clear" />
 367      </div>
 368  
 369      <div class="widgets-holder-wrap">
 370          <div class="sidebar-name">
 371          <div class="sidebar-name-arrow"><br /></div>
 372          <h3><?php _e('Inactive Widgets'); ?>
 373          <span><img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-feedback" title="" alt="" /></span></h3></div>
 374          <div class="widget-holder inactive">
 375          <p class="description"><?php _e('Drag widgets here to remove them from the sidebar but keep their settings.'); ?></p>
 376          <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
 377          <br class="clear" />
 378          </div>
 379      </div>
 380  </div>
 381  </div>
 382  
 383  <div class="widget-liquid-right">
 384  <div id="widgets-right">
 385  <?php
 386  $i = 0;
 387  foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
 388      if ( 'wp_inactive_widgets' == $sidebar )
 389          continue;
 390      $closed = $i ? ' closed' : ''; ?>
 391      <div class="widgets-holder-wrap<?php echo $closed; ?>">
 392      <div class="sidebar-name">
 393      <div class="sidebar-name-arrow"><br /></div>
 394      <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
 395      <span><img src="<?php echo esc_url( admin_url( 'images/wpspin_dark.gif' ) ); ?>" class="ajax-feedback" title="" alt="" /></span></h3></div>
 396      <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
 397      </div>
 398  <?php
 399      $i++;
 400  } ?>
 401  </div>
 402  </div>
 403  <form action="" method="post">
 404  <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
 405  </form>
 406  <br class="clear" />
 407  </div>
 408  
 409  <?php
 410  do_action( 'sidebar_admin_page' );
 411  require_once ( './admin-footer.php' );


Generated: Thu Oct 14 05:11:12 2010 Cross-referenced by PHPXref 0.7