onboarding-builder.php
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Compatibility\Elementor; |
| 4 | |
| 5 | use Elementor\Core\DocumentTypes\Page; |
| 6 | use Elementor\Plugin; |
| 7 | use Elementor\Utils; |
| 8 | use JFB_Modules\Onboarding\Builders\Exceptions\Use_Form_Exception; |
| 9 | use JFB_Modules\Onboarding\Builders\Interfaces\Builder_Create_Page_Interface; |
| 10 | use JFB_Modules\Onboarding\Builders\Interfaces\Builder_Interface; |
| 11 | use JFB_Modules\Onboarding\Builders\Interfaces\Builder_Update_Page_Interface; |
| 12 | use JFB_Modules\Onboarding\Module; |
| 13 | |
| 14 | class Onboarding_Builder { |
| 15 | |
| 16 | public function init_hooks() { |
| 17 | add_action( 'jet-form-builder/use-form', array( $this, 'handle_use' ) ); |
| 18 | add_action( |
| 19 | 'jet-form-builder/use-form/register-assets', |
| 20 | array( $this, 'block_editor_assets' ) |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param Builder_Create_Page_Interface|Builder_Update_Page_Interface $builder |
| 26 | * |
| 27 | * @return void |
| 28 | * @throws Use_Form_Exception |
| 29 | */ |
| 30 | public function handle_use( Builder_Interface $builder ) { |
| 31 | if ( 'elementor' !== $builder->get_builder_type() ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | if ( $builder instanceof Builder_Update_Page_Interface ) { |
| 36 | $this->update( $builder ); |
| 37 | |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $this->create( $builder ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return void |
| 46 | * @throws Use_Form_Exception |
| 47 | */ |
| 48 | public function create( Builder_Create_Page_Interface $create_page ) { |
| 49 | $document = Plugin::$instance->documents->create( |
| 50 | 'page', |
| 51 | array( |
| 52 | 'post_title' => $create_page->get_title(), |
| 53 | 'post_status' => 'draft', |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | if ( is_wp_error( $document ) ) { |
| 58 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 59 | throw new Use_Form_Exception( $document->get_error_message() ); |
| 60 | } |
| 61 | |
| 62 | $new_elements = $this->get_elements_with_wrapper( |
| 63 | array( |
| 64 | array( |
| 65 | 'id' => Utils::generate_random_string(), |
| 66 | 'elType' => 'widget', |
| 67 | 'widgetType' => 'jet-form-builder-form', |
| 68 | 'settings' => array( |
| 69 | 'form_id' => (string) $create_page->get_form_id(), |
| 70 | ), |
| 71 | ), |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | $document->save( |
| 76 | array( |
| 77 | 'elements' => $new_elements, |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | $create_page->set_redirect_url( $document->get_edit_url() ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param Builder_Update_Page_Interface $update_page |
| 86 | * |
| 87 | * @return void |
| 88 | * @throws Use_Form_Exception |
| 89 | */ |
| 90 | public function update( Builder_Update_Page_Interface $update_page ) { |
| 91 | $document = Plugin::$instance->documents->get( $update_page->get_page_id() ); |
| 92 | |
| 93 | if ( ! $document ) { |
| 94 | throw new Use_Form_Exception( 'Undefined page' ); |
| 95 | } |
| 96 | |
| 97 | $elements = $document->get_elements_raw_data(); |
| 98 | $new_elements = $this->get_elements_with_wrapper( |
| 99 | array( |
| 100 | array( |
| 101 | 'id' => Utils::generate_random_string(), |
| 102 | 'elType' => 'widget', |
| 103 | 'widgetType' => 'jet-form-builder-form', |
| 104 | 'settings' => array( |
| 105 | 'form_id' => (string) $update_page->get_form_id(), |
| 106 | ), |
| 107 | ), |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | $document->save( |
| 112 | array( |
| 113 | 'elements' => array_merge( $elements, $new_elements ), |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | $update_page->set_redirect_url( $document->get_edit_url() ); |
| 118 | } |
| 119 | |
| 120 | public function block_editor_assets() { |
| 121 | /** @var Module $onboarding */ |
| 122 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 123 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 124 | /** @var Elementor $elementor */ |
| 125 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 126 | $elementor = jet_form_builder()->compat( 'elementor' ); |
| 127 | |
| 128 | $script_asset = require_once $elementor->get_dir( 'assets/build/block.editor.asset.php' ); |
| 129 | |
| 130 | array_push( |
| 131 | $script_asset['dependencies'], |
| 132 | $onboarding->get_use_form()->get_handle() |
| 133 | ); |
| 134 | |
| 135 | wp_enqueue_script( |
| 136 | $elementor->get_handle( 'block-editor' ), |
| 137 | $elementor->get_url( 'assets/build/block.editor.js' ), |
| 138 | $script_asset['dependencies'], |
| 139 | $script_asset['version'], |
| 140 | true |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | private function get_elements_with_wrapper( array $elements ): array { |
| 145 | if ( empty( Plugin::$instance->experiments ) ) { |
| 146 | return $this->get_sections_elements_data( $elements ); |
| 147 | } |
| 148 | |
| 149 | return Plugin::$instance->experiments->is_feature_active( 'container' ) |
| 150 | ? $this->get_container_elements_data( $elements ) |
| 151 | : $this->get_sections_elements_data( $elements ); |
| 152 | } |
| 153 | |
| 154 | private function get_container_elements_data( array $internal_elements ): array { |
| 155 | return array( |
| 156 | array( |
| 157 | 'id' => Utils::generate_random_string(), |
| 158 | 'elType' => 'container', |
| 159 | 'elements' => $internal_elements, |
| 160 | ), |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param array $internal_elements |
| 166 | * |
| 167 | * @return array[] |
| 168 | */ |
| 169 | private function get_sections_elements_data( array $internal_elements ): array { |
| 170 | return array( |
| 171 | array( |
| 172 | 'id' => Utils::generate_random_string(), |
| 173 | 'elType' => 'section', |
| 174 | 'elements' => array( |
| 175 | array( |
| 176 | 'id' => Utils::generate_random_string(), |
| 177 | 'elType' => 'column', |
| 178 | 'elements' => $internal_elements, |
| 179 | ), |
| 180 | ), |
| 181 | ), |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | } |
| 186 |