| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\BlockLibrary; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provide anchor support for blocks. |
| 7 |
*/ |
| 8 |
class BlockCurrencyConversionSupportService { |
| 9 |
/** |
| 10 |
* Register block support. |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
public function bootstrap() { |
| 15 |
add_filter( 'pre_render_block', [ $this, 'applySupport' ], 10, 2 ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Apply the currency conversion support. |
| 20 |
* |
| 21 |
* @param string $pre_render Pre-render content. |
| 22 |
* @param array $parsed_block Parsed block. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function applySupport( $pre_render, $parsed_block ) { |
| 27 |
// Get the block type object. |
| 28 |
$block_type = \WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); |
| 29 |
|
| 30 |
// Should the block convert currency? (either true or false). |
| 31 |
$support = $block_type->supports['currencyConversion'] ?? null; |
| 32 |
|
| 33 |
if ( ! empty( $block_type ) && null !== $support ) { |
| 34 |
\SureCart::currency()->convert( wp_validate_boolean( $support ) ); |
| 35 |
} |
| 36 |
|
| 37 |
// Returning '' means "go ahead and render normally". |
| 38 |
return $pre_render; |
| 39 |
} |
| 40 |
} |
| 41 |
|