Skip to content

Conversation

@enejb
Copy link
Member

@enejb enejb commented Nov 29, 2025

Some themes overwrite the $page global.

Which should be in integer. We convert it to an interger before trying to use it.

Proposed changes:

  • Make sure that we only pass a integert into the compute_id method.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

See p1764374805068119-slack-C06QF6JJDTM

Does this pull request change what data or activity we track or use?

No

Testing instructions:

Do the tests pass?
Load a page with the form.
Load submit the form. Notice that it produces PHP Warnings.

Ensures the page number defaults to 1 if $page is not an integer when computing the contact form's ID, preventing potential issues with invalid page values.
@enejb enejb requested review from a team and Copilot November 29, 2025 00:27
@enejb enejb added [Type] Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. [Package] Forms labels Nov 29, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 29, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/php-warning-on-forms branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/php-warning-on-forms

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

Copilot finished reviewing on behalf of enejb November 29, 2025 00:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a PHP warning that occurs when themes overwrite the $page global variable with non-integer values. The fix adds type checking in the Contact_Form constructor to ensure only integer values are passed to the compute_id method, defaulting to 1 for non-integer values.

Key Changes:

  • Adds type validation for the $page global before passing it to compute_id()
  • Documents the fix in the changelog

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
projects/packages/forms/src/contact-form/class-contact-form.php Adds is_int() check to sanitize $page global before use in form ID computation
projects/packages/forms/changelog/fix-php-warning-on-forms Changelog entry documenting the fix for PHP warning with overwritten globals

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if ( $set_id ) {
$attributes['id'] = self::compute_id( $attributes, $this->current_post, $page );
$page_number = is_int( $page ) ? $page : 1;
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

The is_int() check is too strict and may incorrectly default valid numeric string values to 1. In WordPress, the $page global can be a numeric string (e.g., '2') rather than a strict integer type. Consider using is_numeric() or applying intval() directly with a null check instead:

$page_number = ( $page && is_numeric( $page ) ) ? (int) $page : 1;

or more simply:

$page_number = $page ? intval( $page ) : 1;

Since compute_id() already uses intval() on line 479, the second approach would be sufficient and more permissive for legitimate numeric values.

Suggested change
$page_number = is_int( $page ) ? $page : 1;
$page_number = $page ? intval( $page ) : 1;

Copilot uses AI. Check for mistakes.
Comment on lines +172 to +173
$page_number = is_int( $page ) ? $page : 1;
$attributes['id'] = self::compute_id( $attributes, $this->current_post, $page_number );
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

This fix for handling non-integer $page global values lacks test coverage. Consider adding a test that verifies the constructor handles non-integer $page values correctly (e.g., string values, null, objects). This would prevent regressions and validate the fix. Example test case:

public function test_constructor_handles_non_integer_page_global() {
    global $page;
    $page = 'not-an-integer'; // Simulating theme overwriting $page
    
    $attributes = array( 'to' => 'test@example.com' );
    $form = new Contact_Form( $attributes );
    
    // Verify no warnings and form is created successfully
    $this->assertInstanceOf( Contact_Form::class, $form );
}

Copilot uses AI. Check for mistakes.
@jp-launch-control
Copy link

jp-launch-control bot commented Nov 29, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/forms/src/contact-form/class-contact-form.php 894/1296 (68.98%) 0.02% 0 💚

Full summary · PHP report · JS report

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Contact Form [Package] Forms [Status] Needs Review This PR is ready for review. [Type] Bug When a feature is broken and / or not performing as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants