Test Failed
Push — develop ( baed09...43d041 )
by Paul
08:56
created

AccountController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
eloc 38
c 1
b 0
f 1
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filterReviewsFallback() 0 10 3
A filterAccountContent() 0 8 2
A hasVisibilityPermission() 0 16 5
A filterAccountTabs() 0 13 2
A shortcodeReviews() 0 9 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\UltimateMember\Controllers;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
10
class AccountController extends AbstractController
11
{
12
    /**
13
     * @filter um_account_content_hook_reviews
14
     */
15
    public function filterAccountContent(): ?string
16
    {
17
        if (!$this->hasVisibilityPermission()) {
18
            return null;
19
        }
20
        return glsr(Template::class)->build('templates/ultimatemember/account-reviews', [
21
            'context' => [
22
                'reviews' => $this->shortcodeReviews(),
23
            ],
24
        ]);
25
    }
26
27
    /**
28
     * @param array $tabs
29
     *
30
     * @filter um_account_page_default_tabs_hook
31
     */
32
    public function filterAccountTabs($tabs): array
33
    {
34
        $tabs = Arr::consolidate($tabs);
35
        if (!$this->hasVisibilityPermission()) {
36
            return $tabs;
37
        }
38
        $tabs[500]['reviews'] = [
39
            'custom' => 'true',
40
            'icon' => 'um-faicon-star',
41
            'show_button' => false,
42
            'title' => esc_html_x('My Reviews', 'admin-text', 'site-reviews'),
43
        ];
44
        return $tabs;
45
    }
46
47
    /**
48
     * @see $this->shortcodeReviews()
49
     *
50
     * @filter site-reviews/reviews/fallback
51
     */
52
    public function filterReviewsFallback(string $fallback, array $args): string
53
    {
54
        if ($args['fallback'] !== __('There are no reviews yet. Be the first one to write one.', 'site-reviews')) {
55
            return $fallback;
56
        }
57
        if (!$this->hasVisibilityPermission()) {
58
            return $fallback;
59
        }
60
        return wpautop(
61
            esc_html__('You have not written any reviews.', 'site-reviews')
62
        );
63
    }
64
65
    protected function hasVisibilityPermission(): bool
66
    {
67
        if (!glsr_get_option('integrations.ultimatemember.display_account_tab', false, 'bool')) {
68
            return false;
69
        }
70
        if (!is_user_logged_in()) {
71
            return false;
72
        }
73
        $roles = glsr_get_option('integrations.ultimatemember.account_tab_roles');
74
        $visibility = glsr_get_option('integrations.ultimatemember.account_tab_visibility');
75
        $user = wp_get_current_user();
76
        $userHasRole = !empty(array_intersect($roles, (array) $user->roles));
77
        if ('roles' === $visibility && !$userHasRole) {
78
            return false;
79
        }
80
        return true;
81
    }
82
83
    protected function shortcodeReviews(): string
84
    {
85
        if (!$this->hasVisibilityPermission()) {
86
            return '';
87
        }
88
        add_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20, 2);
89
        $shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.account_tab_reviews'));
90
        remove_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20);
91
        return $shortcode;
92
    }
93
}
94