module.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Ai; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 12 | use JFB_Components\Module\Base_Module_Handle_It; |
| 13 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 14 | use JFB_Components\Module\Base_Module_It; |
| 15 | use JFB_Components\Module\Base_Module_Url_It; |
| 16 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 17 | use JFB_Modules\Ai\Rest_Api\Endpoints\Generate_Form_Endpoint; |
| 18 | use JFB_Modules\Post_Type; |
| 19 | |
| 20 | class Module implements Base_Module_It, Base_Module_Url_It, Base_Module_Handle_It, Base_Module_After_Install_It { |
| 21 | |
| 22 | use Base_Module_Url_Trait; |
| 23 | use Base_Module_Handle_Trait; |
| 24 | |
| 25 | public function rep_item_id() { |
| 26 | return 'ai'; |
| 27 | } |
| 28 | |
| 29 | public function condition(): bool { |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | public function init_hooks() { |
| 34 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); |
| 35 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'editor_enqueue_assets' ) ); |
| 36 | } |
| 37 | |
| 38 | public function remove_hooks() { |
| 39 | remove_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); |
| 40 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'editor_enqueue_assets' ) ); |
| 41 | } |
| 42 | |
| 43 | public function on_install() { |
| 44 | /** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 45 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 46 | $rest_api = jet_form_builder()->module( 'rest-api' ); |
| 47 | |
| 48 | $rest_api->get_controller()->install( new Generate_Form_Endpoint() ); |
| 49 | } |
| 50 | |
| 51 | public function on_uninstall() { |
| 52 | /** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 53 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 54 | $rest_api = jet_form_builder()->module( 'rest-api' ); |
| 55 | |
| 56 | $rest_api->get_controller()->uninstall( new Generate_Form_Endpoint() ); |
| 57 | } |
| 58 | |
| 59 | public function admin_enqueue_assets() { |
| 60 | global $post_type; |
| 61 | |
| 62 | if ( Post_Type\Module::SLUG !== $post_type ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | /** @var \JFB_Modules\Html_Parser\Module $parser_module */ |
| 67 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 68 | $parser_module = jet_form_builder()->module( 'html-parser' ); |
| 69 | |
| 70 | $parser_module->register_scripts(); |
| 71 | |
| 72 | wp_enqueue_style( |
| 73 | $this->get_handle(), |
| 74 | $this->get_url( 'assets/build/modal.css' ), |
| 75 | array( |
| 76 | 'wp-components', |
| 77 | ), |
| 78 | jet_form_builder()->get_version() |
| 79 | ); |
| 80 | wp_enqueue_script( |
| 81 | $this->get_handle(), |
| 82 | $this->get_url( 'assets/build/admin/forms.js' ), |
| 83 | array( |
| 84 | $parser_module->get_handle(), |
| 85 | 'wp-api-fetch', |
| 86 | 'wp-dom-ready', |
| 87 | 'wp-components', |
| 88 | 'wp-i18n', |
| 89 | ), |
| 90 | jet_form_builder()->get_version(), |
| 91 | true |
| 92 | ); |
| 93 | |
| 94 | wp_localize_script( |
| 95 | $this->get_handle(), |
| 96 | 'JetFormBuilderAdmin', |
| 97 | array( |
| 98 | 'edit_url' => esc_url( |
| 99 | add_query_arg( |
| 100 | array( 'action' => 'edit' ), |
| 101 | admin_url( 'post.php' ) |
| 102 | ) |
| 103 | ), |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | public function editor_enqueue_assets() { |
| 109 | /** @var \JFB_Modules\Html_Parser\Module $parser_module */ |
| 110 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 111 | $parser_module = jet_form_builder()->module( 'html-parser' ); |
| 112 | |
| 113 | $parser_module->register_scripts(); |
| 114 | wp_enqueue_script( |
| 115 | $this->get_handle(), |
| 116 | $this->get_url( 'assets/build/editor.js' ), |
| 117 | array( |
| 118 | $parser_module->get_handle(), |
| 119 | ), |
| 120 | jet_form_builder()->get_version(), |
| 121 | true |
| 122 | ); |
| 123 | wp_enqueue_style( |
| 124 | $this->get_handle(), |
| 125 | $this->get_url( 'assets/build/modal.css' ), |
| 126 | array(), |
| 127 | jet_form_builder()->get_version() |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 |