Skip to content

Add type check for product to prevent fatals #60277

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

Merged
merged 9 commits into from
Aug 11, 2025

Conversation

nerrad
Copy link
Contributor

@nerrad nerrad commented Aug 8, 2025

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR adds a type check for the product object in the clear_transients method to prevent fatal errors when the product might not exist or be invalid.

The change modifies plugins/woocommerce/includes/class-wc-comments.php to:

  • First get the product using wc_get_product( $post_id )
  • Then check if it's actually a WC_Product instance before proceeding with the operations
  • This prevents fatal errors when wc_get_product() returns false or an invalid object

The fix was implemented because we were seeing a fatal in some error logs in a hosting environment where this showed up in the stack trace. I suspect the particular environment was executing the call to wc_get_product to early in the request which resulted in a fatal error:

Fatal error: Uncaught Error: Call to a member function set_rating_counts() on false in [...]/public_html/wp-content/plugins/woocommerce/woocommerce-9.6.1/includes/class-wc-comments.php:211

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Test with valid product ID:

    • Create a product in WooCommerce
    • Add a review/comment to the product
    • Verify that the clear_transients method works correctly and updates the product's rating data
  2. Test with invalid product ID:

    • Try to call the clear_transients method with a non-existent product ID
    • Verify that no fatal error occurs and the method handles the invalid product gracefully
  3. Test with non-product post ID:

    • Try to call the clear_transients method with a post ID that exists but is not a product (e.g., a page or post)
    • Verify that no fatal error occurs and the method handles the non-product post gracefully

Testing that has already taken place:

Changelog entry

  • Automatically create a changelog entry from the details below.
  • This Pull Request does not require a changelog entry. (Comment required below)
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Add type check for product to prevent fatals in WC_Comments::clear_transients method.

Changelog Entry Comment

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 8, 2025
@nerrad nerrad marked this pull request as ready for review August 8, 2025 16:09
@nerrad nerrad self-assigned this Aug 8, 2025
@nerrad nerrad requested a review from prettyboymp August 8, 2025 16:10
Copy link
Contributor

github-actions bot commented Aug 8, 2025

Testing Guidelines

Hi @prettyboymp ,

Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed.

Reminder: PR reviewers are required to document testing performed. This includes:

  • 🖼️ Screenshots or screen recordings.
  • 📝 List of functionality tested / steps followed.
  • 🌐 Site details (environment attributes such as hosting type, plugins, theme, store size, store age, and relevant settings).
  • 🔍 Any analysis performed, such as assessing potential impacts on environment attributes and other plugins, conducting performance profiling, or using LLM/AI-based analysis.

⚠️ Within the testing details you provide, please ensure that no sensitive information (such as API keys, passwords, user data, etc.) is included in this public issue.

Copy link
Contributor

coderabbitai bot commented Aug 8, 2025

📝 Walkthrough

Walkthrough

A type check was added to the WC_Comments::clear_transients method to ensure the product object is valid before performing operations, preventing potential fatal errors. The method now sanitizes and validates the post ID, checks the post type early, retrieves the product object, and verifies its instance before updating. Additional tests were introduced to verify correct behavior for valid, invalid, and non-product inputs.

Changes

Cohort / File(s) Change Summary
Core Logic Update
plugins/woocommerce/includes/class-wc-comments.php
Updated clear_transients to sanitize and validate $post_id, return early if invalid or not a product post type, then retrieve the product object and check its instance before updating rating counts and saving. This prevents fatal errors by ensuring operations only occur on valid WC_Product instances.
Test Coverage
plugins/woocommerce/tests/php/includes/class-wc-comments-test.php
Added three test methods covering clear_transients with a valid product ID, an invalid product ID, and a non-product post ID. These tests confirm the method handles all cases without errors and updates product data correctly when appropriate.
Changelog
plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals
Added a changelog entry describing the patch fix that adds a type check in clear_transients to prevent fatal errors by verifying the product object before proceeding.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant WC_Comments
    participant wc_get_product
    participant WC_Product

    Caller->>WC_Comments: clear_transients(post_id)
    WC_Comments->>WC_Comments: sanitize and validate post_id
    alt post_id invalid or not 'product' post type
        WC_Comments-->>Caller: return early
    else valid product post_id
        WC_Comments->>wc_get_product: wc_get_product(post_id)
        wc_get_product-->>WC_Comments: product_obj
        alt product_obj is instance of WC_Product
            WC_Comments->>WC_Product: update rating counts, average rating, review count
            WC_Comments->>WC_Product: save()
        else product_obj not WC_Product
            WC_Comments-->>Caller: do nothing
        end
    end
    WC_Comments-->>Caller: return
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33aae77 and 22f6a39.

