module.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Macros_Inserter; |
| 4 | |
| 5 | use JFB_Components\Module\Base_Module_Dir_It; |
| 6 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 7 | use JFB_Components\Module\Base_Module_Handle_It; |
| 8 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 9 | use JFB_Components\Module\Base_Module_It; |
| 10 | use JFB_Components\Module\Base_Module_Url_It; |
| 11 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 12 | |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | class Module implements |
| 18 | Base_Module_It, |
| 19 | Base_Module_Url_It, |
| 20 | Base_Module_Handle_It, |
| 21 | Base_Module_Dir_It { |
| 22 | |
| 23 | |
| 24 | |
| 25 | use Base_Module_Handle_Trait; |
| 26 | use Base_Module_Url_Trait; |
| 27 | use Base_Module_Dir_Trait; |
| 28 | |
| 29 | public function rep_item_id() { |
| 30 | return 'macros-inserter'; |
| 31 | } |
| 32 | |
| 33 | public function condition(): bool { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | public function init_hooks() { |
| 38 | add_action( 'init', array( $this, 'register_editor_assets' ), 0 ); |
| 39 | add_action( 'init', array( $this, 'register_block_type' ) ); |
| 40 | |
| 41 | add_action( |
| 42 | 'jet-form-builder/editor-assets/before', |
| 43 | array( $this, 'enqueue_code_editor_assets' ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function remove_hooks() { |
| 48 | remove_action( 'init', array( $this, 'register_editor_assets' ), 0 ); |
| 49 | remove_action( 'init', array( $this, 'register_block_type' ) ); |
| 50 | |
| 51 | remove_action( |
| 52 | 'jet-form-builder/editor-assets/before', |
| 53 | array( $this, 'enqueue_code_editor_assets' ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function register_block_type() { |
| 58 | if ( ! function_exists( 'register_block_type_from_metadata' ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | register_block_type_from_metadata( |
| 63 | $this->get_dir( 'assets/src/editor/blocks/macros-inserter' ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function register_editor_assets() { |
| 68 | $asset_path = $this->get_dir( 'assets/build/editor.asset.php' ); |
| 69 | |
| 70 | $asset = file_exists( $asset_path ) |
| 71 | ? require $asset_path |
| 72 | : array( |
| 73 | 'dependencies' => array(), |
| 74 | 'version' => jet_form_builder()->get_version(), |
| 75 | ); |
| 76 | |
| 77 | $deps = array_unique( |
| 78 | array_merge( |
| 79 | $asset['dependencies'] ?? array(), |
| 80 | array( 'code-editor' ) |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | wp_register_script( |
| 85 | $this->get_handle( 'editor' ), |
| 86 | $this->get_url( 'assets/build/editor.js' ), |
| 87 | $deps, |
| 88 | $asset['version'] ?? jet_form_builder()->get_version(), |
| 89 | true |
| 90 | ); |
| 91 | |
| 92 | wp_register_style( |
| 93 | $this->get_handle( 'editor' ), |
| 94 | $this->get_url( 'assets/build/editor.css' ), |
| 95 | array( 'wp-codemirror' ), |
| 96 | $asset['version'] ?? jet_form_builder()->get_version() |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | public function enqueue_code_editor_assets() { |
| 101 | // Required for wp.codeEditor.initialize(...). |
| 102 | wp_enqueue_code_editor( |
| 103 | array( |
| 104 | 'type' => 'text/html', |
| 105 | ) |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 |