Make WordPress Core

Ticket #40662: 40662.diff

File 40662.diff, 5.2 KB (added by robobot3000, 6 years ago)

Added preselecting of the currently opened archive page in the archives dropdown widgets #wcpl2017

  • wp-includes/general-template.php

     
    16041604 *
    16051605 * @since 1.0.0
    16061606 *
    1607  * @param string $url    URL to archive.
    1608  * @param string $text   Archive text description.
    1609  * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
    1610  * @param string $before Optional. Content to prepend to the description. Default empty.
    1611  * @param string $after  Optional. Content to append to the description. Default empty.
     1607 * @param string $url      URL to archive.
     1608 * @param string $text     Archive text description.
     1609 * @param string $format   Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
     1610 * @param string $before   Optional. Content to prepend to the description. Default empty.
     1611 * @param string $after    Optional. Content to append to the description. Default empty.
     1612 * @param bool   $selected Optional. Set to true if the current page is the selected archive page
    16121613 * @return string HTML link content for archive.
    16131614 */
    1614 function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
     1615function get_archives_link($url, $text, $format = 'html', $before = '', $after = '', $selected = false) {
    16151616        $text = wptexturize($text);
    16161617        $url = esc_url($url);
    16171618
    16181619        if ('link' == $format)
    16191620                $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
    1620         elseif ('option' == $format)
    1621                 $link_html = "\t<option value='$url'>$before $text $after</option>\n";
    1622         elseif ('html' == $format)
     1621        elseif ('option' == $format) {
     1622                $selected_attr = $selected ? " selected='selected'" : '';
     1623                $link_html = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
     1624        } elseif ('html' == $format)
    16231625                $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n";
    16241626        else // custom
    16251627                $link_html = "\t$before<a href='$url'>$text</a>$after\n";
     
    16361638         * @param string $format    Link format. Can be 'link', 'option', 'html', or custom.
    16371639         * @param string $before    Content to prepend to the description.
    16381640         * @param string $after     Content to append to the description.
     1641         * @param bool   $selected  True if the current page is the selected archive
    16391642         */
    1640         return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after );
     1643        return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
    16411644}
    16421645
    16431646/**
     
    16831686                'format' => 'html', 'before' => '',
    16841687                'after' => '', 'show_post_count' => false,
    16851688                'echo' => 1, 'order' => 'DESC',
    1686                 'post_type' => 'post'
     1689                'post_type' => 'post',
     1690                'year' => get_query_var( 'year' ),
     1691                'monthnum' => get_query_var( 'monthnum' ),
     1692                'day' => get_query_var( 'day' ),
     1693                'week' => get_query_var( 'w' )
    16871694        );
    16881695
    16891696        $r = wp_parse_args( $args, $defaults );
     
    17591766                                if ( $r['show_post_count'] ) {
    17601767                                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    17611768                                }
    1762                                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1769                                $selected = is_archive() && ( string ) $r[ 'year' ] === $result->year
     1770                                                        && ( string ) $r[ 'monthnum' ] === $result->month;
     1771                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    17631772                        }
    17641773                }
    17651774        } elseif ( 'yearly' == $r['type'] ) {
     
    17811790                                if ( $r['show_post_count'] ) {
    17821791                                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    17831792                                }
    1784                                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1793                                $selected = is_archive() && ( string ) $r[ 'year' ] === $result->year;
     1794                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    17851795                        }
    17861796                }
    17871797        } elseif ( 'daily' == $r['type'] ) {
     
    18041814                                if ( $r['show_post_count'] ) {
    18051815                                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    18061816                                }
    1807                                 $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1817                                $selected = is_archive() && ( string ) $r[ 'year' ] === $result->year
     1818                                                        && ( string ) $r[ 'monthnum' ] === $result->month
     1819                                                        && ( string ) $r[ 'day' ] === $result->dayofmonth;
     1820                                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18081821                        }
    18091822                }
    18101823        } elseif ( 'weekly' == $r['type'] ) {
     
    18341847                                        if ( $r['show_post_count'] ) {
    18351848                                                $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
    18361849                                        }
    1837                                         $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1850                                        $selected = is_archive() && ( string ) $r[ 'year' ] === $result->yr
     1851                                                                && ( string ) $r[ 'week' ] === $result->week;
     1852                                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18381853                                }
    18391854                        }
    18401855                }
     
    18571872                                        } else {
    18581873                                                $text = $result->ID;
    18591874                                        }
    1860                                         $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
     1875                                        $selected = $result->ID === get_the_ID();
     1876                                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
    18611877                                }
    18621878                        }
    18631879                }