| @@ -27,8 +27,9 @@ | ||
| 27 | 27 | Base_Module_Url_It, |
| 28 | 28 | Base_Module_Dir_It, |
| 29 | 29 | Base_Module_After_Install_It { |
| 30 | 30 | |
| 31 | + | |
| 31 | 32 | use Base_Compat_Handle_Trait; |
| 32 | 33 | use Base_Compat_Url_Trait; |
| 33 | 34 | use Base_Compat_Dir_Trait; |
| 34 | 35 | |
| @@ -50,10 +51,9 @@ | ||
| 50 | 51 | public function on_install() { |
| 51 | 52 | $this->onboarding_builder = new Onboarding_Builder(); |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | - public function on_uninstall() { | |
| 55 | - } | |
| 55 | + public function on_uninstall() {} | |
| 56 | 56 | |
| 57 | 57 | public function init_hooks() { |
| 58 | 58 | add_action( 'elementor/init', array( $this, 'init_widgets' ) ); |
| 59 | 59 | add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'editor_styles' ) ); |
| @@ -133,9 +133,8 @@ | ||
| 133 | 133 | jet_form_builder()->get_version() |
| 134 | 134 | ); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - | |
| 138 | 137 | public function register_widgets( $manager ) { |
| 139 | 138 | foreach ( $this->types as $widget ) { |
| 140 | 139 | // compatibility with 3.7 |
| 141 | 140 | if ( method_exists( $manager, 'register' ) ) { |
| @@ -151,13 +150,23 @@ | ||
| 151 | 150 | */ |
| 152 | 151 | public function enqueue_form_scripts() { |
| 153 | 152 | /** @var Blocks\Module $blocks */ |
| 154 | 153 | $blocks = jet_form_builder()->module( 'blocks' ); |
| 154 | + | |
| 155 | 155 | /** @var Deprecated\Module $deprecated */ |
| 156 | 156 | $deprecated = jet_form_builder()->module( 'deprecated' ); |
| 157 | 157 | |
| 158 | 158 | $blocks->enqueue_frontend_assets(); |
| 159 | 159 | |
| 160 | + // Elementor renders the selected form dynamically inside the preview. | |
| 161 | + // At this point, block view scripts enqueued while rendering the form | |
| 162 | + // are not automatically added to the already loaded preview document. | |
| 163 | + $this->enqueue_form_block_scripts(); | |
| 164 | + | |
| 165 | + // The WYSIWYG block requires WordPress editor scripts and settings. | |
| 166 | + // They must be loaded before the form is dynamically rendered. | |
| 167 | + wp_enqueue_editor(); | |
| 168 | + | |
| 160 | 169 | // appointment/booking compatibility |
| 161 | 170 | $deprecated->register_scripts(); |
| 162 | 171 | $deprecated->add_deprecated_script( '' ); |
| 163 | 172 | } |
| @@ -163,8 +172,72 @@ | ||
| 163 | 172 | } |
| 164 | 173 | |
| 165 | 174 | public function enqueue_form_styles() { |
| 166 | 175 | wp_enqueue_style( 'jet-form-builder-frontend' ); |
| 176 | + | |
| 177 | + // Load frontend styles of all JetFormBuilder blocks in the | |
| 178 | + // Elementor preview before a form is selected dynamically. | |
| 179 | + $this->enqueue_form_block_styles(); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Enqueue frontend scripts of all registered JetFormBuilder blocks. | |
| 184 | + * | |
| 185 | + * Elementor can render or replace a form dynamically after the preview | |
| 186 | + * document has already loaded. Therefore, block scripts must be available | |
| 187 | + * before the form initialization is triggered. | |
| 188 | + * | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + private function enqueue_form_block_scripts() { | |
| 192 | + $registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered(); | |
| 193 | + | |
| 194 | + foreach ( $registered_blocks as $block_type ) { | |
| 195 | + if ( 0 !== strpos( $block_type->name, 'jet-forms/' ) ) { | |
| 196 | + continue; | |
| 197 | + } | |
| 198 | + | |
| 199 | + $script_handles = array_merge( | |
| 200 | + $block_type->script_handles, | |
| 201 | + $block_type->view_script_handles | |
| 202 | + ); | |
| 203 | + | |
| 204 | + foreach ( array_unique( $script_handles ) as $handle ) { | |
| 205 | + if ( ! $handle ) { | |
| 206 | + continue; | |
| 207 | + } | |
| 208 | + | |
| 209 | + wp_enqueue_script( $handle ); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Enqueue frontend styles of all registered JetFormBuilder blocks. | |
| 216 | + * | |
| 217 | + * @return void | |
| 218 | + */ | |
| 219 | + private function enqueue_form_block_styles() { | |
| 220 | + $registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered(); | |
| 221 | + | |
| 222 | + foreach ( $registered_blocks as $block_type ) { | |
| 223 | + if ( 0 !== strpos( $block_type->name, 'jet-forms/' ) ) { | |
| 224 | + continue; | |
| 225 | + } | |
| 226 | + | |
| 227 | + $style_handles = array_merge( | |
| 228 | + $block_type->style_handles, | |
| 229 | + $block_type->view_style_handles | |
| 230 | + ); | |
| 231 | + | |
| 232 | + foreach ( array_unique( $style_handles ) as $handle ) { | |
| 233 | + if ( ! $handle ) { | |
| 234 | + continue; | |
| 235 | + } | |
| 236 | + | |
| 237 | + wp_enqueue_style( $handle ); | |
| 238 | + } | |
| 239 | + } | |
| 167 | 240 | } |
| 168 | 241 | |
| 169 | 242 | /** |
| 170 | 243 | * @return Onboarding_Builder |