Commit b5e5e619 authored by Erick Hitter's avatar Erick Hitter
Browse files

Version 0.2 - clean up plugin for WP.org release.

parent ab4397c0
...@@ -2,10 +2,24 @@ ...@@ -2,10 +2,24 @@
/* /*
Plugin Name: Date-based Taxonomy Archives Plugin Name: Date-based Taxonomy Archives
Plugin URI: Plugin URI:
Description: Renders an unordered list of years with months, linked to corresponding date-based taxonomy archive, nested therein. Description: Add support for date-based taxonomy archives. Render an unordered list of years with months, linked to corresponding date-based taxonomy archive, nested therein.
Author: Erick Hitter Author: Erick Hitter
Version: 0.1 Version: 0.2
Author URI: http://www.ethitter.com/ Author URI: http://www.ethitter.com/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
class Date_Based_Taxonomy_Archives { class Date_Based_Taxonomy_Archives {
...@@ -33,13 +47,13 @@ class Date_Based_Taxonomy_Archives { ...@@ -33,13 +47,13 @@ class Date_Based_Taxonomy_Archives {
* @return null * @return null
*/ */
function __construct() { function __construct() {
add_action( 'transition_post_status', array( $this, 'action_transition_post_status' ), 50, 2 );
add_filter( 'date_based_taxonomy_archives_where', array( $this, 'filter_date_based_taxonomy_archives_where' ), 10, 2 ); add_filter( 'date_based_taxonomy_archives_where', array( $this, 'filter_date_based_taxonomy_archives_where' ), 10, 2 );
add_filter( 'date_based_taxonomy_archives_join', array( $this, 'filter_date_based_taxonomy_archives_join' ), 10, 2 ); add_filter( 'date_based_taxonomy_archives_join', array( $this, 'filter_date_based_taxonomy_archives_join' ), 10, 2 );
add_filter( 'get_archives_link', array( $this, 'filter_get_archives_link' ) ); add_filter( 'get_archives_link', array( $this, 'filter_get_archives_link' ) );
add_action( 'generate_rewrite_rules', array( $this, 'action_generate_rewrite_rules' ) ); add_action( 'generate_rewrite_rules', array( $this, 'action_generate_rewrite_rules' ) );
add_action( 'transition_post_status', array( $this, 'action_transition_post_status' ), 50, 2 );
} }
/** /**
...@@ -60,7 +74,7 @@ class Date_Based_Taxonomy_Archives { ...@@ -60,7 +74,7 @@ class Date_Based_Taxonomy_Archives {
$where = apply_filters( 'date_based_taxonomy_archives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $args ); $where = apply_filters( 'date_based_taxonomy_archives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $args );
$join = apply_filters( 'date_based_taxonomy_archives_join', '', $args ); $join = apply_filters( 'date_based_taxonomy_archives_join', '', $args );
if( is_numeric( $limit ) ) if ( is_numeric( $limit ) )
$limit = ' LIMIT ' . absint( $limit ); $limit = ' LIMIT ' . absint( $limit );
$query = $wpdb->prepare( "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 DESC $limit" ); $query = $wpdb->prepare( "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 DESC $limit" );
...@@ -68,14 +82,14 @@ class Date_Based_Taxonomy_Archives { ...@@ -68,14 +82,14 @@ class Date_Based_Taxonomy_Archives {
//Generate cache key, check cache, query DB if necessary and cache results //Generate cache key, check cache, query DB if necessary and cache results
$cache_key = $this->get_incrementor() . md5( $query ); $cache_key = $this->get_incrementor() . md5( $query );
if( !$results = wp_cache_get( $cache_key, $this->cache_group ) ) { if ( ! $results = wp_cache_get( $cache_key, $this->cache_group ) ) {
$results = $wpdb->get_results( $query ); $results = $wpdb->get_results( $query );
wp_cache_set( $cache_key, $results, $this->cache_group ); wp_cache_set( $cache_key, $results, $this->cache_group );
} }
//Bail out if necessary data isn't available //Bail out if necessary data isn't available
if( !is_array( $results ) || empty( $results ) ) if ( ! is_array( $results ) || empty( $results ) )
return false; return false;
//Render archive //Render archive
...@@ -85,11 +99,11 @@ class Date_Based_Taxonomy_Archives { ...@@ -85,11 +99,11 @@ class Date_Based_Taxonomy_Archives {
//Alias $after for use inside of foreach //Alias $after for use inside of foreach
$_after = $after; $_after = $after;
foreach( $results as $result ) { foreach ( $results as $result ) {
if( $cy !== false && $cy != $result->year ) if ( $cy !== false && $cy != $result->year )
$output .= '</ul></li>'; $output .= '</ul></li>';
if( $cy === false || $cy != $result->year ) { if ( $cy === false || $cy != $result->year ) {
$cy = $result->year; $cy = $result->year;
$output .= '<li><span>' . absint( $result->year ) . '</span>'; $output .= '<li><span>' . absint( $result->year ) . '</span>';
...@@ -105,7 +119,7 @@ class Date_Based_Taxonomy_Archives { ...@@ -105,7 +119,7 @@ class Date_Based_Taxonomy_Archives {
$output .= get_archives_link( $url, $text, 'html', $before, $after ); $output .= get_archives_link( $url, $text, 'html', $before, $after );
} }
if( $cy == $result->year ) if ( $cy == $result->year )
$output .= '</ul></li>'; $output .= '</ul></li>';
$output .= '</ul>'; $output .= '</ul>';
...@@ -113,7 +127,7 @@ class Date_Based_Taxonomy_Archives { ...@@ -113,7 +127,7 @@ class Date_Based_Taxonomy_Archives {
//Reset archive links filter indicator //Reset archive links filter indicator
$this->filter_archive_links = false; $this->filter_archive_links = false;
if( $echo ) if ( $echo )
echo $output; echo $output;
else else
return $output; return $output;
...@@ -135,19 +149,19 @@ class Date_Based_Taxonomy_Archives { ...@@ -135,19 +149,19 @@ class Date_Based_Taxonomy_Archives {
$args = wp_parse_args( $args, $this->defaults ); $args = wp_parse_args( $args, $this->defaults );
extract( $args ); extract( $args );
if( if (
( $taxonomies == 'all' && ( is_category() || is_tag() || is_tax() ) ) || ( $taxonomies == 'all' && ( is_category() || is_tag() || is_tax() ) ) ||
( $taxonomies == 'custom' && !is_category() && !is_tag() && is_tax() ) ( $taxonomies == 'custom' && ! is_category() && ! is_tag() && is_tax() )
) { ) {
$queried_object = get_queried_object(); $queried_object = get_queried_object();
if( is_object( $queried_object ) && property_exists( $queried_object, 'term_taxonomy_id' ) ) if ( is_object( $queried_object ) && property_exists( $queried_object, 'term_taxonomy_id' ) )
$where .= $wpdb->prepare( ' AND dbtrtr.term_taxonomy_id = %d', $queried_object->term_taxonomy_id ); $where .= $wpdb->prepare( ' AND dbtrtr.term_taxonomy_id = %d', $queried_object->term_taxonomy_id );
} }
elseif( is_array( $taxonomies ) ) { elseif ( is_array( $taxonomies ) ) {
$queried_object = get_queried_object(); $queried_object = get_queried_object();
if( is_object( $queried_object ) && property_exists( $queried_object, 'term_taxonomy_id' ) && property_exists( $queried_object, 'taxonomy' ) && in_array( $queried_object->taxonomy, $taxonomies ) ) if ( is_object( $queried_object ) && property_exists( $queried_object, 'term_taxonomy_id' ) && property_exists( $queried_object, 'taxonomy' ) && in_array( $queried_object->taxonomy, $taxonomies ) )
$where .= $wpdb->prepare( ' AND dbtrtr.term_taxonomy_id = %d', $queried_object->term_taxonomy_id ); $where .= $wpdb->prepare( ' AND dbtrtr.term_taxonomy_id = %d', $queried_object->term_taxonomy_id );
} }
...@@ -170,9 +184,9 @@ class Date_Based_Taxonomy_Archives { ...@@ -170,9 +184,9 @@ class Date_Based_Taxonomy_Archives {
$args = wp_parse_args( $args, $this->defaults ); $args = wp_parse_args( $args, $this->defaults );
extract( $args ); extract( $args );
if( if (
( $taxonomies == 'all' && ( is_category() || is_tag() || is_tax() ) ) || ( $taxonomies == 'all' && ( is_category() || is_tag() || is_tax() ) ) ||
( $taxonomies == 'custom' && !is_category() && !is_tag() && is_tax() ) || ( $taxonomies == 'custom' && ! is_category() && ! is_tag() && is_tax() ) ||
is_array( $taxonomies ) is_array( $taxonomies )
) { ) {
$join .= $wpdb->prepare( " INNER JOIN {$wpdb->term_relationships} AS dbtrtr on dbtrtr.object_id = {$wpdb->posts}.ID" ); $join .= $wpdb->prepare( " INNER JOIN {$wpdb->term_relationships} AS dbtrtr on dbtrtr.object_id = {$wpdb->posts}.ID" );
...@@ -192,16 +206,16 @@ class Date_Based_Taxonomy_Archives { ...@@ -192,16 +206,16 @@ class Date_Based_Taxonomy_Archives {
* @return string * @return string
*/ */
function filter_get_archives_link( $link_html ) { function filter_get_archives_link( $link_html ) {
if( $this->filter_archive_links ) { if ( $this->filter_archive_links ) {
global $wp_rewrite; global $wp_rewrite;
$queried_object = get_queried_object(); $queried_object = get_queried_object();
if( is_object( $queried_object ) && !is_wp_error( $queried_object ) ) { if ( is_object( $queried_object ) && ! is_wp_error( $queried_object ) ) {
$exploded = explode( "'", $link_html ); $exploded = explode( "'", $link_html );
if( $wp_rewrite->using_permalinks() && array_key_exists( $queried_object->taxonomy, $wp_rewrite->extra_permastructs ) ) { if ( $wp_rewrite->using_permalinks() && array_key_exists( $queried_object->taxonomy, $wp_rewrite->extra_permastructs ) ) {
$term_rewrite = preg_replace( '#%[^%]+%#i', $queried_object->slug, $wp_rewrite->extra_permastructs[ $queried_object->taxonomy ][ 0 ] ); $term_rewrite = preg_replace( '#%[^%]+%#i', $queried_object->slug, $wp_rewrite->extra_permastructs[ $queried_object->taxonomy ][ 'struct' ] );
$term_rewrite = substr( $term_rewrite, 1 ); //Drop leading slash, otherwise path_join misinterprets request $term_rewrite = substr( $term_rewrite, 1 ); //Drop leading slash, otherwise path_join misinterprets request
$term_rewrite = path_join( $term_rewrite, 'date' ); $term_rewrite = path_join( $term_rewrite, 'date' );
...@@ -210,7 +224,7 @@ class Date_Based_Taxonomy_Archives { ...@@ -210,7 +224,7 @@ class Date_Based_Taxonomy_Archives {
else { else {
$taxonomy = get_taxonomy( $queried_object->taxonomy ); $taxonomy = get_taxonomy( $queried_object->taxonomy );
if( is_object( $taxonomy ) && !is_wp_error( $taxonomy ) ) if ( is_object( $taxonomy ) && ! is_wp_error( $taxonomy ) )
$exploded[ 1 ] = add_query_arg( $taxonomy->query_var, $queried_object->slug, $exploded[ 1 ] ); $exploded[ 1 ] = add_query_arg( $taxonomy->query_var, $queried_object->slug, $exploded[ 1 ] );
} }
...@@ -232,10 +246,10 @@ class Date_Based_Taxonomy_Archives { ...@@ -232,10 +246,10 @@ class Date_Based_Taxonomy_Archives {
function action_generate_rewrite_rules( $wp_rewrite ) { function action_generate_rewrite_rules( $wp_rewrite ) {
$taxonomies = get_taxonomies( null, 'objects' ); $taxonomies = get_taxonomies( null, 'objects' );
if( is_array( $taxonomies ) && !empty( $taxonomies ) ) { if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) {
$rules = array(); $rules = array();
foreach( $taxonomies as $taxonomy ) foreach ( $taxonomies as $taxonomy )
$rules[ $taxonomy->rewrite[ 'slug' ] . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?(page/?([0-9]{1,})/?)?$' ] = 'index.php?' . $taxonomy->query_var . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[5]'; $rules[ $taxonomy->rewrite[ 'slug' ] . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?(page/?([0-9]{1,})/?)?$' ] = 'index.php?' . $taxonomy->query_var . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[5]';
$wp_rewrite->rules = $rules + $wp_rewrite->rules; $wp_rewrite->rules = $rules + $wp_rewrite->rules;
...@@ -251,7 +265,7 @@ class Date_Based_Taxonomy_Archives { ...@@ -251,7 +265,7 @@ class Date_Based_Taxonomy_Archives {
function get_incrementor() { function get_incrementor() {
$incrementor = wp_cache_get( $this->cache_key_incrementor, $this->cache_group ); $incrementor = wp_cache_get( $this->cache_key_incrementor, $this->cache_group );
if( !is_numeric( $incrementor ) ) { if ( ! is_numeric( $incrementor ) ) {
$incrementor = time(); $incrementor = time();
wp_cache_set( $this->cache_key_incrementor, $incrementor, $this->cache_group ); wp_cache_set( $this->cache_key_incrementor, $incrementor, $this->cache_group );
} }
...@@ -269,12 +283,12 @@ class Date_Based_Taxonomy_Archives { ...@@ -269,12 +283,12 @@ class Date_Based_Taxonomy_Archives {
* @return null * @return null
*/ */
function action_transition_post_status( $new_status, $old_status ) { function action_transition_post_status( $new_status, $old_status ) {
if( $new_status == 'publish' || $old_status == 'publish' ) if ( $new_status == 'publish' || $old_status == 'publish' )
wp_cache_delete( $this->cache_key_incrementor, $this->cache_group ); wp_cache_delete( $this->cache_key_incrementor, $this->cache_group );
} }
} }
global $date_based_taxonomy_archives; global $date_based_taxonomy_archives;
if( !is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) ) if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
$date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives; $date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives;
/** /**
...@@ -286,7 +300,7 @@ if( !is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) ) ...@@ -286,7 +300,7 @@ if( !is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
*/ */
function date_based_taxonomy_archives( $args = array() ) { function date_based_taxonomy_archives( $args = array() ) {
global $date_based_taxonomy_archives; global $date_based_taxonomy_archives;
if( !is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) ) if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
$date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives; $date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives;
return $date_based_taxonomy_archives->get_archives( $args ); return $date_based_taxonomy_archives->get_archives( $args );
......
...@@ -4,8 +4,48 @@ Donate link: ...@@ -4,8 +4,48 @@ Donate link:
Tags: archive, taxonomy, taxonomies, date Tags: archive, taxonomy, taxonomies, date
Requires at least: 3.4 Requires at least: 3.4
Tested up to: 3.4 Tested up to: 3.4
Stable tag: Stable tag: 0.2
License: GPLv2 or later License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html License URI: http://www.gnu.org/licenses/gpl-2.0.html
Renders an unordered list of years with months, linked to corresponding date-based taxonomy archive, nested therein. Add support for date-based taxonomy archives. Also includes a function for outputting archive links.
\ No newline at end of file
== Description ==
Add support for date-based taxonomy archives.
Includes a function for rendering an unordered list of years with months, linked to corresponding date-based taxonomy archive, nested therein.
**This plugin is intended for use by plugin and theme developers. It simply adds support for date-based taxonomy archives, but has no native user interface.**
== Installation ==
1. Upload date-based-taxonomy-archives to /wp-content/plugins/.
2. Activate plugin through the WordPress Plugins menu.
3. Go to Settings > Permalinks and click _Save Changes_ to refresh permalinks.
== Frequently Asked Questions ==
= How do I use this plugin? =
Add the function `date_based_taxonomy_archives()` to any template element that appears on a taxonomy archive. The function accepts an array containing the following arguments:
* `taxonomies` - array of taxonomy slugs.
* `show_post_counts` - boolean value specifying whether or not to display show counts in parenthesis after archive links.
* `limit` - integer specifying the number of archive links to show. Omit to show all archive links for the specified taxonomy.
* `before` - output to display before archive link.
* `after` - output to display after archive link.
* `echo` - boolean value specifying whether to echo or return archive links.
= What filters does this plugin include? =
* `date_based_taxonomy_archives_args` - applied to arguments passed to `date_based_taxonomy_archives()` at runtime.
== Changelog ==
= 0.2 =
Initial public release
== Upgrade Notice ==
= 0.2 =
Initial public release
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment