<?php
/*
Template Name: Archives Template
*/
/************* Archive Category Walker *****************/
$ms_stylesheet_directory = get_bloginfo('stylesheet_directory');
class Walker_Category_Rabbit extends Walker_Category {
    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        extract($args);
        global $ms_stylesheet_directory;
        
        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
        if ( $use_desc_for_title == 0 || empty($category->description) )
            $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
        else
            $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
        $link .= '>';
        $link .= $cat_name . '</a>';
            
    if ( !empty($feed_image) || !empty($feed) ) {
      $link .= ' ';
      
      if ( empty($feed_image) )
        $link .= '(';
      
      $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
      
      if ( empty($feed) ) {
        $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
      } else {
        $title = ' title="' . $feed . '"';
        $alt = ' alt="' . $feed . '"';
        $name = $feed;
        $link .= $title;
      }

      $link .= '>';
      
      if ( empty($feed_image) )
        $link .= $name;
      else
        $link .= "<img src='$feed_image'$alt$title" . ' />';
      
      $link .= '</a>';
      
      if ( empty($feed_image) )
        $link .= ')';
    }
    
    if ( !empty($show_count) )
      $link .= ' (' . intval($category->count) . ')';
    
    if ( 'list' == $args['style'] ) {
      $output .= "\t<li";
      $class = 'cat-item cat-item-' . $category->term_id;
      if ( !empty($current_category) ) {
        $_current_category = get_term( $current_category, $category->taxonomy );
        if ( $category->term_id == $current_category )
          $class .=  ' current-cat';
        elseif ( $category->term_id == $_current_category->parent )
          $class .=  ' current-cat-parent';
      }
      $output .=  ' class="' . $class . '"';
      $output .= ">$link\n";
    } else {
      $output .= "\t$link<br />\n";
    }
  }
}

/* called in archives.php
   adds link to mosaic_page for monthly archives */
function miech_archive_mosaic_filter ( $link_html ) {
    preg_match('/\/(\d\d\d\d)\/(\d\d)\//', $link_html, $match);
    $link_html = preg_replace('/<li>\s*<a href=["\'].*?["\']/i', '<li><a href="' . home_url() . '/image-mosaic?mosaic_month=' . $match[2]
                            . '&mosaic_year=' . $match[1] . '"', $link_html );
    return $link_html;
}

/* debugging helper
   from http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ */
if (!function_exists('write_log')) {
    function write_log ( $log )  {
        if ( true === WP_DEBUG ) {
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
}

?>
<?php get_header(); ?>

<div id="content" class="site-content">
  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
      
      <article id="post-31164" class="post-31164 page type-page status-publish hentry entry">
        
	<header class="entry-header alignwide">
	  <h1 class="entry-title">Archives</h1>			
	</header>
	
	<div class="entry-content">
	  
          <div class="wp-block-columns">
            <div class="wp-block-column">
              <strong>Archives by Month</strong>
              <ul>
                <?php
                #          add_filter('get_archives_link', 'miech_archive_mosaic_filter', 10, 1);
                wp_get_archives('type=monthly&show_post_count=1');
                #          remove_filter('get_archives_link', 'miech_archive_mosaic_filter', 10, 1);
                ?>
              </ul>
            </div>
            
            <div class="wp-block-column">
              <strong>Archives by Category</strong>
              <ul>
                <?php /* wp_list_categories('title_li=0&show_count=1&exclude=1140&walker=Walker_Category_Rabbit'); */
                $MyWalker = new Walker_Category_Rabbit();
                wp_list_categories(array('title_li'   => 0,
                                         'show_count' => 1,
                                         /* 'exclude'    => '1140', */
                                         'walker'     => $MyWalker));
                ?><!--  -->
              </ul>
            </div>
          </div>
        </div>
      </article>

<?php get_footer(); ?>
