PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.4
3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 All 197 releases
convertkit / includes / mcp / abilities / category-settings / class-convertkit-mcp-ability-category-settings-update.php

class-convertkit-mcp-ability-category-settings-update.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.4, at includes/mcp/abilities/category-settings/class-convertkit-mcp-ability-category-settings-update.php

164 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Kit MCP Ability: Update Category Settings.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Ability that updates one or more Kit settings for a Category.
11 *
12 * Produces an ability named `kit/category-settings-update`.
13 *
14 * @package ConvertKit
15 * @author ConvertKit
16 */
17 class ConvertKit_MCP_Ability_Category_Settings_Update extends ConvertKit_MCP_Ability_Category_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 Category 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, form_position) for the given Category (WordPress `category` taxonomy term). 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( 'term_id' ),
79 'properties' => array_merge(
80 array(
81 'term_id' => array(
82 'type' => 'integer',
83 'description' => __( 'The Category (term) 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 $term_id = isset( $input['term_id'] ) ? absint( $input['term_id'] ) : 0;
104
105 // Bail if the term does not exist or is not a Category.
106 $valid = $this->validate_term( $term_id );
107 if ( is_wp_error( $valid ) ) {
108 return $valid;
109 }
110
111 // Reject unknown keys.
112 $properties = $this->get_settings_schema_properties();
113 $allowed_keys = array_merge( array( 'term_id' ), array_keys( $properties ) );
114 $unknown_keys = array_diff( array_keys( $input ), $allowed_keys );
115 if ( ! empty( $unknown_keys ) ) {
116 return new WP_Error(
117 'convertkit_mcp_category_settings_unknown_keys',
118 sprintf(
119 /* translators: %s: Comma-separated list of unknown keys. */
120 __( 'The following settings keys are not recognised: %s.', 'convertkit' ),
121 implode( ', ', $unknown_keys )
122 )
123 );
124 }
125
126 // Validate each provided setting against its declared schema.
127 $validated = array();
128 foreach ( $properties as $key => $property_schema ) {
129 if ( ! array_key_exists( $key, $input ) ) {
130 continue;
131 }
132
133 $valid = rest_validate_value_from_schema( $input[ $key ], $property_schema, $key );
134
135 // Bail if the value is invalid.
136 if ( is_wp_error( $valid ) ) {
137 return $valid;
138 }
139
140 $validated[ $key ] = rest_sanitize_value_from_schema( $input[ $key ], $property_schema, $key );
141 }
142
143 // Bail if no settings were provided.
144 if ( empty( $validated ) ) {
145 return new WP_Error(
146 'convertkit_mcp_category_settings_no_input',
147 __( 'At least one setting (form or form_position) must be provided.', 'convertkit' )
148 );
149 }
150
151 // Save. ConvertKit_Term::save() merges the provided values into the
152 // term's existing settings internally, so this is a partial update.
153 $term_settings = new ConvertKit_Term( $term_id );
154 $term_settings->save( $validated );
155
156 // Return the post-save state, using the get ability so the shape
157 // exactly matches kit/category-settings-get.
158 $get_ability = new ConvertKit_MCP_Ability_Category_Settings_Get();
159 return $get_ability->execute_callback( array( 'term_id' => $term_id ) );
160
161 }
162
163 }
164