| 1 |
<?php |
| 2 |
/** |
| 3 |
* `templately/auth-logout` — disconnect this site's Templately account for |
| 4 |
* the acting user (spec 041-mcp-abilities, Auth Tools addendum). |
| 5 |
* |
| 6 |
* Deliberately does NOT proxy `Templately\API\Login::logout()` via |
| 7 |
* RestDispatcher: that route's own `permission_check()` requires an |
| 8 |
* already-valid api_key resolved through `Options`' stale singleton user-id |
| 9 |
* cache (see MCP/CLAUDE.md) — sign-out must not depend on the same broken |
| 10 |
* read path it exists to undo, or it fails with "session expired" even when |
| 11 |
* a connection genuinely exists. `Options::remove()` has the same problem |
| 12 |
* one level deeper: it doesn't accept an explicit user id at all, so this |
| 13 |
* ability deletes the `_templately_*` user meta directly (mirroring |
| 14 |
* `Options::delete_user_meta()`'s own multisite/single-site branch) against |
| 15 |
* the acting user id resolved fresh at execute time. |
| 16 |
* |
| 17 |
* @package Templately\Modules\McpAbilities\Abilities |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace Templately\Modules\McpAbilities\Abilities; |
| 21 |
|
| 22 |
use Templately\Modules\McpCore\Registry\ToolDescriptor; |
| 23 |
use Templately\Modules\McpCore\Support\Permissions; |
| 24 |
use Templately\Utils\Http; |
| 25 |
use Templately\Utils\Options; |
| 26 |
|
| 27 |
class AuthLogoutAbility { |
| 28 |
|
| 29 |
const ID = 'templately/auth-logout'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Mirrors the meta keys `Templately\API\Login::delete()` clears, prefixed |
| 33 |
* the same way `Options::set()`/`get()` auto-prefix every key. |
| 34 |
* |
| 35 |
* @var string[] |
| 36 |
*/ |
| 37 |
const META_KEYS = [ |
| 38 |
'_templately_user', |
| 39 |
'_templately_favourites', |
| 40 |
'_templately_reviews', |
| 41 |
'_templately_cloud_activity', |
| 42 |
'_templately_api_key', |
| 43 |
]; |
| 44 |
|
| 45 |
public static function descriptor(): array { |
| 46 |
return [ |
| 47 |
'id' => self::ID, |
| 48 |
'label' => __( 'Disconnect Templately Account', 'templately' ), |
| 49 |
'description' => __( 'Sign this site out of its connected Templately account.', 'templately' ), |
| 50 |
'input_schema' => [ |
| 51 |
'type' => 'object', |
| 52 |
'properties' => (object) [], |
| 53 |
'additionalProperties' => false, |
| 54 |
], |
| 55 |
'output_schema' => [ |
| 56 |
'type' => 'object', |
| 57 |
], |
| 58 |
'execute_callback' => [ self::class, 'execute' ], |
| 59 |
'permission_callback' => [ Permissions::class, 'can_use_abilities' ], |
| 60 |
'access_level' => ToolDescriptor::ACCESS_FULL, |
| 61 |
'annotations' => [ 'readonly' => false, 'destructive' => true, 'idempotent' => true ], |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param array $input |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public static function execute( array $input ): array { |
| 70 |
$user_id = get_current_user_id(); |
| 71 |
$api_key = Options::get_instance()->get( 'api_key', false, $user_id ); |
| 72 |
|
| 73 |
$cloud_disconnected = null; |
| 74 |
|
| 75 |
if ( ! empty( $api_key ) ) { |
| 76 |
$response = Http::get_instance()->mutation( |
| 77 |
'disconnect', |
| 78 |
'status, message', |
| 79 |
[ 'api_key' => $api_key, 'site_url' => home_url( '/' ) ] |
| 80 |
)->post(); |
| 81 |
|
| 82 |
$cloud_disconnected = ! is_wp_error( $response ) && ( $response['status'] ?? '' ) === 'success'; |
| 83 |
} |
| 84 |
|
| 85 |
self::clear_local_meta( $user_id ); |
| 86 |
|
| 87 |
return [ |
| 88 |
'status' => 'success', |
| 89 |
'cloud_disconnected' => $cloud_disconnected, |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param int $user_id |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
private static function clear_local_meta( int $user_id ): void { |
| 98 |
$is_global = apply_filters( 'templately_multisite_is_global', false ); |
| 99 |
|
| 100 |
foreach ( self::META_KEYS as $key ) { |
| 101 |
if ( is_multisite() ) { |
| 102 |
delete_user_option( $user_id, $key, $is_global ); |
| 103 |
} else { |
| 104 |
delete_user_meta( $user_id, $key ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|