| 1 |
<?php |
| 2 |
/** |
| 3 |
* `templately/revert-full-site-import` — roll back an import's changes (spec 042 |
| 4 |
* US4, FR-010). Loopback `templately_pack_import_revert` (RevertController), |
| 5 |
* which restores the `__templately_*` backup options and deletes the imported |
| 6 |
* content list — the same revert the browser dashboard performs. |
| 7 |
* |
| 8 |
* @package Templately\Modules\FullSiteImport\Abilities |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Templately\Modules\FullSiteImport\Abilities; |
| 12 |
|
| 13 |
use Templately\Modules\McpCore\Registry\ToolDescriptor; |
| 14 |
use Templately\Modules\McpCore\Support\AjaxLoopbackDispatcher; |
| 15 |
use Templately\Modules\FullSiteImport\Abilities\Support\FsiActiveImportGuard; |
| 16 |
use Templately\Modules\McpCore\Support\Permissions; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
class RevertFullSiteImportAbility { |
| 20 |
|
| 21 |
const ID = 'templately/revert-full-site-import'; |
| 22 |
|
| 23 |
public static function descriptor(): array { |
| 24 |
return [ |
| 25 |
'id' => self::ID, |
| 26 |
'label' => __( 'Revert Templately Full Site Import', 'templately' ), |
| 27 |
'description' => __( 'Undo a full-site import, restoring the site to its pre-import state (backed-up options restored and imported content removed).', 'templately' ), |
| 28 |
'input_schema' => [ |
| 29 |
'type' => 'object', |
| 30 |
'properties' => [ |
| 31 |
'handle' => [ 'type' => 'string' ], |
| 32 |
], |
| 33 |
'required' => [ 'handle' ], |
| 34 |
'additionalProperties' => false, |
| 35 |
], |
| 36 |
'output_schema' => [ |
| 37 |
'type' => 'object', |
| 38 |
'properties' => [ |
| 39 |
'handle' => [ 'type' => 'string' ], |
| 40 |
'reverted' => [ 'type' => 'boolean' ], |
| 41 |
'restored' => [ 'type' => 'object' ], |
| 42 |
], |
| 43 |
], |
| 44 |
'execute_callback' => [ self::class, 'execute' ], |
| 45 |
'permission_callback' => [ Permissions::class, 'can_use_abilities' ], |
| 46 |
'access_level' => ToolDescriptor::ACCESS_FULL, |
| 47 |
'annotations' => [ 'readonly' => false, 'destructive' => true, 'idempotent' => false ], |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param array $input |
| 53 |
* @return array|WP_Error |
| 54 |
*/ |
| 55 |
public static function execute( array $input ) { |
| 56 |
$handle = FullSiteImportStatusAbility::sanitize_handle( $input['handle'] ?? '' ); |
| 57 |
if ( is_wp_error( $handle ) ) { |
| 58 |
return $handle; |
| 59 |
} |
| 60 |
|
| 61 |
$response = AjaxLoopbackDispatcher::dispatch_json( 'import_revert', [ 'session_id' => $handle ] ); |
| 62 |
if ( is_wp_error( $response ) ) { |
| 63 |
return $response; |
| 64 |
} |
| 65 |
|
| 66 |
// Reverting abandons the import, so always release the one-active-import |
| 67 |
// guard — even when there was nothing to revert. Otherwise a handle from |
| 68 |
// an abnormally-terminated import could keep wedging future starts with |
| 69 |
// "already_running" until the 7-day session expiry. |
| 70 |
FsiActiveImportGuard::clear( get_current_user_id() ); |
| 71 |
|
| 72 |
// RevertController answers with an error envelope when there were no backups. |
| 73 |
if ( null !== AjaxLoopbackDispatcher::envelope_error( $response, '' ) ) { |
| 74 |
return new WP_Error( 'nothing_to_revert', __( 'There is nothing to revert for this import.', 'templately' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
$data = $response['data'] ?? []; |
| 78 |
return [ |
| 79 |
'handle' => $handle, |
| 80 |
'reverted' => true, |
| 81 |
'restored' => [ |
| 82 |
'options' => ! empty( $data['options'] ), |
| 83 |
'imported_list_cleared' => ! empty( $data['imported_list'] ), |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
} |
| 88 |
|