This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button-widget-register.php
272 lines (258 loc) · 8.88 KB
/
button-widget-register.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Button widget
* A simple button widget for your sidebars.
*
* @package button-widget
* @author MyPreview (Github: @mahdiyazdani, @mypreview)
* @since 1.2.1
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registering Button Widget - Class
*/
class Button_Widget_Register extends WP_Widget {
/**
* Defaults
*
* @since 1.2.1
* @var defaults
*/
private $defaults;
/**
* Setup class.
*
* @since 1.2.1
* @return void
*/
public function __construct() {
parent::__construct(
'button_widget',
apply_filters( 'button_widget_name', _x( 'Button', 'widget name', 'button-widget' ) ),
array(
'description' => _x( 'Display a simple button widget in your sidebar areas.', 'widget description', 'button-widget' ),
'customize_selective_refresh' => true,
)
);
$this->defaults = array(
'text' => '',
'title' => '',
'id' => '',
'class' => 'button',
'link' => '',
'target' => 0,
'text_color' => '',
'background_color' => '',
);
}
/**
* Widget Front End.
*
* @since 1.2.1
* @param mixed $args Arguments.
* @param mixed $instance Instance.
* @return void
*/
public function widget( $args, $instance ) {
$get_colors = array();
$instance = wp_parse_args( (array) $instance, $this->defaults );
$get_text = isset( $instance['text'] ) ? $instance['text'] : $this->defaults['text'];
$get_title = isset( $instance['title'] ) ? $instance['title'] : $this->defaults['title'];
$get_id = isset( $instance['id'] ) ? $instance['id'] : $this->defaults['id'];
$get_class = isset( $instance['class'] ) ? $instance['class'] : $this->defaults['class'];
$get_link = isset( $instance['link'] ) ? $instance['link'] : $this->defaults['link'];
$get_target = isset( $instance['target'] ) ? self::string_to_bool( $instance['target'] ) : $this->defaults['target'];
$get_colors[] = ! empty( $instance['text_color'] ) ? sprintf( 'color:%s;', sanitize_hex_color( $instance['text_color'] ) ) : $this->defaults['text_color'];
$get_colors[] = ! empty( $instance['background_color'] ) ? sprintf( 'background:%s;', sanitize_hex_color( $instance['background_color'] ) ) : $this->defaults['background_color'];
$get_colors = array_filter( $get_colors );
// Bail out, if the button text is NOT defined!
if ( empty( $get_text ) ) {
return;
}
$output = sprintf( '%s<a href="%s" id="%s" title="%s" target="%s" rel="%s" style="%s" class="%s">%s</a>%s', $args['before_widget'], ! empty( $get_link ) ? esc_url( $get_link ) : '#', esc_attr( $get_id ), esc_attr( $get_title ), $get_target ? '_blank' : '_self', $get_target ? 'noopener noreferrer nofollow' : '', implode( '', $get_colors ), esc_attr( $get_class ), esc_html( $get_text ), $args['after_widget'] );
/**
* Filters the `Button` widget output.
*
* @param string $output `Button` Widget html output.
*/
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo apply_filters( 'button_widget_output', $output );
}
/**
* Widget Settings.
*
* @since 1.2.1
* @param mixed $instance Instance.
* @return void
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
?><p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"
>
<?php
echo esc_html_x( 'Text:', 'field label', 'button-widget' );
?>
</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>"
value="<?php echo esc_attr( $instance['text'] ); ?>"
/>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"
>
<?php
/* translators: %s: Small open and close tags. */
printf( esc_html_x( 'ID %1$sattribute%2$s:', 'field label', 'button-widget' ), '<small>', '</small>' );
?>
</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>"
value="<?php echo esc_attr( $instance['id'] ); ?>"
/>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
>
<?php
/* translators: %s: Small open and close tags. */
printf( esc_html_x( 'Title %1$sattribute%2$s:', 'field label', 'button-widget' ), '<small>', '</small>' );
?>
</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
value="<?php echo esc_attr( $instance['title'] ); ?>"
/>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>"
>
<?php
/* translators: %s: Small open and close tags. */
printf( esc_html_x( 'CSS Class(es) %1$sSeparate multiple classes with spaces%2$s:', 'field label', 'button-widget' ), '<small>', '</small>' );
?>
</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'class' ) ); ?>"
value="<?php echo esc_attr( $instance['class'] ); ?>"
/>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"
>
<?php
echo esc_html_x( 'Link to:', 'field label', 'button-widget' );
?>
</label>
<input
type="url"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>"
value="<?php echo esc_url( $instance['link'] ); ?>"
/>
</p>
<p>
<input
value="1"
type="checkbox"
id="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'target' ) ); ?>"
<?php checked( $instance['target'], 1 ); ?>
/>
<label
for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"
>
<?php
echo esc_html_x( 'Open linked address in a new window?', 'field label', 'button-widget' );
?>
</label>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'text_color' ) ); ?>"
>
<?php
echo esc_html_x( 'Color:', 'field label', 'button-widget' );
?>
</label>
<input
type="text"
class="button-widget-color-picker"
data-default-color="<?php echo sanitize_hex_color( apply_filters( 'button_widget_text_color', '#FFFFFF' ) ); ?>"
id="<?php echo esc_attr( $this->get_field_id( 'text_color' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'text_color' ) ); ?>"
value="<?php echo esc_attr( $instance['text_color'] ); ?>"
/>
</p>
<p>
<label
for="<?php echo esc_attr( $this->get_field_id( 'background_color' ) ); ?>"
>
<?php
echo esc_html_x( 'Background:', 'field label', 'button-widget' );
?>
</label>
<input
type="text"
class="button-widget-color-picker"
data-default-color="<?php echo sanitize_hex_color( apply_filters( 'button_widget_background_color', '#0085BA' ) ); ?>"
id="<?php echo esc_attr( $this->get_field_id( 'background_color' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'background_color' ) ); ?>"
value="<?php echo esc_attr( $instance['background_color'] ); ?>"
/>
</p>
<?php
}
/**
* Update Widget Settings.
*
* @since 1.2.1
* @param mixed $new_instance New Instance.
* @param mixed $old_instance Old Instance.
* @return Instance.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['text'] = sanitize_text_field( $new_instance['text'] );
$instance['id'] = sanitize_text_field( $new_instance['id'] );
$instance['class'] = sanitize_text_field( $new_instance['class'] );
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['link'] = esc_url_raw( $new_instance['link'] );
$instance['target'] = ( ! isset( $new_instance['target'] ) ) ? 0 : 1;
$instance['text_color'] = ! empty( $new_instance['text_color'] ) ? sanitize_hex_color( $new_instance['text_color'] ) : $this->defaults['text_color'];
$instance['background_color'] = ! empty( $new_instance['background_color'] ) ? sanitize_hex_color( $new_instance['background_color'] ) : $this->defaults['background_color'];
return $instance;
}
/**
* Converts a string (e.g. 'yes' or 'no') to a bool.
*
* @since 1.2.1
* @param string $input String to convert.
* @return bool
*/
public static function string_to_bool( $input ) {
return is_bool( $input ) ? $input : ( 'yes' === $input || 1 === $input || 'true' === $input || 'TRUE' === $input || '1' === $input );
}
} // End Class.