wp/wp-admin/widgets.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    11 
    11 
    12 /** WordPress Administration Widgets API */
    12 /** WordPress Administration Widgets API */
    13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
    13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
    14 
    14 
    15 if ( ! current_user_can('edit_theme_options') )
    15 if ( ! current_user_can('edit_theme_options') )
    16 	wp_die( __( 'Cheatin’ uh?' ));
    16 	wp_die( __( 'Cheatin’ uh?' ), 403 );
    17 
    17 
    18 $widgets_access = get_user_setting( 'widgets_access' );
    18 $widgets_access = get_user_setting( 'widgets_access' );
    19 if ( isset($_GET['widgets-access']) ) {
    19 if ( isset($_GET['widgets-access']) ) {
    20 	$widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
    20 	$widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
    21 	set_user_setting( 'widgets_access', $widgets_access );
    21 	set_user_setting( 'widgets_access', $widgets_access );
    33 	if ( wp_is_mobile() )
    33 	if ( wp_is_mobile() )
    34 		wp_enqueue_script( 'jquery-touch-punch' );
    34 		wp_enqueue_script( 'jquery-touch-punch' );
    35 }
    35 }
    36 
    36 
    37 /**
    37 /**
    38  * Fires early before the Widgets administration screen loads, after scripts are enqueued.
    38  * Fires early before the Widgets administration screen loads,
       
    39  * after scripts are enqueued.
    39  *
    40  *
    40  * @since 2.2.0
    41  * @since 2.2.0
    41  */
    42  */
    42 do_action( 'sidebar_admin_setup' );
    43 do_action( 'sidebar_admin_setup' );
    43 
    44 
    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 		'<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 ) );
    69 
    70 
    70 get_current_screen()->set_help_sidebar(
    71 get_current_screen()->set_help_sidebar(
    71 	'<p><strong>' . __('For more information:') . '</strong></p>' .
    72 	'<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="https://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 	'<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
    74 );
    75 );
    75 
    76 
    76 if ( ! current_theme_supports( 'widgets' ) ) {
    77 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 	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="https://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
    78 }
    79 }
    79 
    80 
    80 // These are the widgets grouped by sidebar
    81 // These are the widgets grouped by sidebar
    81 $sidebars_widgets = wp_get_sidebars_widgets();
    82 $sidebars_widgets = wp_get_sidebars_widgets();
    82 
    83 
   138 	$position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
   139 	$position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
   139 
   140 
   140 	$id_base = $_POST['id_base'];
   141 	$id_base = $_POST['id_base'];
   141 	$sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
   142 	$sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
   142 
   143 
   143 	// delete
   144 	// Delete.
   144 	if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
   145 	if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
   145 
   146 
   146 		if ( !in_array($widget_id, $sidebar, true) ) {
   147 		if ( !in_array($widget_id, $sidebar, true) ) {
   147 			wp_redirect( admin_url('widgets.php?error=0') );
   148 			wp_redirect( admin_url('widgets.php?error=0') );
   148 			exit;
   149 			exit;
   165 		break;
   166 		break;
   166 	}
   167 	}
   167 
   168 
   168 	$sidebars_widgets[$sidebar_id] = $sidebar;
   169 	$sidebars_widgets[$sidebar_id] = $sidebar;
   169 
   170 
   170 	// remove old position
   171 	// Remove old position.
   171 	if ( !isset($_POST['delete_widget']) ) {
   172 	if ( !isset($_POST['delete_widget']) ) {
   172 		foreach ( $sidebars_widgets as $key => $sb ) {
   173 		foreach ( $sidebars_widgets as $key => $sb ) {
   173 			if ( is_array($sb) )
   174 			if ( is_array($sb) )
   174 				$sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
   175 				$sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
   175 		}
   176 		}
   186 	$widget_id = $_GET['editwidget'];
   187 	$widget_id = $_GET['editwidget'];
   187 
   188 
   188 	if ( isset($_GET['addnew']) ) {
   189 	if ( isset($_GET['addnew']) ) {
   189 		// Default to the first sidebar
   190 		// Default to the first sidebar
   190 		$keys = array_keys( $wp_registered_sidebars );
   191 		$keys = array_keys( $wp_registered_sidebars );
   191 		$sidebar = array_shift( $keys );
   192 		$sidebar = reset( $keys );
   192 
   193 
   193 		if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
   194 		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 			// Copy minimal info from an existing instance of this widget to a new instance
   195 			foreach ( $wp_registered_widget_controls as $control ) {
   196 			foreach ( $wp_registered_widget_controls as $control ) {
   196 				if ( $_GET['base'] === $control['id_base'] ) {
   197 				if ( $_GET['base'] === $control['id_base'] ) {
   221 	if ( !isset($multi_number) )
   222 	if ( !isset($multi_number) )
   222 		$multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
   223 		$multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
   223 
   224 
   224 	$id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
   225 	$id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
   225 
   226 
   226 	// show the widget form
   227 	// Show the widget form.
   227 	$width = ' style="width:' . max($control['width'], 350) . 'px"';
   228 	$width = ' style="width:' . max($control['width'], 350) . 'px"';
   228 	$key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
   229 	$key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
   229 
   230 
   230 	require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
   231 	require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
   231 	<div class="wrap">
   232 	<div class="wrap">
   232 	<?php screen_icon(); ?>
       
   233 	<h2><?php echo esc_html( $title ); ?></h2>
   233 	<h2><?php echo esc_html( $title ); ?></h2>
   234 	<div class="editwidget"<?php echo $width; ?>>
   234 	<div class="editwidget"<?php echo $width; ?>>
   235 	<h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
   235 	<h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
   236 
   236 
   237 	<form action="widgets.php" method="post">
   237 	<form action="widgets.php" method="post">
   308 );
   308 );
   309 
   309 
   310 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
   310 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
   311 
   311 
   312 <div class="wrap">
   312 <div class="wrap">
   313 <?php screen_icon(); ?>
   313 <h2>
   314 <h2><?php echo esc_html( $title ); ?></h2>
   314 <?php
       
   315 	echo esc_html( $title );
       
   316 	if ( current_user_can( 'customize' ) ) {
       
   317 		printf(
       
   318 			' <a class="add-new-h2 hide-if-no-customize" href="%1$s">%2$s</a>',
       
   319 			esc_url( add_query_arg(
       
   320 				array(
       
   321 					array( 'autofocus' => array( 'panel' => 'widgets' ) ),
       
   322 					'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) )
       
   323 				),
       
   324 				admin_url( 'customize.php' )
       
   325 			) ),
       
   326 			__( 'Manage in Customizer' )
       
   327 		);
       
   328 	}
       
   329 ?>
       
   330 </h2>
   315 
   331 
   316 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
   332 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
   317 <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
   333 <div id="message" class="updated notice is-dismissible"><p><?php echo $messages[$_GET['message']]; ?></p></div>
   318 <?php } ?>
   334 <?php } ?>
   319 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
   335 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
   320 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
   336 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
   321 <?php } ?>
   337 <?php } ?>
   322 
   338 
   330 
   346 
   331 <div class="widget-liquid-left">
   347 <div class="widget-liquid-left">
   332 <div id="widgets-left">
   348 <div id="widgets-left">
   333 	<div id="available-widgets" class="widgets-holder-wrap">
   349 	<div id="available-widgets" class="widgets-holder-wrap">
   334 		<div class="sidebar-name">
   350 		<div class="sidebar-name">
   335 		<div class="sidebar-name-arrow"><br /></div>
   351 			<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>
   352 			<h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3>
       
   353 		</div>
   337 		<div class="widget-holder">
   354 		<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>
   355 			<div class="sidebar-description">
   339 		<div id="widget-list">
   356 				<p class="description"><?php _e('To activate a widget drag it to a sidebar or click on it. To deactivate a widget and delete its settings, drag it back.'); ?></p>
   340 		<?php wp_list_widgets(); ?>
   357 			</div>
   341 		</div>
   358 			<div id="widget-list">
   342 		<br class='clear' />
   359 				<?php wp_list_widgets(); ?>
       
   360 			</div>
       
   361 			<br class='clear' />
   343 		</div>
   362 		</div>
   344 		<br class="clear" />
   363 		<br class="clear" />
   345 	</div>
   364 	</div>
   346 
   365 
   347 <?php
   366 <?php
       
   367 
       
   368 $theme_sidebars = array();
   348 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
   369 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
   349 	if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
   370 	if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
   350 		$wrap_class = 'widgets-holder-wrap';
   371 		$wrap_class = 'widgets-holder-wrap';
   351 		if ( !empty( $registered_sidebar['class'] ) )
   372 		if ( !empty( $registered_sidebar['class'] ) )
   352 			$wrap_class .= ' ' . $registered_sidebar['class'];
   373 			$wrap_class .= ' ' . $registered_sidebar['class'];
   353 
   374 
   354 ?>
   375 		?>
   355 
       
   356 		<div class="<?php echo esc_attr( $wrap_class ); ?>">
   376 		<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">
   377 			<div class="widget-holder inactive">
   364 				<?php wp_list_widget_controls( $registered_sidebar['id'] ); ?>
   378 				<?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
   365 				<div class="clear"></div>
   379 				<div class="clear"></div>
   366 			</div>
   380 			</div>
   367 		</div>
   381 		</div>
   368 <?php
   382 		<?php
   369 	}
   383 
   370 }
   384 	} else {
       
   385 		$theme_sidebars[$sidebar] = $registered_sidebar;
       
   386 	}
       
   387 }
       
   388 
   371 ?>
   389 ?>
   372 
   390 </div>
   373 </div>
   391 </div>
   374 </div>
   392 <?php
   375 
   393 
       
   394 $i = $split = 0;
       
   395 $single_sidebar_class = '';
       
   396 $sidebars_count = count( $theme_sidebars );
       
   397 
       
   398 if ( $sidebars_count > 1 ) {
       
   399 	$split = ceil( $sidebars_count / 2 );
       
   400 } else {
       
   401 	$single_sidebar_class = ' class="single-sidebar"';
       
   402 }
       
   403 
       
   404 ?>
   376 <div class="widget-liquid-right">
   405 <div class="widget-liquid-right">
   377 <div id="widgets-right">
   406 <div id="widgets-right"<?php echo $single_sidebar_class; ?>>
   378 <?php
   407 <div class="sidebars-column-1">
   379 $i = 0;
   408 <?php
   380 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
   409 
   381 	if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
   410 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) {
   382 		continue;
       
   383 
       
   384 	$wrap_class = 'widgets-holder-wrap';
   411 	$wrap_class = 'widgets-holder-wrap';
   385 	if ( !empty( $registered_sidebar['class'] ) )
   412 	if ( !empty( $registered_sidebar['class'] ) )
   386 		$wrap_class .= ' sidebar-' . $registered_sidebar['class'];
   413 		$wrap_class .= ' sidebar-' . $registered_sidebar['class'];
   387 
   414 
   388 	if ( $i )
   415 	if ( $i > 0 )
   389 		$wrap_class .= ' closed'; ?>
   416 		$wrap_class .= ' closed';
   390 
   417 
       
   418 	if ( $split && $i == $split ) {
       
   419 		?>
       
   420 		</div><div class="sidebars-column-2">
       
   421 		<?php
       
   422 	}
       
   423 
       
   424 	?>
   391 	<div class="<?php echo esc_attr( $wrap_class ); ?>">
   425 	<div class="<?php echo esc_attr( $wrap_class ); ?>">
   392 	<div class="sidebar-name">
   426 		<?php wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); // Show the control forms for each of the widgets in this sidebar ?>
   393 	<div class="sidebar-name-arrow"><br /></div>
   427 	</div>
   394 	<h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
   428 	<?php
   395 	<span class="spinner"></span></h3></div>
   429 
   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++;
   430 	$i++;
   400 } ?>
   431 }
   401 </div>
   432 
   402 </div>
   433 ?>
   403 <form action="" method="post">
   434 </div>
       
   435 </div>
       
   436 </div>
       
   437 <form method="post">
   404 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
   438 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
   405 </form>
   439 </form>
   406 <br class="clear" />
   440 <br class="clear" />
       
   441 </div>
       
   442 
       
   443 <div class="widgets-chooser">
       
   444 	<ul class="widgets-chooser-sidebars"></ul>
       
   445 	<div class="widgets-chooser-actions">
       
   446 		<button class="button-secondary"><?php _e( 'Cancel' ); ?></button>
       
   447 		<button class="button-primary"><?php _e( 'Add Widget' ); ?></button>
       
   448 	</div>
   407 </div>
   449 </div>
   408 
   450 
   409 <?php
   451 <?php
   410 
   452 
   411 /**
   453 /**