balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Service_Sidebar_Menu.php (4955B)


      1 <?php
      2 namespace Welbim\Helper\Widgets;
      3 
      4 /**
      5  * Widget API: WP_Nav_Menu_Widget class
      6  *
      7  * @package    WordPress
      8  * @subpackage Widgets
      9  * @since      4.4.0
     10  */
     11 
     12 /**
     13  * Core class used to implement the Custom Menu widget.
     14  *
     15  * @since 3.0.0
     16  *
     17  * @see WP_Widget
     18  */
     19 class Service_Sidebar_Menu extends \WP_Widget
     20 {
     21 
     22 
     23 	/**
     24 	 * Sets up a new Custom Menu widget instance.
     25 	 *
     26 	 * @since  3.0.0
     27 	 * @access public
     28 	 */
     29 	public function __construct()
     30 	{
     31 		$widget_ops = array(
     32 			'classname'                   => 'categories-widget',
     33 			'description'                 => __('Add a Welbim Services custom menu to your sidebar.'),
     34 			'customize_selective_refresh' => true,
     35 		);
     36 		parent::__construct('Service_Sidebar_Menu', __('Welbim Service Menu'), $widget_ops);
     37 	}
     38 
     39 	/**
     40 	 * Outputs the content for the current Custom Menu widget instance.
     41 	 *
     42 	 * @since  3.0.0
     43 	 * @access public
     44 	 *
     45 	 * @param array $args     Display arguments including 'before_title', 'after_title',
     46 	 *                        'before_widget', and 'after_widget'.
     47 	 * @param array $instance Settings for the current Custom Menu widget instance.
     48 	 */
     49 	public function widget($args, $instance)
     50 	{
     51 		// Get menu
     52 		$nav_menu = !empty($instance['nav_menu']) ? wp_get_nav_menu_object($instance['nav_menu']) : false;
     53 
     54 		if (!$nav_menu) {
     55 			return;
     56 		}
     57 		/**
     58 		 * This filter is documented in wp-includes/widgets/class-wp-widget-pages.php
     59 		 */
     60 		$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
     61 		echo $args['before_widget'];
     62 		if ($title) {
     63 			printf($args['before_title'] . $title . $args['after_title']);
     64 		}
     65 
     66 		$menu_no = wp_get_nav_menu_items($nav_menu);
     67 
     68 		if (!empty($menu_no)) {
     69 
     70 			echo '<div class="category-widget-two"><ul class="widget cat-list">';
     71 
     72 			$primaryNav     	= wp_get_nav_menu_items($nav_menu); // Get the array of wp objects, the nav items for our queried location.
     73 			$queried_object 	= get_queried_object();
     74 			$object_post_id 	= $queried_object->ID;
     75 
     76 			foreach ($primaryNav as $navItem) {
     77 
     78 				$post_id = $navItem->object_id;
     79 
     80 				$post_object_id = $navItem->object_id;
     81 
     82 				if ($object_post_id == $post_object_id) {
     83 					echo '<li class="current active"><a href="' . $navItem->url . '" title="' . $navItem->title . '">' . $navItem->title . '</a></li>';
     84 				} else {
     85 					echo '<li><a href="' . $navItem->url . '" title="' . $navItem->title . '">' . $navItem->title . '</a></li>';
     86 				}
     87 			}
     88 
     89 			echo '</ul></div>';
     90 		}
     91 		echo $args['after_widget'];
     92 	}
     93 
     94 	public function update($new_instance, $old_instance)
     95 	{
     96 		$instance = array();
     97 		if (!empty($new_instance['title'])) {
     98 			$instance['title'] = sanitize_text_field($new_instance['title']);
     99 		}
    100 		if (!empty($new_instance['nav_menu'])) {
    101 			$instance['nav_menu'] = (int) $new_instance['nav_menu'];
    102 		}
    103 		return $instance;
    104 	}
    105 
    106 	public function form($instance)
    107 	{
    108 		global $wp_customize;
    109 		$title    	= isset($instance['title']) ? $instance['title'] : '';
    110 		$nav_menu 	= isset($instance['nav_menu']) ? $instance['nav_menu'] : '';
    111 		$menus 		= wp_get_nav_menus();
    112 		// If no menus exists, direct the user to go and create some.
    113 ?>
    114 		<p class="nav-menu-widget-no-menus-message" <?php
    115 													if (!empty($menus)) {
    116 														echo ' style="display:none" ';
    117 													}
    118 													?>>
    119 			<?php
    120 			if ($wp_customize instanceof WP_Customize_Manager) {
    121 				$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
    122 			} else {
    123 				$url = admin_url('nav-menus.php');
    124 			}
    125 			?>
    126 			<?php echo sprintf(__('No menus have been created yet. <a href="%s">Create some</a>.'), esc_attr($url)); ?>
    127 		</p>
    128 		<div class="nav-menu-widget-form-controls" <?php
    129 													if (empty($menus)) {
    130 														echo ' style="display:none" ';
    131 													}
    132 													?>>
    133 			<p>
    134 				<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    135 				<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>" />
    136 			</p>
    137 			<p>
    138 				<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
    139 				<select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
    140 					<option value="0"><?php _e('&mdash; Select &mdash;'); ?></option>
    141 					<?php foreach ($menus as $menu) : ?>
    142 						<option value="<?php echo esc_attr($menu->term_id); ?>" <?php selected($nav_menu, $menu->term_id); ?>>
    143 							<?php echo esc_html($menu->name); ?>
    144 						</option>
    145 					<?php endforeach; ?>
    146 				</select>
    147 			</p>
    148 			<?php if ($wp_customize instanceof WP_Customize_Manager) : ?>
    149 				<p class="edit-selected-nav-menu" style="
    150 			<?php
    151 				if (!$nav_menu) {
    152 					echo 'display: none;';
    153 				}
    154 			?>">
    155 					<button type="button" class="button">
    156 						<?php _e('Edit Menu'); ?>
    157 					</button>
    158 				</p>
    159 			<?php
    160 			endif;
    161 			?>
    162 		</div>
    163 <?php
    164 	}
    165 }