Passed
Push — master ( 3e60cb...36c2a1 )
by Paul
07:09 queued 03:38
created

CreateReview::getNumeric()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Helper;
6
7
class CreateReview
8
{
9
	public $ajax_request;
10
	public $assigned_to;
11
	public $author;
12
	public $avatar;
13
	public $blacklisted;
14
	public $category;
15
	public $content;
16
	public $custom;
17
	public $date;
18
	public $email;
19
	public $form_id;
20
	public $ip_address;
21
	public $post_id;
22
	public $rating;
23
	public $referer;
24
	public $request;
25
	public $response;
26
	public $terms;
27
	public $title;
28
	public $url;
29
30 1
	public function __construct( $input )
31
	{
32 1
		$this->request = $input;
33 1
		$this->ajax_request = isset( $input['_ajax_request'] );
34 1
		$this->assigned_to = $this->getNumeric( 'assign_to' );
35 1
		$this->author = sanitize_text_field( $this->getUser( 'name' ));
36 1
		$this->avatar = $this->getAvatar();
37 1
		$this->blacklisted = isset( $input['blacklisted'] );
38 1
		$this->category = sanitize_key( $this->get( 'category' ));
39 1
		$this->content = sanitize_textarea_field( $this->get( 'content' ));
40 1
		$this->custom = $this->getCustom();
41 1
		$this->date = $this->getDate( 'date' );
42 1
		$this->email = sanitize_email( $this->getUser( 'email' ));
43 1
		$this->form_id = sanitize_key( $this->get( 'form_id' ));
44 1
		$this->ip_address = $this->get( 'ip_address' );
45 1
		$this->post_id = intval( $this->get( '_post_id' ));
46 1
		$this->rating = intval( $this->get( 'rating' ));
47 1
		$this->referer = $this->get( '_referer' );
48 1
		$this->response = sanitize_textarea_field( $this->get( 'response' ));
49 1
		$this->terms = !empty( $input['terms'] );
50 1
		$this->title = sanitize_text_field( $this->get( 'title' ));
51 1
		$this->url = esc_url_raw( $this->get( 'url' ));
52 1
	}
53
54
	/**
55
	 * @param string $key
56
	 * @return string
57
	 */
58 1
	protected function get( $key )
59
	{
60 1
		return isset( $this->request[$key] )
61 1
			? (string)$this->request[$key]
62 1
			: '';
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68 1
	protected function getAvatar()
69
	{
70 1
		$avatar = $this->get( 'avatar' );
71 1
		return !filter_var( $avatar, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED )
72 1
			? (string)get_avatar_url( $this->get( 'email' ))
73 1
			: $avatar;
74
	}
75
76
	/**
77
	 * @return array
78
	 */
79 1
	protected function getCustom()
80
	{
81
		$unset = [
82 1
			'_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token',
83
			'_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id',
84
			'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url',
85
		];
86 1
		$unset = apply_filters( 'site-reviews/create/unset-keys-from-custom', $unset );
87 1
		$custom = $this->request;
88 1
		foreach( $unset as $value ) {
89 1
			unset( $custom[$value] );
90
		}
91 1
		return $custom;
92
	}
93
94
	/**
95
	 * @param string $key
96
	 * @return string
97
	 */
98 1
	protected function getDate( $key )
99
	{
100 1
		$date = strtotime( $this->get( $key ));
101 1
		if( $date === false ) {
102 1
			$date = time();
103
		}
104 1
		return get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $date ));
105
	}
106
107
	/**
108
	 * @param string $key
109
	 * @return string
110
	 */
111 1
	protected function getUser( $key )
112
	{
113 1
		$value = $this->get( $key );
114 1
		if( empty( $value )) {
115
			$user = wp_get_current_user();
116
			$userValues = [
117
				'email' => 'user_email',
118
				'name' => 'display_name',
119
			];
120
			if( $user->exists() && array_key_exists( $key, $userValues )) {
121
				return $user->{$userValues[$key]};
122
			}
123
		}
124 1
		return $value;
125
	}
126
127
	/**
128
	 * @param string $key
129
	 * @return string
130
	 */
131 1
	protected function getNumeric( $key )
132
	{
133 1
		$value = $this->get( $key );
134 1
		return is_numeric( $value )
135
			? $value
136 1
			: '';
137
	}
138
}
139