📒 Files selected for processing (1)
  • plugins/woocommerce/includes/class-wc-comments.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugins/woocommerce/includes/class-wc-comments.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
  • GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Lint - @woocommerce/plugin-woocommerce
  • GitHub Check: build
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/add-type-check-for-product-to-prevent-fatals

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
plugins/woocommerce/tests/php/includes/class-wc-comments-test.php (2)

121-127: Prefer @doesNotPerformAssertions over runtime expectNotToPerformAssertions()

This test’s intent is “no crash.” The annotation communicates that better and avoids runtime state.

  /**
   * Test clear_transients with invalid product ID.
+  * @doesNotPerformAssertions
   */
 public function test_clear_transients_with_invalid_product_id() {
   $invalid_product_id = 99999;
 
   // This should not cause any errors or exceptions.
-  $this->expectNotToPerformAssertions();
   WC_Comments::clear_transients( $invalid_product_id );
 }

132-149: Same here: use @doesNotPerformAssertions and remove the runtime call

Keeps consistency and intent clearer across tests that only verify “no fatal.”

  /**
   * Test clear_transients with non-product post ID.
+  * @doesNotPerformAssertions
   */
 public function test_clear_transients_with_non_product_post() {
@@
   // This should not cause any errors or exceptions.
-  $this->expectNotToPerformAssertions();
   WC_Comments::clear_transients( $post_id );
 
   // Clean up.
   wp_delete_post( $post_id, true );
 }
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 348045d and 0fc2563.

📒 Files selected for processing (3)
  • plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals (1 hunks)
  • plugins/woocommerce/includes/class-wc-comments.php (1 hunks)
  • plugins/woocommerce/tests/php/includes/class-wc-comments-test.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{php,js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/code-quality.mdc)

**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable

Files:

  • plugins/woocommerce/includes/class-wc-comments.php
  • plugins/woocommerce/tests/php/includes/class-wc-comments-test.php
