You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

160 lines
5.3 KiB

  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 Custom 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 Custom Menu widget instance.
  19. *
  20. * @since 3.0.0
  21. * @access public
  22. */
  23. public function __construct() {
  24. $widget_ops = array(
  25. 'description' => __( 'Add a custom menu to your sidebar.' ),
  26. 'customize_selective_refresh' => true,
  27. );
  28. parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
  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. if ( !$nav_menu )
  44. return;
  45. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  46. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  47. echo $args['before_widget'];
  48. if ( !empty($instance['title']) )
  49. echo $args['before_title'] . $instance['title'] . $args['after_title'];
  50. $nav_menu_args = array(
  51. 'fallback_cb' => '',
  52. 'menu' => $nav_menu
  53. );
  54. /**
  55. * Filters the arguments for the Custom Menu widget.
  56. *
  57. * @since 4.2.0
  58. * @since 4.4.0 Added the `$instance` parameter.
  59. *
  60. * @param array $nav_menu_args {
  61. * An array of arguments passed to wp_nav_menu() to retrieve a custom menu.
  62. *
  63. * @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
  64. * @type mixed $menu Menu ID, slug, or name.
  65. * }
  66. * @param WP_Term $nav_menu Nav menu object for the current menu.
  67. * @param array $args Display arguments for the current widget.
  68. * @param array $instance Array of settings for the current widget.
  69. */
  70. wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
  71. echo $args['after_widget'];
  72. }
  73. /**
  74. * Handles updating settings for the current Custom Menu widget instance.
  75. *
  76. * @since 3.0.0
  77. * @access public
  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 Custom Menu widget.
  96. *
  97. * @since 3.0.0
  98. * @access public
  99. *
  100. * @param array $instance Current settings.
  101. * @global WP_Customize_Manager $wp_customize
  102. */
  103. public function form( $instance ) {
  104. global $wp_customize;
  105. $title = isset( $instance['title'] ) ? $instance['title'] : '';
  106. $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
  107. // Get menus
  108. $menus = wp_get_nav_menus();
  109. // If no menus exists, direct the user to go and create some.
  110. ?>
  111. <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
  112. <?php
  113. if ( $wp_customize instanceof WP_Customize_Manager ) {
  114. $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
  115. } else {
  116. $url = admin_url( 'nav-menus.php' );
  117. }
  118. ?>
  119. <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
  120. </p>
  121. <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
  122. <p>
  123. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
  124. <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 ); ?>"/>
  125. </p>
  126. <p>
  127. <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
  128. <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
  129. <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
  130. <?php foreach ( $menus as $menu ) : ?>
  131. <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
  132. <?php echo esc_html( $menu->name ); ?>
  133. </option>
  134. <?php endforeach; ?>
  135. </select>
  136. </p>
  137. <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
  138. <p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>">
  139. <button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button>
  140. </p>
  141. <?php endif; ?>
  142. </div>
  143. <?php
  144. }
  145. }