WP_REST_Server::READABLE, 'callback' => [ $this, 'get_settings' ], 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, ], [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [ $this, 'save_settings' ], 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, ] ] ); register_rest_route( 'acumenon/v1', '/search', [ [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'search_docs' ], 'permission_callback' => '__return_true', // Intentionally public endpoint ] ] ); register_rest_route( 'acumenon/v1', '/roles', [ [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_roles' ], 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, ] ] ); register_rest_route( 'acumenon/v1', '/taxonomies', [ [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_taxonomies' ], 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, ] ] ); } public function get_settings( WP_REST_Request $request ) { $settings_manager = Settings::get_instance(); return new WP_REST_Response( $settings_manager->get_all(), 200 ); } public function save_settings( WP_REST_Request $request ) { $params = $request->get_json_params(); $settings_manager = Settings::get_instance(); $settings_manager->save_all( $params ); return new WP_REST_Response( [ 'message' => __( 'Settings saved successfully', 'acumenon' ), 'data' => $settings_manager->get_all() ], 200 ); } public function search_docs( WP_REST_Request $request ) { $query_string = sanitize_text_field( $request->get_param( 'q' ) ); if ( empty( $query_string ) ) { return new WP_REST_Response( [], 200 ); } $post_type = PostType::get_instance()->post_type; $args = [ 'post_type' => $post_type, 'post_status' => 'publish', 's' => $query_string, 'posts_per_page' => 10, ]; $query = new \WP_Query( $args ); $results = []; if ( $query->have_posts() ) { foreach ( $query->posts as $post ) { // Enforce access-control: skip restricted docs if ( ! AccessControl::can_user_access_post( $post->ID ) ) { continue; } $results[] = [ 'id' => $post->ID, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), ]; } } return new WP_REST_Response( $results, 200 ); } public function get_roles( WP_REST_Request $request ) { $wp_roles = wp_roles(); $roles = []; $descriptions = [ 'administrator' => __( 'Access to all administration features.', 'acumenon' ), 'editor' => __( 'Can publish and manage posts, including others\'.', 'acumenon' ), 'author' => __( 'Can publish and manage their own posts.', 'acumenon' ), 'contributor' => __( 'Can write/manage posts but not publish.', 'acumenon' ), 'subscriber' => __( 'Can only manage their own profile.', 'acumenon' ), 'customer' => __( 'Can view orders and edit profile.', 'acumenon' ), 'shop_manager' => __( 'Can manage shop settings and products.', 'acumenon' ), ]; foreach ( $wp_roles->get_names() as $slug => $name ) { $roles[] = [ 'value' => $slug, 'label' => $name, 'description' => isset( $descriptions[ $slug ] ) ? $descriptions[ $slug ] : '', ]; } return new WP_REST_Response( $roles, 200 ); } public function get_taxonomies( WP_REST_Request $request ) { $post_type = PostType::get_instance(); $taxonomies = [ 'knowledge_base' => $post_type->knowledge_base, 'category' => $post_type->category, ]; $results = []; foreach ( $taxonomies as $key => $taxonomy ) { $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false, ] ); $results[ $key ] = []; if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $results[ $key ][] = [ 'value' => (string) $term->term_id, 'label' => $term->name, ]; } } } return new WP_REST_Response( $results, 200 ); } }