module.php
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 7 | use Jet_Form_Builder\Classes\Compatibility; |
| 8 | use Jet_Form_Builder\Classes\Http\Http_Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 10 | use Jet_Form_Builder\Plugin; |
| 11 | use JET_SM\Gutenberg\Block_Manager; |
| 12 | use JFB_Components\Module\Base_Module_It; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | final class Module implements Base_Module_It { |
| 20 | |
| 21 | const MAIN_SCRIPT_HANDLE = 'jet-form-builder-frontend-forms'; |
| 22 | const LISTING_OPTIONS_HANDLE = 'jet-form-builder-listing-options'; |
| 23 | |
| 24 | // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
| 25 | private $_builder_blocks_repository; |
| 26 | private $_default_blocks_repository; |
| 27 | |
| 28 | // phpcs:enable PSR2.Classes.PropertyDeclaration.Underscore |
| 29 | |
| 30 | public function rep_item_id() { |
| 31 | return 'blocks'; |
| 32 | } |
| 33 | |
| 34 | public function condition(): bool { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | public function init_hooks() { |
| 39 | add_action( 'init', array( $this, 'init_jet_sm_block_manager' ) ); |
| 40 | add_action( 'init', array( $this, 'register_block_types' ) ); |
| 41 | |
| 42 | add_action( |
| 43 | 'jet-form-builder/editor-assets/after', |
| 44 | array( $this, 'register_block_types_for_form_editor' ), |
| 45 | 10, |
| 46 | 2 |
| 47 | ); |
| 48 | |
| 49 | add_action( |
| 50 | 'jet-form-builder/other-editor-assets/after', |
| 51 | array( $this, 'register_block_types_for_others' ), |
| 52 | 10, |
| 53 | 2 |
| 54 | ); |
| 55 | |
| 56 | if ( class_exists( 'WP_Block_Editor_Context' ) ) { |
| 57 | add_filter( 'block_categories_all', array( $this, 'add_categories' ), 999, 2 ); |
| 58 | } else { |
| 59 | add_filter( 'block_categories', array( $this, 'add_categories' ), 999, 2 ); |
| 60 | } |
| 61 | |
| 62 | add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) ); |
| 63 | add_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ), 8 ); |
| 64 | add_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) ); |
| 65 | |
| 66 | /** |
| 67 | * @link https://github.com/Crocoblock/issues-tracker/issues/1542 |
| 68 | */ |
| 69 | add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_form_scripts' ) ); |
| 70 | } |
| 71 | |
| 72 | public function remove_hooks() { |
| 73 | remove_action( 'init', array( $this, 'init_jet_sm_block_manager' ) ); |
| 74 | remove_action( 'init', array( $this, 'register_block_types' ) ); |
| 75 | |
| 76 | remove_action( |
| 77 | 'jet-form-builder/editor-assets/after', |
| 78 | array( $this, 'register_block_types_for_form_editor' ) |
| 79 | ); |
| 80 | |
| 81 | remove_action( |
| 82 | 'jet-form-builder/other-editor-assets/after', |
| 83 | array( $this, 'register_block_types_for_others' ) |
| 84 | ); |
| 85 | |
| 86 | if ( class_exists( 'WP_Block_Editor_Context' ) ) { |
| 87 | remove_filter( 'block_categories_all', array( $this, 'add_categories' ), 999 ); |
| 88 | } else { |
| 89 | remove_filter( 'block_categories', array( $this, 'add_categories' ), 999 ); |
| 90 | } |
| 91 | |
| 92 | remove_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) ); |
| 93 | remove_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ), 8 ); |
| 94 | remove_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) ); |
| 95 | |
| 96 | /** |
| 97 | * @link https://github.com/Crocoblock/issues-tracker/issues/1542 |
| 98 | */ |
| 99 | remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_form_scripts' ) ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Register block types |
| 104 | * |
| 105 | * @return void |
| 106 | */ |
| 107 | public function register_block_types() { |
| 108 | $this->default_repository()->rep_install(); |
| 109 | $this->builder_repository()->rep_install(); |
| 110 | |
| 111 | do_action( 'jet-form-builder/blocks/register', $this ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Register new block type |
| 116 | * |
| 117 | * @param Types\Base $block_type |
| 118 | * |
| 119 | * @return void |
| 120 | */ |
| 121 | public function register_block_type( Types\Base $block_type ) { |
| 122 | $this->builder_repository()->rep_install_item_soft( $block_type ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Register new block type |
| 127 | * |
| 128 | * @param Types\Base $block_type |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function register_default_block_type( Types\Base $block_type ) { |
| 133 | $this->default_repository()->rep_install_item_soft( $block_type ); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 138 | public function add_categories( $categories, $post ): array { |
| 139 | return array_merge( |
| 140 | array( |
| 141 | array( |
| 142 | 'slug' => 'jet-form-builder-fields', |
| 143 | 'title' => __( 'Jet Form Fields', 'jet-form-builder' ), |
| 144 | ), |
| 145 | ), |
| 146 | array( |
| 147 | array( |
| 148 | 'slug' => 'jet-form-builder-elements', |
| 149 | 'title' => __( 'Jet Form Elements', 'jet-form-builder' ), |
| 150 | ), |
| 151 | ), |
| 152 | $categories |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Register block types for editor |
| 158 | * |
| 159 | * @param $editor |
| 160 | * @param $handle |
| 161 | * |
| 162 | * @return void [type] [description] |
| 163 | */ |
| 164 | public function register_block_types_for_form_editor( $editor, $handle ) { |
| 165 | foreach ( $this->builder_repository()->rep_get_items() as $type ) { |
| 166 | $type->block_data( $editor, $handle ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Register block types for editor |
| 172 | * |
| 173 | * @param $editor |
| 174 | * @param $handle |
| 175 | * |
| 176 | * @return void [type] [description] |
| 177 | */ |
| 178 | public function register_block_types_for_others( $editor, $handle ) { |
| 179 | foreach ( $this->default_repository()->rep_get_items() as $type ) { |
| 180 | $type->block_data( $editor, $handle ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function enqueue_frontend_styles() { |
| 185 | wp_register_style( |
| 186 | 'jet-form-builder-frontend', |
| 187 | Plugin::instance()->plugin_url( 'assets/build/frontend/main.css' ), |
| 188 | array(), |
| 189 | Plugin::instance()->get_version() |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Register form JS |
| 195 | * |
| 196 | * @return void |
| 197 | */ |
| 198 | public function enqueue_frontend_assets() { |
| 199 | $this->register_form_scripts(); |
| 200 | |
| 201 | wp_enqueue_script( self::MAIN_SCRIPT_HANDLE ); |
| 202 | } |
| 203 | |
| 204 | public function register_form_scripts() { |
| 205 | /** @var \JFB_Modules\Jet_Plugins\Module $jet_plugins */ |
| 206 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 207 | $jet_plugins = jet_form_builder()->module( 'jet-plugins' ); |
| 208 | $jet_plugins->register_scripts(); |
| 209 | |
| 210 | $script_asset = require_once jet_form_builder()->plugin_dir( 'assets/build/frontend/main.asset.php' ); |
| 211 | |
| 212 | if ( true === $script_asset ) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | $script_asset['dependencies'][] = $jet_plugins::HANDLE; |
| 217 | |
| 218 | wp_register_script( |
| 219 | self::MAIN_SCRIPT_HANDLE, |
| 220 | Plugin::instance()->plugin_url( 'assets/build/frontend/main.js' ), |
| 221 | $script_asset['dependencies'], |
| 222 | $script_asset['version'], |
| 223 | true |
| 224 | ); |
| 225 | |
| 226 | $options = Tab_Handler_Manager::get_options( 'options-tab' ); |
| 227 | |
| 228 | wp_localize_script( |
| 229 | self::MAIN_SCRIPT_HANDLE, |
| 230 | 'JetFormBuilderSettings', |
| 231 | apply_filters( |
| 232 | 'jet-form-builder/frontend-settings', |
| 233 | array_merge( |
| 234 | array( |
| 235 | 'ajaxurl' => Http_Tools::get_form_action_url( |
| 236 | array( 'method' => 'ajax' ) |
| 237 | ), |
| 238 | 'devmode' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG, |
| 239 | 'replaceAttrs' => array( |
| 240 | 'href', |
| 241 | 'src', |
| 242 | 'alt', |
| 243 | 'title', |
| 244 | ), |
| 245 | ), |
| 246 | $options |
| 247 | ) |
| 248 | ) |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns toolbar controls list from attributes |
| 254 | * |
| 255 | * @return [type] [description] |
| 256 | */ |
| 257 | public function get_controls_list( $attributes = array(), $context = 'toolbar' ): array { |
| 258 | |
| 259 | $result = array(); |
| 260 | |
| 261 | foreach ( $attributes as $key => $data ) { |
| 262 | if ( ! empty( $data[ $context ] ) ) { |
| 263 | $result[] = array( |
| 264 | 'key' => $key, |
| 265 | 'type' => $data[ $context ]['type'], |
| 266 | 'label' => $data[ $context ]['label'], |
| 267 | 'options' => $data[ $context ]['options'] ?? array(), |
| 268 | 'condition' => $data[ $context ]['condition'] ?? false, |
| 269 | // for Submit field name. |
| 270 | 'show' => $data[ $context ]['show'] ?? true, |
| 271 | // for Date and Time field. |
| 272 | 'help' => $data[ $context ]['help'] ?? '', |
| 273 | ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return $result; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @param $block_name |
| 282 | * @param $attributes |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public function get_field_attrs( $block_name, $attributes ): array { |
| 287 | if ( ! $block_name ) { |
| 288 | return array(); |
| 289 | } |
| 290 | $block_id = Block_Helper::delete_namespace( $block_name ); |
| 291 | |
| 292 | try { |
| 293 | $field = $this->builder_repository()->rep_get_item( $block_id ); |
| 294 | } catch ( Repository_Exception $exception ) { |
| 295 | return array(); |
| 296 | } |
| 297 | |
| 298 | return array_merge( $field->get_default_attributes(), $attributes ); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * @param $block_name |
| 304 | * |
| 305 | * @return Types\Base|bool |
| 306 | */ |
| 307 | public function get_field_by_name( $block_name ) { |
| 308 | $block_id = Block_Helper::delete_namespace( $block_name ); |
| 309 | |
| 310 | try { |
| 311 | return $this->builder_repository()->rep_clone_item( $block_id ); |
| 312 | } catch ( Repository_Exception $exception ) { |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /** |
| 319 | * @return Types\Form|bool |
| 320 | */ |
| 321 | public function get_form_class() { |
| 322 | try { |
| 323 | return $this->default_repository()->rep_get_item( 'form-block' ); |
| 324 | } catch ( Repository_Exception $exception ) { |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | public function render_callback( $instance ) { |
| 330 | return static function ( array $attrs, $content = null, $wp_block = null ) use ( $instance ) { |
| 331 | return call_user_func( array( clone $instance, 'render_callback_field' ), $attrs, $content, $wp_block ); |
| 332 | }; |
| 333 | } |
| 334 | |
| 335 | public function builder_repository(): Form_Builder_Blocks_Repository { |
| 336 | if ( ! $this->_builder_blocks_repository ) { |
| 337 | $this->_builder_blocks_repository = new Form_Builder_Blocks_Repository(); |
| 338 | } |
| 339 | |
| 340 | return $this->_builder_blocks_repository; |
| 341 | } |
| 342 | |
| 343 | public function default_repository(): Default_Blocks_Repository { |
| 344 | if ( ! $this->_default_blocks_repository ) { |
| 345 | $this->_default_blocks_repository = new Default_Blocks_Repository(); |
| 346 | } |
| 347 | |
| 348 | return $this->_default_blocks_repository; |
| 349 | } |
| 350 | |
| 351 | public function init_jet_sm_block_manager() { |
| 352 | if ( ! Compatibility::has_jet_sm() ) { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | Block_Manager::get_instance(); |
| 357 | } |
| 358 | } |
| 359 |