PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / Admin / Notice_Filter.php

Notice_Filter.php in Code Snippets 3.10.1, at php/Admin/Notice_Filter.php

86 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Admin;
4
5 use WP_Screen;
6 use function Code_Snippets\code_snippets;
7
8 /**
9 * Hides admin notices that do not originate from Code Snippets while on a Code Snippets admin screen,
10 * preventing foreign notices from disrupting the plugin's navigation and sub-tab layout.
11 *
12 * @package Code_Snippets
13 */
14 class Notice_Filter {
15
16 /**
17 * Class constructor.
18 */
19 public function __construct() {
20 add_action( 'current_screen', [ $this, 'register_filtering' ] );
21 }
22
23 /**
24 * Activate notice filtering when the current screen belongs to Code Snippets.
25 *
26 * @param WP_Screen $screen Current admin screen.
27 *
28 * @return void
29 */
30 public function register_filtering( WP_Screen $screen ) {
31 if ( ! $this->is_code_snippets_screen( $screen ) ) {
32 return;
33 }
34
35 if ( ! apply_filters( 'code_snippets/admin/filter_foreign_notices', true ) ) {
36 return;
37 }
38
39 add_action( 'admin_head', [ $this, 'print_fallback_styles' ] );
40 }
41
42 /**
43 * Print inline styles that hide foreign notices in the notice region.
44 *
45 * Printed only on the plugin's own screens, so notices are matched at any depth:
46 * each screen renders into its own container, and other plugins inject relative
47 * to whichever wrapper they find.
48 *
49 * @return void
50 */
51 public function print_fallback_styles() {
52 ?>
53 <style>
54 #wpbody-content :is(.notice, .update-nag, .updated, .error):not(.code-snippets-notice):not(.code-snippets-promotion):not(.settings-error) {
55 display: none !important;
56 }
57 </style>
58 <?php
59 }
60
61 /**
62 * Determine whether a screen is one of the plugin's own admin screens.
63 *
64 * @param WP_Screen $screen Current admin screen.
65 *
66 * @return bool
67 */
68 private function is_code_snippets_screen( WP_Screen $screen ): bool {
69 if ( ! isset( code_snippets()->admin ) ) {
70 return false;
71 }
72
73 foreach ( code_snippets()->admin->menus as $menu ) {
74 foreach ( $menu->get_hooknames() as $hookname ) {
75 foreach ( [ $hookname, "$hookname-network" ] as $candidate ) {
76 if ( $screen->id === $candidate || $screen->base === $candidate ) {
77 return true;
78 }
79 }
80 }
81 }
82
83 return false;
84 }
85 }
86