**/*.{php,js,ts,jsx,tsx}

⚙️ CodeRabbit Configuration File

**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:

  • Guards against unexpected inputs.
  • Sanitizes and validates any potentially dangerous inputs.
  • Is backwards compatible.
  • Is readable and intuitive.
  • Has unit or E2E tests where applicable.

Files:

  • plugins/woocommerce/includes/class-wc-comments.php
  • plugins/woocommerce/tests/php/includes/class-wc-comments-test.php
🧠 Learnings (7)
📓 Common learnings
Learnt from: mreishus
PR: woocommerce/woocommerce#58817
File: plugins/woocommerce/includes/wc-product-functions.php:140-140
Timestamp: 2025-06-13T23:52:46.221Z
Learning: In WooCommerce's wc_delete_product_transients function, the context check includes both $is_admin_page (for regular admin screens) and $is_privileged_ajax (for AJAX requests from users with edit_products capability), ensuring that legitimate admin AJAX operations like product imports/exports can still delete transients.
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59499
File: plugins/woocommerce/src/Internal/ProductFilters/QueryClauses.php:327-332
Timestamp: 2025-07-10T04:22:27.648Z
Learning: In the WooCommerce ProductFilters QueryClauses class, the $chosen_taxonomies parameter in add_taxonomy_clauses() already contains only validated public product taxonomies. The validation occurs upstream in the Params class during parameter registration, so additional taxonomy existence validation in the processing methods is redundant.
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59900
File: plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/inspector.tsx:0-0
Timestamp: 2025-07-24T05:37:00.907Z
Learning: The DisplayStyleSwitcher component in plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/components/display-style-switcher/index.tsx has been updated so that its onChange prop accepts only a string type (not string | number | undefined), eliminating the need for type assertions when using this component.
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59878
File: plugins/woocommerce/src/Internal/ProductFilters/FilterData.php:364-383
Timestamp: 2025-07-23T01:06:46.155Z
Learning: In WooCommerce ProductFilters FilterData class, the $product_ids parameter in methods like get_hierarchical_taxonomy_counts() is internally generated from WP_Query results using implode(',', array_column($results, 'ID')). This creates a safe comma-separated string of integers from database IDs, not user input, so SQL injection concerns don't apply to this parameter.
📚 Learning: 2025-06-13T23:52:46.221Z
Learnt from: mreishus
PR: woocommerce/woocommerce#58817
File: plugins/woocommerce/includes/wc-product-functions.php:140-140
Timestamp: 2025-06-13T23:52:46.221Z
Learning: In WooCommerce's wc_delete_product_transients function, the context check includes both $is_admin_page (for regular admin screens) and $is_privileged_ajax (for AJAX requests from users with edit_products capability), ensuring that legitimate admin AJAX operations like product imports/exports can still delete transients.

Applied to files:

  • plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals
  • plugins/woocommerce/includes/class-wc-comments.php
  • plugins/woocommerce/tests/php/includes/class-wc-comments-test.php
📚 Learning: 2025-07-24T05:37:00.907Z
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59900
File: plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/inner-blocks/attribute-filter/inspector.tsx:0-0
Timestamp: 2025-07-24T05:37:00.907Z
Learning: The DisplayStyleSwitcher component in plugins/woocommerce/client/blocks/assets/js/blocks/product-filters/components/display-style-switcher/index.tsx has been updated so that its onChange prop accepts only a string type (not string | number | undefined), eliminating the need for type assertions when using this component.

Applied to files:

  • plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals
📚 Learning: 2025-06-20T17:38:16.565Z
Learnt from: opr
PR: woocommerce/woocommerce#0
File: :0-0
Timestamp: 2025-06-20T17:38:16.565Z
Learning: WooCommerce legacy JavaScript files in plugins/woocommerce/client/legacy/js/ must use older JavaScript syntax and cannot use modern features like optional chaining (?.) due to browser compatibility requirements. Explicit null checking with && operators should be used instead.

Applied to files:

  • plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals
📚 Learning: 2025-07-10T04:22:27.648Z
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59499
File: plugins/woocommerce/src/Internal/ProductFilters/QueryClauses.php:327-332
Timestamp: 2025-07-10T04:22:27.648Z
Learning: In the WooCommerce ProductFilters QueryClauses class, the $chosen_taxonomies parameter in add_taxonomy_clauses() already contains only validated public product taxonomies. The validation occurs upstream in the Params class during parameter registration, so additional taxonomy existence validation in the processing methods is redundant.

Applied to files:

  • plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals
📚 Learning: 2025-07-23T01:06:46.155Z
Learnt from: dinhtungdu
PR: woocommerce/woocommerce#59878
File: plugins/woocommerce/src/Internal/ProductFilters/FilterData.php:364-383
Timestamp: 2025-07-23T01:06:46.155Z
Learning: In WooCommerce ProductFilters FilterData class, the $product_ids parameter in methods like get_hierarchical_taxonomy_counts() is internally generated from WP_Query results using implode(',', array_column($results, 'ID')). This creates a safe comma-separated string of integers from database IDs, not user input, so SQL injection concerns don't apply to this parameter.

Applied to files:

  • plugins/woocommerce/includes/class-wc-comments.php
📚 Learning: 2025-06-16T09:20:22.981Z
Learnt from: gigitux
PR: woocommerce/woocommerce#58846
File: plugins/woocommerce/client/blocks/tests/e2e/tests/all-products/all-products.block_theme.spec.ts:41-52
Timestamp: 2025-06-16T09:20:22.981Z
Learning: In WooCommerce E2E tests, the database is reset to the initial state for each test, so there's no need to manually restore global template changes (like clearing the header template) as the test infrastructure handles cleanup automatically.

Applied to files:

  • plugins/woocommerce/tests/php/includes/class-wc-comments-test.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (23)
  • GitHub Check: build
  • GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
  • GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
🔇 Additional comments (2)
plugins/woocommerce/includes/class-wc-comments.php (1)

262-268: Solid guard against non-product inputs; prevents fatals

Fetching via wc_get_product() and checking instanceof WC_Product is the right fix here. It gracefully no-ops for invalid/non-product IDs and keeps behavior intact for real products.

plugins/woocommerce/changelog/60277-fix-add-type-check-for-product-to-prevent-fatals (1)

1-4: Changelog entry reads well and matches the code change

Scope and rationale are clear; classification as a patch/fix is appropriate.

Copy link
Contributor

github-actions bot commented Aug 8, 2025

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

if ( 'product' === get_post_type( $post_id ) ) {
$product = wc_get_product( $post_id );
$product = wc_get_product( $post_id );
if ( $product instanceof WC_Product ) {

Choose a reason for hiding this comment

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

I wonder about an unintended consequence of this change. By skipping the initial get_post_type() check, you open up the possibility of false being passed to wc_get_product(). This eventually gets to WC_Product_Factory::get_product_id() which will try to use the global $post as the product. This may not be the desired effect.

Conceptually, I like the simplicity of the approach, but I do worry about this potential side-effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 3aae24d

Copy link
Contributor Author

@nerrad nerrad Aug 8, 2025

Choose a reason for hiding this comment

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

Actually what I added has the same problem because get_post_type calls get_post which also reads from global if $post_id is falsey. I have a better implementation coming.

Copy link
Contributor Author

@nerrad nerrad Aug 8, 2025

Choose a reason for hiding this comment

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

See b295db5

Choose a reason for hiding this comment

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

I realized that the use of instanceof is another problem. PHP <7.3 fatals if a constant (such as false or null) is on the left side of the operator. WordPress still supports 7.2. I see that WooCommerce requires 7.4+. So, I'm not sure if this is a valid concern or not.

Copy link
Contributor Author

@nerrad nerrad Aug 8, 2025

Choose a reason for hiding this comment

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

Really? I've never encountered that. Also, tests on earlier php versions don't seem to be a problem.

Choose a reason for hiding this comment

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

Interesting. I only thought of it because I ran into such a situation not long ago. I guess I'm forgetting a specific detail of that situation. Either way, I guess it's not important since Woo requires 7.4 or above.

@nerrad
Copy link
Contributor Author

nerrad commented Aug 8, 2025

Test failures seem legit, investigating.

nerrad and others added 7 commits August 8, 2025 14:49
Check if product is WC_Product instance before calling methods to
prevent fatal errors when product might not exist or be invalid.
Add comprehensive test coverage for WC_Comments::clear_transients method
including tests for valid products, invalid product IDs, non-product posts,
and zero product IDs to ensure the type checking works correctly.
Replace assertNull with expectNotToPerformAssertions for better error testing.
This is the modern PHPUnit approach for testing that code doesn't throw errors
or exceptions, and is already used throughout the WooCommerce codebase.
@nerrad nerrad force-pushed the fix/add-type-check-for-product-to-prevent-fatals branch from b295db5 to 06a9b07 Compare August 8, 2025 18:49
Co-authored-by: Michael Pretty <prettyboymp@users.noreply.github.com>
Comment on lines 263 to 265
if ( $post_id === 0 || 'product' !== get_post_type( $post_id ) ) {
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, although get_post_type will attempt to get a post from the $post global if it is falsey. So I wonder if the initial check here should be a similar falsey check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nvm the absint is here 🤦🏻

@nerrad nerrad requested a review from prettyboymp August 8, 2025 19:41
Copy link
Contributor

@prettyboymp prettyboymp left a comment

Choose a reason for hiding this comment

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

Thank you for these fixes. Change look good.

@prettyboymp prettyboymp merged commit 934f32a into trunk Aug 11, 2025
41 checks passed
@prettyboymp prettyboymp deleted the fix/add-type-check-for-product-to-prevent-fatals branch August 11, 2025 11:53
@github-actions github-actions bot added this to the 10.2.0 milestone Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants