| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Abilities; |
| 5 |
|
| 6 |
/** |
| 7 |
* MCP ability: returns the current Imagify configuration settings. |
| 8 |
* |
| 9 |
* Registers itself with the WP Abilities API under the slug |
| 10 |
* `imagify/get-settings` and returns all user-facing options |
| 11 |
* (stripping the internal `version` key and redacting `api_key`). |
| 12 |
* |
| 13 |
* @since 2.3.0 |
| 14 |
*/ |
| 15 |
class GetSettings extends AbstractAbility { |
| 16 |
|
| 17 |
/** |
| 18 |
* Returns the ability slug. |
| 19 |
* |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function get_id(): string { |
| 23 |
return 'imagify/get-settings'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns the ability label. |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public function get_name(): string { |
| 32 |
return 'Get Imagify settings'; |
| 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-settings', |
| 49 |
[ |
| 50 |
'label' => __( 'Get Imagify settings', 'imagify' ), |
| 51 |
'description' => __( 'Returns all Imagify configuration options and their current values.', 'imagify' ), |
| 52 |
'category' => 'imagify', |
| 53 |
'execute_callback' => [ $this, 'execute' ], |
| 54 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 55 |
'meta' => [ |
| 56 |
'show_in_rest' => true, |
| 57 |
'mcp' => [ |
| 58 |
'public' => true, |
| 59 |
], |
| 60 |
'annotations' => [ |
| 61 |
'readonly' => true, |
| 62 |
'destructive' => false, |
| 63 |
'idempotent' => true, |
| 64 |
], |
| 65 |
], |
| 66 |
] |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Routes through Imagify's capability abstraction so the `imagify_capacity` |
| 72 |
* filter and multisite network-admin logic are honoured. |
| 73 |
* |
| 74 |
* @return bool True when the current user has the Imagify `manage` capability. |
| 75 |
*/ |
| 76 |
protected function has_permission(): bool { |
| 77 |
return imagify_get_context( 'wp' )->current_user_can( 'manage' ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Fetch the raw settings array from the options layer. |
| 82 |
* |
| 83 |
* Extracted into a protected method so that unit tests can override |
| 84 |
* this call without needing to re-mock the legacy singleton. |
| 85 |
* |
| 86 |
* @return array<string, mixed> |
| 87 |
*/ |
| 88 |
protected function fetch_raw_settings(): array { |
| 89 |
return \Imagify_Options::get_instance()->get_all(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Execute the ability: return all Imagify settings. |
| 94 |
* |
| 95 |
* Strips the internal `version` key and omits `api_key` to |
| 96 |
* avoid leaking credentials over the MCP endpoint. |
| 97 |
* |
| 98 |
* @return array<string, mixed> All user-facing Imagify options. |
| 99 |
*/ |
| 100 |
public function execute(): array { |
| 101 |
$start_time = microtime( true ); |
| 102 |
$result = $this->do_execute(); |
| 103 |
|
| 104 |
$this->fire_executed( $result, $start_time ); |
| 105 |
|
| 106 |
return $result; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Internal execution logic for the ability. |
| 111 |
* |
| 112 |
* @return array<string, mixed> |
| 113 |
*/ |
| 114 |
private function do_execute(): array { |
| 115 |
$settings = $this->fetch_raw_settings(); |
| 116 |
|
| 117 |
unset( $settings['version'] ); |
| 118 |
unset( $settings['api_key'] ); |
| 119 |
|
| 120 |
return $settings; |
| 121 |
} |
| 122 |
} |
| 123 |
|