Passed
Push — master ( 817cab...57b58d )
by Paul
09:30 queued 02:21
created

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