This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Single Product Template > Ensure extensions can't trigger fatal errors on templates without any post content blocks #10128
Merged
nefeline
merged 1 commit into
trunk
from
fix/single-product-compatibility-layer-fatal-error
Jul 7, 2023
Merged
Single Product Template > Ensure extensions can't trigger fatal errors on templates without any post content blocks #10128
nefeline
merged 1 commit into
trunk
from
fix/single-product-compatibility-layer-fatal-error
Jul 7, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…nstance of WC_Product.
The release ZIP for this PR is accessible via:
Script Dependencies ReportThere is no changed script dependency between this branch and trunk. This comment was automatically generated by the TypeScript Errors Report
🎉 🎉 This PR does not introduce new TS errors. |
Size Change: 0 B Total Size: 1.35 MB ℹ️ View Unchanged
|
imanish003
approved these changes
Jul 7, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @nefeline, I tested the changes & everything is working as expected. Great job 🙌🏻
Additionally, Thank you for the exceptional description you provided. It is incredibly clear and easy to understand, making it a breeze to review the code. Keep up the fantastic work! 🚀
I'm adding this PR to the current milestone for the release 10.7.0 |
gigitux
pushed a commit
that referenced
this pull request
Jul 31, 2023
…nstance of WC_Product. (#10128)
gigitux
added a commit
that referenced
this pull request
Jul 31, 2023
* Fix: WooCommerce Blocks causing malfunction of the navigation block on WordPress 6.3 (#10388) * Make sure the revert button is registered and enqueued for usage exclusively in the site editor. * Register and enqueue the styles for the revert button. * Rename the files. * Revert "Rename the file renames." This reverts commit c0330ce. * Add plugin-proposal-optional-chaining to the WebPack plugins config. * Rename the files. * Remove unnecessary dependencies as those are already provided via : more specifically, get_script_data. * Address CR. * Cart and Checkout Page Migration: Inherit Page template and fix rendering (#10375) * Change shortcode block render callback * Inherit template from original "page" template * Check if WordPress version is higher than 6.2.2 to make Products block compatible with Gutenberg 16+ (#10360) * Check if WordPress version is higher than 6.2.2 to make Products block compatible with Gutenberg 16+ * Extract the logic of checking the post template support for grid view toi separate function * Change the versions comparison and improve description of custom version compare function * Render Checkout/Cart containing pages without template overriding (#10359) * Render Checkout/Cart containing pages without template overriding * Fix checkout typo --------- Co-authored-by: Mike Jolley <mike.jolley@me.com> * Add to cart form: Fix fatal error when missing `product` param in add_to_cart_redirect_filter (#10316) * Transform product param optional in add_to_cart_redirect_filter method * Remove unnecessary argument from add_to_cart_redirect_filter * Remove mocked filter call * Remove unnecessary argument from function docs * Remove opinionated styles from Product Hero pattern (#10255) * Mini Cart: Remove deprecated print_inline_script() (#10165) * Mini Cart: Replace the deprecated print_inline_script() with supported get_inline_script_data(). Fixes #10004 * Mini Cart: Add version check for the new get_inline_script_data() function * Update the variable names and fix a typo * Mini Cart: Add regex to check for the WP version * Abstract the WP version comparison regex to a separate Utils class * Call wc_setup_product_data if the global product variable is not an instance of WC_Product. (#10128) * Empty commit for release pull request * add testing instructions * Per block stylesheets (#9831) * Clean up blank lines * Update Webpack config * Update PHP logic to load block styles * Style fixes * Style fixes (II) * Style fixes (III) * Style fixes (IV) * Fix missing stylesheets in the Site Editor * Fix wrong return values in some PHP method docs * Fix missing ProductPrice stylesheet causing 404 in tests * Fix missing ProductGallery stylesheet causing 404 in tests * update testing instructions * add zip link * update version * update zip link * remove screenshot * remove testing instructions * Register Legacy block for all taxonomy-product_ template (#10382) * WIP * improve logic * improve unit test * $post validation on Cart and Checkout template (#10410) Co-authored-by: Luigi Teschio <gigitux@gmail.com> * Update zip --------- Co-authored-by: Patricia Hillebrandt <patriciahillebrandt@gmail.com> Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Karol Manijak <20098064+kmanijak@users.noreply.github.com> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com> Co-authored-by: Alexandre Lara <allexandrelara@gmail.com> Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com> Co-authored-by: Daniel Dudzic <daniel.dudzic@automattic.com> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Luigi <gigitux@gmail.com>
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
focus: template
Related to API powering block template functionality in the Site Editor
type: bug
The issue/PR concerns a confirmed bug.
type: compatibility
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10126
When plugin developers add a callback function to an action or filter hook within the Single Product template, it is not uncommon for them to assume the
global $product
variable is always available and an instance ofWC_Product
.While this assumption is not ideal (plugin developers should be guarding against fatal errors), this has been a relatively "safe" practice for the classic Product Template, as the core of WooCommerce ensures this variable is properly set via the
woocommerce_content
, which calls thethe_post()
function. Whenthe_post
is called, WooCommerce then puts product data into a global viawc_setup_product_data
.With the blockified version of the Single Product template, though, the presence of the global
$product
as an instance ofWC_Product
is not reliable, as it currently depends on the presence of at least one block that invokes thethe_post()
to work as expected.In other words, by simply removing both the Product Details and the Related Products blocks from the blockified template, the global
$product
variable is not available as an object anymore, resulting in a fatal error for any extensions with callback functions that assume the global$product
is aWC_Product
.Changes introduced by this PR
To solve the problem, it is now ensured the
$product
global variable is an instance of theWC_Product
right before adding the compatibility layer for the single product template.Discussion
Since the value set for the global
$product
matches the one expected for it within the context of the Single Product template, the variable is not restored to its original value after. I believe this is a safe assumption, considering this is what the core of WooCommerce does for this template as well, but opening the topic for discussion in case folks have any concerns.Testing
User Facing Testing
Product Details
and theRelated Products
blocks and save.woocommerce-gutenberg-products-block.php
file), add the following:WooCommerce Visibility
Performance Impact
Changelog