PluginProbe
Essential Widgets / 3.2
Essential Widgets v3.2
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / widgets / class-ew-menus.php

class-ew-menus.php in Essential Widgets 3.2, at includes/widgets/class-ew-menus.php

336 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 /**
8 * Menus Widget
9 *
10 * @package Essential_Widgets
11 */
12
13
14 /**
15 * Makes a custom Widget for Displaying About
16 *
17 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
18 *
19 * @package Essential_Widgets
20 */
21 if ( ! class_exists( 'EW_Menus' ) ) :
22 class EW_Menus extends WP_Widget // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- EW_ is this plugin's abbreviated prefix; wrapped in class_exists() guard.
23 {
24
25
26 /**
27 * Holds widget settings defaults, populated in constructor.
28 *
29 * @var array
30 */
31 protected $defaults;
32
33 public function __construct() {
34 $this->defaults = array(
35 'title' => esc_attr__( 'Navigation', 'essential-widgets' ),
36 'menu' => '',
37 'container' => 'div',
38 'container_id' => '',
39 'container_class' => '',
40 'menu_id' => '',
41 'menu_class' => 'nav-menu',
42 'depth' => 0,
43 'before' => '',
44 'after' => '',
45 'link_before' => '',
46 'link_after' => '',
47 'fallback_cb' => 'wp_page_menu',
48 );
49
50 $widget_ops = array(
51 'classname' => 'essential-widgets ew-menus ewmenus widget_nav_menu',
52 'description' => esc_html__( 'Displays a list of menus.', 'essential-widgets' ),
53 );
54
55 $control_ops = array(
56 'id_base' => 'ew-menus',
57 );
58
59 parent::__construct(
60 'ew-menus', // Base ID
61 esc_html__( 'EW: Menus', 'essential-widgets' ), // Name
62 $widget_ops,
63 $control_ops
64 );
65 }
66
67 /**
68 * Displays the widget control options in the Widgets admin screen.
69 *
70 * @since 1.0.0
71 * @access public
72 * @param array $instance
73 * @param void
74 */
75 public function form( $instance ) {
76
77 // Merge the user-selected arguments with the defaults.
78 $instance = wp_parse_args( (array) $instance, $this->defaults );
79
80 $container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress hook, not plugin-defined.
81 ?>
82
83 <p>
84 <label>
85 <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
86 <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'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" />
87 </label>
88 </p>
89
90 <p>
91 <label>
92 <?php esc_html_e( 'Menu:', 'essential-widgets' ); ?>
93
94 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu' ) ); ?>">
95 <option value=""></option>
96
97 <?php foreach ( wp_get_nav_menus() as $menu ) : ?>
98
99 <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $instance['menu'], $menu->term_id ); ?>><?php echo esc_html( $menu->name ); ?></option>
100
101 <?php endforeach; ?>
102
103 </select>
104 </label>
105 </p>
106 <p>
107 <label>
108 <?php esc_html_e( 'Container:', 'essential-widgets' ); ?>
109
110 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container' ) ); ?>">
111
112 <?php foreach ( $container as $option ) : ?>
113
114 <option value="<?php echo esc_attr( $option ); ?>" <?php selected( $instance['container'], $option ); ?>><?php echo esc_html( $option ); ?></option>
115
116 <?php endforeach; ?>
117
118 </select>
119 </label>
120 </p>
121
122 <p>
123 <label>
124 <?php esc_html_e( 'Container ID:', 'essential-widgets' ); ?>
125 <input
126 type="text"
127 class="widefat"
128 name="<?php echo esc_attr( $this->get_field_name( 'container_id' ) ); ?>"
129 value="<?php echo esc_attr( $instance['container_id'] ); ?>"
130 placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
131 />
132 </label>
133 </p>
134
135 <p>
136 <label>
137 <?php esc_html_e( 'Container Class:', 'essential-widgets' ); ?>
138 <input
139 type="text"
140 class="widefat"
141 name="<?php echo esc_attr( $this->get_field_name( 'container_class' ) ); ?>"
142 value="<?php echo esc_attr( $instance['container_class'] ); ?>"
143 placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
144 />
145 </label>
146 </p>
147
148 <p>
149 <label>
150 <?php esc_html_e( 'Menu ID:', 'essential-widgets' ); ?>
151 <input
152 type="text"
153 class="widefat"
154 name="<?php echo esc_attr( $this->get_field_name( 'menu_id' ) ); ?>"
155 value="<?php echo esc_attr( $instance['menu_id'] ); ?>"
156 placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
157 />
158 </label>
159 </p>
160
161 <p>
162 <label>
163 <?php esc_html_e( 'Menu Class:', 'essential-widgets' ); ?>
164 <input
165 type="text"
166 class="widefat"
167 name="<?php echo esc_attr( $this->get_field_name( 'menu_class' ) ); ?>"
168 value="<?php echo esc_attr( $instance['menu_class'] ); ?>"
169 placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
170 />
171 </label>
172 </p>
173
174 <p class="button-primary ect-toggle-btn more">
175 <span class="ect-more-text"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></span>
176 <span class="ect-hide-text"><?php esc_html_e( 'Hide Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-up"></i></span>
177
178 <div class="advanced-section">
179
180 <p>
181 <label>
182 <?php esc_html_e( 'Depth:', 'essential-widgets' ); ?>
183 <input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>" value="<?php echo esc_attr( $instance['depth'] ); ?>" placeholder="0" />
184 </label>
185 </p>
186
187 <p>
188 <label>
189 <?php esc_html_e( 'Before:', 'essential-widgets' ); ?>
190 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'before' ) ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" />
191 </label>
192 </p>
193
194 <p>
195 <label>
196 <?php esc_html_e( 'After:', 'essential-widgets' ); ?>
197 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'after' ) ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" />
198 </label>
199 </p>
200
201 <p>
202 <label>
203 <?php esc_html_e( 'Link Before:', 'essential-widgets' ); ?>
204 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_before' ) ); ?>" value="<?php echo esc_attr( $instance['link_before'] ); ?>" />
205 </label>
206 </p>
207
208 <p>
209 <label>
210 <?php esc_html_e( 'Link After:', 'essential-widgets' ); ?>
211 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_after' ) ); ?>" value="<?php echo esc_attr( $instance['link_after'] ); ?>" />
212 </label>
213 </p>
214
215 <p>
216 <label>
217 <?php esc_html_e( 'Fallback Callback Function:', 'essential-widgets' ); ?>
218 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'fallback_cb' ) ); ?>" value="<?php echo esc_attr( $instance['fallback_cb'] ); ?>" placeholder="wp_page_menu" />
219 </label>
220 </p>
221
222 </div><!-- .advanced-section -->
223
224 <div style="clear:both;">&nbsp;</div>
225 <?php
226 }
227
228 /**
229 * update the particular instant
230 *
231 * This function should check that $new_instance is set correctly.
232 * The newly calculated value of $instance should be returned.
233 * If "false" is returned, the instance won't be saved/updated.
234 *
235 * $new_instance New settings for this instance as input by the user via form()
236 * $old_instance Old settings for this instance
237 * Settings to save or bool false to cancel saving
238 */
239 public function update( $new_instance, $old_instance ) {
240 $instance = $old_instance;
241
242 // Sanitize title.
243 $instance['title'] = sanitize_text_field( $new_instance['title'] );
244
245 // Strip tags.
246 $instance['menu'] = wp_strip_all_tags( $new_instance['menu'] );
247
248 // Whitelist options.
249 $container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress hook, not plugin-defined.
250
251 $instance['container'] = in_array( $new_instance['container'], $container, true ) ? $new_instance['container'] : 'div';
252
253 // Integers.
254 $instance['depth'] = absint( $new_instance['depth'] );
255
256 // HTML class.
257 $instance['container_id'] = sanitize_html_class( $new_instance['container_id'] );
258 $instance['container_class'] = sanitize_html_class( $new_instance['container_class'] );
259 $instance['menu_id'] = sanitize_html_class( $new_instance['menu_id'] );
260 $instance['menu_class'] = sanitize_html_class( $new_instance['menu_class'] );
261
262 // Text boxes. Make sure user can use 'unfiltered_html'.
263 $instance['before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] );
264 $instance['after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] );
265 $instance['link_before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_before'] : wp_kses_post( $new_instance['link_before'] );
266 $instance['link_after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_after'] : wp_kses_post( $new_instance['link_after'] );
267
268 // Function name.
269 $instance['fallback_cb'] = empty( $new_instance['fallback_cb'] ) || function_exists( $new_instance['fallback_cb'] ) ? $new_instance['fallback_cb'] : 'wp_page_menu';
270
271 // Return sanitized options.
272 return $instance;
273 }
274
275 /**
276 * Displays the Widget in the front-end.
277 *
278 * $args Display arguments including before_title, after_title, before_widget, and after_widget.
279 * $instance The settings for the particular instance of the widget
280 */
281 public function widget( $args, $instance ) {
282 // Merge instance with defaults.
283 $instance = wp_parse_args( $instance, $this->defaults );
284
285 // Escape widget wrapper attributes.
286 echo wp_kses_post( $args['before_widget'] );
287
288 // If a title was input by the user, display it safely.
289 if ( ! empty( $instance['title'] ) ) {
290 echo wp_kses_post( $args['before_title'] );
291
292 // Apply filters to title, then escape it for output
293 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
294 echo esc_html( $title );
295
296 echo wp_kses_post( $args['after_title'] );
297 }
298
299 // Output the main content of the widget (shortcode output)
300 echo wp_kses_post( $this->shortcode( $instance ) );
301
302 // Close the widget wrapper safely.
303 echo wp_kses_post( $args['after_widget'] );
304 }
305
306 public function shortcode( $atts ) {
307 // Overwrite the $echo argument and set it to false.
308 $atts['echo'] = false;
309
310 $ew_menu = '';
311
312 // Only show this title in block element and not on widget.
313 if ( isset( $atts['is_block'] ) && true === $atts['is_block'] && !empty( $atts['title'] ) ) {
314 $ew_menu .= '<h2 class="ew-menu-block-title">' . esc_html( $atts['title'] ) . '</h2>';
315 }
316
317 // Output the nav menu.
318 $ew_menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_nav_menu( $atts ) );
319
320 return $ew_menu;
321 }
322 }
323 endif;
324
325 if ( ! function_exists( 'ew_menu_register' ) ) :
326 /**
327 * Intiate Menu_Widget Class.
328 *
329 * @since 1.0.0
330 */
331 function ew_menu_register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix.
332 register_widget( 'EW_Menus' );
333 }
334 add_action( 'widgets_init', 'ew_menu_register' );
335 endif;
336