PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
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 / Support / FsiActiveImportGuard.php

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

78 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * One-active-import-per-user guard (spec 042 FR-015, research.md §5).
4 *
5 * FSI is inherently single-session (`SessionData::get_session_id()` tracks one
6 * import); the browser enforces one-at-a-time via its UI. Headless MCP callers
7 * have no such UI, so this small registry makes the invariant hold: a second
8 * `start` while one import is in flight returns the existing handle instead of
9 * minting a parallel session.
10 *
11 * Stored in a NON-autoloaded per-user option so it never bloats the autoload
12 * cache (matches the 026/034 storage convention). Scoped by user id the same
13 * way `Templately\Utils\Options` scopes its rows.
14 *
15 * @package Templately\Modules\FullSiteImport\Abilities\Support
16 */
17
18 namespace Templately\Modules\FullSiteImport\Abilities\Support;
19
20 class FsiActiveImportGuard {
21
22 const OPTION_PREFIX = 'templately_mcp_fsi_active_';
23
24 /**
25 * The active handle for a user, or null when there is none.
26 *
27 * @param int $user_id
28 * @return array|null { session_id, started_at } or null.
29 */
30 public static function get_active( int $user_id ): ?array {
31 $row = get_option( self::key( $user_id ) );
32
33 if ( empty( $row ) || ! is_array( $row ) || empty( $row['session_id'] ) ) {
34 return null;
35 }
36
37 return $row;
38 }
39
40 /**
41 * Record a user's active import handle.
42 *
43 * @param int $user_id
44 * @param string $session_id
45 * @return void
46 */
47 public static function set_active( int $user_id, string $session_id ): void {
48 // add_option with autoload 'no' on first write; update_option keeps the
49 // existing autoload flag on subsequent writes.
50 $key = self::key( $user_id );
51 $row = [ 'session_id' => $session_id, 'started_at' => time() ];
52
53 if ( false === get_option( $key ) ) {
54 add_option( $key, $row, '', 'no' );
55 } else {
56 update_option( $key, $row );
57 }
58 }
59
60 /**
61 * Clear a user's active import handle (on a terminal complete/failed/revert).
62 *
63 * @param int $user_id
64 * @return void
65 */
66 public static function clear( int $user_id ): void {
67 delete_option( self::key( $user_id ) );
68 }
69
70 /**
71 * @param int $user_id
72 * @return string
73 */
74 private static function key( int $user_id ): string {
75 return self::OPTION_PREFIX . $user_id;
76 }
77 }
78