Ticket #23328: 23328.2.diff
File 23328.2.diff, 5.9 KB (added by , 7 years ago) |
---|
-
src/wp-admin/css/widgets.css
332 332 } 333 333 334 334 .editwidget { 335 ma rgin: 0 auto;335 max-width: 40em; /* about 80 characters, for readability */ 336 336 } 337 337 338 .editwidget select { 339 width: 100%; 340 } 341 338 342 .editwidget .widget-inside { 339 343 display: block; 340 344 padding: 0 15px; -
src/wp-admin/widgets.php
181 181 $sidebars_widgets[$sidebar_id] = $sidebar; 182 182 183 183 // Remove old position. 184 if ( !isset($_POST['delete_widget']) ) { 184 if ( ! isset( $_POST['delete_widget'] ) ) { 185 // Detect if the widget is registered, in case remove from the sidebar in editing. 186 global $wp_widget_factory; 187 foreach ( $sidebars_widgets[ $_POST['sidebar'] ] as $key => $sb ) { 188 $notfound = false; 189 foreach ( $wp_widget_factory->widgets as $widget_obj => $class ) { 190 // Get the base id of the widget. 191 $sb_pieces = explode( '-', $sb ); 192 array_pop( $sb_pieces ); 193 $widget_base_id = implode( '-', $sb_pieces ); 194 // Have we found that widget? 195 if ( $widget_base_id === $class->id_base ) { 196 $notfound = true; 197 } 198 } 199 // In case the widget is not found, we remove it from the db. 200 if ( false === $notfound ) { 201 unset( $sidebars_widgets[ $_POST['sidebar'] ][ $key ] ); 202 } 203 } 204 array_values( $sidebars_widgets ); 185 205 foreach ( $sidebars_widgets as $key => $sb ) { 186 if ( is_array( $sb) )187 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );206 if ( is_array( $sb ) ) 207 $sidebars_widgets[ $key ] = array_diff( $sb, array( $widget_id ) ); 188 208 } 189 array_splice( $sidebars_widgets[ $sidebar_id], $position, 0, $widget_id );209 array_splice( $sidebars_widgets[ $sidebar_id ], $position, 0, $widget_id ); 190 210 } 191 211 192 212 wp_set_sidebars_widgets($sidebars_widgets); … … 259 279 $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id']; 260 280 261 281 // Show the widget form. 262 $width = ' style="width:' . max($control['width'], 350) . 'px"';263 282 $key = isset($_GET['key']) ? (int) $_GET['key'] : 0; 264 283 265 284 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?> 266 285 <div class="wrap"> 267 286 <h1><?php echo esc_html( $title ); ?></h1> 268 <div class="editwidget" <?php echo $width; ?>>287 <div class="editwidget"> 269 288 <h2><?php printf( __( 'Widget %s' ), $name ); ?></h2> 270 289 271 290 <form action="widgets.php" method="post"> … … 279 298 280 299 <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p> 281 300 <div class="widget-position"> 282 <table class="widefat"><thead><tr><th><?php _e( 'Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>301 <table class="widefat"><thead><tr><th><?php _e( 'Sidebar' ); ?></th><th><?php _e( 'Widget position' ); ?></th></tr></thead><tbody> 283 302 <?php 284 303 foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) { 285 304 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>"; … … 286 305 if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) { 287 306 echo ' '; 288 307 } else { 289 if ( ! isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {308 if ( ! isset( $sidebars_widgets[ $sbname ] ) || ! is_array( $sidebars_widgets[ $sbname ] ) ) { 290 309 $j = 1; 291 $sidebars_widgets[ $sbname] = array();310 $sidebars_widgets[ $sbname ] = array(); 292 311 } else { 293 $j = count($sidebars_widgets[$sbname]); 294 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) ) 312 $j = count( $sidebars_widgets[ $sbname ] ); 313 // When adding a new widget or the edited widget is not in this sidebar, add one more cycle to the loop below. 314 if ( isset( $_GET['addnew'] ) || ! in_array( $widget_id, $sidebars_widgets[ $sbname ], true ) ) { 295 315 $j++; 316 } 296 317 } 297 318 $selected = ''; 319 $index = $widget_new_position = 0; 298 320 echo "\t\t<select name='{$sbname}_position'>\n"; 299 321 echo "\t\t<option value=''>" . __('— Select —') . "</option>\n"; 322 /* 323 * When adding a new widget or the edited widget is not in this sidebar, this loop runs 324 * one cycle more than the actual widgets count to build an empty option in the select. 325 */ 300 326 for ( $i = 1; $i <= $j; $i++ ) { 301 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) ) 302 $selected = selected( $i, $key + 1, false ); 303 echo "\t\t<option value='$i'$selected> $i </option>\n"; 327 // Get the ID of the widget in the loop. Set it to false when running the additional loop cycle. 328 $sidebar_widget_id = isset( $sidebars_widgets[ $sbname ][ $index ] ) ? $sidebars_widgets[ $sbname ][ $index ] : false; 329 if( empty( $sidebar_widget_id ) && $i !== $j) { 330 continue; 331 } 332 // If the widget is not avalaible o not exist add an available position option 333 if ( empty( $sidebar_widget_id ) || isset( $wp_registered_widgets[ $sidebar_widget_id ]['name'] ) ) { 334 $widget_new_position++; 335 // If the widget ID is set then get the widget name otherwise set a name for the empty option. 336 $select_option_content = $sidebar_widget_id ? sprintf( __( '(currently set to: %s)' ), $wp_registered_widgets[ $sidebar_widget_id ]['name'] ) : esc_html_x( '(available position)', 'widget' ); 337 338 if ( in_array( $widget_id, $sidebars_widgets[ $sbname ], true ) ) { 339 $selected = selected( $i, $key + 1, false ); 340 } 341 342 echo "\t\t<option value='$widget_new_position'$selected>$widget_new_position $select_option_content</option>\n"; 343 } else { 344 // Add a new option because this code is executed only when the widget is removed from the check above 345 $j++; 346 } 347 $index++; 304 348 } 305 349 echo "\t\t</select>\n"; 306 350 }