Skip to content

Instantly share code, notes, and snippets.

@2Fwebd
Created April 24, 2019 22:43
Show Gist options
  • Save 2Fwebd/63af41a19007ca4e57639984b1adca4a to your computer and use it in GitHub Desktop.
Save 2Fwebd/63af41a19007ca4e57639984b1adca4a to your computer and use it in GitHub Desktop.
Ticket #1978249
/**
* Woffice members filter query
* See bp_nouveau_ajax_querystring() for details
*
* @since 2.8.0
*
* @param $query_string
*
* @return string
*/
function woffice_members_filter_query($query_string) {
global $bp;
if ($bp->current_component !== 'members') {
return $query_string;
}
$query_array = array();
parse_str($query_string, $query_array);
/**
* Filter member by matching field
*/
$matching_field_id = woffice_get_settings_option('member_matching_fields');
$user_id = get_current_user_id();
$all_include_string = '';
$all_include_ids = '';
if (isset($matching_field_id, $user_id)) {
$matching_field = xprofile_get_field($matching_field_id, $user_id);
if (isset($matching_field->data->value)) {
global $wpdb;
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $matching_field->id . " AND value != '" . $matching_field->data->value . "';";
$all_include_ids = $wpdb->get_col($query);
$all_include_string = ($all_include_ids !== '' && sizeof($all_include_ids) > 0) ? implode(',',
$all_include_ids) : '';
}
}
$buddy_excluded_directory = woffice_get_settings_option( 'buddy_excluded_directory' );
if (isset($_POST['filterRole']) || !empty($buddy_excluded_directory)) {
$exclude_members_role_filter = 0;
$exclude_members_role_option = 0;
if (isset($_POST['filterRole'])) {
$the_role = sanitize_text_field($_POST['filterRole']);
// We set a role and we want the list of all the other users not in the role
$exclude_members_role_filter = woffice_exclude_members( $the_role, 'exclude_all' );
}
if (!empty($buddy_excluded_directory)) {
// We set a role and we want to exclude it so all its users
$exclude_members_role_option = woffice_exclude_members($buddy_excluded_directory, 'exclude_role');
}
if ($exclude_members_role_filter || $exclude_members_role_option) {
$query_array['exclude'] = $exclude_members_role_filter . ',' . $exclude_members_role_option;
}
}
// Get the request to filter members by xProfile fields
if (woffice_bp_is_active('xprofile') && isset($_POST['advanced-search-submit'])) {
$advanced_fields = woffice_get_advanced_search_fields_from_post_request();
$ids = woffice_get_users_ids_by_xprofile_fields($advanced_fields);
$include_string = ($ids !== '' && sizeof($ids) > 0) ? implode( ',', $ids ) : '';
// If empty => no result so we return to avoid BuddyPress fallback (all active users)
if ($include_string === '') {
return 'include=0';
}
$all_include_ids = array_merge($all_include_ids, $ids);
$all_include_string .= ','. $include_string;
}
/**
* Filter `woffice_members_loop_query_members_included`
*
* Includes members in the BuddyPress directory
*
* @param string $all_include_string - included members as a string
* @param array $all_include_ids - all user ids as an array
*/
$include_string = apply_filters( 'woffice_members_loop_query_members_included', $all_include_string, $all_include_ids );
if (!empty($include_string)) {
$query_array['include'] = $include_string;
}
$members_per_page = woffice_get_settings_option( 'buddypress_members_per_page', 12 );
$members_per_page = (is_numeric($members_per_page) && $members_per_page > 0) ? absint( $members_per_page ) : 12;
$query_array['per_page'] = $members_per_page;
/**
* Woffice members directory query
*
* @param string $members_loop_query - the current query
*/
$members_loop_query = apply_filters('woffice_members_loop_query', http_build_query($query_array));
return $members_loop_query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment