| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Content base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Base class for abilities that insert, update or remove a Kit element |
| 11 |
* (Broadcast, Form, Form Trigger, Product) within a WordPress Post's content. |
| 12 |
* |
| 13 |
* Each subclass represents a single verb (list / insert / update / delete). |
| 14 |
* |
| 15 |
* @package ConvertKit |
| 16 |
* @author ConvertKit |
| 17 |
*/ |
| 18 |
abstract class ConvertKit_MCP_Ability_Content extends ConvertKit_MCP_Ability { |
| 19 |
|
| 20 |
/** |
| 21 |
* The block this ability operates on. |
| 22 |
* |
| 23 |
* @since 3.4.0 |
| 24 |
* |
| 25 |
* @var ConvertKit_Block |
| 26 |
*/ |
| 27 |
protected $block; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @since 3.4.0 |
| 33 |
* |
| 34 |
* @param ConvertKit_Block $block The block instance this ability targets. |
| 35 |
*/ |
| 36 |
public function __construct( $block ) { |
| 37 |
|
| 38 |
$this->block = $block; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the ability name, derived from the Kit element's name and the verb |
| 44 |
* returned by get_verb(). |
| 45 |
* |
| 46 |
* For example, the Form element's insert ability is named `kit/form-insert`. |
| 47 |
* |
| 48 |
* @since 3.4.0 |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function get_name() { |
| 53 |
|
| 54 |
return 'kit/' . $this->block->get_name() . '-' . $this->get_verb(); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the verb this ability represents. |
| 60 |
* |
| 61 |
* @since 3.4.0 |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
abstract public function get_verb(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Only permit an ability to be executed if the current user can edit the given post. |
| 69 |
* |
| 70 |
* @since 3.4.0 |
| 71 |
* |
| 72 |
* @param array $input Ability input. |
| 73 |
* @return bool|WP_Error |
| 74 |
*/ |
| 75 |
public function permission_callback( $input ) { |
| 76 |
|
| 77 |
// Get Post ID. |
| 78 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 79 |
|
| 80 |
// Bail if no Post ID is provided. |
| 81 |
if ( ! $post_id ) { |
| 82 |
return new WP_Error( |
| 83 |
'convertkit_mcp_missing_post_id', |
| 84 |
__( 'A post_id is required.', 'convertkit' ) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
// Bail if the current user does not have permission to edit the post. |
| 89 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 90 |
return new WP_Error( |
| 91 |
'convertkit_mcp_cannot_edit_post', |
| 92 |
__( 'You do not have permission to edit this post.', 'convertkit' ) |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
return true; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the ability's output JSON Schema. |
| 102 |
* |
| 103 |
* @since 3.4.0 |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function get_output_schema() { |
| 108 |
|
| 109 |
return array( |
| 110 |
'type' => 'object', |
| 111 |
'required' => array( 'post_id', 'occurrence_index' ), |
| 112 |
'properties' => array( |
| 113 |
'post_id' => array( |
| 114 |
'type' => 'integer', |
| 115 |
'description' => __( 'The Post/Page/Custom Post Type ID.', 'convertkit' ), |
| 116 |
), |
| 117 |
'occurrence_index' => array( |
| 118 |
'type' => 'integer', |
| 119 |
'description' => __( 'The zero-based occurrence index of the Kit element in the post.', 'convertkit' ), |
| 120 |
), |
| 121 |
), |
| 122 |
); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns JSON Schema properties derived from the block's get_fields(), |
| 128 |
* suitable for use as the `attrs` object in an Abilities API input schema. |
| 129 |
* |
| 130 |
* Used by verb subclasses whose input schema includes an `attrs` object |
| 131 |
* (insert, update). |
| 132 |
* |
| 133 |
* @since 3.4.0 |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
protected function get_input_schema_properties() { |
| 138 |
|
| 139 |
// Define properties. |
| 140 |
$properties = array(); |
| 141 |
$fields = $this->block->get_fields(); |
| 142 |
|
| 143 |
foreach ( $fields as $field_name => $field ) { |
| 144 |
$properties[ $field_name ] = array( |
| 145 |
'description' => isset( $field['label'] ) ? (string) $field['label'] : '', |
| 146 |
'type' => $this->get_input_schema_property_type( $field ), |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return $properties; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the JSON Schema type for the given field definition. |
| 156 |
* |
| 157 |
* @since 3.4.0 |
| 158 |
* |
| 159 |
* @param array $field Field definition. |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
private function get_input_schema_property_type( $field ) { |
| 163 |
|
| 164 |
$type = isset( $field['type'] ) ? (string) $field['type'] : 'string'; |
| 165 |
|
| 166 |
switch ( $type ) { |
| 167 |
case 'resource': |
| 168 |
case 'text': |
| 169 |
case 'color': |
| 170 |
case 'select': |
| 171 |
return 'string'; |
| 172 |
|
| 173 |
case 'number': |
| 174 |
return 'integer'; |
| 175 |
|
| 176 |
case 'toggle': |
| 177 |
return 'boolean'; |
| 178 |
|
| 179 |
default: |
| 180 |
// Unknown field type — fall back to string. |
| 181 |
return 'string'; |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|