Test Failed
Push — main ( db043e...d2c26f )
by Paul
14:29 queued 07:14
created

AbstractController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 22
eloc 30
c 1
b 0
f 1
dl 0
loc 76
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isReviewListTable() 0 4 2
A app() 0 3 1
A isListTable() 0 4 2
A execute() 0 4 1
A isReviewAdminPage() 0 5 2
A getPostId() 0 3 1
A download() 0 8 2
A isNoticeAdminScreen() 0 3 2
A hasQueryPermission() 0 7 5
A isReviewAdminScreen() 0 3 1
A isReviewEditor() 0 6 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Contracts\CommandContract;
6
use GeminiLabs\SiteReviews\Contracts\ControllerContract;
7
use GeminiLabs\SiteReviews\Contracts\PluginContract;
8
use GeminiLabs\SiteReviews\HookProxy;
9
10
abstract class AbstractController implements ControllerContract
11
{
12
    use HookProxy;
13
14
    public function app(): PluginContract
15
    {
16
        return glsr();
17
    }
18
19
    public function download(string $filename, string $content): void
20
    {
21
        if (glsr()->can('edit_others_posts')) {
22
            nocache_headers();
23
            header('Content-Type: text/plain');
24
            header("Content-Disposition: attachment; filename=\"{$filename}\"");
25
            echo wp_specialchars_decode($content);
26
            exit;
27
        }
28
    }
29
30
    public function execute(CommandContract $command): CommandContract
31
    {
32
        $command->handle();
33
        return $command;
34
    }
35
36
    protected function getPostId(): int
37
    {
38
        return intval(filter_input(INPUT_GET, 'post'));
39
    }
40
41
    protected function hasQueryPermission(\WP_Query $query, string $postType = ''): bool
42
    {
43
        global $pagenow;
44
        return glsr()->isAdmin()
45
            && $query->is_main_query()
46
            && $query->get('post_type') === ($postType ?: glsr()->post_type)
47
            && 'edit.php' === $pagenow;
48
    }
49
50
    protected function isListTable(): bool
51
    {
52
        $screen = glsr_current_screen();
53
        return 'edit' === $screen->base && $this->isReviewAdminScreen();
54
    }
55
56
    protected function isNoticeAdminScreen(): bool
57
    {
58
        return 'dashboard' === glsr_current_screen()->id || $this->isReviewAdminScreen();
59
    }
60
61
    protected function isReviewAdminPage(): bool
62
    {
63
        return glsr()->isAdmin() && in_array(glsr()->post_type, [
64
            filter_input(INPUT_GET, 'post_type'),
65
            get_post_type(),
66
        ]);
67
    }
68
69
    protected function isReviewAdminScreen(): bool
70
    {
71
        return str_starts_with(glsr_current_screen()->post_type, glsr()->post_type);
72
    }
73
74
    protected function isReviewEditor(): bool
75
    {
76
        $screen = glsr_current_screen();
77
        return ('post' === $screen->base)
78
            && glsr()->post_type === $screen->id
79
            && glsr()->post_type === $screen->post_type;
80
    }
81
82
    protected function isReviewListTable(): bool
83
    {
84
        $screen = glsr_current_screen();
85
        return 'edit' === $screen->base && glsr()->post_type === $screen->post_type;
86
    }
87
}
88