plugin = $plugin; } /** * Registers the admin_menu hook and the Plugins screen row link. */ public function register(): void { add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_filter( 'plugin_action_links_' . plugin_basename( TAGSMITH_FILE ), array( $this, 'add_row_link' ) ); } /** * Adds the top-level page plus the Dashboard and Settings submenus. */ public function register_menu(): void { add_menu_page( __( 'Tagsmith', 'tagsmith' ), __( 'Tagsmith', 'tagsmith' ), Capabilities::VIEW, self::DASHBOARD_SLUG, array( $this, 'render_dashboard' ), 'dashicons-chart-area', 30 ); add_submenu_page( self::DASHBOARD_SLUG, __( 'Dashboard', 'tagsmith' ), __( 'Dashboard', 'tagsmith' ), Capabilities::VIEW, self::DASHBOARD_SLUG, array( $this, 'render_dashboard' ) ); add_submenu_page( self::DASHBOARD_SLUG, __( 'Settings', 'tagsmith' ), __( 'Settings', 'tagsmith' ), 'manage_options', self::SETTINGS_SLUG, array( $this, 'render_settings' ) ); } /** * Puts a link to the Settings screen first in the plugin's row on the * Plugins screen: "Set up" until the site is configured, "Settings" * after. It is the only pointer to setup; the plugin shows no admin * notice and does not redirect on activation. Only users who can reach * the Settings screen get it. * * @param array $links The row's action links. * @return array */ public function add_row_link( array $links ): array { if ( ! current_user_can( 'manage_options' ) ) { return $links; } $name = 'Tagsmith'; if ( $this->is_configured() ) { $label = __( 'Settings', 'tagsmith' ); /* translators: %s: plugin name. */ $aria = sprintf( __( 'Settings for %s', 'tagsmith' ), $name ); } else { $label = __( 'Set up', 'tagsmith' ); /* translators: %s: plugin name. */ $aria = sprintf( __( 'Set up %s', 'tagsmith' ), $name ); } $link = sprintf( '%3$s', esc_url( admin_url( 'admin.php?page=' . self::SETTINGS_SLUG ) ), esc_attr( $aria ), esc_html( $label ) ); return array( 'tagsmith_setup' => $link ) + $links; } /** * Whether the site has a way to get data: a Measurement ID is saved, or * Google is connected and a property is chosen. It does not depend on * the wizard's `onboarded` flag, which only records that the wizard was * finished or skipped. */ private function is_configured(): bool { $settings = $this->plugin->settings(); if ( '' !== (string) $settings->get( 'measurement_id' ) ) { return true; } return '' !== (string) $settings->get( 'property_id' ) && $this->plugin->connection()->is_connected(); } /** * Renders the Dashboard screen's React mount point. */ public function render_dashboard(): void { echo '
'; } /** * Renders the Settings screen's React mount point. */ public function render_settings(): void { echo '
'; } }