PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / full-site-import / Abilities / RevertFullSiteImportAbility.php

RevertFullSiteImportAbility.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/full-site-import/Abilities/RevertFullSiteImportAbility.php

88 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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