| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file specifically loads JavaScript and CSS dependencies. |
| 4 |
* |
| 5 |
* @link https://posimyth.com/ |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Wdesignkit |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wdkit\WdKit_enqueue; |
| 12 |
|
| 13 |
use wdkit\Wdkit_Wdesignkit; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'Wdkit_Enqueue' ) ) { |
| 20 |
|
| 21 |
/** |
| 22 |
* Here Enqueue all js and css script |
| 23 |
*/ |
| 24 |
class Wdkit_Enqueue { |
| 25 |
/** |
| 26 |
* Member Variable |
| 27 |
* |
| 28 |
* @var instance |
| 29 |
*/ |
| 30 |
private static $instance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initiator |
| 34 |
*/ |
| 35 |
public static function get_instance() { |
| 36 |
if ( ! isset( self::$instance ) ) { |
| 37 |
self::$instance = new self(); |
| 38 |
} |
| 39 |
return self::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize the class and set its properties. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
add_action( 'admin_menu', array( $this, 'wdkit_admin_menu' ) ); |
| 49 |
|
| 50 |
add_action( 'admin_enqueue_scripts', array( $this, 'wdkit_admin_scripts' ), 10, 1 ); |
| 51 |
add_action( 'enqueue_block_editor_assets', array( $this, 'wdkit_admin_scripts' ), 10, 1 ); |
| 52 |
add_action( 'enqueue_block_editor_assets', array( $this, 'wdkit_enqueue_styles' ), 10 ); |
| 53 |
|
| 54 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 55 |
add_action( 'elementor/preview/enqueue_styles', array( $this, 'wdkit_elementor_preview_style' ) ); |
| 56 |
add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'wdkit_elementor_scripts' ) ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* This function is used to retrieve all post types in WordPress and store them in an array. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
public function wdkit_get_post_type_list() { |
| 66 |
$args = array( |
| 67 |
'public' => true, |
| 68 |
'show_ui' => true, |
| 69 |
); |
| 70 |
|
| 71 |
$post_types = get_post_types( $args, 'objects' ); |
| 72 |
$options = array(); |
| 73 |
foreach ( $post_types as $post_type ) { |
| 74 |
$exclude = array( 'attachment' ); |
| 75 |
if ( true === in_array( $post_type->name, $exclude, true ) ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! empty( $post_type->name ) && ( 'post' === $post_type->name || 'page' === $post_type->name || 'elementor_library' === $post_type->name || 'nxt_builder' === $post_type->name ) ) { |
| 80 |
$options[ $post_type->name ] = $post_type->label; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $options; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get Editor Use. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
*/ |
| 92 |
protected function wdkit_use_editor() { |
| 93 |
global $current_screen; |
| 94 |
|
| 95 |
$editor = 'wdkit'; |
| 96 |
if ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ) { |
| 97 |
$editor = 'elementor'; |
| 98 |
} |
| 99 |
|
| 100 |
if ( $current_screen->is_block_editor() && isset( $_GET['action'] ) && 'elementor' !== $_GET['action'] ) { |
| 101 |
$editor = 'gutenberg'; |
| 102 |
} |
| 103 |
|
| 104 |
return $editor; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Load Admin Scripts. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public function wdkit_elementor_scripts() { |
| 113 |
$this->wdkit_admin_scripts( 'elementor' ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Elementor Preview Style Loaded |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
*/ |
| 121 |
public function wdkit_elementor_preview_style() { |
| 122 |
wp_enqueue_style( 'wdkit-elementor-editor-css', WDKIT_URL . 'assets/css/elementor/wdkit_enqueue_preview_styles.css', array(), WDKIT_VERSION ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Enqueue Scripts admin area. |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* |
| 130 |
* @param string $hook use for check page type. |
| 131 |
*/ |
| 132 |
public function wdkit_admin_scripts( $hook ) { |
| 133 |
if ( ! in_array( $hook, array( 'toplevel_page_wdesign-kit', 'elementor', 'post-new.php', 'post.php' ), true ) ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$this->wdkit_enqueue_scripts( $hook ); |
| 138 |
$this->wdkit_enqueue_styles(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Enqueue Styles admin area. |
| 143 |
* |
| 144 |
* @since 1.0.0 |
| 145 |
*/ |
| 146 |
public function wdkit_enqueue_styles() { |
| 147 |
wp_enqueue_style( 'wdkit-editor-css', WDKIT_URL . 'build/index.css', array(), WDKIT_VERSION ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register the JavaScript for the admin area. |
| 152 |
* |
| 153 |
* @param string $hook give builder name. |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
public function wdkit_enqueue_scripts( $hook ) { |
| 157 |
wp_enqueue_script( 'wdkit-editor-js', WDKIT_URL . 'build/index.js', array( 'wp-i18n', 'wp-element', 'wp-components' ), WDKIT_VERSION, true ); |
| 158 |
|
| 159 |
wp_localize_script( |
| 160 |
'wdkit-editor-js', |
| 161 |
'wdkitData', |
| 162 |
array( |
| 163 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 164 |
'WDKIT_PATH' => WDKIT_PATH, |
| 165 |
'WDKIT_URL' => WDKIT_URL, |
| 166 |
'WDKIT_ASSETS' => WDKIT_ASSETS, |
| 167 |
'wdkit_server_url' => WDKIT_SERVER_SITE_URL, |
| 168 |
'home_url' => esc_url( home_url( '/' ) ), |
| 169 |
'kit_nonce' => wp_create_nonce( 'wdkit_nonce' ), |
| 170 |
'post_id' => get_the_ID(), |
| 171 |
'post_type' => get_post_type(), |
| 172 |
'text_domain' => WDKIT_TEXT_DOMAIN, |
| 173 |
'use_editor' => $this->wdkit_use_editor(), |
| 174 |
'post_type_list' => $this->wdkit_get_post_type_list(), |
| 175 |
|
| 176 |
/** Widget Builder Path */ |
| 177 |
'WDKIT_SITE_URL' => WDKIT_GET_SITE_URL, |
| 178 |
'WDKIT_SERVER_PATH' => WDKIT_SERVER_PATH, |
| 179 |
'WDKIT_BUILDER_PATH' => WDKIT_BUILDER_PATH, |
| 180 |
'WDKIT_VERSION' => WDKIT_VERSION, |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
/**Ace Editor files for Widget-builder */ |
| 185 |
if ( 'toplevel_page_wdesign-kit' === $hook && Wdkit_Wdesignkit::wdkit_is_compatible( 'builder' ) ) { |
| 186 |
wp_enqueue_script( 'widgetBuilder-script-editor-js', WDKIT_URL . 'assets/js/extra/ace.min.js', array( 'wp-element' ), WDKIT_VERSION, true ); |
| 187 |
wp_enqueue_script( 'widgetBuilder-script-editor-cobalt', WDKIT_URL . 'assets/js/extra/theme-cobalt.js', array( 'wp-element' ), WDKIT_VERSION, true ); |
| 188 |
wp_enqueue_script( 'widgetBuilder-script-editor-html', WDKIT_URL . 'assets/js/extra//mode-html.js', array( 'wp-element' ), WDKIT_VERSION, true ); |
| 189 |
} |
| 190 |
|
| 191 |
if ( 'elementor' === $hook ) { |
| 192 |
wp_enqueue_script( 'wdkit-frontend-editor', WDKIT_ASSETS . '/js/main/elementor/elementor-editor.js', array( 'jquery', 'wp-i18n' ), WDKIT_VERSION, true ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Add Menu Page WdKit. |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
*/ |
| 201 |
public function wdkit_admin_menu() { |
| 202 |
$capability = 'manage_options'; |
| 203 |
if ( current_user_can( $capability ) ) { |
| 204 |
$hook = add_menu_page( __( 'WDesignKit', 'wdesignkit' ), __( 'WDesignKit', 'wdesignkit' ), 'manage_options', 'wdesign-kit', array( $this, 'wdkit_menu_page_template' ), WDKIT_ASSETS . 'images/svg/logo-icon.svg' ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Load wdkit page content. |
| 210 |
* |
| 211 |
* @since 1.0.0 |
| 212 |
*/ |
| 213 |
public function wdkit_menu_page_template() { |
| 214 |
echo '<div id="wdesignkit-app"></div>'; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
Wdkit_Enqueue::get_instance(); |
| 219 |
} |
| 220 |
|