Passed
Push — main ( a0e9c5...805967 )
by Paul
07:42
created

ApproveReview::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 23
ccs 0
cts 19
cp 0
rs 9.7333
c 1
b 0
f 0
cc 4
nc 4
nop 0
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Contracts\CommandContract as Contract;
6
use GeminiLabs\SiteReviews\Modules\Notice;
7
use GeminiLabs\SiteReviews\Review;
8
9
class ApproveReview implements Contract
10
{
11
    public $review;
12
13
    public function __construct(Review $review)
14
    {
15
        $this->review = $review;
16
    }
17
18
    /**
19
     * @return bool
20
     */
21
    public function handle()
22
    {
23
        if ($this->review->is_approved) {
24
            return false;
25
        }
26
        if (!glsr()->can('edit_post', $this->review->ID)) {
27
            glsr_log()->error('Cannot approve review: Invalid permission.');
28
            return false;
29
        }
30
        $args = [
31
            'ID' => $this->review->ID,
32
            'post_status' => 'publish',
33
        ];
34
        $postId = wp_update_post($args, true);
35
        if (is_wp_error($postId)) {
36
            glsr_log()->error($postId->get_error_message());
37
            return false;
38
        }
39
        $message = sprintf(_x('The %sreview%s was approved successfully.', 'admin-text', 'site-reviews'),
40
            sprintf('<a href="%s">', $this->review->editUrl()), '</a>'
41
        );
42
        glsr(Notice::class)->addSuccess($message);
43
        return true;
44
    }
45
}
46