Passed
Push — master ( e5c4f5...cb56ad )
by Paul
03:27
created

Review::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
8
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews as SiteReviewsPartial;
11
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml;
12
use WP_Post;
13
14
class Review implements \ArrayAccess
15
{
16
	public $assigned_to;
17
	public $author;
18
	public $avatar;
19
	public $content;
20
	public $custom;
21
	public $date;
22
	public $email;
23
	public $ID;
24
	public $ip_address;
25
	public $modified;
26
	public $pinned;
27
	public $rating;
28
	public $response;
29
	public $review_id;
30
	public $review_type;
31
	public $status;
32
	public $term_ids;
33
	public $title;
34
	public $url;
35
	public $user_id;
36
37 1
	public function __construct( WP_Post $post )
38
	{
39 1
		if( $post->post_type != Application::POST_TYPE )return;
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;
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
		if( property_exists( $this, $key )) {
96
			return $this->$key;
97
		}
98
		if( array_key_exists( $key, (array)$this->custom )) {
99
			return $this->custom[$key];
100
		}
101
		return null;
102
	}
103
104
	/**
105
	 * @param mixed $key
106
	 * @param mixed $value
107
	 * @return void
108
	 */
109
	public function offsetSet( $key, $value )
110
	{
111
		if( property_exists( $this, $key )) {
112
			$this->$key = $value;
113
			return;
114
		}
115
		if( !is_array( $this->custom )) {
116
			$this->custom = array_filter( (array)$this->custom );
117
		}
118
		$this->custom[$key] = $value;
119
	}
120
121
	/**
122
	 * @param mixed $key
123
	 * @return void
124
	 */
125
	public function offsetUnset( $key )
126
	{
127
		$this->offsetSet( $key, null );
128
	}
129
130
	/**
131
	 * @return void
132
	 */
133
	public function render()
134
	{
135
		echo $this->build();
136
	}
137
138
	/**
139
	 * @return bool
140
	 */
141 1
	protected function isModified( array $properties )
142
	{
143 1
		return $this->date != $properties['date']
144 1
			|| $this->content != $properties['content']
145 1
			|| $this->title != $properties['title'];
146
	}
147
148
	/**
149
	 * @return void
150
	 */
151 1
	protected function setProperties( WP_Post $post )
152
	{
153
		$defaults = [
154 1
			'author' => __( 'Anonymous', 'site-reviews' ),
155 1
			'date' => '',
156 1
			'review_id' => '',
157 1
			'review_type' => 'local',
158
		];
159 1
		$meta = array_filter(
160 1
			array_map( 'array_shift', array_filter((array)get_post_meta( $post->ID ))),
161 1
			'strlen'
162
		);
163 1
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
164 1
		$this->modified = $this->isModified( $properties );
165 1
		array_walk( $properties, function( $value, $key ) {
166 1
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
167 1
			$this->$key = maybe_unserialize( $value );
168 1
		});
169 1
	}
170
171
	/**
172
	 * @return void
173
	 */
174 1
	protected function setTermIds( WP_Post $post )
175
	{
176 1
		$this->term_ids = [];
177 1
		if( !is_array( $terms = get_the_terms( $post, Application::TAXONOMY )))return;
178
		foreach( $terms as $term ) {
179
			$this->term_ids[] = $term->term_id;
180
		}
181
	}
182
}
183