Browse Source

Add menu setup routines.

pull/43/head
Ned Zimmerman 7 years ago
parent
commit
4cc00956a1
No known key found for this signature in database
GPG Key ID: FF56334A013120CA
  1. 2
      footer.php
  2. 5
      functions.php
  3. 19
      header.php
  4. 5
      inc/actions/namespace.php
  5. 104
      inc/activation/namespace.php
  6. 58
      inc/filters/namespace.php

2
footer.php

@ -45,7 +45,7 @@ $pb_network_contact_form = get_option( 'pb_network_contact_form' );
</div> </div>
</div> </div>
<div class="network-footer__block network-footer__menu"> <div class="network-footer__block network-footer__menu">
<?php wp_nav_menu( 'network-footer-menu' ); ?> <?php wp_nav_menu( [ 'theme_location' => 'network-footer-menu' ] ); ?>
</div> </div>
</section> </section>
<section class="footer__pressbooks"> <section class="footer__pressbooks">

5
functions.php

@ -36,13 +36,16 @@ foreach ( $includes as $include ) {
} }
require get_template_directory() . '/inc/intervention.php'; require get_template_directory() . '/inc/intervention.php';
add_action( 'admin_init', '\\Aldine\\Activation\\create_default_content' ); add_action( 'after_switch_theme', '\\Aldine\\Activation\\create_default_content', 10 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\create_menus', 11 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\assign_menus', 12 );
add_action( 'admin_bar_init', '\\Aldine\\Actions\\remove_admin_bar_callback' ); add_action( 'admin_bar_init', '\\Aldine\\Actions\\remove_admin_bar_callback' );
add_action( 'after_setup_theme', '\\Aldine\\Actions\\setup' ); add_action( 'after_setup_theme', '\\Aldine\\Actions\\setup' );
add_action( 'after_setup_theme', '\\Aldine\\Actions\\content_width', 0 ); add_action( 'after_setup_theme', '\\Aldine\\Actions\\content_width', 0 );
add_action( 'wp_head', '\\Aldine\\Actions\\output_custom_colors' ); add_action( 'wp_head', '\\Aldine\\Actions\\output_custom_colors' );
add_filter( 'body_class', '\\Aldine\\Filters\\body_classes' ); add_filter( 'body_class', '\\Aldine\\Filters\\body_classes' );
add_filter( 'excerpt_more', '\\Aldine\\Filters\\excerpt_more' ); add_filter( 'excerpt_more', '\\Aldine\\Filters\\excerpt_more' );
add_filter( 'wp_nav_menu_items', '\\Aldine\\Filters\\adjust_menu', 10, 2 );
add_action( 'widgets_init', '\\Aldine\\Actions\\widgets_init' ); add_action( 'widgets_init', '\\Aldine\\Actions\\widgets_init' );
add_action( 'wp_enqueue_scripts', '\\Aldine\\Actions\\enqueue_assets' ); add_action( 'wp_enqueue_scripts', '\\Aldine\\Actions\\enqueue_assets' );
add_action( 'customize_register', '\\Aldine\\Customizer\\customize_register' ); add_action( 'customize_register', '\\Aldine\\Customizer\\customize_register' );

19
header.php

@ -72,24 +72,7 @@
</div> </div>
<div class="header__end-container"> <div class="header__end-container">
<nav class="header__nav js-header-nav" id="navigation"> <nav class="header__nav js-header-nav" id="navigation">
<?php if ( defined( 'PB_PLUGIN_VERSION' ) ) : ?> <?php wp_nav_menu( [ 'theme_location' => 'primary-menu' ] ); ?>
<a href="<?php echo home_url( '/catalog' ); ?>">Catalog</a>
<?php endif; ?>
<?php if ( get_option( 'pb_network_contact_form' ) ) : ?>
<a class="banner__navigation--contact" href="#contact">Contact</a>
<?php endif; ?>
<?php if ( ! is_user_logged_in() ) : ?>
<a href="<?php echo wp_login_url( get_permalink() ) ?>"><?php _e( 'Sign in', 'pressbooks-aldine' ) ?></a>
<?php if ( in_array( get_site_option( 'registration' ), [ 'user', 'all' ], true ) ) : ?>
<a href="<?php echo network_home_url( '/wp-signup.php' ); ?>"><?php _e( 'Sign up', 'pressbooks-aldine' ); ?></a>
<?php endif; ?>
<?php else : ?>
<?php if ( is_super_admin() || is_user_member_of_blog() ) : ?>
<a href="<?php echo admin_url(); ?>"><?php _e( 'Admin', 'pressbooks-aldine' ); ?></a>
<span class="sep">/</span>
<?php endif; ?>
<a href="<?php echo wp_logout_url( get_permalink() ); ?>"><?php _e( 'Sign out', 'pressbooks-aldine' ); ?></a>
<?php endif; ?>
</nav> </nav>
<a class="header__menu-icon js-header-menu-toggle" href="#navigation"><?php _e( 'Toggle Menu', 'pressbooks-book' ); ?><span class="header__menu-icon__icon"></span></a> <a class="header__menu-icon js-header-menu-toggle" href="#navigation"><?php _e( 'Toggle Menu', 'pressbooks-book' ); ?><span class="header__menu-icon__icon"></span></a>
</div> </div>

