Passed
Push — master ( ce4978...7a447f )
by Paul
07:31
created

SiteReviewsFormShortcode::getClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Modules\Html\Builder;
7
use GeminiLabs\SiteReviews\Modules\Html\Template;
8
use GeminiLabs\SiteReviews\Modules\Style;
9
10
class SiteReviewsFormShortcode extends Shortcode
11
{
12
    /**
13
     * @var array
14
     */
15
    public $args;
16
17
    /**
18
     * @var \GeminiLabs\SiteReviews\Arguments
19
     */
20
    protected $with;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function buildTemplate(array $args = [])
26
    {
27
        if (!is_user_logged_in() && glsr_get_option('general.require.login', false, 'bool')) {
28
            return $this->loginOrRegister();
29
        }
30
        $this->args = $args;
31
        $this->with = $this->with();
32
        return glsr(Template::class)->build('templates/reviews-form', [
33
            'args' => $args,
34
            'context' => [
35
                'class' => $this->getClasses(),
36
                'fields' => $this->buildTemplateTag('fields'),
37
                'id' => '', // @deprecated in v5.0
38
                'response' => $this->buildTemplateTag('response'),
39
                'submit_button' => $this->buildTemplateTag('submit_button'),
40
            ],
41
        ]);
42
    }
43
44
    /**
45
     * @param string $tag
46
     * @return false|string
47
     */
48
    protected function buildTemplateTag($tag)
49
    {
50
        $args = $this->args;
51
        $classname = implode('-', ['form', $tag, 'tag']);
52
        $className = Helper::buildClassName($classname, 'Modules\Html\Tags');
53
        $field = class_exists($className)
54
            ? glsr($className, compact('tag', 'args'))->handleFor('form', null, $this->with)
55
            : null;
56
        return glsr()->filterString('form/build/'.$tag, $field, $this->with, $this);
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    protected function getClasses()
63
    {
64
        $classes = [
65
            'glsr-review-form',
66
            glsr(Style::class)->classes('form'),
67
            $this->args['class'],
68
        ];
69
        if (!empty($this->with->errors)) {
70
            $classes[] = glsr(Style::class)->validation('form_error');
71
        }
72
        return trim(implode(' ', array_filter($classes)));
73
    }
74
75
    /**
76
     * @return array
77
     */
78 7
    protected function hideOptions()
79
    {
80
        return [
81 7
            'rating' => _x('Hide the rating field', 'admin-text', 'site-reviews'),
82 7
            'title' => _x('Hide the title field', 'admin-text', 'site-reviews'),
83 7
            'content' => _x('Hide the review field', 'admin-text', 'site-reviews'),
84 7
            'name' => _x('Hide the name field', 'admin-text', 'site-reviews'),
85 7
            'email' => _x('Hide the email field', 'admin-text', 'site-reviews'),
86 7
            'terms' => _x('Hide the terms field', 'admin-text', 'site-reviews'),
87
        ];
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    protected function loginOrRegister()
94
    {
95
        return glsr(Template::class)->build('templates/login-register', [
96
            'context' => [
97
                'text' => trim($this->loginText().' '.$this->registerText()),
98
            ],
99
        ]);
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    protected function loginText()
106
    {
107
        $loginLink = glsr(Builder::class)->a([
108
            'href' => wp_login_url(strval(get_permalink())),
109
            'text' => __('logged in', 'site-reviews'),
110
        ]);
111
        return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink);
112
    }
113
114
    /**
115
     * @return void|string
116
     */
117
    protected function registerText()
118
    {
119
        if (get_option('users_can_register') && glsr_get_option('general.require.login', false, 'bool')) {
120
            $registerLink = glsr(Builder::class)->a([
121
                'href' => wp_registration_url(),
122
                'text' => __('register', 'site-reviews'),
123
            ]);
124
            return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink);
125
        }
126
    }
127
128
    /**
129
     * @return \GeminiLabs\SiteReviews\Arguments
130
     */
131
    protected function with()
132
    {
133
        return glsr()->args([
134
            'errors' => glsr()->sessionGet($this->args['id'].'errors', []),
135
            'message' => glsr()->sessionGet($this->args['id'].'message', ''),
136
            'required' => glsr_get_option('submissions.required', []),
137
            'values' => glsr()->sessionGet($this->args['id'].'values', []),
138
        ]);
139
    }
140
}
141