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 / mcp-abilities / Abilities / AuthLogoutAbility.php

AuthLogoutAbility.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/mcp-abilities/Abilities/AuthLogoutAbility.php

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