Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/61 limit utility nav #284

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

- Restrict utility menu output to the parent level, fixes #61.
- Prevent child menu items from being added to the Utility menu, fixes #61.
- Reset nested Utility menu items to the top/parent level on save, fixes #61.
- Add admin notice stating the utility nav restrictions, fixes #61.

## 2.5.2

- Add alternate-footbar class to alternate-footbar fixes#5.
Expand Down
9 changes: 9 additions & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ function responsive_maybe_save_layout_setting() {
set_transient( 'responsive_layout_setting_check', '', WEEK_IN_SECONDS );
}
add_action( 'admin_init', 'responsive_maybe_save_layout_setting' );

/**
* Enqueue Custom Admin Menu Scripts.
*/
function responsive_enqueue_admin_scripts() {
wp_enqueue_script( 'custom-menu-js', get_template_directory_uri() . '/admin/menu.js', array(), RESPONSIVE_FRAMEWORK_VERSION, true );

}
add_action( 'admin_enqueue_scripts', 'responsive_enqueue_admin_scripts', 20 );
43 changes: 43 additions & 0 deletions admin/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
jQuery( document ).ready(function($) {
function verifyNav() {
let lis = jQuery('#update-nav-menu #post-body ul.menu li');
lis.each(function () {
// First Check if active in sort and don't act on the placeholder span.
if ( !$(this).hasClass('ui-sortable-helper') && !$(this).hasClass('sortable-placeholder')) {
// If not active and not at 0 depth reset to 0 depth and alert user.
if ( !$(this).hasClass('menu-item-depth-0') ) {
let itemID = $(this).attr('id');
let item = document.getElementById( itemID );
let itemClasses = item.className;
itemClasses = itemClasses.split( ' ' );
itemClasses.forEach((element, index) => {
if ( -1 !== element.toString().indexOf('menu-item-depth') ) {
let currentDepthClass = itemClasses[index];
$(this).removeClass(currentDepthClass);
$(this).addClass('menu-item-depth-0');
$(this).find( '.is-submenu').hide();
window.alert( 'Nested menu items not allowed on Utility Menu. Menu item Reset to top level.');
}
} );
}
}
});
}

// Setup Observer only on Utility Menu.
let selectedMenu = jQuery('.manage-menus select').find(':selected').text();

if( 'Utility Menu (Utility Navigation)' === selectedMenu.trim() ) {
// Select the node that will be observed for mutations.
const targetNode = document.getElementById('update-nav-menu');

// Options for the observer (which mutations to observe).
const config = {attributes: true, childList: true, subtree: true};

// Create an observer instance linked to the callback function.
const observer = new MutationObserver(verifyNav);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);
}
});
69 changes: 69 additions & 0 deletions inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ function responsive_get_utility_nav( $args = array() ) {
'menu_class' => 'utility-nav-menu',
'container' => false,
'echo' => false,
'depth' => 1,
)
);
}
Expand Down Expand Up @@ -1210,3 +1211,71 @@ function responsive_get_the_excerpt( $post_id = null, $length = 55 ) {
}
return $excerpt;
}


/**
* Add Admin notice describing limitations of the Utility menu.
*/
function responsive_utility_menu_notice() {

// Display only on nav-menus screen.
$screen = get_current_screen();
if ( 'nav-menus' !== $screen->base ) {
return;
}

// Get Current menu id from the global namespace and compare to the utility-menu id.
global $nav_menu_selected_id;
$utility_menu = wp_get_nav_menu_object( 'utility-menu' );

if ( $nav_menu_selected_id === $utility_menu->term_id ) {
$notice = __( 'The Utility Menu only displays the top level items.', 'responsive-framework' );
$notice .= '<br>';
$notice .= __( 'Nested items are prevented and/or reset to the top level on save.', 'responsive-framework' );
echo '<div class="notice notice-warning">' . wp_kses_post( $notice ) . '</div>';
}
}
add_action( 'admin_notices', 'responsive_utility_menu_notice' );


