| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Check WDesignKit login status and account info. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/get-login-status', [ |
| 13 |
'label' => __('Get WDesignKit Login Status', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Checks whether the user is logged in to WDesignKit cloud. Returns login status, email, token validity, expiry time, and widget credit limits. Cloud operations like pushing widgets to marketplace require login.', |
| 16 |
'wdesignkit', |
| 17 |
), |
| 18 |
'category' => 'wdesignkit', |
| 19 |
'input_schema' => [ |
| 20 |
'type' => 'object', |
| 21 |
], |
| 22 |
'output_schema' => [ |
| 23 |
'type' => 'object', |
| 24 |
'properties' => [ |
| 25 |
'success' => ['type' => 'boolean'], |
| 26 |
'logged_in' => ['type' => 'boolean'], |
| 27 |
'session_state' => ['type' => 'string'], |
| 28 |
'email' => ['type' => ['string', 'null']], |
| 29 |
'token_expiry' => ['type' => ['string', 'null']], |
| 30 |
'deactivated_widgets' => ['type' => 'integer'], |
| 31 |
'login_url' => ['type' => 'string'], |
| 32 |
'message' => ['type' => 'string'], |
| 33 |
], |
| 34 |
], |
| 35 |
'execute_callback' => 'wdesignkit_mcp_get_login_status', |
| 36 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 37 |
'meta' => [ |
| 38 |
'public' => true, |
| 39 |
'show_in_rest' => true, |
| 40 |
'mcp' => ['public' => true], |
| 41 |
'annotations' => [ |
| 42 |
'instructions' => implode("\n", [ |
| 43 |
'Checks if the user is logged in to WDesignKit cloud (wdesignkit.com).', |
| 44 |
'Login is required for cloud operations: pushing widgets to marketplace, downloading from marketplace, workspace sharing.', |
| 45 |
'Local widget CRUD (create, edit, delete, list) does NOT require login.', |
| 46 |
'session_state values:', |
| 47 |
'- "logged_in": active valid session found', |
| 48 |
'- "session_expired": a session existed but the token has expired — user must log in again', |
| 49 |
'- "not_logged_in": no session found at all', |
| 50 |
'If not logged in or session expired, direct the user to WP Admin → WDesignKit → Login.', |
| 51 |
]), |
| 52 |
'readonly' => true, |
| 53 |
'destructive' => false, |
| 54 |
'idempotent' => true, |
| 55 |
], |
| 56 |
], |
| 57 |
]); |
| 58 |
|
| 59 |
function wdesignkit_mcp_get_login_status(array $input): array { |
| 60 |
$email = null; |
| 61 |
$token_expiry = null; |
| 62 |
|
| 63 |
// Single shared lookup — see wdesignkit_mcp_find_auth_session() in |
| 64 |
// includes/abilities/class-wdk-ability-main.php. Every cloud ability resolves the |
| 65 |
// session through it, so this status can never contradict what push-widget or any |
| 66 |
// other cloud call sees. |
| 67 |
$session = wdesignkit_mcp_find_auth_session(); |
| 68 |
$logged_in = !empty($session['found']); |
| 69 |
|
| 70 |
if ($logged_in) { |
| 71 |
$session_state = 'logged_in'; |
| 72 |
$email = $session['data']['user_email'] ?? null; |
| 73 |
} else { |
| 74 |
$session_state = !empty($session['expired']) ? 'session_expired' : 'not_logged_in'; |
| 75 |
} |
| 76 |
|
| 77 |
if (!empty($session['timeout'])) { |
| 78 |
$token_expiry = wp_date('Y-m-d H:i:s', (int) $session['timeout']); |
| 79 |
} |
| 80 |
|
| 81 |
$login_url = admin_url('admin.php?page=wdesignkit'); |
| 82 |
$deactivated_count = count((array) get_option('wkit_deactivate_widgets', [])); |
| 83 |
|
| 84 |
if (!$logged_in) { |
| 85 |
$message = $session_state === 'session_expired' |
| 86 |
? 'Your WDesignKit session has expired. Go to WP Admin → WDesignKit and log in again to restore cloud access.' |
| 87 |
: 'Not logged in to WDesignKit. Go to WP Admin → WDesignKit and click Login. Login is needed for cloud features (marketplace, workspace). Local widget creation works without login.'; |
| 88 |
|
| 89 |
return [ |
| 90 |
'success' => true, |
| 91 |
'logged_in' => false, |
| 92 |
'session_state' => $session_state, |
| 93 |
'email' => $email, |
| 94 |
'token_expiry' => $token_expiry, |
| 95 |
'deactivated_widgets' => $deactivated_count, |
| 96 |
'message' => $message, |
| 97 |
'login_url' => $login_url, |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
return [ |
| 102 |
'success' => true, |
| 103 |
'logged_in' => true, |
| 104 |
'session_state' => 'logged_in', |
| 105 |
'email' => $email, |
| 106 |
'token_expiry' => $token_expiry, |
| 107 |
'deactivated_widgets' => $deactivated_count, |
| 108 |
'message' => 'Logged in to WDesignKit as ' . ($email ?? '') . ($token_expiry ? '. Session expires: ' . $token_expiry . '.' : '.') . ' Cloud features are available.', |
| 109 |
'login_url' => $login_url, |
| 110 |
]; |
| 111 |
} |
| 112 |
|