ProductContextShortcodeWrapper.php
1 month ago
ShortcodesBlockConversionService.php
2 years ago
ShortcodesService.php
1 month ago
ShortcodesServiceProvider.php
1 month ago
ProductContextShortcodeWrapper.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Shortcodes; |
| 4 | |
| 5 | /** |
| 6 | * Wraps the render callback for shortcodes that opt in via `['supports_product_id' => true]`. |
| 7 | */ |
| 8 | class ProductContextShortcodeWrapper { |
| 9 | |
| 10 | public function bootstrap() { |
| 11 | add_filter( 'surecart/shortcodes/render_callback', [ $this, 'maybeWrap' ], 10, 3 ); |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Wrap the render callback when the caller opted in; pass through otherwise. |
| 16 | * |
| 17 | * @param callable $callback Render callback supplied by ShortcodesService. |
| 18 | * @param string $name Shortcode name (unused — opt-in is via $options). |
| 19 | * @param array $options Caller-supplied options bag. |
| 20 | * |
| 21 | * @return callable |
| 22 | */ |
| 23 | public function maybeWrap( callable $callback, $name, array $options = array() ) { |
| 24 | if ( empty( $options['supports_product_id'] ) ) { |
| 25 | return $callback; |
| 26 | } |
| 27 | |
| 28 | return function ( $attributes = '', $content = null ) use ( $callback ) { |
| 29 | // Accept both `id` and `product_id`; `product_id` (these shortcodes' native param) wins. |
| 30 | $product_id = is_array( $attributes ) |
| 31 | ? ( $attributes['product_id'] ?? $attributes['id'] ?? '' ) |
| 32 | : ''; |
| 33 | |
| 34 | if ( empty( $product_id ) ) { |
| 35 | return $callback( $attributes, $content ); |
| 36 | } |
| 37 | |
| 38 | $product_id = (string) $product_id; |
| 39 | unset( $attributes['product_id'], $attributes['id'] ); |
| 40 | |
| 41 | // Clear so sc_get_product() doesn't short-circuit to the page's product. |
| 42 | $product = $this->withQueryVar( null, fn() => sc_get_product( $product_id ) ); |
| 43 | |
| 44 | // Unresolved id: render empty rather than silently using the page's product. |
| 45 | if ( empty( $product ) || empty( $product->id ) ) { |
| 46 | return ''; |
| 47 | } |
| 48 | |
| 49 | // Inner next-gen blocks read this query var via render_block_context. |
| 50 | return $this->withQueryVar( $product, fn() => $callback( $attributes, $content ) ); |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Run `$fn` with `surecart_current_product` temporarily set to `$value`, |
| 56 | * restoring the previous value afterward (even on exception). |
| 57 | * |
| 58 | * @param mixed $value New query var value (null clears it). |
| 59 | * @param callable $fn Callable to invoke. |
| 60 | * |
| 61 | * @return mixed |
| 62 | */ |
| 63 | protected function withQueryVar( $value, callable $fn ) { |
| 64 | $previous = get_query_var( 'surecart_current_product' ); |
| 65 | set_query_var( 'surecart_current_product', $value ); |
| 66 | |
| 67 | try { |
| 68 | return $fn(); |
| 69 | } finally { |
| 70 | set_query_var( 'surecart_current_product', $previous ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 |