PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Abilities / GetAccount.php

GetAccount.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at classes/Abilities/GetAccount.php

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