mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Widgets administration panel.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /** WordPress Administration Bootstrap */
mp-wp_genesis 10 require_once( 'admin.php' );
mp-wp_genesis 11
mp-wp_genesis 12 /** WordPress Administration Widgets API */
mp-wp_genesis 13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
mp-wp_genesis 14
mp-wp_genesis 15 if ( ! current_user_can('switch_themes') )
mp-wp_genesis 16 wp_die( __( 'Cheatin’ uh?' ));
mp-wp_genesis 17
mp-wp_genesis 18 wp_enqueue_script( array( 'wp-lists', 'admin-widgets' ) );
mp-wp_genesis 19 wp_admin_css( 'widgets' );
mp-wp_genesis 20
mp-wp_genesis 21 do_action( 'sidebar_admin_setup' );
mp-wp_genesis 22
mp-wp_genesis 23 $title = __( 'Widgets' );
mp-wp_genesis 24 $parent_file = 'themes.php';
mp-wp_genesis 25
mp-wp_genesis 26 // $sidebar = What sidebar are we editing?
mp-wp_genesis 27 if ( isset($_GET['sidebar']) && isset($wp_registered_sidebars[$_GET['sidebar']]) ) {
mp-wp_genesis 28 $sidebar = attribute_escape( $_GET['sidebar'] );
mp-wp_genesis 29 } elseif ( is_array($wp_registered_sidebars) && !empty($wp_registered_sidebars) ) {
mp-wp_genesis 30 // By default we look at the first defined sidebar
mp-wp_genesis 31 $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
mp-wp_genesis 32 } else {
mp-wp_genesis 33 // If no sidebars, die.
mp-wp_genesis 34 require_once( 'admin-header.php' );
mp-wp_genesis 35 ?>
mp-wp_genesis 36
mp-wp_genesis 37 <div class="wrap">
mp-wp_genesis 38 <?php screen_icon(); ?>
mp-wp_genesis 39 <h2><?php echo wp_specialchars( $title ); ?></h2>
mp-wp_genesis 40 <div class="error">
mp-wp_genesis 41 <p><?php _e( 'No Sidebars Defined' ); ?></p>
mp-wp_genesis 42 </div>
mp-wp_genesis 43 <p><?php _e( 'The theme you are currently using isn’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>
mp-wp_genesis 44 </div>
mp-wp_genesis 45
mp-wp_genesis 46 <?php
mp-wp_genesis 47 require_once( 'admin-footer.php' );
mp-wp_genesis 48 exit;
mp-wp_genesis 49 }
mp-wp_genesis 50
mp-wp_genesis 51 // These are the widgets grouped by sidebar
mp-wp_genesis 52 $sidebars_widgets = wp_get_sidebars_widgets();
mp-wp_genesis 53 if ( empty( $sidebars_widgets ) )
mp-wp_genesis 54 $sidebars_widgets = wp_get_widget_defaults();
mp-wp_genesis 55
mp-wp_genesis 56 // for the sake of PHP warnings
mp-wp_genesis 57 if ( empty( $sidebars_widgets[$sidebar] ) )
mp-wp_genesis 58 $sidebars_widgets[$sidebar] = array();
mp-wp_genesis 59
mp-wp_genesis 60 $http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
mp-wp_genesis 61
mp-wp_genesis 62 // We're updating a sidebar
mp-wp_genesis 63 if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
mp-wp_genesis 64 check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
mp-wp_genesis 65
mp-wp_genesis 66 /* Hack #1
mp-wp_genesis 67 * The widget_control is overloaded. It updates the widget's options AND echoes out the widget's HTML form.
mp-wp_genesis 68 * Since we want to update before sending out any headers, we have to catch it with an output buffer,
mp-wp_genesis 69 */
mp-wp_genesis 70 ob_start();
mp-wp_genesis 71 /* There can be multiple widgets of the same type, but the widget_control for that
mp-wp_genesis 72 * widget type needs only be called once if it's a multi-widget.
mp-wp_genesis 73 */
mp-wp_genesis 74 $already_done = array();
mp-wp_genesis 75
mp-wp_genesis 76 foreach ( $wp_registered_widget_controls as $name => $control ) {
mp-wp_genesis 77 if ( in_array( $control['callback'], $already_done ) )
mp-wp_genesis 78 continue;
mp-wp_genesis 79
mp-wp_genesis 80 if ( is_callable( $control['callback'] ) ) {
mp-wp_genesis 81 call_user_func_array( $control['callback'], $control['params'] );
mp-wp_genesis 82 $control_output = ob_get_contents();
mp-wp_genesis 83 if ( false !== strpos( $control_output, '%i%' ) ) // if it's a multi-widget, only call control function once.
mp-wp_genesis 84 $already_done[] = $control['callback'];
mp-wp_genesis 85 }
mp-wp_genesis 86
mp-wp_genesis 87 ob_clean();
mp-wp_genesis 88 }
mp-wp_genesis 89 ob_end_clean();
mp-wp_genesis 90
mp-wp_genesis 91 // Prophylactic. Take out empty ids.
mp-wp_genesis 92 foreach ( (array) $_POST['widget-id'] as $key => $val )
mp-wp_genesis 93 if ( !$val )
mp-wp_genesis 94 unset($_POST['widget-id'][$key]);
mp-wp_genesis 95
mp-wp_genesis 96 // Reset the key numbering and store
mp-wp_genesis 97 $new_sidebar = isset( $_POST['widget-id'] ) && is_array( $_POST['widget-id'] ) ? array_values( $_POST['widget-id'] ) : array();
mp-wp_genesis 98 $sidebars_widgets[$_POST['sidebar']] = $new_sidebar;
mp-wp_genesis 99 wp_set_sidebars_widgets( $sidebars_widgets );
mp-wp_genesis 100
mp-wp_genesis 101 wp_redirect( add_query_arg( 'message', 'updated' ) );
mp-wp_genesis 102 exit;
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 // What widget (if any) are we editing
mp-wp_genesis 106 $edit_widget = -1;
mp-wp_genesis 107
mp-wp_genesis 108 $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
mp-wp_genesis 109
mp-wp_genesis 110 if ( isset($_GET['add']) && $_GET['add'] ) {
mp-wp_genesis 111 // Add to the end of the sidebar
mp-wp_genesis 112 $control_callback;
mp-wp_genesis 113 if ( isset($wp_registered_widgets[$_GET['add']]) ) {
mp-wp_genesis 114 check_admin_referer( "add-widget_$_GET[add]" );
mp-wp_genesis 115 $sidebars_widgets[$sidebar][] = $_GET['add'];
mp-wp_genesis 116 wp_set_sidebars_widgets( $sidebars_widgets );
mp-wp_genesis 117 } elseif ( isset($_GET['base']) && isset($_GET['key']) ) { // It's a multi-widget
mp-wp_genesis 118 check_admin_referer( "add-widget_$_GET[add]" );
mp-wp_genesis 119 // Copy minimal info from an existing instance of this widget to a new instance
mp-wp_genesis 120 foreach ( $wp_registered_widget_controls as $control ) {
mp-wp_genesis 121 if ( $_GET['base'] === $control['id_base'] ) {
mp-wp_genesis 122 $control_callback = $control['callback'];
mp-wp_genesis 123 $num = (int) $_GET['key'];
mp-wp_genesis 124 $control['params'][0]['number'] = $num;
mp-wp_genesis 125 $control['id'] = $control['id_base'] . '-' . $num;
mp-wp_genesis 126 $wp_registered_widget_controls[$control['id']] = $control;
mp-wp_genesis 127 $sidebars_widgets[$sidebar][] = $control['id'];
mp-wp_genesis 128 break;
mp-wp_genesis 129 }
mp-wp_genesis 130 }
mp-wp_genesis 131 }
mp-wp_genesis 132
mp-wp_genesis 133 // it's a multi-widget. The only way to add multi-widgets without JS is to actually submit POST content...
mp-wp_genesis 134 // so here we go
mp-wp_genesis 135 if ( is_callable( $control_callback ) ) {
mp-wp_genesis 136 require_once( 'admin-header.php' );
mp-wp_genesis 137 ?>
mp-wp_genesis 138 <div class="wrap">
mp-wp_genesis 139 <h2><?php _e( 'Add Widget' ); ?></h2>
mp-wp_genesis 140 <br />
mp-wp_genesis 141 <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
mp-wp_genesis 142
mp-wp_genesis 143 <ul class="widget-control-list">
mp-wp_genesis 144 <li class="widget-list-control-item">
mp-wp_genesis 145 <div class="widget-top">
mp-wp_genesis 146 <h4 class="widget-title"><?php echo $control['name']; ?></h4>
mp-wp_genesis 147 </div>
mp-wp_genesis 148 <div class="widget-control" style="display: block;">
mp-wp_genesis 149 <?php
mp-wp_genesis 150 call_user_func_array( $control_callback, $control['params'] );
mp-wp_genesis 151 ?>
mp-wp_genesis 152 <div class="widget-control-actions">
mp-wp_genesis 153 <input type="submit" class="button" value="<?php _e( 'Add Widget' ); ?>" />
mp-wp_genesis 154 <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
mp-wp_genesis 155 <?php wp_nonce_field ( 'edit-sidebar_' . $sidebar );
mp-wp_genesis 156 foreach ( $sidebars_widgets[$sidebar] as $sidebar_widget_id ) : ?>
mp-wp_genesis 157 <input type="hidden" name='widget-id[]' value="<?php echo $sidebar_widget_id; ?>" />
mp-wp_genesis 158 <?php endforeach; ?>
mp-wp_genesis 159 </div>
mp-wp_genesis 160 </div>
mp-wp_genesis 161 </li>
mp-wp_genesis 162 </ul>
mp-wp_genesis 163 </form>
mp-wp_genesis 164 </div>
mp-wp_genesis 165 <?php
mp-wp_genesis 166
mp-wp_genesis 167 require_once( 'admin-footer.php' );
mp-wp_genesis 168 exit;
mp-wp_genesis 169 }
mp-wp_genesis 170 wp_redirect( remove_query_arg( $query_args ) );
mp-wp_genesis 171 exit;
mp-wp_genesis 172 } elseif ( isset($_GET['remove']) && $_GET['remove'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
mp-wp_genesis 173 // Remove from sidebar the widget of type $_GET['remove'] and in position $_GET['key']
mp-wp_genesis 174 $key = (int) $_GET['key'];
mp-wp_genesis 175 if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['remove']) ) && in_array($key, $keys) ) {
mp-wp_genesis 176 check_admin_referer( "remove-widget_$_GET[remove]" );
mp-wp_genesis 177 unset($sidebars_widgets[$sidebar][$key]);
mp-wp_genesis 178 $sidebars_widgets[$sidebar] = array_values($sidebars_widgets[$sidebar]);
mp-wp_genesis 179 wp_set_sidebars_widgets( $sidebars_widgets );
mp-wp_genesis 180 }
mp-wp_genesis 181 wp_redirect( remove_query_arg( $query_args ) );
mp-wp_genesis 182 exit;
mp-wp_genesis 183 } elseif ( isset($_GET['edit']) && $_GET['edit'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
mp-wp_genesis 184 // Edit widget of type $_GET['edit'] and position $_GET['key']
mp-wp_genesis 185 $key = (int) $_GET['key'];
mp-wp_genesis 186 if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['edit']) ) && in_array($key, $keys) )
mp-wp_genesis 187 $edit_widget = $key;
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 // Total number of registered sidebars
mp-wp_genesis 191 $sidebar_widget_count = count($sidebars_widgets[$sidebar]);
mp-wp_genesis 192
mp-wp_genesis 193 // This is sort of lame since "widget" won't be converted to "widgets" in the JS
mp-wp_genesis 194 if ( 1 < $sidebars_count = count($wp_registered_sidebars) )
mp-wp_genesis 195 $sidebar_info_text = __ngettext( 'You are using %1$s widget in the "%2$s" sidebar.', 'You are using %1$s widgets in the "%2$s" sidebar.', $sidebar_widget_count );
mp-wp_genesis 196 else
mp-wp_genesis 197 $sidebar_info_text = __ngettext( 'You are using %1$s widget in the sidebar.', 'You are using %1$s widgets in the sidebar.', $sidebar_widget_count );
mp-wp_genesis 198
mp-wp_genesis 199
mp-wp_genesis 200 $sidebar_info_text = sprintf( wp_specialchars( $sidebar_info_text ), "<span id='widget-count'>$sidebar_widget_count</span>", $wp_registered_sidebars[$sidebar]['name'] );
mp-wp_genesis 201
mp-wp_genesis 202 $page = isset($_GET['apage']) ? abs( (int) $_GET['apage'] ) : 1;
mp-wp_genesis 203
mp-wp_genesis 204 /* TODO: Paginate widgets list
mp-wp_genesis 205 $page_links = paginate_links( array(
mp-wp_genesis 206 'base' => add_query_arg( 'apage', '%#%' ),
mp-wp_genesis 207 'format' => '',
mp-wp_genesis 208 'total' => ceil(($total = 105 )/ 10),
mp-wp_genesis 209 'current' => $page
mp-wp_genesis 210 ));
mp-wp_genesis 211 */
mp-wp_genesis 212 $page_links = ' ';
mp-wp_genesis 213
mp-wp_genesis 214 // Unsanitized!
mp-wp_genesis 215 $widget_search = isset($_GET['s']) ? $_GET['s'] : false;
mp-wp_genesis 216
mp-wp_genesis 217 // Not entirely sure what all should be here
mp-wp_genesis 218 $show_values = array(
mp-wp_genesis 219 '' => $widget_search ? __( 'Show any widgets' ) : __( 'Show all widgets' ),
mp-wp_genesis 220 'unused' => __( 'Show unused widgets' ),
mp-wp_genesis 221 'used' => __( 'Show used widgets' )
mp-wp_genesis 222 );
mp-wp_genesis 223
mp-wp_genesis 224 $show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
mp-wp_genesis 225
mp-wp_genesis 226 $messages = array(
mp-wp_genesis 227 'updated' => __('Changes saved.')
mp-wp_genesis 228 );
mp-wp_genesis 229
mp-wp_genesis 230 require_once( 'admin-header.php' ); ?>
mp-wp_genesis 231
mp-wp_genesis 232 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) : ?>
mp-wp_genesis 233 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
mp-wp_genesis 234 <?php endif; ?>
mp-wp_genesis 235
mp-wp_genesis 236 <div class="wrap">
mp-wp_genesis 237 <?php screen_icon(); ?>
mp-wp_genesis 238 <h2><?php echo wp_specialchars( $title ); ?></h2>
mp-wp_genesis 239
mp-wp_genesis 240 <form id="widgets-filter" action="" method="get">
mp-wp_genesis 241
mp-wp_genesis 242 <div class="widget-liquid-left-holder">
mp-wp_genesis 243 <div id="available-widgets-filter" class="widget-liquid-left">
mp-wp_genesis 244 <h3><label for="show"><?php _e('Available Widgets'); ?></label></h3>
mp-wp_genesis 245 <div class="nav">
mp-wp_genesis 246 <select name="show" id="show">
mp-wp_genesis 247 <?php foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
mp-wp_genesis 248 <option value='<?php echo $show_value; ?>'<?php selected( $show_value, $show ); ?>><?php echo wp_specialchars( $show_text ); ?></option>
mp-wp_genesis 249 <?php endforeach; ?>
mp-wp_genesis 250 </select>
mp-wp_genesis 251 <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
mp-wp_genesis 252 <p class="pagenav">
mp-wp_genesis 253 <?php echo $page_links; ?>
mp-wp_genesis 254 </p>
mp-wp_genesis 255 </div>
mp-wp_genesis 256 </div>
mp-wp_genesis 257 </div>
mp-wp_genesis 258
mp-wp_genesis 259 <div id="available-sidebars" class="widget-liquid-right">
mp-wp_genesis 260 <h3><label for="sidebar-selector"><?php _e('Current Widgets'); ?></label></h3>
mp-wp_genesis 261
mp-wp_genesis 262 <div class="nav">
mp-wp_genesis 263 <select id="sidebar-selector" name="sidebar">
mp-wp_genesis 264 <?php foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
mp-wp_genesis 265 <option value='<?php echo $sidebar_id; ?>'<?php selected( $sidebar_id, $sidebar ); ?>><?php echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
mp-wp_genesis 266 <?php endforeach; ?>
mp-wp_genesis 267 </select>
mp-wp_genesis 268 <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
mp-wp_genesis 269 </div>
mp-wp_genesis 270
mp-wp_genesis 271 </div>
mp-wp_genesis 272
mp-wp_genesis 273 </form>
mp-wp_genesis 274
mp-wp_genesis 275 <div id="widget-content" class="widget-liquid-left-holder">
mp-wp_genesis 276
mp-wp_genesis 277 <div id="available-widgets" class="widget-liquid-left">
mp-wp_genesis 278
mp-wp_genesis 279 <?php wp_list_widgets( $show, $widget_search ); // This lists all the widgets for the query ( $show, $search ) ?>
mp-wp_genesis 280
mp-wp_genesis 281 <div class="nav">
mp-wp_genesis 282 <p class="pagenav">
mp-wp_genesis 283 <?php echo $page_links; ?>
mp-wp_genesis 284 </p>
mp-wp_genesis 285 </div>
mp-wp_genesis 286 </div>
mp-wp_genesis 287 </div>
mp-wp_genesis 288
mp-wp_genesis 289 <form id="widget-controls" action="" method="post">
mp-wp_genesis 290
mp-wp_genesis 291 <div id="current-widgets-head" class="widget-liquid-right">
mp-wp_genesis 292
mp-wp_genesis 293 <div id="sidebar-info">
mp-wp_genesis 294 <p><?php echo $sidebar_info_text; ?></p>
mp-wp_genesis 295 <p><?php _e( 'Add more from the Available Widgets section.' ); ?></p>
mp-wp_genesis 296 </div>
mp-wp_genesis 297
mp-wp_genesis 298 </div>
mp-wp_genesis 299
mp-wp_genesis 300 <div id="current-widgets" class="widget-liquid-right">
mp-wp_genesis 301 <div id="current-sidebar">
mp-wp_genesis 302
mp-wp_genesis 303 <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
mp-wp_genesis 304
mp-wp_genesis 305 </div>
mp-wp_genesis 306
mp-wp_genesis 307 <p class="submit">
mp-wp_genesis 308 <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
mp-wp_genesis 309 <input type="hidden" id="generated-time" name="generated-time" value="<?php echo time() - 1199145600; // Jan 1, 2008 ?>" />
mp-wp_genesis 310 <input type="submit" name="save-widgets" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
mp-wp_genesis 311 <?php
mp-wp_genesis 312 wp_nonce_field( 'edit-sidebar_' . $sidebar );
mp-wp_genesis 313 ?>
mp-wp_genesis 314 </p>
mp-wp_genesis 315 </div>
mp-wp_genesis 316
mp-wp_genesis 317 </form>
mp-wp_genesis 318 <br class="clear" />
mp-wp_genesis 319
mp-wp_genesis 320 </div>
mp-wp_genesis 321
mp-wp_genesis 322 <?php do_action( 'sidebar_admin_page' ); ?>
mp-wp_genesis 323
mp-wp_genesis 324 <?php require_once( 'admin-footer.php' ); ?>
mp-wp_genesis 325
mp-wp_genesis 326