Test Failed
Push — main ( 5ad595...61d11b )
by Paul
06:26
created

SendVerificationEmail::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Email;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Html\TemplateTags;
11
use GeminiLabs\SiteReviews\Modules\Notice;
12
use GeminiLabs\SiteReviews\Review;
13
14
class SendVerificationEmail extends AbstractCommand
15
{
16
    /** @var Review */
17
    public $review;
18 24
    public $verifyUrl;
19
20 24
    public function __construct(Review $review, string $verifyUrl)
21 24
    {
22
        $this->review = $review;
23
        $this->verifyUrl = $verifyUrl;
24
    }
25
26
    public function handle(): void
27 24
    {
28
        if (!$this->review->isValid()) {
29 24
            glsr(Notice::class)->addError(
30 24
                _x('The email could not be sent because the review is invalid.', 'admin-text', 'site-reviews')
31
            );
32
            $this->fail();
33
            return;
34
        }
35
        if (!glsr(OptionManager::class)->getBool('settings.general.request_verification', false)) {
36
            glsr(Notice::class)->addError(
37
                _x('The email could not be sent because the Request Verification setting is disabled.', 'admin-text', 'site-reviews')
38
            );
39
            $this->fail();
40
            return;
41
        }
42
        $recipient = $this->review->email ?: Arr::get($this->review->user(), 'data.user_email');
43
        if (empty($recipient)) {
44
            glsr(Notice::class)->addError(
45
                _x('The email could not be sent because the review does not have a valid email.', 'admin-text', 'site-reviews')
46
            );
47
            $this->fail();
48
            return;
49
        }
50
        $email = glsr(Email::class)->compose($this->buildEmail($recipient), [
51
            'review' => $this->review,
52
        ]);
53
        if (!$email->send()) {
54
            glsr(Notice::class)->addError(
55
                sprintf(_x('The email could not be sent, check the <a href="%s">Site Reviews &rarr; Tools &rarr; Console</a> page for errors.', 'admin-text', 'site-reviews'),
56
                    glsr_admin_url('tools', 'console')
57
                )
58
            );
59
            $this->fail();
60
            return;
61
        }
62
        glsr(Database::class)->metaSet($this->review->ID, 'verified_requested', 1);
63
        glsr(Notice::class)->addSuccess(
64
            _x('The verification request email was sent.', 'admin-text', 'site-reviews')
65
        );
66
    }
67
68
    public function response(): array
69
    {
70
        return [
71
            'notices' => glsr(Notice::class)->get(),
72
            'text' => esc_html_x('Resend Verification Request', 'admin-text', 'site-reviews'),
73
        ];
74
    }
75
76
    protected function buildEmail(string $recipient): array
77
    {
78
        $context = glsr(TemplateTags::class)->tags($this->review, [
79
            'include' => [
80
                'review_assigned_links',
81
                'review_assigned_posts',
82
                'review_assigned_terms',
83
                'review_assigned_users',
84
                'review_author',
85
                'review_categories',
86
                'review_content',
87
                'review_email',
88
                'review_rating',
89
                'review_title',
90
                'site_title',
91
                'site_url',
92
            ],
93
        ]);
94
        $context['verify_url'] = $this->verifyUrl; // use the provided verify_url with the redirect path
95
        $template = trim(glsr(OptionManager::class)->get('settings.general.request_verification_message'));
96
        if (!empty($template)) {
97
            $templatePathForHook = 'request_verification_message';
98
            $message = glsr(Template::class)->interpolate($template, $templatePathForHook, compact('context'));
99
        } else {
100
            glsr_log()->error('The request_verification_message setting is missing.');
101
        }
102
        return [
103
            'to' => $recipient,
104
            'subject' => __('Please verify your review', 'site-reviews'),
105
            'message' => $message ?? '',
106
        ];
107
    }
108
}
109