Skip to content

Commit

Permalink
Fix post per page and offset handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iansvo committed Nov 29, 2024
1 parent ded895e commit df17294
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 7 deletions.
13 changes: 7 additions & 6 deletions includes/blocks/class-query-page-numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function render_block( $attributes, $block_content, $block ) {
$page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
$args = $block->context['generateblocks/query'] ?? [];
$per_page = $args['per_page'] ?? apply_filters( 'generateblocks_query_per_page_default', 10, $args );
$per_page = $args['posts_per_page'] ?? apply_filters( 'generateblocks_query_per_page_default', 10, $args );
$content = '';
$mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null;
$inherit_query = $block->context['generateblocks/inheritQuery'] ?? false;
Expand All @@ -39,19 +39,20 @@ public static function render_block( $attributes, $block_content, $block ) {

$content = paginate_links( $paginate_args );
} else {
$query_data = $block->context['generateblocks/queryData'] ?? null;
$query_type = $block->context['generateblocks/queryType'] ?? GenerateBlocks_Block_Query::TYPE_WP_QUERY;
$is_wp_query = GenerateBlocks_Block_Query::TYPE_WP_QUERY === $query_type;
$query_data = $block->context['generateblocks/queryData'] ?? null;
$max_pages = $block->context['generateblocks/maxPages'] ?? 0;

if ( ! $query_data || ( ! $is_wp_query && ! is_array( $query_data ) ) ) {
if( !$query_data ) {
return '';
}

$query_data;

$paginate_args = array(
'base' => '%_%',
'format' => "?$page_key=%#%",
'current' => max( 1, $page ),
'total' => $is_wp_query ? $query_data->max_num_pages : ceil( count( $query_data ) / $per_page ),
'total' => $max_pages,
'prev_next' => false,
);

Expand Down
3 changes: 3 additions & 0 deletions includes/blocks/class-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public static function render_block( $attributes, $block_content, $block ) {
}
}

$max_pages = $query_data['max_num_pages'] ?? $query_data['data']->max_num_pages ?? 0;

$parsed_content = (
new WP_Block(
$block->parsed_block,
Expand All @@ -129,6 +131,7 @@ public static function render_block( $attributes, $block_content, $block ) {
'generateblocks/queryData' => $query_data['data'],
'generateblocks/query' => $query_data['args'],
'generateblocks/queryType' => $query_type,
'generateblocks/maxPages' => $max_pages,
)
)
)->render( array( 'dynamic' => false ) );
Expand Down
3 changes: 2 additions & 1 deletion includes/dynamic-tags/class-dynamic-tag-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,10 @@ public static function get_next_posts_page_url( $options, $block, $instance ) {
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
$args = $instance->context['generateblocks/query'] ?? [];
$inherit_query = $instance->context['generateblocks/inheritQuery'] ?? false;
$per_page = $args['per_page'] ?? apply_filters( 'generateblocks_query_per_page_default', 10, $args );
$per_page = $args['posts_per_page'] ?? apply_filters( 'generateblocks_query_per_page_default', 10, $args );
$output = '';


if ( $inherit_query ) {
global $wp_query, $paged;

Expand Down
1 change: 1 addition & 0 deletions src/blocks/element/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"usesContext": [
"generateblocks/queryData",
"generateblocks/maxPages",
"generateblocks/queryType",
"generateblocks/paginationType",
"postId"
Expand Down
1 change: 1 addition & 0 deletions src/blocks/loop-item/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
],
"usesContext": [
"generateblocks/queryData",
"generateblocks/maxPages",
"generateblocks/queryType",
"generateblocks/loopIndex",
"generateblocks/loopItem",
Expand Down
1 change: 1 addition & 0 deletions src/blocks/looper/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"generateblocks/query",
"generateblocks/inheritQuery",
"generateblocks/queryData",
"generateblocks/maxPages",
"generateblocks/noResults",
"generateblocks/queryType",
"generateblocks/loopIndex",
Expand Down
1 change: 1 addition & 0 deletions src/blocks/query-page-numbers/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
],
"usesContext": [
"generateblocks/queryData",
"generateblocks/maxPages",
"generateblocks/queryType",
"generateblocks/query",
"generateblocks/queryId",
Expand Down
1 change: 1 addition & 0 deletions src/blocks/text/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"postType",
"generateblocks/query",
"generateblocks/queryData",
"generateblocks/maxPages",
"generateblocks/queryType",
"generateblocks/inheritQuery",
"generateblocks/queryId",
Expand Down

0 comments on commit df17294

Please sign in to comment.