| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Update a Kit Element in a post. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Ability that updates 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>-update` (e.g. `kit/form-update`). |
| 15 |
* |
| 16 |
* @package ConvertKit |
| 17 |
* @author ConvertKit |
| 18 |
*/ |
| 19 |
class ConvertKit_MCP_Ability_Content_Update extends ConvertKit_MCP_Ability_Content { |
| 20 |
|
| 21 |
/** |
| 22 |
* Sets whether the ability is idempotent. |
| 23 |
* |
| 24 |
* @since 3.4.0 |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
private $idempotent = true; // @phpstan-ignore-line |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns the verb this ability represents. |
| 32 |
* |
| 33 |
* @since 3.4.0 |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function get_verb() { |
| 38 |
|
| 39 |
return 'update'; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the ability's human-readable label. |
| 45 |
* |
| 46 |
* @since 3.4.0 |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function get_label() { |
| 51 |
|
| 52 |
return sprintf( |
| 53 |
/* translators: %s: block title */ |
| 54 |
__( 'Update Existing %s in a Page, Post or Custom Post', 'convertkit' ), |
| 55 |
$this->block->get_title() |
| 56 |
); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the ability's human-readable description. |
| 62 |
* |
| 63 |
* @since 3.4.0 |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function get_description() { |
| 68 |
|
| 69 |
return sprintf( |
| 70 |
/* translators: Block Name */ |
| 71 |
__( 'Updates the attributes of an existing %s in a Page, Post or Custom Post. The provided attributes are merged into the existing attributes.', 'convertkit' ), |
| 72 |
$this->block->get_title_plural() |
| 73 |
); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the ability's input JSON Schema. |
| 79 |
* |
| 80 |
* @since 3.4.0 |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public function get_input_schema() { |
| 85 |
|
| 86 |
return array( |
| 87 |
'type' => 'object', |
| 88 |
'required' => array( 'post_id', 'occurrence_index', 'attrs' ), |
| 89 |
'properties' => array( |
| 90 |
'post_id' => array( |
| 91 |
'type' => 'integer', |
| 92 |
'minimum' => 1, |
| 93 |
'description' => __( 'Page / Post / Custom Post Type ID containing the existing element.', 'convertkit' ), |
| 94 |
), |
| 95 |
'occurrence_index' => array( |
| 96 |
'type' => 'integer', |
| 97 |
'minimum' => 0, |
| 98 |
'description' => __( 'The zero-based occurrence index of the element to update.', 'convertkit' ), |
| 99 |
), |
| 100 |
'attrs' => array( |
| 101 |
'type' => 'object', |
| 102 |
'description' => __( 'Element attributes to update. Any attributes not provided will be left unchanged.', 'convertkit' ), |
| 103 |
'properties' => $this->get_input_schema_properties(), |
| 104 |
), |
| 105 |
), |
| 106 |
); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Executes the ability. |
| 112 |
* |
| 113 |
* @since 3.4.0 |
| 114 |
* |
| 115 |
* @param array $input Ability input. |
| 116 |
* @return array|WP_Error |
| 117 |
*/ |
| 118 |
public function execute_callback( $input ) { |
| 119 |
|
| 120 |
// Get Post ID. |
| 121 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 122 |
|
| 123 |
// Bail if no Post ID is provided. |
| 124 |
if ( ! $post_id ) { |
| 125 |
return new WP_Error( |
| 126 |
'convertkit_mcp_missing_post_id', |
| 127 |
__( 'A post_id is required.', 'convertkit' ) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
// Get attributes and occurrence index. |
| 132 |
$attrs = isset( $input['attrs'] ) && is_array( $input['attrs'] ) ? $input['attrs'] : array(); |
| 133 |
$occurrence_index = isset( $input['occurrence_index'] ) ? (int) $input['occurrence_index'] : 0; |
| 134 |
|
| 135 |
// Update the element in the post. |
| 136 |
return ConvertKit_Content_Post_Helper::update( $post_id, $this->block->get_name(), $occurrence_index, $attrs ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|