api
3 weeks ago
assets
3 weeks ago
controls
3 weeks ago
fonts
5 years ago
views
4 years ago
cpt.php
4 years ago
init.php
2 years ago
live-action.php
4 months ago
widget-file.php
4 years ago
init.php
137 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Modules\Widget_Builder; |
| 3 | |
| 4 | use ElementsKit_Lite\Modules\Widget_Builder\Controls\Widget_Writer; |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | class Init { |
| 9 | private $dir; |
| 10 | private $url; |
| 11 | |
| 12 | public function __construct() { |
| 13 | |
| 14 | // get current directory path |
| 15 | $this->dir = dirname( __FILE__ ) . '/'; |
| 16 | |
| 17 | // get current module's url |
| 18 | $this->url = \ElementsKit_Lite::plugin_url() . 'modules/widget-builder/'; |
| 19 | |
| 20 | // include all necessary files |
| 21 | $this->include_files(); |
| 22 | |
| 23 | //hooks |
| 24 | add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 25 | add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); |
| 26 | // add_action('elementor/init', [$this, 'elementor_widget_category']); |
| 27 | add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) ); |
| 28 | add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'editor_css' ) ); |
| 29 | add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'frontend_css' ) ); |
| 30 | |
| 31 | add_action( 'admin_init', array( $this, 'on_empty_trash_delete_files' ) ); |
| 32 | //add_action('wp_trash_post', [$this, 'on_trash_delete_files']); |
| 33 | |
| 34 | // calling necessary classess |
| 35 | new Api\Common(); |
| 36 | new Cpt(); |
| 37 | new Live_Action(); |
| 38 | } |
| 39 | |
| 40 | public function include_files() { |
| 41 | // include $this->dir . 'extend-controls.php'; |
| 42 | } |
| 43 | |
| 44 | public function register_widgets( $widgets_manager ) { |
| 45 | $widgets = get_posts( |
| 46 | array( |
| 47 | 'post_type' => 'elementskit_widget', |
| 48 | 'post_status' => 'publish', |
| 49 | 'numberposts' => -1, |
| 50 | ) |
| 51 | ); |
| 52 | |
| 53 | $upload = wp_upload_dir(); |
| 54 | foreach ( $widgets as $widget ) { |
| 55 | $slug = 'ekit_wb_' . $widget->ID; |
| 56 | $dir = $upload['basedir'] . '/elementskit/custom_widgets/' . $slug . '/'; |
| 57 | $file = $dir . 'widget.php'; |
| 58 | $class_name = '\Elementor\Ekit_Wb_' . $widget->ID; |
| 59 | |
| 60 | if ( file_exists( $file ) ) { |
| 61 | include $file; |
| 62 | $widgets_manager->register( new $class_name() ); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function elementor_widget_category( $widgets_manager ) { |
| 68 | $widgets_manager->register( |
| 69 | 'elementskit-lite', |
| 70 | array( |
| 71 | 'title' => esc_html__( 'ElementsKit Custom', 'elementskit-lite' ), |
| 72 | 'icon' => 'fa fa-plug', |
| 73 | ), |
| 74 | 1 |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public function frontend_css() { |
| 79 | if ( ! is_singular( 'elementskit_widget' ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | wp_enqueue_style( 'elementskit-widget-builder-common-css', $this->url . 'assets/css/ekit-widget-builder-common.css', array(), \ElementsKit_Lite::version() ); |
| 84 | } |
| 85 | |
| 86 | public function editor_css() { |
| 87 | $screen = get_current_screen(); |
| 88 | |
| 89 | if ( $screen->id != 'elementskit_widget' ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | wp_enqueue_style( 'elementskit-widget-builder-common-css', $this->url . 'assets/css/ekit-widget-builder-common.css', array(), \ElementsKit_Lite::version() ); |
| 94 | } |
| 95 | |
| 96 | public function load_scripts() { |
| 97 | $screen = get_current_screen(); |
| 98 | |
| 99 | if ( $screen->id != 'elementskit_widget' ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | wp_enqueue_style( 'google-fonts-roboto', 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700', array(), null ); // PHPCS:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 104 | wp_enqueue_style( 'font-awesome', ELEMENTOR_ASSETS_URL . 'lib/font-awesome/css/all.min.css', array(), null ); // PHPCS:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 105 | wp_enqueue_style( 'elementskit-widget-builder-editor-css', $this->url . 'assets/css/ekit-widget-builder-editor.css', array(), \ElementsKit_Lite::version() ); |
| 106 | wp_enqueue_style( 'elementskit-widget-builder-common-css', $this->url . 'assets/css/ekit-widget-builder-common.css', array(), \ElementsKit_Lite::version() ); |
| 107 | |
| 108 | wp_enqueue_script( 'elementskit-widget-builder-editor-js', $this->url . 'assets/js/ekit-widget-builder-editor.js', array(), \ElementsKit_Lite::version(), true ); |
| 109 | } |
| 110 | |
| 111 | public function register_meta_boxes() { |
| 112 | add_meta_box( 'elementskit-widget-builder-markup', __( 'Builder', 'elementskit-lite' ), array( $this, 'metabox_display_callback' ), 'elementskit_widget' ); |
| 113 | } |
| 114 | |
| 115 | public function metabox_display_callback( $post ) { |
| 116 | include $this->dir . 'views/builder.php'; |
| 117 | } |
| 118 | |
| 119 | public function on_empty_trash_delete_files() { |
| 120 | |
| 121 | if ( !empty( $_GET['post'] ) && !empty($_GET['_wpnonce']) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'delete-post_' . intval( $_GET['post'] ) ) ) { |
| 122 | |
| 123 | $post_id = intval( $_GET['post'] ); |
| 124 | $post_type = get_post_type( $post_id ); |
| 125 | |
| 126 | if ( $post_type == 'elementskit_widget' ) { |
| 127 | |
| 128 | Widget_Writer::delete_widget( $post_id ); |
| 129 | } |
| 130 | } elseif ( |
| 131 | isset( $_GET['post_type'] ) && $_GET['post_type'] == 'elementskit_widget' && |
| 132 | isset( $_GET['action'] ) && $_GET['action'] == -1 && |
| 133 | isset( $_GET['post_status'] ) && $_GET['post_status'] == 'trash' ) { |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |