From bba488f2b7fc241b03063fefc58a1db479e977d6 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 14 Mar 2019 19:31:49 +0000 Subject: [PATCH] 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 --- wp-includes/general-template.php | 46 +++++++++++++------ wp-includes/version.php | 2 +- .../widgets/class-wp-widget-archives.php | 17 ++++++- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index f63bafb406..7f9b96ef59 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -1660,21 +1660,23 @@ function get_the_post_type_description() { * * @since 1.0.0 * - * @param string $url URL to archive. - * @param string $text Archive text description. - * @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 $after Optional. Content to append to the description. Default empty. + * @param string $url URL to archive. + * @param string $text Archive text description. + * @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 $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. */ -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 ); $url = esc_url( $url ); if ( 'link' == $format ) { $link_html = "\t\n"; } elseif ( 'option' == $format ) { - $link_html = "\t\n"; + $selected_attr = $selected ? " selected='selected'" : ''; + $link_html = "\t\n"; } elseif ( 'html' == $format ) { $link_html = "\t
  • $before$text$after
  • \n"; } else { // custom @@ -1686,6 +1688,7 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after * * @since 2.6.0 * @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 $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 $before Content to prepend 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. * * @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() * @@ -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'. * Default 'DESC'. * @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. */ @@ -1745,6 +1754,10 @@ function wp_get_archives( $args = '' ) { 'echo' => 1, 'order' => 'DESC', '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 ); @@ -1820,7 +1833,8 @@ function wp_get_archives( $args = '' ) { if ( $r['show_post_count'] ) { $r['after'] = ' (' . $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'] ) { @@ -1842,7 +1856,8 @@ function wp_get_archives( $args = '' ) { if ( $r['show_post_count'] ) { $r['after'] = ' (' . $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'] ) { @@ -1865,7 +1880,8 @@ function wp_get_archives( $args = '' ) { if ( $r['show_post_count'] ) { $r['after'] = ' (' . $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'] ) { @@ -1901,7 +1917,8 @@ function wp_get_archives( $args = '' ) { if ( $r['show_post_count'] ) { $r['after'] = ' (' . $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 { $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 ); } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 27b78f841a..26b583ec94 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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. diff --git a/wp-includes/widgets/class-wp-widget-archives.php b/wp-includes/widgets/class-wp-widget-archives.php index ab6e0d9ab2..db3a91d654 100644 --- a/wp-includes/widgets/class-wp-widget-archives.php +++ b/wp-includes/widgets/class-wp-widget-archives.php @@ -58,7 +58,7 @@ class WP_Widget_Archives extends WP_Widget { $dropdown_id = "{$this->id_base}-dropdown-{$this->number}"; ?> - + + +