|
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 $response; |
|
25
|
|
|
public $terms; |
|
26
|
|
|
public $title; |
|
27
|
|
|
public $url; |
|
28
|
|
|
|
|
29
|
|
|
protected $request; |
|
30
|
|
|
|
|
31
|
1 |
|
public function __construct( $input ) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->request = $input; |
|
34
|
1 |
|
$this->ajax_request = isset( $input['_ajax_request'] ); |
|
35
|
1 |
|
$this->assigned_to = $this->getNumeric( 'assign_to' ); |
|
36
|
1 |
|
$this->author = sanitize_text_field( $this->getUser( 'name' )); |
|
37
|
1 |
|
$this->avatar = $this->getAvatar(); |
|
38
|
1 |
|
$this->blacklisted = isset( $input['blacklisted'] ); |
|
39
|
1 |
|
$this->category = sanitize_key( $this->get( 'category' )); |
|
40
|
1 |
|
$this->content = sanitize_textarea_field( $this->get( 'content' )); |
|
41
|
1 |
|
$this->custom = $this->getCustom(); |
|
42
|
1 |
|
$this->date = $this->getDate( 'date' ); |
|
43
|
1 |
|
$this->email = sanitize_email( $this->getUser( 'email' )); |
|
44
|
1 |
|
$this->form_id = sanitize_key( $this->get( 'form_id' )); |
|
45
|
1 |
|
$this->ip_address = $this->get( 'ip_address' ); |
|
46
|
1 |
|
$this->post_id = intval( $this->get( '_post_id' )); |
|
47
|
1 |
|
$this->rating = intval( $this->get( 'rating' )); |
|
48
|
1 |
|
$this->referer = $this->get( '_referer' ); |
|
49
|
1 |
|
$this->response = sanitize_textarea_field( $this->get( 'response' )); |
|
50
|
1 |
|
$this->terms = !empty( $input['terms'] ); |
|
51
|
1 |
|
$this->title = sanitize_text_field( $this->get( 'title' )); |
|
52
|
1 |
|
$this->url = esc_url_raw( $this->get( 'url' )); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
protected function get( $key ) |
|
60
|
|
|
{ |
|
61
|
1 |
|
return isset( $this->request[$key] ) |
|
62
|
1 |
|
? (string)$this->request[$key] |
|
63
|
1 |
|
: ''; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
1 |
|
protected function getAvatar() |
|
70
|
|
|
{ |
|
71
|
1 |
|
$avatar = $this->get( 'avatar' ); |
|
72
|
1 |
|
return !filter_var( $avatar, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED ) |
|
73
|
1 |
|
? (string)get_avatar_url( $this->get( 'email' )) |
|
74
|
1 |
|
: $avatar; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected function getCustom() |
|
81
|
|
|
{ |
|
82
|
|
|
$unset = [ |
|
83
|
1 |
|
'_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token', |
|
84
|
|
|
'_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id', |
|
85
|
|
|
'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url', |
|
86
|
|
|
]; |
|
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 |
|
return is_numeric( $this->request[$key] ) |
|
134
|
|
|
? (string)$this->request[$key] |
|
135
|
1 |
|
: ''; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|