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