ShortcodeOptionManager::assignedTerms()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 4
nop 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
    public function __call(string $name, array $arguments)
23
    {
24
        $name = 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' => $name,
32
                'shortcode' => $shortcode->tag,
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
            ];
34
        } else {
35
            $args = Arr::consolidate($shortcode);
36
        }
37
        $args = glsr()->args(glsr(ShortcodeApiFetchDefaults::class)->merge($args));
38
        try {
39
            $method = Helper::buildMethodName($name);
40
            $reflection = new \ReflectionMethod($this, $method);
41
            $results = $reflection->isProtected()
42
                ? call_user_func([$this, $method], $args)
43
                : [];
44
        } catch (\ReflectionException $e) {
45
            $results = [];
46
        }
47
        $results = glsr()->filterArray("shortcode/options/{$name}", $results, $args);
48
        if (!empty($results) && !empty($args->placeholder)) {
49
            $results = Arr::prepend($results, esc_attr($args->placeholder), '');
50
        }
51
        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
    protected function hide(Arguments $args): array
140
    {
141
        if ($shortcode = glsr()->shortcode($args->shortcode)) {
142
            $fn = fn () => $this->hideOptions(); // @phpstan-ignore-line
0 ignored issues
show
Bug introduced by
The method hideOptions() does not exist on GeminiLabs\SiteReviews\D...\ShortcodeOptionManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
            $fn = fn () => $this->/** @scrutinizer ignore-call */ hideOptions(); // @phpstan-ignore-line
Loading history...
143
            return $fn->bindTo($shortcode, $shortcode)();
144
        }
145
        return [];
146
    }
147
148
    protected function pagination(): array
149
    {
150
        return [ // order is intentional
151
            'loadmore' => _x('Load More Button', 'admin-text', 'site-reviews'),
152
            'ajax' => _x('Pagination Links (AJAX)', 'admin-text', 'site-reviews'),
153
            'true' => _x('Pagination Links', 'admin-text', 'site-reviews'),
154
        ];
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
    protected function terms(): array
186
    {
187
        return [
188
            'true' => _x('Terms were accepted', 'admin-text', 'site-reviews'),
189
            'false' => _x('Terms were not accepted', 'admin-text', 'site-reviews'),
190
        ];
191
    }
192
193
    protected function type(): array
194
    {
195
        $types = glsr()->retrieveAs('array', 'review_types', []);
196
        return 1 < count($types) ? $types : [];
197
    }
198
}
199