55
66namespace StellarWP \AdminNotices \Actions ;
77
8- use DateTimeImmutable ;
9- use DateTimeZone ;
108use StellarWP \AdminNotices \AdminNotice ;
119use StellarWP \AdminNotices \Traits \HasNamespace ;
1210
@@ -31,156 +29,10 @@ public function __invoke(AdminNotice ...$notices)
3129 }
3230
3331 foreach ($ notices as $ notice ) {
34- if ($ this ->shouldDisplayNotice ($ notice )) {
32+ if (( new NoticeShouldRender ( $ this ->namespace )) ($ notice )) {
3533 echo (new RenderAdminNotice ($ this ->namespace ))($ notice );
3634 }
3735 }
3836 }
39-
40- /**
41- * Checks whether the notice should be displayed based on the provided conditions.
42- *
43- * @since 1.0.0
44- */
45- private function shouldDisplayNotice (AdminNotice $ notice ): bool
46- {
47- return $ this ->passesDismissedConditions ($ notice )
48- && $ this ->passesDateLimits ($ notice )
49- && $ this ->passesWhenCallback ($ notice )
50- && $ this ->passesUserCapabilities ($ notice )
51- && $ this ->passesScreenConditions ($ notice );
52- }
53-
54- /**
55- * Checks whether the notice should be displayed based on the provided date limits.
56- *
57- * @since 1.0.0
58- */
59- private function passesDateLimits (AdminNotice $ notice ): bool
60- {
61- if (!$ notice ->getAfterDate () && !$ notice ->getUntilDate ()) {
62- return true ;
63- }
64-
65- $ now = new DateTimeImmutable ('now ' , new DateTimeZone ('UTC ' ));
66-
67- if ($ notice ->getAfterDate () && $ notice ->getAfterDate () > $ now ) {
68- return false ;
69- }
70-
71- if ($ notice ->getUntilDate () && $ notice ->getUntilDate () < $ now ) {
72- return false ;
73- }
74-
75- return true ;
76- }
77-
78- /**
79- * Checks whether the notice should be displayed based on the provided callback.
80- *
81- * @since 1.0.0
82- */
83- private function passesWhenCallback (AdminNotice $ notice ): bool
84- {
85- $ callback = $ notice ->getWhenCallback ();
86-
87- if ($ callback === null ) {
88- return true ;
89- }
90-
91- return $ callback ();
92- }
93-
94- /**
95- * Checks whether user limits were provided and they pass. Only one capability is required to pass, allowing for
96- * multiple users have visibility.
97- *
98- * @since 1.0.0
99- */
100- private function passesUserCapabilities (AdminNotice $ notice ): bool
101- {
102- $ capabilities = $ notice ->getUserCapabilities ();
103-
104- if (empty ($ capabilities )) {
105- return true ;
106- }
107-
108- foreach ($ capabilities as $ capability ) {
109- if ($ capability ->currentUserCan ()) {
110- return true ;
111- }
112- }
113-
114- return false ;
115- }
116-
117- /**
118- * Checks whether the notice is limited to specific screens and the current screen matches the conditions. Only one
119- * screen condition is required to pass, allowing for the notice to appear on multiple screens.
120- *
121- * @since 1.0.0
122- */
123- private function passesScreenConditions (AdminNotice $ notice ): bool
124- {
125- $ screenConditions = $ notice ->getOnConditions ();
126-
127- if (empty ($ screenConditions )) {
128- return true ;
129- }
130-
131- $ screen = get_current_screen ();
132- $ currentUrl = get_admin_url (null , $ _SERVER ['REQUEST_URI ' ]);
133-
134- foreach ($ screenConditions as $ screenCondition ) {
135- $ condition = $ screenCondition ->getCondition ();
136-
137- if ($ screenCondition ->isRegex ()) {
138- // do a regex comparison on the current url
139- if (preg_match ($ condition , $ currentUrl ) === 1 ) {
140- return true ;
141- }
142- } elseif (is_string ($ condition )) {
143- // do a string comparison on the current url
144- if (strpos ($ currentUrl , $ condition ) !== false ) {
145- return true ;
146- }
147- } else {
148- // compare the condition array against the WP_Screen object
149- foreach ($ condition as $ property => $ value ) {
150- if ($ screen ->$ property === $ value ) {
151- return true ;
152- }
153- }
154- }
155- }
156-
157- return false ;
158- }
159-
160- /**
161- * Checks whether the notice has been dismissed by the user.
162- *
163- * @since 1.1.0 added namespacing to the preferences key
164- * @since 1.0.0
165- */
166- private function passesDismissedConditions (AdminNotice $ notice ): bool
167- {
168- global $ wpdb ;
169-
170- $ userPreferences = get_user_meta (get_current_user_id (), $ wpdb ->get_blog_prefix () . 'persisted_preferences ' , true );
171-
172- $ key = "stellarwp/admin-notices/ $ this ->namespace " ;
173- if (!is_array ($ userPreferences ) || empty ($ userPreferences [$ key ])) {
174- return true ;
175- }
176-
177- $ dismissedNotices = $ userPreferences [$ key ];
178-
179- if (key_exists ($ notice ->getId (), $ dismissedNotices )) {
180- return false ;
181- }
182-
183- return true ;
184- }
18537}
18638
0 commit comments