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