| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimatePostKit\Includes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
// Load the Remote Data Handler |
| 10 |
require_once __DIR__ . '/class-remote-data-handler.php'; |
| 11 |
|
| 12 |
use UltimatePostKit\Admin\ModuleService; |
| 13 |
use Elementor\Plugin; |
| 14 |
/** |
| 15 |
* Overwrite the feedback method in the WP_Upgrader_Skin |
| 16 |
* to suppress the normal feedback. |
| 17 |
*/ |
| 18 |
|
| 19 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 20 |
|
| 21 |
class Quiet_Upgrader_Skin extends \WP_Upgrader_Skin { |
| 22 |
/* |
| 23 |
* Suppress normal upgrader feedback / output |
| 24 |
*/ |
| 25 |
public function feedback( $string, ...$args ) { |
| 26 |
/* no output */ |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
class Setup_Wizard { |
| 32 |
|
| 33 |
// Singleton instance |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
// Constructor |
| 37 |
private function __construct() { |
| 38 |
$this->init_hooks(); |
| 39 |
} |
| 40 |
|
| 41 |
// Get instance |
| 42 |
public static function get_instance() { |
| 43 |
if ( self::$instance == null ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
// Initialize hooks |
| 50 |
private function init_hooks() { |
| 51 |
add_action( 'wp_ajax_setup_wizard_install_plugins', array( $this, 'install_plugins' ) ); |
| 52 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 53 |
add_action( 'admin_init', array( $this, 'activate_default_widgets' ) ); |
| 54 |
add_action( 'admin_init', array( $this, 'maybe_display_setup_wizard' ) ); |
| 55 |
add_action( 'admin_init', array( $this, 'check_manual_wizard_request' ) ); |
| 56 |
|
| 57 |
if ( function_exists( 'add_filter' ) ) { |
| 58 |
add_filter( 'auto_update_translation', '__return_false' ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Check for manual wizard requests |
| 63 |
public function check_manual_wizard_request() { |
| 64 |
$is_setup_wizard_request = isset($_GET['upk_setup_wizard']) && $_GET['upk_setup_wizard'] === 'show'; |
| 65 |
|
| 66 |
if ( $is_setup_wizard_request ) { |
| 67 |
// Use the same approach as first activation - completely override the page |
| 68 |
add_action('admin_head', function() { |
| 69 |
?> |
| 70 |
<style> |
| 71 |
html, body { |
| 72 |
height: 100%; |
| 73 |
margin: 0; |
| 74 |
padding: 0; |
| 75 |
overflow: hidden; |
| 76 |
} |
| 77 |
#wpwrap, #wpcontent, #wpbody, #wpbody-content { |
| 78 |
height: 100%; |
| 79 |
padding: 0; |
| 80 |
margin: 0; |
| 81 |
} |
| 82 |
#adminmenumain, #wpadminbar { |
| 83 |
display: none; |
| 84 |
} |
| 85 |
</style> |
| 86 |
<script> |
| 87 |
jQuery(document).ready(function($) { |
| 88 |
$('body').addClass('bdt-setup-wizard-active'); |
| 89 |
}); |
| 90 |
</script> |
| 91 |
<?php |
| 92 |
|
| 93 |
// Display setup wizard using the same method as first activation |
| 94 |
$this->display_page(); |
| 95 |
}); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Display wizard in fullscreen mode |
| 100 |
public function display_wizard_fullscreen() { |
| 101 |
?> |
| 102 |
<style> |
| 103 |
html, body { |
| 104 |
height: 100%; |
| 105 |
margin: 0; |
| 106 |
padding: 0; |
| 107 |
overflow: hidden; |
| 108 |
} |
| 109 |
#wpwrap, #wpcontent, #wpbody, #wpbody-content { |
| 110 |
height: 100%; |
| 111 |
padding: 0; |
| 112 |
margin: 0; |
| 113 |
} |
| 114 |
#adminmenumain, #wpadminbar { |
| 115 |
display: none; |
| 116 |
} |
| 117 |
</style> |
| 118 |
<?php |
| 119 |
// Directly output the wizard content |
| 120 |
add_action('admin_footer', function() { |
| 121 |
echo '<div id="upk-setup-wizard-container">'; |
| 122 |
$this->display_page(); |
| 123 |
echo '</div>'; |
| 124 |
?> |
| 125 |
<script> |
| 126 |
jQuery(document).ready(function($) { |
| 127 |
$('body').addClass('bdt-setup-wizard-active'); |
| 128 |
// Hide all other content and show only our wizard |
| 129 |
$('#wpbody-content').html($('#upk-setup-wizard-container').html()); |
| 130 |
$('#upk-setup-wizard-container').remove(); |
| 131 |
}); |
| 132 |
</script> |
| 133 |
<?php |
| 134 |
}, 999); |
| 135 |
} |
| 136 |
|
| 137 |
// Get wizard HTML content |
| 138 |
public function get_wizard_html() { |
| 139 |
ob_start(); |
| 140 |
?> |
| 141 |
<div class="bdt-setup-wizard-overlay upk-setup-wizard "> |
| 142 |
<div class="bdt-setup-wizard content-loaded"> |
| 143 |
<?php |
| 144 |
require_once plugin_dir_path( BDTUPK__FILE__ ) . 'includes/setup-wizard/views/render.php'; |
| 145 |
?> |
| 146 |
</div> |
| 147 |
</div> |
| 148 |
<?php |
| 149 |
return ob_get_clean(); |
| 150 |
} |
| 151 |
|
| 152 |
// Check if this is first activation and display setup wizard if needed |
| 153 |
public function maybe_display_setup_wizard() { |
| 154 |
// Only check for first activation here |
| 155 |
if ( get_option( 'bdtupk_setup_wizard_completed' ) === false ) { |
| 156 |
// Set the flag so it doesn't run again |
| 157 |
update_option( 'bdtupk_setup_wizard_completed', true ); |
| 158 |
|
| 159 |
// Add a header to ensure proper full-page display |
| 160 |
add_action('admin_head', function() { |
| 161 |
?> |
| 162 |
<style> |
| 163 |
html, body { |
| 164 |
height: 100%; |
| 165 |
margin: 0; |
| 166 |
padding: 0; |
| 167 |
overflow: hidden; |
| 168 |
} |
| 169 |
#wpwrap, #wpcontent, #wpbody, #wpbody-content { |
| 170 |
height: 100%; |
| 171 |
padding: 0; |
| 172 |
margin: 0; |
| 173 |
} |
| 174 |
#adminmenumain, #wpadminbar { |
| 175 |
display: none; |
| 176 |
} |
| 177 |
</style> |
| 178 |
<script> |
| 179 |
jQuery(document).ready(function($) { |
| 180 |
$('body').addClass('bdt-setup-wizard-active'); |
| 181 |
}); |
| 182 |
</script> |
| 183 |
<?php |
| 184 |
|
| 185 |
// Display setup wizard |
| 186 |
$this->display_page(); |
| 187 |
}); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Keep the admin_menu method for reference but not hooked |
| 192 |
public function admin_menu() { |
| 193 |
add_submenu_page( |
| 194 |
'ultimate_post_kit_options', |
| 195 |
esc_html__( 'Setup Wizard', 'ultimate-post-kit' ), |
| 196 |
esc_html__( 'Setup Wizard', 'ultimate-post-kit' ), |
| 197 |
'manage_options', |
| 198 |
'ultimate-post-kit-setup-wizard', |
| 199 |
array( $this, 'display_page' ) |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
public function display_page() { |
| 204 |
?> |
| 205 |
<div class="bdt-setup-wizard-overlay upk-setup-wizard"> |
| 206 |
<div class="bdt-setup-wizard content-loaded"> |
| 207 |
<?php |
| 208 |
require_once plugin_dir_path( BDTUPK__FILE__ ) . 'includes/setup-wizard/views/render.php'; |
| 209 |
?> |
| 210 |
</div> |
| 211 |
</div> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
// Enqueue necessary scripts |
| 216 |
public function enqueue_scripts() { |
| 217 |
|
| 218 |
$direction_suffix = is_rtl() ? '.rtl' : ''; |
| 219 |
|
| 220 |
wp_enqueue_style('bdt-uikit', BDTUPK_ADMIN_ASSETS_URL . 'css/bdt-uikit' . $direction_suffix . '.css', [], '3.17.0'); |
| 221 |
wp_enqueue_script('bdt-uikit', BDTUPK_ADMIN_ASSETS_URL . 'js/bdt-uikit.min.js', ['jquery'], '3.17.0'); |
| 222 |
|
| 223 |
wp_register_script( 'upk-setup-wizard', plugins_url( 'assets/js/setup-wizard.js', __FILE__ ), array( 'jquery' ), '1.0.0', true ); |
| 224 |
wp_register_style( 'upk-setup-wizard', plugins_url( 'assets/css/setup-wizard.css', __FILE__ ), array(), '1.0.0' ); |
| 225 |
|
| 226 |
wp_enqueue_script( 'upk-setup-wizard' ); |
| 227 |
wp_enqueue_style( 'upk-setup-wizard' ); |
| 228 |
|
| 229 |
wp_localize_script( |
| 230 |
'upk-setup-wizard', |
| 231 |
'BDT_SetupWizard', |
| 232 |
array( |
| 233 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 234 |
'nonce' => wp_create_nonce( 'setup_wizard_nonce' ), |
| 235 |
'is_fullscreen' => true |
| 236 |
) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
public static function get_widget_map() { |
| 241 |
$arr_obj = ModuleService::get_widget_settings( |
| 242 |
function ( $settings ) { |
| 243 |
$core_widgets = $settings['settings_fields']['ultimate_post_kit_active_modules']; |
| 244 |
return $core_widgets; |
| 245 |
} |
| 246 |
); |
| 247 |
return $arr_obj; |
| 248 |
} |
| 249 |
|
| 250 |
// Install plugins |
| 251 |
public function install_plugins() { |
| 252 |
check_ajax_referer( 'setup_wizard_nonce', 'nonce' ); |
| 253 |
|
| 254 |
$plugin_slugs = isset( $_POST['plugins'] ) ? $_POST['plugins'] : array(); |
| 255 |
|
| 256 |
if ( empty( $plugin_slugs ) || ! is_array( $plugin_slugs ) ) { |
| 257 |
wp_send_json_error( array( 'message' => 'Invalid plugins array' ) ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 261 |
wp_send_json_error( array( 'message' => 'Unauthorized' ) ); |
| 262 |
} |
| 263 |
|
| 264 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 265 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 266 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php'; |
| 267 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 268 |
|
| 269 |
// Replace new \Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed. |
| 270 |
$skin = new Quiet_Upgrader_Skin(); |
| 271 |
// $skin = new \Plugin_Installer_Skin( array( 'api' => $api ) ); |
| 272 |
$upgrader = new \Plugin_Upgrader( $skin ); |
| 273 |
|
| 274 |
// $upgrader = new \Plugin_Upgrader(); |
| 275 |
|
| 276 |
$installedPlugins = get_plugins(); |
| 277 |
$results = array(); |
| 278 |
|
| 279 |
foreach ( $plugin_slugs as $plugin_slug ) { |
| 280 |
// skip when the plugin is already active |
| 281 |
if (is_plugin_active($plugin_slug)) { |
| 282 |
$results[] = array( |
| 283 |
'slug' => $plugin_slug, |
| 284 |
'success' => true, |
| 285 |
'message' => 'Installed and activated successfully', |
| 286 |
); |
| 287 |
continue; |
| 288 |
} |
| 289 |
|
| 290 |
// Download the plugin if the plugin is not installed |
| 291 |
if (!isset($installedPlugins[$plugin_slug])) { |
| 292 |
$slug = explode('/', $plugin_slug)[0]; |
| 293 |
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) ); |
| 294 |
|
| 295 |
if ( is_wp_error( $api ) ) { |
| 296 |
$results[] = array( |
| 297 |
'slug' => $plugin_slug, |
| 298 |
'success' => false, |
| 299 |
'message' => $api->get_error_message(), |
| 300 |
); |
| 301 |
continue; |
| 302 |
} |
| 303 |
|
| 304 |
$result = $upgrader->install( $api->download_link ); |
| 305 |
if ( is_wp_error( $result ) ) { |
| 306 |
$results[] = array( |
| 307 |
'slug' => $plugin_slug, |
| 308 |
'success' => false, |
| 309 |
'message' => $result->get_error_message(), |
| 310 |
); |
| 311 |
continue; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// active the plugin |
| 316 |
if ( is_plugin_inactive($plugin_slug) ) { |
| 317 |
$activation_result = activate_plugin( $plugin_slug ); |
| 318 |
if ( is_wp_error( $activation_result ) ) { |
| 319 |
$results[] = array( |
| 320 |
'slug' => $slug, |
| 321 |
'success' => false, |
| 322 |
'message' => $activation_result->get_error_message(), |
| 323 |
); |
| 324 |
continue; |
| 325 |
} |
| 326 |
|
| 327 |
$results[] = array( |
| 328 |
'slug' => $plugin_slug, |
| 329 |
'success' => true, |
| 330 |
'message' => 'Installed and activated successfully', |
| 331 |
); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
ob_clean(); |
| 336 |
wp_send_json_success( array( 'results' => $results ) ); |
| 337 |
wp_die(); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Get the main plugin file path for a given slug. |
| 342 |
* |
| 343 |
* @param string $slug Plugin slug. |
| 344 |
* @return string|false Plugin file path or false if not found. |
| 345 |
*/ |
| 346 |
private function get_plugin_file( $slug ) { |
| 347 |
$plugins = get_plugins(); |
| 348 |
|
| 349 |
foreach ( $plugins as $file => $plugin ) { |
| 350 |
if ( strpos( $file, $slug ) !== false ) { |
| 351 |
return $file; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
return false; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Activate default widgets in setup wizard |
| 360 |
*/ |
| 361 |
public function activate_default_widgets() { |
| 362 |
// List of widgets to activate by default |
| 363 |
$default_active_widgets = array( |
| 364 |
'alex-grid', |
| 365 |
'alice-grid', |
| 366 |
'alter-carousel', |
| 367 |
'banner', |
| 368 |
'buzz-list', |
| 369 |
'carbon-slider', |
| 370 |
'featured-list', |
| 371 |
'news-ticker', |
| 372 |
'pholox-slider', |
| 373 |
'timeline', |
| 374 |
'category', |
| 375 |
'social-count', |
| 376 |
'tag-cloud' |
| 377 |
); |
| 378 |
|
| 379 |
// Get current active modules |
| 380 |
$active_modules = get_option('ultimate_post_kit_active_modules', array()); |
| 381 |
|
| 382 |
// Make sure $active_modules is an array |
| 383 |
if (!is_array($active_modules)) { |
| 384 |
$active_modules = array(); |
| 385 |
} |
| 386 |
|
| 387 |
// Check if active_modules option exists and is not empty |
| 388 |
// If it's a new installation or option doesn't exist, we'll set our defaults |
| 389 |
$modified = false; |
| 390 |
|
| 391 |
foreach ($default_active_widgets as $widget) { |
| 392 |
// Only set if not already defined (prevents overriding user settings on existing installations) |
| 393 |
if (!isset($active_modules[$widget])) { |
| 394 |
$active_modules[$widget] = 'on'; |
| 395 |
$modified = true; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
// Update the option if changes were made |
| 400 |
if ($modified) { |
| 401 |
update_option('ultimate_post_kit_active_modules', $active_modules); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// Initialize the Setup Wizard |
| 407 |
Setup_Wizard::get_instance(); |
| 408 |
|
| 409 |
use Elementor\TemplateLibrary\Source_Local; |
| 410 |
|
| 411 |
add_action('wp_ajax_import_elementor_template', function () { |
| 412 |
check_ajax_referer( 'setup_wizard_nonce', 'nonce' ); |
| 413 |
|
| 414 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 415 |
wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'ultimate-post-kit' ) ) ); |
| 416 |
wp_die(); |
| 417 |
} |
| 418 |
|
| 419 |
$json_url = isset( $_POST['import_url'] ) ? esc_url_raw( wp_unslash( $_POST['import_url'] ) ) : ''; |
| 420 |
|
| 421 |
$response = wp_safe_remote_get($json_url, array( |
| 422 |
'timeout' => 60, |
| 423 |
'sslverify' => false |
| 424 |
)); |
| 425 |
|
| 426 |
if (is_wp_error($response)) { |
| 427 |
wp_send_json_error(['message' => esc_html__('Failed to fetch template from URL.', 'ultimate-post-kit')]); |
| 428 |
wp_die(); |
| 429 |
} |
| 430 |
|
| 431 |
$sourceData = wp_remote_retrieve_body($response); |
| 432 |
$sourceData2 = json_decode($sourceData, true); |
| 433 |
|
| 434 |
if (!$sourceData2 || !is_array($sourceData2)) { |
| 435 |
wp_send_json_error(['message' => esc_html__('Failed to fetch template from URL.', 'ultimate-post-kit')]); |
| 436 |
wp_die(); |
| 437 |
} |
| 438 |
|
| 439 |
$temp_file = wp_upload_dir()['path'] . '/elementor_import_' . time() . '.json'; |
| 440 |
file_put_contents($temp_file, $sourceData); |
| 441 |
|
| 442 |
// Initialize Elementor's Template Importer |
| 443 |
if (!class_exists('\Elementor\TemplateLibrary\Source_Local')) { |
| 444 |
wp_delete_file($temp_file); |
| 445 |
wp_send_json_error(['message' => esc_html__('Elementor is not installed or activated!', 'ultimate-post-kit')]); |
| 446 |
wp_die(); |
| 447 |
} |
| 448 |
|
| 449 |
$manager = new Source_Local(); |
| 450 |
$templateData = $manager->import_template('elementor_template', $temp_file); |
| 451 |
wp_delete_file($temp_file); // Delete temp file after import |
| 452 |
|
| 453 |
if (is_wp_error($templateData) || !is_array($templateData) || empty($templateData[0]['template_id'])) { |
| 454 |
wp_send_json_error(['message' => esc_html__('Failed to import template!', 'ultimate-post-kit')]); |
| 455 |
wp_die(); |
| 456 |
} |
| 457 |
|
| 458 |
$template_id = $templateData[0]['template_id']; |
| 459 |
$metaData = get_post_meta($template_id); |
| 460 |
|
| 461 |
$page_title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : esc_html__("No Title", 'ultimate-post-kit'); |
| 462 |
|
| 463 |
// Validate Elementor Data |
| 464 |
if (!isset($metaData['_elementor_data'][0])) { |
| 465 |
wp_send_json_error(['message' => esc_html__('Elementor data not found in template.', 'ultimate-post-kit')]); |
| 466 |
wp_die(); |
| 467 |
} |
| 468 |
|
| 469 |
$_elementor_data = wp_slash($metaData['_elementor_data'][0]); |
| 470 |
|
| 471 |
// Create New Page |
| 472 |
$new_post_id = wp_insert_post([ |
| 473 |
'post_type' => 'page', |
| 474 |
'post_status' => empty($page_title) ? 'draft' : 'publish', |
| 475 |
'post_title' => $page_title, |
| 476 |
'post_content' => '', |
| 477 |
]); |
| 478 |
|
| 479 |
if (is_wp_error($new_post_id)) { |
| 480 |
wp_send_json_error(['message' => esc_html__('Failed to create page!', 'ultimate-post-kit')]); |
| 481 |
wp_die(); |
| 482 |
} |
| 483 |
|
| 484 |
// Assign Elementor Template Data |
| 485 |
update_post_meta($new_post_id, '_elementor_data', $_elementor_data); |
| 486 |
|
| 487 |
// Import Page Settings if available |
| 488 |
if (isset($metaData['_elementor_page_settings'][0])) { |
| 489 |
$_elementor_page_settings = maybe_unserialize($metaData['_elementor_page_settings'][0]); |
| 490 |
update_post_meta($new_post_id, '_elementor_page_settings', $_elementor_page_settings); |
| 491 |
} |
| 492 |
|
| 493 |
update_post_meta($new_post_id, '_elementor_template_type', $sourceData2['type'] ?? ''); |
| 494 |
update_post_meta($new_post_id, '_elementor_edit_mode', 'builder'); |
| 495 |
// update_post_meta($new_post_id, '_wp_page_template', !empty($pageTemplate) ? $pageTemplate : 'elementor_header_footer'); |
| 496 |
|
| 497 |
wp_send_json_success([ |
| 498 |
'message' => esc_html__('The template was imported successfully.', 'ultimate-post-kit'), |
| 499 |
'ids' => $new_post_id, |
| 500 |
'edit_link' => admin_url('post.php?post=' . $new_post_id . '&action=elementor'), |
| 501 |
]); |
| 502 |
} |
| 503 |
); |
| 504 |
|
| 505 |
|
| 506 |
add_action('wp_ajax_import_upk_elementor_bundle_template', function () { |
| 507 |
check_ajax_referer('setup_wizard_nonce', 'nonce'); |
| 508 |
|
| 509 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 510 |
wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'ultimate-post-kit' ) ) ); |
| 511 |
wp_die(); |
| 512 |
} |
| 513 |
|
| 514 |
$file_url = isset($_POST['import_url']) ? esc_url_raw(wp_unslash($_POST['import_url'])) : ''; |
| 515 |
|
| 516 |
if (!filter_var($file_url, FILTER_VALIDATE_URL) || 0 !== strpos($file_url, 'http')) { |
| 517 |
wp_send_json_error(['message' => esc_html__('Invalid import URL', 'ultimate-post-kit')]); |
| 518 |
} |
| 519 |
|
| 520 |
$remote_zip_request = wp_safe_remote_get($file_url, array( |
| 521 |
'timeout' => 60, |
| 522 |
'sslverify' => false, |
| 523 |
)); |
| 524 |
|
| 525 |
if (is_wp_error($remote_zip_request)) { |
| 526 |
wp_send_json_error(['message' => esc_html__('Failed to fetch template from URL.', 'ultimate-post-kit')]); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
if (200 !== $remote_zip_request['response']['code']) { |
| 531 |
wp_send_json_error(['message' => esc_html__('Failed to fetch template from URL.', 'ultimate-post-kit')]); |
| 532 |
} |
| 533 |
|
| 534 |
$kit_zip_path = Plugin::$instance->uploads_manager->create_temp_file($remote_zip_request['body'], 'kit.zip'); |
| 535 |
|
| 536 |
$app = Plugin::$instance->app; |
| 537 |
if (!$app) { |
| 538 |
wp_send_json_error(['message' => esc_html__('Elementor app not available', 'ultimate-post-kit')]); |
| 539 |
} |
| 540 |
|
| 541 |
$import_export_module = $app->get_component('import-export'); |
| 542 |
|
| 543 |
try { |
| 544 |
$result = $import_export_module->upload_kit($kit_zip_path, 'local'); |
| 545 |
$manifest = $result['manifest'] ?? []; |
| 546 |
$plugins = $manifest['plugins']; |
| 547 |
|
| 548 |
$missingPlugins = []; |
| 549 |
foreach ($plugins as $plugin) { |
| 550 |
$pluginSlug = $plugin['plugin'].".php"; |
| 551 |
if (is_plugin_inactive($pluginSlug)) { |
| 552 |
$missingPlugins[] = $plugin; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
if (count($missingPlugins)) { |
| 557 |
wp_send_json_error([ |
| 558 |
'plugins' => $missingPlugins, |
| 559 |
'message' => esc_html__('Missing plugins', 'ultimate-post-kit'), |
| 560 |
]); |
| 561 |
} |
| 562 |
|
| 563 |
$tmp_folder_id = $result['session']; |
| 564 |
$includes = []; |
| 565 |
$selectedCustomPostTypes = []; |
| 566 |
|
| 567 |
if (isset($manifest['templates'])) { |
| 568 |
$includes[] = 'templates'; |
| 569 |
} |
| 570 |
|
| 571 |
if (isset($manifest['content'])) { |
| 572 |
$includes[] = 'content'; |
| 573 |
} |
| 574 |
|
| 575 |
if (isset($manifest['site-settings'])) { |
| 576 |
$includes[] = 'settings'; |
| 577 |
} |
| 578 |
|
| 579 |
if (isset($manifest['custom-post-type-title'])) { |
| 580 |
$selectedCustomPostTypes = array_keys($manifest['custom-post-type-title']); |
| 581 |
} |
| 582 |
|
| 583 |
$settings = [ |
| 584 |
'id' => '', |
| 585 |
'session' => $tmp_folder_id, |
| 586 |
'include' => $includes, |
| 587 |
'overrideConditions' => [], |
| 588 |
'selectedCustomPostTypes' => $selectedCustomPostTypes, |
| 589 |
]; |
| 590 |
|
| 591 |
$import = $import_export_module->import_kit($tmp_folder_id, $settings, true); |
| 592 |
|
| 593 |
Plugin::$instance->uploads_manager->enable_unfiltered_files_upload(); |
| 594 |
|
| 595 |
wp_send_json_success($import); |
| 596 |
} catch (\Throwable $e) { |
| 597 |
wp_send_json_error(['message' => esc_html__('Import failed: ', 'ultimate-post-kit') . esc_html($e->getMessage())]); |
| 598 |
} |
| 599 |
}); |
| 600 |
|
| 601 |
add_action('wp_ajax_import_upk_elementor_bundle_runner_template', function () { |
| 602 |
check_ajax_referer('setup_wizard_nonce', 'nonce'); |
| 603 |
|
| 604 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 605 |
wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'ultimate-post-kit' ) ) ); |
| 606 |
wp_die(); |
| 607 |
} |
| 608 |
|
| 609 |
$runner = isset($_POST['runner']) ? sanitize_text_field(wp_unslash($_POST['runner'])) : ''; |
| 610 |
$sessionId = isset($_POST['sessionId']) ? sanitize_text_field(wp_unslash($_POST['sessionId'])) : ''; |
| 611 |
|
| 612 |
if (!$runner || !$sessionId) { |
| 613 |
wp_send_json_error(['message' => esc_html__('Required Param Is Missing.', 'ultimate-post-kit')]); |
| 614 |
} |
| 615 |
|
| 616 |
$app = Plugin::$instance->app; |
| 617 |
if (!$app) { |
| 618 |
wp_send_json_error(['message' => esc_html__('Elementor app not available.', 'ultimate-post-kit')]); |
| 619 |
} |
| 620 |
|
| 621 |
try { |
| 622 |
@ini_set('max_execution_time', 60 * 5); |
| 623 |
|
| 624 |
$import_export_module = $app->get_component('import-export'); |
| 625 |
$import = $import_export_module->import_kit_by_runner($sessionId, $runner); |
| 626 |
|
| 627 |
do_action('elementor/import-export/import-kit/runner/after-run', $import); |
| 628 |
wp_send_json_success($import); |
| 629 |
} catch (\Throwable $throwable) { |
| 630 |
wp_send_json_error(['message' => $throwable->getMessage()]); |
| 631 |
} |
| 632 |
}); |
| 633 |
|