Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

ProfileController::filterSummaryTextValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 5
c 2
b 0
f 1
dl 0
loc 9
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\UltimateMember\Controllers;
4
5
use GeminiLabs\SiteReviews\Contracts\TagContract;
6
use GeminiLabs\SiteReviews\Controllers\AbstractController;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsFormShortcode;
10
11
class ProfileController extends AbstractController
12
{
13
    /**
14
     * @filter site-reviews/enqueue/public/inline-script/after
15
     */
16
    public function filterInlineScript(string $javascript): string
17
    {
18
        if (!$this->hasVisibilityPermission()) {
19
            return $javascript;
20
        }
21
        return $javascript.'document.addEventListener("DOMContentLoaded", () => {'.
22
            'GLSR.Event.on("site-reviews/form/handle", (response, form) => {'.
23
                'if (true !== response.success || "undefined" === typeof response.html) return;'.
24
                'form.classList.add("glsr-hide-form");'.
25
                'form.insertAdjacentHTML("afterend", "<p class=\"glsr-no-margins glsr-form-success\">"+response.message+"</p>");'.
26
                'form.remove()'.
27
            '})'.
28
        '})';
29
    }
30
31
    /**
32
     * @filter um_user_profile_tabs
33
     */
34
    public function filterProfileTabs(array $tabs): array
35
    {
36
        if (!$this->hasVisibilityPermission()) {
37
            return $tabs;
38
        }
39
        $tabs['user_reviews'] = [
40
            'icon' => 'um-faicon-star',
41
            'name' => __('Reviews', 'site-reviews'),
42
        ];
43
        return $tabs;
44
    }
45
46
    /**
47
     * @filter site-reviews/review/value/author
48
     */
49
    public function filterReviewAuthorValue(string $value, TagContract $tag): string
50
    {
51
        if ($user = $tag->review->user()) { // @phpstan-ignore-line
1 ignored issue
show
Bug introduced by
Accessing review on the interface GeminiLabs\SiteReviews\Contracts\TagContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
            $url = um_user_profile_url($user->ID);
0 ignored issues
show
Bug introduced by
The function um_user_profile_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
            $url = /** @scrutinizer ignore-call */ um_user_profile_url($user->ID);
Loading history...
53
            return sprintf('<a href="%s">%s</a>', $url, $value);
54
        }
55
        return $value;
56
    }
57
58
    /**
59
     * @filter site-reviews/reviews/fallback
60
     */
61
    public function filterReviewsFallback(string $fallback, array $args): string
62
    {
63
        if ($args['fallback'] !== __('There are no reviews yet. Be the first one to write one.', 'site-reviews')) {
64
            return $fallback;
65
        }
66
        $value = get_current_user_id() === um_get_requested_user()
0 ignored issues
show
Bug introduced by
The function um_get_requested_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

66
        $value = get_current_user_id() === /** @scrutinizer ignore-call */ um_get_requested_user()
Loading history...
67
            ? __('You have not received any reviews.', 'site-reviews')
68
            : __('This person has not received any reviews.', 'site-reviews');
69
        return glsr(Builder::class)->p([
70
            'class' => 'glsr-no-margins',
71
            'text' => $value,
72
        ]);
73
    }
74
75
    /**
76
     * @filter site-reviews/summary/value/percentages
77
     */
78
    public function filterSummaryPercentagesValue(string $value, TagContract $tag): string
79
    {
80
        if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line
1 ignored issue
show
Bug introduced by
Accessing ratings on the interface GeminiLabs\SiteReviews\Contracts\TagContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
81
            return $value;
82
        }
83
        return '';
84
    }
85
86
    /**
87
     * @filter site-reviews/summary/value/text
88
     */
89
    public function filterSummaryRatingValue(string $value, TagContract $tag): string
2 ignored issues
show
Unused Code introduced by
The parameter $tag is not used and could be removed. ( Ignorable by Annotation )

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

89
    public function filterSummaryRatingValue(string $value, /** @scrutinizer ignore-unused */ TagContract $tag): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

89
    public function filterSummaryRatingValue(/** @scrutinizer ignore-unused */ string $value, TagContract $tag): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $value = get_current_user_id() === um_get_requested_user()
0 ignored issues
show
Bug introduced by
The function um_get_requested_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

91
        $value = get_current_user_id() === /** @scrutinizer ignore-call */ um_get_requested_user()
Loading history...
92
            ? __('Your rating', 'site-reviews')
93
            : __('User rating', 'site-reviews');
