Skip to content

Commit 0d55fbe

Browse files
up1512001sanketio
andcommitted
chore: address PR feedbacks
Co-authored-by: Utsav Patel <[email protected]> Co-authored-by: Sanketio <[email protected]>
1 parent 7d56343 commit 0d55fbe

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

inc/classes/class-assets.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static function build_localized_data(): void {
6767
*/
6868
public function add_admin_scripts( $hook_suffix ): void {
6969

70-
$current_screen = get_current_screen();
70+
$current_screen = Utils::get_current_screen();
7171

7272
if ( strpos( $hook_suffix, 'onedesign-settings' ) !== false ) {
7373

@@ -105,7 +105,7 @@ public function add_admin_scripts( $hook_suffix ): void {
105105
}
106106
}
107107

108-
if ( strpos( $hook_suffix, 'plugins' ) !== false && empty( Utils::get_current_site_type() ) && 'plugins-network' !== $current_screen->id ) {
108+
if ( strpos( $hook_suffix, 'plugins' ) !== false && empty( Utils::get_current_site_type() ) && ( $current_screen && 'plugins-network' !== $current_screen->id ) ) {
109109

110110
// remove all notices.
111111
remove_all_actions( 'admin_notices' );
@@ -161,9 +161,9 @@ public function add_admin_scripts( $hook_suffix ): void {
161161
*/
162162
public function enqueue_scripts(): void {
163163

164-
$current_screen = get_current_screen();
164+
$current_screen = Utils::get_current_screen();
165165

166-
if ( Pattern::SLUG === $current_screen->id ) {
166+
if ( $current_screen && Pattern::SLUG === $current_screen->id ) {
167167

168168
$this->register_script(
169169
'onedesign-patterns-library-script',

inc/classes/class-cpt-restriction.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function setup_hooks(): void {
5656
* @return string Modified title.
5757
*/
5858
public function add_default_title_to_editor( string $title ): string {
59-
if ( Pattern::SLUG === get_current_screen()->post_type ) {
59+
$current_screen = Utils::get_current_screen();
60+
if ( $current_screen && Pattern::SLUG === $current_screen->post_type ) {
6061
return esc_html__( 'Pattern Library', 'onedesign' );
6162
}
6263
return $title;
@@ -131,7 +132,7 @@ public function unregister_cpt(): void {
131132
*/
132133
public function limit_pattern_library_posts(): void {
133134
// Check if we're trying to create a new pattern library post.
134-
$screen = get_current_screen();
135+
$screen = Utils::get_current_screen();
135136
if ( ! $screen || Pattern::SLUG !== $screen->post_type || 'add' !== $screen->action ) {
136137
return;
137138
}
@@ -166,7 +167,7 @@ public function limit_pattern_library_posts(): void {
166167
* @return void
167168
*/
168169
public function limit_template_posts(): void {
169-
$screen = get_current_screen();
170+
$screen = Utils::get_current_screen();
170171
if ( ! $screen || Template::SLUG !== $screen->post_type || 'add' !== $screen->action ) {
171172
return;
172173
}

inc/classes/class-hooks.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function add_setup_page_link( $links ): array {
8282
* @return void
8383
*/
8484
public function add_site_selection_modal(): void {
85-
$current_screen = get_current_screen();
85+
$current_screen = Utils::get_current_screen();
8686
if ( ! $current_screen || 'plugins' !== $current_screen->base ) {
8787
return;
8888
}
@@ -108,7 +108,7 @@ public function add_site_selection_modal(): void {
108108
* @return string
109109
*/
110110
public function add_body_class_for_modal( $classes ): string {
111-
$current_screen = get_current_screen();
111+
$current_screen = Utils::get_current_screen();
112112
if ( ! $current_screen || 'plugins' !== $current_screen->base ) {
113113
return $classes;
114114
}
@@ -133,7 +133,7 @@ public function add_body_class_for_modal( $classes ): string {
133133
* @return string
134134
*/
135135
public function add_body_class_for_missing_sites( $classes ): string {
136-
$current_screen = get_current_screen();
136+
$current_screen = Utils::get_current_screen();
137137

138138
if ( ! $current_screen ) {
139139
return $classes;
@@ -328,7 +328,7 @@ public function remove_core_block_patterns(): void {
328328
* @return void
329329
*/
330330
public function print_pattern_library_button_in_editor_js_template(): void {
331-
$current_screen = get_current_screen();
331+
$current_screen = Utils::get_current_screen();
332332
if ( ! $current_screen || Pattern::SLUG !== $current_screen->post_type ) {
333333
return;
334334
}
@@ -350,7 +350,7 @@ public function print_pattern_library_button_in_editor_js_template(): void {
350350
* @return void
351351
*/
352352
public function add_templates_button_to_editor(): void {
353-
$current_screen = get_current_screen();
353+
$current_screen = Utils::get_current_screen();
354354
if ( ! $current_screen || Template::SLUG !== $current_screen->post_type ) {
355355
return;
356356
}

inc/classes/class-multisite.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public function render_governing_site_modal(): void {
6666
return;
6767
}
6868

69-
$current_screen = get_current_screen();
69+
$current_screen = Utils::get_current_screen();
7070

71-
if ( 'plugins-network' !== $current_screen->id ) {
71+
if ( $current_screen && 'plugins-network' !== $current_screen->id ) {
7272
return;
7373
}
7474

@@ -95,9 +95,9 @@ public function add_admin_body_class( string $classes ): string {
9595
return $classes;
9696
}
9797

98-
$current_screen = get_current_screen();
98+
$current_screen = Utils::get_current_screen();
9999

100-
if ( is_network_admin() && 'plugins-network' === $current_screen->id ) {
100+
if ( is_network_admin() && $current_screen && 'plugins-network' === $current_screen->id ) {
101101
$classes .= ' onedesign-multisite-selection-modal ';
102102
}
103103
return $classes;

inc/classes/class-utils.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,22 @@ public static function replace_block_refs( array $items, array $id_map = array()
472472

473473
return $items;
474474
}
475+
476+
/**
477+
* Get current screen object.
478+
*
479+
* @return WP_Screen|null Current screen object or null if not available.
480+
*/
481+
public static function get_current_screen(): ?\WP_Screen {
482+
if ( ! function_exists( 'get_current_screen' ) ) {
483+
return null;
484+
}
485+
$screen = get_current_screen();
486+
487+
if ( ! is_a( $screen, '\WP_Screen' ) ) {
488+
return null;
489+
}
490+
491+
return $screen;
492+
}
475493
}

0 commit comments

Comments
 (0)