| 1 |
<?php |
| 2 |
/** |
| 3 |
* `templately/retry-full-site-import` — resume a recoverable (needs_retry) or |
| 4 |
* stalled (running) import (spec 042 US4, FR-010). Shares the advance path with |
| 5 |
* `FullSiteImportStatusAbility` (task T016) — the pipeline resumes from the |
| 6 |
* saved `progress`, it does not restart. |
| 7 |
* |
| 8 |
* Terminal detection: a `complete`/`failed` slice clears the one-active-import |
| 9 |
* guard, whereas `needs_retry` leaves it set. So if the handle is no longer the |
| 10 |
* user's active handle, the import already finished and there is nothing to |
| 11 |
* resume (`not_retryable`). |
| 12 |
* |
| 13 |
* @package Templately\Modules\FullSiteImport\Abilities |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace Templately\Modules\FullSiteImport\Abilities; |
| 17 |
|
| 18 |
use Templately\Modules\FullSiteImport\Utils\SessionData; |
| 19 |
use Templately\Modules\McpCore\Registry\ToolDescriptor; |
| 20 |
use Templately\Modules\FullSiteImport\Abilities\Support\FsiActiveImportGuard; |
| 21 |
use Templately\Modules\McpCore\Support\Permissions; |
| 22 |
use WP_Error; |
| 23 |
|
| 24 |
class RetryFullSiteImportAbility { |
| 25 |
|
| 26 |
const ID = 'templately/retry-full-site-import'; |
| 27 |
|
| 28 |
public static function descriptor(): array { |
| 29 |
return [ |
| 30 |
'id' => self::ID, |
| 31 |
'label' => __( 'Retry Templately Full Site Import', 'templately' ), |
| 32 |
'description' => __( 'Resume a full-site import that reported a recoverable error, continuing from where it stopped (does not restart). Keep polling full-site-import-status afterward.', 'templately' ), |
| 33 |
'input_schema' => [ |
| 34 |
'type' => 'object', |
| 35 |
'properties' => [ |
| 36 |
'handle' => [ 'type' => 'string' ], |
| 37 |
], |
| 38 |
'required' => [ 'handle' ], |
| 39 |
'additionalProperties' => false, |
| 40 |
], |
| 41 |
'output_schema' => [ |
| 42 |
'type' => 'object', |
| 43 |
'properties' => [ |
| 44 |
'handle' => [ 'type' => 'string' ], |
| 45 |
'status' => [ 'type' => 'string' ], |
| 46 |
'progress' => [ 'type' => 'integer' ], |
| 47 |
'phase' => [ 'type' => 'string' ], |
| 48 |
], |
| 49 |
], |
| 50 |
'execute_callback' => [ self::class, 'execute' ], |
| 51 |
'permission_callback' => [ Permissions::class, 'can_use_abilities' ], |
| 52 |
'access_level' => ToolDescriptor::ACCESS_FULL, |
| 53 |
'annotations' => [ 'readonly' => false, 'destructive' => false, 'idempotent' => false ], |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param array $input |
| 59 |
* @return array|WP_Error |
| 60 |
*/ |
| 61 |
public static function execute( array $input ) { |
| 62 |
$handle = FullSiteImportStatusAbility::sanitize_handle( $input['handle'] ?? '' ); |
| 63 |
if ( is_wp_error( $handle ) ) { |
| 64 |
return $handle; |
| 65 |
} |
| 66 |
|
| 67 |
$session_data = SessionData::get_data( $handle ); |
| 68 |
if ( empty( $session_data ) || ! is_array( $session_data ) ) { |
| 69 |
return new WP_Error( 'expired_handle', __( 'Unknown or expired import handle.', 'templately' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
$active = FsiActiveImportGuard::get_active( get_current_user_id() ); |
| 73 |
if ( empty( $active ) || ( $active['session_id'] ?? '' ) !== $handle ) { |
| 74 |
return new WP_Error( 'not_retryable', __( 'This import has already finished and cannot be retried.', 'templately' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
return FullSiteImportStatusAbility::advance( $handle, 0, true ); |
| 78 |
} |
| 79 |
} |
| 80 |
|