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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-09-26 21:16:24 -04:00
|
|
|
* Core class used to implement the Custom 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
|
|
|
/**
|
|
|
|
* Sets up a new Custom Menu widget instance.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
* @access public
|
|
|
|
*/
|
2015-09-01 09:49:21 -04:00
|
|
|
public function __construct() {
|
2016-03-21 17:59:29 -04:00
|
|
|
$widget_ops = array(
|
|
|
|
'description' => __( 'Add a custom menu to your sidebar.' ),
|
|
|
|
'customize_selective_refresh' => true,
|
|
|
|
);
|
2015-09-01 09:49:21 -04:00
|
|
|
parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-26 21:16:24 -04:00
|
|
|
* Outputs the content for the current Custom Menu widget instance.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @param array $args Display arguments including 'before_title', 'after_title',
|
|
|
|
* 'before_widget', and 'after_widget'.
|
|
|
|
* @param array $instance Settings for the current Custom Menu widget instance.
|
2015-09-01 09:49:21 -04:00
|
|
|
*/
|
|
|
|
public function widget( $args, $instance ) {
|
|
|
|
// Get menu
|
|
|
|
$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
|
|
|
|
|
|
|
|
if ( !$nav_menu )
|
|
|
|
return;
|
|
|
|
|
2015-09-08 18:17:26 -04:00
|
|
|
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
|
2015-09-01 09:49:21 -04:00
|
|
|
$instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
|
|
|
|
|
|
|
|
echo $args['before_widget'];
|
|
|
|
|
|
|
|
if ( !empty($instance['title']) )
|
|
|
|
echo $args['before_title'] . $instance['title'] . $args['after_title'];
|
|
|
|
|
|
|
|
$nav_menu_args = array(
|
|
|
|
'fallback_cb' => '',
|
|
|
|
'menu' => $nav_menu
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:06:28 -04:00
|
|
|
* Filters the arguments for the Custom 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
|
|
|
*
|
|
|
|
* @param array $nav_menu_args {
|
|
|
|
* An array of arguments passed to wp_nav_menu() to retrieve a custom menu.
|
|
|
|
*
|
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.
|
|
|
|
* }
|
2016-08-26 12:49:45 -04:00
|
|
|
* @param WP_Term $nav_menu Nav menu object for the current menu.
|
2015-09-01 09:49:21 -04:00
|
|
|
* @param array $args Display arguments for the current widget.
|
2015-09-28 11:31:25 -04:00
|
|
|
* @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'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-26 21:16:24 -04:00
|
|
|
* Handles updating settings for the current Custom Menu widget instance.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-26 21:16:24 -04:00
|
|
|
* Outputs the settings form for the Custom Menu widget.
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @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;
|
2015-09-01 09:49:21 -04:00
|
|
|
$title = isset( $instance['title'] ) ? $instance['title'] : '';
|
|
|
|
$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
|
|
|
|
|
|
|
|
// Get menus
|
|
|
|
$menus = wp_get_nav_menus();
|
|
|
|
|
|
|
|
// If no menus exists, direct the user to go and create some.
|
|
|
|
?>
|
|
|
|
<p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
|
|
|
|
<?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' );
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
<?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
|
|
|
|
</p>
|
|
|
|
<div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
|
|
|
|
<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 ); ?>"/>
|
|
|
|
</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 ) : ?>
|
|
|
|
<p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>">
|
|
|
|
<button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button>
|
|
|
|
</p>
|
|
|
|
<?php endif; ?>
|
2015-09-01 09:49:21 -04:00
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
}
|