94
        return glsr(Builder::class)->h4($value);
95
    }
96
97
    /**
98
     * @filter site-reviews/summary/value/rating
99
     */
100
    public function filterSummaryTextValue(string $value, TagContract $tag): string
101
    {
102
        if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line
1 ignored issue
show
Bug introduced by
Accessing ratings on the interface GeminiLabs\SiteReviews\Contracts\TagContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
103
            return $value;
104
        }
105
        if (get_current_user_id() === um_get_requested_user()) {
0 ignored issues
show
Bug introduced by
The function um_get_requested_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

105
        if (get_current_user_id() === /** @scrutinizer ignore-call */ um_get_requested_user()) {
Loading history...
106
            return __('No one has reviewed you yet.', 'site-reviews');
107
        }
108
        return __('No one has reviewed this person yet.', 'site-reviews');
109
    }
110
111
    /**
112
     * @action um_profile_content_user_reviews
113
     */
114
    public function renderReviewsTab(): void
115
    {
116
        if (!$this->hasVisibilityPermission()) {
117
            return;
118
        }
119
        glsr(Template::class)->render('templates/ultimatemember/reviews', [
120
            'context' => [
121
                'form' => $this->shortcodeForm(),
122
                'reviews' => $this->shortcodeReviews(),
123
                'summary' => $this->shortcodeSummary(),
124
            ],
125
        ]);
126
    }
127
128
    protected function hasVisibilityPermission(): bool
129
    {
130
        if (!glsr_get_option('integrations.ultimatemember.display_reviews_tab', false, 'bool')) {
131
            return false;
132
        }
133
        if (!um_is_core_page('user')) {
0 ignored issues
show
Bug introduced by
The function um_is_core_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

133
        if (!/** @scrutinizer ignore-call */ um_is_core_page('user')) {
Loading history...
134
            return false;
135
        }
136
        $roles = glsr_get_option('integrations.ultimatemember.reviews_tab_roles');
137
        $visibility = glsr_get_option('integrations.ultimatemember.reviews_tab_visibility');
138
        $user = wp_get_current_user();
139
        $userHasRole = !empty(array_intersect($roles, (array) $user->roles));
140
        $userIsOwner = $user->ID === um_get_requested_user();
0 ignored issues
show
Bug introduced by
The function um_get_requested_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

140
        $userIsOwner = $user->ID === /** @scrutinizer ignore-call */ um_get_requested_user();
Loading history...
141
        if ('guest' === $visibility && $user->ID) {
142
            return false;
143
        }
144
        if ('member' === $visibility && !$user->ID) {
145
            return false;
146
        }
147
        if ('owner' === $visibility && !$userIsOwner) {
148
            return false;
149
        }
150
        if ('roles' === $visibility && !$userHasRole) {
151
            return false;
152
        }
153
        if ('owner_roles' === $visibility && !$userIsOwner && !$userHasRole) {
154
            return false;
155
        }
156
        return true;
157
    }
158
159
    protected function shortcodeForm(): string
160
    {
161
        if (!is_user_logged_in()) {
162
            $text = sprintf(__('You must be %s to review this person.', 'site-reviews'), glsr(SiteReviewsFormShortcode::class)->loginLink());
163
            return glsr(Template::class)->build('templates/login-register', [
164
                'context' => compact('text'),
165
            ]);
166
        } else {
167
            $ratings = glsr_get_ratings([
168
                'assigned_users' => um_get_requested_user(),
0 ignored issues
show
Bug introduced by
The function um_get_requested_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

168
                'assigned_users' => /** @scrutinizer ignore-call */ um_get_requested_user(),
Loading history...
169
                'status' => 'all',
170
                'user__in' => get_current_user_id(),
171
            ]);
172
            if (0 < $ratings->reviews) {
173
                return '';
174
            }
175
        }
176
        return do_shortcode(glsr_get_option('integrations.ultimatemember.form'));
177
    }
178
179
    protected function shortcodeReviews(): string
180
    {
181
        add_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20, 2);
182
        add_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20, 2);
183
        $shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.reviews'));
184
        remove_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20);
185
        remove_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20);
186
        return $shortcode;
187
    }
188
189
    protected function shortcodeSummary(): string
190
    {
191
        add_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20, 2);
192
        add_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20, 2);
193
        add_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20, 2);
194
        $shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.summary'));
195
        remove_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20);
196
        remove_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20);
197
        remove_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20);
198
        return $shortcode;
199
    }
200
}
201