| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Get Settings. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Ability that returns the current values of a Kit settings group. |
| 11 |
* |
| 12 |
* Produces an ability named `kit/settings-<name>-get` (e.g. `kit/settings-general-get`). |
| 13 |
* |
| 14 |
* @package ConvertKit |
| 15 |
* @author ConvertKit |
| 16 |
*/ |
| 17 |
class ConvertKit_MCP_Ability_Settings_Get extends ConvertKit_MCP_Ability_Settings { |
| 18 |
|
| 19 |
/** |
| 20 |
* Sets whether the ability is readonly. |
| 21 |
* |
| 22 |
* @since 3.4.0 |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
private $readonly = true; // @phpstan-ignore-line |
| 27 |
|
| 28 |
/** |
| 29 |
* Sets whether the ability is idempotent. |
| 30 |
* |
| 31 |
* @since 3.4.0 |
| 32 |
* |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
private $idempotent = true; // @phpstan-ignore-line |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns the operation suffix used in the ability name. |
| 39 |
* |
| 40 |
* @since 3.4.0 |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
protected function get_operation() { |
| 45 |
|
| 46 |
return 'get'; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the ability's human-readable label. |
| 52 |
* |
| 53 |
* @since 3.4.0 |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function get_label() { |
| 58 |
|
| 59 |
return sprintf( |
| 60 |
/* translators: %s: Settings Title, e.g. 'General Settings'. */ |
| 61 |
__( 'Get Kit Plugin %s', 'convertkit' ), |
| 62 |
$this->settings->get_title() |
| 63 |
); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the ability's human-readable description. |
| 69 |
* |
| 70 |
* @since 3.4.0 |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function get_description() { |
| 75 |
|
| 76 |
return sprintf( |
| 77 |
/* translators: %s: Settings Title, e.g. 'General Settings'. */ |
| 78 |
__( 'Returns the current values of the Kit Plugin "%s".', 'convertkit' ), |
| 79 |
$this->settings->get_title() |
| 80 |
); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the ability's input JSON Schema. |
| 86 |
* |
| 87 |
* Get takes no input. |
| 88 |
* |
| 89 |
* @since 3.4.0 |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_input_schema() { |
| 94 |
|
| 95 |
return array( |
| 96 |
'type' => 'object', |
| 97 |
'properties' => new stdClass(), |
| 98 |
); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the ability's output JSON Schema. |
| 104 |
* |
| 105 |
* @since 3.4.0 |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function get_output_schema() { |
| 110 |
|
| 111 |
return $this->get_public_schema(); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Executes the ability: returns the current settings, scoped to the keys |
| 117 |
* declared in the public schema. |
| 118 |
* |
| 119 |
* Stored values are sanitised through the property schema before being |
| 120 |
* returned, so the response matches the declared types (e.g. a numeric |
| 121 |
* setting stored as the string "0.5" is returned as 0.5). |
| 122 |
* |
| 123 |
* @since 3.4.0 |
| 124 |
* |
| 125 |
* @param array $input Ability input (unused). |
| 126 |
* @return array|WP_Error |
| 127 |
*/ |
| 128 |
public function execute_callback( $input ) { |
| 129 |
|
| 130 |
$values = $this->settings->get(); |
| 131 |
$schema = $this->get_public_schema(); |
| 132 |
$result = array(); |
| 133 |
|
| 134 |
if ( ! isset( $schema['properties'] ) || ! is_array( $schema['properties'] ) ) { |
| 135 |
return $result; |
| 136 |
} |
| 137 |
|
| 138 |
foreach ( $schema['properties'] as $key => $property_schema ) { |
| 139 |
if ( array_key_exists( $key, $values ) ) { |
| 140 |
$result[ $key ] = rest_sanitize_value_from_schema( $values[ $key ], $property_schema, $key ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $result; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|