From bccac4380f9f372fa4672113fdbf8a4ea555c434 Mon Sep 17 00:00:00 2001 From: Ben Word Date: Sun, 15 Jan 2012 14:17:21 -0700 Subject: [PATCH] re-did example vcard widget based on twentyeleven's widgets.php --- inc/roots-widgets.php | 127 +++++++++++++++++++++++++++++++----------- 1 file changed, 95 insertions(+), 32 deletions(-) diff --git a/inc/roots-widgets.php b/inc/roots-widgets.php index 3a024b3..1b91b14 100644 --- a/inc/roots-widgets.php +++ b/inc/roots-widgets.php @@ -1,73 +1,136 @@ 'widget_roots_vcard', 'description' => __('Use this widget to add a vCard', 'roots')); + $this->WP_Widget('widget_roots_vcard', __('Roots: vCard', 'roots'), $widget_ops); + $this->alt_option_name = 'widget_roots_vcard'; - function roots_vcard() { - $widget_ops = array('description' => 'Display a vCard'); - parent::WP_Widget(false, __('Roots: vCard', 'roots'), $widget_ops); + add_action('save_post', array(&$this, 'flush_widget_cache')); + add_action('deleted_post', array(&$this, 'flush_widget_cache')); + add_action('switch_theme', array(&$this, 'flush_widget_cache')); } function widget($args, $instance) { - extract($args); - extract($instance); + $cache = wp_cache_get('widget_roots_vcard', 'widget'); + + if (!is_array($cache)) { + $cache = array(); + } + + if (!isset($args['widget_id'])) { + $args['widget_id'] = null; + } + + if (isset($cache[$args['widget_id']])) { + echo $cache[$args['widget_id']]; + return; + } + + ob_start(); + extract($args, EXTR_SKIP); + + $title = apply_filters('widget_title', empty($instance['title']) ? __('vCard', 'roots') : $instance['title'], $instance, $this->id_base); + if (!isset($instance['street_address'])) { $instance['street_address'] = ''; } + if (!isset($instance['locality'])) { $instance['locality'] = ''; } + if (!isset($instance['region'])) { $instance['region'] = ''; } + if (!isset($instance['postal_code'])) { $instance['postal_code'] = ''; } + if (!isset($instance['tel'])) { $instance['tel'] = ''; } + if (!isset($instance['email'])) { $instance['email'] = ''; } + echo $before_widget; if ($title) { - echo $before_title, $title, $after_title; + echo $before_title; + echo $title; + echo $after_title; } ?>


-
- , - -
+
+ , + +
-
- +
+

- flush_widget_cache(); + + $alloptions = wp_cache_get('alloptions', 'options'); + if (isset($alloptions['widget_roots_vcard'])) { + delete_option('widget_roots_vcard'); + } + + return $instance; + } + + function flush_widget_cache() { + wp_cache_delete('widget_roots_vcard', 'widget'); } function form($instance) { + $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; + $street_address = isset($instance['street_address']) ? esc_attr($instance['street_address']) : ''; + $locality = isset($instance['locality']) ? esc_attr($instance['locality']) : ''; + $region = isset($instance['region']) ? esc_attr($instance['region']) : ''; + $postal_code = isset($instance['postal_code']) ? esc_attr($instance['postal_code']) : ''; + $tel = isset($instance['tel']) ? esc_attr($instance['tel']) : ''; + $email = isset($instance['email']) ? esc_attr($instance['email']) : ''; ?>

- - + +

- - + +

- - + +

- - + +

- - + +

- - + +

- - + +

+add_action('widgets_init', 'roots_widget_init'); + +?> \ No newline at end of file