| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Insert a Kit Element into a post. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Ability that inserts a single occurrence of a Kit element |
| 11 |
* (Broadcast, Form, Form Trigger, Product) within a WordPress Post's content. |
| 12 |
* |
| 13 |
* Registered by an element opting in via the `convertkit_abilities` filter and |
| 14 |
* produces an ability named `kit/<element>-insert` (e.g. `kit/form-insert`). |
| 15 |
* |
| 16 |
* @package ConvertKit |
| 17 |
* @author ConvertKit |
| 18 |
*/ |
| 19 |
class ConvertKit_MCP_Ability_Content_Insert extends ConvertKit_MCP_Ability_Content { |
| 20 |
|
| 21 |
/** |
| 22 |
* Returns the verb this ability represents. |
| 23 |
* |
| 24 |
* @since 3.4.0 |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_verb() { |
| 29 |
|
| 30 |
return 'insert'; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the ability's human-readable label. |
| 36 |
* |
| 37 |
* @since 3.4.0 |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function get_label() { |
| 42 |
|
| 43 |
return sprintf( |
| 44 |
/* translators: %s: block title */ |
| 45 |
__( 'Insert %s into a Page, Post or Custom Post', 'convertkit' ), |
| 46 |
$this->block->get_title() |
| 47 |
); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the ability's human-readable description. |
| 53 |
* |
| 54 |
* @since 3.4.0 |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_description() { |
| 59 |
|
| 60 |
return sprintf( |
| 61 |
/* translators: 1: block full name e.g. convertkit/form, 2: block title */ |
| 62 |
__( 'Inserts a new %s in a Page, Post or Custom Post\'s content. The element can be appended (default), prepended, or inserted relative to an existing element using a zero-based index.', 'convertkit' ), |
| 63 |
$this->block->get_title_plural() |
| 64 |
); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the ability's input JSON Schema. |
| 70 |
* |
| 71 |
* @since 3.4.0 |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function get_input_schema() { |
| 76 |
|
| 77 |
return array( |
| 78 |
'type' => 'object', |
| 79 |
'required' => array( 'post_id', 'attrs' ), |
| 80 |
'properties' => array( |
| 81 |
'post_id' => array( |
| 82 |
'type' => 'integer', |
| 83 |
'minimum' => 1, |
| 84 |
'description' => __( 'Page / Post / Custom Post Type ID to insert the element into.', 'convertkit' ), |
| 85 |
), |
| 86 |
'position' => array( |
| 87 |
'type' => 'string', |
| 88 |
'enum' => array( 'append', 'prepend', 'index' ), |
| 89 |
'default' => 'append', |
| 90 |
'description' => __( 'Where to insert the new element. "index" requires the "index" property.', 'convertkit' ), |
| 91 |
), |
| 92 |
'index' => array( |
| 93 |
'type' => 'integer', |
| 94 |
'minimum' => 0, |
| 95 |
'description' => __( 'When position is "index", the zero-based top-level element index at which to insert the new element.', 'convertkit' ), |
| 96 |
), |
| 97 |
'attrs' => array( |
| 98 |
'type' => 'object', |
| 99 |
'description' => __( 'Element attributes.', 'convertkit' ), |
| 100 |
'properties' => $this->get_input_schema_properties(), |
| 101 |
), |
| 102 |
), |
| 103 |
); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Executes the ability. |
| 109 |
* |
| 110 |
* @since 3.4.0 |
| 111 |
* |
| 112 |
* @param array $input Ability input. |
| 113 |
* @return array|WP_Error |
| 114 |
*/ |
| 115 |
public function execute_callback( $input ) { |
| 116 |
|
| 117 |
// Get Post ID. |
| 118 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 119 |
|
| 120 |
// Bail if no Post ID is provided. |
| 121 |
if ( ! $post_id ) { |
| 122 |
return new WP_Error( |
| 123 |
'convertkit_mcp_missing_post_id', |
| 124 |
__( 'A post_id is required.', 'convertkit' ) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
// Get attributes, position and index. |
| 129 |
$attrs = isset( $input['attrs'] ) && is_array( $input['attrs'] ) ? $input['attrs'] : array(); |
| 130 |
$position = isset( $input['position'] ) ? (string) $input['position'] : 'append'; |
| 131 |
$index = isset( $input['index'] ) ? (int) $input['index'] : 0; |
| 132 |
|
| 133 |
// Insert the element into the post. |
| 134 |
return ConvertKit_Content_Post_Helper::insert( $post_id, $this->block->get_name(), $attrs, $position, $index ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|