convertkit
/
includes
/
mcp
/
abilities
/
post-settings
/
class-convertkit-mcp-ability-post-settings-update.php
class-convertkit-mcp-ability-post-settings-update.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.4, at includes/mcp/abilities/post-settings/class-convertkit-mcp-ability-post-settings-update.php
| 1 | <?php |
| 2 | /** |
| 3 | * Kit MCP Ability: Update Post Settings. |
| 4 | * |
| 5 | * @package ConvertKit |
| 6 | * @author ConvertKit |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Ability that updates one or more Kit settings for a Post/Page/Custom Post Type. |
| 11 | * |
| 12 | * Produces an ability named `kit/post-settings-update`. |
| 13 | * |
| 14 | * @package ConvertKit |
| 15 | * @author ConvertKit |
| 16 | */ |
| 17 | class ConvertKit_MCP_Ability_Post_Settings_Update extends ConvertKit_MCP_Ability_Post_Settings { |
| 18 | |
| 19 | /** |
| 20 | * Sets whether the ability is idempotent. |
| 21 | * |
| 22 | * @since 3.4.0 |
| 23 | * |
| 24 | * @var bool |
| 25 | */ |
| 26 | private $idempotent = true; // @phpstan-ignore-line |
| 27 | |
| 28 | /** |
| 29 | * Returns the operation suffix used in the ability name. |
| 30 | * |
| 31 | * @since 3.4.0 |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | protected function get_operation() { |
| 36 | |
| 37 | return 'update'; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the ability's human-readable label. |
| 43 | * |
| 44 | * @since 3.4.0 |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function get_label() { |
| 49 | |
| 50 | return __( 'Update Kit Post Settings', 'convertkit' ); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the ability's human-readable description. |
| 56 | * |
| 57 | * @since 3.4.0 |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function get_description() { |
| 62 | |
| 63 | return __( 'Updates one or more Kit settings (form, landing_page, tag, restrict_content) for the given Post, Page or Custom Post Type. Only the settings provided in the input are updated; other settings are preserved.', 'convertkit' ); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the ability's input JSON Schema. |
| 69 | * |
| 70 | * @since 3.4.0 |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function get_input_schema() { |
| 75 | |
| 76 | return array( |
| 77 | 'type' => 'object', |
| 78 | 'required' => array( 'post_id' ), |
| 79 | 'properties' => array_merge( |
| 80 | array( |
| 81 | 'post_id' => array( |
| 82 | 'type' => 'integer', |
| 83 | 'description' => __( 'The Post/Page/Custom Post Type ID to update Kit settings for.', 'convertkit' ), |
| 84 | 'minimum' => 1, |
| 85 | ), |
| 86 | ), |
| 87 | $this->get_settings_schema_properties() |
| 88 | ), |
| 89 | ); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Executes the ability. |
| 95 | * |
| 96 | * @since 3.4.0 |
| 97 | * |
| 98 | * @param array $input Ability input. |
| 99 | * @return array|WP_Error |
| 100 | */ |
| 101 | public function execute_callback( $input ) { |
| 102 | |
| 103 | $post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 104 | |
| 105 | // Bail if the Post does not exist. |
| 106 | if ( ! get_post( $post_id ) ) { |
| 107 | return new WP_Error( |
| 108 | 'convertkit_mcp_post_not_found', |
| 109 | sprintf( |
| 110 | /* translators: %d: Post ID. */ |
| 111 | __( 'Post %d does not exist.', 'convertkit' ), |
| 112 | $post_id |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | // Reject unknown keys. |
| 118 | $properties = $this->get_settings_schema_properties(); |
| 119 | $allowed_keys = array_merge( array( 'post_id' ), array_keys( $properties ) ); |
| 120 | $unknown_keys = array_diff( array_keys( $input ), $allowed_keys ); |
| 121 | if ( ! empty( $unknown_keys ) ) { |
| 122 | return new WP_Error( |
| 123 | 'convertkit_mcp_post_settings_unknown_keys', |
| 124 | sprintf( |
| 125 | /* translators: %s: Comma-separated list of unknown keys. */ |
| 126 | __( 'The following settings keys are not recognised: %s.', 'convertkit' ), |
| 127 | implode( ', ', $unknown_keys ) |
| 128 | ) |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | // Validate each provided setting against its declared schema. |
| 133 | $validated = array(); |
| 134 | foreach ( $properties as $key => $property_schema ) { |
| 135 | if ( ! array_key_exists( $key, $input ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | $valid = rest_validate_value_from_schema( $input[ $key ], $property_schema, $key ); |
| 140 | |
| 141 | // Bail if the value is invalid. |
| 142 | if ( is_wp_error( $valid ) ) { |
| 143 | return $valid; |
| 144 | } |
| 145 | |
| 146 | $validated[ $key ] = rest_sanitize_value_from_schema( $input[ $key ], $property_schema, $key ); |
| 147 | } |
| 148 | |
| 149 | // Bail if no settings were provided. |
| 150 | if ( empty( $validated ) ) { |
| 151 | return new WP_Error( |
| 152 | 'convertkit_mcp_post_settings_no_input', |
| 153 | __( 'At least one setting (form, landing_page, tag or restrict_content) must be provided.', 'convertkit' ) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | // Merge into the Post's existing settings so this is a partial update. |
| 158 | $post_settings = new ConvertKit_Post( $post_id ); |
| 159 | $merged = array_merge( $post_settings->get(), $validated ); |
| 160 | |
| 161 | // Save. This fires updated_post_meta, which the Restrict Content |
| 162 | // cache class listens for; nothing extra required here. |
| 163 | $post_settings->save( $merged ); |
| 164 | |
| 165 | // Return the post-save state, using the get ability so the shape |
| 166 | // exactly matches kit/post-settings-get. |
| 167 | $get_ability = new ConvertKit_MCP_Ability_Post_Settings_Get(); |
| 168 | return $get_ability->execute_callback( array( 'post_id' => $post_id ) ); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | } |
| 173 |