| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Ability: Delete a Kit Element from a post. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Ability that removes a single occurrence of a Kit element |
| 11 |
* (Broadcast, Form, Form Trigger, Product) from 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>-delete` (e.g. `kit/form-delete`). |
| 15 |
* |
| 16 |
* @package ConvertKit |
| 17 |
* @author ConvertKit |
| 18 |
*/ |
| 19 |
class ConvertKit_MCP_Ability_Content_Delete extends ConvertKit_MCP_Ability_Content { |
| 20 |
|
| 21 |
/** |
| 22 |
* Sets whether the ability is destructive. |
| 23 |
* |
| 24 |
* @since 3.4.0 |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
private $destructive = 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 'delete'; |
| 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 |
__( 'Delete Existing %s from a Post, Page 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 |
__( 'Removes an existing %s from a Post, Page or Custom Post using the supplied zero-based occurrence index.', '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' ), |
| 89 |
'properties' => array( |
| 90 |
'post_id' => array( |
| 91 |
'type' => 'integer', |
| 92 |
'minimum' => 1, |
| 93 |
'description' => __( 'ID of the post containing the element.', 'convertkit' ), |
| 94 |
), |
| 95 |
'occurrence_index' => array( |
| 96 |
'type' => 'integer', |
| 97 |
'minimum' => 0, |
| 98 |
'description' => __( 'The zero-based occurrence index of the element to delete.', 'convertkit' ), |
| 99 |
), |
| 100 |
), |
| 101 |
); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Executes the ability. |
| 107 |
* |
| 108 |
* @since 3.4.0 |
| 109 |
* |
| 110 |
* @param array $input Ability input. |
| 111 |
* @return array|WP_Error |
| 112 |
*/ |
| 113 |
public function execute_callback( $input ) { |
| 114 |
|
| 115 |
// Get Post ID. |
| 116 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 117 |
|
| 118 |
// Bail if no Post ID is provided. |
| 119 |
if ( ! $post_id ) { |
| 120 |
return new WP_Error( |
| 121 |
'convertkit_mcp_missing_post_id', |
| 122 |
__( 'A post_id is required.', 'convertkit' ) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
// Get occurrence index. |
| 127 |
$occurrence_index = isset( $input['occurrence_index'] ) ? (int) $input['occurrence_index'] : 0; |
| 128 |
|
| 129 |
// Delete the element from the post. |
| 130 |
return ConvertKit_Content_Post_Helper::delete( $post_id, $this->block->get_name(), $occurrence_index ); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|