Make WordPress Core

Ticket #23328: 23328.2.diff

File 23328.2.diff, 5.9 KB (added by Mte90, 7 years ago)
  • 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

     
    181181        $sidebars_widgets[$sidebar_id] = $sidebar;
    182182
    183183        // 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 );
    185205                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 ) );
    188208                }
    189                 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
     209                array_splice( $sidebars_widgets[ $sidebar_id ], $position, 0, $widget_id );
    190210        }
    191211
    192212        wp_set_sidebars_widgets($sidebars_widgets);
     
    259279        $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
    260280
    261281        // Show the widget form.
    262         $width = ' style="width:' . max($control['width'], 350) . 'px"';
    263282        $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
    264283
    265284        require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
    266285        <div class="wrap">
    267286        <h1><?php echo esc_html( $title ); ?></h1>
    268         <div class="editwidget"<?php echo $width; ?>>
     287        <div class="editwidget">
    269288        <h2><?php printf( __( 'Widget %s' ), $name ); ?></h2>
    270289
    271290        <form action="widgets.php" method="post">
     
    279298
    280299        <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
    281300        <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>
    283302<?php
    284303        foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
    285304                echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
     
    286305                if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
    287306                        echo '&nbsp;';
    288307                } else {
    289                         if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
     308                        if ( ! isset( $sidebars_widgets[ $sbname ] ) || ! is_array( $sidebars_widgets[ $sbname ] ) ) {
    290309                                $j = 1;
    291                                 $sidebars_widgets[$sbname] = array();
     310                                $sidebars_widgets[ $sbname ] = array();
    292311                        } 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 ) ) {
    295315                                        $j++;
     316                                }
    296317                        }
    297318                        $selected = '';
     319                        $index = $widget_new_position = 0;
    298320                        echo "\t\t<select name='{$sbname}_position'>\n";
    299321                        echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</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                         */
    300326                        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++;
    304348                        }
    305349                        echo "\t\t</select>\n";
    306350                }