Skip to content

Commit 7db5be2

Browse files
authored
Notices (#6)
1 parent ab6c585 commit 7db5be2

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

inc/notices.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace WPT;
4+
5+
use WPT\Enums\NoticeType;
6+
7+
function adminNotices()
8+
{
9+
return [
10+
NoticeType::Test->value => [
11+
'enabled' => \get_option('wpt_notice_for_'.NoticeType::Test->value, false),
12+
'type' => 'info',
13+
'text' => __('This is a test notice.', 'wp-plugin-template'),
14+
],
15+
];
16+
}
17+
18+
function toggleAdminNotice(NoticeType $type, $enabled = true)
19+
{
20+
return \update_option("wpt_notice_for_{$type->value}", $enabled);
21+
}
22+
23+
function displayAdminNotices()
24+
{
25+
$html = '';
26+
27+
\collect(adminNotices())
28+
->each(function ($notice) use (&$html) {
29+
if ($notice['enabled']) {
30+
$html .= <<< EOT
31+
<div class='notice notice-{$notice['type']}'>
32+
<p>{$notice['text']}</p>
33+
</div>
34+
EOT;
35+
}
36+
});
37+
38+
echo $html;
39+
}
40+
41+
add_action('admin_notices', 'WPT\displayAdminNotices');

src/Crons/CronJob.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use SebKay\WPCronable\WPCronable;
66
use WPT\Concerns\Instanceable;
7+
use WPT\Enums\NoticeType;
78
use WPT\Processes\BackgroundProcess;
89

10+
use function WPT\toggleAdminNotice;
11+
912
class CronJob extends WPCronable
1013
{
1114
use Instanceable;
@@ -21,9 +24,13 @@ public function __construct(
2124
public function run(): void
2225
{
2326
try {
27+
toggleAdminNotice(NoticeType::Test);
28+
2429
$items = \collect([]);
2530

2631
if ($items->isEmpty()) {
32+
toggleAdminNotice(NoticeType::Test, false);
33+
2734
return;
2835
}
2936

@@ -41,6 +48,8 @@ public function run(): void
4148

4249
$this->process->dispatch();
4350
} catch (\Exception $e) {
51+
toggleAdminNotice(NoticeType::Test, false);
52+
4453
\error_log($e);
4554

4655
\wptPlugin()->logger()->general()->error($e->getMessage());

src/Enums/NoticeType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace WPT\Enums;
4+
5+
enum NoticeType: string
6+
{
7+
case Test = 'test';
8+
}

src/Processes/BackgroundProcess.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace WPT\Processes;
44

5+
use WPT\Enums\NoticeType;
6+
7+
use function WPT\toggleAdminNotice;
8+
59
class BackgroundProcess extends \WPT_WP_Background_Process
610
{
711
protected $action = 'wpt_background_process';
@@ -22,4 +26,18 @@ protected function task($data)
2226

2327
return false;
2428
}
29+
30+
public function cancel()
31+
{
32+
parent::cancel();
33+
34+
toggleAdminNotice(NoticeType::Test, false);
35+
}
36+
37+
public function complete()
38+
{
39+
toggleAdminNotice(NoticeType::Test, false);
40+
41+
parent::complete();
42+
}
2543
}

wordpress-plugin-template.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
require_once WPT_DIR_PATH.'/vendor/autoload.php';
2323

24+
require_once WPT_DIR_PATH.'/inc/notices.php';
25+
2426
function wptPlugin()
2527
{
2628
return WPT\Plugin::instance();

0 commit comments

Comments
 (0)