Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: display a kinda-sorta sidebar menu on function reference pages #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions source/wp-content/themes/wporg-developer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
require __DIR__ . '/inc/admin.php';
}

/**
* Function reference sidebar menu.
*/
require __DIR__ . '/inc/sidebar-menu.php';

/**
* Set the content width based on the theme's design and stylesheet.
*/
Expand Down Expand Up @@ -172,6 +177,8 @@ function init() {

add_filter( 'mkaz_code_syntax_force_loading', '__return_true' );
add_filter( 'mkaz_prism_css_path', __NAMESPACE__ . '\\update_prism_css_path' );

add_filter( 'body_class', __NAMESPACE__ . '\\body_class' );
}

/**
Expand Down Expand Up @@ -258,17 +265,26 @@ function widgets_init() {
)
);

register_sidebar(
array(
'name' => __( 'Landing Page Footer - Center', 'wporg' ),
'id' => 'landing-footer-2',
'description' => __( 'Appears in footer of the primary landing page', 'wporg' ),
'before_widget' => '<div id="%1$s" class="widget box %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
)
);
register_sidebar( array(
'name' => __( 'Landing Page Footer - Center', 'wporg' ),
'id' => 'landing-footer-2',
'description' => __( 'Appears in footer of the primary landing page', 'wporg' ),
'before_widget' => '<div id="%1$s" class="widget box %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
) );

register_sidebar( array(
'id' => 'wp-parser-function',
'name' => __( 'Function Reference Sidebar', 'wporg' ),
'description' => __( 'Sidebar for function reference pages', 'wporg' ),
'before_widget' => '<aside id="%1$s" class="box gray widget %2$s">',
'after_widget' => '</div></aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1><div class="widget-content">',
) );

}

/**
Expand Down Expand Up @@ -407,3 +423,12 @@ function rename_comments_meta_box( $post_type, $post ) {
function update_prism_css_path( $path ) {
return '/stylesheets/prism.css';
}

function body_class( $classes ) {
// Trick the CSS into displaying a handbook menu on the function reference pages
if ( is_singular() && 'wp-parser-function' === get_post_type() ) {
$classes[] = 'single-handbook';
}

return $classes;
}
79 changes: 79 additions & 0 deletions source/wp-content/themes/wporg-developer/inc/sidebar-menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php


// WIP: very rough function reference menu. Lots of ugly code here, just PoC stuff.
// If we're going forward with the idea then this needs to be rewritten to use something similar to WPorg_Handbook_Walker
// (If it's even possible to use a page walker on CPTs like this)
// Ideally this could be turned into a block.
function function_reference_sidebar_menu() {
?>
<aside id="handbook_pages-99" class="widget widget_wporg_handbook_pages">
<h2 class="widget-title">Chapters</h2>
<div class="menu-table-of-contents-container">
<ul>
<li class="page_item menu-item page_item_has_children menu-item-has-children current_page_ancestor current-menu-ancestor open">
<?php
if ( 'wp-parser-function' === get_post_type() ) {
global $post;

$_post_id = $post->ID;

$terms = wp_get_post_terms( $post->ID, 'wp-parser-source-file', [ 'fields' => 'names' ] );
if ( $terms ) {
?>
<div class="expandable">
<a href="/reference/files/<?php echo esc_attr( $terms[0] ); ?>/"><?php echo esc_html( basename($terms[0]) ?? __('Functions', 'wporg') ); ?></a>
<button class="dashicons dashicons-arrow-down-alt2" aria-expanded="false"></button>
</div>
<?php

$q1 = new WP_Query( [
'post_type' => 'wp-parser-function',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => [
'relation' => 'OR',
[
'taxonomy' => 'wp-parser-source-file',
'terms' => $terms,
'field' => 'name',
]
]
]);

if ( $q1->found_posts ) {
?>
<ul class="children">
<?php
while ( $q1->have_posts() ) {
$q1->the_post();
if ( get_the_ID() == $_post_id ) {
?>
<li class="page_item menu-item"><a class="active" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title() ?>()</a></li>
<?php
} else {
?>
<li class="page_item menu-item"><a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title() ?>()</a></li>
<?php
}
}
}
?>
</ul>
<?php

wp_reset_postdata();
}
}
?>
</li>
<li class="page_item menu-item"><a href="https://developer.wordpress.org/reference/hooks/">Hooks</a></li>
<li class="page_item menu-item"><a href="https://developer.wordpress.org/reference/classes/">Classes</a></li>
<li class="page_item menu-item"><a href="https://developer.wordpress.org/reference/methods/">Methods</a></li>
</ul>
</div>
</aside>
<?php

}
14 changes: 8 additions & 6 deletions source/wp-content/themes/wporg-developer/sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* @package wporg-developer
*/
?>
<?php if ( is_active_sidebar( get_post_type() ) ) : ?>
<div id="sidebar" class="widget-area sidebar section" role="complementary">
<?php do_action( 'before_sidebar' ); ?>

<?php if ( ! dynamic_sidebar( get_post_type() ) ) : ?>
<?php endif; // end sidebar widget area ?>

<?php if ( is_active_sidebar( get_post_type() ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<a href="#" id="secondary-toggle"></a>
<div id="secondary-content">
<?php function_reference_sidebar_menu(); ?>
<?php do_action( 'before_sidebar' ); ?>
<?php dynamic_sidebar( get_query_var( 'current_handbook' ) ); ?>
</div>
</div><!-- #secondary -->
<?php endif; ?>
9 changes: 4 additions & 5 deletions source/wp-content/themes/wporg-developer/single.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

get_header(); ?>

<div id="content-area" <?php body_class( 'code-reference' ); ?>>
<?php get_sidebar(); ?>

<?php breadcrumb_trail(); ?>
<main id="primary" <?php post_class( 'site-main' ); ?> role="main">

<main id="main" class="site-main" role="main">
<?php breadcrumb_trail(); ?>

<?php while ( have_posts() ) : the_post(); ?>

Expand All @@ -21,6 +21,5 @@

<?php endwhile; // end of the loop. ?>

</main><!-- #main -->
</div><!-- #primary -->
</main><!-- #main -->
<?php get_footer(); ?>