| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\ImportRevert; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 7 |
use Templately\Utils\Response\AjaxResponder; |
| 8 |
use Templately\Utils\Response\ErrorCode; |
| 9 |
|
| 10 |
/** |
| 11 |
* RevertController — the "revert to old website" AJAX endpoint. |
| 12 |
* |
| 13 |
* Moved from modules/full-site-import/Ajax/ (2026-07-23, the split-modules |
| 14 |
* follow-up): revert reads FINISHED-import artifacts (`__templately_*` backup |
| 15 |
* options via FSI's `Utils::get_backup_options()`, `templately_fsi_imported_list`) |
| 16 |
* but shares no live import state with the runner pipeline — a real module |
| 17 |
* boundary. Still registered through FullSiteImport's nonce/capability AJAX |
| 18 |
* wrapper, via its `templately_fsi_ajax_handlers` filter seam (see module.php); |
| 19 |
* the `wp_ajax_templately_pack_import_revert` action name is unchanged. |
| 20 |
*/ |
| 21 |
class RevertController { |
| 22 |
public function import_revert() { |
| 23 |
|
| 24 |
// // Get the nonce value from the request (usually from $_POST or $_GET) |
| 25 |
// $received_nonce = isset($_REQUEST['_wpnonce']) ? $_REQUEST['_wpnonce'] : ''; |
| 26 |
|
| 27 |
// // Verify the nonce using wp_verify_nonce() |
| 28 |
// $verified = wp_verify_nonce($received_nonce, 'templately_pack_import_revert_nonce'); |
| 29 |
|
| 30 |
// if (!$verified) { |
| 31 |
// wp_send_json_error("Nonce not verified."); |
| 32 |
// } |
| 33 |
|
| 34 |
delete_option('templately_import_platform'); |
| 35 |
|
| 36 |
$option_active = null; |
| 37 |
$options_deleted = false; |
| 38 |
$imported_list_deleted = false; |
| 39 |
$options = Utils::get_backup_options(); |
| 40 |
$status_args = [ 'post_type' => 'templately_library' ]; |
| 41 |
$all_post_url = add_query_arg( [ |
| 42 |
"page" => "templately_settings", |
| 43 |
"path" => "settings/elementor/miscellaneous", |
| 44 |
], admin_url('admin.php' )); |
| 45 |
// wp_send_json_success([$options]); |
| 46 |
|
| 47 |
if(class_exists('Elementor\Plugin')){ |
| 48 |
$kits_manager = Plugin::$instance->kits_manager; |
| 49 |
$option_active = $kits_manager::OPTION_ACTIVE; |
| 50 |
$kit = $kits_manager->get_active_kit(); |
| 51 |
|
| 52 |
if ( ! $kit->get_id() ) { |
| 53 |
$kit = $kits_manager->create_default(); |
| 54 |
update_option( $kits_manager::OPTION_ACTIVE, $kit ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if (!empty($options) && is_array($options)) { |
| 60 |
foreach ($options as $key => $value) { |
| 61 |
if ('stylesheet' === $key) { |
| 62 |
if (get_option('stylesheet') !== $value) { |
| 63 |
switch_theme($value); |
| 64 |
} |
| 65 |
} else if($option_active === $key && class_exists('Elementor\Plugin')) { |
| 66 |
$kits_manager->revert( (int) $kits_manager->get_active_id(), (int) $value, 0 ); |
| 67 |
$kit = $kits_manager->get_active_kit(); |
| 68 |
$settings = $kit->get_data('settings'); |
| 69 |
if ( isset( $settings['site_logo'] ) ) { |
| 70 |
set_theme_mod( 'custom_logo', $settings['site_logo']['id'] ); |
| 71 |
} |
| 72 |
} else { |
| 73 |
update_option($key, $value); |
| 74 |
} |
| 75 |
delete_option("__templately_$key"); |
| 76 |
$options_deleted = true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$imported_list = get_option('templately_fsi_imported_list', []); |
| 81 |
if (!empty($imported_list) && is_array($imported_list)) { |
| 82 |
$_GET['force_delete_kit'] = 1; // Fallback GET Ready! |
| 83 |
foreach ($imported_list as $type => $list) { |
| 84 |
if (empty($list) || !is_array($list)) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
// Loop through each item ID and delete it |
| 88 |
foreach ($list as $key => $item_id) { |
| 89 |
switch ($type) { |
| 90 |
case 'posts': |
| 91 |
// making sure default kit don't get deleted. |
| 92 |
if($option_active && isset($options[$option_active]) && $options[$option_active] == $item_id){ |
| 93 |
break; |
| 94 |
} |
| 95 |
wp_delete_post($item_id, true); // Set true for permanent deletion |
| 96 |
break; |
| 97 |
case 'attachment': |
| 98 |
wp_delete_attachment($item_id, true); // Set true for permanent deletion |
| 99 |
break; |
| 100 |
case 'term': |
| 101 |
list($term_id, $taxonomy) = $item_id; |
| 102 |
wp_delete_term($term_id, $taxonomy); // Use corresponding taxonomy |
| 103 |
break; |
| 104 |
case 'taxonomy': |
| 105 |
// Taxonomies cannot be directly deleted. Consider de-registering it. |
| 106 |
break; |
| 107 |
case 'fluentform': |
| 108 |
if(class_exists('\FluentForm\App\Models\Form')){ |
| 109 |
\FluentForm\App\Models\Form::remove($item_id); |
| 110 |
} |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$imported_list_deleted = true; |
| 117 |
delete_option('templately_fsi_imported_list'); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if($options_deleted || $imported_list_deleted){ |
| 122 |
sleep(5); |
| 123 |
AjaxResponder::success([ 'options' => $options_deleted, 'imported_list' => $imported_list_deleted, 'site_url' => home_url(), 'redirect' => $all_post_url ]); |
| 124 |
} |
| 125 |
|
| 126 |
AjaxResponder::error(ErrorCode::SERVER_ERROR, __('Nothing was reverted.', 'templately'), [ 'options' => $options_deleted, 'imported_list' => $imported_list_deleted, 'site_url' => home_url() ]); |
| 127 |
} |
| 128 |
public static function has_revert(){ |
| 129 |
$options = Utils::get_backup_options(); |
| 130 |
$imported_list = get_option('templately_fsi_imported_list', []); |
| 131 |
if(!empty($options) || !empty($imported_list)){ |
| 132 |
return true; |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
|