[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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              'show_instance_in_rest'       => true,
  29          );
  30          parent::__construct( 'nav_menu', __( 'Navigation Menu' ), $widget_ops );
  31      }
  32  
  33      /**
  34       * Outputs the content for the current Navigation Menu widget instance.
  35       *
  36       * @since 3.0.0
  37       *
  38       * @param array $args     Display arguments including 'before_title', 'after_title',
  39       *                        'before_widget', and 'after_widget'.
  40       * @param array $instance Settings for the current Navigation Menu widget instance.
  41       */
  42  	public function widget( $args, $instance ) {
  43          // Get menu.
  44          $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
  45  
  46          if ( ! $nav_menu ) {
  47              return;
  48          }
  49  
  50          $default_title = __( 'Menu' );
  51          $title         = ! empty( $instance['title'] ) ? $instance['title'] : '';
  52  
  53          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  54          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  55  
  56          echo $args['before_widget'];
  57  
  58          if ( $title ) {
  59              echo $args['before_title'] . $title . $args['after_title'];
  60          }
  61  
  62          $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  63  
  64          /**
  65           * Filters the HTML format of widgets with navigation links.
  66           *
  67           * @since 5.5.0
  68           *
  69           * @param string $format The type of markup to use in widgets with navigation links.
  70           *                       Accepts 'html5', 'xhtml'.
  71           */
  72          $format = apply_filters( 'navigation_widgets_format', $format );
  73  
  74          if ( 'html5' === $format ) {
  75              // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  76              $title      = trim( strip_tags( $title ) );
  77              $aria_label = $title ? $title : $default_title;
  78  
  79              $nav_menu_args = array(
  80                  'fallback_cb'          => '',
  81                  'menu'                 => $nav_menu,
  82                  'container'            => 'nav',
  83                  'container_aria_label' => $aria_label,
  84                  'items_wrap'           => '<ul id="%1$s" class="%2$s">%3$s</ul>',
  85              );
  86          } else {
  87              $nav_menu_args = array(
  88                  'fallback_cb' => '',
  89                  'menu'        => $nav_menu,
  90              );
  91          }
  92  
  93          /**
  94           * Filters the arguments for the Navigation Menu widget.
  95           *
  96           * @since 4.2.0
  97           * @since 4.4.0 Added the `$instance` parameter.
  98           *
  99           * @param array   $nav_menu_args {
 100           *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
 101           *
 102           *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
 103           *     @type mixed         $menu        Menu ID, slug, or name.
 104           * }
 105           * @param WP_Term $nav_menu      Nav menu object for the current menu.
 106           * @param array   $args          Display arguments for the current widget.
 107           * @param array   $instance      Array of settings for the current widget.
 108           */
 109          wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
 110  
 111          echo $args['after_widget'];
 112      }
 113  
 114      /**
 115       * Handles updating settings for the current Navigation Menu widget instance.
 116       *
 117       * @since 3.0.0
 118       *
 119       * @param array $new_instance New settings for this instance as input by the user via
 120       *                            WP_Widget::form().
 121       * @param array $old_instance Old settings for this instance.
 122       * @return array Updated settings to save.
 123       */
 124  	public function update( $new_instance, $old_instance ) {
 125          $instance = array();
 126          if ( ! empty( $new_instance['title'] ) ) {
 127              $instance['title'] = sanitize_text_field( $new_instance['title'] );
 128          }
 129          if ( ! empty( $new_instance['nav_menu'] ) ) {
 130              $instance['nav_menu'] = (int) $new_instance['nav_menu'];
 131          }
 132          return $instance;
 133      }
 134  
 135      /**
 136       * Outputs the settings form for the Navigation Menu widget.
 137       *
 138       * @since 3.0.0
 139       *
 140       * @param array $instance Current settings.
 141       * @global WP_Customize_Manager $wp_customize
 142       */
 143  	public function form( $instance ) {
 144          global $wp_customize;
 145          $title    = isset( $instance['title'] ) ? $instance['title'] : '';
 146          $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
 147  
 148          // Get menus.
 149          $menus = wp_get_nav_menus();
 150  
 151          $empty_menus_style     = '';
 152          $not_empty_menus_style = '';
 153          if ( empty( $menus ) ) {
 154              $empty_menus_style = ' style="display:none" ';
 155          } else {
 156              $not_empty_menus_style = ' style="display:none" ';
 157          }
 158  
 159          $nav_menu_style = '';
 160          if ( ! $nav_menu ) {
 161              $nav_menu_style = 'display: none;';
 162          }
 163  
 164          // If no menus exists, direct the user to go and create some.
 165          ?>
 166          <p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>
 167              <?php
 168              if ( $wp_customize instanceof WP_Customize_Manager ) {
 169                  $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
 170              } else {
 171                  $url = admin_url( 'nav-menus.php' );
 172              }
 173  
 174              printf(
 175                  /* translators: %s: URL to create a new menu. */
 176                  __( 'No menus have been created yet. <a href="%s">Create some</a>.' ),
 177                  // The URL can be a `javascript:` link, so esc_attr() is used here instead of esc_url().
 178                  esc_attr( $url )
 179              );
 180              ?>
 181          </p>
 182          <div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
 183              <p>
 184                  <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 185                  <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 ); ?>" />
 186              </p>
 187              <p>
 188                  <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
 189                  <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
 190                      <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
 191                      <?php foreach ( $menus as $menu ) : ?>
 192                          <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
 193                              <?php echo esc_html( $menu->name ); ?>
 194                          </option>
 195                      <?php endforeach; ?>
 196                  </select>
 197              </p>
 198              <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
 199                  <p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">
 200                      <button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>
 201                  </p>
 202              <?php endif; ?>
 203          </div>
 204          <?php
 205      }
 206  }


Generated : Fri Mar 29 08:20:02 2024 Cross-referenced by PHPXref