-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.Unfiltered_Text_Widget.php
238 lines (212 loc) · 5.31 KB
/
class.Unfiltered_Text_Widget.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php # -*- coding: utf-8 -*- php-version: 5.4 -*-
/**
* Similar to the native Text widget, this class offers a plain textarea.
* And visibility options.
*
* @version 2014.03.23
*/
class Unfiltered_Text_Widget extends WP_Widget
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct(
'unfiltered_text',
__( 'Unfiltered Text', 'plugin_magic_widgets' ),
array (
'description' => __( 'Pure Markup', 'plugin_magic_widgets' )
),
array (
'width' => 300,
'height' => 150
)
);
}
/**
* Front end output
*
* @param array $args
* @param array $instance
* @return void
*/
public function widget( $args, $instance )
{
/**
* Create output before widget output.
*
* @param array $instance Current widget data, 'text' and 'title'.
* @param array $args Sidebar registration args, 'before' and so on.
*/
do_action( 'tmw_before_show_widget', $instance, $args );
if ( empty ( $instance[ 'visibility' ] ) ) {
print $instance[ 'text' ];
return;
}
$user_logged_in = is_user_logged_in();
switch ( $instance[ 'visibility' ] )
{
case 'all':
print $instance['text'];
break;
case 'members':
print ( $user_logged_in ? $instance[ 'text' ] : '' );
break;
case 'anonymous':
print ( $user_logged_in ? '' : $instance[ 'text' ] );
break;
default: // custom visibility option
/**
* Print custom content.
*
* @param array $instance Current widget data, 'text', 'visibility' and 'title'.
* @param array $args Sidebar registration args, 'before' and so on.
*/
do_action( 'tmw_show_widget', $instance, $args );
}
/**
* Create output after widget output.
*
* @param array $instance Current widget data, 'text' and 'title'.
* @param array $args Sidebar registration args, 'before' and so on.
*/
do_action( 'tmw_after_show_widget', $instance, $args );
}
/**
* Prepares the content
*
* @param array $new_instance New content
* @param array $old_instance Old content
* @return array New content
*/
public function update( $new_instance, $old_instance )
{
$visibility = $this->get_visibility_options();
if ( empty ( $new_instance[ 'visibility' ] )
or ! isset ( $visibility[ $new_instance[ 'visibility' ] ] )
)
$new_instance[ 'visibility' ] = $this->get_default_visibility();
$new_instance[ 'title' ] = esc_attr( $new_instance[ 'title' ] );
return $new_instance;
}
/**
* Backend form
*
* @param array $instance
* @return void
*/
public function form( $instance )
{
$instance = wp_parse_args(
$instance,
array(
'text' => '',
'title' => '',
'visibility' => $this->get_default_visibility()
)
);
print $this->get_title( $instance[ 'title' ] );
$text = format_to_edit( $instance[ 'text' ] );
print $this->get_textarea( $text, 'text' );
print $this->get_visibility_html( $instance[ 'visibility' ], 'visibility' );
}
/**
* Add a title to identify the widget in the widget manager.
*
* @ticket https://github.com/toscho/WP-Magic-Widgets/issues/3
* @param string $title
* @return string
*/
protected function get_title( $title )
{
$title = esc_attr( $title );
$id = $this->get_field_id( 'title' );
$name = $this->get_field_name( 'title' );
$label = __( 'Title:' );
return "<p>
<label for='$id'>$label
<input class='widefat' id='$id' name='$name' value='$title' type='text'>
</label>
</p>";
}
/**
* Create the textarea for the main content
*
* @param string $content
* @param string $name
* @return string
*/
protected function get_textarea( $content, $name )
{
return sprintf(
'<p>
<textarea class="widefat code" rows="7" cols="20" id="%1$s" name="%2$s">%3$s</textarea>
</p>',
$this->get_field_id( $name ),
$this->get_field_name( $name ),
$content
);
}
/**
* Render visibility radio buttons
*
* @param string $current
* @param string $name
* @return string
*/
protected function get_visibility_html( $current, $name )
{
$options = $this->get_visibility_options();
$out = '<fieldset>
<legend><b>' . __( 'Visibility', 'plugin_magic_widgets' ) .'</b></legend>
<ul>';
foreach ( $options as $key => $label )
{
$out .= sprintf(
'<li>
<label for="%1$s">
<input type="radio" name="%2$s" id="%1$s" value="%3$s" %4$s> %5$s
</label>
</li>',
$this->get_field_id( $key ),
$this->get_field_name( $name ),
$key,
checked( $key, $current, FALSE ),
esc_html( $label )
);
}
return "$out</ul></fieldset>";
}
/**
* Default options for widget visibility
*
* @uses apply_filters tmw_visibility_options
* @return array
*/
protected function get_visibility_options()
{
$options = array (
'all' => __( 'All', 'plugin_magic_widgets' ),
'members' => __( 'Members only', 'plugin_magic_widgets' ),
'anonymous' => __( 'Anonymous visitors only', 'plugin_magic_widgets' )
);
/**
* Add custom visibility options, or remove existing ones.
* You *must* return an array.
*
* @param array $options
*/
return apply_filters( 'tmw_visibility_options', $options );
}
/**
* Get the first visibility options key as default
*
* @return string
*/
protected function get_default_visibility()
{
$options = $this->get_visibility_options();
return key( $options );
}
}