PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/widgets/class-wp-nav-menu-widget.php

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