PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.14
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.14
2.7.0 2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 2.6.4 2.6.3 All 139 releases
metasync / wp-mcp-server / tools / class-mcp-tool-post-crud.php

class-mcp-tool-post-crud.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.6.14, at wp-mcp-server/tools/class-mcp-tool-post-crud.php

269 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MCP Tools for Post CRUD Operations
4 *
5 * Provides MCP tools for complete post CRUD operations (Create, Delete, Restore).
6 *
7 * @package MetaSync
8 * @subpackage MCP_Server/Tools
9 */
10
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 require_once plugin_dir_path(dirname(__FILE__)) . 'class-mcp-tool-base.php';
16
17 /**
18 * Create Post Tool
19 *
20 * Creates a new post or page
21 */
22 class MCP_Tool_Create_Post extends MCP_Tool_Base {
23
24 public function get_name() {
25 return 'wordpress_create_post';
26 }
27
28 public function get_description() {
29 return 'Create a new post or page';
30 }
31
32 public function get_input_schema() {
33 return [
34 'type' => 'object',
35 'properties' => [
36 'title' => [
37 'type' => 'string',
38 'description' => 'Post title',
39 ],
40 'content' => [
41 'type' => 'string',
42 'description' => 'Post content',
43 ],
44 'excerpt' => [
45 'type' => 'string',
46 'description' => 'Post excerpt (optional)',
47 ],
48 'status' => [
49 'type' => 'string',
50 'enum' => ['publish', 'draft', 'pending', 'private'],
51 'description' => 'Post status (default: draft)',
52 ],
53 'post_type' => [
54 'type' => 'string',
55 'description' => 'Post type (default: post)',
56 ],
57 'author_id' => [
58 'type' => 'integer',
59 'description' => 'Author ID (optional, defaults to current user)',
60 ],
61 'category_ids' => [
62 'type' => 'array',
63 'description' => 'Array of category IDs (optional)',
64 'items' => ['type' => 'integer'],
65 ],
66 'tags' => [
67 'type' => 'array',
68 'description' => 'Array of tag names (optional)',
69 'items' => ['type' => 'string'],
70 ],
71 'slug' => [
72 'type' => 'string',
73 'description' => 'Post slug (optional)',
74 ],
75 ],
76 'required' => ['title', 'content'],
77 ];
78 }
79
80 public function execute($params) {
81 $this->validate_params($params);
82 $this->require_capability('edit_posts');
83
84 $post_data = [
85 'post_title' => sanitize_text_field($params['title']),
86 'post_content' => wp_kses_post($params['content']),
87 'post_status' => isset($params['status']) ? sanitize_text_field($params['status']) : 'draft',
88 'post_type' => isset($params['post_type']) ? sanitize_text_field($params['post_type']) : 'post',
89 ];
90
91 if (isset($params['excerpt'])) {
92 $post_data['post_excerpt'] = sanitize_textarea_field($params['excerpt']);
93 }
94
95 if (isset($params['author_id'])) {
96 $post_data['post_author'] = intval($params['author_id']);
97 }
98
99 if (isset($params['slug'])) {
100 $post_data['post_name'] = sanitize_title($params['slug']);
101 }
102
103 // Create post
104 $post_id = wp_insert_post($post_data, true);
105
106 if (is_wp_error($post_id)) {
107 throw new Exception('Failed to create post: ' . $post_id->get_error_message());
108 }
109
110 // Set categories if provided
111 if (isset($params['category_ids']) && is_array($params['category_ids'])) {
112 wp_set_post_categories($post_id, array_map('intval', $params['category_ids']));
113 }
114
115 // Set tags if provided
116 if (isset($params['tags']) && is_array($params['tags'])) {
117 wp_set_post_tags($post_id, array_map('sanitize_text_field', $params['tags']));
118 }
119
120 $post = get_post($post_id);
121
122 return $this->success([
123 'post_id' => $post_id,
124 'title' => $post->post_title,
125 'slug' => $post->post_name,
126 'url' => get_permalink($post_id),
127 'edit_url' => get_edit_post_link($post_id, 'raw'),
128 'status' => $post->post_status,
129 'post_type' => $post->post_type,
130 'created_at' => $post->post_date,
131 'message' => 'Post created successfully',
132 ]);
133 }
134 }
135
136 /**
137 * Delete Post Tool
138 *
139 * Deletes a post (moves to trash or permanently deletes)
140 */
141 class MCP_Tool_Delete_Post extends MCP_Tool_Base {
142
143 public function get_name() {
144 return 'wordpress_delete_post';
145 }
146
147 public function get_description() {
148 return 'Delete a post (move to trash or delete permanently)';
149 }
150
151 public function get_input_schema() {
152 return [
153 'type' => 'object',
154 'properties' => [
155 'post_id' => [
156 'type' => 'integer',
157 'description' => 'Post ID to delete',
158 ],
159 'force_delete' => [
160 'type' => 'boolean',
161 'description' => 'If true, permanently delete (default: false, moves to trash)',
162 ],
163 ],
164 'required' => ['post_id'],
165 ];
166 }
167
168 public function execute($params) {
169 $this->validate_params($params);
170 $this->require_capability('delete_posts');
171
172 $post_id = intval($params['post_id']);
173 $force_delete = isset($params['force_delete']) ? (bool)$params['force_delete'] : false;
174
175 // Validate post exists
176 $post = $this->verify_post_exists($post_id);
177
178 // Check permissions
179 if (!current_user_can('delete_post', $post_id)) {
180 throw new Exception('You do not have permission to delete this post');
181 }
182
183 $title = $post->post_title;
184 $post_type = $post->post_type;
185
186 // Delete post
187 $result = wp_delete_post($post_id, $force_delete);
188
189 if (!$result) {
190 throw new Exception('Failed to delete post');
191 }
192
193 return $this->success([
194 'post_id' => $post_id,
195 'title' => $title,
196 'post_type' => $post_type,
197 'permanently_deleted' => $force_delete,
198 'message' => $force_delete ? 'Post permanently deleted' : 'Post moved to trash',
199 ]);
200 }
201 }
202
203 /**
204 * Restore Post Tool
205 *
206 * Restores a post from trash
207 */
208 class MCP_Tool_Restore_Post extends MCP_Tool_Base {
209
210 public function get_name() {
211 return 'wordpress_restore_post';
212 }
213
214 public function get_description() {
215 return 'Restore a post from trash';
216 }
217
218 public function get_input_schema() {
219 return [
220 'type' => 'object',
221 'properties' => [
222 'post_id' => [
223 'type' => 'integer',
224 'description' => 'Post ID to restore',
225 ],
226 ],
227 'required' => ['post_id'],
228 ];
229 }
230
231 public function execute($params) {
232 $this->validate_params($params);
233 $this->require_capability('delete_posts');
234
235 $post_id = intval($params['post_id']);
236
237 // Validate post exists
238 $post = $this->verify_post_exists($post_id);
239
240 // Check if post is in trash
241 if ($post->post_status !== 'trash') {
242 throw new Exception('Post is not in trash');
243 }
244
245 // Check permissions
246 if (!current_user_can('delete_post', $post_id)) {
247 throw new Exception('You do not have permission to restore this post');
248 }
249
250 // Restore post
251 $result = wp_untrash_post($post_id);
252
253 if (!$result) {
254 throw new Exception('Failed to restore post');
255 }
256
257 $restored_post = get_post($post_id);
258
259 return $this->success([
260 'post_id' => $post_id,
261 'title' => $restored_post->post_title,
262 'status' => $restored_post->post_status,
263 'url' => get_permalink($post_id),
264 'edit_url' => get_edit_post_link($post_id, 'raw'),
265 'message' => 'Post restored successfully',
266 ]);
267 }
268 }
269