classification = $classification; } public function register_routes() { register_rest_route( self::NAMESPACE, '/content', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'can_list_items' ), 'args' => array( 'status' => array( 'type' => 'string', 'default' => '', 'enum' => array( '', 'unchecked', 'none', 'assisted', 'generated' ), 'sanitize_callback' => 'sanitize_key', ), 'page' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1, 'sanitize_callback' => 'absint', ), 'per_page' => array( 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => 100, 'sanitize_callback' => 'absint', ), ), ) ); register_rest_route( self::NAMESPACE, '/content/(?P\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'can_read_item' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'can_edit_item' ), 'args' => array( 'status' => array( 'type' => 'string', 'required' => true, 'enum' => AIparently_Classification_Service::STATUSES, 'sanitize_callback' => array( 'AIparently_Classification_Service', 'sanitize_status' ), ), ), ), ) ); } public function can_list_items() { return current_user_can( 'edit_posts' ); } public function can_read_item( $request ) { $post = get_post( absint( $request['id'] ) ); return $post && ( 'publish' === $post->post_status || current_user_can( 'edit_post', $post->ID ) ); } public function can_edit_item( $request ) { $post_id = absint( $request['id'] ); return $this->classification->supports( $post_id ) && current_user_can( 'edit_post', $post_id ); } private function prepare_item( $post_id ) { $post = get_post( $post_id ); return array( 'id' => $post->ID, 'type' => $post->post_type, 'title' => get_the_title( $post ), 'classification' => $this->classification->get_status( $post->ID ), 'disclosure' => $this->classification->get_decision( $post->ID ), 'modified_gmt' => get_post_modified_time( 'c', true, $post ), ); } public function get_item( $request ) { $post_id = absint( $request['id'] ); if ( ! $this->classification->supports( $post_id ) ) { return new WP_Error( 'aiparently_not_supported', __( 'This content type is not supported.', 'aiparently' ), array( 'status' => 404 ) ); } return rest_ensure_response( $this->prepare_item( $post_id ) ); } public function update_item( $request ) { $result = $this->classification->set_status( absint( $request['id'] ), $request['status'] ); return is_wp_error( $result ) ? $result : $this->get_item( $request ); } public function get_items( $request ) { $per_page = min( 100, max( 1, absint( $request['per_page'] ) ) ); $query = $this->classification->list_content( array( 'status' => $request['status'], 'paged' => max( 1, absint( $request['page'] ) ), 'posts_per_page' => $per_page, 'perm' => 'editable', ) ); $response = rest_ensure_response( array_map( array( $this, 'prepare_item' ), wp_list_pluck( $query->posts, 'ID' ) ) ); $response->header( 'X-WP-Total', (int) $query->found_posts ); $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); return $response; } }