module.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Switcher; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Blocks\Module as BlocksModule; |
| 12 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 13 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 14 | use JFB_Components\Module\Base_Module_Handle_It; |
| 15 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 16 | use JFB_Components\Module\Base_Module_It; |
| 17 | use JFB_Components\Module\Base_Module_Url_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 19 | |
| 20 | class Module implements |
| 21 | Base_Module_After_Install_It, |
| 22 | Base_Module_It, |
| 23 | Base_Module_Url_It, |
| 24 | Base_Module_Handle_It { |
| 25 | |
| 26 | use Base_Module_Handle_Trait; |
| 27 | use Base_Module_Dir_Trait; |
| 28 | use Base_Module_Url_Trait; |
| 29 | |
| 30 | public function rep_item_id() { |
| 31 | return 'switcher'; |
| 32 | } |
| 33 | |
| 34 | public function condition(): bool { |
| 35 | return jet_form_builder()->has_module( 'jet-style' ); |
| 36 | } |
| 37 | |
| 38 | public function on_install() { |
| 39 | } |
| 40 | |
| 41 | public function on_uninstall() { |
| 42 | } |
| 43 | |
| 44 | public function init_hooks() { |
| 45 | add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 46 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 47 | add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 48 | add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 49 | add_action( 'enqueue_block_editor_assets', array( $this, 'preview_styles' ) ); |
| 50 | } |
| 51 | |
| 52 | public function remove_hooks() { |
| 53 | remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 54 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 55 | remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 56 | remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 57 | } |
| 58 | |
| 59 | public function add_blocks_types( array $types ): array { |
| 60 | $types[] = new Blocks\Switcher\Block_Type(); |
| 61 | |
| 62 | return $types; |
| 63 | } |
| 64 | |
| 65 | public function preview_styles() { |
| 66 | |
| 67 | $script_asset = require $this->get_dir( 'assets/build/editor.asset.php' ); |
| 68 | |
| 69 | wp_enqueue_style( |
| 70 | $this->get_handle(), |
| 71 | $this->get_url( 'assets/build/switcher.css' ), |
| 72 | array(), |
| 73 | $script_asset['version'] |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | public function enqueue_admin_assets() { |
| 78 | |
| 79 | $script_asset = require $this->get_dir( 'assets/build/editor.asset.php' ); |
| 80 | |
| 81 | wp_enqueue_script( |
| 82 | $this->get_handle(), |
| 83 | $this->get_url( 'assets/build/editor.js' ), |
| 84 | $script_asset['dependencies'], |
| 85 | $script_asset['version'], |
| 86 | true |
| 87 | ); |
| 88 | |
| 89 | wp_enqueue_style( |
| 90 | $this->get_handle(), |
| 91 | $this->get_url( 'assets/build/switcher.css' ), |
| 92 | array(), |
| 93 | $script_asset['version'] |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | public function register_frontend_scripts() { |
| 98 | $script_asset = require_once $this->get_dir( 'assets/build/switcher.asset.php' ); |
| 99 | |
| 100 | if ( true === $script_asset ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | array_push( |
| 105 | $script_asset['dependencies'], |
| 106 | BlocksModule::MAIN_SCRIPT_HANDLE |
| 107 | ); |
| 108 | |
| 109 | wp_register_script( |
| 110 | $this->get_handle(), |
| 111 | $this->get_url( 'assets/build/switcher.js' ), |
| 112 | $script_asset['dependencies'], |
| 113 | $script_asset['version'], |
| 114 | true |
| 115 | ); |
| 116 | wp_register_style( |
| 117 | $this->get_handle(), |
| 118 | $this->get_url( 'assets/build/switcher.css' ), |
| 119 | array(), |
| 120 | $script_asset['version'] |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 |