]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-nav-menu-widget.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-nav-menu-widget.php
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 Custom 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 Custom Menu widget instance.
21          *
22          * @since 3.0.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array(
27                         'description' => __( 'Add a custom menu to your sidebar.' ),
28                         'customize_selective_refresh' => true,
29                 );
30                 parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
31         }
32
33         /**
34          * Outputs the content for the current Custom Menu widget instance.
35          *
36          * @since 3.0.0
37          * @access public
38          *
39          * @param array $args     Display arguments including 'before_title', 'after_title',
40          *                        'before_widget', and 'after_widget'.
41          * @param array $instance Settings for the current Custom Menu widget instance.
42          */
43         public function widget( $args, $instance ) {
44                 // Get menu
45                 $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
46
47                 if ( !$nav_menu )
48                         return;
49
50                 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
51                 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
52
53                 echo $args['before_widget'];
54
55                 if ( !empty($instance['title']) )
56                         echo $args['before_title'] . $instance['title'] . $args['after_title'];
57
58                 $nav_menu_args = array(
59                         'fallback_cb' => '',
60                         'menu'        => $nav_menu
61                 );
62
63                 /**
64                  * Filter the arguments for the Custom Menu widget.
65                  *
66                  * @since 4.2.0
67                  * @since 4.4.0 Added the `$instance` parameter.
68                  *
69                  * @param array    $nav_menu_args {
70                  *     An array of arguments passed to wp_nav_menu() to retrieve a custom menu.
71                  *
72                  *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
73                  *     @type mixed         $menu        Menu ID, slug, or name.
74                  * }
75                  * @param stdClass $nav_menu      Nav menu object for the current menu.
76                  * @param array    $args          Display arguments for the current widget.
77                  * @param array    $instance      Array of settings for the current widget.
78                  */
79                 wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
80
81                 echo $args['after_widget'];
82         }
83
84         /**
85          * Handles updating settings for the current Custom Menu widget instance.
86          *
87          * @since 3.0.0
88          * @access public
89          *
90          * @param array $new_instance New settings for this instance as input by the user via
91          *                            WP_Widget::form().
92          * @param array $old_instance Old settings for this instance.
93          * @return array Updated settings to save.
94          */
95         public function update( $new_instance, $old_instance ) {
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         /**
107          * Outputs the settings form for the Custom Menu widget.
108          *
109          * @since 3.0.0
110          * @access public
111          *
112          * @param array $instance Current settings.
113          */
114         public function form( $instance ) {
115                 $title = isset( $instance['title'] ) ? $instance['title'] : '';
116                 $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
117
118                 // Get menus
119                 $menus = wp_get_nav_menus();
120
121                 // If no menus exists, direct the user to go and create some.
122                 ?>
123                 <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
124                         <?php
125                         if ( isset( $GLOBALS['wp_customize'] ) && $GLOBALS['wp_customize'] instanceof WP_Customize_Manager ) {
126                                 $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
127                         } else {
128                                 $url = admin_url( 'nav-menus.php' );
129                         }
130                         ?>
131                         <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
132                 </p>
133                 <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
134                         <p>
135                                 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
136                                 <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 ); ?>"/>
137                         </p>
138                         <p>
139                                 <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
140                                 <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
141                                         <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
142                                         <?php foreach ( $menus as $menu ) : ?>
143                                                 <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
144                                                         <?php echo esc_html( $menu->name ); ?>
145                                                 </option>
146                                         <?php endforeach; ?>
147                                 </select>
148                         </p>
149                 </div>
150                 <?php
151         }
152 }