Skip to content

Commit

Permalink
Dynamic disabling of unavailable directory filters in search results.
Browse files Browse the repository at this point in the history
  • Loading branch information
boonebgorges committed Jul 22, 2020
1 parent 817c7a2 commit b06a839
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
11 changes: 11 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ function openlab_load_scripts() {
);

wp_register_script( 'parsley', $stylesheet_dir_uri . '/js/parsley.min.js', array( 'jquery' ), $ver, true );

if ( bp_is_groups_directory() || openlab_is_search_results_page() || bp_is_members_directory() ) {
wp_enqueue_script( 'openlab-directory', $stylesheet_dir_uri . '/js/directory.js', [ 'jquery' ], $ver, true );
wp_localize_script(
'openlab-directory',
'OLDirectory',
[
'groupTypeDisabledFilters' => openlab_group_type_disabled_filters(),
]
);
}
}
}
add_action( 'wp_enqueue_scripts', 'openlab_load_scripts' );
Expand Down
79 changes: 79 additions & 0 deletions js/directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
(function($){
var $groupTypeCheckboxes,
allSidebarFilters = [];

$(document).ready(function(){
if ( 0 !== $('.openlab-search-results').length ) {
$('.sidebar-filter input[type="checkbox"], .custom-select select' ).each( function() {
allSidebarFilters.push( this.id );
} );

$groupTypeCheckboxes = $('.sidebar-filter-checkbox input.group-type-checkbox');
$groupTypeCheckboxes.on( 'change', calculateFilterStates );
calculateFilterStates();
}
});

calculateSelectedGroupTypes = function() {
var allGroupTypes = [];
var selectedGroupTypes = [];

$groupTypeCheckboxes.each(function(){
var thisGroupType = this;

allGroupTypes.push( thisGroupType.value );
if ( thisGroupType.checked ) {
selectedGroupTypes.push( thisGroupType.value );
}
});

if ( 0 === selectedGroupTypes.length ) {
return allGroupTypes;
} else {
return selectedGroupTypes;
}
};

/**
* Determines whether filters should be disabled based on select group types.
*/
calculateFilterStates = function() {
var selectedGroupTypes = calculateSelectedGroupTypes();
var disabledFilters = {};

// Convert group-type disabled filters lists to an array of arrays (to use reduce() below).
var disabledFiltersArray = []
for ( var i in window.OLDirectory.groupTypeDisabledFilters ) {
if ( -1 === selectedGroupTypes.indexOf( i ) ) {
continue;
}

if ( ! window.OLDirectory.groupTypeDisabledFilters.hasOwnProperty( i ) ) {
continue;
}

disabledFiltersArray.push( window.OLDirectory.groupTypeDisabledFilters[ i ] );
}

// Intersect of all disabled fields.
var disabledFilters = []
if ( disabledFiltersArray.length ) {
disabledFilters = disabledFiltersArray.reduce((a, b) => a.filter(c => b.includes(c)));
}

allSidebarFilters.forEach( function( sidebarFilterId ) {
var $el = $( '#' + sidebarFilterId );
var $elLabel = $( 'label[for="' + sidebarFilterId + '"]' );

// Everything is enabled by default.
$el.removeProp( 'disabled' ).removeClass( 'disabled-checkbox' );
$elLabel.removeClass( 'disabled-label' );

if ( -1 !== disabledFilters.indexOf( sidebarFilterId ) ) {
$el.prop( 'disabled', true ).addClass( 'disabled-checkbox' );
$elLabel.addClass( 'disabled-label' );
}
} );
};

}(jQuery));
47 changes: 47 additions & 0 deletions lib/group-funcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,53 @@ function openlab_current_directory_filters() {
echo $markup;
}

/**
* Gets a list of directory filter fields for each group type.
*
* @since 1.2.0
*
* @return array
*/
function openlab_group_type_disabled_filters() {
$disabled = [];
$group_types = cboxol_get_group_types();
foreach ( $group_types as $group_type ) {
$group_type_disabled = [];

if ( ! $group_type->get_can_be_cloned() ) {
$group_type_disabled[] = 'checkbox-is-cloneable';
}

if ( ! $group_type->get_supports_course_information() ) {
$group_type_disabled[] = 'course-term-select';
}

$group_terms = bpcgc_get_terms_by_group_type( $group_type->get_slug() );
if ( ! $group_terms ) {
$group_type_disabled[] = 'bp-group-categories-select';
}

if ( ! $group_type->get_is_portfolio() ) {
$group_type_disabled[] = 'portfolio-user-member-type-select';
}


$disabled[ $group_type->get_slug() ] = $group_type_disabled;
}

if ( defined( 'OLBADGES_VERSION' ) ) {
$all_badges = \OpenLab\Badges\Badge::get();
foreach ( $all_badges as $badge ) {
foreach ( $disabled as $group_type => &$type_disabled ) {
if ( ! in_array( $group_type, $badge->get_group_types(), true ) ) {
$type_disabled[] = 'checkbox-badge-' . $badge->get_id();
}
}
}
}

return $disabled;
}
/**
* Get a group's recent posts and comments, and display them in two widgets
*/
Expand Down
2 changes: 1 addition & 1 deletion parts/sidebar/filter-group-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?php foreach ( cboxol_get_group_types() as $group_type ) : ?>
<div class="sidebar-filter-checkbox">
<label for="checkbox-group-type-<?php echo esc_attr( $group_type->get_slug() ); ?>">
<input type="checkbox" name="group-types[]" id="checkbox-group-type-<?php echo esc_attr( $group_type->get_slug() ); ?>" <?php checked( in_array( $group_type->get_slug(), $current_group_types, true ) ); ?> value="<?php echo esc_attr( $group_type->get_slug() ); ?>" /> <?php echo esc_html( $group_type->get_name() ); ?>
<input type="checkbox" name="group-types[]" class="group-type-checkbox" id="checkbox-group-type-<?php echo esc_attr( $group_type->get_slug() ); ?>" <?php checked( in_array( $group_type->get_slug(), $current_group_types, true ) ); ?> value="<?php echo esc_attr( $group_type->get_slug() ); ?>" /> <?php echo esc_html( $group_type->get_name() ); ?>
</label>
</div>
<?php endforeach; ?>
Expand Down
2 changes: 1 addition & 1 deletion search-results.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div id="content" class="hfeed row">
<?php get_template_part( 'parts/sidebar/groups' ); ?>
<div <?php post_class( 'col-sm-18 col-xs-24' ); ?>>
<div id="openlab-main-content" class="content-wrapper">
<div id="openlab-main-content" class="content-wrapper openlab-search-results">
<div class="entry-title">
<h1><?php esc_html_e( 'Search Results', 'commons-in-a-box' ); ?></h1>

Expand Down

0 comments on commit b06a839

Please sign in to comment.