-
Notifications
You must be signed in to change notification settings - Fork 20
/
functions.php
100 lines (88 loc) · 2.47 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Proper way to enqueue scripts and styles.
*/
function gitbook_scripts() : void {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_script( 'gitbook.js', get_template_directory_uri() . '/gitbook.js', array(), '1.0.0', true );
wp_enqueue_script( 'gitbook-theme.js', get_template_directory_uri() . '/theme.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'gitbook_scripts' );
add_action( 'after_setup_theme', 'gitbook_register_nav_menu' );
function gitbook_register_nav_menu() : void {
register_nav_menu( 'summary', 'Summary' );
}
add_filter( 'nav_menu_css_class', 'githook_nav_classes', 10, 1 );
function githook_nav_classes( $classes ) : array {
$classes[] = 'chapter';
if ( in_array( 'current_page_item', $classes ) ) {
$classes[] = 'active';
}
return $classes;
}
function get_next_menu_item( string $menu_name ) {
if ( ! get_the_ID() ) {
return false;
}
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'nopaging' => true ) );
if ( empty( $menuitems ) ) {
return false;
}
$i = -1;
$current_idx = -1;
foreach ( $menuitems as $item ) {
$i++;
$id = get_post_meta( $item->ID, '_menu_item_object_id', true );
if ( intval( $id ) === get_the_ID() ) {
$current_idx = $i;
break;
}
}
$next = false;
if ( -1 === $current_idx ) {
$next = $menuitems[0];
}
if ( isset( $menuitems[ $current_idx + 1 ] ) ) {
$next = $menuitems[ $current_idx + 1 ];
}
if ( $next ) {
$next = get_post_meta( $next->ID, '_menu_item_object_id', true );
return get_post( $next );
}
return false;
}
function get_previous_menu_item( string $menu_name ) {
if ( ! get_the_ID() ) {
return false;
}
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'nopaging' => true ) );
if ( empty( $menuitems ) ) {
return false;
}
$i = -1;
$current_idx = -1;
foreach ( $menuitems as $item ) {
$i++;
$id = get_post_meta( $item->ID, '_menu_item_object_id', true );
if ( intval( $id ) === get_the_ID() ) {
$current_idx = $i;
break;
}
}
$next = false;
if ( -1 === $current_idx ) {
return false;
}
if ( isset( $menuitems[ $current_idx - 1 ] ) ) {
$next = $menuitems[ $current_idx - 1 ];
}
if ( $next ) {
$next = get_post_meta( $next->ID, '_menu_item_object_id', true );
return get_post( $next );
}
return false;
}