2015-09-01 09:49:21 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-22 10:41:25 -04:00
|
|
|
* Widget API: WP_Nav_Menu_Widget class
|
2015-09-01 09:49:21 -04:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Widgets
|
2015-09-22 10:41:25 -04:00
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Core class used to implement the Navigation Menu widget.
|
2015-09-22 10:41:25 -04:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
2016-05-16 03:22:30 -04:00
|
|
|
class WP_Nav_Menu_Widget extends WP_Widget {
|
2015-09-01 09:49:21 -04:00
|
|
|
|
2015-09-26 21:16:24 -04:00
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Sets up a new Navigation Menu widget instance.
|
2015-09-26 21:16:24 -04:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2015-09-01 09:49:21 -04:00
|
|
|
public function __construct() {
|
2016-03-21 17:59:29 -04:00
|
|
|
$widget_ops = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'description' => __( 'Add a navigation menu to your sidebar.' ),
|
2016-03-21 17:59:29 -04:00
|
|
|
'customize_selective_refresh' => true,
|
|
|
|
);
|
2017-11-30 18:11:00 -05:00
|
|
|
parent::__construct( 'nav_menu', __( 'Navigation Menu' ), $widget_ops );
|
2015-09-01 09:49:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Outputs the content for the current Navigation Menu widget instance.
|
2015-09-26 21:16:24 -04:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param array $args Display arguments including 'before_title', 'after_title',
|
|
|
|
* 'before_widget', and 'after_widget'.
|
2017-10-15 18:23:47 -04:00
|
|
|
* @param array $instance Settings for the current Navigation Menu widget instance.
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
|
|
|
public function widget( $args, $instance ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Get menu.
|
2015-09-01 09:49:21 -04:00
|
|
|
$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
|
|
|
|
|
2017-10-15 17:43:47 -04:00
|
|
|
if ( ! $nav_menu ) {
|
2015-09-01 09:49:21 -04:00
|
|
|
return;
|
2017-10-15 17:43:47 -04:00
|
|
|
}
|
|
|
|
|
2020-07-06 16:44:02 -04:00
|
|
|
$default_title = __( 'Menu' );
|
|
|
|
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
|
2015-09-01 09:49:21 -04:00
|
|
|
|
2015-09-08 18:17:26 -04:00
|
|
|
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
|
2017-10-15 17:43:47 -04:00
|
|
|
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
|
2015-09-01 09:49:21 -04:00
|
|
|
|
|
|
|
echo $args['before_widget'];
|
|
|
|
|
2017-10-15 17:43:47 -04:00
|
|
|
if ( $title ) {
|
|
|
|
echo $args['before_title'] . $title . $args['after_title'];
|
|
|
|
}
|
2015-09-01 09:49:21 -04:00
|
|
|
|
2020-07-06 16:44:02 -04:00
|
|
|
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the HTML format of widgets with navigation links.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
|
|
|
* @param string $format The type of markup to use in widgets with navigation links.
|
|
|
|
* Accepts 'html5', 'xhtml'.
|
|
|
|
*/
|
|
|
|
$format = apply_filters( 'navigation_widgets_format', $format );
|
|
|
|
|
|
|
|
if ( 'html5' === $format ) {
|
|
|
|
// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
|
|
|
|
$title = trim( strip_tags( $title ) );
|
|
|
|
$aria_label = $title ? $title : $default_title;
|
|
|
|
|
|
|
|
$nav_menu_args = array(
|
|
|
|
'fallback_cb' => '',
|
|
|
|
'menu' => $nav_menu,
|
|
|
|
'container' => 'nav',
|
|
|
|
'container_aria_label' => $aria_label,
|
|
|
|
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$nav_menu_args = array(
|
|
|
|
'fallback_cb' => '',
|
|
|
|
'menu' => $nav_menu,
|
|
|
|
);
|
|
|
|
}
|
2015-09-01 09:49:21 -04:00
|
|
|
|
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Filters the arguments for the Navigation Menu widget.
|
2015-09-01 09:49:21 -04:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
2015-09-28 11:31:25 -04:00
|
|
|
* @since 4.4.0 Added the `$instance` parameter.
|
2015-09-01 09:49:21 -04:00
|
|
|
*
|
2020-07-22 20:52:05 -04:00
|
|
|
* @param array $nav_menu_args {
|
2017-10-15 18:23:47 -04:00
|
|
|
* An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
|
2015-09-01 09:49:21 -04:00
|
|
|
*
|
2015-09-25 19:58:25 -04:00
|
|
|
* @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
|
2015-09-01 09:49:21 -04:00
|
|
|
* @type mixed $menu Menu ID, slug, or name.
|
|
|
|
* }
|
2020-07-22 20:52:05 -04:00
|
|
|
* @param WP_Term $nav_menu Nav menu object for the current menu.
|
|
|
|
* @param array $args Display arguments for the current widget.
|
|
|
|
* @param array $instance Array of settings for the current widget.
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
2015-09-28 11:31:25 -04:00
|
|
|
wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
|
2015-09-01 09:49:21 -04:00
|
|
|
|
|
|
|
echo $args['after_widget'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Handles updating settings for the current Navigation Menu widget instance.
|
2015-09-26 21:16:24 -04:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param array $new_instance New settings for this instance as input by the user via
|
|
|
|
* WP_Widget::form().
|
|
|
|
* @param array $old_instance Old settings for this instance.
|
|
|
|
* @return array Updated settings to save.
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
|
|
|
public function update( $new_instance, $old_instance ) {
|
|
|
|
$instance = array();
|
|
|
|
if ( ! empty( $new_instance['title'] ) ) {
|
2016-02-22 20:02:26 -05:00
|
|
|
$instance['title'] = sanitize_text_field( $new_instance['title'] );
|
2015-09-01 09:49:21 -04:00
|
|
|
}
|
|
|
|
if ( ! empty( $new_instance['nav_menu'] ) ) {
|
|
|
|
$instance['nav_menu'] = (int) $new_instance['nav_menu'];
|
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-15 18:23:47 -04:00
|
|
|
* Outputs the settings form for the Navigation Menu widget.
|
2015-09-26 21:16:24 -04:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param array $instance Current settings.
|
2016-05-16 03:22:30 -04:00
|
|
|
* @global WP_Customize_Manager $wp_customize
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
|
|
|
public function form( $instance ) {
|
2016-05-16 03:22:30 -04:00
|
|
|
global $wp_customize;
|
2017-11-30 18:11:00 -05:00
|
|
|
$title = isset( $instance['title'] ) ? $instance['title'] : '';
|
2015-09-01 09:49:21 -04:00
|
|
|
$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Get menus.
|
2015-09-01 09:49:21 -04:00
|
|
|
$menus = wp_get_nav_menus();
|
|
|
|
|
2019-07-02 19:42:58 -04:00
|
|
|
$empty_menus_style = '';
|
|
|
|
$not_empty_menus_style = '';
|
2017-11-22 23:09:49 -05:00
|
|
|
if ( empty( $menus ) ) {
|
|
|
|
$empty_menus_style = ' style="display:none" ';
|
|
|
|
} else {
|
|
|
|
$not_empty_menus_style = ' style="display:none" ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$nav_menu_style = '';
|
|
|
|
if ( ! $nav_menu ) {
|
|
|
|
$nav_menu_style = 'display: none;';
|
|
|
|
}
|
|
|
|
|
2015-09-01 09:49:21 -04:00
|
|
|
// If no menus exists, direct the user to go and create some.
|
|
|
|
?>
|
2017-11-22 23:09:49 -05:00
|
|
|
<p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>
|
2015-09-01 09:49:21 -04:00
|
|
|
<?php
|
2016-05-16 03:22:30 -04:00
|
|
|
if ( $wp_customize instanceof WP_Customize_Manager ) {
|
2015-09-01 09:49:21 -04:00
|
|
|
$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
|
|
|
|
} else {
|
|
|
|
$url = admin_url( 'nav-menus.php' );
|
|
|
|
}
|
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
|
|
|
|
2019-09-02 20:41:05 -04:00
|
|
|
/* translators: %s: URL to create a new menu. */
|
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
|
|
|
printf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) );
|
2015-09-01 09:49:21 -04:00
|
|
|
?>
|
|
|
|
</p>
|
2017-11-22 23:09:49 -05:00
|
|
|
<div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
|
2015-09-01 09:49:21 -04:00
|
|
|
<p>
|
2017-11-30 18:11:00 -05:00
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
|
2021-03-20 14:30:08 -04:00
|
|
|
<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 ); ?>" />
|
2015-09-01 09:49:21 -04:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
|
|
|
|
<select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
|
|
|
|
<option value="0"><?php _e( '— Select —' ); ?></option>
|
|
|
|
<?php foreach ( $menus as $menu ) : ?>
|
|
|
|
<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
|
|
|
|
<?php echo esc_html( $menu->name ); ?>
|
|
|
|
</option>
|
|
|
|
<?php endforeach; ?>
|
|
|
|
</select>
|
|
|
|
</p>
|
2016-05-16 03:22:30 -04:00
|
|
|
<?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
|
2017-11-22 23:09:49 -05:00
|
|
|
<p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">
|
2017-11-30 18:11:00 -05:00
|
|
|
<button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>
|
2016-05-16 03:22:30 -04:00
|
|
|
</p>
|
|
|
|
<?php endif; ?>
|
2015-09-01 09:49:21 -04:00
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
}
|