Plugin Directory

Changeset 2508747


Ignore:
Timestamp:
04/03/2021 07:08:46 PM (5 years ago)
Author:
petroffm
Message:

Add support for collapsing decades.

Location:
collapsed-archives/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • collapsed-archives/trunk/collapsed-archives.php

    r2080908 r2508747  
    44Plugin URI: https://github.com/mpetroff/collapsed-archives-wp
    55Description: Adds a widget to display archive links using purely CSS-based collapsing.
    6 Version: 1.5
     6Version: 1.6
    77Author: Matthew Petroff
    88Author URI: https://mpetroff.net/
     
    1919 *     @type bool       $show_post_count Whether to display the post count alongside the link. Default false.
    2020 *     @type bool       $use_triangles   Whether to use triangles to indicate expansion instead of +/-. Default false.
     21 *     @type bool       $use_decades     Whether to collapse decades as well. Default false.
    2122 *     @type bool       $never_expand    Whether to never expand for date (normally expands for current post). Default false.
    2223 *     @type string     $order           Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.
     
    3132        'show_post_count' => false,
    3233        'use_triangles' => false,
     34        'use_decades' => false,
    3335        'never_expand' => false,
    3436        'order' => 'DESC',
     
    5355    }
    5456
    55     $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order";
     57    $query = "SELECT FLOOR(YEAR(post_date)/10)*10 AS `decade`, YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY FLOOR(YEAR(post_date)/10)*10, YEAR(post_date), MONTH(post_date) ORDER BY post_date $order";
    5658    $key = md5( $query );
    5759    $key = "wp_get_archives:$key:$last_changed";
     
    6668        }
    6769        $output .= '"><ul>';
    68        
     70
     71        $prev_decade = false;
    6972        $prev_year = false;
    7073        foreach ( (array) $results as $result ) {
     74            if ( $r['use_decades'] && $prev_decade != $result->decade ) {
     75                if ( $prev_decade !== false ) {
     76                    $output .= '</ul></li></ul></li>';
     77                    $prev_year = false; // this is so that the year output does not append extra </ul></li> below
     78                }
     79
     80                $decade_id = 'archive-decade-' . $result->decade;
     81
     82                $output .= '<li>';
     83                $output .= '<input type="checkbox" id="' . $decade_id . '"';
     84                if ( isset( $wp_query->post->ID ) ) {
     85                    if ( !$r['never_expand'] && ( ( $result->decade == floor(get_the_date('Y', $wp_query->post->ID)/10)*10 && !is_page() ) || ( is_page() && $result->decade == floor(date('Y')/10)*10 ) ) ) {
     86                        $output .= ' checked';
     87                    }
     88                }
     89                $output .= '>';
     90                $output .= '<label for="' . $decade_id . '"></label>';
     91
     92                $after = '';
     93                if ( $r['show_post_count'] ) {
     94                    $decade_args = array(
     95                        'date_query' => array(
     96                            array(
     97                                'after'     => array(
     98                                    'year'  => $result->decade,
     99                                    'month' => 1,
     100                                    'day'   => 1,
     101                                ),
     102                                'before'    => array(
     103                                    'year'  => $result->decade + 9,
     104                                    'month' => 12,
     105                                    'day'   => 31,
     106                                ),
     107                                'inclusive' => true,
     108                            ),
     109                        ),
     110                        'posts_per_page' => -1,
     111                    );
     112                    $decade_query = new WP_Query( $decade_args );
     113                    $after = '&nbsp;(' . $decade_query->found_posts . ')';
     114                }
     115                $output .= $result->decade . 's' . $after;
     116
     117                $output .= '<ul class="decade">';
     118
     119                $prev_decade = $result->decade;
     120            }
     121
    71122            if ( $prev_year != $result->year ) {
    72123                if ( $prev_year !== false ) {
    73124                    $output .= '</ul></li>';
    74125                }
    75                
     126
    76127                $year_id = 'archive-year-' . $result->year;
    77                
     128
    78129                $output .= '<li>';
    79130                $output .= '<input type="checkbox" id="' . $year_id . '"';
     
    85136                $output .= '>';
    86137                $output .= '<label for="' . $year_id . '"></label>';
    87                
     138
    88139                $url = get_year_link( $result->year );
    89140                $after = '';
     
    93144                }
    94145                $output .= get_archives_link( $url, $result->year, '', '', $after );
    95                
    96                 $output .= '<ul>';
    97                
     146
     147                $output .= '<ul class="year">';
     148
    98149                $prev_year = $result->year;
    99150            }
    100            
     151
    101152            $url = get_month_link( $result->year, $result->month );
    102153            /* translators: 1: month name */
     
    108159            $output .= get_archives_link( $url, $text, 'html', '', $after );
    109160        }
     161        if ( $r['use_decades'] ) {
     162            $output .= '</ul></li>';
     163        }
    110164        $output .= '</ul></li></ul></div>';
    111165    }
    112    
     166
    113167    return $output;
    114168} // function get_collapsed_archives
     
    128182
    129183    public function widget( $args, $instance ) {
    130    
     184
    131185        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Archives' ) : $instance['title'], $instance, $this->id_base );
    132186        $count = ! empty( $instance['count'] ) ? '1' : '0';
    133187        $use_triangles = ! empty( $instance['use_triangles'] ) ? '1' : '0';
     188        $use_decades = ! empty( $instance['use_decades'] ) ? '1' : '0';
    134189        $never_expand = ! empty( $instance['never_expand'] ) ? '1' : '0';
    135190        $order = ! empty( $instance['asc_order'] ) ? 'ASC' : 'DESC';
    136        
     191
    137192        echo $args['before_widget'];
    138193        if ( $title ) {
    139194            echo $args['before_title'] . $title . $args['after_title'];
    140195        }
    141         echo collapsed_archives_get_collapsed_archives( array( 'show_post_count' => $count, 'use_triangles' => $use_triangles, 'never_expand' => $never_expand, 'order' => $order ) );
     196        echo collapsed_archives_get_collapsed_archives( array( 'show_post_count' => $count, 'use_triangles' => $use_triangles, 'use_decades' => $use_decades, 'never_expand' => $never_expand, 'order' => $order ) );
    142197        echo $args['after_widget'];
    143198    }
     
    148203        $count = $instance['count'] ? 'checked="checked"' : '';
    149204        $use_triangles = $instance['use_triangles'] ? 'checked="checked"' : '';
     205        $use_decades = $instance['use_decades'] ? 'checked="checked"' : '';
    150206        $never_expand = $instance['never_expand'] ? 'checked="checked"' : '';
    151207        $asc_order = $instance['asc_order'] ? 'checked="checked"' : '';
    152208        ?>
    153209        <p>
    154         <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
     210            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
    155211        </p>
    156212        <p>
    157         <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
    158         <br/>
    159         <input class="checkbox" type="checkbox" <?php echo $use_triangles; ?> id="<?php echo $this->get_field_id('use_triangles'); ?>" name="<?php echo $this->get_field_name('use_triangles'); ?>" /> <label for="<?php echo $this->get_field_id('use_triangles'); ?>"><?php _e('Use triangles to indicate expansion instead of +/-.'); ?></label>
    160         <br/>
    161         <input class="checkbox" type="checkbox" <?php echo $never_expand; ?> id="<?php echo $this->get_field_id('never_expand'); ?>" name="<?php echo $this->get_field_name('never_expand'); ?>" /> <label for="<?php echo $this->get_field_id('never_expand'); ?>"><?php _e('Don\'t expand list for current post / year.'); ?></label>
    162         <br/>
    163         <input class="checkbox" type="checkbox" <?php echo $asc_order; ?> id="<?php echo $this->get_field_id('asc_order'); ?>" name="<?php echo $this->get_field_name('asc_order'); ?>" /> <label for="<?php echo $this->get_field_id('asc_order'); ?>"><?php _e('Display archives in chronological order instead of reverse chronological order.'); ?></label>
     213            <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
     214            <br/>
     215            <input class="checkbox" type="checkbox" <?php echo $use_triangles; ?> id="<?php echo $this->get_field_id('use_triangles'); ?>" name="<?php echo $this->get_field_name('use_triangles'); ?>" /> <label for="<?php echo $this->get_field_id('use_triangles'); ?>"><?php _e('Use triangles to indicate expansion instead of +/-.'); ?></label>
     216            <br/>
     217            <input class="checkbox" type="checkbox" <?php echo $use_decades; ?> id="<?php echo $this->get_field_id('use_decades'); ?>" name="<?php echo $this->get_field_name('use_decades'); ?>" /> <label for="<?php echo $this->get_field_id('use_decades'); ?>"><?php _e('Collapse decades.'); ?></label>
     218            <br/>
     219            <input class="checkbox" type="checkbox" <?php echo $never_expand; ?> id="<?php echo $this->get_field_id('never_expand'); ?>" name="<?php echo $this->get_field_name('never_expand'); ?>" /> <label for="<?php echo $this->get_field_id('never_expand'); ?>"><?php _e('Don\'t expand list for current post / year.'); ?></label>
     220            <br/>
     221            <input class="checkbox" type="checkbox" <?php echo $asc_order; ?> id="<?php echo $this->get_field_id('asc_order'); ?>" name="<?php echo $this->get_field_name('asc_order'); ?>" /> <label for="<?php echo $this->get_field_id('asc_order'); ?>"><?php _e('Display archives in chronological order instead of reverse chronological order.'); ?></label>
    164222        </p>
    165         <?php 
     223        <?php
    166224    }
    167225
     
    172230        $instance['count'] = $new_instance['count'] ? 1 : 0;
    173231        $instance['use_triangles'] = $new_instance['use_triangles'] ? 1 : 0;
     232        $instance['use_decades'] = $new_instance['use_decades'] ? 1 : 0;
    174233        $instance['never_expand'] = $new_instance['never_expand'] ? 1 : 0;
    175234        $instance['asc_order'] = $new_instance['asc_order'] ? 1 : 0;
    176        
     235
    177236        return $instance;
    178237    }
  • collapsed-archives/trunk/readme.txt

    r2386684 r2508747  
    33Tags: archives, collapsed, collapsing, CSS
    44Requires at least: 2.8
    5 Tested up to: 5.5.1
    6 Stable tag: 1.5
     5Tested up to: 5.7.0
     6Stable tag: 1.6
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    2020* Option to never automatically expand list for current post / year
    2121* Choice between displaying archive links in reverse chronological order or chronological order
     22* Decades can be collapsed
     23
     24= 1.6 (2021-04-03) =
     25* Added support for collapsing decades (thanks to @PHil-PlainTech)
    2226
    2327= 1.5 (2019-05-05) =
     
    6872== Changelog ==
    6973
     74= 1.6 (2021-04-03) =
     75* Added support for collapsing decades (thanks to @PHil-PlainTech)
     76
    7077= 1.5 (2019-05-05) =
    7178* Fix for warning message in logs
  • collapsed-archives/trunk/style.css

    r1085312 r2508747  
    1111    list-style: none !important;
    1212    margin-left: 0 !important;
     13}
     14.collapsed-archives > ul > li > .decade {
     15    list-style: none !important;
     16    margin-left: 1.25em !important;
    1317}
    1418.collapsed-archives ul ul {
Note: See TracChangeset for help on using the changeset viewer.