'POD Profile', * 'icon' => 'dashicons-id-alt', * 'menu' => 'edit.php?post_type=cpod_product_profile', // submenu item to highlight * 'tabs' => array( 'profiles' => array( 'Product Profiles', $url ), … ), * 'current' => callable returning the active tab key, or null when the screen is not theirs, * ); * * @package CPODKit */ namespace CPODKit\Core\Admin; defined( 'ABSPATH' ) || exit; class TopBar { /** Online documentation. */ const DOCS_URL = 'https://cpodkit.com/docs/'; /** @var array|null|false Resolved section of the current screen (false = none). */ private static $current = null; public function register_hooks() { add_filter( 'cpod_admin_sections', array( $this, 'core_section' ), 1 ); add_filter( 'parent_file', array( $this, 'parent_file' ) ); add_filter( 'submenu_file', array( $this, 'submenu_file' ) ); add_action( 'in_admin_header', array( $this, 'render' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); add_filter( 'admin_body_class', array( $this, 'body_class' ) ); add_filter( 'woocommerce_display_admin_footer_text', array( $this, 'hide_wc_footer' ) ); add_filter( 'update_footer', array( $this, 'core_version_footer' ), 99 ); } /** WooCommerce counts some cPOD Kit screens as its own; keep its rating request off them. */ public function hide_wc_footer( $show ) { return self::current() ? false : $show; } /** The WordPress version in the footer, as on the other cPOD Kit screens, not WooCommerce's. */ public function core_version_footer( $text ) { return self::current() && function_exists( 'core_update_footer' ) ? core_update_footer() : $text; } /** Grey page background and the card look on every cPOD Kit screen. */ public function body_class( $classes ) { return self::current() ? $classes . ' cpod-admin' : $classes; } public static function current_page() { return isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended } /** Post type of the current admin screen, '' when none. */ public static function current_post_type() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && $screen->post_type ? $screen->post_type : ''; } public function core_section( $sections ) { $sections['core'] = array( 'label' => '', 'icon' => 'dashicons-screenoptions', 'menu' => null, 'tabs' => array( Menu::SLUG => array( __( 'Dashboard', 'cpod-kit' ), Menu::url() ), ModulesPage::PAGE => array( __( 'Modules', 'cpod-kit' ), Menu::url( ModulesPage::PAGE ) ), SettingsPage::PAGE => array( __( 'Settings', 'cpod-kit' ), Menu::url( SettingsPage::PAGE ) ), StatusPage::PAGE => array( __( 'System Status', 'cpod-kit' ), Menu::url( StatusPage::PAGE ) ), ), 'current' => static function () { $page = TopBar::current_page(); return in_array( $page, array( Menu::SLUG, ModulesPage::PAGE, SettingsPage::PAGE, StatusPage::PAGE ), true ) ? $page : null; }, ); return $sections; } /** * Section and active tab of the current screen. * * @return array|false array( section, tab key ) or false. */ public static function current() { if ( null !== self::$current ) { return self::$current; } if ( ! function_exists( 'get_current_screen' ) || ! get_current_screen() ) { return false; // Too early; try again later. } self::$current = false; foreach ( (array) apply_filters( 'cpod_admin_sections', array() ) as $id => $section ) { if ( empty( $section['current'] ) || ! is_callable( $section['current'] ) ) { continue; } $tab = call_user_func( $section['current'] ); if ( null !== $tab ) { $section['id'] = $id; self::$current = array( $section, $tab ); break; } } return self::$current; } public function parent_file( $parent_file ) { $current = self::current(); return $current ? Menu::SLUG : $parent_file; } public function submenu_file( $submenu_file ) { $current = self::current(); if ( $current && ModulesPage::PAGE === $current[1] ) { return Menu::SLUG; // Modules has no menu item of its own. } if ( $current && ! empty( $current[0]['menu'] ) ) { return $current[0]['menu']; } return $submenu_file; } public function enqueue() { if ( ! self::current() ) { return; } wp_enqueue_style( 'cpod-admin', CPOD_URL . 'core/assets/admin/admin.css', array(), CPOD_VERSION ); } public function render() { $current = self::current(); if ( ! $current ) { return; } list( $section, $active ) = $current; ?>
' . '