Menu Locations page */ class Mega_Menu_Locations { /** * Constructor * * @since 2.8 */ public function __construct() { add_action( 'admin_post_megamenu_add_menu_location', array( $this, 'add_menu_location' ) ); add_action( 'admin_post_megamenu_delete_menu_location', array( $this, 'delete_menu_location' ) ); add_action( 'admin_post_megamenu_save_menu_location', array( $this, 'save_menu_location' ) ); add_action( 'admin_post_megamenu_sandbox', array( $this, 'sandbox' ) ); add_action( 'wp_print_scripts', array( $this, 'sandbox_remove_unnecessary_scripts' ) ); add_action( 'wp_print_styles', array( $this, 'sandbox_remove_unnecessary_styles' ) ); add_filter( 'megamenu_menu_tabs', array( $this, 'add_locations_tab' ), 1 ); add_action( 'megamenu_page_menu_locations', array( $this, 'menu_locations_page' ) ); } /** * Add the Menu Locations tab to our available tabs * * @param array $tabs array of available tabs. * @since 2.8 */ public function add_locations_tab( $tabs ) { $tabs['menu_locations'] = __( 'Menu Locations', 'megamenu' ); return $tabs; } /** * Add a new menu location. * * @since 2.8 */ public function add_menu_location() { check_admin_referer( 'megamenu_add_menu_location' ); $locations = get_option( 'megamenu_locations' ); $next_id = $this->get_next_menu_location_id(); $new_menu_location_id = 'max_mega_menu_' . $next_id; $title = 'Max Mega Menu Location ' . $next_id; if ( isset( $_POST['title'] ) ) { $title = esc_attr( wp_unslash( $_POST['title'] ) ); } $locations[ $new_menu_location_id ] = esc_attr( $title ); update_option( 'megamenu_locations', $locations ); $menu_id = 0; if ( isset( $_POST['menu_id'] ) ) { $menu_id = absint( $_POST['menu_id'] ); } if ( $menu_id > 0 ) { $locations = get_theme_mod( 'nav_menu_locations' ); $locations[ $new_menu_location_id ] = $menu_id; set_theme_mod( 'nav_menu_locations', $locations ); } do_action( 'megamenu_after_add_menu_location' ); $redirect_url = add_query_arg( array( 'page' => 'maxmegamenu', 'location_added' => 'true', 'location' => $new_menu_location_id, ), admin_url( 'admin.php' ) ); $this->redirect( $redirect_url ); } /** * Delete a menu location. * * @since 2.8 */ public function delete_menu_location() { check_admin_referer( 'megamenu_delete_menu_location' ); $locations = get_option( 'megamenu_locations' ); $location_to_delete = esc_attr( $_GET['location'] ); if ( isset( $locations[ $location_to_delete ] ) ) { unset( $locations[ $location_to_delete ] ); update_option( 'megamenu_locations', $locations ); } do_action( 'megamenu_after_delete_menu_location' ); do_action( 'megamenu_delete_cache' ); $redirect_url = add_query_arg( array( 'page' => 'maxmegamenu', 'delete_location' => 'true', ), admin_url( 'admin.php' ) ); $this->redirect( $redirect_url ); } /** * Save a menu location * * @since 2.0 */ public function save_menu_location() { check_admin_referer( 'megamenu_save_menu_location' ); $location = false; if ( isset( $_POST['location'] ) ) { $location = esc_attr( $_POST['location'] ); } if ( $location ) { $submitted_settings = apply_filters( 'megamenu_submitted_settings_meta', $_POST['megamenu_meta'] ); if ( isset( $submitted_settings[ $location ]['enabled'] ) ) { $submitted_settings[ $location ]['enabled'] = '1'; } if ( ! isset( $submitted_settings[ $location ]['unbind'] ) ) { $submitted_settings[ $location ]['unbind'] = 'disabled'; } if ( ! isset( $submitted_settings[ $location ]['descriptions'] ) ) { $submitted_settings[ $location ]['descriptions'] = 'disabled'; } if ( ! isset( $submitted_settings[ $location ]['prefix'] ) ) { $submitted_settings[ $location ]['prefix'] = 'disabled'; } if ( ! get_option( 'megamenu_settings' ) ) { update_option( 'megamenu_settings', $submitted_settings ); } else { $existing_settings = get_option( 'megamenu_settings' ); $new_settings = array_merge( $existing_settings, $submitted_settings ); update_option( 'megamenu_settings', $new_settings ); } do_action( 'megamenu_after_save_settings' ); do_action( 'megamenu_delete_cache' ); } /* Save custom location description **/ if ( isset( $_POST['custom_location'] ) && is_array( $_POST['custom_location'] ) ) { $custom_location = array_map( 'sanitize_text_field', $_POST['custom_location'] ); $locations = get_option( 'megamenu_locations' ); $new_locations = array_merge( (array) $locations, $custom_location ); update_option( 'megamenu_locations', $new_locations ); } $args = array( 'page' => 'maxmegamenu', 'location' => urlencode( $location ), 'save_location' => 'true', ); if ( ! isset( $submitted_settings[ $location ]['enabled'] ) ) { unset( $args['location'] ); } $redirect_url = add_query_arg( $args, admin_url( 'admin.php' ) ); $this->redirect( $redirect_url ); } /** * Redirect and exit * * @since 2.8 */ public function redirect( $url ) { wp_redirect( $url ); exit; } /** * Returns the next available menu location ID * * @since 2.8 */ public function get_next_menu_location_id() { $last_id = 0; if ( $locations = get_option( 'megamenu_locations' ) ) { foreach ( $locations as $key => $value ) { if ( strpos( $key, 'max_mega_menu_' ) !== false ) { $parts = explode( '_', $key ); $menu_id = end( $parts ); if ( $menu_id > $last_id ) { $last_id = $menu_id; } } } } $next_id = $last_id + 1; return $next_id; } /** * Content for Menu Locations page * * @since 2.8 */ public function menu_locations_page( $saved_settings ) { if ( isset( $_GET['add_location'] ) ) { $this->add_location_page(); return; } $all_locations = $this->get_registered_locations(); $enabled_locations = array(); $disabled_locations = array(); foreach ( $all_locations as $id => $description ) { if ( max_mega_menu_is_enabled( $id ) ) { $enabled_locations[ $id ] = $description; } else { $disabled_locations[ $id ] = $description; } } ?> 0 ) { $has_active_location_class = ' mega-has-active-location'; $tooltip = __( 'Active for Instance' ) . ' ' . esc_attr( $active_instance ); } ?>

0 ) { $tooltip_attr = " data-tooltip='{$tooltip}'"; } ?> >
  • assigned_menu_link( $location ); ?>
  • ' . $this->sandbox_link( $location ) . ''; } if ( strpos( $location, 'max_mega_menu_' ) !== false ) { echo '
  • ' . $this->delete_location_link( $location ) . '
  • '; } ?>
