* @copyright Copyright (c) 2013-2015, Nicolas GUILLAUME * @link http://presscustomizr.com/customizr * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ if ( ! class_exists( 'TC_menu' ) ) : class TC_menu { static $instance; function __construct () { self::$instance =& $this; //Set menu customizer options (since 3.2.0) add_action( 'wp' , array( $this, 'tc_set_menu_hooks') ); } /*************************************** * WP HOOKS SETTINGS ****************************************/ /* * hook : wp * * @package Customizr * @since Customizr 3.2.0 */ function tc_set_menu_hooks() { if ( (bool) TC_utils::$inst->tc_opt('tc_hide_all_menus') ) return; //VARIOUS USER OPTIONS add_filter( 'body_class' , array( $this , 'tc_add_body_classes') ); //Set header css classes based on user options add_filter( 'tc_header_classes' , array( $this , 'tc_set_header_classes') ); add_filter( 'tc_social_header_block_class' , array( $this, 'tc_set_social_header_class') ); //! tc_user_options_style filter is shared by several classes => must always check the local context inside the callback before appending new css //fired on hook : wp_enqueue_scripts add_filter( 'tc_user_options_style' , array( $this , 'tc_menu_item_style_first_letter_css') ); //set second menu specific style including @media rules add_filter( 'tc_user_options_style' , array( $this , 'tc_add_second_menu_inline_style') ); //SIDE MENU HOOKS SINCE v3.3+ if ( $this -> tc_is_sidenav_enabled() ){ add_action( 'wp_head' , array( $this , 'tc_set_sidenav_hooks') ); add_filter( 'tc_user_options_style' , array( $this , 'tc_set_sidenav_style') ); } else { // add main menu notice add_action( '__navbar' , array( $this, 'tc_maybe_display_main_menu_notice'), 50 ); } //this adds css classes to the navbar-wrapper : //1) to the main menu if regular (sidenav not enabled) //2) to the secondary menu if enabled if ( ! $this -> tc_is_sidenav_enabled() || TC_utils::$inst->tc_is_secondary_menu_enabled() ) { add_filter( 'tc_navbar_wrapper_class' , array( $this, 'tc_set_menu_style_options'), 0 ); } //body > header > navbar action ordered by priority add_action ( '__navbar' , array( $this , 'tc_menu_display' ), 30 ); //adds class add_filter ( 'wp_page_menu' , array( $this , 'tc_add_menuclass' )); } /*************************************** * WP_HEAD HOOKS SETTINGS ****************************************/ /** * Set Various hooks for the sidemenu * hook : wp_head * @return void */ function tc_set_sidenav_hooks() { add_filter( 'body_class' , array( $this, 'tc_sidenav_body_class') ); // disable dropdown on click add_filter( 'tc_menu_open_on_click' , array( $this, 'tc_disable_dropdown_on_click'), 10, 3 ); // add side menu before the page wrapper add_action( '__before_page_wrapper' , array( $this, 'tc_sidenav_display'), 0 ); // add side menu help block add_action( '__sidenav' , array( $this, 'tc_maybe_display_sidenav_help') ); // add menu button to the sidebar add_action( '__sidenav' , array( $this, 'tc_sidenav_toggle_button_display'), 5 ); // add menu add_action( '__sidenav' , array( $this, 'tc_sidenav_display_menu_customizer'), 10 ); } /** * Displays a dismissable block of information in the sidenav wrapper when conditions are met * hook : __sidenav */ function tc_maybe_display_sidenav_help() { if ( ! TC_placeholders::tc_is_sidenav_help_on() ) return; ?>
%1$s

%2$s

%3$s

