assets-build
2 years ago
block-parsers
2 years ago
block-renders
2 years ago
block-sanitizers
2 years ago
block-types
2 years ago
blocks-metadata
2 years ago
module.php
2 years ago
module.php
182 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Advanced_Choices; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Module as BlocksModule; |
| 7 | use JFB_Modules\Advanced_Choices\Block_Parsers\Choices_Field_Parser; |
| 8 | use JFB_Modules\Advanced_Choices\Block_Sanitizers\Choices_Default_Value_Sanitizer; |
| 9 | use JFB_Modules\Advanced_Choices\Block_Sanitizers\Choice_Single_Context_Sanitizer; |
| 10 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 11 | use JFB_Components\Module\Base_Module_Handle_It; |
| 12 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 13 | use JFB_Components\Module\Base_Module_It; |
| 14 | use JFB_Components\Module\Base_Module_Dir_It; |
| 15 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 16 | use JFB_Modules\Advanced_Choices\Block_Types; |
| 17 | use JFB_Components\Module\Base_Module_Url_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 19 | use Jet_Form_Builder\Plugin; |
| 20 | use JFB_Modules\Block_Sanitizer; |
| 21 | use JFB_Modules\Block_Parsers; |
| 22 | |
| 23 | // If this file is called directly, abort. |
| 24 | if ( ! defined( 'WPINC' ) ) { |
| 25 | die; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @since 3.1.0 |
| 30 | * |
| 31 | * Class Module |
| 32 | * @package JFB_Modules\Advanced_Choices |
| 33 | */ |
| 34 | class Module implements |
| 35 | Base_Module_It, |
| 36 | Base_Module_Dir_It, |
| 37 | Base_Module_Handle_It, |
| 38 | Base_Module_Url_It, |
| 39 | Base_Module_After_Install_It { |
| 40 | |
| 41 | use Base_Module_Dir_Trait; |
| 42 | use Base_Module_Handle_Trait; |
| 43 | use Base_Module_Url_Trait; |
| 44 | |
| 45 | public function rep_item_id() { |
| 46 | return 'advanced-choices'; |
| 47 | } |
| 48 | |
| 49 | public function condition(): bool { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @noinspection PhpUnhandledExceptionInspection |
| 55 | */ |
| 56 | public function on_install() { |
| 57 | /** @var Block_Parsers\Module $parsers */ |
| 58 | $parsers = jet_form_builder()->module( 'block-parsers' ); |
| 59 | |
| 60 | $parsers->install( new Choices_Field_Parser() ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @noinspection PhpUnhandledExceptionInspection |
| 65 | */ |
| 66 | public function on_uninstall() { |
| 67 | /** @var Block_Parsers\Module $parsers */ |
| 68 | $parsers = jet_form_builder()->module( 'block-parsers' ); |
| 69 | |
| 70 | $parsers->uninstall( new Choices_Field_Parser() ); |
| 71 | } |
| 72 | |
| 73 | public function init_hooks() { |
| 74 | add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 75 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 76 | |
| 77 | if ( ! jet_form_builder()->has_module( 'jet-style' ) ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 82 | add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 83 | |
| 84 | // compatibility with WordPress <= 6.2 |
| 85 | if ( ! function_exists( 'wp_is_development_mode' ) ) { |
| 86 | add_filter( |
| 87 | 'block_type_metadata', |
| 88 | array( $this, 'migrate_block_supports' ), |
| 89 | 0 |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function remove_hooks() { |
| 95 | remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 96 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 97 | |
| 98 | if ( ! jet_form_builder()->has_module( 'jet-style' ) ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 103 | remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 104 | |
| 105 | if ( ! function_exists( 'wp_is_development_mode' ) ) { |
| 106 | remove_filter( |
| 107 | 'block_type_metadata', |
| 108 | array( $this, 'migrate_block_supports' ), |
| 109 | 0 |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function add_blocks_types( array $block_types ): array { |
| 115 | $types = jet_form_builder()->has_module( 'jet-style' ) |
| 116 | ? array( |
| 117 | new Block_Types\Choices_Field(), |
| 118 | new Block_Types\Choice(), |
| 119 | new Block_Types\Choice_Control(), |
| 120 | ) : array( |
| 121 | new Block_Types\Choices_Field_Not_Supported(), |
| 122 | ); |
| 123 | |
| 124 | return array_merge( $block_types, $types ); |
| 125 | } |
| 126 | |
| 127 | public function register_frontend_scripts() { |
| 128 | wp_register_script( |
| 129 | $this->get_handle(), |
| 130 | $this->get_url( 'assets-build/js/frontend/choices.field.js' ), |
| 131 | array( |
| 132 | BlocksModule::MAIN_SCRIPT_HANDLE, |
| 133 | ), |
| 134 | Plugin::instance()->get_version(), |
| 135 | true |
| 136 | ); |
| 137 | wp_register_style( |
| 138 | $this->get_handle(), |
| 139 | $this->get_url( 'assets-build/css/main.css' ), |
| 140 | array(), |
| 141 | jet_form_builder()->get_version() |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | public function enqueue_admin_assets() { |
| 146 | $path = jet_form_builder()->has_module( 'jet-style' ) |
| 147 | ? 'assets-build/js/editor/main.js' |
| 148 | : 'assets-build/js/editor-not-supported/main.js'; |
| 149 | |
| 150 | wp_enqueue_script( |
| 151 | $this->get_handle(), |
| 152 | $this->get_url( $path ), |
| 153 | array(), |
| 154 | jet_form_builder()->get_version(), |
| 155 | true |
| 156 | ); |
| 157 | |
| 158 | wp_enqueue_style( |
| 159 | $this->get_handle(), |
| 160 | $this->get_url( 'assets-build/css/main.css' ), |
| 161 | array(), |
| 162 | jet_form_builder()->get_version() |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | public function migrate_block_supports( array $metadata ): array { |
| 167 | $need_to_migrate = array( |
| 168 | 'jet-forms/choices-field', |
| 169 | 'jet-forms/choice', |
| 170 | ); |
| 171 | |
| 172 | if ( ! in_array( $metadata['name'], $need_to_migrate, true ) ) { |
| 173 | return $metadata; |
| 174 | } |
| 175 | |
| 176 | $metadata['supports']['__experimentalLayout'] = $metadata['supports']['layout']; |
| 177 | unset( $metadata['supports']['layout'] ); |
| 178 | |
| 179 | return $metadata; |
| 180 | } |
| 181 | } |
| 182 |