items = $items; $this->summary = $summary; } /** * Whether the current user can see the card. * * @return bool */ public function can_view(): bool { return current_user_can( self::CAPABILITY ); } /** * The card's data: counts, one page of items, and where menus are set up. * * @param int $page Page number, from 1. * @param int $per_page Items per page. * @return array{total: int, menus: int, page: int, pages: int, items: array, setupLinks: array} */ public function data( int $page = 1, int $per_page = self::CARD_ITEMS ): array { $all = $this->items->all(); $total = count( $all ); $per_page = max( 1, $per_page ); $pages = (int) max( 1, ceil( $total / $per_page ) ); $page = min( max( 1, $page ), $pages ); $menus = array(); foreach ( $all as $item ) { $menus[ $item['source'] . '-' . $item['menu'] ] = true; } $items = array(); foreach ( array_slice( $all, ( $page - 1 ) * $per_page, $per_page ) as $item ) { $items[] = array( 'key' => $item['key'], 'label' => '' !== $item['label'] ? $item['label'] : __( '(no label)', 'yangra-conditional' ), 'menuName' => '' !== $item['menu_name'] ? $item['menu_name'] : __( '(no title)', 'yangra-conditional' ), 'menuType' => $item['menu_type'], 'editUrl' => $item['edit_url'], 'summary' => $this->summary->summarize( $item['rule'] ), ); } return array( 'total' => $total, 'menus' => count( $menus ), 'page' => $page, 'pages' => $pages, 'items' => $items, 'setupLinks' => $this->setup_links(), ); } /** * Where the active theme's menus are edited, for the empty card's tip. * * @return array */ private function setup_links(): array { $links = array(); if ( current_theme_supports( 'menus' ) ) { $links[] = array( 'label' => __( 'Appearance → Menus', 'yangra-conditional' ), 'url' => admin_url( 'nav-menus.php' ), ); } if ( wp_is_block_theme() ) { // WordPress 6.8+ redirects this older path to its current Navigation screen. $links[] = array( 'label' => __( 'Navigation in the Site Editor', 'yangra-conditional' ), 'url' => admin_url( 'site-editor.php?path=/navigation' ), ); } return $links; } }