Skip to content
Snippets Groups Projects
Commit 74e765a5 authored by Erick Hitter's avatar Erick Hitter
Browse files

Implement singleton pattern in place of reliance on a global variable.

parent 7b401919
Branches
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ Plugin Name: Date-based Taxonomy Archives ...@@ -4,7 +4,7 @@ Plugin Name: Date-based Taxonomy Archives
Plugin URI: http://www.ethitter.com/plugins/date-based-taxonomy-archives/ Plugin URI: http://www.ethitter.com/plugins/date-based-taxonomy-archives/
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. 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.2 Version: 0.3
Author URI: http://www.ethitter.com/ Author URI: http://www.ethitter.com/
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
...@@ -23,6 +23,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ...@@ -23,6 +23,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
class Date_Based_Taxonomy_Archives { class Date_Based_Taxonomy_Archives {
/**
* Singleton
*/
private static $__instance = null;
/** /**
* Class variables * Class variables
*/ */
...@@ -40,13 +45,34 @@ class Date_Based_Taxonomy_Archives { ...@@ -40,13 +45,34 @@ class Date_Based_Taxonomy_Archives {
var $filter_archive_links = false; var $filter_archive_links = false;
/**
* Silence is golden!
*/
private function __construct() {}
/**
* Singleton implementation
*
* @uses self::setup
* @return object
*/
public static function get_instance() {
if ( ! is_a( self::$__instance, __CLASS__ ) ) {
self::$__instance = new self;
self::$__instance->setup();
}
return self::$__instance;
}
/** /**
* Register actions and filters * Register actions and filters
* *
* @uses add_action, add_filter * @uses add_action, add_filter
* @return null * @return null
*/ */
function __construct() { private function setup() {
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' ) );
...@@ -287,9 +313,12 @@ class Date_Based_Taxonomy_Archives { ...@@ -287,9 +313,12 @@ class Date_Based_Taxonomy_Archives {
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; Date_Based_Taxonomy_Archives::get_instance();
if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
$date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives; /**
* Alias global variable used to hold class prior to singleton implementation in v0.3.
*/
$GLOBALS['date_based_taxonomy_archives'] = Date_Based_Taxonomy_Archives::get_instance();
/** /**
* Render unordered lists of monthly archive links grouped by year * Render unordered lists of monthly archive links grouped by year
...@@ -299,10 +328,5 @@ if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) ) ...@@ -299,10 +328,5 @@ if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
* @return string or false * @return string or false
*/ */
function date_based_taxonomy_archives( $args = array() ) { function date_based_taxonomy_archives( $args = array() ) {
global $date_based_taxonomy_archives; return Date_Based_Taxonomy_Archives::get_instance()->get_archives( $args );
if ( ! is_a( $date_based_taxonomy_archives, 'Date_Based_Taxonomy_Archives' ) )
$date_based_taxonomy_archives = new Date_Based_Taxonomy_Archives;
return $date_based_taxonomy_archives->get_archives( $args );
} }
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment