|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Genesis Header Nav |
| 4 | + * |
| 5 | + * @package GenesisHeaderNav |
| 6 | + * @author Gary Jones <gary@garyjones.co.uk> |
| 7 | + * @license GPL-2.0+ |
| 8 | + * @link https://github.com/GaryJones/genesis-header-nav |
| 9 | + * @copyright 2013 Gary Jones, Gamajo Tech |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Plugin class. |
| 14 | + * |
| 15 | + * @package GenesisHeaderNav |
| 16 | + * @author Gary Jones <gary@garyjones.co.uk> |
| 17 | + */ |
| 18 | +class Genesis_Header_Nav { |
| 19 | + |
| 20 | + /** |
| 21 | + * Instance of this class. |
| 22 | + * |
| 23 | + * @since 1.0.0 |
| 24 | + * |
| 25 | + * @var object |
| 26 | + */ |
| 27 | + protected static $instance = null; |
| 28 | + |
| 29 | + /** |
| 30 | + * Initialize the plugin by setting localization, filters, and administration functions. |
| 31 | + * |
| 32 | + * @since 1.0.0 |
| 33 | + */ |
| 34 | + private function __construct() { |
| 35 | + add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); |
| 36 | + add_action( 'genesis_setup', array( $this, 'register_nav_menu' ), 15 ); |
| 37 | + add_action( 'genesis_header', array( $this, 'show_menu' ), 8 ); |
| 38 | + add_filter( 'body_class', array( $this, 'body_classes' ), 15 ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Return an instance of this class. |
| 43 | + * |
| 44 | + * @since 1.0.0 |
| 45 | + * |
| 46 | + * @return object A single instance of this class. |
| 47 | + */ |
| 48 | + public static function get_instance() { |
| 49 | + // If the single instance hasn't been set, set it now. |
| 50 | + if ( null == self::$instance ) { |
| 51 | + self::$instance = new self; |
| 52 | + } |
| 53 | + |
| 54 | + return self::$instance; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Load the plugin text domain for translation. |
| 59 | + * |
| 60 | + * @since 1.0.0 |
| 61 | + */ |
| 62 | + public function load_plugin_textdomain() { |
| 63 | + $domain = 'genesis-header-nav'; |
| 64 | + $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
| 65 | + |
| 66 | + load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ); |
| 67 | + load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/languages' ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Register the menu location. |
| 72 | + * |
| 73 | + * @since 1.0.0 |
| 74 | + */ |
| 75 | + public function register_nav_menu() { |
| 76 | + register_nav_menu( 'header', __( 'Header', 'genesis-header-nav' ) ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Display the menu. |
| 81 | + * |
| 82 | + * @since 1.0.0 |
| 83 | + */ |
| 84 | + public function show_menu() { |
| 85 | + //* If menu is assigned to theme location, output |
| 86 | + if ( ! has_nav_menu( 'header' ) ) |
| 87 | + return; |
| 88 | + |
| 89 | + $class = 'menu genesis-nav-menu menu-header'; |
| 90 | + if ( genesis_superfish_enabled() ) |
| 91 | + $class .= ' js-superfish'; |
| 92 | + |
| 93 | + $args = array( |
| 94 | + 'theme_location' => 'header', |
| 95 | + 'container' => '', |
| 96 | + 'menu_class' => $class, |
| 97 | + 'echo' => 0, |
| 98 | + ); |
| 99 | + |
| 100 | + $nav = wp_nav_menu( $args ); |
| 101 | + |
| 102 | + //* Do nothing if there is nothing to show |
| 103 | + if ( ! $nav ) |
| 104 | + return; |
| 105 | + |
| 106 | + $nav_markup_open = genesis_markup( array( |
| 107 | + 'html5' => '<nav %s>', |
| 108 | + 'xhtml' => '<div id="nav">', |
| 109 | + 'context' => 'nav-header', |
| 110 | + 'echo' => false, |
| 111 | + ) ); |
| 112 | + $nav_markup_open .= genesis_structural_wrap( 'menu-header', 'open', 0 ); |
| 113 | + |
| 114 | + $nav_markup_close = genesis_structural_wrap( 'menu-header', 'close', 0 ); |
| 115 | + $nav_markup_close .= genesis_html5() ? '</nav>' : '</div>'; |
| 116 | + |
| 117 | + $nav_output = $nav_markup_open . $nav . $nav_markup_close; |
| 118 | + |
| 119 | + echo apply_filters( 'genesis_do_header_nav', $nav_output, $nav, $args ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Remove then conditionally re-add header-full-width body class. |
| 124 | + * |
| 125 | + * As well as just checking for something being in the header right widget area, or the action having something |
| 126 | + * hooked* in, we also need to check to see if the header navigation has a menu assigned to it. Only if all are |
| 127 | + * false can we* proceed with saying the header-full-width class should be applied. |
| 128 | + * |
| 129 | + * Function must be hooked after priority 10, so Genesis has had a chance to do the filtering first. |
| 130 | + * |
| 131 | + * @since 1.0.0 |
| 132 | + * |
| 133 | + * @param array $classes Existing classes. |
| 134 | + * |
| 135 | + * @return array Amended classes. |
| 136 | + */ |
| 137 | + public function body_classes( array $classes ) { |
| 138 | + // Loop through existing classes to remove 'header-full-width' |
| 139 | + foreach ( $classes as $index => $class ) { |
| 140 | + if ( 'header-full-width' === $class ) { |
| 141 | + unset( $classes[$index] ); |
| 142 | + break; // No need to check the rest. |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Do all the checks |
| 147 | + if ( ! is_active_sidebar( 'header-right' ) && ! has_action( 'genesis_header_right' ) && ! has_nav_menu( 'top' ) ) |
| 148 | + $classes[] = 'header-full-width'; |
| 149 | + |
| 150 | + return $classes; |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments