| 1 |
<?php |
| 2 |
|
| 3 |
namespace UiCoreElements; |
| 4 |
|
| 5 |
/** |
| 6 |
* DesignCloud Handler |
| 7 |
*/ |
| 8 |
class DesignCloud |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Constructor function to initialize hooks |
| 12 |
* |
| 13 |
* @return void |
| 14 |
* @author Andrei Voica <andrei@uicore.co> |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('elementor/editor/before_enqueue_scripts', [$this, 'enqueue_elementor_assets']); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Elementor Editor Style, Fonts and Scripts |
| 25 |
* |
| 26 |
* @return void |
| 27 |
* @author Andrei Voica <andrei@uicore.co |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function enqueue_elementor_assets() |
| 31 |
{ |
| 32 |
if (!function_exists('_uicore_pro') || apply_filters('uicore_hide_design_cloud', false)) { |
| 33 |
return; |
| 34 |
} |
| 35 |
$dc_data = require UICORE_ELEMENTS_PATH . '/assets/design-cloud/design-cloud.asset.php'; |
| 36 |
|
| 37 |
wp_enqueue_script( |
| 38 |
'ui-e-design-cloud', |
| 39 |
UICORE_ELEMENTS_ASSETS . '/design-cloud/design-cloud.js', |
| 40 |
$dc_data['dependencies'], |
| 41 |
$dc_data['version'], |
| 42 |
true |
| 43 |
); |
| 44 |
wp_enqueue_style( |
| 45 |
'ui-e-design-cloud', |
| 46 |
UICORE_ELEMENTS_ASSETS . '/design-cloud/design-cloud.css', |
| 47 |
array(), |
| 48 |
$dc_data['version'], |
| 49 |
); |
| 50 |
$kit_id = get_option('elementor_active_kit'); |
| 51 |
$kit_id = $kit_id ? $kit_id : 1; |
| 52 |
$assets = $this->get_elementor_kit_assets($kit_id); |
| 53 |
|
| 54 |
$upload_dir = wp_upload_dir(); |
| 55 |
$css_path = class_exists('\UiCore\Assets') |
| 56 |
? $upload_dir['basedir'] . "/uicore-global.css" |
| 57 |
: $upload_dir['basedir'] . "/elementor/css/post-{$kit_id}.css"; |
| 58 |
|
| 59 |
$inline_css = file_exists($css_path) ? file_get_contents($css_path) : ''; |
| 60 |
|
| 61 |
$local_data = get_option('uicore_connect', [ |
| 62 |
'url' => '', |
| 63 |
'token' => '', |
| 64 |
]); |
| 65 |
$product = \defined('UICORE_NAME') ? \UICORE_NAME : 'uicore-elements-free'; |
| 66 |
$product = \apply_filters('uicore_product_id', $product); |
| 67 |
$dc_settings = [ |
| 68 |
'api' => 'https://dc.uicore.co', |
| 69 |
'builder' => 'el', |
| 70 |
'nonce' => wp_create_nonce('wp_rest'), |
| 71 |
'preview' => [ |
| 72 |
'class' => 'elementor-kit-' . $kit_id, |
| 73 |
'assets' => $assets, |
| 74 |
'inline_css' => $inline_css, |
| 75 |
], |
| 76 |
'local_url' => get_site_url(), |
| 77 |
'license' => [ |
| 78 |
'product' => $product, |
| 79 |
'key' => $local_data['token'], |
| 80 |
'url' => $local_data['url'], |
| 81 |
] |
| 82 |
]; |
| 83 |
wp_add_inline_script('ui-e-design-cloud', "window.ui_dc_global = " . json_encode($dc_settings) . ";", 'before'); |
| 84 |
} |
| 85 |
|
| 86 |
function get_elementor_kit_assets($kit_id) |
| 87 |
{ |
| 88 |
$post_css_file = new \Elementor\Core\Files\CSS\Post($kit_id); |
| 89 |
$meta = $post_css_file->get_meta(); |
| 90 |
$assets = []; |
| 91 |
|
| 92 |
// Handle fonts. |
| 93 |
if (! empty($meta['fonts'])) { |
| 94 |
$fonts_url = \Elementor\Plugin::$instance->frontend->get_stable_google_fonts_url($meta['fonts']); |
| 95 |
$assets[] = $fonts_url; |
| 96 |
} |
| 97 |
|
| 98 |
if (! empty($meta['icons'])) { |
| 99 |
$icons_types = \Elementor\Icons_Manager::get_icon_manager_tabs(); |
| 100 |
|
| 101 |
foreach ($meta['icons'] as $icon_font) { |
| 102 |
if (isset($icons_types[$icon_font]['url'])) { |
| 103 |
$assets[] = $icons_types[$icon_font]['url']; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return $assets; |
| 108 |
} |
| 109 |
|
| 110 |
static function process_import_content(\Elementor\Controls_Stack $element) |
| 111 |
{ |
| 112 |
$element_data = $element->get_data(); |
| 113 |
$method = 'on_import'; |
| 114 |
|
| 115 |
if (method_exists($element, $method)) { |
| 116 |
$element_data = $element->{$method}($element_data); |
| 117 |
} |
| 118 |
|
| 119 |
foreach ($element->get_controls() as $control) { |
| 120 |
$control_class = \Elementor\Plugin::instance()->controls_manager->get_control($control['type']); |
| 121 |
|
| 122 |
if (!$control_class) { |
| 123 |
return $element_data; |
| 124 |
} |
| 125 |
|
| 126 |
// If the control isn't exist, like a plugin that creates the control but deactivated. |
| 127 |
if (method_exists($control_class, $method)) { |
| 128 |
$element_data['settings'][$control['name']] = $control_class->{$method}($element->get_settings($control['name']), $control); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $element_data; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static function import_content($content) |
| 137 |
{ |
| 138 |
return \Elementor\Plugin::instance()->db->iterate_data( |
| 139 |
$content, |
| 140 |
function ($element_data) { |
| 141 |
//change $element id |
| 142 |
$element_data['id'] = \Elementor\Utils::generate_random_string(); |
| 143 |
$element = \Elementor\Plugin::instance()->elements_manager->create_element_instance($element_data); |
| 144 |
|
| 145 |
if (!$element) { |
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
return self::process_import_content($element); |
| 150 |
} |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
new DesignCloud(); |
| 155 |
|