get_param('page') ?: 1)); $perPage = max(1, min(100, (int) ($request->get_param('per_page') ?: 10))); $search = sanitize_text_field((string) ($request->get_param('search') ?: '')); $authorId = max(0, (int) ($request->get_param('author_id') ?: 0)); $view = (string) ($request->get_param('status') ?: 'all'); if (!isset(PostListing::VIEWS[$view])) { $view = 'all'; } // Authors without edit_others_fspd_posts see their own posts, and a member also sees the // posts that target the sites they edit for. $viewAll = current_user_can('edit_others_fspd_posts'); $ownId = $viewAll ? 0 : get_current_user_id(); $siteIds = $ownId > 0 ? (new MemberCaps([]))->editorSitesFor($ownId) : []; if ($ownId > 0) { $authorId = empty($siteIds) ? $ownId : 0; } $listing = new PostListing(); $counts = $listing->viewCounts($ownId, $siteIds); // The author dropdown and name search only apply to users who see others' posts. $authors = $viewAll ? $listing->authorOptions() : []; $result = $listing->query([ 'page' => $page, 'per_page' => $perPage, 'search' => $search, 'author_id' => $authorId, 'view' => $view, 'own_id' => $ownId, 'site_ids' => $siteIds, 'view_all' => $viewAll, 'authors' => $authors, ]); return $this->sendSuccess([ 'posts' => $result['posts'], 'total' => $result['total'], 'total_all' => $counts['all'], 'counts' => $counts, 'authors' => $authors, 'view' => $view, 'page' => $page, 'per_page' => $perPage, 'total_pages' => $result['total_pages'], ]); } }