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

Props robobot3000, audrasjb, afercia, Themonic.
Fixes #40662.
Built from https://develop.svn.wordpress.org/trunk@44897


git-svn-id: http://core.svn.wordpress.org/trunk@44728 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-03-14 19:31:49 +00:00
parent 72979b167d
commit bba488f2b7
3 changed files with 49 additions and 16 deletions

View File

@ -1660,21 +1660,23 @@ function get_the_post_type_description() {
* *
* @since 1.0.0 * @since 1.0.0
* *
* @param string $url URL to archive. * @param string $url URL to archive.
* @param string $text Archive text description. * @param string $text Archive text description.
* @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom. * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
* @param string $before Optional. Content to prepend to the description. Default empty. * @param string $before Optional. Content to prepend to the description. Default empty.
* @param string $after Optional. Content to append to the description. Default empty. * @param string $after Optional. Content to append to the description. Default empty.
* @param bool $selected Optional. Set to true if the current page is the selected archive page.
* @return string HTML link content for archive. * @return string HTML link content for archive.
*/ */
function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '' ) { function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
$text = wptexturize( $text ); $text = wptexturize( $text );
$url = esc_url( $url ); $url = esc_url( $url );
if ( 'link' == $format ) { if ( 'link' == $format ) {
$link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
} elseif ( 'option' == $format ) { } elseif ( 'option' == $format ) {
$link_html = "\t<option value='$url'>$before $text $after</option>\n"; $selected_attr = $selected ? " selected='selected'" : '';
$link_html = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
} elseif ( 'html' == $format ) { } elseif ( 'html' == $format ) {
$link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n";
} else { // custom } else { // custom
@ -1686,6 +1688,7 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
* *
* @since 2.6.0 * @since 2.6.0
* @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters. * @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters.
* @since 5.2.0 Added the `$selected` parameter.
* *
* @param string $link_html The archive HTML link content. * @param string $link_html The archive HTML link content.
* @param string $url URL to archive. * @param string $url URL to archive.
@ -1693,15 +1696,17 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
* @param string $format Link format. Can be 'link', 'option', 'html', or custom. * @param string $format Link format. Can be 'link', 'option', 'html', or custom.
* @param string $before Content to prepend to the description. * @param string $before Content to prepend to the description.
* @param string $after Content to append to the description. * @param string $after Content to append to the description.
* @param bool $selected True if the current page is the selected archive.
*/ */
return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after ); return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
} }
/** /**
* Display archive links based on type and format. * Display archive links based on type and format.
* *
* @since 1.2.0 * @since 1.2.0
* @since 4.4.0 $post_type arg was added. * @since 4.4.0 The `$post_type` argument was added.
* @since 5.2.0 The `$year`, `$monthnum`, `$day`, and `$w` arguments were added.
* *
* @see get_archives_link() * @see get_archives_link()
* *
@ -1729,6 +1734,10 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
* @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'. * @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.
* Default 'DESC'. * Default 'DESC'.
* @type string $post_type Post type. Default 'post'. * @type string $post_type Post type. Default 'post'.
* @type string $year Year. Default current year.
* @type string $monthnum Month number. Default current month number.
* @type string $day Day. Default current day.
* @type string $w Week. Default current week.
* } * }
* @return string|void String when retrieving. * @return string|void String when retrieving.
*/ */
@ -1745,6 +1754,10 @@ function wp_get_archives( $args = '' ) {
'echo' => 1, 'echo' => 1,
'order' => 'DESC', 'order' => 'DESC',
'post_type' => 'post', 'post_type' => 'post',
'year' => get_query_var( 'year' ),
'monthnum' => get_query_var( 'monthnum' ),
'day' => get_query_var( 'day' ),
'w' => get_query_var( 'w' ),
); );
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
@ -1820,7 +1833,8 @@ function wp_get_archives( $args = '' ) {
if ( $r['show_post_count'] ) { if ( $r['show_post_count'] ) {
$r['after'] = '&nbsp;(' . $result->posts . ')' . $after; $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
} }
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); $selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month;
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
} }
} }
} elseif ( 'yearly' == $r['type'] ) { } elseif ( 'yearly' == $r['type'] ) {
@ -1842,7 +1856,8 @@ function wp_get_archives( $args = '' ) {
if ( $r['show_post_count'] ) { if ( $r['show_post_count'] ) {
$r['after'] = '&nbsp;(' . $result->posts . ')' . $after; $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
} }
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); $selected = is_archive() && (string) $r['year'] === $result->year;
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
} }
} }
} elseif ( 'daily' == $r['type'] ) { } elseif ( 'daily' == $r['type'] ) {
@ -1865,7 +1880,8 @@ function wp_get_archives( $args = '' ) {
if ( $r['show_post_count'] ) { if ( $r['show_post_count'] ) {
$r['after'] = '&nbsp;(' . $result->posts . ')' . $after; $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
} }
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); $selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month && (string) $r['day'] === $result->dayofmonth;
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
} }
} }
} elseif ( 'weekly' == $r['type'] ) { } elseif ( 'weekly' == $r['type'] ) {
@ -1901,7 +1917,8 @@ function wp_get_archives( $args = '' ) {
if ( $r['show_post_count'] ) { if ( $r['show_post_count'] ) {
$r['after'] = '&nbsp;(' . $result->posts . ')' . $after; $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
} }
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); $selected = is_archive() && (string) $r['year'] === $result->yr && (string) $r['w'] === $result->week;
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
} }
} }
} }
@ -1924,7 +1941,8 @@ function wp_get_archives( $args = '' ) {
} else { } else {
$text = $result->ID; $text = $result->ID;
} }
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); $selected = $result->ID === get_the_ID();
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
} }
} }
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.2-alpha-44896'; $wp_version = '5.2-alpha-44897';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -58,7 +58,7 @@ class WP_Widget_Archives extends WP_Widget {
$dropdown_id = "{$this->id_base}-dropdown-{$this->number}"; $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
?> ?>
<label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label> <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
<select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
<?php <?php
/** /**
* Filters the arguments for the Archives widget drop-down. * Filters the arguments for the Archives widget drop-down.
@ -104,6 +104,21 @@ class WP_Widget_Archives extends WP_Widget {
<?php wp_get_archives( $dropdown_args ); ?> <?php wp_get_archives( $dropdown_args ); ?>
</select> </select>
<script type='text/javascript'>
/* <![CDATA[ */
(function() {
var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
function onSelectChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
document.location.href = this.options[ this.selectedIndex ].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
<?php } else { ?> <?php } else { ?>
<ul> <ul>
<?php <?php