[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

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('switch_themes') )
  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 added and arranged by simple drag &#8217;n&#8217; drop. If you hover your mouse over the titlebar of a widget, you&#8217;ll see a 4-arrow cursor which indicates that the widget is movable.  Click on the titlebar, hold down the mouse button and drag the widget to a sidebar. As you drag, you&#8217;ll see a dotted box that also moves. This box shows where the widget will go once you drop it.') . '</p>
  38      <p>' . __('To remove a widget from a sidebar, drag it back to Available Widgets or click on the arrow on its titlebar to reveal its settings, and then click Remove.') . '</p>
  39      <p>' . __('To remove a widget from a sidebar <em>and keep its configuration</em>, drag it to Inactive Widgets.') . '</p>
  40      <p>' . __('The Inactive Widgets area stores widgets that are configured but not curently used. If you change themes and the new theme has fewer sidebars than the old, all extra widgets will be stored to Inactive Widgets automatically.') . '</p>
  41  ';
  42  add_contextual_help($current_screen, $help);
  43  
  44  // register the inactive_widgets area as sidebar
  45  register_sidebar(array(
  46      'name' => __('Inactive Widgets'),
  47      'id' => 'wp_inactive_widgets',
  48      'description' => '',
  49      'before_widget' => '',
  50      'after_widget' => '',
  51      'before_title' => '',
  52      'after_title' => '',
  53  ));
  54  
  55  // These are the widgets grouped by sidebar
  56  $sidebars_widgets = wp_get_sidebars_widgets();
  57  if ( empty( $sidebars_widgets ) )
  58      $sidebars_widgets = wp_get_widget_defaults();
  59  
  60  // look for "lost" widgets, this has to run at least on each theme change
  61  function retrieve_widgets() {
  62      global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
  63  
  64      $_sidebars_widgets = array();
  65      $sidebars = array_keys($wp_registered_sidebars);
  66  
  67      unset( $sidebars_widgets['array_version'] );
  68  
  69      $old = array_keys($sidebars_widgets);
  70      sort($old);
  71      sort($sidebars);
  72  
  73      if ( $old == $sidebars )
  74          return;
  75  
  76      // Move the known-good ones first
  77      foreach ( $sidebars as $id ) {
  78          if ( array_key_exists( $id, $sidebars_widgets ) ) {
  79              $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  80              unset($sidebars_widgets[$id], $sidebars[$id]);
  81          }
  82      }
  83  
  84      // if new theme has less sidebars than the old theme
  85      if ( !empty($sidebars_widgets) ) {
  86          foreach ( $sidebars_widgets as $lost => $val ) {
  87              if ( is_array($val) )
  88                  $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
  89          }
  90      }
  91  
  92      // discard invalid, theme-specific widgets from sidebars
  93      $shown_widgets = array();
  94      foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
  95          if ( !is_array($widgets) )
  96              continue;
  97  
  98          $_widgets = array();
  99          foreach ( $widgets as $widget ) {
 100              if ( isset($wp_registered_widgets[$widget]) )
 101                  $_widgets[] = $widget;
 102          }
 103          $_sidebars_widgets[$sidebar] = $_widgets;
 104          $shown_widgets = array_merge($shown_widgets, $_widgets);
 105      }
 106  
 107      $sidebars_widgets = $_sidebars_widgets;
 108      unset($_sidebars_widgets, $_widgets);
 109  
 110      // find hidden/lost multi-widget instances
 111      $lost_widgets = array();
 112      foreach ( $wp_registered_widgets as $key => $val ) {
 113          if ( in_array($key, $shown_widgets, true) )
 114              continue;
 115  
 116          $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
 117  
 118          if ( 2 > (int) $number )
 119              continue;
 120  
 121          $lost_widgets[] = $key;
 122      }
 123  
 124      $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
 125      wp_set_sidebars_widgets($sidebars_widgets);
 126  }
 127  retrieve_widgets();
 128  
 129  if ( count($wp_registered_sidebars) == 1 ) {
 130      // If only "wp_inactive_widgets" is defined the theme has no sidebars, die.
 131      require_once ( 'admin-header.php' );
 132  ?>
 133  
 134      <div class="wrap">
 135      <?php screen_icon(); ?>
 136      <h2><?php echo esc_html( $title ); ?></h2>
 137          <div class="error">
 138              <p><?php _e( 'No Sidebars Defined' ); ?></p>
 139          </div>
 140          <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>
 141      </div>
 142  
 143  <?php
 144      require_once ( 'admin-footer.php' );
 145      exit;
 146  }
 147  
 148  // We're saving a widget without js
 149  if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
 150      $widget_id = $_POST['widget-id'];
 151      check_admin_referer("save-delete-widget-$widget_id");
 152  
 153      $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
 154      if ( $number ) {
 155          foreach ( $_POST as $key => $val ) {
 156              if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
 157                  $_POST[$key] = array( $number => array_shift($val) );
 158                  break;
 159              }
 160          }
 161      }
 162  
 163      $sidebar_id = $_POST['sidebar'];
 164      $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
 165  
 166      $id_base = $_POST['id_base'];
 167      $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
 168  
 169      // delete
 170      if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
 171  
 172          if ( !in_array($widget_id, $sidebar, true) ) {
 173              wp_redirect('widgets.php?error=0');
 174              exit;
 175          }
 176  
 177          $sidebar = array_diff( $sidebar, array($widget_id) );
 178          $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
 179      }
 180  
 181      $_POST['widget-id'] = $sidebar;
 182  
 183      foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
 184          if ( $name != $id_base || !is_callable($control['callback']) )
 185              continue;
 186  
 187          ob_start();
 188              call_user_func_array( $control['callback'], $control['params'] );
 189          ob_end_clean();
 190  
 191          break;
 192      }
 193  
 194      $sidebars_widgets[$sidebar_id] = $sidebar;
 195  
 196      // remove old position
 197      if ( !isset($_POST['delete_widget']) ) {
 198          foreach ( $sidebars_widgets as $key => $sb ) {
 199              if ( is_array($sb) )
 200                  $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
 201          }
 202          array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
 203      }
 204  
 205      wp_set_sidebars_widgets($sidebars_widgets);
 206      wp_redirect('widgets.php?message=0');
 207      exit;
 208  }
 209  
 210  // Output the widget form without js
 211  if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
 212      $widget_id = $_GET['editwidget'];
 213  
 214      if ( isset($_GET['addnew']) ) {
 215          // Default to the first sidebar
 216          $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
 217  
 218          if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
 219              // Copy minimal info from an existing instance of this widget to a new instance
 220              foreach ( $wp_registered_widget_controls as $control ) {
 221                  if ( $_GET['base'] === $control['id_base'] ) {
 222                      $control_callback = $control['callback'];
 223                      $multi_number = (int) $_GET['num'];
 224                      $control['params'][0]['number'] = -1;
 225                      $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
 226                      $wp_registered_widget_controls[$control['id']] = $control;
 227                      break;
 228                  }
 229              }
 230          }
 231      }
 232  
 233      if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
 234          $control = $wp_registered_widget_controls[$widget_id];
 235          $control_callback = $control['callback'];
 236      } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
 237          $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
 238      }
 239  
 240      if ( !isset($name) )
 241          $name = esc_html( strip_tags($control['name']) );
 242  
 243      if ( !isset($sidebar) )
 244          $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
 245  
 246      if ( !isset($multi_number) )
 247          $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
 248  
 249      $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
 250  
 251      // show the widget form
 252      $width = ' style="width:' . max($control['width'], 350) . 'px"';
 253      $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
 254  
 255      require_once ( 'admin-header.php' ); ?>
 256      <div class="wrap">
 257      <?php screen_icon(); ?>
 258      <h2><?php echo esc_html( $title ); ?></h2>
 259      <div class="editwidget"<?php echo $width; ?>>
 260      <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
 261  
 262      <form action="widgets.php" method="post">
 263      <div class="widget-inside">
 264  <?php
 265      if ( is_callable( $control_callback ) )
 266          call_user_func_array( $control_callback, $control['params'] );
 267      else
 268          echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
 269      </div>
 270  
 271      <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
 272      <div class="widget-position">
 273      <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
 274  <?php
 275      foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
 276          echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
 277          if ( 'wp_inactive_widgets' == $sbname ) {
 278              echo '&nbsp;';
 279          } else {
 280              if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
 281                  $j = 1;
 282                  $sidebars_widgets[$sbname] = array();
 283              } else {
 284                  $j = count($sidebars_widgets[$sbname]);
 285                  if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
 286                      $j++;
 287              }
 288              $selected = '';
 289              echo "\t\t<select name='{$sbname}_position'>\n";
 290              echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
 291              for ( $i = 1; $i <= $j; $i++ ) {
 292                  if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
 293                      $selected = selected( $i, $key + 1, false );
 294                  echo "\t\t<option value='$i'$selected> $i </option>\n";
 295              }
 296              echo "\t\t</select>\n";
 297          }
 298          echo "</td></tr>\n";
 299      } ?>
 300      </tbody></table>
 301      </div>
 302  
 303      <div class="widget-control-actions">
 304  <?php    if ( isset($_GET['addnew']) ) { ?>
 305      <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
 306  <?php    } else { ?>
 307      <input type="submit" name="removewidget" class="button alignleft" value="<?php esc_attr_e('Delete'); ?>" />
 308  <?php    } ?>
 309      <input type="submit" name="savewidget" class="button-primary alignright" value="<?php esc_attr_e('Save Widget'); ?>" />
 310      <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
 311      <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
 312      <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
 313  <?php    wp_nonce_field("save-delete-widget-$widget_id"); ?>
 314      <br class="clear" />
 315      </div>
 316      </form>
 317      </div>
 318      </div>
 319  <?php
 320      require_once ( 'admin-footer.php' );
 321      exit;
 322  }
 323  
 324  $messages = array(
 325      __('Changes saved.')
 326  );
 327  
 328  $errors = array(
 329      __('Error while saving.'),
 330      __('Error in displaying the widget settings form.')
 331  );
 332  
 333  require_once ( 'admin-header.php' ); ?>
 334  
 335  <div class="wrap">
 336  <?php screen_icon(); ?>
 337  <h2><?php echo esc_html( $title ); ?></h2>
 338  
 339  <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
 340  <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
 341  <?php } ?>
 342  <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
 343  <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
 344  <?php } ?>
 345  
 346  <?php do_action( 'widgets_admin_page' ); ?>
 347  
 348  <div class="widget-liquid-left">
 349  <div id="widgets-left">
 350      <div id="available-widgets" class="widgets-holder-wrap">
 351          <div class="sidebar-name">
 352          <div class="sidebar-name-arrow"><br /></div>
 353          <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _e('Deactivate'); ?> <span></span></span></h3></div>
 354          <div class="widget-holder">
 355          <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>
 356          <div id="widget-list">
 357          <?php wp_list_widgets(); ?>
 358          </div>
 359          <br class='clear' />
 360          </div>
 361          <br class="clear" />
 362      </div>
 363  
 364      <div class="widgets-holder-wrap">
 365          <div class="sidebar-name">
 366          <div class="sidebar-name-arrow"><br /></div>
 367          <h3><?php _e('Inactive Widgets'); ?>
 368          <span><img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-feedback" title="" alt="" /></span></h3></div>
 369          <div class="widget-holder inactive">
 370          <p class="description"><?php _e('Drag widgets here to remove them from the sidebar but keep their settings.'); ?></p>
 371          <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
 372          <br class="clear" />
 373          </div>
 374      </div>
 375  </div>
 376  </div>
 377  
 378  <div class="widget-liquid-right">
 379  <div id="widgets-right">
 380  <?php
 381  $i = 0;
 382  foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
 383      if ( 'wp_inactive_widgets' == $sidebar )
 384          continue;
 385      $closed = $i ? ' closed' : ''; ?>
 386      <div class="widgets-holder-wrap<?php echo $closed; ?>">
 387      <div class="sidebar-name">
 388      <div class="sidebar-name-arrow"><br /></div>
 389      <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
 390      <span><img src="<?php echo esc_url( admin_url( 'images/wpspin_dark.gif' ) ); ?>" class="ajax-feedback" title="" alt="" /></span></h3></div>
 391      <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
 392      </div>
 393  <?php
 394      $i++;
 395  } ?>
 396  </div>
 397  </div>
 398  <form action="" method="post">
 399  <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
 400  </form>
 401  <br class="clear" />
 402  </div>
 403  
 404  <?php
 405  do_action( 'sidebar_admin_page' );
 406  require_once ( 'admin-footer.php' );


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7