-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Move add_filter calls to block initialize(). Closes #61012. #61013
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
Move add_filter calls to block initialize(). Closes #61012. #61013
Conversation
Testing GuidelinesHi , 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:
|
kmanijak
left a comment
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 @helgatheviking, thanks for contribution and fixing this issue!
Current place for filters is not good either as it returns false positives but I suggested an alternative 🙌
| protected function initialize() { | ||
| parent::initialize(); | ||
| add_action( | ||
| 'wp_footer', | ||
| function () { | ||
| wc_get_template( 'single-product/photoswipe.php' ); | ||
| } | ||
| ); | ||
| add_filter( 'woocommerce_single_product_zoom_enabled', '__return_true' ); | ||
| add_filter( 'woocommerce_single_product_photoswipe_enabled', '__return_true' ); | ||
| add_filter( 'woocommerce_single_product_flexslider_enabled', '__return_true' ); |
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.
That's not a good place either since it block doesn't even have to rendered and filters would return true:
I believe they should be moved to render function after initial checks if block can be rendered here
| With Product Image Gallery | Without Product Image Gallery |
|---|---|
![]() |
![]() |
Do you see any downsides of moving it there?
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.
@kmanijak Hi Karol! :wave Specifically I am trying to piggy back in Mix and Match on whether photoswipe is enabled. your suggestion looks like it should work. I'll commit it.
📝 WalkthroughWalkthroughMoves enabling of legacy gallery filters (zoom, PhotoSwipe, flexslider) from asset-enqueue time into block render time and adds a patch changelog entry documenting that legacy gallery filters are available during block rendering. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as Frontend Request
participant B as ProductImageGallery Block
participant H as WP Hooks
participant T as PhotoSwipe Template
U->>B: render() called
activate B
B->>H: add_action('wp_footer', render PhotoSwipe template)
B->>B: add_filter(enable zoom)
B->>B: add_filter(enable photoswipe)
B->>B: add_filter(enable flexslider)
B-->>U: return sale badge + gallery markup
deactivate B
H-->>T: wp_footer triggers PhotoSwipe template
T-->>U: PhotoSwipe markup output
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. 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. Comment |
kmanijak
left a comment
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.
It tests well now for me but let's just make a cleanup and remove unnecessary changes with action moved to initialize 🙌
Once done, I'm happy to approve!
| add_action( | ||
| 'wp_footer', | ||
| function () { | ||
| wc_get_template( 'single-product/photoswipe.php' ); | ||
| } | ||
| ); |
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.
I think this can be moved to the previous place.
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.
You mean to the render callback? So we wouldn't need initialize() at all anymore
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.
I meant enqueue_legacy_assets so we have all the enqueueing in a single function (as it was on trunk).
So we wouldn't need initialize() at all anymore
Yass!
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
plugins/woocommerce/src/Blocks/BlockTypes/ProductImageGallery.php (1)
95-97: Guard gallery filter registrations and confirm lifetime
- ProductImageGallery.php (95–97) and ClassicTemplate.php (123–125) both unconditionally call
add_filterfor the same hooks. Wrap these inhas_filter(…, '__return_true')checks or extract into a shared init so each filter is added only once per request.- Verify that these filters should persist for the remainder of the request; if not, pair each
add_filterwith a matchingremove_filterimmediately after the block’s output.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
plugins/woocommerce/src/Blocks/BlockTypes/ProductImageGallery.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/src/Blocks/BlockTypes/ProductImageGallery.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/src/Blocks/BlockTypes/ProductImageGallery.php
kmanijak
left a comment
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.
Thanks for addressing my feedback and for fixing the issue in the first place! 🙌
|
Setting the milestone to 10.2, as we would like to backport it to a patch release. Incorrect filter placement causes issues with Product Image Gallery block image sizes #61138 and this PR fixes the issue. |
* Move add_filter calls to block initialize(). Closes #61012. * move legacy gallery filters into render() * Finish moving add_action() to render() as well. * Revert add_action() back to original place in enqueue_legacy_assets() --------- Co-authored-by: Karol Manijak <20098064+kmanijak@users.noreply.github.com>
|
IMPORTANT: Merging this PR to the appropriate branches is critical to the release process and ensures that the bug does not cause regressions in the future releases. Cherry picking was successful for |
Closes #61012. (#61173) Move add_filter calls to block initialize(). Closes #61012. (#61013) * Move add_filter calls to block initialize(). Closes #61012. * move legacy gallery filters into render() * Finish moving add_action() to render() as well. * Revert add_action() back to original place in enqueue_legacy_assets() --------- Co-authored-by: Kathy <507025+helgatheviking@users.noreply.github.com> Co-authored-by: Karol Manijak <20098064+kmanijak@users.noreply.github.com>


Submission Review Guidelines:
Changes proposed in this Pull Request:
Move the legacy filters to the block's
initializemethod so that they actually are fired during block render.Closes #61012.
How to test the changes in this Pull Request:
Use the MVE snippet from #61012
Without PR you should see
photoswipe disabledand with PR you should seephotoswipe enabledjust prior to the single product's add to cart form block.