PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.0.9
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.0.9
4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 All 146 releases
ultimate-post-kit / includes / setup-wizard / init.php

init.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.0.9, at includes/setup-wizard/init.php

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