Make WordPress Core

Ticket #23328: 23328.3.patch

File 23328.3.patch, 5.7 KB (added by Mte90, 7 years ago)

fix also #36772

  • src/wp-admin/css/widgets.css

     
    332332}
    333333
    334334.editwidget {
    335         margin: 0 auto;
     335        max-width: 40em; /* about 80 characters, for readability */
    336336}
    337337
     338.editwidget select {
     339    width: 100%;
     340}
     341
    338342.editwidget .widget-inside {
    339343        display: block;
    340344        padding: 0 15px;
  • src/wp-admin/widgets.php

     
    182182
    183183        // Remove old position.
    184184        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                        $widget_base_id = explode('-', $sb);
     192                        $widget_base_id = $widget_base_id[0];
     193                        // We have found that widget?
     194                        if( $widget_base_id === $class->id_base ) {
     195                            $notfound = true;
     196                        }
     197                    }
     198                    // Inc ase not found we remove from the db
     199                    if( false === $notfound ) {
     200                        unset($sidebars_widgets[$_POST['sidebar']][$key]);
     201                    }
     202                }
     203                array_values($sidebars_widgets);
    185204                foreach ( $sidebars_widgets as $key => $sb ) {
    186205                        if ( is_array($sb) )
    187206                                $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
     
    259278        $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
    260279
    261280        // Show the widget form.
    262         $width = ' style="width:' . max($control['width'], 350) . 'px"';
    263281        $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
    264282
    265283        require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
    266284        <div class="wrap">
    267285        <h1><?php echo esc_html( $title ); ?></h1>
    268         <div class="editwidget"<?php echo $width; ?>>
     286        <div class="editwidget">
    269287        <h2><?php printf( __( 'Widget %s' ), $name ); ?></h2>
    270288
    271289        <form action="widgets.php" method="post">
     
    279297
    280298        <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
    281299        <div class="widget-position">
    282         <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
     300        <table class="widefat"><thead><tr><th><?php _e( 'Sidebar' ); ?></th><th><?php _e( 'Widget position' ); ?></th></tr></thead><tbody>
    283301<?php
    284302        foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
    285303                echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
     
    291309                                $sidebars_widgets[$sbname] = array();
    292310                        } else {
    293311                                $j = count($sidebars_widgets[$sbname]);
    294                                 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
     312                                // When adding a new widget or the edited widget is not in this sidebar, add one more cycle to the loop below.
     313                                if ( isset( $_GET['addnew'] ) || ! in_array( $widget_id, $sidebars_widgets[$sbname], true ) ) {
    295314                                        $j++;
     315                                }
    296316                        }
    297317                        $selected = '';
     318                        $index = $widget_new_position = 0;
    298319                        echo "\t\t<select name='{$sbname}_position'>\n";
    299320                        echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
     321                        /*
     322                         * When adding a new widget or the edited widget is not in this sidebar, this loop runs
     323                         * one cycle more than the actual widgets count to build an empty option in the select.
     324                         */
    300325                        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";
     326                                // Get the ID of the widget in the loop. Set it to false when running the additional loop cycle.
     327                                $sidebar_widget_id = isset( $sidebars_widgets[$sbname][$index] ) ? $sidebars_widgets[$sbname][$index] : false;
     328                                // If the widget is not avalaible o not exist add an available position option
     329                                if( empty( $sidebar_widget_id ) || isset( $wp_registered_widgets[$sidebar_widget_id]['name'] ) ) {
     330                                    $widget_new_position++;
     331                                    // If the widget ID is set then get the widget name otherwise set a name for the empty option.
     332                                    $select_option_content = $sidebar_widget_id ? sprintf( __( '(currently set to: %s)' ), $wp_registered_widgets[$sidebar_widget_id]['name'] ) : esc_html_x( '(available position)', 'widget' );
     333
     334                                    if ( in_array( $widget_id, $sidebars_widgets[$sbname], true ) ) {
     335                                            $selected = selected( $i, $key + 1, false );
     336                                    }
     337                                   
     338                                    echo "\t\t<option value='$widget_new_position'$selected>$widget_new_position $select_option_content</option>\n";
     339                                } else {
     340                                    // Add a new option because this code is executed only when the widget is removed from the check above
     341                                    $j++;
     342                                }
     343                                $index++;
    304344                        }
    305345                        echo "\t\t</select>\n";
    306346                }