wp/wp-includes/widgets/class-wp-nav-menu-widget.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     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 		);
       
    29 		parent::__construct( 'nav_menu', __('Navigation Menu'), $widget_ops );
       
    30 	}
       
    31 
       
    32 	/**
       
    33 	 * Outputs the content for the current Navigation Menu widget instance.
       
    34 	 *
       
    35 	 * @since 3.0.0
       
    36 	 *
       
    37 	 * @param array $args     Display arguments including 'before_title', 'after_title',
       
    38 	 *                        'before_widget', and 'after_widget'.
       
    39 	 * @param array $instance Settings for the current Navigation Menu widget instance.
       
    40 	 */
       
    41 	public function widget( $args, $instance ) {
       
    42 		// Get menu
       
    43 		$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
       
    44 
       
    45 		if ( ! $nav_menu ) {
       
    46 			return;
       
    47 		}
       
    48 
       
    49 		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
       
    50 
       
    51 		/** 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 
       
    54 		echo $args['before_widget'];
       
    55 
       
    56 		if ( $title ) {
       
    57 			echo $args['before_title'] . $title . $args['after_title'];
       
    58 		}
       
    59 
       
    60 		$nav_menu_args = array(
       
    61 			'fallback_cb' => '',
       
    62 			'menu'        => $nav_menu
       
    63 		);
       
    64 
       
    65 		/**
       
    66 		 * Filters the arguments for the Navigation Menu widget.
       
    67 		 *
       
    68 		 * @since 4.2.0
       
    69 		 * @since 4.4.0 Added the `$instance` parameter.
       
    70 		 *
       
    71 		 * @param array    $nav_menu_args {
       
    72 		 *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
       
    73 		 *
       
    74 		 *     @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.
       
    76 		 * }
       
    77 		 * @param WP_Term  $nav_menu      Nav menu object for the current menu.
       
    78 		 * @param array    $args          Display arguments for the current widget.
       
    79 		 * @param array    $instance      Array of settings for the current widget.
       
    80 		 */
       
    81 		wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
       
    82 
       
    83 		echo $args['after_widget'];
       
    84 	}
       
    85 
       
    86 	/**
       
    87 	 * Handles updating settings for the current Navigation Menu widget instance.
       
    88 	 *
       
    89 	 * @since 3.0.0
       
    90 	 *
       
    91 	 * @param array $new_instance New settings for this instance as input by the user via
       
    92 	 *                            WP_Widget::form().
       
    93 	 * @param array $old_instance Old settings for this instance.
       
    94 	 * @return array Updated settings to save.
       
    95 	 */
       
    96 	public function update( $new_instance, $old_instance ) {
       
    97 		$instance = array();
       
    98 		if ( ! empty( $new_instance['title'] ) ) {
       
    99 			$instance['title'] = sanitize_text_field( $new_instance['title'] );
       
   100 		}
       
   101 		if ( ! empty( $new_instance['nav_menu'] ) ) {
       
   102 			$instance['nav_menu'] = (int) $new_instance['nav_menu'];
       
   103 		}
       
   104 		return $instance;
       
   105 	}
       
   106 
       
   107 	/**
       
   108 	 * Outputs the settings form for the Navigation Menu widget.
       
   109 	 *
       
   110 	 * @since 3.0.0
       
   111 	 *
       
   112 	 * @param array $instance Current settings.
       
   113 	 * @global WP_Customize_Manager $wp_customize
       
   114 	 */
       
   115 	public function form( $instance ) {
       
   116 		global $wp_customize;
       
   117 		$title = isset( $instance['title'] ) ? $instance['title'] : '';
       
   118 		$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
       
   119 
       
   120 		// Get menus
       
   121 		$menus = wp_get_nav_menus();
       
   122 
       
   123 		// If no menus exists, direct the user to go and create some.
       
   124 		?>
       
   125 		<p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
       
   126 			<?php
       
   127 			if ( $wp_customize instanceof WP_Customize_Manager ) {
       
   128 				$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
       
   129 			} else {
       
   130 				$url = admin_url( 'nav-menus.php' );
       
   131 			}
       
   132 			?>
       
   133 			<?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
       
   134 		</p>
       
   135 		<div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
       
   136 			<p>
       
   137 				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
       
   138 				<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 ); ?>"/>
       
   139 			</p>
       
   140 			<p>
       
   141 				<label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
       
   142 				<select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
       
   143 					<option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
       
   144 					<?php foreach ( $menus as $menu ) : ?>
       
   145 						<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
       
   146 							<?php echo esc_html( $menu->name ); ?>
       
   147 						</option>
       
   148 					<?php endforeach; ?>
       
   149 				</select>
       
   150 			</p>
       
   151 			<?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
       
   152 				<p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>">
       
   153 					<button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button>
       
   154 				</p>
       
   155 			<?php endif; ?>
       
   156 		</div>
       
   157 		<?php
       
   158 	}
       
   159 }