Make WordPress Core

Changeset 44897


Ignore:
Timestamp:
03/14/2019 07:30:59 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Widgets: Pre-select currently viewed archive in the Archives dropdown widget, for consistency with the Categories widget.

Props robobot3000, audrasjb, afercia, Themonic.
Fixes #40662.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r44809 r44897  
    16611661 * @since 1.0.0
    16621662 *
    1663  * @param string $url    URL to archive.
    1664  * @param string $text   Archive text description.
    1665  * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
    1666  * @param string $before Optional. Content to prepend to the description. Default empty.
    1667  * @param string $after  Optional. Content to append to the description. Default empty.
     1663 * @param string $url      URL to archive.
     1664 * @param string $text     Archive text description.
     1665 * @param string $format   Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
     1666 * @param string $before   Optional. Content to prepend to the description. Default empty.
     1667 * @param string $after    Optional. Content to append to the description. Default empty.
     1668 * @param bool   $selected Optional. Set to true if the current page is the selected archive page.
    16681669 * @return string HTML link content for archive.
    16691670 */
    1670 function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '' ) {
     1671function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
    16711672    $text = wptexturize( $text );
    16721673    $url  = esc_url( $url );
     
    16751676        $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
    16761677    } elseif ( 'option' == $format ) {
    1677         $link_html = "\t<option value='$url'>$before $text $after</option>\n";
     1678        $selected_attr = $selected ? " selected='selected'" : '';
     1679        $link_html     = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
    16781680    } elseif ( 'html' == $format ) {
    16791681        $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n";
     
    16871689     * @since 2.6.0
    16881690     * @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters.
     1691     * @since 5.2.0 Added the `$selected` parameter.
    16891692     *
    16901693     * @param string $link_html The archive HTML link content.
     
    16941697     * @param string $before    Content to prepend to the description.
    16951698     * @param string $after     Content to append to the description.
    1696      */
    1697     return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after );
     1699     * @param bool   $selected  True if the current page is the selected archive.
     1700     */
     1701    return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
    16981702}
    16991703
     
    17021706 *
    17031707 * @since 1.2.0
    1704  * @since 4.4.0 $post_type arg was added.
     1708 * @since 4.4.0 The `$post_type` argument was added.
     1709 * @since 5.2.0 The `$year`, `$monthnum`, `$day`, and `$w` arguments were added.
    17051710 *
    17061711 * @see get_archives_link()
     
    17301735 *                                       Default 'DESC'.
    17311736 *     @type string     $post_type       Post type. Default 'post'.
     1737 *     @type string     $year            Year. Default current year.
     1738 *     @type string     $monthnum        Month number. Default current month number.
     1739 *     @type string     $day             Day. Default current day.
     1740 *     @type string     $w               Week. Default current week.
    17321741 * }
    17331742 * @return string|void String when retrieving.
     
    17461755        'order'           => 'DESC',
    17471756        'post_type'       => 'post',
     1757        'year'            => get_query_var( 'year' ),
     1758        'monthnum'        => get_query_var( 'monthnum' ),
     1759        'day'             => get_query_var( 'day' ),
     1760        'w'               => get_query_var( 'w' ),
    17481761    );
    17491762
     
    18211834                    $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    18221835                }
    1823                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1836                $selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month;
     1837                $output  .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18241838            }
    18251839        }
     
    18431857                    $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    18441858                }
    1845                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1859                $selected = is_archive() && (string) $r['year'] === $result->year;
     1860                $output  .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18461861            }
    18471862        }
     
    18661881                    $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    18671882                }
    1868                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1883                $selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month && (string) $r['day'] === $result->dayofmonth;
     1884                $output  .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18691885            }
    18701886        }
     
    19021918                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    19031919                    }
    1904                     $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1920                    $selected = is_archive() && (string) $r['year'] === $result->yr && (string) $r['w'] === $result->week;
     1921                    $output  .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    19051922                }
    19061923            }
     
    19251942                        $text = $result->ID;
    19261943                    }
    1927                     $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1944                    $selected = $result->ID === get_the_ID();
     1945                    $output  .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    19281946                }
    19291947            }
  • trunk/src/wp-includes/widgets/class-wp-widget-archives.php

    r44589 r44897  
    5959            ?>
    6060        <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
    61         <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
     61        <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
    6262            <?php
    6363            /**
     
    105105
    106106        </select>
     107
     108<script type='text/javascript'>
     109/* <![CDATA[ */
     110(function() {
     111    var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
     112    function onSelectChange() {
     113        if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
     114            document.location.href = this.options[ this.selectedIndex ].value;
     115        }
     116    }
     117    dropdown.onchange = onSelectChange;
     118})();
     119/* ]]> */
     120</script>
     121
    107122        <?php } else { ?>
    108123        <ul>
Note: See TracChangeset for help on using the changeset viewer.