Test Failed
Push — develop ( f70f8a...a40230 )
by Paul
11:01
created

ShortcodeOptionManager::author()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 18
rs 9.8666
cc 4
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
        $shortcode = array_shift($arguments);
25
        if (is_string($shortcode)) {
26
            $shortcode = glsr()->shortcode($shortcode);
27
        }
28
        if ($shortcode instanceof ShortcodeContract) {
29
            $args = [
30
                'option' => Str::snakeCase($name),
31
                '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...
32
            ];
33
        } else {
34
            $args = Arr::consolidate($shortcode);
35
        }
36
        $args = glsr()->args(glsr(ShortcodeApiFetchDefaults::class)->merge($args));
37
        $method = Helper::buildMethodName($name);
38
        $results = method_exists($this, $method)
39
            ? call_user_func([$this, $method], $args)
40
            : [];
41
        $results = glsr()->filterArray("shortcode/options/{$name}", $results, $args);
42
        if (!empty($results) && !empty($args->placeholder)) {
43
            $results = Arr::prepend($results, esc_attr($args->placeholder), '');
44
        }
45
        return $results;
46
    }
47
48
    protected function assignedPosts(Arguments $args): array
49
    {
50
        $results = [];
51
        if (!empty($args->search)
52
            && !in_array($args->search, ['post_id', 'parent_id'])) {
53
            $results += glsr(Database::class)->posts([
54
                // @see MainController::parseAssignedPostTypesInQuery
55
                'post_type' => glsr()->prefix.'assigned_posts',
56
                'posts_per_page' => 50,
57
                's' => $args->search,
58
            ]);
59
        }
60
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
61
        if (!empty($include)) {
62
            $results += glsr(Database::class)->posts([
63
                'post__in' => $include,
64
            ]);
65
        }
66
        return [
67
            'post_id' => esc_html_x('The Current Page', 'admin-text', 'site-reviews'),
68
            'parent_id' => esc_html_x('The Parent Page', 'admin-text', 'site-reviews'),
69
        ] + $results;
70
    }
71
72
    protected function assignedTerms(Arguments $args): array
73
    {
74
        $results = [];
75
        if (!empty($args->search)) {
76
            $results += glsr(Database::class)->terms([
77
                'number' => 50,
78
                'search' => $args->search,
79
            ]);
80
        }
81
        if (!empty($args->include)) {
82
            $results += glsr(Database::class)->terms([
83
                'term_taxonomy_id' => $args->include,
84
            ]);
85
        }
86
        return $results;
87
    }
88
89
    protected function assignedUsers(Arguments $args): array
90
    {
91
        $results = [];
92
        if (!empty($args->search)
93
            && !in_array($args->search, ['author_id', 'profile_id', 'user_id'])) {
94
            $results += glsr(Database::class)->users([
95
                'number' => 50,
96
                'search_wild' => $args->search,
97
            ]);
98
        }
99
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
100
        if (!empty($include)) {
101
            $results += glsr(Database::class)->users([
102
                'include' => $include,
103
            ]);
104
        }
105
        return [
106
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
107
            'author_id' => esc_html_x('The Page Author', 'admin-text', 'site-reviews'),
108
            'profile_id' => esc_html_x('The Profile User', 'admin-text', 'site-reviews'),
109
        ] + $results;
110
    }
111
112
    protected function author(Arguments $args): array
113
    {
114
        $results = [];
115
        if (!empty($args->search) && !in_array($args->search, ['user_id'])) {
116
            $results += glsr(Database::class)->users([
117
                'number' => 50,
118
                'search_wild' => $args->search,
119
            ]);
120
        }
121
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
122
        if (!empty($include)) {
123
            $results += glsr(Database::class)->users([
124
                'include' => $include,
125
            ]);
126
        }
127
        return [
128
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
129
        ] + $results;
130
    }
131
132
    protected function hide(Arguments $args): array
133
    {
134
        if ($shortcode = glsr()->shortcode($args->shortcode)) {
135
            $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

135
            $fn = fn () => $this->/** @scrutinizer ignore-call */ hideOptions(); // @phpstan-ignore-line
Loading history...
136
            return $fn->bindTo($shortcode, $shortcode)();
137
        }
138
        return [];
139
    }
140
141
    protected function pagination(): array
142
    {
143
        return [
144
            'loadmore' => _x('Load More Button', 'admin-text', 'site-reviews'),
145
            'true' => _x('Pagination Links', 'admin-text', 'site-reviews'),
146
            'ajax' => _x('Pagination Links (AJAX)', 'admin-text', 'site-reviews'),
147
        ];
148
    }
149
150
    protected function postId(Arguments $args): array
151
    {
152
        $results = [];
153
        if (!empty($args->search)) {
154
            $results += glsr(Database::class)->posts([
155
                'post_type' => glsr()->post_type,
156
                'posts_per_page' => 50,
157
                's' => $args->search,
158
            ]);
159
        }
160
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
161
        if (!empty($include)) {
162
            $results += glsr(Database::class)->posts([
163
                'post_type' => glsr()->post_type,
164
                'post__in' => $include,
165
            ]);
166
        }
167
        return $results;
168
    }
169
170
    protected function schema(): array
171
    {
172
        return [
173
            'true' => _x('Enable rich snippets', 'admin-text', 'site-reviews'),
174
            'false' => _x('Disable rich snippets', 'admin-text', 'site-reviews'),
175
        ];
176
    }
177
178
    protected function terms(): array
179
    {
180
        return [
181
            'true' => _x('Terms were accepted', 'admin-text', 'site-reviews'),
182
            'false' => _x('Terms were not accepted', 'admin-text', 'site-reviews'),
183
        ];
184
    }
185
186
    protected function type(): array
187
    {
188
        $types = glsr()->retrieveAs('array', 'review_types', []);
189
        return 1 < count($types) ? $types : [];
190
    }
191
}
192