1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews as SiteReviewsPartial; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml; |
10
|
|
|
use WP_Post; |
11
|
|
|
|
12
|
|
|
class Review implements \ArrayAccess |
13
|
|
|
{ |
14
|
|
|
public $assigned_to; |
15
|
|
|
public $author; |
16
|
|
|
public $avatar; |
17
|
|
|
public $content; |
18
|
|
|
public $custom; |
19
|
|
|
public $date; |
20
|
|
|
public $email; |
21
|
|
|
public $ID; |
22
|
|
|
public $ip_address; |
23
|
|
|
public $modified; |
24
|
|
|
public $pinned; |
25
|
|
|
public $rating; |
26
|
|
|
public $response; |
27
|
|
|
public $review_id; |
28
|
|
|
public $review_type; |
29
|
|
|
public $status; |
30
|
|
|
public $term_ids; |
31
|
|
|
public $title; |
32
|
|
|
public $url; |
33
|
|
|
public $user_id; |
34
|
|
|
|
35
|
1 |
|
public function __construct(WP_Post $post) |
36
|
|
|
{ |
37
|
1 |
|
if (Application::POST_TYPE != $post->post_type) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
1 |
|
$this->content = $post->post_content; |
41
|
1 |
|
$this->date = $post->post_date; |
42
|
1 |
|
$this->ID = intval($post->ID); |
43
|
1 |
|
$this->status = $post->post_status; |
44
|
1 |
|
$this->title = $post->post_title; |
45
|
1 |
|
$this->user_id = intval($post->post_author); |
46
|
1 |
|
$this->setProperties($post); |
47
|
1 |
|
$this->setTermIds($post); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function __get($key) |
54
|
|
|
{ |
55
|
|
|
return $this->offsetGet($key); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function __toString() |
62
|
|
|
{ |
63
|
|
|
return (string) $this->build(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return ReviewHtml |
68
|
|
|
*/ |
69
|
|
|
public function build(array $args = []) |
70
|
|
|
{ |
71
|
|
|
if (empty($this->ID)) { |
72
|
|
|
return new ReviewHtml($this); |
73
|
|
|
} |
74
|
|
|
$partial = glsr(SiteReviewsPartial::class); |
75
|
|
|
$partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
76
|
|
|
$partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
77
|
|
|
return $partial->buildReview($this); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed $key |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function offsetExists($key) |
85
|
|
|
{ |
86
|
|
|
return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $key |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function offsetGet($key) |
94
|
|
|
{ |
95
|
|
|
return property_exists($this, $key) |
96
|
|
|
? $this->$key |
97
|
|
|
: glsr_get($this->custom, $key, null); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param mixed $key |
102
|
|
|
* @param mixed $value |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function offsetSet($key, $value) |
106
|
|
|
{ |
107
|
|
|
if (property_exists($this, $key)) { |
108
|
|
|
$this->$key = $value; |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
if (!is_array($this->custom)) { |
112
|
|
|
$this->custom = array_filter((array) $this->custom); |
113
|
|
|
} |
114
|
|
|
$this->custom[$key] = $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $key |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function offsetUnset($key) |
122
|
|
|
{ |
123
|
|
|
$this->offsetSet($key, null); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function render() |
130
|
|
|
{ |
131
|
|
|
echo $this->build(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
1 |
|
protected function isModified(array $properties) |
138
|
|
|
{ |
139
|
1 |
|
return $this->date != $properties['date'] |
140
|
1 |
|
|| $this->content != $properties['content'] |
141
|
1 |
|
|| $this->title != $properties['title']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
1 |
|
protected function setProperties(WP_Post $post) |
148
|
|
|
{ |
149
|
|
|
$defaults = [ |
150
|
1 |
|
'author' => __('Anonymous', 'site-reviews'), |
151
|
1 |
|
'date' => '', |
152
|
1 |
|
'review_id' => '', |
153
|
1 |
|
'review_type' => 'local', |
154
|
|
|
]; |
155
|
1 |
|
$meta = array_filter( |
156
|
1 |
|
array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
157
|
1 |
|
'strlen' |
158
|
|
|
); |
159
|
1 |
|
$meta = array_merge($defaults, glsr(Helper::class)->unprefixArrayKeys($meta)); |
160
|
1 |
|
$properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
161
|
1 |
|
$this->modified = $this->isModified($properties); |
162
|
1 |
|
array_walk($properties, function ($value, $key) { |
163
|
1 |
|
if (!property_exists($this, $key) || isset($this->$key)) { |
164
|
1 |
|
return; |
165
|
|
|
} |
166
|
1 |
|
$this->$key = maybe_unserialize($value); |
167
|
1 |
|
}); |
168
|
1 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
1 |
|
protected function setTermIds(WP_Post $post) |
174
|
|
|
{ |
175
|
1 |
|
$this->term_ids = []; |
176
|
1 |
|
if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
177
|
1 |
|
return; |
178
|
|
|
} |
179
|
|
|
foreach ($terms as $term) { |
180
|
|
|
$this->term_ids[] = $term->term_id; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|