module.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Wp_Experiments; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 9 | use JFB_Components\Module\Base_Module_It; |
| 10 | |
| 11 | if ( ! defined( 'WPINC' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * @since 3.1.0 |
| 17 | * |
| 18 | * Class Wp_Experiments |
| 19 | * @package JFB_Modules\Wp_Experiments |
| 20 | */ |
| 21 | class Module implements Base_Module_It { |
| 22 | |
| 23 | const SUPPORT_CUSTOM_LAYOUT = 'jetCustomWrapperLayout'; |
| 24 | |
| 25 | public function rep_item_id() { |
| 26 | return 'wp-experiments'; |
| 27 | } |
| 28 | |
| 29 | public function condition(): bool { |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | public function init_hooks() { |
| 34 | add_filter( |
| 35 | 'block_type_metadata', |
| 36 | array( $this, 'disable_layout_support' ) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | public function remove_hooks() { |
| 41 | remove_filter( |
| 42 | 'block_type_metadata', |
| 43 | array( $this, 'disable_layout_support' ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function disable_layout_support( array $metadata ): array { |
| 48 | if ( |
| 49 | ! Block_Helper::is_field( $metadata['name'] ) || |
| 50 | ! is_array( $metadata['supports'] ) || |
| 51 | ! Array_Tools::get( $metadata['supports'], array( self::SUPPORT_CUSTOM_LAYOUT ), false ) |
| 52 | ) { |
| 53 | return $metadata; |
| 54 | } |
| 55 | |
| 56 | $metadata['supports'][ self::SUPPORT_CUSTOM_LAYOUT ] = $metadata['supports']['__experimentalLayout']; |
| 57 | unset( $metadata['supports']['__experimentalLayout'] ); |
| 58 | |
| 59 | return $metadata; |
| 60 | } |
| 61 | |
| 62 | public function enable_native_layout() { |
| 63 | $block_type = Block_Helper::current_block_type(); |
| 64 | |
| 65 | if ( ! $block_type ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $block_type->supports['__experimentalLayout'] = $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ]; |
| 70 | unset( $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ] ); |
| 71 | } |
| 72 | |
| 73 | public function remove_native_layout() { |
| 74 | $block_type = Block_Helper::current_block_type(); |
| 75 | |
| 76 | if ( ! $block_type ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ] = $block_type->supports['__experimentalLayout']; |
| 81 | unset( $block_type->supports['__experimentalLayout'] ); |
| 82 | } |
| 83 | } |
| 84 |