$screen pairs. * * @since 1.0.0 * @var array */ private $screens = array(); /** * Constructor. * * @since 1.0.0 * * @param Context $context Plugin context. * @param Assets $assets Optional. Assets API instance. Default is a new instance. */ public function __construct( Context $context, Assets $assets = null ) { $this->context = $context; if ( ! $assets ) { $assets = new Assets( $this->context ); } $this->assets = $assets; } /** * Registers functionality through WordPress hooks. * * @since 1.0.0 */ public function register() { if ( $this->context->is_network_mode() ) { add_action( 'network_admin_menu', function() { $this->add_screens(); } ); } add_action( 'admin_menu', function() { $this->add_screens(); } ); add_action( 'admin_enqueue_scripts', function( $hook_suffix ) { $this->enqueue_screen_assets( $hook_suffix ); } ); // Ensure the menu icon always is rendered correctly, without enqueueing a global CSS file. add_action( 'admin_head', function() { ?> screens[ $hook_suffix ] ) ) { remove_all_actions( current_action() ); } }; add_action( 'admin_notices', $remove_notices_callback, -9999 ); add_action( 'network_admin_notices', $remove_notices_callback, -9999 ); add_action( 'all_admin_notices', $remove_notices_callback, -9999 ); add_filter( 'custom_menu_order', '__return_true' ); add_filter( 'menu_order', function( array $menu_order ) { $new_order = array(); foreach ( $menu_order as $index => $item ) { if ( 'index.php' === $item || 0 === strpos( $item, self::PREFIX ) ) { $new_order[] = $item; unset( $menu_order[ $index ] ); } } return array_values( array_merge( $new_order, $menu_order ) ); } ); } /** * Adds all screens to the admin. * * @since 1.0.0 */ private function add_screens() { $screens = $this->get_screens(); array_walk( $screens, array( $this, 'add_screen' ) ); } /** * Adds the given screen to the admin. * * @since 1.0.0 * * @param Screen $screen Screen to add. */ private function add_screen( Screen $screen ) { $hook_suffix = $screen->add( $this->context ); if ( empty( $hook_suffix ) ) { return; } add_action( "load-{$hook_suffix}", function() use ( $screen ) { $screen->initialize( $this->context ); } ); $this->screens[ $hook_suffix ] = $screen; } /** * Enqueues assets if a plugin screen matches the given hook suffix. * * @since 1.0.0 * * @param string $hook_suffix Hook suffix for the current admin screen. */ private function enqueue_screen_assets( $hook_suffix ) { if ( ! isset( $this->screens[ $hook_suffix ] ) ) { return; } $this->screens[ $hook_suffix ]->enqueue_assets( $this->assets ); /** * Fires when assets are enqueued for a Site Kit admin screen. * * @since 1.0.0 */ do_action( 'googlesitekit_enqueue_screen_assets' ); } /** * Gets available admin screens. * * @since 1.0.0 * * @return array List of Screen instances. */ private function get_screens() { $screens = array( new Screen( self::PREFIX . 'dashboard', array( 'title' => __( 'Dashboard', 'google-site-kit' ), 'capability' => Permissions::VIEW_DASHBOARD, 'enqueue_callback' => function( Assets $assets ) { if ( filter_input( INPUT_GET, 'permaLink' ) ) { $assets->enqueue_asset( 'googlesitekit_dashboard_details' ); } else { $assets->enqueue_asset( 'googlesitekit_dashboard' ); } }, 'render_callback' => function( Context $context ) { ?>
__( 'Settings', 'google-site-kit' ), 'capability' => Permissions::MANAGE_OPTIONS, 'enqueue_callback' => function( Assets $assets ) { $assets->enqueue_asset( 'googlesitekit_settings' ); }, 'render_callback' => function( Context $context ) { ?>
__( 'Dashboard', 'google-site-kit' ), 'capability' => Permissions::AUTHENTICATE, 'parent_slug' => $show_splash_in_menu ? Screen::MENU_SLUG : null, // This callback will redirect to the dashboard on successful authentication. 'initialize_callback' => function( Context $context ) { $splash_context = filter_input( INPUT_GET, 'googlesitekit_context' ); $authentication = new Authentication( $context ); // If the user is authenticated, redirect them to the disconnect URL and then send them back here. if ( empty( $_GET['googlesitekit_reset_session'] ) && 'revoked' === $splash_context && $authentication->is_authenticated() ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification $authentication->disconnect(); wp_safe_redirect( add_query_arg( array( 'googlesitekit_reset_session' => 1 ) ) ); exit; } $notification = filter_input( INPUT_GET, 'notification' ); $error = filter_input( INPUT_GET, 'error' ); // Bail if no success parameter indicator. if ( 'authentication_success' !== $notification || ! empty( $error ) ) { return; } // Bail if the current user cannot access the dashboard. if ( ! current_user_can( Permissions::VIEW_DASHBOARD ) ) { return; } wp_safe_redirect( $context->admin_url( 'dashboard', array( 'notification' => 'authentication_success', ) ) ); exit; }, 'enqueue_callback' => function( Assets $assets ) { $assets->enqueue_asset( 'googlesitekit_dashboard_splash' ); }, 'render_callback' => function( Context $context ) { ?>