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();
?>
$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'] ) ) {
?>
/>
/>
>
>
>
>
>
>
>
>
><div>
><nav>
/>
/>
/>
__( 'Hover Intent', 'megamenu' ),
'hover_' => __( 'Hover', 'megamenu' ),
'click' => __( 'Click', 'megamenu' ),
)
);
?>
$name ) {
echo "' . esc_html( $name ) . ' ';
}
echo ' ';
}
/**
* 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 ) {
?>
array(
'label' => __( 'None', 'megamenu' ),
'selected' => $selected == 'disabled',
),
'fade' => array(
'label' => __( 'Fade', 'megamenu' ),
'selected' => $selected == 'fade',
),
'fade_up' => array(
'label' => __( 'Fade Up', 'megamenu' ),
'selected' => $selected == 'fade_up' || $selected == 'fadeUp',
),
'slide' => array(
'label' => __( 'Slide', 'megamenu' ),
'selected' => $selected == 'slide',
),
'slide_up' => array(
'label' => __( 'Slide Up', 'megamenu' ),
'selected' => $selected == 'slide_up',
),
),
$selected
);
foreach ( $options as $key => $value ) {
echo "' . esc_html( $value['label'] ) . ' ';
}
echo ' ';
}
/**
* 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 ) {
?>
__( 'Slow', 'megamenu' ),
'400' => __( 'Med', 'megamenu' ),
'200' => __( 'Fast', 'megamenu' ),
),
$selected
);
ksort( $options );
foreach ( $options as $key => $value ) {
echo "' . esc_html( $value ) . ' ';
}
echo ' ';
}
/**
* 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 ) {
?>
array(
'label' => __( 'None', 'megamenu' ),
'selected' => $selected == 'disabled',
),
'slide' => array(
'label' => __( 'Slide Down', 'megamenu' ),
'selected' => $selected == 'slide',
),
'slide_left' => array(
'label' => __( 'Off Canvas ←', 'megamenu' ),
'selected' => $selected == 'slide_left',
),
'slide_right' => array(
'label' => __( 'Off Canvas →', 'megamenu' ),
'selected' => $selected == 'slide_right',
),
),
$selected
);
foreach ( $options as $key => $value ) {
echo "' . esc_html( $value['label'] ) . ' ';
}
echo ' ';
}
/**
* 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 ) {
?>
array(
'label' => __( 'Up / Down ↕', 'megamenu' ),
'selected' => $selected == 'vertical',
'disabled' => '',
),
/*'horizontal' => array(
'label' => __( 'Left / Right ↔ (Pro)', 'megamenu' ),
'selected' => $selected == 'horizontal',
'disabled' => 'disabled',
),*/
),
$selected
);
foreach ( $options as $key => $value ) {
$disabled = isset($value['disabled']) && $value['disabled'] == 'disabled' ? 'disabled="disabled"' : '';
echo "" . esc_html( $value['label'] ) . " ";
}
echo ' ';
}
/**
* 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 ) {
?>
__( 'Slow', 'megamenu' ),
'400' => __( 'Med', 'megamenu' ),
'200' => __( 'Fast', 'megamenu' ),
),
$selected
);
ksort( $options );
foreach ( $options as $key => $value ) {
echo "' . esc_html( $value ) . ' ';
}
echo ' ';
}
/**
* 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 ) {
?>
get_themes();
$selected_theme = strlen( $value ) ? $value : 'default';
foreach ( $themes as $key => $theme ) {
$edit_theme_url = esc_url(
add_query_arg(
array(
'page' => 'maxmegamenu_theme_editor',
'theme' => $key,
),
admin_url( 'admin.php' )
)
);
echo "' . esc_html( $theme['title'] ) . ' ';
}
echo ' ';
echo "