5
inc/actions/namespace.php

@ -40,9 +40,10 @@ function setup() {
*/ */
add_theme_support( 'post-thumbnails' ); add_theme_support( 'post-thumbnails' );
// This theme uses wp_nav_menu() in one location. // This theme uses wp_nav_menu() in two locations.
register_nav_menus( [ register_nav_menus( [
'network-footer-menu' => __( 'Network Footer Menu', 'pressbooks-aldine' ), 'primary-menu' => __( 'Primary Menu', 'pressbooks-aldine' ),
'network-footer-menu' => __( 'Footer Menu', 'pressbooks-aldine' ),
] ); ] );
/* /*

104
inc/activation/namespace.php

@ -64,3 +64,107 @@ function create_default_content() {
add_option( 'pb_aldine_activated', 1 ); add_option( 'pb_aldine_activated', 1 );
} }
} }
/**
* Create default primary and footer menus.
*/
function create_menus() {
$menu_name = __( 'Primary Menu', 'pressbooks-aldine' );
if ( ! wp_get_nav_menu_object( $menu_name ) ) {
$menu_id = wp_create_nav_menu( $menu_name );
$catalog = get_page_by_title( __( 'Catalog', 'pressbooks-aldine' ) );
if ( $catalog && defined( 'PB_PLUGIN_VERSION' ) ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'Catalog', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $catalog->ID,
'menu-item-status' => 'publish',
]
);
}
}
$menu_name = __( 'Footer Menu', 'pressbooks-aldine' );
if ( ! wp_get_nav_menu_object( $menu_name ) ) {
$menu_id = wp_create_nav_menu( $menu_name );
$about = get_page_by_title( __( 'About', 'pressbooks-aldine' ) );
if ( $about ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'About', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $about->ID,
'menu-item-status' => 'publish',
]
);
}
$catalog = get_page_by_title( __( 'Catalog', 'pressbooks-aldine' ) );
if ( $catalog && defined( 'PB_PLUGIN_VERSION' ) ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'Catalog', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $catalog->ID,
'menu-item-status' => 'publish',
]
);
}
$help = get_page_by_title( __( 'Help', 'pressbooks-aldine' ) );
if ( $help ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'Help', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $help->ID,
'menu-item-status' => 'publish',
]
);
}
}
}
/**
* Check for presence of menus; if they exist, assign them to their locations.
*/
function assign_menus() {
$locations = get_theme_mod( 'nav_menu_locations' );
if ( ! empty( $locations ) ) {
foreach ( $locations as $id => $value ) {
switch ( $id ) {
case 'primary-menu':
$menu = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
break;
case 'network-footer-menu':
$menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
break;
}
if ( $menu ) {
$locations[ $id ] = $menu->term_id;
}
}
set_theme_mod( 'nav_menu_locations', $locations );
}
}

58
inc/filters/namespace.php

@ -33,7 +33,65 @@ function body_classes( array $classes ) {
/** /**
* Customize excerpt. * Customize excerpt.
*
* @return string
*/ */
function excerpt_more() { function excerpt_more() {
return ' &hellip; <a href="' . get_permalink() . '">' . __( 'Continued', 'pressbooks-aldine' ) . '</a>'; return ' &hellip; <a href="' . get_permalink() . '">' . __( 'Continued', 'pressbooks-aldine' ) . '</a>';
} }
/**
* Add things to the menu.
*
* @param string $items
* @param object $args
* @return string
*/
function adjust_menu( $items, $args ) {
if ( $args->theme_location === 'primary-menu' ) {
if ( ! is_front_page() ) {
$items = sprintf(
'<li><a href="%1$s">%2$s</a></li>',
'/',
__( 'Home', 'pressbooks-aldine' )
) . $items;
}
if ( get_option( 'pb_network_contact_form' ) ) {
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
'#contact',
__( 'Contact', 'pressbooks-aldine' )
);
}
if ( ! is_user_logged_in() ) {
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
wp_login_url( get_permalink() ),
__( 'Sign in', 'pressbooks-aldine' )
);
if ( in_array( get_site_option( 'registration' ), [ 'user', 'all' ], true ) ) {
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
network_home_url( '/wp-signup.php' ),
__( 'Sign up', 'pressbooks-aldine' )
);
}
} else {
if ( is_super_admin() || is_user_member_of_blog() ) {
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
admin_url(),
__( 'Admin', 'pressbooks-aldine' )
);
}
$items .= sprintf(
'<li><a href="%1$s">%2$s</a></li>',
wp_logout_url( get_permalink() ),
__( 'Sign out', 'pressbooks-aldine' )
);
}
}
return $items;
}

Loading…
Cancel
Save