Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

Review   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Test Coverage

Coverage 36.36%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 37
eloc 87
c 5
b 0
f 0
dl 0
loc 283
ccs 36
cts 99
cp 0.3636
rs 9.44

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __toString() 0 3 1
A assignedPosts() 0 9 2
A offsetSet() 0 9 2
A rating() 0 3 1
A meta() 0 10 2
A isEditable() 0 6 3
A render() 0 3 1
A custom() 0 8 1
A offsetExists() 0 3 2
A assignedUsers() 0 8 2
A offsetUnset() 0 2 1
A post() 0 6 2
A build() 0 3 1
A type() 0 5 1
A assignedTerms() 0 10 3
A date() 0 3 1
A isValid() 0 3 2
A offsetGet() 0 20 4
A avatar() 0 3 1
A isReview() 0 3 1
A hasRevisions() 0 8 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Query;
6
use GeminiLabs\SiteReviews\Defaults\ReviewDefaults;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Modules\Avatar;
11
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml;
12
13
/**
14
 * @property array $assigned_posts
15
 * @property array $assigned_terms
16
 * @property array $assigned_users
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 $name  This property is mapped to $author
24
 * @property string $email
25
 * @property int $ID
26
 * @property string $ip_address
27
 * @property bool $is_approved
28
 * @property bool $is_modified
29
 * @property bool $is_pinned
30
 * @property int $rating
31
 * @property int $rating_id
32
 * @property string $response
33
 * @property string $status
34
 * @property string $title
35
 * @property string $type
36
 * @property string $url
37
 */
