Passed
Push — master ( 2c1b34...eeeeb5 )
by Paul
04:03
created
plugin/Controllers/TrustalyzeController.php 1 patch
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -12,210 +12,210 @@
 block discarded – undo
12 12
 
13 13
 class TrustalyzeController extends Controller
14 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
-    public function onCreated(Review $review)
63
-    {
64
-        if (!$this->canPostReview($review)) {
65
-            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
-    protected function canPostReview(Review $review)
156
-    {
157
-        $requiredValues = [
158
-            $review->author,
159
-            $review->content,
160
-            $review->rating,
161
-            $review->review_id,
162
-            $review->title,
163
-        ];
164
-        return $this->canProceed($review)
165
-            && 'publish' === $review->status
166
-            && 5 === count(array_filter($requiredValues));
167
-    }
168
-
169
-    /**
170
-     * @param string $metaKey
171
-     * @return bool
172
-     */
173
-    protected function canProceed(Review $review, $metaKey = 'trustalyze')
174
-    {
175
-        return glsr(OptionManager::class)->getBool($this->enabledKey)
176
-            && $this->isReviewPostId($review->ID)
177
-            && !$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
-    }
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
+	public function onCreated(Review $review)
63
+	{
64
+		if (!$this->canPostReview($review)) {
65
+			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
+	protected function canPostReview(Review $review)
156
+	{
157
+		$requiredValues = [
158
+			$review->author,
159
+			$review->content,
160
+			$review->rating,
161
+			$review->review_id,
162
+			$review->title,
163
+		];
164
+		return $this->canProceed($review)
165
+			&& 'publish' === $review->status
166
+			&& 5 === count(array_filter($requiredValues));
167
+	}
168
+
169
+	/**
170
+	 * @param string $metaKey
171
+	 * @return bool
172
+	 */
173
+	protected function canProceed(Review $review, $metaKey = 'trustalyze')
174
+	{
175
+		return glsr(OptionManager::class)->getBool($this->enabledKey)
176
+			&& $this->isReviewPostId($review->ID)
177
+			&& !$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 221
 }
Please login to merge, or discard this patch.