diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b9b744..6e06eb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### HEAD +* Refactor/simplify Roots vCard Widget * Move custom entry_meta code into template * Move Google Analytics code into footer template * Add CONTRIBUTING.md to assist with the new GitHub UI diff --git a/lib/widgets.php b/lib/widgets.php index b12a786..98e7c6b 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -27,7 +27,17 @@ add_action('widgets_init', 'roots_widgets_init'); // Example vCard widget class Roots_Vcard_Widget extends WP_Widget { - function Roots_Vcard_Widget() { + private $fields = array( + 'title' => 'Title (optional)', + 'street_address' => 'Street Address', + 'locality' => 'City/Locality', + 'region' => 'State/Region', + 'postal_code' => 'Zipcode/Postal Code', + 'tel' => 'Telephone', + 'email' => 'Email' + ); + + function __construct() { $widget_ops = array('classname' => 'widget_roots_vcard', 'description' => __('Use this widget to add a vCard', 'roots')); $this->WP_Widget('widget_roots_vcard', __('Roots: vCard', 'roots'), $widget_ops); @@ -58,19 +68,15 @@ class Roots_Vcard_Widget extends WP_Widget { 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'] = ''; } + + foreach($this->fields as $name => $label) { + if (!isset($instance[$name])) { $instance[$name] = ''; } + } echo $before_widget; if ($title) { - echo $before_title; - echo $title; - echo $after_title; + echo $before_title, $title, $after_title; } ?>

@@ -92,14 +98,8 @@ class Roots_Vcard_Widget extends WP_Widget { } function update($new_instance, $old_instance) { - $instance = $old_instance; - $instance['title'] = strip_tags($new_instance['title']); - $instance['street_address'] = strip_tags($new_instance['street_address']); - $instance['locality'] = strip_tags($new_instance['locality']); - $instance['region'] = strip_tags($new_instance['region']); - $instance['postal_code'] = strip_tags($new_instance['postal_code']); - $instance['tel'] = strip_tags($new_instance['tel']); - $instance['email'] = strip_tags($new_instance['email']); + $instance = array_map('strip_tags', $new_instance); + $this->flush_widget_cache(); $alloptions = wp_cache_get('alloptions', 'options'); @@ -116,42 +116,14 @@ class Roots_Vcard_Widget extends WP_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']) : ''; - ?> + foreach($this->fields as $name => $label) { + ${$name} = isset($instance[$name]) ? esc_attr($instance[$name]) : ''; + ?>

- - + +

-

- - -

-

- - -

-

- - -

-

- - -

-

- - -

-

- - -

-