Skip to content

Commit

Permalink
Merge pull request #58 from sectsect/feature/plugin-check
Browse files Browse the repository at this point in the history
fix: fix bug for pagination
  • Loading branch information
sectsect authored Nov 30, 2024
2 parents 362b4d4 + fd84df6 commit 30f1392
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
23 changes: 15 additions & 8 deletions admin/class-recursivetable.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,18 @@ private static function array_to_html_table_recursive( array $arr ): string {
$paged = filter_input( INPUT_GET, 'paged', FILTER_VALIDATE_INT );
$nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

$paged = $paged && wp_verify_nonce( $nonce, 'google_ss2db_pagination' )
? $paged
: 1;
if ( $paged && ! wp_verify_nonce( $nonce, 'google_ss2db_pagination' ) ) {
$paged = 1;
}

$paged = $paged ? $paged : 1;
$limit = 24;
$offset = ( $paged - 1 ) * $limit;
$countsql = 'SELECT * FROM ' . GOOGLE_SS2DB_TABLE_NAME . ' ORDER BY date DESC';
$allrows = count( $wpdb->get_results( $countsql ) ); // phpcs:ignore
$allrows = count( $wpdb->get_results( $countsql ) ); // phpcs:ignore
$max_num_pages = ceil( $allrows / $limit );
// $sql = 'SELECT * FROM ' . GOOGLE_SS2DB_TABLE_NAME . ' ORDER BY date DESC LIMIT ' . $offset . ', ' . $limit;
$sql = 'SELECT * FROM ' . $table . ' ORDER BY date DESC LIMIT %d OFFSET %d';
$prepared = $wpdb->prepare(
$sql = 'SELECT * FROM ' . $table . ' ORDER BY date DESC LIMIT %d OFFSET %d';
$prepared = $wpdb->prepare(
$sql, // phpcs:ignore
$limit,
$offset
Expand Down Expand Up @@ -287,8 +288,14 @@ private static function array_to_html_table_recursive( array $arr ): string {
</dl>
<?php endforeach; ?>
<?php
$pagination_nonce = wp_create_nonce( 'google_ss2db_pagination' );
if ( function_exists( 'google_ss2db_options_pagination' ) ) {
google_ss2db_options_pagination( $paged, (int) $max_num_pages, 2 );
google_ss2db_options_pagination(
$paged,
(int) $max_num_pages,
2,
$pagination_nonce
);
}
?>
</section>
Expand Down
55 changes: 46 additions & 9 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,74 @@
* This function generates HTML for a simple pagination interface, based on the current page,
* total number of pages, and the range of pages to display around the current page.
*
* @param int $paged Current page number, defaults to 1.
* @param int $pages Total number of pages, defaults to 1.
* @param int $range Number of pages to display around the current page, defaults to 2.
* @param int $paged Current page number, defaults to 1.
* @param int $pages Total number of pages, defaults to 1.
* @param int $range Number of pages to display around the current page, defaults to 2.
* @param string $nonce Nonce for security verification.
* @return void
*/
function google_ss2db_options_pagination( int $paged = 1, int $pages = 1, int $range = 2 ): void {
function google_ss2db_options_pagination( int $paged = 1, int $pages = 1, int $range = 2, string $nonce = '' ): void {
$paged = intval( $paged );
$pages = intval( $pages );
$range = intval( $range );
$showitems = ( $range * 2 ) + 1;

$base_link = add_query_arg(
array(
'paged' => 1,
'nonce' => $nonce,
),
remove_query_arg( array( 'paged', 'nonce' ) )
);

if ( 1 !== $pages ) {
echo '<ul class="pagination">';
if ( 2 < $paged && $paged > $range + 1 && $showitems < $pages ) {
echo '<li class="first"><a href="' . esc_url( get_pagenum_link( 1 ) ) . '">&laquo;</a></li>';
echo '<li class="first"><a href="' . esc_url( $base_link ) . '">&laquo;</a></li>';
}
if ( 1 < $paged && $showitems < $pages ) {
echo '<li class="prevnext"><a href="' . esc_url( get_pagenum_link( $paged - 1 ) ) . '">&lsaquo;</a></li>';
echo '<li class="prevnext"><a href="' . esc_url(
add_query_arg(
array(
'paged' => $paged - 1,
'nonce' => $nonce,
)
)
) . '">&lsaquo;</a></li>';
}
for ( $i = 1; $i <= $pages; $i++ ) {
if ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) {
echo ( $paged === $i )
? '<li class="current"><span>' . esc_html( (string) $i ) . '</span></li>'
: '<li><a href="' . esc_url( get_pagenum_link( (int) $i ) ) . '">' . esc_html( (string) $i ) . '</a></li>';
: '<li><a href="' . esc_url(
add_query_arg(
array(
'paged' => $i,
'nonce' => $nonce,
)
)
) . '">' . esc_html( (string) $i ) . '</a></li>';
}
}
if ( $paged < $pages && $showitems < $pages ) {
echo '<li class="prevnext"><a href="' . esc_url( get_pagenum_link( $paged + 1 ) ) . '">&rsaquo;</a></li>';
echo '<li class="prevnext"><a href="' . esc_url(
add_query_arg(
array(
'paged' => $paged + 1,
'nonce' => $nonce,
)
)
) . '">&rsaquo;</a></li>';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ) {
echo '<li class="last"><a href="' . esc_url( get_pagenum_link( $pages ) ) . '">&raquo;</a></li>';
echo '<li class="last"><a href="' . esc_url(
add_query_arg(
array(
'paged' => $pages,
'nonce' => $nonce,
)
)
) . '">&raquo;</a></li>';
}
echo '</ul>';
}
Expand Down

0 comments on commit 30f1392

Please sign in to comment.