File: //home/vitanhod/www/wp-content/plugins/system-control/api/endpoints/class-sc-posts-endpoint.php
<?php
class SC_Posts_Endpoint {
public function register() {
register_rest_route(SC_REST_NAMESPACE, '/posts', [
[
'methods' => 'GET',
'callback' => [$this, 'list_posts'],
'permission_callback' => ['SC_Api_Auth', 'verify'],
],
[
'methods' => 'POST',
'callback' => [$this, 'create_post'],
'permission_callback' => ['SC_Api_Auth', 'verify'],
],
]);
register_rest_route(SC_REST_NAMESPACE, '/posts/(?P<id>\d+)', [
[
'methods' => 'PUT',
'callback' => [$this, 'update_post'],
'permission_callback' => ['SC_Api_Auth', 'verify'],
],
[
'methods' => 'DELETE',
'callback' => [$this, 'delete_post'],
'permission_callback' => ['SC_Api_Auth', 'verify'],
],
]);
}
public function list_posts($request) {
$per_page = (int)($request->get_param('per_page') ?: 20);
$page = (int)($request->get_param('page') ?: 1);
$status = $request->get_param('status') ?: 'any';
$args = [
'post_type' => 'post',
'post_status' => $status,
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'date',
'order' => 'DESC',
];
$query = new WP_Query($args);
$posts = [];
foreach ($query->posts as $post) {
$posts[] = [
'id' => $post->ID,
'title' => $post->post_title,
'slug' => $post->post_name,
'status' => $post->post_status,
'date' => $post->post_date,
'content' => $post->post_content,
'excerpt' => $post->post_excerpt,
'author' => $post->post_author,
];
}
return rest_ensure_response([
'posts' => $posts,
'total' => $query->found_posts,
'pages' => $query->max_num_pages,
]);
}
public function create_post($request) {
$params = $request->get_json_params();
$post_id = wp_insert_post([
'post_title' => sanitize_text_field($params['title'] ?? ''),
'post_content' => $params['content'] ?? '',
'post_status' => sanitize_text_field($params['status'] ?? 'draft'),
'post_excerpt' => $params['excerpt'] ?? '',
'post_type' => 'post',
], true);
if (is_wp_error($post_id)) return $post_id;
// Handle categories
if (!empty($params['categories'])) {
wp_set_post_categories($post_id, array_map('intval', $params['categories']));
}
return rest_ensure_response(['success' => true, 'id' => $post_id]);
}
public function update_post($request) {
$id = (int)$request['id'];
$params = $request->get_json_params();
$data = ['ID' => $id];
if (isset($params['title'])) $data['post_title'] = sanitize_text_field($params['title']);
if (isset($params['content'])) $data['post_content'] = $params['content'];
if (isset($params['status'])) $data['post_status'] = sanitize_text_field($params['status']);
if (isset($params['excerpt'])) $data['post_excerpt'] = $params['excerpt'];
$result = wp_update_post($data, true);
if (is_wp_error($result)) return $result;
if (!empty($params['categories'])) {
wp_set_post_categories($id, array_map('intval', $params['categories']));
}
return rest_ensure_response(['success' => true, 'id' => $id]);
}
public function delete_post($request) {
$id = (int)$request['id'];
$force = (bool)($request->get_param('force') ?? false);
$result = wp_delete_post($id, $force);
return rest_ensure_response(['success' => (bool)$result]);
}
}