use-form.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Onboarding\Use_Form; |
| 5 | |
| 6 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 7 | use JFB_Components\Module\Base_Module_Dir_It; |
| 8 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 9 | use JFB_Components\Module\Base_Module_Handle_It; |
| 10 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 11 | use JFB_Components\Module\Base_Module_It; |
| 12 | use JFB_Components\Module\Base_Module_Url_It; |
| 13 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 14 | use JFB_Modules\Onboarding\Builders\Block_Editor_Builder; |
| 15 | use JFB_Modules\Onboarding\Builders\No_Builder_Handler; |
| 16 | use JFB_Modules\Onboarding\Module; |
| 17 | use JFB_Modules\Onboarding\Use_Form\Rest_Api\Use_Form_Route\Use_Form_Route; |
| 18 | |
| 19 | // If this file is called directly, abort. |
| 20 | if ( ! defined( 'WPINC' ) ) { |
| 21 | die; |
| 22 | } |
| 23 | |
| 24 | class Use_Form implements |
| 25 | Base_Module_Url_It, |
| 26 | Base_Module_Dir_It, |
| 27 | Base_Module_Handle_It { |
| 28 | |
| 29 | /** |
| 30 | * @var Block_Editor_Builder |
| 31 | */ |
| 32 | private $block_builder; |
| 33 | /** |
| 34 | * @var No_Builder_Handler |
| 35 | */ |
| 36 | private $no_builder; |
| 37 | |
| 38 | public function __construct() { |
| 39 | $this->block_builder = new Block_Editor_Builder(); |
| 40 | $this->no_builder = new No_Builder_Handler(); |
| 41 | } |
| 42 | |
| 43 | public function init_hooks() { |
| 44 | add_action( |
| 45 | 'jet-form-builder/use-form/register-assets', |
| 46 | array( $this, 'register_assets' ), |
| 47 | 0 |
| 48 | ); |
| 49 | |
| 50 | add_action( |
| 51 | 'rest_api_init', |
| 52 | array( $this, 'rest_api_init' ) |
| 53 | ); |
| 54 | |
| 55 | $this->get_block_builder()->init_hooks(); |
| 56 | $this->get_no_builder()->init_hooks(); |
| 57 | } |
| 58 | |
| 59 | public function rest_api_init() { |
| 60 | $route = new Use_Form_Route(); |
| 61 | $route->register(); |
| 62 | } |
| 63 | |
| 64 | public function register_assets() { |
| 65 | $script_asset = require_once $this->get_dir( 'assets/build/index.asset.php' ); |
| 66 | |
| 67 | if ( true === $script_asset ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | array_push( |
| 72 | $script_asset['dependencies'], |
| 73 | 'jet-fb-components' |
| 74 | ); |
| 75 | |
| 76 | wp_register_script( |
| 77 | $this->get_handle(), |
| 78 | $this->get_url( 'assets/build/index.js' ), |
| 79 | $script_asset['dependencies'], |
| 80 | $script_asset['version'], |
| 81 | true |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public function get_block_builder(): Block_Editor_Builder { |
| 86 | return $this->block_builder; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return No_Builder_Handler |
| 91 | */ |
| 92 | public function get_no_builder(): No_Builder_Handler { |
| 93 | return $this->no_builder; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | public function get_dir( string $path ): string { |
| 98 | /** @var Module $onboarding */ |
| 99 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 100 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 101 | |
| 102 | return $onboarding->get_dir( 'use-form/' . $path ); |
| 103 | } |
| 104 | |
| 105 | public function get_handle( string $unique_string = '' ): string { |
| 106 | /** @var Module $onboarding */ |
| 107 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 108 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 109 | |
| 110 | return $onboarding->get_handle( 'use-form' . $unique_string ); |
| 111 | } |
| 112 | |
| 113 | public function get_url( string $url ): string { |
| 114 | /** @var Module $onboarding */ |
| 115 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 116 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 117 | |
| 118 | return $onboarding->get_url( 'use-form/' . $url ); |
| 119 | } |
| 120 | } |
| 121 |