elementor.php
249 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Compatibility\Elementor; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks; |
| 7 | use Jet_Form_Builder\Classes\Builder_Helper; |
| 8 | use JFB_Components\Compatibility\Base_Compat_Dir_Trait; |
| 9 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 10 | use JFB_Components\Module\Base_Module_Dir_It; |
| 11 | use JFB_Modules\Deprecated; |
| 12 | use JFB_Components\Compatibility\Base_Compat_Handle_Trait; |
| 13 | use JFB_Components\Compatibility\Base_Compat_Url_Trait; |
| 14 | use JFB_Components\Module\Base_Module_Handle_It; |
| 15 | use JFB_Components\Module\Base_Module_It; |
| 16 | use JFB_Compatibility\Elementor\Widgets; |
| 17 | use JFB_Components\Module\Base_Module_Url_It; |
| 18 | |
| 19 | // If this file is called directly, abort. |
| 20 | if ( ! defined( 'WPINC' ) ) { |
| 21 | die; |
| 22 | } |
| 23 | |
| 24 | class Elementor implements |
| 25 | Base_Module_It, |
| 26 | Base_Module_Handle_It, |
| 27 | Base_Module_Url_It, |
| 28 | Base_Module_Dir_It, |
| 29 | Base_Module_After_Install_It { |
| 30 | |
| 31 | |
| 32 | use Base_Compat_Handle_Trait; |
| 33 | use Base_Compat_Url_Trait; |
| 34 | use Base_Compat_Dir_Trait; |
| 35 | |
| 36 | private $types; |
| 37 | |
| 38 | /** |
| 39 | * @var Onboarding_Builder |
| 40 | */ |
| 41 | private $onboarding_builder; |
| 42 | |
| 43 | public function rep_item_id() { |
| 44 | return 'elementor'; |
| 45 | } |
| 46 | |
| 47 | public function condition(): bool { |
| 48 | return defined( 'ELEMENTOR_VERSION' ); |
| 49 | } |
| 50 | |
| 51 | public function on_install() { |
| 52 | $this->onboarding_builder = new Onboarding_Builder(); |
| 53 | } |
| 54 | |
| 55 | public function on_uninstall() {} |
| 56 | |
| 57 | public function init_hooks() { |
| 58 | add_action( 'elementor/init', array( $this, 'init_widgets' ) ); |
| 59 | add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'editor_styles' ) ); |
| 60 | add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_form_scripts' ), 9 ); |
| 61 | add_action( 'elementor/preview/enqueue_styles', array( $this, 'enqueue_form_styles' ) ); |
| 62 | add_action( 'elementor/elements/categories_registered', array( $this, 'register_category' ) ); |
| 63 | |
| 64 | $this->get_onboarding_builder()->init_hooks(); |
| 65 | |
| 66 | // compatibility with 3.7 |
| 67 | if ( |
| 68 | defined( 'ELEMENTOR_VERSION' ) && |
| 69 | version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) |
| 70 | ) { |
| 71 | add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) ); |
| 72 | } else { |
| 73 | add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_widgets' ) ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function remove_hooks() { |
| 78 | remove_action( 'elementor/init', array( $this, 'init_widgets' ) ); |
| 79 | remove_action( 'elementor/editor/after_enqueue_styles', array( $this, 'editor_styles' ) ); |
| 80 | remove_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_form_scripts' ), 9 ); |
| 81 | remove_action( 'elementor/preview/enqueue_styles', array( $this, 'enqueue_form_styles' ) ); |
| 82 | remove_action( 'elementor/elements/categories_registered', array( $this, 'register_category' ) ); |
| 83 | |
| 84 | // compatibility with 3.7 |
| 85 | if ( |
| 86 | defined( 'ELEMENTOR_VERSION' ) && |
| 87 | version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) |
| 88 | ) { |
| 89 | remove_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) ); |
| 90 | } else { |
| 91 | remove_action( 'elementor/widgets/widgets_registered', array( $this, 'register_widgets' ) ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function init_widgets() { |
| 96 | $this->types = array( |
| 97 | new Widgets\Form(), |
| 98 | ); |
| 99 | |
| 100 | foreach ( $this->types as $type ) { |
| 101 | $type->init_hooks(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Register category for elementor if not exists |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function register_category() { |
| 111 | |
| 112 | $elements_manager = \Elementor\Plugin::instance()->elements_manager; |
| 113 | |
| 114 | $elements_manager->add_category( |
| 115 | 'jet-form-builder', |
| 116 | array( |
| 117 | 'title' => esc_html__( 'JetFormBuilder', 'jet-form-builder' ), |
| 118 | 'icon' => 'font', |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Enqueue editor styles |
| 125 | * |
| 126 | * @return void |
| 127 | */ |
| 128 | public function editor_styles() { |
| 129 | wp_enqueue_style( |
| 130 | $this->get_handle( 'icons' ), |
| 131 | $this->get_url( 'assets/build/css/icons.css' ), |
| 132 | array(), |
| 133 | jet_form_builder()->get_version() |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | public function register_widgets( $manager ) { |
| 138 | foreach ( $this->types as $widget ) { |
| 139 | // compatibility with 3.7 |
| 140 | if ( method_exists( $manager, 'register' ) ) { |
| 141 | $manager->register( $widget ); |
| 142 | } else { |
| 143 | $manager->register_widget_type( $widget ); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @noinspection PhpUnhandledExceptionInspection |
| 150 | */ |
| 151 | public function enqueue_form_scripts() { |
| 152 | /** @var Blocks\Module $blocks */ |
| 153 | $blocks = jet_form_builder()->module( 'blocks' ); |
| 154 | |
| 155 | /** @var Deprecated\Module $deprecated */ |
| 156 | $deprecated = jet_form_builder()->module( 'deprecated' ); |
| 157 | |
| 158 | $blocks->enqueue_frontend_assets(); |
| 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 | |
| 169 | // appointment/booking compatibility |
| 170 | $deprecated->register_scripts(); |
| 171 | $deprecated->add_deprecated_script( '' ); |
| 172 | } |
| 173 | |
| 174 | public function enqueue_form_styles() { |
| 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 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @return Onboarding_Builder |
| 244 | */ |
| 245 | public function get_onboarding_builder(): Onboarding_Builder { |
| 246 | return $this->onboarding_builder; |
| 247 | } |
| 248 | } |
| 249 |