| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Settings base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Base class for abilities to read and update Plugin Settings. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
abstract class ConvertKit_MCP_Ability_Settings extends ConvertKit_MCP_Ability { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the settings class for the ability. |
| 19 |
* |
| 20 |
* @since 3.4.0 |
| 21 |
* |
| 22 |
* @var false|ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content|ConvertKit_Settings_Broadcasts|ConvertKit_Forminator_Settings|ConvertKit_Settings_MCP |
| 23 |
*/ |
| 24 |
protected $settings; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @since 3.4.0 |
| 30 |
* |
| 31 |
* @param ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content|ConvertKit_Settings_Broadcasts|ConvertKit_Forminator_Settings|ConvertKit_Settings_MCP $settings Settings class. |
| 32 |
*/ |
| 33 |
public function __construct( $settings ) { |
| 34 |
|
| 35 |
$this->settings = $settings; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the operation suffix used in the ability name (e.g. 'get', |
| 41 |
* 'update'). Combined with the settings name to produce the full |
| 42 |
* `kit/settings-<name>-<operation>` name. |
| 43 |
* |
| 44 |
* @since 3.4.0 |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
abstract protected function get_operation(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the ability name, derived from the settings name and operation |
| 52 |
* (e.g. `kit/settings-general-get`). |
| 53 |
* |
| 54 |
* @since 3.4.0 |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_name() { |
| 59 |
|
| 60 |
return 'kit/settings-' . $this->settings->get_name() . '-' . $this->get_operation(); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Permission callback for settings abilities. |
| 66 |
* |
| 67 |
* Plugin settings are restricted to users who can manage options, matching |
| 68 |
* the capability that gates the Plugin's own settings screens. |
| 69 |
* |
| 70 |
* @since 3.4.0 |
| 71 |
* |
| 72 |
* @param array $input Ability input (unused). |
| 73 |
* @return bool|WP_Error |
| 74 |
*/ |
| 75 |
public function permission_callback( $input ) { |
| 76 |
|
| 77 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 78 |
return new WP_Error( |
| 79 |
'convertkit_mcp_cannot_manage_settings', |
| 80 |
__( 'You do not have permission to read or update Kit Plugin settings.', 'convertkit' ) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
return true; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the ability's input and output JSON schemas. |
| 90 |
* |
| 91 |
* @since 3.4.0 |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
protected function get_public_schema() { |
| 96 |
|
| 97 |
$schema = $this->settings->get_schema(); |
| 98 |
$secret = $this->settings->get_secret_keys(); |
| 99 |
|
| 100 |
if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) { |
| 101 |
foreach ( $secret as $key ) { |
| 102 |
unset( $schema['properties'][ $key ] ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $schema; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|