module.php
97 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_After_Install_It; |
| 10 | use JFB_Components\Module\Base_Module_It; |
| 11 | |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * @since 3.1.0 |
| 18 | * |
| 19 | * Class Wp_Experiments |
| 20 | * @package JFB_Modules\Wp_Experiments |
| 21 | */ |
| 22 | class Module implements Base_Module_It, Base_Module_After_Install_It { |
| 23 | |
| 24 | const SUPPORT_CUSTOM_LAYOUT = 'jetCustomWrapperLayout'; |
| 25 | |
| 26 | private $layout_prop = 'layout'; |
| 27 | |
| 28 | public function rep_item_id() { |
| 29 | return 'wp-experiments'; |
| 30 | } |
| 31 | |
| 32 | public function condition(): bool { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | public function init_hooks() { |
| 37 | add_filter( |
| 38 | 'block_type_metadata', |
| 39 | array( $this, 'disable_layout_support' ) |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public function remove_hooks() { |
| 44 | remove_filter( |
| 45 | 'block_type_metadata', |
| 46 | array( $this, 'disable_layout_support' ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function on_install() { |
| 51 | if ( function_exists( 'wp_is_development_mode' ) ) { |
| 52 | return; |
| 53 | } |
| 54 | $this->layout_prop = '__experimentalLayout'; |
| 55 | } |
| 56 | |
| 57 | public function on_uninstall() { |
| 58 | } |
| 59 | |
| 60 | public function disable_layout_support( array $metadata ): array { |
| 61 | if ( |
| 62 | ! Block_Helper::is_field( $metadata['name'] ) || |
| 63 | ! is_array( $metadata['supports'] ) || |
| 64 | ! Array_Tools::get( $metadata['supports'], array( self::SUPPORT_CUSTOM_LAYOUT ), false ) |
| 65 | ) { |
| 66 | return $metadata; |
| 67 | } |
| 68 | |
| 69 | $metadata['supports'][ self::SUPPORT_CUSTOM_LAYOUT ] = $metadata['supports'][ $this->layout_prop ]; |
| 70 | unset( $metadata['supports'][ $this->layout_prop ] ); |
| 71 | |
| 72 | return $metadata; |
| 73 | } |
| 74 | |
| 75 | public function enable_native_layout() { |
| 76 | $block_type = Block_Helper::current_block_type(); |
| 77 | |
| 78 | if ( ! $block_type ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $block_type->supports[ $this->layout_prop ] = $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ]; |
| 83 | unset( $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ] ); |
| 84 | } |
| 85 | |
| 86 | public function remove_native_layout() { |
| 87 | $block_type = Block_Helper::current_block_type(); |
| 88 | |
| 89 | if ( ! $block_type ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $block_type->supports[ self::SUPPORT_CUSTOM_LAYOUT ] = $block_type->supports[ $this->layout_prop ]; |
| 94 | unset( $block_type->supports[ $this->layout_prop ] ); |
| 95 | } |
| 96 | } |
| 97 |