forked from roots/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.php
133 lines (110 loc) · 4.08 KB
/
widgets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Register sidebars and widgets
*/
function roots_widgets_init() {
// Sidebars
register_sidebar(array(
'name' => __('Primary', 'roots'),
'id' => 'sidebar-primary',
'before_widget' => '<section class="widget %1$s %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => __('Footer', 'roots'),
'id' => 'sidebar-footer',
'before_widget' => '<section class="widget %1$s %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
// Widgets
register_widget('Roots_Vcard_Widget');
}
add_action('widgets_init', 'roots_widgets_init');
/**
* Example vCard widget
*/
class Roots_Vcard_Widget extends WP_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);
$this->alt_option_name = 'widget_roots_vcard';
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) {
$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);
foreach($this->fields as $name => $label) {
if (!isset($instance[$name])) { $instance[$name] = ''; }
}
echo $before_widget;
if ($title) {
echo $before_title, $title, $after_title;
}
?>
<p class="vcard">
<a class="fn org url" href="<?php echo home_url('/'); ?>"><?php bloginfo('name'); ?></a><br>
<span class="adr">
<span class="street-address"><?php echo $instance['street_address']; ?></span><br>
<span class="locality"><?php echo $instance['locality']; ?></span>,
<span class="region"><?php echo $instance['region']; ?></span>
<span class="postal-code"><?php echo $instance['postal_code']; ?></span><br>
</span>
<span class="tel"><span class="value"><?php echo $instance['tel']; ?></span></span><br>
<a class="email" href="mailto:<?php echo $instance['email']; ?>"><?php echo $instance['email']; ?></a>
</p>
<?php
echo $after_widget;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_roots_vcard', $cache, 'widget');
}
function update($new_instance, $old_instance) {
$instance = array_map('strip_tags', $new_instance);
$this->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) {
foreach($this->fields as $name => $label) {
${$name} = isset($instance[$name]) ? esc_attr($instance[$name]) : '';
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id($name)); ?>"><?php _e("{$label}:", 'roots'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id($name)); ?>" name="<?php echo esc_attr($this->get_field_name($name)); ?>" type="text" value="<?php echo ${$name}; ?>">
</p>
<?php
}
}
}