Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Total: Prevent stricted type fatal errors on post per page. #69508

Merged
merged 2 commits into from
Mar 10, 2025

Conversation

cbravobernal
Copy link
Contributor

@cbravobernal cbravobernal commented Mar 10, 2025

Why?

Some hostings, with PHP updated versions and strict types, can cause a fatal error if posts_per_page is returning 0 as a string instead of an integer.

$start = ( $current_page - 1 ) * $posts_per_page + 1; this arithmetical function will cause a Fatal error if $posts_per_page is an string.

How?

Forcing it to be a an integer.

@cbravobernal cbravobernal added [Type] Bug An existing feature does not function as intended [Block] Query Total Affects the query total block labels Mar 10, 2025
@@ -31,7 +31,7 @@ function render_block_core_query_total( $attributes, $content, $block ) {
}

$max_rows = $query_to_use->found_posts;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also cast this value just to be on the safe side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think found_posts is already set as int on the query object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that found_posts is always set as int.

What about $current_page = max( 1, get_query_var( 'paged', 1 ) );? In particular, I'm worried about get_query_var as it has a mixed return value.

Copy link

github-actions bot commented Mar 10, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added the Backport to WP 6.8 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Mar 10, 2025
@@ -23,15 +23,15 @@ function render_block_core_query_total( $attributes, $content, $block ) {
$wrapper_attributes = get_block_wrapper_attributes();
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
$query_to_use = $wp_query;
$current_page = max( 1, get_query_var( 'paged', 1 ) );
$current_page = max( 1, (int) get_query_var( 'paged', 1 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_query_var also uses WP_Query::get under the hood and ``min( 1, '0' )will return"0"`.

Copy link

Flaky tests detected in 343bb4c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/13764560001
📝 Reported issues:

@Mamaduka Mamaduka merged commit 90d34ff into trunk Mar 10, 2025
61 of 62 checks passed
@Mamaduka Mamaduka deleted the fix/query-total-prevent-fatal branch March 10, 2025 13:13
@github-actions github-actions bot added this to the Gutenberg 20.5 milestone Mar 10, 2025
@github-actions github-actions bot removed the Backport to WP 6.8 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Mar 10, 2025
gutenbergplugin pushed a commit that referenced this pull request Mar 10, 2025
…9508)

Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
@github-actions github-actions bot added the Backported to WP Core Pull request that has been successfully merged into WP Core label Mar 10, 2025
Copy link

I just cherry-picked this PR to the wp/6.8 branch to get it included in the next release: 918d0cd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Block] Query Total Affects the query total block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants