[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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          $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          $empty_menus_style     = '';
 124          $not_empty_menus_style = '';
 125          if ( empty( $menus ) ) {
 126              $empty_menus_style = ' style="display:none" ';
 127          } else {
 128              $not_empty_menus_style = ' style="display:none" ';
 129          }
 130  
 131          $nav_menu_style = '';
 132          if ( ! $nav_menu ) {
 133              $nav_menu_style = 'display: none;';
 134          }
 135  
 136          // If no menus exists, direct the user to go and create some.
 137          ?>
 138          <p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>
 139              <?php
 140              if ( $wp_customize instanceof WP_Customize_Manager ) {
 141                  $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
 142              } else {
 143                  $url = admin_url( 'nav-menus.php' );
 144              }
 145  
 146              /* translators: %s: URL to create a new menu. */
 147              printf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) );
 148              ?>
 149          </p>
 150          <div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
 151              <p>
 152                  <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 153                  <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 ); ?>"/>
 154              </p>
 155              <p>
 156                  <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
 157                  <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
 158                      <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
 159                      <?php foreach ( $menus as $menu ) : ?>
 160                          <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
 161                              <?php echo esc_html( $menu->name ); ?>
 162                          </option>
 163                      <?php endforeach; ?>
 164                  </select>
 165              </p>
 166              <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
 167                  <p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">
 168                      <button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>
 169                  </p>
 170              <?php endif; ?>
 171          </div>
 172          <?php
 173      }
 174  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7