[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Widget API: WP_Nav_Menu_Widget class 4 * 5 * @package WordPress 6 * @subpackage Widgets 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement the Navigation Menu widget. 12 * 13 * @since 3.0.0 14 * 15 * @see WP_Widget 16 */ 17 class WP_Nav_Menu_Widget extends WP_Widget { 18 19 /** 20 * Sets up a new Navigation Menu widget instance. 21 * 22 * @since 3.0.0 23 */ 24 public function __construct() { 25 $widget_ops = array( 26 'description' => __( 'Add a navigation menu to your sidebar.' ), 27 'customize_selective_refresh' => true, 28 'show_instance_in_rest' => true, 29 ); 30 parent::__construct( 'nav_menu', __( 'Navigation Menu' ), $widget_ops ); 31 } 32 33 /** 34 * Outputs the content for the current Navigation Menu widget instance. 35 * 36 * @since 3.0.0 37 * 38 * @param array $args Display arguments including 'before_title', 'after_title', 39 * 'before_widget', and 'after_widget'. 40 * @param array $instance Settings for the current Navigation Menu widget instance. 41 */ 42 public function widget( $args, $instance ) { 43 // Get menu. 44 $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false; 45 46 if ( ! $nav_menu ) { 47 return; 48 } 49 50 $default_title = __( 'Menu' ); 51 $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; 52 53 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 54 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 55 56 echo $args['before_widget']; 57 58 if ( $title ) { 59 echo $args['before_title'] . $title . $args['after_title']; 60 } 61 62 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 63 64 /** 65 * Filters the HTML format of widgets with navigation links. 66 * 67 * @since 5.5.0 68 * 69 * @param string $format The type of markup to use in widgets with navigation links. 70 * Accepts 'html5', 'xhtml'. 71 */ 72 $format = apply_filters( 'navigation_widgets_format', $format ); 73 74 if ( 'html5' === $format ) { 75 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 76 $title = trim( strip_tags( $title ) ); 77 $aria_label = $title ? $title : $default_title; 78 79 $nav_menu_args = array( 80 'fallback_cb' => '', 81 'menu' => $nav_menu, 82 'container' => 'nav', 83 'container_aria_label' => $aria_label, 84 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 85 ); 86 } else { 87 $nav_menu_args = array( 88 'fallback_cb' => '', 89 'menu' => $nav_menu, 90 ); 91 } 92 93 /** 94 * Filters the arguments for the Navigation Menu widget. 95 * 96 * @since 4.2.0 97 * @since 4.4.0 Added the `$instance` parameter. 98 * 99 * @param array $nav_menu_args { 100 * An array of arguments passed to wp_nav_menu() to retrieve a navigation menu. 101 * 102 * @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty. 103 * @type mixed $menu Menu ID, slug, or name. 104 * } 105 * @param WP_Term $nav_menu Nav menu object for the current menu. 106 * @param array $args Display arguments for the current widget. 107 * @param array $instance Array of settings for the current widget. 108 */ 109 wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) ); 110 111 echo $args['after_widget']; 112 } 113 114 /** 115 * Handles updating settings for the current Navigation Menu widget instance. 116 * 117 * @since 3.0.0 118 * 119 * @param array $new_instance New settings for this instance as input by the user via 120 * WP_Widget::form(). 121 * @param array $old_instance Old settings for this instance. 122 * @return array Updated settings to save. 123 */ 124 public function update( $new_instance, $old_instance ) { 125 $instance = array(); 126 if ( ! empty( $new_instance['title'] ) ) { 127 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 128 } 129 if ( ! empty( $new_instance['nav_menu'] ) ) { 130 $instance['nav_menu'] = (int) $new_instance['nav_menu']; 131 } 132 return $instance; 133 } 134 135 /** 136 * Outputs the settings form for the Navigation Menu widget. 137 * 138 * @since 3.0.0 139 * 140 * @global WP_Customize_Manager $wp_customize 141 * 142 * @param array $instance Current settings. 143 */ 144 public function form( $instance ) { 145 global $wp_customize; 146 $title = isset( $instance['title'] ) ? $instance['title'] : ''; 147 $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : ''; 148 149 // Get menus. 150 $menus = wp_get_nav_menus(); 151 152 $empty_menus_style = ''; 153 $not_empty_menus_style = ''; 154 if ( empty( $menus ) ) { 155 $empty_menus_style = ' style="display:none" '; 156 } else { 157 $not_empty_menus_style = ' style="display:none" '; 158 } 159 160 $nav_menu_style = ''; 161 if ( ! $nav_menu ) { 162 $nav_menu_style = 'display: none;'; 163 } 164 165 // If no menus exists, direct the user to go and create some. 166 ?> 167 <p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>> 168 <?php 169 if ( $wp_customize instanceof WP_Customize_Manager ) { 170 $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();'; 171 } else { 172 $url = admin_url( 'nav-menus.php' ); 173 } 174 175 printf( 176 /* translators: %s: URL to create a new menu. */ 177 __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), 178 // The URL can be a `javascript:` link, so esc_attr() is used here instead of esc_url(). 179 esc_attr( $url ) 180 ); 181 ?> 182 </p> 183 <div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>> 184 <p> 185 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 186 <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 ); ?>" /> 187 </p> 188 <p> 189 <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label> 190 <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>"> 191 <option value="0"><?php _e( '— Select —' ); ?></option> 192 <?php foreach ( $menus as $menu ) : ?> 193 <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>> 194 <?php echo esc_html( $menu->name ); ?> 195 </option> 196 <?php endforeach; ?> 197 </select> 198 </p> 199 <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?> 200 <p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>"> 201 <button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button> 202 </p> 203 <?php endif; ?> 204 </div> 205 <?php 206 } 207 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |