| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Resource: list base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Base class for live-state resources that expose an existing resource-list |
| 11 |
* ability's data (Forms, Tags, Landing Pages, Products) as an MCP Resource, |
| 12 |
* so clients can attach the current id/name mappings as read-only context. |
| 13 |
* |
| 14 |
* The data is single-sourced from the equivalent `kit/*-list` tool, so the |
| 15 |
* resource and tool never drift apart. |
| 16 |
* |
| 17 |
* @package ConvertKit |
| 18 |
* @author ConvertKit |
| 19 |
*/ |
| 20 |
abstract class ConvertKit_MCP_Resource_List extends ConvertKit_MCP_Resource { |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns the class name of the ConvertKit_MCP_Ability_Resource_* |
| 24 |
* ability backing this resource. |
| 25 |
* |
| 26 |
* @since 3.5.0 |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
abstract protected function get_ability_class(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the resource content's MIME type. |
| 34 |
* |
| 35 |
* @since 3.5.0 |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function get_mime_type() { |
| 40 |
|
| 41 |
return 'application/json'; |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Permission callback. |
| 47 |
* |
| 48 |
* Matches the backing list tool's `edit_posts` gate, so the same users who |
| 49 |
* can look up a Form / Tag when placing a Kit element can read this resource. |
| 50 |
* |
| 51 |
* @since 3.5.0 |
| 52 |
* |
| 53 |
* @param array $input Resource input (unused). |
| 54 |
* @return bool|WP_Error |
| 55 |
*/ |
| 56 |
public function permission_callback( $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 57 |
|
| 58 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 59 |
return new WP_Error( |
| 60 |
'convertkit_mcp_resource_permission_callback', |
| 61 |
__( 'You do not have permission to list Kit resources.', 'convertkit' ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
return true; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Executes the resource: run the backing list ability and return its result |
| 71 |
* as a JSON string. |
| 72 |
* |
| 73 |
* @since 3.5.0 |
| 74 |
* |
| 75 |
* @param array $input Resource input. |
| 76 |
* @return string|WP_Error |
| 77 |
*/ |
| 78 |
public function execute_callback( $input ) { |
| 79 |
|
| 80 |
$ability_class = $this->get_ability_class(); |
| 81 |
if ( ! class_exists( $ability_class ) ) { |
| 82 |
return new WP_Error( |
| 83 |
'convertkit_mcp_resource_ability_missing', |
| 84 |
sprintf( |
| 85 |
/* translators: %s: Ability class name */ |
| 86 |
__( 'The ability class "%s" does not exist.', 'convertkit' ), |
| 87 |
$ability_class |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$ability = new $ability_class(); |
| 93 |
$result = $ability->execute_callback( $input ); |
| 94 |
if ( is_wp_error( $result ) ) { |
| 95 |
return $result; |
| 96 |
} |
| 97 |
|
| 98 |
return (string) wp_json_encode( $result ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|