module.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Block_Sanitizer; |
| 5 | |
| 6 | use JFB_Components\Module\Base_Module_It; |
| 7 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * The creation of this class is determined by the block rendering logic. |
| 16 | * Because it happens from the depths to the surface. |
| 17 | * |
| 18 | * The task was to throw the prepared value in the attribute into the child blocks. |
| 19 | * But the `render_callback` of the main block is run after the children, |
| 20 | * so changing the attributes for further transmission to the context makes no sense. |
| 21 | * |
| 22 | * The solution is to change the attributes on the `render_block_data' hook, |
| 23 | * because it is run in the correct order of block rendering - from top to bottom. |
| 24 | * |
| 25 | * @since 3.1.0 |
| 26 | * |
| 27 | * Class Module |
| 28 | * @package Jet_Form_Builder\Blocks\Block_Sanitizer |
| 29 | */ |
| 30 | final class Module implements Base_Module_It { |
| 31 | |
| 32 | /** |
| 33 | * @var Base_Block_Sanitizer[] |
| 34 | */ |
| 35 | private $sanitizers = array(); |
| 36 | |
| 37 | public function condition(): bool { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | public function rep_item_id() { |
| 42 | return 'block-sanitizer'; |
| 43 | } |
| 44 | |
| 45 | public function init_hooks() { |
| 46 | add_filter( |
| 47 | 'render_block_data', |
| 48 | array( $this, 'apply_blocks_data' ), |
| 49 | 10, |
| 50 | 2 |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | public function remove_hooks() { |
| 55 | remove_filter( |
| 56 | 'render_block_data', |
| 57 | array( $this, 'apply_blocks_data' ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function apply_blocks_data( array $block_data, array $initial_block ): array { |
| 62 | $name = $initial_block['blockName'] ?? ''; |
| 63 | |
| 64 | // is has 'jet-forms/' namespace in 'blockName' property |
| 65 | if ( ! is_string( $name ) || ! Block_Helper::is_field( $name ) ) { |
| 66 | return $block_data; |
| 67 | } |
| 68 | |
| 69 | $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( $name ); |
| 70 | |
| 71 | if ( ! ( $block_type instanceof \WP_Block_Type ) ) { |
| 72 | return $block_data; |
| 73 | } |
| 74 | |
| 75 | $name = Block_Helper::delete_namespace( $name ); |
| 76 | |
| 77 | foreach ( $this->sanitizers as $item ) { |
| 78 | if ( ! in_array( $name, $item->for_blocks(), true ) ) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | $block_data = $item->apply_data( $block_data ); |
| 83 | } |
| 84 | |
| 85 | return $block_data; |
| 86 | } |
| 87 | |
| 88 | public function register( Base_Block_Sanitizer $item ): Module { |
| 89 | $this->sanitizers[] = $item; |
| 90 | |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | public function unregister( Base_Block_Sanitizer $item ): Module { |
| 95 | foreach ( $this->sanitizers as $index => $sanitizer ) { |
| 96 | if ( get_class( $sanitizer ) !== get_class( $item ) ) { |
| 97 | continue; |
| 98 | } |
| 99 | unset( $this->sanitizers[ $index ] ); |
| 100 | } |
| 101 | |
| 102 | $this->sanitizers = array_values( $this->sanitizers ); |
| 103 | |
| 104 | return $this; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |