*/ private array $screens = []; /** * Init hooks. */ public function hooks(): void { add_action( 'admin_menu', [ $this, 'register_screens' ], 999 ); add_action( 'parent_file', [ $this, 'highlight_menu' ], 9999 ); add_action( 'submenu_file', [ $this, 'highlight_menu' ], 9999 ); add_action( 'admin_bar_menu', [ $this, 'add_new_entity_links' ], 100 ); add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ], 100 ); } /** * Add new entity links to the admin bar. * * @param $wp_admin_bar * * @return void */ public function add_new_entity_links( $wp_admin_bar ): void { if ( ! current_user_can( adpresso_get_capability( 'edit_ad' ) ) ) { return; } $ad_args = [ 'id' => 'new-adpresso-ad', 'title' => __( 'Ad', 'adpresso' ), 'href' => admin_url( 'admin.php?page=adpresso-list-tables&tab=ads' ), 'parent' => 'new-content', ]; $group_args = [ 'id' => 'new-adpresso-group', 'title' => __( 'Group', 'adpresso' ), 'href' => admin_url( 'admin.php?page=adpresso-list-tables&tab=groups' ), 'parent' => 'new-content', ]; $placement_args = [ 'id' => 'new-adpresso-placement', 'title' => __( 'Placement', 'adpresso' ), 'href' => admin_url( 'admin.php?page=adpresso-list-tables&tab=placements' ), 'parent' => 'new-content', ]; $wp_admin_bar->add_node( $ad_args ); $wp_admin_bar->add_node( $group_args ); $wp_admin_bar->add_node( $placement_args ); } /** * Fixes menu highlighting for hidden pages and CPTs. * * @param string|null $file The current file string passed by the filter. * * @return string|null */ public function highlight_menu( ?string $file ): ?string { global $post_type; global $plugin_page; // Check if we are in one of our post-types. if ( in_array( $post_type, [ Constants::POST_TYPE_AD, Constants::POST_TYPE_PLACEMENT, Constants::POST_TYPE_GROUP ], true ) ) { // Open the main menu if ( current_filter() === 'parent_file' ) { $plugin_page = 'adpresso'; return Constants::ADPRESSO_SLUG; } // Highlight the submenu if ( current_filter() === 'submenu_file' ) { return 'edit.php?post_type=' . $post_type; } } // Check if we are on the list table if ( Params::get()->getKey( 'page' ) === 'adpresso-list-tables' ) { // Keep the main menu open. if ( current_filter() === 'parent_file' ) { $plugin_page = 'adpresso'; return Constants::ADPRESSO_SLUG; } // Highlight the active tab in the menu. if ( current_filter() === 'submenu_file' ) { $current_tab = Params::get()->getKey( 'tab', 'ads' ); // Return exactly the same string, we pushed manually in register_screens. return 'admin.php?page=adpresso-list-tables&tab=' . $current_tab; } } // Return the default. return $file; } /** * Returns all registered screens. * * @return Screen[] */ public function get_screens(): array { return $this->screens; } /** * Overrides WordPress text in Footer * * @param mixed $text The footer text. * * @return string */ public function admin_footer_text( mixed $text ): string { if ( ! Util::is_adpresso_admin_screen() ) { return (string) $text; } return sprintf( /* translators: %1$s is the URL to add a new review */ __( 'If AdPresso makes your life easier, consider leaving a ★★★★★ review', 'adpresso' ), 'https://wordpress.org/support/plugin/adpresso/reviews/#new-post' ); } /** * Register all default screens. * * @return void */ public function register_screens(): void { $this->add_screen( new ListTableViewScreen() ); $this->add_screen( new Ads() ); $this->add_screen( new Groups() ); $this->add_screen( new Placements() ); $this->add_screen( new Dashboard() ); $this->add_screen( new Tools() ); $this->add_screen( SettingsPage::get() ); // Allow 3rd-party extensions to register their own screens. do_action( 'adpresso-admin-menu-init', $this ); // Sort by order. uasort( $this->screens, fn( $a, $b ) => $a->get_order() <=> $b->get_order() ); foreach ( $this->screens as $screen ) { $screen->register_screen(); $screen->hooks(); } // remove_submenu_page('adpresso', 'adpresso-list-tables'); } /** * Add a screen. * * @param Screen $screen * * @return void */ public function add_screen( Screens\Screen $screen ): void { $this->screens[ $screen->get_id() ] = $screen; } }