Passed
Push — main ( f45cd4...dfed84 )
by Paul
06:07 queued 49s
created

NoticeController::injectAfterNotices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Modules\Sanitizer;
7
use GeminiLabs\SiteReviews\Request;
8
9
class NoticeController extends Controller
10
{
11
    /**
12
     * @action current_screen
13
     */
14 9
    public function activatePlugin(): void
15
    {
16 9
        $action = filter_input(INPUT_GET, 'action');
17 9
        $plugin = filter_input(INPUT_GET, 'plugin');
18 9
        $trigger = filter_input(INPUT_GET, 'trigger');
19 9
        if ('activate' !== $action || 'notice' !== $trigger || empty($plugin)) {
20 9
            return;
21
        }
22
        check_admin_referer('activate-plugin_'.$plugin);
23
        $result = activate_plugin($plugin, '', is_network_admin(), true);
24
        if (is_wp_error($result)) {
25
            wp_die($result->get_error_message());
26
        }
27
        wp_safe_redirect(wp_get_referer());
28
        exit;
29
    }
30
31
    /**
32
     * @admin admin_head
33
     */
34
    public function adminNotices(): void
35
    {
36
        $dir = glsr()->path('plugin/Notices');
37
        if (!is_dir($dir)) {
38
            return;
39
        }
40
        $iterator = new \DirectoryIterator($dir);
41
        foreach ($iterator as $fileinfo) {
42
            if (!$fileinfo->isFile()) {
43
                continue;
44
            }
45
            try {
46
                $notice = '\GeminiLabs\SiteReviews\Notices\\'.$fileinfo->getBasename('.php');
47
                $reflect = new \ReflectionClass($notice);
48
                if ($reflect->isInstantiable()) {
49
                    glsr()->singleton($notice); // make singleton
50
                    glsr($notice);
51
                }
52
            } catch (\ReflectionException $e) {
53
                glsr_log()->error($e->getMessage());
54
            }
55
        }
56
    }
57
58
    /**
59
     * @action site-reviews/route/admin/dismiss-notice
60
     */
61
    public function dismissNotice(Request $request): void
62
    {
63
        $noticeKey = glsr(Sanitizer::class)->sanitizeText($request->notice);
64
        $notice = Helper::buildClassName($noticeKey.'-notice', 'Notices');
65
        if (class_exists($notice)) {
66
            glsr($notice)->dismiss();
67
        }
68
    }
69
70
    /**
71
     * @action site-reviews/route/ajax/dismiss-notice
72
     */
73
    public function dismissNoticeAjax(Request $request): void
74
    {
75
        $this->dismissNotice($request);
76
        wp_send_json_success();
77
    }
78
79
    /**
80
     * @action admin_notices
81
     */
82
    public function injectAfterNotices(): void
83
    {
84
        if (str_contains(glsr_current_screen()->id, glsr()->post_type)) {
85
            // Close the hidden div used to prevent notices from flickering before
86
            // they are moved elsewhere in the page by WordPress Core.
87
            echo '</div>';
88
        }
89
    }
90
91
    /**
92
     * @action admin_notices
93
     */
94
    public function injectBeforeNotices(): void
95
    {
96
        if (str_contains(glsr_current_screen()->id, glsr()->post_type)) {
97
            // Wrap the notices in a hidden div to prevent flickering before
98
            // they are moved elsewhere in the page by WordPress Core.
99
            echo '<div id="glsr-notice-catcher">';
100
        }
101
    }
102
}
103