Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

ReviewController::getAssignedDiff()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Commands\CreateReview;
6
use GeminiLabs\SiteReviews\Commands\ToggleStatus;
7
use GeminiLabs\SiteReviews\Database\Query;
8
use GeminiLabs\SiteReviews\Database\ReviewManager;
9
use GeminiLabs\SiteReviews\Review;
10
use WP_Post;
11
12
class ReviewController extends Controller
13
{
14
    /**
15
     * @return void
16
     * @action admin_action_approve
17
     */
18
    public function approve()
19
    {
20
        if (glsr()->id == filter_input(INPUT_GET, 'plugin')) {
21
            check_admin_referer('approve-review_'.($postId = $this->getPostId()));
22
            $this->execute(new ToggleStatus($postId, 'publish'));
23
            wp_safe_redirect(wp_get_referer());
24
            exit;
25
        }
26
    }
27
28
    /**
29
     * @param array $posts
30
     * @return array
31
     * @filter the_posts
32
     */
33
    public function filterPostsToCacheReviews($posts)
34
    {
35
        $reviews = array_filter($posts, function ($post) {
36
            return glsr()->post_type === $post->post_type;
37
        });
38
        if ($postIds = wp_list_pluck($reviews, 'ID')) {
39
            glsr(Query::class)->reviews([], $postIds); // this caches the associated Review objects
40
        }
41
        return $posts;
42
    }
43
44
    /**
45
     * Triggered when one or more categories are added or removed from a review.
46
     *
47
     * @param int $postId
48
     * @param array $terms
49
     * @param array $newTTIds
50
     * @param string $taxonomy
51
     * @param bool $append
52
     * @param array $oldTTIds
53
     * @return void
54
     * @action set_object_terms
55
     */
56
    public function onAfterChangeAssignedTerms($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds)
57
    {
58
        if (!Review::isReview($postId)) {
59
            return;
60
        }
61
        $diff = $this->getAssignedDiff($oldTTIds, $newTTIds);
62
        $review = glsr(Query::class)->review($postId);
63
        foreach ($diff['old'] as $termId) {
64
            glsr(ReviewManager::class)->unassignTerm($review, $termId);
65
        }
66
        foreach ($diff['new'] as $termId) {
67
            glsr(ReviewManager::class)->assignTerm($review, $termId);
68
        }
69
    }
70
71
    /**
72
     * Triggered when a review's assigned post IDs are updated.
73
     *
74
     * @return void
75
     * @action site-reviews/review/updated/post_ids
76
     */
77
    public function onChangeAssignedPosts(Review $review, array $postIds = [])
78
    {
79
        $diff = $this->getAssignedDiff($review->assigned_post_ids, $postIds);
80
        foreach ($diff['old'] as $postId) {
81
            glsr(ReviewManager::class)->unassignPost($review, $postId);
82
        }
83
        foreach ($diff['new'] as $postId) {
84
            glsr(ReviewManager::class)->assignPost($review, $postId);
85
        }
86
    }
87
88
    /**
89
     * Triggered when a review's assigned users IDs are updated.
90
     *
91
     * @return void
92
     * @action site-reviews/review/updated/user_ids
93
     */
94
    public function onChangeAssignedUsers(Review $review, array $userIds = [])
95
    {
96
        $diff = $this->getAssignedDiff($review->assigned_user_ids, $userIds);
97
        foreach ($diff['old'] as $userId) {
98
            glsr(ReviewManager::class)->unassignUser($review, $userId);
99
        }
100
        foreach ($diff['new'] as $userId) {
101
            glsr(ReviewManager::class)->assignUser($review, $userId);
102
        }
103
    }
104
105
    /**
106
     * @return void
107
     * @action admin_action_unapprove
108
     */
109
    public function unapprove()
110
    {
111
        if (glsr()->id == filter_input(INPUT_GET, 'plugin')) {
112
            check_admin_referer('unapprove-review_'.($postId = $this->getPostId()));
113
            $this->execute(new ToggleStatus($postId, 'pending'));
114
            wp_safe_redirect(wp_get_referer());
115
            exit;
116
        }
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    protected function getAssignedDiff(array $existing, array $replacements)
123
    {
124
        sort($existing);
125
        sort($replacements);
126
        $new = $old = [];
127
        if ($existing !== $replacements) {
128
            $ignored = array_intersect($existing, $replacements);
129
            $new = array_diff($replacements, $ignored);
130
            $old = array_diff($existing, $ignored);
131
        }
132
        return [
133
            'new' => $new,
134
            'old' => $old,
135
        ];
136
    }
137
138
    public function onAfterChangeAssignedUsers($userIds)
139
    {
140
    }
141
142
    /**
143
     * Triggered when a post status changes or when a review is approved|unapproved|trashed.
144
     *
145
     * @param string $oldStatus
146
     * @param string $newStatus
147
     * @param \WP_Post $post
148
     * @return void
149
     * @action transition_post_status
150
     */
151
    public function onAfterChangeStatus($newStatus, $oldStatus, $post)
152
    {
153
        if (in_array($oldStatus, ['new', $newStatus])) {
154
            return;
155
        }
156
        $postType = get_post_type($post);
157
        if (glsr()->post_type === $postType) {
158
            glsr(ReviewManager::class)->update($post->ID, [
159
                'is_approved' => 'publish' === $newStatus,
160
            ]);
161
        } else {
162
            glsr(ReviewManager::class)->updateAssignedPost($post->ID, [
163
                'is_published' => 'publish' === $newStatus,
164
            ]);
165
        }
166
    }
167
168
    /**
169
     * Triggered when a review is first created.
170
     *
171
     * @return void
172
     * @action site-reviews/review/creating
173
     * @todo fix $command->review_type
174
     */
175
    public function onAfterCreate(WP_Post $post, CreateReview $command)
176
    {
177
        glsr(ReviewManager::class)->insert($post->ID, [
178
            'is_approved' => 'publish' === $post->status,
179
            'rating' => $command->rating,
180
            'type' => $command->review_type,
0 ignored issues
show
Bug introduced by
The property review_type does not seem to exist on GeminiLabs\SiteReviews\Commands\CreateReview.
Loading history...
181
        ]);
182
    }
183
}
184