Test Failed
Push — develop ( c4a2cb...83a5b5 )
by Paul
07:40
created

ShortcodeOptionManager::pagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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 hide(Arguments $args): array
113
    {
114
        if ($shortcode = glsr()->shortcode($args->shortcode)) {
115
            $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

115
            $fn = fn () => $this->/** @scrutinizer ignore-call */ hideOptions(); // @phpstan-ignore-line
Loading history...
116
            return $fn->bindTo($shortcode, $shortcode)();
117
        }
118
        return [];
119
    }
120
121
    protected function pagination(): array
122
    {
123
        return [
124
            'loadmore' => _x('Load More Button', 'admin-text', 'site-reviews'),
125
            'ajax' => _x('Pagination Links (AJAX)', 'admin-text', 'site-reviews'),
126
            'true' => _x('Pagination Links (with page reload)', 'admin-text', 'site-reviews'),
127
        ];
128
    }
129
130
    protected function postId(Arguments $args): array
131
    {
132
        $results = [];
133
        if (!empty($args->search)) {
134
            $results += glsr(Database::class)->posts([
135
                'post_type' => glsr()->post_type,
136
                'posts_per_page' => 50,
137
                's' => $args->search,
138
            ]);
139
        }
140
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
141
        if (!empty($include)) {
142
            $results += glsr(Database::class)->posts([
143
                'post_type' => glsr()->post_type,
144
                'post__in' => $include,
145
            ]);
146
        }
147
        return $results;
148
    }
149
150
    protected function terms(): array
151
    {
152
        return [
153
            'true' => _x('Terms were accepted', 'admin-text', 'site-reviews'),
154
            'false' => _x('Terms were not accepted', 'admin-text', 'site-reviews'),
155
        ];
156
    }
157
158
    protected function type(): array
159
    {
160
        $types = glsr()->retrieveAs('array', 'review_types', []);
161
        return 1 < count($types) ? $types : [];
162
    }
163
}
164