wp/wp-admin/widgets.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php
       
     2 /**
       
     3  * Widgets administration panel.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Administration
       
     7  */
       
     8 
       
     9 /** WordPress Administration Bootstrap */
       
    10 require_once( dirname( __FILE__ ) . '/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 $widgets_access = get_user_setting( 'widgets_access' );
       
    19 if ( isset($_GET['widgets-access']) ) {
       
    20 	$widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
       
    21 	set_user_setting( 'widgets_access', $widgets_access );
       
    22 }
       
    23 
       
    24 function wp_widgets_access_body_class($classes) {
       
    25 	return "$classes widgets_access ";
       
    26 }
       
    27 
       
    28 if ( 'on' == $widgets_access ) {
       
    29 	add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
       
    30 } else {
       
    31 	wp_enqueue_script('admin-widgets');
       
    32 
       
    33 	if ( wp_is_mobile() )
       
    34 		wp_enqueue_script( 'jquery-touch-punch' );
       
    35 }
       
    36 
       
    37 /**
       
    38  * Fires early before the Widgets administration screen loads, after scripts are enqueued.
       
    39  *
       
    40  * @since 2.2.0
       
    41  */
       
    42 do_action( 'sidebar_admin_setup' );
       
    43 
       
    44 $title = __( 'Widgets' );
       
    45 $parent_file = 'themes.php';
       
    46 
       
    47 get_current_screen()->add_help_tab( array(
       
    48 'id'		=> 'overview',
       
    49 'title'		=> __('Overview'),
       
    50 'content'	=>
       
    51 	'<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>
       
    52 	<p>' . __('The 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>'
       
    53 ) );
       
    54 get_current_screen()->add_help_tab( array(
       
    55 'id'		=> 'removing-reusing',
       
    56 'title'		=> __('Removing and Reusing'),
       
    57 'content'	=>
       
    58 	'<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>
       
    59 	<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>
       
    60 	<p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
       
    61 ) );
       
    62 get_current_screen()->add_help_tab( array(
       
    63 'id'		=> 'missing-widgets',
       
    64 'title'		=> __('Missing Widgets'),
       
    65 'content'	=>
       
    66 	'<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>' .
       
    67 		'<p>' . __('When changing themes, there is often some variation in the number and setup of widget areas/sidebars and sometimes these conflicts make the transition a bit less smooth. If you changed themes and seem to be missing widgets, scroll down on this screen to the Inactive Widgets area, where all of your widgets and their settings will have been saved.') . '</p>'
       
    68 ) );
       
    69 
       
    70 get_current_screen()->set_help_sidebar(
       
    71 	'<p><strong>' . __('For more information:') . '</strong></p>' .
       
    72 	'<p>' . __('<a href="http://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
       
    73 	'<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
       
    74 );
       
    75 
       
    76 if ( ! current_theme_supports( 'widgets' ) ) {
       
    77 	wp_die( __( '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>.' ) );
       
    78 }
       
    79 
       
    80 // These are the widgets grouped by sidebar
       
    81 $sidebars_widgets = wp_get_sidebars_widgets();
       
    82 
       
    83 if ( empty( $sidebars_widgets ) )
       
    84 	$sidebars_widgets = wp_get_widget_defaults();
       
    85 
       
    86 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
       
    87 	if ( 'wp_inactive_widgets' == $sidebar_id )
       
    88 		continue;
       
    89 
       
    90 	if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
       
    91 		if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
       
    92 			register_sidebar(array(
       
    93 				'name' => __( 'Inactive Sidebar (not used)' ),
       
    94 				'id' => $sidebar_id,
       
    95 				'class' => 'inactive-sidebar orphan-sidebar',
       
    96 				'description' => __( 'This sidebar is no longer available and does not show anywhere on your site. Remove each of the widgets below to fully remove this inactive sidebar.' ),
       
    97 				'before_widget' => '',
       
    98 				'after_widget' => '',
       
    99 				'before_title' => '',
       
   100 				'after_title' => '',
       
   101 			));
       
   102 		} else {
       
   103 			unset( $sidebars_widgets[ $sidebar_id ] );
       
   104 		}
       
   105 	}
       
   106 }
       
   107 
       
   108 // register the inactive_widgets area as sidebar
       
   109 register_sidebar(array(
       
   110 	'name' => __('Inactive Widgets'),
       
   111 	'id' => 'wp_inactive_widgets',
       
   112 	'class' => 'inactive-sidebar',
       
   113 	'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
       
   114 	'before_widget' => '',
       
   115 	'after_widget' => '',
       
   116 	'before_title' => '',
       
   117 	'after_title' => '',
       
   118 ));
       
   119 
       
   120 retrieve_widgets();
       
   121 
       
   122 // We're saving a widget without js
       
   123 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
       
   124 	$widget_id = $_POST['widget-id'];
       
   125 	check_admin_referer("save-delete-widget-$widget_id");
       
   126 
       
   127 	$number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
       
   128 	if ( $number ) {
       
   129 		foreach ( $_POST as $key => $val ) {
       
   130 			if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
       
   131 				$_POST[$key] = array( $number => array_shift($val) );
       
   132 				break;
       
   133 			}
       
   134 		}
       
   135 	}
       
   136 
       
   137 	$sidebar_id = $_POST['sidebar'];
       
   138 	$position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
       
   139 
       
   140 	$id_base = $_POST['id_base'];
       
   141 	$sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
       
   142 
       
   143 	// delete
       
   144 	if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
       
   145 
       
   146 		if ( !in_array($widget_id, $sidebar, true) ) {
       
   147 			wp_redirect( admin_url('widgets.php?error=0') );
       
   148 			exit;
       
   149 		}
       
   150 
       
   151 		$sidebar = array_diff( $sidebar, array($widget_id) );
       
   152 		$_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
       
   153 	}
       
   154 
       
   155 	$_POST['widget-id'] = $sidebar;
       
   156 
       
   157 	foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
       
   158 		if ( $name != $id_base || !is_callable($control['callback']) )
       
   159 			continue;
       
   160 
       
   161 		ob_start();
       
   162 			call_user_func_array( $control['callback'], $control['params'] );
       
   163 		ob_end_clean();
       
   164 
       
   165 		break;
       
   166 	}
       
   167 
       
   168 	$sidebars_widgets[$sidebar_id] = $sidebar;
       
   169 
       
   170 	// remove old position
       
   171 	if ( !isset($_POST['delete_widget']) ) {
       
   172 		foreach ( $sidebars_widgets as $key => $sb ) {
       
   173 			if ( is_array($sb) )
       
   174 				$sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
       
   175 		}
       
   176 		array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
       
   177 	}
       
   178 
       
   179 	wp_set_sidebars_widgets($sidebars_widgets);
       
   180 	wp_redirect( admin_url('widgets.php?message=0') );
       
   181 	exit;
       
   182 }
       
   183 
       
   184 // Output the widget form without js
       
   185 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
       
   186 	$widget_id = $_GET['editwidget'];
       
   187 
       
   188 	if ( isset($_GET['addnew']) ) {
       
   189 		// Default to the first sidebar
       
   190 		$keys = array_keys( $wp_registered_sidebars );
       
   191 		$sidebar = array_shift( $keys );
       
   192 
       
   193 		if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
       
   194 			// Copy minimal info from an existing instance of this widget to a new instance
       
   195 			foreach ( $wp_registered_widget_controls as $control ) {
       
   196 				if ( $_GET['base'] === $control['id_base'] ) {
       
   197 					$control_callback = $control['callback'];
       
   198 					$multi_number = (int) $_GET['num'];
       
   199 					$control['params'][0]['number'] = -1;
       
   200 					$widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
       
   201 					$wp_registered_widget_controls[$control['id']] = $control;
       
   202 					break;
       
   203 				}
       
   204 			}
       
   205 		}
       
   206 	}
       
   207 
       
   208 	if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
       
   209 		$control = $wp_registered_widget_controls[$widget_id];
       
   210 		$control_callback = $control['callback'];
       
   211 	} elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
       
   212 		$name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
       
   213 	}
       
   214 
       
   215 	if ( !isset($name) )
       
   216 		$name = esc_html( strip_tags($control['name']) );
       
   217 
       
   218 	if ( !isset($sidebar) )
       
   219 		$sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
       
   220 
       
   221 	if ( !isset($multi_number) )
       
   222 		$multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
       
   223 
       
   224 	$id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
       
   225 
       
   226 	// show the widget form
       
   227 	$width = ' style="width:' . max($control['width'], 350) . 'px"';
       
   228 	$key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
       
   229 
       
   230 	require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
       
   231 	<div class="wrap">
       
   232 	<?php screen_icon(); ?>
       
   233 	<h2><?php echo esc_html( $title ); ?></h2>
       
   234 	<div class="editwidget"<?php echo $width; ?>>
       
   235 	<h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
       
   236 
       
   237 	<form action="widgets.php" method="post">
       
   238 	<div class="widget-inside">
       
   239 <?php
       
   240 	if ( is_callable( $control_callback ) )
       
   241 		call_user_func_array( $control_callback, $control['params'] );
       
   242 	else
       
   243 		echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
       
   244 	</div>
       
   245 
       
   246 	<p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
       
   247 	<div class="widget-position">
       
   248 	<table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
       
   249 <?php
       
   250 	foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
       
   251 		echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
       
   252 		if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
       
   253 			echo '&nbsp;';
       
   254 		} else {
       
   255 			if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
       
   256 				$j = 1;
       
   257 				$sidebars_widgets[$sbname] = array();
       
   258 			} else {
       
   259 				$j = count($sidebars_widgets[$sbname]);
       
   260 				if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
       
   261 					$j++;
       
   262 			}
       
   263 			$selected = '';
       
   264 			echo "\t\t<select name='{$sbname}_position'>\n";
       
   265 			echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
       
   266 			for ( $i = 1; $i <= $j; $i++ ) {
       
   267 				if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
       
   268 					$selected = selected( $i, $key + 1, false );
       
   269 				echo "\t\t<option value='$i'$selected> $i </option>\n";
       
   270 			}
       
   271 			echo "\t\t</select>\n";
       
   272 		}
       
   273 		echo "</td></tr>\n";
       
   274 	} ?>
       
   275 	</tbody></table>
       
   276 	</div>
       
   277 
       
   278 	<div class="widget-control-actions">
       
   279 <?php
       
   280 	if ( isset($_GET['addnew']) ) { ?>
       
   281 	<a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
       
   282 <?php
       
   283 	} else {
       
   284 		submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
       
   285 	}
       
   286 	submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
       
   287 	<input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
       
   288 	<input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
       
   289 	<input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
       
   290 <?php	wp_nonce_field("save-delete-widget-$widget_id"); ?>
       
   291 	<br class="clear" />
       
   292 	</div>
       
   293 	</form>
       
   294 	</div>
       
   295 	</div>
       
   296 <?php
       
   297 	require_once( ABSPATH . 'wp-admin/admin-footer.php' );
       
   298 	exit;
       
   299 }
       
   300 
       
   301 $messages = array(
       
   302 	__('Changes saved.')
       
   303 );
       
   304 
       
   305 $errors = array(
       
   306 	__('Error while saving.'),
       
   307 	__('Error in displaying the widget settings form.')
       
   308 );
       
   309 
       
   310 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
       
   311 
       
   312 <div class="wrap">
       
   313 <?php screen_icon(); ?>
       
   314 <h2><?php echo esc_html( $title ); ?></h2>
       
   315 
       
   316 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
       
   317 <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
       
   318 <?php } ?>
       
   319 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
       
   320 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
       
   321 <?php } ?>
       
   322 
       
   323 <?php
       
   324 /**
       
   325  * Fires before the Widgets administration page content loads.
       
   326  *
       
   327  * @since 3.0.0
       
   328  */
       
   329 do_action( 'widgets_admin_page' ); ?>
       
   330 
       
   331 <div class="widget-liquid-left">
       
   332 <div id="widgets-left">
       
   333 	<div id="available-widgets" class="widgets-holder-wrap">
       
   334 		<div class="sidebar-name">
       
   335 		<div class="sidebar-name-arrow"><br /></div>
       
   336 		<h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3></div>
       
   337 		<div class="widget-holder">
       
   338 		<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>
       
   339 		<div id="widget-list">
       
   340 		<?php wp_list_widgets(); ?>
       
   341 		</div>
       
   342 		<br class='clear' />
       
   343 		</div>
       
   344 		<br class="clear" />
       
   345 	</div>
       
   346 
       
   347 <?php
       
   348 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
       
   349 	if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
       
   350 		$wrap_class = 'widgets-holder-wrap';
       
   351 		if ( !empty( $registered_sidebar['class'] ) )
       
   352 			$wrap_class .= ' ' . $registered_sidebar['class'];
       
   353 
       
   354 ?>
       
   355 
       
   356 		<div class="<?php echo esc_attr( $wrap_class ); ?>">
       
   357 			<div class="sidebar-name">
       
   358 				<div class="sidebar-name-arrow"><br /></div>
       
   359 				<h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
       
   360 					<span class="spinner"></span>
       
   361 				</h3>
       
   362 			</div>
       
   363 			<div class="widget-holder inactive">
       
   364 				<?php wp_list_widget_controls( $registered_sidebar['id'] ); ?>
       
   365 				<div class="clear"></div>
       
   366 			</div>
       
   367 		</div>
       
   368 <?php
       
   369 	}
       
   370 }
       
   371 ?>
       
   372 
       
   373 </div>
       
   374 </div>
       
   375 
       
   376 <div class="widget-liquid-right">
       
   377 <div id="widgets-right">
       
   378 <?php
       
   379 $i = 0;
       
   380 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
       
   381 	if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
       
   382 		continue;
       
   383 
       
   384 	$wrap_class = 'widgets-holder-wrap';
       
   385 	if ( !empty( $registered_sidebar['class'] ) )
       
   386 		$wrap_class .= ' sidebar-' . $registered_sidebar['class'];
       
   387 
       
   388 	if ( $i )
       
   389 		$wrap_class .= ' closed'; ?>
       
   390 
       
   391 	<div class="<?php echo esc_attr( $wrap_class ); ?>">
       
   392 	<div class="sidebar-name">
       
   393 	<div class="sidebar-name-arrow"><br /></div>
       
   394 	<h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
       
   395 	<span class="spinner"></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 
       
   411 /**
       
   412  * Fires after the available widgets and sidebars have loaded, before the admin footer.
       
   413  *
       
   414  * @since 2.2.0
       
   415  */
       
   416 do_action( 'sidebar_admin_page' );
       
   417 require_once( ABSPATH . 'wp-admin/admin-footer.php' );