[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/widgets/ -> class-wp-nav-menu-widget.php (source)

   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          $default_title = __( 'Menu' );
  50          $title         = ! empty( $instance['title'] ) ? $instance['title'] : '';
  51  
  52          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  53          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  54  
  55          echo $args['before_widget'];
  56  
  57          if ( $title ) {
  58              echo $args['before_title'] . $title . $args['after_title'];
  59          }
  60  
  61          $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  62  
  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          }
  91  
  92          /**
  93           * Filters the arguments for the Navigation Menu widget.
  94           *
  95           * @since 4.2.0
  96           * @since 4.4.0 Added the `$instance` parameter.
  97           *
  98           * @param array   $nav_menu_args {
  99           *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
 100           *
 101           *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
 102           *     @type mixed         $menu        Menu ID, slug, or name.
 103           * }
 104           * @param WP_Term $nav_menu      Nav menu object for the current menu.
 105           * @param array   $args          Display arguments for the current widget.
 106           * @param array   $instance      Array of settings for the current widget.
 107           */
 108          wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
 109  
 110          echo $args['after_widget'];
 111      }
 112  
 113      /**
 114       * Handles updating settings for the current Navigation Menu widget instance.
 115       *
 116       * @since 3.0.0
 117       *
 118       * @param array $new_instance New settings for this instance as input by the user via
 119       *                            WP_Widget::form().
 120       * @param array $old_instance Old settings for this instance.
 121       * @return array Updated settings to save.
 122       */
 123  	public function update( $new_instance, $old_instance ) {
 124          $instance = array();
 125          if ( ! empty( $new_instance['title'] ) ) {
 126              $instance['title'] = sanitize_text_field( $new_instance['title'] );
 127          }
 128          if ( ! empty( $new_instance['nav_menu'] ) ) {
 129              $instance['nav_menu'] = (int) $new_instance['nav_menu'];
 130          }
 131          return $instance;
 132      }
 133  
 134      /**
 135       * Outputs the settings form for the Navigation Menu widget.
 136       *
 137       * @since 3.0.0
 138       *
 139       * @param array $instance Current settings.
 140       * @global WP_Customize_Manager $wp_customize
 141       */
 142  	public function form( $instance ) {
 143          global $wp_customize;
 144          $title    = isset( $instance['title'] ) ? $instance['title'] : '';
 145          $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
 146  
 147          // Get menus.
 148          $menus = wp_get_nav_menus();
 149  
 150          $empty_menus_style     = '';
 151          $not_empty_menus_style = '';
 152          if ( empty( $menus ) ) {
 153              $empty_menus_style = ' style="display:none" ';
 154          } else {
 155              $not_empty_menus_style = ' style="display:none" ';
 156          }
 157  
 158          $nav_menu_style = '';
 159          if ( ! $nav_menu ) {
 160              $nav_menu_style = 'display: none;';
 161          }
 162  
 163          // If no menus exists, direct the user to go and create some.
 164          ?>
 165          <p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>
 166              <?php
 167              if ( $wp_customize instanceof WP_Customize_Manager ) {
 168                  $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
 169              } else {
 170                  $url = admin_url( 'nav-menus.php' );
 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 ) );
 175              ?>
 176          </p>
 177          <div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
 178              <p>
 179                  <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 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 ); ?>"/>
 181              </p>
 182              <p>
 183                  <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
 184                  <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
 185                      <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
 186                      <?php foreach ( $menus as $menu ) : ?>
 187                          <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
 188                              <?php echo esc_html( $menu->name ); ?>
 189                          </option>
 190                      <?php endforeach; ?>
 191                  </select>
 192              </p>
 193              <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
 194                  <p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">
 195                      <button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>
 196                  </p>
 197              <?php endif; ?>
 198          </div>
 199          <?php
 200      }
 201  }


Generated: Thu Aug 13 01:00:02 2020 Cross-referenced by PHPXref 0.7.1