Passed
Push — develop ( afff43...70b7ee )
by Paul
14:29
created

ShortcodeOptionManager::verified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Database;
8
use GeminiLabs\SiteReviews\Defaults\ShortcodeApiFetchDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Helpers\Arr;
11
use GeminiLabs\SiteReviews\Helpers\Str;
12
13
class ShortcodeOptionManager
14
{
15
    /**
16
     * The parameter passed to the called method can be either a shortcode tag,
17
     * an instantiated shortcode class, or an array with key/values found in
18
     * ShortcodeApiFetchDefaults::class
19
     * 
20
     * @return array
21
     */
22 8
    public function __call(string $name, array $arguments)
23
    {
24 8
        $name = Str::snakeCase($name);
25 8
        $shortcode = array_shift($arguments);
26 8
        if (is_string($shortcode)) {
27
            $shortcode = glsr()->shortcode($shortcode);
28
        }
29 8
        if ($shortcode instanceof ShortcodeContract) {
30
            $args = [
31
                'option' => $name,
32
                'shortcode' => $shortcode->tag,
33
            ];
34
        } else {
35 8
            $args = Arr::consolidate($shortcode);
36
        }
37 8
        $args = glsr()->args(glsr(ShortcodeApiFetchDefaults::class)->merge($args));
38
        try {
39 8
            $method = Helper::buildMethodName($name);
40 8
            $reflection = new \ReflectionMethod($this, $method);
41 8
            $results = $reflection->isProtected()
42 8
                ? call_user_func([$this, $method], $args)
43
                : [];
44
        } catch (\ReflectionException $e) {
45
            $results = [];
46
        }
47 8
        $results = glsr()->filterArray("shortcode/options/{$name}", $results, $args);
48 8
        if (!empty($results) && !empty($args->placeholder)) {
49
            $results = Arr::prepend($results, esc_attr($args->placeholder), '');
50
        }
51 8
        return $results;
52
    }
53
54
    protected function assignedPosts(Arguments $args): array
55
    {
56
        $results = [];
57
        if (!empty($args->search)
58
            && !in_array($args->search, ['post_id', 'parent_id'])) {
59
            $results += glsr(Database::class)->posts([
60
                // @see MainController::parseAssignedPostTypesInQuery
61
                'post_type' => glsr()->prefix.'assigned_posts',
62
                'posts_per_page' => $args->per_page,
63
                's' => $args->search,
64
            ]);
65
        }
66
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
67
        if (!empty($include)) {
68
            $results += glsr(Database::class)->posts([
69
                'post__in' => $include,
70
            ]);
71
        }
72
        return [
73
            'post_id' => esc_html_x('The Current Page', 'admin-text', 'site-reviews'),
74
            'parent_id' => esc_html_x('The Parent Page', 'admin-text', 'site-reviews'),
75
        ] + $results;
76
    }
77
78
    protected function assignedTerms(Arguments $args): array
79
    {
80
        $query = [
81
            'number' => $args->per_page,
82
        ];
83
        if (!empty($args->search)) {
84
            $query['search'] = $args->search;
85
        }
86
        $results = glsr(Database::class)->terms($query);
87
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
88
        if (!empty($include)) {
89
            $results += glsr(Database::class)->terms([
90
                'term_taxonomy_id' => $include,
91
            ]);
92
        }
93
        return $results;
94
    }
95
96
    protected function assignedUsers(Arguments $args): array
97
    {
98
        $query = [
99
            'number' => $args->per_page,
100
        ];
101
        if (!empty($args->search)
102
            && !in_array($args->search, ['author_id', 'profile_id', 'user_id'])) {
103
            $query['search_wild'] = $args->search;
104
        }
105
        $results = glsr(Database::class)->users($query);
106
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
107
        if (!empty($include)) {
108
            $results += glsr(Database::class)->users([
109
                'include' => $include,
110
            ]);
111
        }
112
        return [
113
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
114
            'author_id' => esc_html_x('The Page Author', 'admin-text', 'site-reviews'),
115
            'profile_id' => esc_html_x('The Profile User', 'admin-text', 'site-reviews'),
116
        ] + $results;
117
    }
118
119
    protected function author(Arguments $args): array
120
    {
121
        $query = [
122
            'number' => $args->per_page,
123
        ];
124
        if (!empty($args->search) && !in_array($args->search, ['user_id'])) {
125
            $query['search_wild'] = $args->search;
126
        }
127
        $results = glsr(Database::class)->users($query);
128
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
129
        if (!empty($include)) {
130
            $results += glsr(Database::class)->users([
131
                'include' => $include,
132
            ]);
133
        }
134
        return [
135
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
136
        ] + $results;
137
    }
138
139 8
    protected function hide(Arguments $args): array
140
    {
141 8
        if ($shortcode = glsr()->shortcode($args->shortcode)) {
142 8
            $fn = fn () => $this->hideOptions(); // @phpstan-ignore-line
143 8
            return $fn->bindTo($shortcode, $shortcode)();
144
        }
145
        return [];
146
    }
147
148 8
    protected function pagination(): array
149
    {
150 8
        return [ // order is intentional
151 8
            'loadmore' => _x('Load More Button', 'admin-text', 'site-reviews'),
152 8
            'ajax' => _x('Pagination Links (AJAX)', 'admin-text', 'site-reviews'),
153 8
            'true' => _x('Pagination Links', 'admin-text', 'site-reviews'),
154 8
        ];
155
    }
156
157
    protected function postId(Arguments $args): array
158
    {
159
        $results = [];
160
        if (!empty($args->search)) {
161
            $results += glsr(Database::class)->posts([
162
                'post_type' => glsr()->post_type,
163
                'posts_per_page' => $args->per_page,
164
                's' => $args->search,
165
            ]);
166
        }
167
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
168
        if (!empty($include)) {
169
            $results += glsr(Database::class)->posts([
170
                'post_type' => glsr()->post_type,
171
                'post__in' => $include,
172
            ]);
173
        }
174
        return $results;
175
    }
176
177
    protected function schema(): array
178
    {
179
        return [
180
            'true' => _x('Enable rich snippets', 'admin-text', 'site-reviews'),
181
            'false' => _x('Disable rich snippets', 'admin-text', 'site-reviews'),
182
        ];
183
    }
184
185 8
    protected function terms(): array
186
    {
187 8
        return [
188 8
            'true' => _x('Terms were accepted', 'admin-text', 'site-reviews'),
189 8
            'false' => _x('Terms were not accepted', 'admin-text', 'site-reviews'),
190 8
        ];
191
    }
192
193 8
    protected function type(): array
194
    {
195 8
        $types = glsr()->retrieveAs('array', 'review_types', []);
196 8
        return 1 < count($types) ? $types : [];
197
    }
198
199 8
    protected function verified(): array
200
    {
201 8
        return [
202 8
            'true' => _x('Review is verified', 'admin-text', 'site-reviews'),
203 8
            'false' => _x('Review is not verified', 'admin-text', 'site-reviews'),
204 8
        ];
205
    }
206
}
207