convertkit
/
includes
/
mcp
/
abilities
/
post-settings
/
class-convertkit-mcp-ability-post-settings.php
class-convertkit-mcp-ability-post-settings.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.php
| 1 | <?php |
| 2 | /** |
| 3 | * Kit MCP Ability: Post Settings base class. |
| 4 | * |
| 5 | * @package ConvertKit |
| 6 | * @author ConvertKit |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Base class for abilities that read or update the per-Post Kit settings |
| 11 | * (form, landing_page, tag, restrict_content) stored in the |
| 12 | * `_wp_convertkit_post_meta` post meta key. |
| 13 | * |
| 14 | * Each subclass represents a single verb (get / update). Produces ability |
| 15 | * names of the form `kit/post-settings-<operation>`. |
| 16 | * |
| 17 | * @package ConvertKit |
| 18 | * @author ConvertKit |
| 19 | */ |
| 20 | abstract class ConvertKit_MCP_Ability_Post_Settings extends ConvertKit_MCP_Ability { |
| 21 | |
| 22 | /** |
| 23 | * Returns the operation suffix used in the ability name (e.g. 'get', |
| 24 | * 'update'). |
| 25 | * |
| 26 | * @since 3.4.0 |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | abstract protected function get_operation(); |
| 31 | |
| 32 | /** |
| 33 | * Returns the ability name. |
| 34 | * |
| 35 | * @since 3.4.0 |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function get_name() { |
| 40 | |
| 41 | return 'kit/post-settings-' . $this->get_operation(); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Only permit an ability to be executed if the current user can edit |
| 47 | * the given post. |
| 48 | * |
| 49 | * @since 3.4.0 |
| 50 | * |
| 51 | * @param array $input Ability input. |
| 52 | * @return bool|WP_Error |
| 53 | */ |
| 54 | public function permission_callback( $input ) { |
| 55 | |
| 56 | // Get Post ID. |
| 57 | $post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 58 | |
| 59 | // Bail if no Post ID is provided. |
| 60 | if ( ! $post_id ) { |
| 61 | return new WP_Error( |
| 62 | 'convertkit_mcp_missing_post_id', |
| 63 | __( 'A post_id is required.', 'convertkit' ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | // Bail if the current user does not have permission to edit the post. |
| 68 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 69 | return new WP_Error( |
| 70 | 'convertkit_mcp_cannot_edit_post', |
| 71 | __( 'You do not have permission to edit this post.', 'convertkit' ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the JSON Schema properties that describe the four Kit post |
| 81 | * settings, shared by both the input and output schemas. |
| 82 | * |
| 83 | * Values are stored by the Plugin as strings (matching what the metabox |
| 84 | * submits), so the schemas expose them as strings with format constraints. |
| 85 | * |
| 86 | * @since 3.4.0 |
| 87 | * |
| 88 | * @return array |
| 89 | */ |
| 90 | protected function get_settings_schema_properties() { |
| 91 | |
| 92 | return array( |
| 93 | 'form' => array( |
| 94 | 'type' => 'string', |
| 95 | 'description' => __( 'Form to display for the Post. `-1` = use the Plugin Default Form for this Post Type; `0` = display no form; any other positive integer is a specific Kit Form ID.', 'convertkit' ), |
| 96 | 'pattern' => '^(-1|0|[1-9][0-9]*)$', |
| 97 | ), |
| 98 | 'landing_page' => array( |
| 99 | 'type' => 'string', |
| 100 | 'description' => __( 'Kit Landing Page ID to display instead of the Post content. `0` or empty string for none.', 'convertkit' ), |
| 101 | 'pattern' => '^([0-9]+)?$', |
| 102 | ), |
| 103 | 'tag' => array( |
| 104 | 'type' => 'string', |
| 105 | 'description' => __( 'Kit Tag ID to apply when the Post is viewed by a Kit subscriber. `0` or empty string for none.', 'convertkit' ), |
| 106 | 'pattern' => '^([0-9]+)?$', |
| 107 | ), |
| 108 | 'restrict_content' => array( |
| 109 | 'type' => 'string', |
| 110 | 'description' => __( 'Restrict Post content to Kit subscribers. Empty string or `0` for no restriction, or one of `form_<id>`, `tag_<id>`, `product_<id>` to require subscription to that resource.', 'convertkit' ), |
| 111 | 'pattern' => '^$|^0$|^(form|tag|product)_[1-9][0-9]*$', |
| 112 | ), |
| 113 | ); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the JSON Schema for the ability's output. |
| 119 | * |
| 120 | * The output shape is the same for get and update: the four settings |
| 121 | * plus the post_id, so a caller can chain update -> confirm without a |
| 122 | * follow-up get. |
| 123 | * |
| 124 | * @since 3.4.0 |
| 125 | * |
| 126 | * @return array |
| 127 | */ |
| 128 | public function get_output_schema() { |
| 129 | |
| 130 | return array( |
| 131 | 'type' => 'object', |
| 132 | 'required' => array( 'post_id', 'form', 'landing_page', 'tag', 'restrict_content' ), |
| 133 | 'properties' => array_merge( |
| 134 | array( |
| 135 | 'post_id' => array( |
| 136 | 'type' => 'integer', |
| 137 | 'description' => __( 'The Post/Page/Custom Post Type ID.', 'convertkit' ), |
| 138 | ), |
| 139 | ), |
| 140 | $this->get_settings_schema_properties() |
| 141 | ), |
| 142 | ); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | } |
| 147 |