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