diff --git a/src/Bootstrap.php b/src/Bootstrap.php index ab659da5..6437ec4c 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -12,6 +12,7 @@ use PressbooksMultiInstitution\Services\PermissionsManager; use PressbooksMultiInstitution\Views\BookList; use PressbooksMultiInstitution\Views\UserList; +use PressbooksMultiInstitution\Views\WpBookList; /** * Class Bootstrap @@ -39,6 +40,7 @@ public function setUp(): void $this->loadTranslations(); Container::getInstance()->singleton(BookList::class, fn () => new BookList(app('db'))); + Container::getInstance()->singleton(WpBookList::class, fn () => new WpBookList); Container::getInstance()->singleton(UserList::class, fn () => new UserList(app('db'))); } diff --git a/src/Services/PermissionsManager.php b/src/Services/PermissionsManager.php index 2b429e1a..f18d3e10 100644 --- a/src/Services/PermissionsManager.php +++ b/src/Services/PermissionsManager.php @@ -9,6 +9,8 @@ use PressbooksMultiInstitution\Views\BookList; use PressbooksMultiInstitution\Views\UserList; +use PressbooksMultiInstitution\Views\WpBookList; + use function Pressbooks\Admin\NetworkManagers\_restricted_users; use function PressbooksMultiInstitution\Support\get_allowed_book_pages; use function PressbooksMultiInstitution\Support\get_allowed_pages; @@ -38,6 +40,7 @@ public function setupFilters(): void Container::get(TableViews::class)->init(); Container::get(BookList::class)->init(); + Container::get(WpBookList::class)->init(); Container::get(UserList::class)->init(); do_action('pb_institutional_filters_created', $institution, $institutionalManagers, $institutionalUsers); @@ -187,10 +190,8 @@ private function currentUserHasAccess(string $currentPageParam, array $allowedBo $institutionalUsers = apply_filters('pb_institutional_users', []); - if ($currentPageParam === 'pb_network_analytics_userlist' || $pagenow === 'users.php' || $pagenow === 'user-edit.php') { - if (isset($_REQUEST['user_id']) && in_array($_REQUEST['user_id'], $institutionalUsers)) { - $isAccessAllowed = true; - } + if ($currentPageParam === 'pb_network_analytics_userlist' || $pagenow === 'users.php' || $pagenow === 'user-edit.php' || $pagenow === 'sites.php') { + $isAccessAllowed = true; if (isset($_REQUEST['id']) && !in_array($_REQUEST['id'], $institutionalUsers)) { $isAccessAllowed = false; diff --git a/src/Views/WpBookList.php b/src/Views/WpBookList.php new file mode 100644 index 00000000..e3d39fc5 --- /dev/null +++ b/src/Views/WpBookList.php @@ -0,0 +1,98 @@ + __('Institution', 'pressbooks-multi-institution'), + ...$columns, + ]; + }); + + // Handle new column rendering (look for a more performant way?) + add_action('manage_sites_custom_column', function (string $columnId, int $id): void { + if ($columnId !== 'institution') { + return; + } + + if (is_main_site($id)) { + return; + } + + /** @var Institution|null $institution */ + $institution = Institution::query() + ->whereHas('books', fn ($query) => $query->where('blog_id', $id)) + ->first(); + + echo $institution->name ?? __('Unassigned', 'pressbooks-multi-institution'); + }, 10, 2); + + // Instruct WP to allow the new column to be sortable + add_filter('manage_sites-network_sortable_columns', function (array $columns): array { + return [ + ...$columns, + 'institution' => 'institution', + ]; + }); + + // Modify WP_Site_Query clauses + add_filter('sites_clauses', function (array $clauses): array { + if (get_current_screen()?->id !== 'sites-network') { + return $clauses; + } + + if (! is_main_site()) { + return $clauses; + } + + if (! is_super_admin()) { + return $clauses; + } + + // TODO: find a better way other than clearing the cache + wp_cache_flush_group('site-queries'); + + global $wpdb; + + $clauses['join'] .= " LEFT JOIN {$wpdb->base_prefix}institutions_blogs ON {$wpdb->blogs}.blog_id = {$wpdb->base_prefix}institutions_blogs.blog_id"; + $clauses['join'] .= " LEFT JOIN {$wpdb->base_prefix}institutions ON {$wpdb->base_prefix}institutions_blogs.institution_id = {$wpdb->base_prefix}institutions.id"; + + $clauses['fields'] .= ", {$wpdb->base_prefix}institutions.name AS institution"; + + $institutionId = get_institution_by_manager(); + + if ($institutionId > 0) { + $clauses['where'] .= $wpdb->prepare(" AND {$wpdb->base_prefix}institutions.id = %d", $institutionId); + } + + $orderBy = $_REQUEST['orderby'] ?? null; + $order = $_REQUEST['order'] ?? 'asc'; + + if ($orderBy === 'institution') { + $clauses['orderby'] = $order === 'asc' ? 'i.name ASC' : 'i.name DESC'; + } + + return $clauses; + }); + + // add_filter('sites_pre_query', function (array|null $site_data, WP_Site_Query $query): array|null { + // dump($site_data); + // + // dump($query); + // + // return $site_data; + // }, 10, 2); + } +}