0) { return $id; } $admins = get_users(array('role' => 'administrator', 'number' => 1, 'fields' => 'ID')); return ! empty($admins) ? (int) $admins[0] : 0; } /** * GET /posts */ public static function list_posts(WP_REST_Request $request) { $type = self::post_type($request->get_param('type')); $status = $request->get_param('status'); $status = $status && 'any' !== $status ? sanitize_text_field($status) : 'any'; $query = new WP_Query(array( 'post_type' => $type, 'post_status' => $status, 's' => (string) $request->get_param('search'), 'posts_per_page' => min(max((int) $request->get_param('per_page'), 1), 100), 'paged' => max((int) $request->get_param('page'), 1), 'orderby' => 'modified', 'order' => 'DESC', )); $posts = array(); foreach ($query->posts as $post) { $posts[] = self::summary($post); } return new WP_REST_Response(array( 'count' => (int) $query->found_posts, 'posts' => $posts, ), 200); } /** * GET /posts/{id} */ public static function get_post(WP_REST_Request $request) { $post = get_post((int) $request['id']); if (! $post) { return new WP_REST_Response(array('message' => 'Contingut no trobat.'), 404); } return new WP_REST_Response(self::detail($post), 200); } /** * POST /posts — crea un esborrany (status sempre draft). */ public static function create_post(WP_REST_Request $request) { $p = $request->get_json_params(); if (empty($p['title']) && empty($p['content'])) { return new WP_REST_Response(array('message' => 'Cal com a mínim title o content.'), 422); } $postarr = array( 'post_type' => self::post_type($p['type'] ?? 'posts'), 'post_status' => 'draft', 'post_title' => isset($p['title']) ? wp_kses_post($p['title']) : '', 'post_content' => isset($p['content']) ? wp_kses_post($p['content']) : '', 'post_excerpt' => isset($p['excerpt']) ? wp_kses_post($p['excerpt']) : '', 'post_author' => self::author_id(), ); if (! empty($p['slug'])) { $postarr['post_name'] = sanitize_title($p['slug']); } $id = wp_insert_post($postarr, true); if (is_wp_error($id)) { return new WP_REST_Response(array('message' => $id->get_error_message()), 400); } self::apply_terms($id, $p); if (! empty($p['seo']) && is_array($p['seo'])) { Abency_Seo::write($id, $p['seo']); } return new WP_REST_Response(self::detail(get_post($id)), 201); } /** * POST /posts/{id} — actualitza un ESBORRANY. Rebutja si està publicat. */ public static function update_post(WP_REST_Request $request) { $post = get_post((int) $request['id']); if (! $post) { return new WP_REST_Response(array('message' => 'Contingut no trobat.'), 404); } if ('publish' === $post->post_status) { return new WP_REST_Response(array( 'message' => 'Aquest contingut està publicat: no s\'edita en viu. Usa el flux de propostes.', ), 409); } $p = $request->get_json_params(); $postarr = array('ID' => $post->ID); if (isset($p['title'])) { $postarr['post_title'] = wp_kses_post($p['title']); } if (isset($p['content'])) { $postarr['post_content'] = wp_kses_post($p['content']); } if (isset($p['excerpt'])) { $postarr['post_excerpt'] = wp_kses_post($p['excerpt']); } if (! empty($p['slug'])) { $postarr['post_name'] = sanitize_title($p['slug']); } $result = wp_update_post($postarr, true); if (is_wp_error($result)) { return new WP_REST_Response(array('message' => $result->get_error_message()), 400); } self::apply_terms($post->ID, $p); if (! empty($p['seo']) && is_array($p['seo'])) { Abency_Seo::write($post->ID, $p['seo']); } return new WP_REST_Response(self::detail(get_post($post->ID)), 200); } /** * GET /terms — categories o etiquetes. */ public static function list_terms(WP_REST_Request $request) { $taxonomy = 'tags' === $request->get_param('taxonomy') ? 'post_tag' : 'category'; $terms = get_terms(array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'search' => (string) $request->get_param('search'), 'number' => 100, )); $out = array(); if (! is_wp_error($terms)) { foreach ($terms as $t) { $out[] = array( 'id' => (int) $t->term_id, 'name' => $t->name, 'slug' => $t->slug, 'count' => (int) $t->count, ); } } return new WP_REST_Response(array('terms' => $out), 200); } // ── Helpers ─────────────────────────────────────────────────────────────── private static function apply_terms($post_id, array $p) { if (! empty($p['categories']) && is_array($p['categories'])) { wp_set_post_terms($post_id, array_map('intval', $p['categories']), 'category'); } if (! empty($p['tags']) && is_array($p['tags'])) { wp_set_post_terms($post_id, array_map('intval', $p['tags']), 'post_tag'); } } private static function summary($post) { return array( 'id' => (int) $post->ID, 'title' => get_the_title($post), 'status' => $post->post_status, 'type' => ('page' === $post->post_type) ? 'pages' : 'posts', 'slug' => $post->post_name, 'link' => get_permalink($post), 'modified' => $post->post_modified_gmt, 'date' => $post->post_date_gmt, ); } private static function detail($post) { $data = self::summary($post); $data['content_raw'] = $post->post_content; $data['content_html'] = apply_filters('the_content', $post->post_content); $data['excerpt'] = $post->post_excerpt; $data['categories'] = wp_get_post_categories($post->ID); $data['tags'] = wp_get_post_tags($post->ID, array('fields' => 'ids')); $data['seo'] = Abency_Seo::read($post->ID); return $data; } }