Skip to content

Commit

Permalink
Merge pull request #77 from bu-ist/release/1.2.24
Browse files Browse the repository at this point in the history
Release 1.2.24
  • Loading branch information
jdub233 authored Nov 30, 2020
2 parents b6fd5c6 + ec59fa2 commit 314cbfa
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 312 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ cache:

matrix:
include:
- php: 7.4
env: WP_VERSION=latest
- php: 7.4
env: WP_VERSION=5.4.2
- php: 7.2
env: WP_VERSION=latest
- php: 7.2
Expand Down
5 changes: 3 additions & 2 deletions bu-navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Author: Boston University (IS&T)
* Author URI: http://sites.bu.edu/web/
* Description: Provides alternative navigation elements designed for blogs with large page counts
* Version: 1.2.23
* Version: 1.2.24
* Text Domain: bu-navigation
* Domain Path: /languages
* License: GPL2+
Expand Down Expand Up @@ -38,6 +38,7 @@
@author Mike Burns <[email protected]>
@author Tyler Wiest <[email protected]>
@author Andrew Bauer <[email protected]>
@author Jonathan Williams <[email protected]>
*/

// Absolute server path to this plugin dir and file for use by included files.
Expand Down Expand Up @@ -81,7 +82,7 @@ class BU_Navigation_Plugin {
*
* @var string
*/
const VERSION = '1.2.23';
const VERSION = '1.2.24';

/**
* Plugin class constructor.
Expand Down
19 changes: 8 additions & 11 deletions class-bu-widget-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ protected function get_site_title( $wrapped_title_format ) {
*
* @see bu_navigation_supported_post_types() from library.php
* @see bu_navigation_list_pages() from library.php
* @see widget_bu_pages_args_adaptive() from bu-navigation-adaptive-contentnav.php
*
* @param array $args Display arguments for WP_Widget.
* @param array $instance The settings for the particular instance of the widget.
Expand All @@ -139,10 +140,13 @@ public function widget( $args, $instance ) {
// Set list arguments based on post type and navigation style.
$list_args = $this->get_list_args( $post, $instance );

do_action( 'bu_navigation_widget_before_list' );
// Alter args for adaptive mode.
if ( 'adaptive' === $instance['navigation_style'] ) {
$list_args = widget_bu_pages_args_adaptive( $list_args );
}

// Fetch markup.
$nav_list_markup = bu_navigation_list_pages( apply_filters( 'widget_bu_pages_args', $list_args ) );
$nav_list_markup = bu_navigation_list_pages( $list_args );

// Only output anything at all if there is existing markup from list_pages.
if ( empty( $nav_list_markup ) ) {
Expand Down Expand Up @@ -260,6 +264,8 @@ protected function get_list_args( $post, $instance ) {
'echo' => 0,
'container_id' => BU_WIDGET_PAGES_LIST_ID,
'post_types' => $post->post_type,
'style' => $instance['navigation_style'],
'widget' => true,
);

// Not sure this check is necessary as there should always be an instance style, but leaving it in to preserve original behavior.
Expand All @@ -268,22 +274,13 @@ protected function get_list_args( $post, $instance ) {
return $list_args;
}

// Include the instance navigation style in the list args.
$list_args['style'] = $instance['navigation_style'];

// 'section' style has special handling.
if ( 'section' === $instance['navigation_style'] ) {
$list_args['navigate_in_section'] = 1;
// Not sure why it is necessary to check for a 404 here, but this is the original handling.
return ( is_404() ) ? '' : $list_args;
}

// 'adaptive' style needs an action from included/library.php to be loaded.
if ( 'adaptive' === $instance['navigation_style'] ) {
add_action( 'bu_navigation_widget_before_list', 'bu_navigation_widget_adaptive_before_list' );
return $list_args;
}

// 'site' navigation_style doesn't require additional handling.
return $list_args;
}
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
"name": "Andrew Bauer",
"email": "[email protected]"
},

{
"name": "Jonathan Desrosiers",
"email": "[email protected]"
},
{
"name": "Jonathan Williams",
"email": "[email protected]"
}
],
"require": {},
Expand Down
122 changes: 68 additions & 54 deletions extras/bu-navigation-adaptive-contentnav.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,100 @@
* Adaptive navigation mode tames list size for large site hierarchies by
* "drilling down" based on the currently active post, limiting listings
* to no more than two levels (plus section title if configured).
*
*
* @package BU_Navigation
*
* @see [Content Navigation Widget Modes](https://github.com/bu-ist/bu-navigation/wiki/Content-Navigation-Widget)
* @todo bu_navigation_list_pages has a lot of adaptive mode logic -- refactor to consolidate
*/

/**
* Attach adaptive-mode specific hooks
*/
function bu_navigation_widget_adaptive_before_list() {
add_filter( 'bu_navigation_filter_pages_by_parent', 'bu_navigation_filter_pages_adaptive' );
add_filter( 'widget_bu_pages_args', 'widget_bu_pages_args_adaptive' );
}
*/

/**
* Filters arguments passed to bu_navigation_list_pages from widget display
*
*
* This is an ugly way of short circuiting the logic within bu_navigation_list_pages to not
* display all sections.
*/
*
* @param array $args Associative array of arguments for the list pages query.
* @return array Array of arguments transformed for adaptive mode.
*/
function widget_bu_pages_args_adaptive( $args ) {
remove_filter( 'widget_bu_pages_args', 'widget_bu_pages_args_adaptive' );
if ( $args['page_id'] ) {
$section_args = array( 'post_types' => $args['post_types'] );
$sections = bu_navigation_gather_sections( $args['page_id'], $section_args );
$sections = bu_navigation_gather_sections( $args['page_id'], $section_args );

$args['sections'] = $sections;
$args['page_id'] = NULL;
$args['page_id'] = null;
}
return $args;
}

/**
* Filters posts returned from bu_navigation_pages_by_parent to only include those
* centered around the current post
*/
*
* @param array $pages_by_parent Array of parents and children in the form of a 'section'.
* @return array Transformed array for adaptive mode.
*/
function bu_navigation_filter_pages_adaptive( $pages_by_parent ) {
global $post;

remove_filter('bu_navigation_filter_pages_by_parent', 'bu_navigation_filter_pages_adaptive' );
$filtered = array();
$display_has_children = array_key_exists( $post->ID, $pages_by_parent ) && ( count( $pages_by_parent[ $post->ID ] ) > 0 );

$filtered = array();
$has_children = FALSE;
foreach ( $pages_by_parent as $parent_id => $children ) {

if ( array_key_exists( $post->ID, $pages_by_parent ) && ( count( $pages_by_parent[$post->ID] ) > 0 ) ) {
$has_children = TRUE;
}
$adaptive_children = adaptive_filter_children( $children, $display_has_children, $post );

foreach ( $pages_by_parent as $parent_id => $posts ) {
if ( ( is_array( $posts ) ) && ( count( $posts ) > 0 ) ) {
$potentials = array();

foreach ( $posts as $p ) {
if ( $has_children ) {
// Only include the current page from the list of siblings if we have children
if ( $p->ID == $post->ID ) {
array_push( $potentials, $p );
}
} else {
// If we don't have children...
// Display siblings of current page also
if ( $p->post_parent == $post->post_parent ) {
array_push( $potentials, $p );
}
// Display the parent page
if ( $p->ID == $post->post_parent ) {
array_push( $potentials, $p );
}
}

// Include pages that are children of the current page
if ( $p->post_parent == $post->ID ) {
array_push( $potentials, $p );
}
}

if ( count( $potentials ) > 0 ) {
$filtered[$parent_id] = $potentials;
}
if ( count( $adaptive_children ) > 0 ) {
$filtered[ $parent_id ] = $adaptive_children;
}
}

return $filtered;
}
}

/**
* Filters the children of a post relative to the post being rendered for adaptive display
*
* This method has too many returns, but is unwound from a more complicated set of nested conditionals.
* The id's are cast as integers, because the $child ids are currently strings, but the parent ids are integers.
* Casting everything to an integers is clearer than using un-strict comparisons.
*
* @param array $children Array of post objects.
* @param boolean $display_has_children Whether the post being displayed has children.
* @param WP_Post $display_post The post being rendered (from the global $post).
* @return array Array of filtered post objects.
*/
function adaptive_filter_children( $children, $display_has_children, $display_post ) {

$filtered = array_filter( $children, function ( $child ) use ( $display_has_children, $display_post ) {
// Only include the current page from the list of siblings if the current page has children.
if ( $display_has_children && (int) $child->ID === (int) $display_post->ID ) {
return true;
}

// If the display post doens't have children, display siblings of current page also.
if ( ! $display_has_children && (int) $child->post_parent === (int) $display_post->post_parent ) {
return true;
}

// If the display post doens't have children, display the parent page.
if ( ! $display_has_children && (int) $child->ID === (int) $display_post->post_parent ) {
return true;
}

// Include pages that are children of the current page.
if ( (int) $child->post_parent === (int) $display_post->ID ) {
return true;
}

// Posts that don't meet any of these criteria are filtered out of the result.
return false;

});

// Re-index the array keys, since array_filter preserves them.
// This is just to match the previous behavior, it is unclear if it is necessary and can probably be removed.
return array_values( $filtered );

}
Loading

0 comments on commit 314cbfa

Please sign in to comment.