class-wp-nav-menu-widget.php 5.5 KB

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