'object', 'additionalProperties' => false, 'properties' => array( 'post_type' => array( 'type' => 'string', 'description' => __( 'Post type to query (e.g. post, page). Defaults to post.', 'gripmcp' ), 'default' => 'post', ), 'search' => array( 'type' => 'string', 'description' => __( 'Keyword search across title and content.', 'gripmcp' ), ), 'status' => array( 'type' => 'string', 'description' => __( 'Post status filter.', 'gripmcp' ), 'enum' => array( 'publish', 'draft', 'pending', 'future', 'private', 'trash', 'any' ), 'default' => 'any', ), 'author_id' => array( 'type' => 'integer', 'description' => __( 'Restrict to a specific author user id.', 'gripmcp' ), 'minimum' => 1, ), 'per_page' => array( 'type' => 'integer', 'description' => __( 'Results per page (1-50).', 'gripmcp' ), 'minimum' => 1, 'maximum' => 50, 'default' => 10, ), 'page' => array( 'type' => 'integer', 'description' => __( 'Page number.', 'gripmcp' ), 'minimum' => 1, 'default' => 1, ), ), ); } protected function required_capability(): string { return 'edit_posts'; } protected function is_read_only(): bool { return true; } protected function is_idempotent(): bool { return true; } protected function run( array $arguments, Request_Context $context ): Tool_Result { $post_type = sanitize_key( $arguments['post_type'] ?? 'post' ); if ( ! post_type_exists( $post_type ) ) { return $this->fail( sprintf( /* translators: %s: post type. */ __( 'Unknown post type: %s', 'gripmcp' ), $post_type ) ); } if ( ! Post_Access::can_list_type( $post_type, $context ) ) { return $this->fail( sprintf( /* translators: %s: post type. */ __( 'Your WordPress account cannot list %s content.', 'gripmcp' ), $post_type ) ); } // Only the posts this account may see: published ones, its own, and // others' unpublished ones only with the capability WordPress asks for. $query_args = Post_Access::restrict( array( 'post_type' => $post_type, 'post_status' => $arguments['status'] ?? 'any', 'posts_per_page' => (int) ( $arguments['per_page'] ?? 10 ), 'paged' => (int) ( $arguments['page'] ?? 1 ), 'no_found_rows' => false, ), $context ); if ( ! empty( $arguments['search'] ) ) { $query_args['s'] = (string) $arguments['search']; } if ( ! empty( $arguments['author_id'] ) ) { $query_args['author'] = (int) $arguments['author_id']; } $query = new \WP_Query( $query_args ); $items = array(); foreach ( $query->posts as $post ) { // The query already applies these rules; this is the backstop. if ( ! Post_Access::can_see( $post, $context ) ) { continue; } $item = Post_Presenter::to_array( $post, false ); if ( ! Post_Access::can_read_body( $post, $context ) ) { $item['excerpt'] = ''; } $items[] = $item; } $total = (int) $query->found_posts; $page = (int) $query_args['paged']; $truncated = $page < (int) $query->max_num_pages; $data = array( 'items' => $items, 'total' => $total, 'total_pages' => (int) $query->max_num_pages, 'page' => $page, 'truncated' => $truncated, ); $summary = sprintf( /* translators: 1: number returned, 2: total. */ __( 'Found %1$d of %2$d matching items.', 'gripmcp' ), count( $items ), $total ); if ( $truncated ) { $data['note'] = __( 'More items exist on later pages; this is not the complete set. Request the next page or narrow the query.', 'gripmcp' ); } return $this->data( $data, $summary ); } }