elementor-controller.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Widgets; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Plugin; |
| 8 | use Jet_Form_Builder\Widgets\Types; |
| 9 | |
| 10 | class Elementor_Controller { |
| 11 | |
| 12 | private $_types = array(); |
| 13 | |
| 14 | public function __construct() { |
| 15 | add_action( 'elementor/init', array( $this, 'setup_widgets' ) ); |
| 16 | add_action( 'elementor/init', array( $this, 'register_category' ) ); |
| 17 | add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'editor_styles' ) ); |
| 18 | add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_widgets' ), 11 ); |
| 19 | add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_form_assets' ) ); |
| 20 | } |
| 21 | |
| 22 | public function enqueue_form_assets() { |
| 23 | Plugin::instance()->blocks->enqueue_frontend_assets(); |
| 24 | } |
| 25 | |
| 26 | private function widgets() { |
| 27 | return array( |
| 28 | new Types\Form() |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Register category for elementor if not exists |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function register_category() { |
| 38 | |
| 39 | $elements_manager = \Elementor\Plugin::instance()->elements_manager; |
| 40 | |
| 41 | $elements_manager->add_category( |
| 42 | 'jet-form-builder', |
| 43 | array( |
| 44 | 'title' => esc_html__( 'JetFormBuilder', 'jet-form-builder' ), |
| 45 | 'icon' => 'font', |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Enqueue editor styles |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public function editor_styles() { |
| 56 | |
| 57 | wp_enqueue_style( |
| 58 | 'jet-form-builder-icons', |
| 59 | jet_form_builder()->plugin_url( 'assets/css/icons.css' ), |
| 60 | array(), |
| 61 | jet_form_builder()->get_version() |
| 62 | ); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | public function setup_widgets() { |
| 67 | foreach ( $this->widgets() as $widget ) { |
| 68 | $this->setup_widget( $widget ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private function setup_widget( $widget ) { |
| 73 | $widget->init_hooks(); |
| 74 | $this->_types[ $widget->get_name() ] = $widget; |
| 75 | } |
| 76 | |
| 77 | public function register_widgets( $manager ) { |
| 78 | foreach ( $this->_types as $widget ) { |
| 79 | $manager->register_widget_type( $widget ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |