module.php
145 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_Dir_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 | use JFB_Modules\Ai\Rest_Api\Endpoints\Generate_Form_Endpoint; |
| 20 | use JFB_Modules\Post_Type; |
| 21 | |
| 22 | class Module implements |
| 23 | Base_Module_It, |
| 24 | Base_Module_Url_It, |
| 25 | Base_Module_Handle_It, |
| 26 | Base_Module_After_Install_It, |
| 27 | Base_Module_Dir_It { |
| 28 | |
| 29 | use Base_Module_Url_Trait; |
| 30 | use Base_Module_Handle_Trait; |
| 31 | use Base_Module_Dir_Trait; |
| 32 | |
| 33 | public function rep_item_id() { |
| 34 | return 'ai'; |
| 35 | } |
| 36 | |
| 37 | public function condition(): bool { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | public function init_hooks() { |
| 42 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); |
| 43 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'editor_enqueue_assets' ) ); |
| 44 | } |
| 45 | |
| 46 | public function remove_hooks() { |
| 47 | remove_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); |
| 48 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'editor_enqueue_assets' ) ); |
| 49 | } |
| 50 | |
| 51 | public function on_install() { |
| 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()->install( new Generate_Form_Endpoint() ); |
| 57 | } |
| 58 | |
| 59 | public function on_uninstall() { |
| 60 | /** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 61 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 62 | $rest_api = jet_form_builder()->module( 'rest-api' ); |
| 63 | |
| 64 | $rest_api->get_controller()->uninstall( new Generate_Form_Endpoint() ); |
| 65 | } |
| 66 | |
| 67 | public function admin_enqueue_assets() { |
| 68 | global $post_type; |
| 69 | |
| 70 | if ( Post_Type\Module::SLUG !== $post_type ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | /** @var \JFB_Modules\Html_Parser\Module $parser_module */ |
| 75 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 76 | $parser_module = jet_form_builder()->module( 'html-parser' ); |
| 77 | |
| 78 | $parser_module->register_scripts(); |
| 79 | |
| 80 | $script_asset = require_once $this->get_dir( 'assets/build/admin/forms.asset.php' ); |
| 81 | |
| 82 | if ( true === $script_asset ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | array_push( |
| 87 | $script_asset['dependencies'], |
| 88 | $parser_module->get_handle() |
| 89 | ); |
| 90 | |
| 91 | wp_enqueue_script( |
| 92 | $this->get_handle(), |
| 93 | $this->get_url( 'assets/build/admin/forms.js' ), |
| 94 | $script_asset['dependencies'], |
| 95 | $script_asset['version'], |
| 96 | true |
| 97 | ); |
| 98 | |
| 99 | wp_localize_script( |
| 100 | $this->get_handle(), |
| 101 | 'JetFormBuilderAdmin', |
| 102 | array( |
| 103 | 'edit_url' => esc_url( |
| 104 | add_query_arg( |
| 105 | array( 'action' => 'edit' ), |
| 106 | admin_url( 'post.php' ) |
| 107 | ) |
| 108 | ), |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | /** |
| 113 | * Required to show styled AI form modal on the forms list page |
| 114 | */ |
| 115 | wp_enqueue_style( 'wp-components' ); |
| 116 | } |
| 117 | |
| 118 | public function editor_enqueue_assets() { |
| 119 | $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' ); |
| 120 | |
| 121 | if ( true === $script_asset ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /** @var \JFB_Modules\Html_Parser\Module $parser_module */ |
| 126 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 127 | $parser_module = jet_form_builder()->module( 'html-parser' ); |
| 128 | $parser_module->register_scripts(); |
| 129 | |
| 130 | array_push( |
| 131 | $script_asset['dependencies'], |
| 132 | $parser_module->get_handle(), |
| 133 | 'jet-fb-components' |
| 134 | ); |
| 135 | |
| 136 | wp_enqueue_script( |
| 137 | $this->get_handle(), |
| 138 | $this->get_url( 'assets/build/editor.js' ), |
| 139 | $script_asset['dependencies'], |
| 140 | $script_asset['version'], |
| 141 | true |
| 142 | ); |
| 143 | } |
| 144 | } |
| 145 |