Skip to content

Instantly share code, notes, and snippets.

@GLWalker
Created November 26, 2021 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLWalker/84ab1c400de488f4e85bcf50274ca75e to your computer and use it in GitHub Desktop.
Save GLWalker/84ab1c400de488f4e85bcf50274ca75e to your computer and use it in GitHub Desktop.
<?php
/**
* Function for recreating BuddyPress pagination with BootStrap 5 markup
*
* @package SystemPress
*/
// Exit if accessed directly.
defined('ABSPATH') || exit;
/**
* Pagination for loops
*
* @param string $position
*
* @since 1.0.0
*/
function systmp_bp_pagination($position, $echo = true, $params = [])
{
//global $wp_query;
$add_args = [];
//add query (GET) parameters to generated page URLs
/*if (isset($_GET[ 'sort' ])) {
$add_args[ 'sort' ] = (string)$_GET[ 'sort' ];
}*/
$screen = 'dir';
$pagination_type = bp_current_component();
$page_max ='';
$page_cur = '';
$pag_class = '';
if (bp_is_user()) {
$screen = 'user';
} elseif (bp_is_group()) {
$screen = 'group';
$pagination_type = bp_current_action();
if (bp_is_group_admin_page()) {
$pagination_type = bp_action_variable(0);
}
}
switch ($pagination_type) {
case 'blogs':
$pag_count = bp_get_blogs_pagination_count();
$page_arg = $GLOBALS['blogs_template']->pag_arg;
break;
case 'members':
case 'friends':
case 'manage-members':
$pag_count = bp_get_members_pagination_count();
$page_arg = $GLOBALS['members_template']->pag_arg;
$page_cur = $GLOBALS['members_template']->pag_page;
$page_max = bp_core_number_format(ceil((int) $GLOBALS['members_template']->total_member_count/ (int) $GLOBALS['members_template']->pag_num));
$pag_class = ' pagination-sm';
if (bp_is_group()) {
$pag_class = ' no-ajax';
}
break;
case 'friends':
case 'manage-members':
$pag_class = ' no-ajax';
break;
case 'groups':
global $groups_template;
$pag_count = bp_get_groups_pagination_count();
$page_arg = $GLOBALS['groups_template']->pag_arg;
$page_cur = $groups_template->pag_page;
$page_max = bp_core_number_format(ceil((int) $groups_template->total_group_count / (int) $groups_template->pag_num));
break;
case 'notifications':
$pag_count = bp_get_notifications_pagination_count();
$page_arg = buddypress()->notifications->query_loop->pag_arg;
break;
case 'membership-requests':
$pag_count = bp_get_group_requests_pagination_count();
$page_arg = $GLOBALS['requests_template']->pag_arg;
break;
default:
/**
* Use this filter to define your custom pagination parameters.
*
* @abstract bp_nouveau_pagination_params
* @since 1.0.0
*
* @param array $value {
* An associative array of pagination parameters.
* @type string $pag_count Information about the pagination count.
* eg: "Viewing 1 - 10 of 20 items".
* @type string $pag_links The Pagination links.
* @type string $page_arg The argument to use to pass the page number.
* }
* @param string $pagination_type Information about the pagination type.
*/
$pagination_params = apply_filters(
'systmp_bp_pagination_params',
array(
'pag_count' => '',
'page_arg' => '',
'page_cur' => '',
'page_max' => '',
),
$pagination_type
);
list($pag_count, $page_arg, $page_cur, $page_max) = array_values($pagination_params);
break;
}
$count_id = sprintf('%1$s-%2$s-count-%3$s', $pagination_type, $screen, $position);
$pag_id = sprintf('%1$s-%2$s-pag-%3$s', $pagination_type, $screen, $position);
//add query (GET) parameters to generated page URLs
/*if (isset($_GET[ 'sort' ])) {
$add_args[ 'sort' ] = (string)$_GET[ 'sort' ];
}*/
$pages = paginate_links(
array_merge([
// 'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
'base' => add_query_arg($page_arg, '%#%'),
'format' => '',
'total' => $page_max,
'current' => $page_cur,
'type' => 'array',
'show_all' => false,
'end_size' => 3,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
'add_args' => $add_args,
'add_fragment' => ''
], $params)
);
$pagination = '';
$pagination .= '<div id="pag-' . sanitize_html_class($position) . '" class="bp-pagination row row-cols-1 row-cols-md-2 mb-2">';
if ($pag_count) :
$pagination .= ' <div id="' . sanitize_html_class($count_id) . '" class="pag-count col">';
$pagination .= ' <div class="d-flex justify-content-center justify-content-md-start p-2">' . esc_html($pag_count) . '</div>';
$pagination .= ' </div>';
endif;
if (is_array($pages)) :
$pagination .= ' <nav id="' . sanitize_html_class($pag_id) . '" class="pagination-links col" aria-label="Page navigation">';
$pagination .= ' <ul class="pagination justify-content-center justify-content-md-end'. $pag_class .'">';
foreach ($pages as $page) :
$pagination .= ' <li class="page-item' . (strpos($page, 'current') !== false ? ' active' : '') . '"> ' . str_replace('page-numbers', 'page-numbers page-link', $page) . '</li>';
endforeach;
$pagination .= ' </ul>';
$pagination .= ' </nav>';
endif;
$pagination .= '</div>';
if ($echo) {
echo $pagination;
} else {
return $pagination;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment