Passed
Push — develop ( 7073e7...becc1e )
by Paul
13:56
created

ShortcodeOptionManager::assignedUsers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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