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