', __( "This is a default page menu.", "customizr" ), __( "( If you don't have any pages in your website, then this side menu is empty for the moment. )" , "customizr"), sprintf( __("If you have already created menu(s), you can %s. If you need to create a new menu, jump to the %s.", "customizr"), sprintf( '%3$s', TC_utils::tc_get_customizer_url( array( "section" => "nav") ), __( "change the default menu", "customizr"), __("replace this default menu by another one", "customizr") ), sprintf( '%2$s', admin_url('nav-menus.php'), __( "menu creation screen", "customizr") ) ) ); printf('%1$s x', __( 'dismiss notice', 'customizr') ); ?>
tc_is_sidenav_enabled() ) { $this -> tc_regular_menu_display( 'main' ); } else { $this -> tc_sidenav_toggle_button_display(); if ( $this -> tc_is_second_menu_enabled() ) $this -> tc_regular_menu_display( 'secondary' ); else $this -> tc_maybe_display_second_menu_placeholder(); } $html = ob_get_contents(); ob_end_clean(); echo apply_filters( 'tc_menu_display', $html ); } /** * Menu button View * * @return html string * @package Customizr * @since v3.3+ * */ function tc_menu_button_view( $args ) { //extracts : 'type', 'button_class', 'button_attr' extract( $args ); $_button_label = sprintf( '%s', '__sidenav' == current_filter() ? __('Close', 'customizr') : __('Menu' , 'customizr') ); $_button = sprintf( '
%4$s
', implode(' ', apply_filters( "tc_{$type}_button_class", $button_class ) ), apply_filters( "tc_{$type}_menu_button_attr", $button_attr), '', (bool)esc_attr( TC_utils::$inst->tc_opt('tc_display_menu_label') ) ? $_button_label : '', '__sidenav' == current_filter() ? __('Close', 'customizr') : __('Open the menu' , 'customizr') ); return apply_filters( "tc_{$type}_menu_button_view", $_button ); } /** * Menu fallback. Link to the menu editor. * Thanks to tosho (http://wordpress.stackexchange.com/users/73/toscho) * http://wordpress.stackexchange.com/questions/64515/fall-back-for-main-menu * * @package Customizr * @since Customizr 1.0 */ function tc_link_to_menu_editor( $args ) { if ( ! current_user_can( 'manage_options' ) ) return; // see wp-includes/nav-menu-template.php for available arguments extract( $args ); $link = sprintf('%1$s%3$s%4$s%5$s%6$s', $link_before, admin_url( 'nav-menus.php' ), $before, __('Add a menu','customizr'), $after, $link_after ); // We have a list $link = ( FALSE !== stripos( $items_wrap, '' . $link . '' : $link; $output = sprintf( $items_wrap, $menu_id, $menu_class, $link ); $output = ( ! empty ( $container ) ) ? sprintf('<%1$s class="%2$s" id="%3$s">%4$s', $container, $container_class, $container_id, $output ) : $output; if ( $echo ) { echo $output; } return $output; } /*************************************** * REGULAR VIEWS ****************************************/ /** * Prepare params and echo menu views * * @return html string * @since v3.3+ * */ function tc_regular_menu_display( $_location = 'main' ){ $type = 'regular'; $where = 'right' != esc_attr( TC_utils::$inst->tc_opt( 'tc_header_layout') ) ? 'pull-right' : 'pull-left'; $button_class = array( 'btn-toggle-nav', $where ); $button_attr = 'data-toggle="collapse" data-target=".nav-collapse"'; $menu_class = ( ! wp_is_mobile() && 'hover' == esc_attr( TC_utils::$inst->tc_opt( 'tc_menu_type' ) ) ) ? array( 'nav tc-hover-menu' ) : array( 'nav' ) ; $menu_wrapper_class = ( ! wp_is_mobile() && 'hover' == esc_attr( TC_utils::$inst->tc_opt( 'tc_menu_type' ) ) ) ? array( 'nav-collapse collapse', 'tc-hover-menu-wrapper' ) : array( 'nav-collapse', 'collapse' ); $menu_view = $this -> tc_wp_nav_menu_view( compact( '_location', 'type', 'menu_class', 'menu_wrapper_class' ) ); if ( $menu_view && 'main' == $_location ) $menu_view = $menu_view . $this -> tc_menu_button_view( compact( 'type', 'button_class', 'button_attr') ); echo $menu_view; } /*************************************** * SIDENAV VIEWS ****************************************/ /** * @return html string * @since v3.3+ * * hook: __before_page_wrapper */ function tc_sidenav_display() { ob_start(); $tc_side_nav_class = implode(' ', apply_filters('tc_side_nav_class', array( 'tc-sn', 'navbar' ) ) ); $tc_side_nav_inner_class = implode(' ', apply_filters('tc_side_nav_inner_class', array( 'tc-sn-inner', 'nav-collapse') ) ); ?> tc_wp_nav_menu_view( compact( '_location', 'type', 'menu_class', 'menu_wrapper_class') ); } /** * @return html string * @since v3.3+ * * hooks: __sidenav, __navbar */ function tc_sidenav_toggle_button_display() { $type = 'sidenav'; $where = 'right' != esc_attr( TC_utils::$inst->tc_opt( 'tc_header_layout') ) ? 'pull-right' : 'pull-left'; $button_class = array( 'btn-toggle-nav', 'sn-toggle', $where ); $button_attr = ''; echo $this -> tc_menu_button_view( compact( 'type', 'button_class', 'button_attr') ); } /*************************************** * COMMON VIEW ****************************************/ /** * WP Nav Menu View * * @return html string * @package Customizr * @since Customizr 3.3+ */ function tc_wp_nav_menu_view( $args ) { extract( $args ); //'_location', 'type', 'menu_class', 'menu_wrapper_class' $menu_args = apply_filters( "tc_{$type}_menu_args", array( 'theme_location' => $_location, 'menu_class' => implode(' ', apply_filters( "tc_{$type}_menu_class", $menu_class ) ), 'fallback_cb' => array( $this, 'tc_page_menu' ), //if no menu is set to the required location, fallsback to tc_page_menu //=> tc_page_menu has it's own class extension of Walker, therefore no need to specify one below 'walker' => ! TC_utils::$inst -> tc_has_location_menu($_location) ? '' : new TC_nav_walker($_location), 'echo' => false, ) ); $menu = wp_nav_menu( $menu_args ); if ( $menu ) $menu = sprintf('
%2$s
', implode(' ', apply_filters( "tc_{$type}_menu_wrapper_class", $menu_wrapper_class ) ), $menu ); return apply_filters("tc_{$type}_menu_view", $menu ); } /*************************************** * PLACEHOLDERS VIEW ****************************************/ /** * Displays the placeholder view if conditions are met in TC_placeholders::tc_is_main_menu_notice_on() * fired in tc_menu_display(), hook : __navbar * @since Customizr 3.4+ */ function tc_maybe_display_main_menu_notice() { if ( ! TC_placeholders::tc_is_main_menu_notice_on() ) return; ?>
%1$s
%2$s

', __( "You can now display your menu as a vertical and mobile friendly side menu, animated when revealed.", "customizr" ), sprintf( __("%s or %s.", "customizr"), sprintf( '%2$s', esc_url('demo.presscustomizr.com?design=nav'), __( "Try it with the demo", "customizr") ), sprintf( '%3$s', TC_utils::tc_get_customizer_url( array( "section" => "nav") ), __( "open the customizer menu section", "customizr"), __("change your menu design now", "customizr") ) ) ); printf('%1$s x', __( 'dismiss notice', 'customizr') ); ?>
tc_opt( 'tc_menu_submenu_fade_effect') ) ) ? array_merge( $_classes, array( 'tc-submenu-fade' ) ) : $_classes; $_classes = ( 0 != esc_attr( TC_utils::$inst->tc_opt( 'tc_menu_submenu_item_move_effect') ) ) ? array_merge( $_classes, array( 'tc-submenu-move' ) ) : $_classes; $_classes = ( ! wp_is_mobile() && 'hover' == esc_attr( TC_utils::$inst->tc_opt( 'tc_menu_type' ) ) ) ? array_merge( $_classes, array( 'tc-open-on-hover' ) ) : array_merge( $_classes, array( 'tc-open-on-click' ) ); //Navbar menus positions (not sidenav) //CASE 1 : regular menu (sidenav not enabled), controled by option 'tc_menu_position' //CASE 2 : second menu ( is_secondary_menu_enabled ?), controled by option 'tc_second_menu_position' if ( ! $this -> tc_is_sidenav_enabled() ) array_push( $_classes , esc_attr( TC_utils::$inst->tc_opt( 'tc_menu_position') ) ); if ( TC_utils::$inst->tc_is_secondary_menu_enabled() ) array_push( $_classes , esc_attr( TC_utils::$inst->tc_opt( 'tc_second_menu_position') ) ); return $_classes; } /* * hook : body_class hook * * @package Customizr * @since Customizr 3.2.0 */ function tc_add_body_classes($_classes) { //menu type class $_menu_type = $this -> tc_is_sidenav_enabled() ? 'tc-side-menu' : 'tc-regular-menu'; array_push( $_classes, $_menu_type ); return $_classes; } /** * Set the header classes * Callback for tc_header_classes filter * * @package Customizr * @since Customizr 3.4+ */ function tc_set_header_classes( $_classes ) { //backward compatibility (was not handled has an array in previous versions) if ( ! is_array($_classes) ) return $_classes; //adds the second menu state if ( TC_Utils::$inst -> tc_is_secondary_menu_enabled() ) array_push( $_classes, 'tc-second-menu-on' ); //adds the resp. behaviour option for secondary menu array_push( $_classes, 'tc-second-menu-' . esc_attr( TC_utils::$inst->tc_opt( 'tc_second_menu_resp_setting' ) . '-when-mobile' ) ); return $_classes; } /* * hook : tc_social_header_block_class hook * * @package Customizr * @since Customizr 3.2.0 */ function tc_set_social_header_class($_classes) { return 'span5'; } /** * Adds a specific class to the ul wrapper * hook : 'wp_page_menu' * * @package Customizr * @since Customizr 3.0 */ function tc_add_menuclass( $ulclass) { $html = preg_replace( '/