From 0bca4d94be95dfabb1d878e401cb17d484615f38 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Dec 2009 16:00:17 -0500 Subject: [PATCH] improved widget loading architecture --- functions.php | 22 ++++++++++++++++++++-- widgets/ArchiveDropdownWidget.inc | 8 +++++--- widgets/BookmarkWidget.inc | 11 ++++++++--- widgets/BuyThisPrintWidget.inc | 10 ++++++---- widgets/CalendarWidget.inc | 8 +++++--- widgets/GraphicalNavigationWidget.inc | 10 ++++++---- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/functions.php b/functions.php index b5d04e2..a9ab8c3 100644 --- a/functions.php +++ b/functions.php @@ -6,18 +6,36 @@ if (function_exists('add_theme_support')) { } function __comicpress_widgets_init() { + $available_widgets = array(); + if (($dh = opendir(dirname(__FILE__) . '/widgets')) !== false) { while (($file = readdir($dh)) !== false) { if (strpos($file, '.inc') !== false) { $class_name = preg_replace('#\..*$#', '', $file); require_once(dirname(__FILE__) . '/widgets/' . $file); register_widget($class_name); - $widget = new $class_name(); - if (method_exists($widget, 'init')) { $widget->init(); } + $widget = new $class_name(true); + if (method_exists($widget, 'init')) { + $widget->init(); + } + + $available_widgets[strtolower($class_name)] = $widget; } } closedir($dh); } + + foreach (wp_get_sidebars_widgets() as $type => $widgets) { + if ($type != 'wp_inactive_widgets') { + foreach ($widgets as $widget_id) { + foreach ($available_widgets as $key => $widget) { + if (strpos(strtolower($widget_id), $key) === 0) { + $widget->is_active(); + } + } + } + } + } } function __comicpress_init() { diff --git a/widgets/ArchiveDropdownWidget.inc b/widgets/ArchiveDropdownWidget.inc index 665762c..ad47082 100644 --- a/widgets/ArchiveDropdownWidget.inc +++ b/widgets/ArchiveDropdownWidget.inc @@ -12,9 +12,11 @@ Author URI: http://frumph.net/ class ArchiveDropdownWidget extends WP_Widget { var $modes; - function ArchiveDropdownWidget() { - $widget_ops = array('classname' => 'ArchiveDropdownWidget', 'description' => __('Display a dropdown list of your archives, styled.','comicpress') ); - $this->WP_Widget('archive_dropdown', __('ComicPress Archive Dropdown','comicpress'), $widget_ops); + function ArchiveDropdownWidget($skip_widget_init = false) { + if (!$skip_widget_init) { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Display a dropdown list of your archives, styled.','comicpress') ); + $this->WP_Widget(__CLASS__, __('ComicPress Archive Dropdown','comicpress'), $widget_ops); + } $this->modes = array( 'monthly_archive' => __('Monthly archive', 'comicpress'), diff --git a/widgets/BookmarkWidget.inc b/widgets/BookmarkWidget.inc index b2e4648..eb0308c 100644 --- a/widgets/BookmarkWidget.inc +++ b/widgets/BookmarkWidget.inc @@ -1,13 +1,18 @@ __CLASS__, 'description' => __('Allow the user to bookmark a page and then jump to it upon return.','comicpress') ); - $this->WP_Widget('comicpress-bookmark', __('ComicPress Bookmark','comicpress'), $widget_ops); + function BookmarkWidget($skip_widget_init = false) { + if (!$skip_widget_init) { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Allow the user to bookmark a page and then jump to it upon return.','comicpress') ); + $this->WP_Widget(__CLASS__, __('ComicPress Bookmark','comicpress'), $widget_ops); + } } function init() { add_action('wp_head', array(&$this, 'wp_head')); + } + + function is_active() { add_action('template_redirect', array(&$this, 'template_redirect')); } diff --git a/widgets/BuyThisPrintWidget.inc b/widgets/BuyThisPrintWidget.inc index 319b00c..21fecc0 100644 --- a/widgets/BuyThisPrintWidget.inc +++ b/widgets/BuyThisPrintWidget.inc @@ -10,9 +10,11 @@ Author URI: http://frumph.net/ */ class BuyThisPrintWidget extends WP_Widget { - function BuyThisPrintWidget() { - $widget_ops = array('classname' => 'ComicPressBuyThisPrint', 'description' => __('Adds a button that goes to the buy print template page.','comicpress') ); - $this->WP_Widget('comicpress_buyprint', __('Buy This Print','comicpress'), $widget_ops); + function BuyThisPrintWidget($skip_widget_init = false) { + if (!$skip_widget_init) { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Adds a button that goes to the buy print template page.','comicpress') ); + $this->WP_Widget(__CLASS__, __('Buy This Print','comicpress'), $widget_ops); + } } function init() { @@ -21,7 +23,7 @@ class BuyThisPrintWidget extends WP_Widget { function widget($args, $instance) { extract($args, EXTR_SKIP); - + echo $before_widget; $title = apply_filters('widget_title', $instance['title']); if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; diff --git a/widgets/CalendarWidget.inc b/widgets/CalendarWidget.inc index 4a5f87a..0f5a9c4 100644 --- a/widgets/CalendarWidget.inc +++ b/widgets/CalendarWidget.inc @@ -10,9 +10,11 @@ Author URI: http://frumph.net/ class CalendarWidget extends WP_Widget { - function CalendarWidget() { - $widget_ops = array('classname' => 'CalendarWidget', 'description' => __('Display a calendar showing this months posts. (this calendar does not drop lines if there is no title given.)','comicpress') ); - $this->WP_Widget('comicpress_calendar', __('ComicPress Calendar','comicpress'), $widget_ops); + function CalendarWidget($skip_widget_init = false) { + if (!$skip_widget_init) { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Display a calendar showing this months posts. (this calendar does not drop lines if there is no title given.)','comicpress') ); + $this->WP_Widget(__CLASS__, __('ComicPress Calendar','comicpress'), $widget_ops); + } } function widget($args, $instance) { diff --git a/widgets/GraphicalNavigationWidget.inc b/widgets/GraphicalNavigationWidget.inc index 4aad79e..90fbc74 100644 --- a/widgets/GraphicalNavigationWidget.inc +++ b/widgets/GraphicalNavigationWidget.inc @@ -11,16 +11,18 @@ Author URI: http://frumph.net/ require_once(dirname(__FILE__) . '/../classes/ComicPressNavigation.inc'); class GraphicalNavigationWidget extends WP_Widget { - function GraphicalNavigationWidget() { - $widget_ops = array('classname' => 'WidgetComicPressGraphicalStorylineNavigation', 'description' => __('Displays Graphical Navigation Buttons. (used in comic sidebars)','comicpress') ); - $this->WP_Widget('graphicalstorylinenavigation', __('Comic Navigation','comicpress'), $widget_ops); + function GraphicalNavigationWidget($skip_widget_init = false) { + if (!$skip_widget_init) { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Displays Graphical Navigation Buttons. (used in comic sidebars)','comicpress') ); + $this->WP_Widget(__CLASS__, __('Comic Navigation','comicpress'), $widget_ops); + } } /** * Initialize the widget class. */ function init() { - add_filter('comicpress_display_navigation_order', array(&$this, 'comicpress_display_navigation_order')); + add_filter('comicpress_display_navigation_order', array($this, 'comicpress_display_navigation_order')); add_filter('comicpress_display_navigation_link', array(&$this, 'comicpress_display_navigation_link'), 10, 5); add_filter('comicpress_wrap_navigation_buttons', array(&$this, 'comicpress_wrap_navigation_buttons'), 10, 2); add_filter('comicpress_navigation_grouping_details', array(&$this, 'comicpress_navigation_grouping_details'));