Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions includes/Core/Admin/Authorize_Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Assets\Assets;
use Google\Site_Kit\Core\Util\Current_Screen;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;

/**
Expand Down Expand Up @@ -75,13 +76,9 @@ public function register() {
* @return bool True if the current screen is the Authorize Application screen, false otherwise.
*/
protected function is_authorize_application_screen() {
$current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
$current_screen = Current_Screen::get();

if ( $current_screen instanceof \WP_Screen && 'authorize-application' === $current_screen->id ) {
return true;
}

return false;
return null !== $current_screen && 'authorize-application' === $current_screen->id;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions includes/Core/Admin_Bar/Admin_Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Google\Site_Kit\Core\REST_API\REST_Route;
use Google\Site_Kit\Core\REST_API\REST_Routes;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Util\Current_Screen;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Core\Util\Requires_Javascript_Trait;
use WP_REST_Server;
Expand Down Expand Up @@ -244,10 +245,10 @@ public function is_active() {
* @since 1.0.0
*/
private function is_admin_post_screen() {
$current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
$current_screen = Current_Screen::get();

// No screen context available.
if ( ! $current_screen instanceof \WP_Screen ) {
if ( null === $current_screen ) {
return false;
}

Expand Down
42 changes: 42 additions & 0 deletions includes/Core/Util/Current_Screen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Class Google\Site_Kit\Core\Util\Current_Screen
*
* @package Google\Site_Kit\Core\Util
* @copyright 2026 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Core\Util;

use WP_Screen;

/**
* Helper for safely accessing the current `WP_Screen`.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Current_Screen {

/**
* Returns the current `WP_Screen`, or `null` when called outside the admin
* (where `wp-admin/includes/screen.php` has not been loaded and
* `get_current_screen()` is not defined).
*
* @since n.e.x.t
*
* @return WP_Screen|null Current `WP_Screen` instance, or null when called outside the admin.
*/
public static function get(): ?WP_Screen {
if ( ! function_exists( 'get_current_screen' ) ) {
return null;
}

$screen = get_current_screen();

return $screen instanceof WP_Screen ? $screen : null;
}
}
5 changes: 2 additions & 3 deletions includes/Core/Util/Entity_Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use WP_Term;
use WP_User;
use WP_Post_Type;
use WP_Screen;

/**
* Class providing access to entities.
Expand Down Expand Up @@ -46,8 +45,8 @@ public static function from_context() {

// If currently in WP admin, run admin-specific checks.
if ( is_admin() ) {
$screen = get_current_screen();
if ( ! $screen instanceof WP_Screen || 'post' !== $screen->base ) {
$screen = Current_Screen::get();
if ( null === $screen || 'post' !== $screen->base ) {
return null;
}

Expand Down
Loading