38
class Review extends Arguments
39
{
40
    /**
41
     * @var Arguments
42
     */
43
    protected $_meta;
44
45
    /**
46
     * @var \WP_Post
47
     */
48
    protected $_post;
49
50
    /**
51
     * @var object
52
     */
53
    protected $_review;
54
55
    /**
56
     * @var bool
57
     */
58
    protected $has_checked_revisions;
59
60
    /**
61
     * @var int
62
     */
63
    protected $id;
64
65
    /**
66
     * @param array|object $values
67
     */
68 13
    public function __construct($values)
69
    {
70 13
        $values = glsr()->args($values);
71 13
        $this->id = Cast::toInt($values->review_id);
72 13
        $args = glsr(ReviewDefaults::class)->restrict($values->toArray());
73 13
        $args['custom'] = $this->custom();
74 13
        $args['ID'] = $this->id;
75 13
        $args['response'] = $this->meta()->_response;
76 13
        parent::__construct($args);
77 13
    }
78
79
    /**
80
     * @return string
81
     */
82 6
    public function __toString()
83
    {
84 6
        return (string) $this->build();
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function assignedPosts()
91
    {
92
        if (empty($this->assigned_posts)) {
93
            return $this->assigned_posts;
94
        }
95
        return get_posts([
0 ignored issues
show
Bug introduced by
The function get_posts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        return /** @scrutinizer ignore-call */ get_posts([
Loading history...
96
            'post__in' => $this->assigned_posts,
97
            'post_type' => 'any',
98
            'posts_per_page' => -1,
99
        ]);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function assignedTerms()
106
    {
107
        if (empty($this->assigned_terms)) {
108
            return $this->assigned_terms;
109
        }
110
        $terms = get_terms(glsr()->taxonomy, ['include' => $this->assigned_terms]);
0 ignored issues
show
Bug introduced by
The function get_terms was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        $terms = /** @scrutinizer ignore-call */ get_terms(glsr()->taxonomy, ['include' => $this->assigned_terms]);
Loading history...
111
        if (is_wp_error($terms)) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        if (/** @scrutinizer ignore-call */ is_wp_error($terms)) {
Loading history...
112
            return $this->assigned_terms;
113
        }
114
        return $terms;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function assignedUsers()
121
    {
122
        if (empty($this->assigned_users)) {
123
            return $this->assigned_users;
124
        }
125
        return get_users([
0 ignored issues
show
Bug introduced by
The function get_users was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
        return /** @scrutinizer ignore-call */ get_users([
Loading history...
126
            'fields' => ['display_name', 'ID', 'user_email', 'user_nicename', 'user_url'],
127
            'include' => $this->assigned_users,
128
        ]);
129
    }
130
131
    /**
132
     * @param int $size
133
     * @return string
134
     */
135
    public function avatar($size = null)
136
    {
137
        return glsr(Avatar::class)->img($this->get('avatar'), $size);
138
    }
139
140
    /**
141
     * @return ReviewHtml
142
     */
143 6
    public function build(array $args = [])
144
    {
145 6
        return new ReviewHtml($this, $args);
146
    }
147
148
    /**
149
     * @return Arguments
150
     */
151 13
    public function custom()
152
    {
153
        $custom = array_filter($this->meta()->toArray(), function ($key) {
154
            return Str::startsWith('_custom', $key);
155 13
        }, ARRAY_FILTER_USE_KEY);
156 13
        $custom = Arr::unprefixKeys($custom, '_custom_');
157 13
        $custom = Arr::unprefixKeys($custom, '_');
158 13
        return glsr()->args($custom);
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function date($format = 'F j, Y')
165
    {
166
        return get_date_from_gmt($this->get('date'), $format);
0 ignored issues
show
Bug introduced by
The function get_date_from_gmt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
        return /** @scrutinizer ignore-call */ get_date_from_gmt($this->get('date'), $format);
Loading history...
167
    }
168
169
    /**
170
     * @param int|\WP_Post $post
171
     * @return bool
172
     */
173
    public static function isEditable($post)
174
    {
175
        $postId = Helper::getPostId($post);
176
        return static::isReview($postId)
177
            && post_type_supports(glsr()->post_type, 'title')
0 ignored issues
show
Bug introduced by
The function post_type_supports was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
            && /** @scrutinizer ignore-call */ post_type_supports(glsr()->post_type, 'title')
Loading history...
178
            && 'local' === glsr(Query::class)->review($postId)->type;
179
    }
180
181
    /**
182
     * @param \WP_Post|int|false $post
183
     * @return bool
184
     */
185 12
    public static function isReview($post)
186
    {
187 12
        return glsr()->post_type === get_post_type($post);
0 ignored issues
show
Bug introduced by
The function get_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
        return glsr()->post_type === /** @scrutinizer ignore-call */ get_post_type($post);
Loading history...
188
    }
189
190
    /**
191
     * @return bool
192
     */
193 13
    public function isValid()
194
    {
195 13
        return !empty($this->id) && !empty($this->get('rating_id'));
196
    }
197
198
    /**
199
     * @return Arguments
200
     */
201 13
    public function meta()
202
    {
203 13
        if (!$this->_meta instanceof Arguments) {
204 13
            $meta = Arr::consolidate(get_post_meta($this->id));
0 ignored issues
show
Bug introduced by
The function get_post_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
            $meta = Arr::consolidate(/** @scrutinizer ignore-call */ get_post_meta($this->id));
Loading history...
205 13
            $meta = array_map('array_shift', array_filter($meta));
206 13
            $meta = array_filter($meta, 'strlen');
207 13
            $meta = array_map('maybe_unserialize', $meta);
208 13
            $this->_meta = glsr()->args($meta);
209
        }
210 13
        return $this->_meta;
211
    }
212
213
    /**
214
     * @param mixed $key
215
     * @return bool
216
     */
217
    public function offsetExists($key)
218
    {
219
        return parent::offsetExists($key) || !is_null($this->custom()->$key);
220
    }
221
222
    /**
223
     * @param mixed $key
224
     * @return mixed
225
     */
226 13
    public function offsetGet($key)
227
    {
228
        $alternateKeys = [
229 13
            'approved' => 'is_approved',
230
            'has_revisions' => 'is_modified',
231
            'modified' => 'is_modified',
232
            'name' => 'author',
233
            'pinned' => 'is_pinned',
234
            'user_id' => 'author_id',
235
        ];
236 13
        if (array_key_exists($key, $alternateKeys)) {
237
            return $this->offsetGet($alternateKeys[$key]);
238
        }
239 13
        if ('is_modified' === $key) {
240
            return $this->hasRevisions();
241
        }
242 13
        if (is_null($value = parent::offsetGet($key))) {
243
            return $this->custom()->$key;
244
        }
245 13
        return $value;
246
    }
247
248
    /**
249
     * @param mixed $key
250
     * @return void
251
     */
252
    public function offsetSet($key, $value)
253
    {
254
        // This class is read-only, except for custom fields
255
        if ('custom' === $key) {
256
            $value = Arr::consolidate($value);
257
            $value = Arr::prefixKeys($value, '_custom_');
258
            $meta = wp_parse_args($this->_meta->toArray(), $value);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
            $meta = /** @scrutinizer ignore-call */ wp_parse_args($this->_meta->toArray(), $value);
Loading history...
259
            $this->_meta = glsr()->args($meta);
260
            parent::offsetSet($key, $this->custom());
261
        }
262
    }
263
264
    /**
265
     * @param mixed $key
266
     * @return void
267
     */
268
    public function offsetUnset($key)
269
    {
270
        // This class is read-only
271
    }
272
273
    /**
274
     * @return \WP_Post|null
275
     */
276
    public function post()
277
    {
278
        if (!$this->_post instanceof \WP_Post) {
279
            $this->_post = get_post($this->id);
0 ignored issues
show
Bug introduced by
The function get_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
            $this->_post = /** @scrutinizer ignore-call */ get_post($this->id);
Loading history...
280
        }
281
        return $this->_post;
282
    }
283
284
    /**
285
     * @return void
286
     */
287
    public function render()
288
    {
289
        echo $this->build();
290
    }
291
292
    /**
293
     * @return string
294
     */
295
    public function rating()
296
    {
297
        return glsr_star_rating($this->get('rating'));
298
    }
299
300
    /**
301
     * @return string
302
     */
303
    public function type()
304
    {
305
        $type = $this->get('type');
306
        $reviewTypes = glsr()->retrieveAs('array', 'review_types');
307
        return Arr::get($reviewTypes, $type, _x('Unknown', 'admin-text', 'site-reviews'));
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
        return Arr::get($reviewTypes, $type, /** @scrutinizer ignore-call */ _x('Unknown', 'admin-text', 'site-reviews'));
Loading history...
308
    }
309
310
    /**
311
     * @return bool
312
     */
313
    protected function hasRevisions()
314
    {
315
        if (!$this->has_checked_revisions) {
316
            $modified = glsr(Query::class)->hasRevisions($this->ID);
317
            $this->set('is_modified', $modified);
318
            $this->has_checked_revisions = true;
319
        }
320
        return $this->get('is_modified');
321
    }
322
}
323