-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-simple-menu.php
More file actions
178 lines (161 loc) · 6.51 KB
/
class-simple-menu.php
File metadata and controls
178 lines (161 loc) · 6.51 KB
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
<?php
/**
* Simple Menu Widget class.
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @package WordPress
* @subpackage SimpleMenuWidget
*
*/
namespace ScheRas\Plugins\Widgets;
class Simple_Menu_Widget extends \WP_Widget
{
public function __construct ()
{
parent::__construct ( 'simple-menu-widget', __ ( 'Simple Menu Widget', 'simple-menu' ), array ( 'description' => __ ( 'Displays custom links list.', 'simple-menu' ) ) );
}
/**
* Print the widget content
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
* @param array $instance The settings for the particular instance of the widget.
* @return void
*/
public function widget ( $args, $instance )
{
$links_count = ( isset ( $instance[ 'links_count' ] ) ) ? $instance[ 'links_count' ] : 5;
?>
<?php print $args[ 'before_widget' ]; ?>
<?php $this->display_title ( $args, $instance ); ?>
<ul class="simple-menu-widget">
<?php for ( $i = 0; $i < $links_count; $i++ ) : ?>
<?php if ( isset ( $instance[ 'item_' . $i . '_title' ] ) && $instance[ 'item_' . $i . '_link' ] ) : ?>
<?php $this->display_single_item ( $instance[ 'item_' . $i . '_title' ], $instance[ 'item_' . $i . '_link' ] ); ?>
<?php endif; ?>
<?php endfor; ?>
</ul>
<?php print $args[ 'after_widget' ]; ?>
<?php
}
/**
* Output the settings update form
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param array $instance Current settings.
* @return string Default return is 'noform'.
*/
public function form ( $instance )
{
$title = ( isset ( $instance[ 'title' ] ) ) ? $instance[ 'title' ] : '';
$links_count = ( isset ( $instance[ 'links_count' ] ) ) ? $instance[ 'links_count' ] : 5;
?>
<p>
<label for="<?php print esc_attr ( $this->get_field_id ( 'title' ) ); ?>"><?php esc_html_e ( 'Title:', 'simple-menu' ); ?>
<input type="text" name="<?php print esc_attr ( $this->get_field_name ( 'title' ) ); ?>" id="<?php echo $this->get_field_id ( 'title' ); ?>" class="widefat" value="<?php print esc_attr ( $title ) ?>" placeholder="<?php esc_attr_e ( 'Widget Title', 'simple-menu' ); ?>">
</label>
<label for="<?php print esc_attr ( $this->get_field_id ( 'links_count' ) ); ?>"><?php esc_html_e ( 'Links Count:', 'simple-menu' ) ?>
<input type="number" step="1" min="1" max="10" name="<?php print esc_attr ( $this->get_field_name ( 'links_count' ) ); ?>" id="<?php print esc_attr ( $this->get_field_id ( 'links_count' ) ); ?>" class="widefat" value="<?php print esc_attr ( $links_count ); ?>">
</label>
</p>
<?php for ( $i = 0; $i < $links_count; $i++ ) : ?>
<?php $item_title = isset( $instance[ 'item_'.$i.'_title' ] ) ? $instance[ 'item_'.$i.'_title' ] : ''; ?>
<?php $item_link = isset( $instance[ 'item_'.$i.'_link' ] ) ? $instance[ 'item_'.$i.'_link' ] : ''; ?>
<p>
<div style="text-align: center">
<b><?php print esc_html ( sprintf ( __ ( 'Link #%d', 'simple-menu' ), $i + 1 ) ); ?></b>
</div>
<label for="<?php echo $this->get_field_id ( 'item_'.$i.'_title' ); ?>"><?php esc_html_e ( 'Link Title:', 'simple-menu' ) ?>
<input type="text" name="<?php print esc_attr ( $this->get_field_name ( 'item_'.$i.'_title' ) ); ?>" id="<?php print esc_attr ( $this->get_field_id ( 'item_'.$i.'_title' ) ); ?>" class="widefat" value="<?php print esc_attr ( $item_title ); ?>" placeholder="<?php esc_attr_e ( 'Link Title', 'simple-menu' ) ?>">
</label>
<label for="<?php echo $this->get_field_id ( 'item_'.$i.'_link' ); ?>"><?php esc_html_e ( 'Link URL:', 'simple-menu' ) ?>
<input type="url" name="<?php print esc_attr ( $this->get_field_name ( 'item_'.$i.'_link' ) ); ?>" id="<?php print esc_attr ( $this->get_field_id ( 'item_'.$i.'_link' ) ); ?>" class="widefat" value="<?php print esc_attr ( $item_link ); ?>" placeholder="<?php esc_attr_e ( 'Link URL', 'simple-menu' ) ?>">
</label>
</p>
<?php endfor; ?>
</table>
<?php
}
/**
* Update a particular instance
*
* This function should check that $new_instance is set correctly. The newly-calculated
* value of `$instance` should be returned. If false is returned, the instance won't be
* saved/updated.
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param array $new New settings for this instance as input by the user via {@see WP_Widget::form()}.
* @param array $old Old settings for this instance.
* @return array Settings to save or bool false to cancel saving.
*/
public function update ( $new, $old )
{
$instance = array ();
$instance[ 'title' ] = $new[ 'title' ];
$instance[ 'links_count' ] = filter_var ( (int)$new[ 'links_count' ], FILTER_VALIDATE_INT, array ( 'options' => array ( 'min_range' => 0, 'max_range' => 10, 'default' => 5 ) ) );
for ( $i = 0; $i < $old[ 'links_count' ]; $i++ ) {
$instance[ 'item_'.$i.'_title' ] = sanitize_text_field ( $new[ 'item_'.$i.'_title' ] );
$instance[ 'item_'.$i.'_link' ] = filter_var ( $new[ 'item_'.$i.'_link' ], FILTER_VALIDATE_URL );
}
return $instance;
}
/**
* Function used to register widget into WordPress
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param void
* @return void
*/
public static function register_widget ()
{
register_widget ( 'ScheRas\Plugins\Widgets\Simple_Menu_Widget' );
}
/**
* Displays widget title
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
* @param array $instance The settings for the particular instance of the widget.
* @return void
*/
protected function display_title ( $args, $instance )
{
?>
<?php if ( isset ( $instance[ 'title' ] ) && trim ( $instance[ 'title' ] ) !== '' ) : ?>
<?php print $args[ 'before_title' ]; ?>
<?php print esc_html ( trim ( $instance[ 'title' ] ) ) ?>
<?php print $args[ 'after_title' ]; ?>
<?php endif; ?>
<?php
}
/**
* Displays widget single item.
*
* @author Šimon Schierreich <admin@scheras.eu>
* @since 0.1
*
* @param string $title Single item title.
* @param string $link Single item URL.
* @return void
*/
protected function display_single_item ( $title, $link )
{
?>
<li>
<a href="<?php print esc_attr ( esc_url ( $link ) ); ?>" title="<?php print esc_attr ( $title ); ?>"><?php print esc_html ( $title ); ?></a>
</li>
<?php
}
}