# essential-widgets/trunk/includes/widgets/class-ew-menus.php

Essential Widgets, version trunk. 336 lines.

- Page: https://pluginprobe.com/plugins/essential-widgets/trunk/code/includes/widgets/class-ew-menus.php
- Raw: https://pluginprobe.com/plugins/essential-widgets/trunk/raw/includes/widgets/class-ew-menus.php
- Modified: 2026-05-26T18:08:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/essential-widgets/trunk/code/includes/widgets/class-ew-menus.php#L10-L20`.

```php
<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

/**
 * Menus Widget
 *
 * @package Essential_Widgets
 */


/**
 * Makes a custom Widget for Displaying About
 *
 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
 *
 * @package Essential_Widgets
 */
if ( ! class_exists( 'EW_Menus' ) ) :
	class EW_Menus extends WP_Widget // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- EW_ is this plugin's abbreviated prefix; wrapped in class_exists() guard.
	{


		/**
		 * Holds widget settings defaults, populated in constructor.
		 *
		 * @var array
		 */
		protected $defaults;

		public function __construct() {
			$this->defaults = array(
				'title'           => esc_attr__( 'Navigation', 'essential-widgets' ),
				'menu'            => '',
				'container'       => 'div',
				'container_id'    => '',
				'container_class' => '',
				'menu_id'         => '',
				'menu_class'      => 'nav-menu',
				'depth'           => 0,
				'before'          => '',
				'after'           => '',
				'link_before'     => '',
				'link_after'      => '',
				'fallback_cb'     => 'wp_page_menu',
			);

			$widget_ops = array(
				'classname'   => 'essential-widgets ew-menus ewmenus widget_nav_menu',
				'description' => esc_html__( 'Displays a list of menus.', 'essential-widgets' ),
			);

			$control_ops = array(
				'id_base' => 'ew-menus',
			);

			parent::__construct(
				'ew-menus', // Base ID
				esc_html__( 'EW: Menus', 'essential-widgets' ), // Name
				$widget_ops,
				$control_ops
			);
		}

		/**
		 * Displays the widget control options in the Widgets admin screen.
		 *
		 * @since  1.0.0
		 * @access public
		 * @param  array $instance
		 * @param  void
		 */
		public function form( $instance ) {

			// Merge the user-selected arguments with the defaults.
			$instance = wp_parse_args( (array) $instance, $this->defaults );

			$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress hook, not plugin-defined.
			?>

			<p>
				<label>
					<?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
					<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'] ); ?>" />
				</label>
			</p>

			<p>
				<label>
					<?php esc_html_e( 'Menu:', 'essential-widgets' ); ?>

					<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu' ) ); ?>">
						<option value=""></option>

						<?php foreach ( wp_get_nav_menus() as $menu ) : ?>

							<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $instance['menu'], $menu->term_id ); ?>><?php echo esc_html( $menu->name ); ?></option>

						<?php endforeach; ?>

					</select>
				</label>
			</p>
			<p>
				<label>
					<?php esc_html_e( 'Container:', 'essential-widgets' ); ?>

					<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container' ) ); ?>">

						<?php foreach ( $container as $option ) : ?>

							<option value="<?php echo esc_attr( $option ); ?>" <?php selected( $instance['container'], $option ); ?>><?php echo esc_html( $option ); ?></option>

						<?php endforeach; ?>

					</select>
				</label>
			</p>

			<p>
				<label>
					<?php esc_html_e( 'Container ID:', 'essential-widgets' ); ?>
					<input
						type="text"
						class="widefat"
						name="<?php echo esc_attr( $this->get_field_name( 'container_id' ) ); ?>"
						value="<?php echo esc_attr( $instance['container_id'] ); ?>"
						placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
					/>
				</label>
			</p>

			<p>
				<label>
					<?php esc_html_e( 'Container Class:', 'essential-widgets' ); ?>
					<input
						type="text"
						class="widefat"
						name="<?php echo esc_attr( $this->get_field_name( 'container_class' ) ); ?>"
						value="<?php echo esc_attr( $instance['container_class'] ); ?>"
						placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
					/>
				</label>
			</p>

			<p>
				<label>
					<?php esc_html_e( 'Menu ID:', 'essential-widgets' ); ?>
					<input
						type="text"
						class="widefat"
						name="<?php echo esc_attr( $this->get_field_name( 'menu_id' ) ); ?>"
						value="<?php echo esc_attr( $instance['menu_id'] ); ?>"
						placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
					/>
				</label>
			</p>

			<p>
				<label>
					<?php esc_html_e( 'Menu Class:', 'essential-widgets' ); ?>
					<input
						type="text"
						class="widefat"
						name="<?php echo esc_attr( $this->get_field_name( 'menu_class' ) ); ?>"
						value="<?php echo esc_attr( $instance['menu_class'] ); ?>"
						placeholder="<?php echo esc_html__( 'example', 'essential-widgets' ); ?>"
					/>
				</label>
			</p>

			<p class="button-primary ect-toggle-btn more">
				<span class="ect-more-text"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></span>
				<span class="ect-hide-text"><?php esc_html_e( 'Hide Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-up"></i></span>

			<div class="advanced-section">

				<p>
					<label>
						<?php esc_html_e( 'Depth:', 'essential-widgets' ); ?>
						<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" />
					</label>
				</p>

				<p>
					<label>
						<?php esc_html_e( 'Before:', 'essential-widgets' ); ?>
						<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'before' ) ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" />
					</label>
				</p>

				<p>
					<label>
						<?php esc_html_e( 'After:', 'essential-widgets' ); ?>
						<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'after' ) ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" />
					</label>
				</p>

				<p>
					<label>
						<?php esc_html_e( 'Link Before:', 'essential-widgets' ); ?>
						<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_before' ) ); ?>" value="<?php echo esc_attr( $instance['link_before'] ); ?>" />
					</label>
				</p>

				<p>
					<label>
						<?php esc_html_e( 'Link After:', 'essential-widgets' ); ?>
						<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_after' ) ); ?>" value="<?php echo esc_attr( $instance['link_after'] ); ?>" />
					</label>
				</p>

				<p>
					<label>
						<?php esc_html_e( 'Fallback Callback Function:', 'essential-widgets' ); ?>
						<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" />
					</label>
				</p>

			</div><!-- .advanced-section -->

			<div style="clear:both;">&nbsp;</div>
			<?php
		}

		/**
		 * update the particular instant
		 *
		 * 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.
		 *
		 * $new_instance New settings for this instance as input by the user via form()
		 * $old_instance Old settings for this instance
		 * Settings to save or bool false to cancel saving
		 */
		public function update( $new_instance, $old_instance ) {
			$instance = $old_instance;

			// Sanitize title.
			$instance['title'] = sanitize_text_field( $new_instance['title'] );

			// Strip tags.
			$instance['menu'] = wp_strip_all_tags( $new_instance['menu'] );

			// Whitelist options.
			$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress hook, not plugin-defined.

			$instance['container'] = in_array( $new_instance['container'], $container, true ) ? $new_instance['container'] : 'div';

			// Integers.
			$instance['depth'] = absint( $new_instance['depth'] );

			// HTML class.
			$instance['container_id']    = sanitize_html_class( $new_instance['container_id'] );
			$instance['container_class'] = sanitize_html_class( $new_instance['container_class'] );
			$instance['menu_id']         = sanitize_html_class( $new_instance['menu_id'] );
			$instance['menu_class']      = sanitize_html_class( $new_instance['menu_class'] );

			// Text boxes. Make sure user can use 'unfiltered_html'.
			$instance['before']      = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] );
			$instance['after']       = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] );
			$instance['link_before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_before'] : wp_kses_post( $new_instance['link_before'] );
			$instance['link_after']  = current_user_can( 'unfiltered_html' ) ? $new_instance['link_after'] : wp_kses_post( $new_instance['link_after'] );

			// Function name.
			$instance['fallback_cb'] = empty( $new_instance['fallback_cb'] ) || function_exists( $new_instance['fallback_cb'] ) ? $new_instance['fallback_cb'] : 'wp_page_menu';

			// Return sanitized options.
			return $instance;
		}

		/**
		 * Displays the Widget in the front-end.
		 *
		 * $args Display arguments including before_title, after_title, before_widget, and after_widget.
		 * $instance The settings for the particular instance of the widget
		 */
		public function widget( $args, $instance ) {
			// Merge instance with defaults.
			$instance = wp_parse_args( $instance, $this->defaults );

			// Escape widget wrapper attributes.
			echo wp_kses_post( $args['before_widget'] );

			// If a title was input by the user, display it safely.
			if ( ! empty( $instance['title'] ) ) {
				echo wp_kses_post( $args['before_title'] );

				// Apply filters to title, then escape it for output
				$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
				echo esc_html( $title );

				echo wp_kses_post( $args['after_title'] );
			}

			// Output the main content of the widget (shortcode output)
			echo wp_kses_post( $this->shortcode( $instance ) );

			// Close the widget wrapper safely.
			echo wp_kses_post( $args['after_widget'] );
		}

		public function shortcode( $atts ) {
			// Overwrite the $echo argument and set it to false.
			$atts['echo'] = false;

			$ew_menu = '';

			// Only show this title in block element and not on widget.
			if ( isset( $atts['is_block'] ) && true === $atts['is_block'] && !empty( $atts['title'] ) ) {
				$ew_menu .= '<h2 class="ew-menu-block-title">' . esc_html( $atts['title'] ) . '</h2>';
			}

			// Output the nav menu.
			$ew_menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_nav_menu( $atts ) );

			return $ew_menu;
		}
	}
endif;

if ( ! function_exists( 'ew_menu_register' ) ) :
	/**
	 * Intiate Menu_Widget Class.
	 *
	 * @since 1.0.0
	 */
	function ew_menu_register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix.
		register_widget( 'EW_Menus' );
	}
	add_action( 'widgets_init', 'ew_menu_register' );
endif;

```
