diff --git a/CHANGELOG.md b/CHANGELOG.md index ff030286..e17ebbf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/admin/admin.php b/admin/admin.php index 4c9558b6..3132511c 100755 --- a/admin/admin.php +++ b/admin/admin.php @@ -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 ); diff --git a/admin/menu.js b/admin/menu.js new file mode 100644 index 00000000..6c80dad7 --- /dev/null +++ b/admin/menu.js @@ -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); + } +}); diff --git a/inc/template-tags.php b/inc/template-tags.php index d9bc1e3c..b8389ee1 100755 --- a/inc/template-tags.php +++ b/inc/template-tags.php @@ -434,6 +434,7 @@ function responsive_get_utility_nav( $args = array() ) { 'menu_class' => 'utility-nav-menu', 'container' => false, 'echo' => false, + 'depth' => 1, ) ); } @@ -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 .= '
'; + $notice .= __( 'Nested items are prevented and/or reset to the top level on save.', 'responsive-framework' ); + echo '
' . wp_kses_post( $notice ) . '
'; + } +} +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 '
' . esc_html__( 'Nested Utility Menu Items not allowed. All menu items reset to the the top level.', 'responsive-framework' ) . '
'; +} diff --git a/languages/responsive-framework.pot b/languages/responsive-framework.pot index c464d902..31af2562 100644 --- a/languages/responsive-framework.pot +++ b/languages/responsive-framework.pot @@ -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" @@ -231,7 +231,7 @@ 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 "" @@ -239,7 +239,7 @@ msgstr "" 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 "" @@ -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 "By %s" msgstr "" -#: inc/template-tags.php:743 +#: inc/template-tags.php:744 #. translators: %s: category list for the post. msgid "in %s" msgstr "" -#: inc/template-tags.php:752 +#: inc/template-tags.php:753 msgid "0 comments" msgstr "" -#: inc/template-tags.php:752 +#: inc/template-tags.php:753 msgid "1 comment" msgstr "" -#: inc/template-tags.php:752 +#: inc/template-tags.php:753 msgid "% 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 ""