CampaignCommentsShortcode.php
1 month ago
CampaignDonationsShortcode.php
1 year ago
CampaignDonorsShortcode.php
1 year ago
CampaignFormShortcode.php
1 year ago
CampaignGoalShortcode.php
1 year ago
CampaignGridShortcode.php
1 year ago
CampaignShortcode.php
1 year ago
CampaignStatsShortcode.php
1 year ago
ShortcodeRenderController.php
1 year ago
ShortcodeRenderController.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Shortcodes; |
| 4 | |
| 5 | /** |
| 6 | * Controller for rendering blocks in shortcode context. |
| 7 | * |
| 8 | * This controller ensures that WordPress block functions like get_block_wrapper_attributes() |
| 9 | * work properly when rendering blocks through shortcodes by setting up the proper |
| 10 | * block context that WordPress expects. |
| 11 | * |
| 12 | * @since 4.7.0 |
| 13 | */ |
| 14 | class ShortcodeRenderController |
| 15 | { |
| 16 | /** |
| 17 | * Renders a block file with proper WordPress block context. |
| 18 | * |
| 19 | * This method temporarily sets up the block context that WordPress block functions |
| 20 | * expect, renders the block file, then restores the previous context. |
| 21 | * |
| 22 | * @since 4.7.0 |
| 23 | * |
| 24 | * @param string $renderFilePath The absolute path to the block render file |
| 25 | * @param string $blockName The registered block name (e.g., 'givewp/campaign-stats-block') |
| 26 | * @param array $attributes The block attributes |
| 27 | * @param array $extraVars Optional. Additional variables to make available in the render file |
| 28 | * |
| 29 | * @return string The rendered block HTML |
| 30 | */ |
| 31 | public static function renderWithBlockContext( |
| 32 | string $renderFilePath, |
| 33 | string $blockName, |
| 34 | array $attributes, |
| 35 | array $extraVars = [] |
| 36 | ): string { |
| 37 | // Create a proper parsed block structure |
| 38 | $parsed_block = [ |
| 39 | 'blockName' => $blockName, |
| 40 | 'attrs' => $attributes, |
| 41 | ]; |
| 42 | |
| 43 | // Create a proper WP_Block instance if the block type is registered |
| 44 | $block_type = \WP_Block_Type_Registry::get_instance()->get_registered($blockName); |
| 45 | if ($block_type) { |
| 46 | $block = new \WP_Block($parsed_block, []); |
| 47 | } else { |
| 48 | // Fallback to mock object if block type isn't registered |
| 49 | $block = (object) [ |
| 50 | 'blockName' => $blockName, |
| 51 | 'attributes' => $attributes, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | // Set the block context for WordPress block supports (needed for get_block_wrapper_attributes) |
| 56 | $previous_block_to_render = \WP_Block_Supports::$block_to_render; |
| 57 | \WP_Block_Supports::$block_to_render = $parsed_block; |
| 58 | |
| 59 | // Extract extra variables to make them available in the render file |
| 60 | if (!empty($extraVars)) { |
| 61 | extract($extraVars, EXTR_SKIP); |
| 62 | } |
| 63 | |
| 64 | ob_start(); |
| 65 | include $renderFilePath; |
| 66 | $output = ob_get_clean(); |
| 67 | |
| 68 | // Restore the previous block context |
| 69 | \WP_Block_Supports::$block_to_render = $previous_block_to_render; |
| 70 | |
| 71 | return $output; |
| 72 | } |
| 73 | } |
| 74 |