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

Review::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Database\Query;
7
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
8
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
use GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews as SiteReviewsPartial;
11
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml;
12
13
/**
14
 * @property array $assigned_post_ids
15
 * @property array $assigned_term_ids
16
 * @property array $assigned_user_ids
17
 * @property string $author;
18
 * @property int $author_id
19
 * @property string $avatar;
20
 * @property string $content
21
 * @property Arguments $custom
22
 * @property string $date
23
 * @property string $email
24
 * @property int $ID
25
 * @property string $ip_address
26
 * @property bool $is_approved
27
 * @property bool $is_modified
28
 * @property bool $is_pinned
29
 * @property int $rating
30
 * @property int $rating_id
31
 * @property string $response
32
 * @property string $status
33
 * @property string $title
34
 * @property string $type
35
 * @property string $url
36
 */
37
class Review extends Arguments
38
{
39
    /**
40
     * @var Arguments
41
     */
42
    protected $_meta;
43
44
    /**
45
     * @var \WP_Post
46
     */
47
    protected $_post;
48
49
    /**
50
     * @var object
51
     */
52
    protected $_review;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $hasCheckedModified;
58
59
    /**
60
     * @var int
61
     */
62
    protected $id;
63
64
    /**
65
     * @param array|object $values
66
     */
67
    public function __construct($values)
68
    {
69
        $values = glsr()->args($values);
70
        $this->id = Helper::castToInt($values->review_id);
71
        $args = [];
72
        $args['assigned_post_ids'] = Arr::uniqueInt(explode(',', $values->post_ids));
73
        $args['assigned_term_ids'] = Arr::uniqueInt(explode(',', $values->term_ids));
74
        $args['assigned_user_ids'] = Arr::uniqueInt(explode(',', $values->user_ids));
75
        $args['author'] = $values->name;
76
        $args['author_id'] = Helper::castToInt($values->author_id);
77
        $args['avatar'] = $values->avatar;
78
        $args['content'] = $values->content;
79
        $args['custom'] = new Arguments($this->meta()->custom);
80
        $args['date'] = $values->date;
81
        $args['email'] = $values->email;
82
        $args['ID'] = $this->id;
83
        $args['ip_address'] = $values->ip_address;
84
        $args['is_approved'] = Helper::castToBool($values->is_approved);
85
        $args['is_modified'] = false;
86
        $args['is_pinned'] = Helper::castToBool($values->is_pinned);
87
        $args['rating'] = Helper::castToInt($values->rating);
88
        $args['rating_id'] = Helper::castToInt($values->ID);
89
        $args['response'] = $this->meta()->response;
90
        $args['status'] = $values->status;
91
        $args['title'] = $values->title;
92
        $args['type'] = $values->type;
93
        $args['url'] = $values->url;
94
        parent::__construct($args);
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function __toString()
101
    {
102
        return (string) $this->build();
103
    }
104
105
    /**
106
     * @return ReviewHtml
107
     */
108
    public function build(array $args = [])
109
    {
110
        if (empty($this->id)) {
111
            return new ReviewHtml($this);
112
        }
113
        $partial = glsr(SiteReviewsPartial::class);
114
        $partial->args = glsr(SiteReviewsDefaults::class)->merge($args);
115
        $partial->options = Arr::flatten(glsr(OptionManager::class)->all());
116
        return $partial->buildReview($this);
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function date()
123
    {
124
        return get_date_from_gmt($this->get('date'), 'F j, Y');
125
    }
126
127
    /**
128
     * @param int|\WP_Post $postId
129
     * @return bool
130
     */
131
    public static function isEditable($postId)
132
    {
133
        $post = get_post($postId);
134
        return static::isReview($post)
135
            && post_type_supports(glsr()->post_type, 'title')
136
            && 'local' === glsr(Query::class)->review($post->ID)->type;
137
    }
138
139
    /**
140
     * @param int|\WP_Post $postId
141
     * @return bool
142
     */
143
    public static function isReview($postId)
144
    {
145
        return glsr()->post_type === get_post_type($postId);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isValid()
152
    {
153
        return !empty($this->ID);
154
    }
155
156
    /**
157
     * @return Arguments
158
     */
159
    public function meta()
160
    {
161
        if (!$this->_meta instanceof Arguments) {
0 ignored issues
show
introduced by
$this->_meta is always a sub-type of GeminiLabs\SiteReviews\Arguments.
Loading history...
162
            $meta = Arr::consolidate(get_post_meta($this->id));
163
            $meta = array_map('array_shift', array_filter($meta));
164
            $meta = Arr::unprefixKeys(array_filter($meta, 'strlen'));
165
            $meta = array_map('maybe_unserialize', $meta);
166
            $meta = glsr(CreateReviewDefaults::class)->restrict($meta);
167
            $this->_meta = new Arguments($meta);
168
        }
169
        return $this->_meta;
170
    }
171
172
    /**
173
     * @param mixed $key
174
     * @return bool
175
     */
176
    public function offsetExists($key)
177
    {
178
        return parent::offsetExists($key) || !is_null($this->custom->$key);
179
    }
180
181
    /**
182
     * @param mixed $key
183
     * @return mixed
184
     */
185
    public function offsetGet($key)
186
    {
187
        $alternateKeys = [
188
            'approved' => 'is_approved',
189
            'modified' => 'is_modified',
190
            'pinned' => 'is_pinned',
191
            'user_id' => 'author_id',
192
        ];
193
        if (array_key_exists($key, $alternateKeys)) {
194
            return $this->offsetGet($alternateKeys[$key]);
195
        }
196
        if ('is_modified' === $key) {
197
            return $this->isModified();
198
        }
199
        if (is_null($value = parent::offsetGet($key))) {
200
            return $this->custom->$key;
201
        }
202
        return $value;
203
    }
204
205
    /**
206
     * @param mixed $key
207
     * @return void
208
     */
209
    public function offsetSet($key, $value)
210
    {
211
        // This class is read-only
212
    }
213
214
    /**
215
     * @param mixed $key
216
     * @return void
217
     */
218
    public function offsetUnset($key)
219
    {
220
        // This class is read-only
221
    }
222
223
    /**
224
     * @return \WP_Post|null
225
     */
226
    public function post()
227
    {
228
        if (!$this->_post instanceof \WP_Post) {
0 ignored issues
show
introduced by
$this->_post is always a sub-type of WP_Post.
Loading history...
229
            $this->_post = get_post($this->id);
230
        }
231
        return $this->_post;
232
    }
233
234
    /**
235
     * @return void
236
     */
237
    public function render()
238
    {
239
        echo $this->build();
240
    }
241
242
    /**
243
     * @return string
244
     */
245
    public function rating()
246
    {
247
        return glsr_star_rating($this->get('rating'));
248
    }
249
250
    /**
251
     * @return string
252
     */
253
    public function type()
254
    {
255
        $type = $this->get('type');
256
        return array_key_exists($type, glsr()->reviewTypes)
257
            ? glsr()->reviewTypes[$type]
258
            : _x('Unknown', 'admin-text', 'site-reviews');
259
    }
260
261
    /**
262
     * @return bool
263
     */
264
    protected function isModified()
265
    {
266
        if (!$this->hasCheckedModified) {
267
            $modified = glsr(Query::class)->hasRevisions($this->ID);
268
            $this->set('is_modified', $modified);
269
            $this->hasCheckedModified = true;
270
        }
271
        return $this->get('is_modified');
272
    }
273
}
274