$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 ) { ?>