Skip to content

Commit 30fa50a

Browse files
Feature: enqueue script and stylesheet with notice (#10)
1 parent f05e69a commit 30fa50a

File tree

10 files changed

+618
-350
lines changed

10 files changed

+618
-350
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,36 @@ dismissible notices.
493493
If you want the notice to be marked as dismissed, but not fade out, you can pass "clear" to the
494494
`customerCloserAttributes` method: e.g. `$elements->customCloserAttributes('clear')`.
495495

496+
## Custom scripts & styles
497+
498+
Often times, especially for custom notices, you may want to enqueue custom scripts and styles,
499+
however they should only be enqueued when the notice is displayed. This can be done by using the
500+
`enqueueScript` and `enqueueStyle` methods on the notice.
501+
502+
### `enqueueScript($src, $deps = [], $ver = false, $args = [])`
503+
504+
Enqueues a script to be loaded when the notice is displayed, following the same parameters as
505+
`wp_enqueue_script`. The only difference is that the loading strategy is "defer" by default.
506+
507+
```php
508+
use StellarWP\AdminNotices\AdminNotices;
509+
510+
$notice = AdminNotices::show('my_notice', 'This is a notice')
511+
->enqueueScript('https://example.com/my-script.js', ['jquery']);
512+
```
513+
514+
### `enqueueStyle($src, $deps = [], $ver = false, $media = 'all')`
515+
516+
Enqueues a style to be loaded when the notice is displayed, following the same parameters as
517+
`wp_enqueue_style`.
518+
519+
```php
520+
use StellarWP\AdminNotices\AdminNotices;
521+
522+
$notice = AdminNotices::show('my_notice', 'This is a notice')
523+
->enqueueStylesheet('https://example.com/my-style.css');
524+
```
525+
496526
## Resetting dismissed notices
497527

498528
For dismissible notices, when the user dismisses the notice, it is permanently dismissed. If you

src/Actions/DisplayNoticesInAdmin.php

Lines changed: 1 addition & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
namespace StellarWP\AdminNotices\Actions;
77

8-
use DateTimeImmutable;
9-
use DateTimeZone;
108
use StellarWP\AdminNotices\AdminNotice;
119
use 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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace StellarWP\AdminNotices\Actions;
7+
8+
use StellarWP\AdminNotices\AdminNotice;
9+
use StellarWP\AdminNotices\Traits\HasNamespace;
10+
11+
/**
12+
* Checks the given admin notices and enqueues their scripts and styles if they exist and should be rendered.
13+
*
14+
* @unreleased
15+
*/
16+
class EnqueueNoticesScriptsAndStyles
17+
{
18+
use HasNamespace;
19+
20+
/**
21+
* @unreleased
22+
*/
23+
public function __invoke(AdminNotice ...$notices)
24+
{
25+
foreach ($notices as $notice) {
26+
$script = $notice->getScriptToEnqueue();
27+
$style = $notice->getStyleToEnqueue();
28+
29+
if (($script || $style) && (new NoticeShouldRender($this->namespace))($notice)) {
30+
if ($script) {
31+
$script->enqueue("stellarwp-{$this->namespace}-{$notice->getId()}");
32+
}
33+
34+
if ($style) {
35+
$style->enqueue("stellarwp-{$this->namespace}-{$notice->getId()}");
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)