Passed
Push — master ( 2c1b34...eeeeb5 )
by Paul
04:03
created

TrustalyzeController::onSaved()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use GeminiLabs\SiteReviews\Modules\Notice;
10
use GeminiLabs\SiteReviews\Modules\Trustalyze;
11
use GeminiLabs\SiteReviews\Review;
12
13
class TrustalyzeController extends Controller
14
{
15
    protected $apiKey = 'settings.general.trustalyze_serial';
16
    protected $emailKey = 'settings.general.trustalyze_email';
17
    protected $enabledKey = 'settings.general.trustalyze';
18
    protected $trustalyzeKey = '_glsr_trustalyze';
19
20
    /**
21
     * @return array
22
     * @filter site-reviews/settings/callback
23
     */
24
    public function filterSettingsCallback(array $settings)
25
    {
26
        if ('yes' !== Arr::get($settings, $this->enabledKey)) {
27
            return $settings;
28
        }
29
        $isApiKeyModified = $this->isEmptyOrModified($this->apiKey, $settings);
30
        $isEmailModified = $this->isEmptyOrModified($this->emailKey, $settings);
31
        $isAccountVerified = glsr(OptionManager::class)->getWP($this->trustalyzeKey, false);
32
        if (!$isAccountVerified || $isApiKeyModified || $isEmailModified) {
33
            $settings = $this->sanitizeTrustalyzeSettings($settings);
34
        }
35
        return $settings;
36
    }
37
38
    /**
39
     * @param string $template
40
     * @return array
41
     * @filter site-reviews/interpolate/partials/form/table-row-multiple
42
     */
43
    public function filterSettingsTableRow(array $context, $template, array $data)
44
    {
45
        if ($this->enabledKey !== Arr::get($data, 'field.path')) {
46
            return $context;
47
        }
48
        $isAccountValidated = !empty(glsr(OptionManager::class)->getWP($this->trustalyzeKey));
49
        $isIntegrationEnabled = glsr(OptionManager::class)->getBool('settings.general.trustalyze');
50
        if ($isAccountValidated && $isIntegrationEnabled) {
51
            return $context;
52
        }
53
        $context['field'].= $this->buildCreateButton();
54
        return $context;
55
    }
56
57
    /**
58
     * Triggered when a review is created.
59
     * @return void
60
     * @action site-reviews/review/created
61
     */
62 1
    public function onCreated(Review $review)
63
    {
64 1
        if (!$this->canPostReview($review)) {
65 1
            return;
66
        }
67
        $trustalyze = glsr(Trustalyze::class)->sendReview($review);
68
        if ($trustalyze->success) {
69
            glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id);
70
        }
71
    }
72
73
    /**
74
     * Triggered when a review is reverted to its original title/content/date_timestamp.
75
     * @return void
76
     * @action site-reviews/review/reverted
77
     */
78
    public function onReverted(Review $review)
79
    {
80
        if (!$this->canPostReview($review)) {
81
            return;
82
        }
83
        $trustalyze = glsr(Trustalyze::class)->sendReview($review);
84
        if ($trustalyze->success) {
85
            glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id);
86
        }
87
    }
88
89
    /**
90
     * Triggered when an existing review is updated.
91
     * @return void
92
     * @action site-reviews/review/saved
93
     */
94
    public function onSaved(Review $review)
95
    {
96
        if (!$this->canPostReview($review)) {
97
            return;
98
        }
99
        $trustalyze = glsr(Trustalyze::class)->sendReview($review);
100
        if ($trustalyze->success) {
101
            glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id);
102
        }
103
    }
104
105
    /**
106
     * Triggered when a review's response is added or updated.
107
     * @param int $metaId
108
     * @param int $postId
109
     * @param string $metaKey
110
     * @return void
111
     * @action updated_postmeta
112
     */
113
    public function onUpdatedMeta($metaId, $postId, $metaKey)
114
    {
115
        $review = glsr_get_review($postId);
116
        if (!$this->canPostResponse($review) || '_response' !== $metaKey) {
117
            return;
118
        }
119
        $trustalyze = glsr(Trustalyze::class)->sendReviewResponse($review);
120
        if ($trustalyze->success) {
121
            glsr(Database::class)->set($review->ID, 'trustalyze_response', true);
122
        }
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    protected function buildCreateButton()
129
    {
130
        return glsr(Builder::class)->a(__('Create Your Trustalyze Account', 'site-reviews'), [
131
            'class' => 'button',
132
            'href' => Trustalyze::WEB_URL,
133
            'target' => '_blank',
134
        ]);
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    protected function canPostResponse(Review $review)
141
    {
142
        $requiredValues = [
143
            glsr(Database::class)->get($review->ID, 'trustalyze'),
144
            $review->response,
145
            $review->review_id,
146
        ];
147
        return $this->canProceed($review, 'trustalyze_response')
148
            && 'publish' === $review->status
149
            && 3 === count(array_filter($requiredValues));
150
    }
151
152
    /**
153
     * @return bool
154
     */
155 1
    protected function canPostReview(Review $review)
156
    {
157
        $requiredValues = [
158 1
            $review->author,
159 1
            $review->content,
160 1
            $review->rating,
161 1
            $review->review_id,
162 1
            $review->title,
163
        ];
164 1
        return $this->canProceed($review)
165 1
            && 'publish' === $review->status
166 1
            && 5 === count(array_filter($requiredValues));
167
    }
168
169
    /**
170
     * @param string $metaKey
171
     * @return bool
172
     */
173 1
    protected function canProceed(Review $review, $metaKey = 'trustalyze')
174
    {
175 1
        return glsr(OptionManager::class)->getBool($this->enabledKey)
176 1
            && $this->isReviewPostId($review->ID)
177 1
            && !$this->hasMetaKey($review, $metaKey);
178
    }
179
180
    /**
181
     * @param string $metaKey
182
     * @return bool
183
     */
184
    protected function hasMetaKey(Review $review, $metaKey = 'trustalyze')
185
    {
186
        return '' !== glsr(Database::class)->get($review->ID, $metaKey);
187
    }
188
189
    /**
190
     * @param string $key
191
     * @return bool
192
     */
193
    protected function isEmptyOrModified($key, array $settings)
194
    {
195
        $oldValue = glsr_get_option($key);
196
        $newValue = Arr::get($settings, $key);
197
        return empty($newValue) || $newValue !== $oldValue;
198
    }
199
200
    /**
201
     * @return array
202
     */
203
    protected function sanitizeTrustalyzeSettings(array $settings)
204
    {
205
        $trustalyze = glsr(Trustalyze::class)->activateKey(
206
            Arr::get($settings, $this->apiKey),
207
            Arr::get($settings, $this->emailKey)
208
        );
209
        if ($trustalyze->success) {
210
            update_option($this->trustalyzeKey, Arr::get($trustalyze->response, 'producttype'));
211
        } else {
212
            delete_option($this->trustalyzeKey);
213
            $settings = Arr::set($settings, $this->enabledKey, 'no');
214
            glsr(Notice::class)->addError(sprintf(
215
                __('Your Trustalyze account details could not be verified, please try again. %s', 'site-reviews'),
216
                '('.$trustalyze->message.')'
217
            ));
218
        }
219
        return $settings;
220
    }
221
}
222