wp/wp-includes/widgets/class-wp-nav-menu-widget.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    37 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    37 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    38 	 *                        'before_widget', and 'after_widget'.
    38 	 *                        'before_widget', and 'after_widget'.
    39 	 * @param array $instance Settings for the current Navigation Menu widget instance.
    39 	 * @param array $instance Settings for the current Navigation Menu widget instance.
    40 	 */
    40 	 */
    41 	public function widget( $args, $instance ) {
    41 	public function widget( $args, $instance ) {
    42 		// Get menu
    42 		// Get menu.
    43 		$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
    43 		$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
    44 
    44 
    45 		if ( ! $nav_menu ) {
    45 		if ( ! $nav_menu ) {
    46 			return;
    46 			return;
    47 		}
    47 		}
    48 
    48 
    49 		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
    49 		$default_title = __( 'Menu' );
       
    50 		$title         = ! empty( $instance['title'] ) ? $instance['title'] : '';
    50 
    51 
    51 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    52 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    52 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    53 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    53 
    54 
    54 		echo $args['before_widget'];
    55 		echo $args['before_widget'];
    55 
    56 
    56 		if ( $title ) {
    57 		if ( $title ) {
    57 			echo $args['before_title'] . $title . $args['after_title'];
    58 			echo $args['before_title'] . $title . $args['after_title'];
    58 		}
    59 		}
    59 
    60 
    60 		$nav_menu_args = array(
    61 		$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
    61 			'fallback_cb' => '',
    62 
    62 			'menu'        => $nav_menu,
    63 		/**
    63 		);
    64 		 * Filters the HTML format of widgets with navigation links.
       
    65 		 *
       
    66 		 * @since 5.5.0
       
    67 		 *
       
    68 		 * @param string $format The type of markup to use in widgets with navigation links.
       
    69 		 *                       Accepts 'html5', 'xhtml'.
       
    70 		 */
       
    71 		$format = apply_filters( 'navigation_widgets_format', $format );
       
    72 
       
    73 		if ( 'html5' === $format ) {
       
    74 			// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
       
    75 			$title      = trim( strip_tags( $title ) );
       
    76 			$aria_label = $title ? $title : $default_title;
       
    77 
       
    78 			$nav_menu_args = array(
       
    79 				'fallback_cb'          => '',
       
    80 				'menu'                 => $nav_menu,
       
    81 				'container'            => 'nav',
       
    82 				'container_aria_label' => $aria_label,
       
    83 				'items_wrap'           => '<ul id="%1$s" class="%2$s">%3$s</ul>',
       
    84 			);
       
    85 		} else {
       
    86 			$nav_menu_args = array(
       
    87 				'fallback_cb' => '',
       
    88 				'menu'        => $nav_menu,
       
    89 			);
       
    90 		}
    64 
    91 
    65 		/**
    92 		/**
    66 		 * Filters the arguments for the Navigation Menu widget.
    93 		 * Filters the arguments for the Navigation Menu widget.
    67 		 *
    94 		 *
    68 		 * @since 4.2.0
    95 		 * @since 4.2.0
    69 		 * @since 4.4.0 Added the `$instance` parameter.
    96 		 * @since 4.4.0 Added the `$instance` parameter.
    70 		 *
    97 		 *
    71 		 * @param array    $nav_menu_args {
    98 		 * @param array   $nav_menu_args {
    72 		 *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
    99 		 *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
    73 		 *
   100 		 *
    74 		 *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
   101 		 *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
    75 		 *     @type mixed         $menu        Menu ID, slug, or name.
   102 		 *     @type mixed         $menu        Menu ID, slug, or name.
    76 		 * }
   103 		 * }
    77 		 * @param WP_Term  $nav_menu      Nav menu object for the current menu.
   104 		 * @param WP_Term $nav_menu      Nav menu object for the current menu.
    78 		 * @param array    $args          Display arguments for the current widget.
   105 		 * @param array   $args          Display arguments for the current widget.
    79 		 * @param array    $instance      Array of settings for the current widget.
   106 		 * @param array   $instance      Array of settings for the current widget.
    80 		 */
   107 		 */
    81 		wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
   108 		wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
    82 
   109 
    83 		echo $args['after_widget'];
   110 		echo $args['after_widget'];
    84 	}
   111 	}
   115 	public function form( $instance ) {
   142 	public function form( $instance ) {
   116 		global $wp_customize;
   143 		global $wp_customize;
   117 		$title    = isset( $instance['title'] ) ? $instance['title'] : '';
   144 		$title    = isset( $instance['title'] ) ? $instance['title'] : '';
   118 		$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
   145 		$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
   119 
   146 
   120 		// Get menus
   147 		// Get menus.
   121 		$menus = wp_get_nav_menus();
   148 		$menus = wp_get_nav_menus();
   122 
   149 
   123 		$empty_menus_style = $not_empty_menus_style = '';
   150 		$empty_menus_style     = '';
       
   151 		$not_empty_menus_style = '';
   124 		if ( empty( $menus ) ) {
   152 		if ( empty( $menus ) ) {
   125 			$empty_menus_style = ' style="display:none" ';
   153 			$empty_menus_style = ' style="display:none" ';
   126 		} else {
   154 		} else {
   127 			$not_empty_menus_style = ' style="display:none" ';
   155 			$not_empty_menus_style = ' style="display:none" ';
   128 		}
   156 		}
   139 			if ( $wp_customize instanceof WP_Customize_Manager ) {
   167 			if ( $wp_customize instanceof WP_Customize_Manager ) {
   140 				$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
   168 				$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
   141 			} else {
   169 			} else {
   142 				$url = admin_url( 'nav-menus.php' );
   170 				$url = admin_url( 'nav-menus.php' );
   143 			}
   171 			}
       
   172 
       
   173 			/* translators: %s: URL to create a new menu. */
       
   174 			printf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) );
   144 			?>
   175 			?>
   145 			<?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
       
   146 		</p>
   176 		</p>
   147 		<div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
   177 		<div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
   148 			<p>
   178 			<p>
   149 				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   179 				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   150 				<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 ); ?>"/>
   180 				<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 ); ?>"/>