Test Failed
Push — develop ( 6a684d...5c54e5 )
by Paul
08:51
created

AccountController::hasVisibilityPermission()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\ProfilePress\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
     * @param array $tabs
14
     *
15
     * @filter ppress_myaccount_tabs
16
     */
17
    public function filterAccountTabs($tabs): array
18
    {
19
        $tabs = Arr::consolidate($tabs);
20
        if (!$this->hasVisibilityPermission()) {
21
            return $tabs;
22
        }
23
        $tabs['reviews'] = [
24
            'title' => esc_html_x('My Reviews', 'admin-text', 'site-reviews'),
25
            'priority' => 10,
26
            'icon' => 'star',
27
            'endpoint' => 'reviews',
28
            'callback' => [$this, 'renderAccountReviews'],
29
        ];
30
        return $tabs;
31
    }
32
33
    /**
34
     * @see filterAccountTabs
35
     */
36
    public function renderAccountReviews(): void
37
    {
38
        if (!$this->hasVisibilityPermission()) {
39
            return;
40
        }
41
        glsr(Template::class)->render('templates/profilepress/account-reviews', [
42
            'context' => [
43
                'reviews' => $this->shortcodeReviews(),
44
            ],
45
        ]);
46
    }
47
48
    /**
49
     * @see $this->shortcodeReviews()
50
     *
51
     * @filter site-reviews/reviews/fallback
52
     */
53
    public function filterReviewsFallback(string $fallback, array $args): string
54
    {
55
        if ($args['fallback'] !== __('There are no reviews yet. Be the first one to write one.', 'site-reviews')) {
56
            return $fallback;
57
        }
58
        if (!$this->hasVisibilityPermission()) {
59
            return $fallback;
60
        }
61
        return glsr(Builder::class)->p([
62
            'class' => 'profilepress-myaccount-alert pp-alert-danger',
63
            'text' => esc_html__('You have not written any reviews.', 'site-reviews'),
64
        ]);
65
    }
66
67
    protected function hasVisibilityPermission(): bool
68
    {
69
        if (!glsr_get_option('integrations.profilepress.display_account_tab', false, 'bool')) {
70
            return false;
71
        }
72
        if (!is_user_logged_in()) {
73
            return false;
74
        }
75
        $roles = glsr_get_option('integrations.profilepress.account_tab_roles');
76
        $visibility = glsr_get_option('integrations.profilepress.account_tab_visibility');
77
        $user = wp_get_current_user();
78
        $userHasRole = !empty(array_intersect($roles, (array) $user->roles));
79
        if ('roles' === $visibility && !$userHasRole) {
80
            return false;
81
        }
82
        return true;
83
    }
84
85
    protected function shortcodeReviews(): string
86
    {
87
        if (!$this->hasVisibilityPermission()) {
88
            return '';
89
        }
90
        add_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20, 2);
91
        $shortcode = do_shortcode(glsr_get_option('integrations.profilepress.account_tab_reviews'));
92
        remove_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20);
93
        return $shortcode;
94
    }
95
}
96