"; echo " " . esc_html__( 'Assign a menu', 'megamenu' ) . ' '; echo __( 'to this location to enable these options.', 'megamenu' ); echo '

'; } else { $this->show_menu_locations_options( $locations, $location, $description ); } ?>
get_menu_id_for_location( $location ); if ( $menu_id ) { return "" . esc_html( $this->get_menu_name_for_location( $location ) ) . ''; } else { return "" . esc_html__( 'Assign a menu', 'megamenu' ) . ''; } } /** * Display a link showing the menu assigned to the specified location * * @param string $location * @since 2.8 */ public function sandbox_link( $location ) { $sandbox_url = esc_url( add_query_arg( array( 'action' => 'megamenu_sandbox', 'location' => $location, ), wp_nonce_url( admin_url( 'admin-post.php' ), "megamenu_sandbox_" . $location ) ) ); return "" . esc_html__( 'View in Sandbox', 'megamenu' ) . ''; } /** * Display a link showing the menu assigned to the specified location * * @param string $location * @since 2.8 */ public function delete_location_link( $location ) { $delete_location_url = esc_url( add_query_arg( array( 'action' => 'megamenu_delete_menu_location', 'location' => $location, ), wp_nonce_url( admin_url( 'admin-post.php' ), 'megamenu_delete_menu_location' ) ) ); return "" . esc_html__( 'Delete location', 'megamenu' ) . ''; } /** * Remove unnecessary scripts from the sandbox page * * @since 2.9 */ public function sandbox_remove_unnecessary_scripts() { if ( isset( $_GET['action'] ) && $_GET['action'] === 'megamenu_sandbox' ) { global $wp_scripts; $queue_items = $wp_scripts->queue; $wp_scripts->queue = array(); do_action( 'megamenu_enqueue_scripts' ); } } /** * Remove unnecessary styles from the sandbox page * * @since 2.9 */ public function sandbox_remove_unnecessary_styles() { if ( isset( $_GET['action'] ) && $_GET['action'] === 'megamenu_sandbox' ) { global $wp_styles; $queue_items = $wp_styles->queue; $wp_styles->queue = array(); do_action( 'megamenu_enqueue_styles' ); } } /** * Content for Sandbox page * * @since 2.9 */ public function sandbox() { $location = ""; if ( isset( $_GET['location'] ) ) { $location = esc_attr( $_GET['location'] ); } else { die(); } check_admin_referer( 'megamenu_sandbox_' . $location ); if ( ! has_nav_menu( $location ) ) { die(); } if ( ! max_mega_menu_is_enabled( $location ) ) { die(); } remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); remove_action( 'wp_head', '_admin_bar_bump_cb' ); ?> Sandbox get_menu_id_for_location( $location ); $is_custom_location = strpos( $location, 'max_mega_menu_' ) !== false; $plugin_settings = get_option( 'megamenu_settings' ); $location_settings = isset( $plugin_settings[ $location ] ) ? $plugin_settings[ $location ] : array(); ?>
array( 'priority' => 10, 'title' => __( 'General Settings', 'megamenu' ), 'settings' => array( 'enabled' => array( 'priority' => 10, 'title' => __( 'Enabled', 'megamenu' ), 'description' => __( 'Enable Max Mega Menu for this menu location?', 'megamenu' ), 'settings' => array( array( 'type' => 'checkbox_enabled', 'key' => 'enabled', 'value' => isset( $location_settings['enabled'] ) ? $location_settings['enabled'] : 0, ), ), ), 'event' => array( 'priority' => 20, 'title' => __( 'Event', 'megamenu' ), 'description' => __( 'Select the event to trigger sub menus', 'megamenu' ), 'settings' => array( array( 'type' => 'event', 'key' => 'event', 'value' => isset( $location_settings['event'] ) ? $location_settings['event'] : 'hover', ), ), ), 'effect' => array( 'priority' => 30, 'title' => __( 'Effect', 'megamenu' ), 'description' => __( 'Select the desktop sub menu animation type', 'megamenu' ), 'settings' => array( array( 'type' => 'effect', 'key' => 'effect', 'value' => isset( $location_settings['effect'] ) ? $location_settings['effect'] : 'fade_up', 'title' => __( 'Animation', 'megamenu' ), ), array( 'type' => 'effect_speed', 'key' => 'effect_speed', 'value' => isset( $location_settings['effect_speed'] ) ? $location_settings['effect_speed'] : '200', 'title' => __( 'Speed', 'megamenu' ), ), ), ), 'effect_mobile' => array( 'priority' => 40, 'title' => __( 'Mobile Menu', 'megamenu' ), 'description' => __( 'Choose a style for your mobile menu', 'megamenu' ), 'settings' => array( array( 'type' => 'effect_mobile', 'key' => 'effect_mobile', 'value' => isset( $location_settings['effect_mobile'] ) ? $location_settings['effect_mobile'] : 'slide_right', 'title' => __( 'Type', 'megamenu' ), ), array( 'type' => 'effect_mobile_direction', 'key' => 'effect_mobile_direction', 'value' => isset( $location_settings['effect_mobile_direction'] ) ? $location_settings['effect_mobile_direction'] : 'vertical', 'title' => __( 'Submenu Style', 'megamenu' ), ), array( 'type' => 'effect_speed_mobile', 'key' => 'effect_speed_mobile', 'value' => isset( $location_settings['effect_speed_mobile'] ) ? $location_settings['effect_speed_mobile'] : '200', 'title' => __( 'Speed', 'megamenu' ), ), ), ), 'theme' => array( 'priority' => 50, 'title' => __( 'Theme', 'megamenu' ), 'description' => __( 'Select a theme to be applied to the menu', 'megamenu' ), 'settings' => array( array( 'type' => 'theme_selector', 'key' => 'theme', 'value' => isset( $location_settings['theme'] ) ? $location_settings['theme'] : 'default', ), ), ), ), ), 'advanced' => array( 'priority' => 25, 'title' => __( 'Advanced', 'megamenu' ), 'settings' => array( 'click_behaviour' => array( 'priority' => 10, 'title' => __( 'Click Event Behaviour', 'megamenu' ), 'description' => __( "Define what should happen when the event is set to 'click'. This also applies to mobiles.", 'megamenu' ), 'settings' => array( array( 'type' => 'click_behaviour', 'key' => 'click_behaviour', 'value' => $plugin_settings, ), ), ), 'mobile_behaviour' => array( 'priority' => 20, 'title' => __( 'Mobile Sub Menu Behaviour', 'megamenu' ), 'description' => __( 'Define the sub menu toggle behaviour for the mobile menu.', 'megamenu' ), 'settings' => array( array( 'type' => 'mobile_behaviour', 'key' => 'mobile_behaviour', 'value' => $plugin_settings, ), ), ), 'mobile_state' => array( 'priority' => 20, 'title' => __( 'Mobile Sub Menu Default State', 'megamenu' ), 'description' => __( 'Define the default state of the sub menus when the mobile menu is visible.', 'megamenu' ), 'settings' => array( array( 'type' => 'mobile_state', 'key' => 'mobile_state', 'value' => $plugin_settings, ), ), ), 'descriptions' => array( 'priority' => 20, 'title' => __( 'Menu Item Descriptions', 'megamenu' ), 'description' => __( 'Enable output of menu item descriptions.', 'megamenu' ), 'settings' => array( array( 'type' => 'descriptions', 'key' => 'descriptions', 'value' => $plugin_settings, ), ), ), 'unbind' => array( 'priority' => 20, 'title' => __( 'Unbind JavaScript Events', 'megamenu' ), 'description' => __( 'To avoid conflicts with theme menu systems, JavaScript events which have been added to menu items will be removed by default.', 'megamenu' ), 'settings' => array( array( 'type' => 'unbind', 'key' => 'unbind', 'value' => $plugin_settings, ), ), ), 'prefix' => array( 'priority' => 20, 'title' => __( 'Prefix Menu Item Classes', 'megamenu' ), 'description' => __( "Prefix custom menu item classes with 'mega-'?", 'megamenu' ), 'settings' => array( array( 'type' => 'prefix', 'key' => 'prefix', 'value' => $plugin_settings, ), ), ), 'container' => array( 'priority' => 20, 'title' => __( 'Container', 'megamenu' ), 'description' => __( 'Use nav or div element for menu wrapper?', 'megamenu' ), 'settings' => array( array( 'type' => 'container', 'key' => 'container', 'value' => $plugin_settings, ), ), ), 'active_instance' => array( 'priority' => 30, 'title' => __( 'Active Menu Instance', 'megamenu' ), 'info' => array( __( '0: Apply to all instances. 1: Apply to first instance. 2: Apply to second instance', 'megamenu' ) . '…' ), 'description' => __( 'Some themes will output this menu location multiple times on the same page. For example, it may be output once for the main menu, then again for the mobile menu. This setting can be used to make sure Max Mega Menu is only applied to one of those instances.', 'megamenu' ), 'settings' => array( array( 'type' => 'active_instance', 'key' => 'active_instance', 'value' => $plugin_settings, ), ), ), ), ), 'output_options' => array( 'priority' => 30, 'title' => __( 'Display Options', 'megamenu' ), 'settings' => array( 'location_block' => array( 'priority' => 10, 'title' => __( 'Block (Gutenberg)', 'megamenu' ), 'description' => __( 'Display this menu location in any block supported area.', 'megamenu' ), 'settings' => array( array( 'type' => 'location_block', 'key' => 'location_block', 'value' => $location, ), ), ), 'location_php_function' => array( 'priority' => 10, 'title' => __( 'PHP Function', 'megamenu' ), 'description' => __( 'Display this menu location in a theme template (usually header.php).', 'megamenu' ), 'settings' => array( array( 'type' => 'location_php_function', 'key' => 'location_php_function', 'value' => $location, ), ), ), 'location_shortcode' => array( 'priority' => 20, 'title' => __( 'Shortcode', 'megamenu' ), 'description' => __( 'Display this menu location in a post or page.', 'megamenu' ), 'settings' => array( array( 'type' => 'location_shortcode', 'key' => 'location_shortcode', 'value' => $location, ), ), ), 'location_widget' => array( 'priority' => 30, 'title' => __( 'Widget', 'megamenu' ), 'description' => __( 'Display this menu location in a widget area.', 'megamenu' ), 'settings' => array( array( 'type' => 'location_widget', 'key' => 'location_widget', 'value' => $location, ), ), ), ), ), ), $location, $plugin_settings ); if ( $is_custom_location ) { $settings['general']['settings']['location_description'] = array( 'priority' => 15, 'title' => __( 'Location Description', 'megamenu' ), 'description' => __( 'Update the custom location description', 'megamenu' ), 'settings' => array( array( 'type' => 'location_description', 'key' => 'location_description', 'value' => $description, ), ), ); } $initial_version = get_option( 'megamenu_initial_version' ); if ( $initial_version && version_compare( $initial_version, '2.8', '>' ) ) { //unset( $settings['advanced']['settings']['prefix'] ); // users who started out with 2.8.1+ will not see this option. } echo "
"; echo "'; $is_first = true; foreach ( $settings as $section_id => $section ) { if ( $is_first ) { $display = 'block'; $is_first = false; } else { $display = 'none'; } echo "
"; if ( $section_id == 'output_options' && ! $is_custom_location ) { echo "

" . __( 'This menu location is registered by your theme. Your theme should already include the code required to display this menu location on your site.', 'megamenu' ) . '

'; } echo ""; // order the fields by priority uasort( $section['settings'], array( $this, 'compare_elems' ) ); foreach ( $section['settings'] as $group_id => $group ) { echo ""; if ( isset( $group['settings'] ) ) { echo "'; echo "'; } else { echo ""; } echo ''; } if ( $section_id == 'general' ) { do_action( 'megamenu_settings_table', $location, $plugin_settings ); } echo '
"; if ( isset( $group['icon'] ) ) { echo ""; } echo esc_html( $group['title'] ); echo "
" . esc_html( $group['description'] ) . '
'; echo '
"; foreach ( $group['settings'] as $setting_id => $setting ) { echo "'; } if ( isset( $group['info'] ) ) { foreach ( $group['info'] as $paragraph ) { echo "
{$paragraph}
"; } } echo '
{$group['title']}
'; echo '
'; } ?>
$description ) { if ( false !== strpos( $loc, '___' ) ) { // Remove locations created by Polylang unregister_nav_menu( $loc ); } else { // Remove the language name appended to the original locations register_nav_menu( $loc, str_replace( ' ' . $default_lang, '', $description ) ); } } $all_locations = get_registered_nav_menus(); } $locations = array(); $custom_locations = get_option( 'megamenu_locations' ); if ( is_array( $custom_locations ) ) { $all_locations = array_merge( $custom_locations, $all_locations ); } if ( count( $all_locations ) ) { $megamenu_locations = array(); // reorder locations so custom MMM locations are listed at the bottom foreach ( $all_locations as $location => $val ) { if ( strpos( $location, 'max_mega_menu_' ) === false ) { $locations[ $location ] = $val; } else { $megamenu_locations[ $location ] = $val; } } $locations = array_merge( $locations, $megamenu_locations ); } return $locations; } /** * Returns the menu ID for a specified menu location, defaults to 0 * * @since 2.8 * @param string $location */ private function get_menu_id_for_location( $location ) { $locations = get_nav_menu_locations(); $id = isset( $locations[ $location ] ) ? $locations[ $location ] : 0; return $id; } /** * Returns the menu name for a specified menu location * * @since 2.8 * @param string $location */ private function get_menu_name_for_location( $location ) { $id = $this->get_menu_id_for_location( $location ); $menus = wp_get_nav_menus(); foreach ( $menus as $menu ) { if ( $menu->term_id == $id ) { return $menu->name; } } return false; } /** * Display messages to the user * * @since 2.0 */ public function print_messages() { if ( isset( $_GET['location_added'] ) ) { ?>

/> /> /> /> /> __( 'Hover Intent', 'megamenu' ), 'hover_' => __( 'Hover', 'megamenu' ), 'click' => __( 'Click', 'megamenu' ), ) ); ?> '; } /** * Print a select box containing all available sub menu animation options * * @since 2.8 * @param string $key * @param string $value */ public function print_location_effect_option( $location, $key, $value ) { ?> '; } /** * Print a select box containing all available effect speeds (desktop) * * @since 2.8 * @param string $key * @param string $value */ public function print_location_effect_speed_option( $location, $key, $value ) { ?> '; } /** * Print the textbox containing the various mobile menu options * * @since 2.8 * @param string $key * @param string $value */ public function print_location_effect_mobile_option( $location, $key, $value ) { ?> '; } /** * Print the textbox containing the various mobile menu options * * @since 2.8 * @param string $key * @param string $value */ public function print_location_effect_mobile_direction_option( $location, $key, $value ) { ?> '; } /** * Print a select box containing all available effect speeds (mobile) * * @since 2.8 * @param string $key * @param string $value */ public function print_location_effect_speed_mobile_option( $location, $key, $value ) { ?> '; } /** * Print a select box containing all available menu themes * * @since 2.8 * @param string $key * @param string $value */ public function print_location_theme_selector_option( $location, $key, $value ) { ?> '; echo ""; } /** * Print the textbox containing the sample PHP code to output a menu location * * @since 2.8 * @param string $key * @param string $value */ public function print_location_php_function_option( $location, $value ) { ?> "; } /** * Print a text input box allowing the user to change the name of a custom menu location * * @since 2.8 * @param string $key * @param string $value */ public function print_location_description_option( $location, $key, $value ) { echo ""; } /** * Compare array values * * @since 2.8 * @param array $elem1 * @param array $elem2 * @return bool */ private function compare_elems( $elem1, $elem2 ) { if ( $elem1['priority'] > $elem2['priority'] ) { return 1; } return 0; } } endif;