| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Abilities; |
| 5 |
|
| 6 |
use Imagify\User\User; |
| 7 |
|
| 8 |
/** |
| 9 |
* MCP ability: returns the current Imagify account status and quota data. |
| 10 |
* |
| 11 |
* Registers itself with the WP Abilities API under the slug |
| 12 |
* `imagify/get-account` and returns plan details, quota consumption, |
| 13 |
* and whether the configured API key is valid. |
| 14 |
* |
| 15 |
* @since 2.3.0 |
| 16 |
*/ |
| 17 |
class GetAccount extends AbstractAbility { |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the ability slug. |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function get_id(): string { |
| 25 |
return 'imagify/get-account'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the ability label. |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public function get_name(): string { |
| 34 |
return 'Get Imagify account status'; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the ability with the WP Abilities API. |
| 39 |
* |
| 40 |
* No-ops gracefully when the API is not available (WP < 6.9). |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register(): void { |
| 45 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
wp_register_ability( |
| 50 |
'imagify/get-account', |
| 51 |
[ |
| 52 |
'label' => __( 'Get Imagify account status', 'imagify' ), |
| 53 |
'description' => __( 'Returns account quota, plan details, and API key validity.', 'imagify' ), |
| 54 |
'category' => 'imagify', |
| 55 |
'output_schema' => [ |
| 56 |
'type' => 'object', |
| 57 |
'properties' => [ |
| 58 |
'plan_label' => [ 'type' => 'string' ], |
| 59 |
'quota' => [ 'type' => [ 'string', 'number' ] ], |
| 60 |
'consumed_current_month_quota' => [ 'type' => [ 'string', 'number' ] ], |
| 61 |
'extra_quota' => [ 'type' => [ 'string', 'number' ] ], |
| 62 |
'extra_quota_consumed' => [ 'type' => [ 'string', 'number' ] ], |
| 63 |
'next_date_update' => [ 'type' => 'string' ], |
| 64 |
'is_api_key_valid' => [ 'type' => 'boolean' ], |
| 65 |
], |
| 66 |
], |
| 67 |
'execute_callback' => [ $this, 'execute' ], |
| 68 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 69 |
'meta' => [ |
| 70 |
'show_in_rest' => true, |
| 71 |
'mcp' => [ |
| 72 |
'public' => true, |
| 73 |
], |
| 74 |
'annotations' => [ |
| 75 |
'readonly' => true, |
| 76 |
'destructive' => false, |
| 77 |
'idempotent' => true, |
| 78 |
], |
| 79 |
], |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Routes through Imagify's capability abstraction so the `imagify_capacity` |
| 86 |
* filter and multisite network-admin logic are honoured. |
| 87 |
* |
| 88 |
* @return bool True when the current user has the Imagify `manage` capability. |
| 89 |
*/ |
| 90 |
protected function has_permission(): bool { |
| 91 |
return imagify_get_context( 'wp' )->current_user_can( 'manage' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Fetch the initialized User instance. |
| 96 |
* |
| 97 |
* Extracted into a protected method so that unit tests can override |
| 98 |
* this call without needing to bootstrap the full Imagify API layer. |
| 99 |
* |
| 100 |
* @return User |
| 101 |
*/ |
| 102 |
protected function fetch_user(): User { |
| 103 |
$user = new User(); |
| 104 |
$user->init_user(); |
| 105 |
return $user; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Execute the ability: return the current Imagify account status. |
| 110 |
* |
| 111 |
* @return array<string, mixed> Account quota, plan details, and API key validity. |
| 112 |
*/ |
| 113 |
public function execute(): array { |
| 114 |
$start_time = microtime( true ); |
| 115 |
$result = $this->do_execute(); |
| 116 |
|
| 117 |
$this->fire_executed( $result, $start_time ); |
| 118 |
|
| 119 |
return $result; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Internal execution logic for the ability. |
| 124 |
* |
| 125 |
* @return array<string, mixed> |
| 126 |
*/ |
| 127 |
private function do_execute(): array { |
| 128 |
$user = $this->fetch_user(); |
| 129 |
$is_valid = ! is_wp_error( $user->get_error() ); |
| 130 |
|
| 131 |
if ( ! $is_valid ) { |
| 132 |
return [ |
| 133 |
'plan_label' => '', |
| 134 |
'quota' => 0, |
| 135 |
'consumed_current_month_quota' => 0, |
| 136 |
'extra_quota' => 0, |
| 137 |
'extra_quota_consumed' => 0, |
| 138 |
'next_date_update' => '', |
| 139 |
'is_api_key_valid' => false, |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
return [ |
| 144 |
'plan_label' => $user->plan_label, |
| 145 |
'quota' => $user->quota, |
| 146 |
'consumed_current_month_quota' => $user->consumed_current_month_quota, |
| 147 |
'extra_quota' => $user->extra_quota, |
| 148 |
'extra_quota_consumed' => $user->extra_quota_consumed, |
| 149 |
'next_date_update' => $user->next_date_update, |
| 150 |
'is_api_key_valid' => true, |
| 151 |
]; |
| 152 |
} |
| 153 |
} |
| 154 |
|