/**
* Reset any nested menu items to the top/parent level for the utility menu.
*
* @param int $menu_id The ID of the menu.
* @param array $menu_data An array of menu data that has the menu name.
*/
function responsive_update_utility_menu( int $menu_id, $menu_data = array() ) {

// Return if no data to update.
if ( empty( $menu_id ) || empty( $menu_data ) ) {
return;
}

$utility_menu_id = wp_get_nav_menu_object( 'utility-menu' )->term_id;

$count = 0;
// Only update Utility Menu.
if ( $menu_id === $utility_menu_id ) {
$menu_items = wp_get_nav_menu_items( 'utility-menu' );
foreach ( $menu_items as $item ) {

// Move any child menu items to the top/parent level.
if ( 0 !== (int) $item->menu_item_parent ) {
update_post_meta( $item->ID, 'menu_item_parent', 0 );
$count++;
}
}
}

if ( 0 < $count ) {
add_action( 'admin_notices', 'responsive_utility_menu_items_deleted_notice' );
}
}
add_action( 'wp_update_nav_menu', 'responsive_update_utility_menu', 10, 2 );

/**
* Add message that Utility Menu Items have been deleted.
*/
function responsive_utility_menu_items_deleted_notice() {
echo '<div class="notice notice-error is-dismissible">' . esc_html__( 'Nested Utility Menu Items not allowed. All menu items reset to the the top level.', 'responsive-framework' ) . '</div>';
}
46 changes: 30 additions & 16 deletions languages/responsive-framework.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: "
"https://wordpress.org/support/theme/responsive-framework-2-x\n"
"POT-Creation-Date: 2021-09-24 14:58:57+00:00\n"
"POT-Creation-Date: 2021-10-19 15:48:45+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
Expand Down Expand Up @@ -231,15 +231,15 @@ msgid ""
"2 widgets will appear."
msgstr ""

#: functions.php:267 inc/template-tags.php:1069
#: functions.php:267 inc/template-tags.php:1070
msgid "Footbar"
msgstr ""

#: functions.php:269 functions.php:283
msgid "Add widgets here to appear in your footer."
msgstr ""

#: functions.php:281 inc/template-tags.php:1070
#: functions.php:281 inc/template-tags.php:1071
msgid "Alternate Footbar"
msgstr ""

Expand Down Expand Up @@ -456,63 +456,77 @@ msgstr ""
msgid "Profile Directory"
msgstr ""

#: inc/template-tags.php:467 template-parts/masthead-side-nav.php:20
#: inc/template-tags.php:468 template-parts/masthead-side-nav.php:20
#: template-parts/masthead-top-nav.php:10 template-parts/masthead.php:15
msgid "Open menu"
msgstr ""

#: inc/template-tags.php:468
#: inc/template-tags.php:469
msgid "Full Menu"
msgstr ""

#: inc/template-tags.php:469
#: inc/template-tags.php:470
msgid "Close Menu"
msgstr ""

#: inc/template-tags.php:651
#: inc/template-tags.php:652
#. translators: %s: archive type singular name.
msgid "%s navigation"
msgstr ""

#: inc/template-tags.php:656
#: inc/template-tags.php:657
msgid "Newer posts"
msgstr ""

#: inc/template-tags.php:657
#: inc/template-tags.php:658
msgid "Older posts"
msgstr ""

#: inc/template-tags.php:698
#: inc/template-tags.php:699
msgid "Post navigation"
msgstr ""

#: inc/template-tags.php:726 single.php:49
#: inc/template-tags.php:727 single.php:49
#. translators: %s: author name linking to their archive page.
#. translators: %s: author name
msgid "<em>By </em>%s"
msgstr ""

#: inc/template-tags.php:743
#: inc/template-tags.php:744
#. translators: %s: category list for the post.
msgid "<em>in</em> %s"
msgstr ""

#: inc/template-tags.php:752
#: inc/template-tags.php:753
msgid "<strong>0</strong> comments"
msgstr ""

#: inc/template-tags.php:752
#: inc/template-tags.php:753
msgid "<strong>1</strong> comment"
msgstr ""

#: inc/template-tags.php:752
#: inc/template-tags.php:753
msgid "<strong>%</strong> comments"
msgstr ""

#: inc/template-tags.php:897
#: inc/template-tags.php:898
msgid "View all posts"
msgstr ""

#: inc/template-tags.php:1232
msgid "The Utility Menu only displays the top level items."
msgstr ""

#: inc/template-tags.php:1234
msgid "Nested items are prevented and/or reset to the top level on save."
msgstr ""

#: inc/template-tags.php:1280
msgid ""
"Nested Utility Menu Items not allowed. All menu items reset to the the top "
"level."
msgstr ""

#: no-access-bumc.php:13
msgid "Access Restricted To BUMC/BMC"
msgstr ""
Expand Down