|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class Config { |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \Wordlift_Admin_Setup |
|
9
|
|
|
*/ |
|
10
|
|
|
private $admin_setup; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Wordlift_Key_Validation_Service |
|
13
|
|
|
*/ |
|
14
|
|
|
private $key_validation_service; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Config constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $admin_setup \Wordlift_Admin_Setup |
|
20
|
|
|
* @param $key_validation_service \Wordlift_Key_Validation_Service |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct( $admin_setup, $key_validation_service ) { |
|
23
|
|
|
|
|
24
|
|
|
$this->admin_setup = $admin_setup; |
|
25
|
|
|
$this->key_validation_service = $key_validation_service; |
|
26
|
|
|
add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Check if the key is valid and also not bound to any domain. |
|
32
|
|
|
* |
|
33
|
|
|
* @param $key string |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
38
|
|
|
$account_info = $this->key_validation_service->get_account_info( $key ); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* we need to check if the key is not associated with any account |
|
42
|
|
|
* before setting it, we should check if the url is null. |
|
43
|
|
|
*/ |
|
44
|
|
|
if ( is_wp_error( $account_info ) |
|
45
|
|
|
|| wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$account_info_json = $account_info['body']; |
|
50
|
|
|
|
|
51
|
|
|
$account_info_data = json_decode( $account_info_json, true ); |
|
52
|
|
|
|
|
53
|
|
|
if ( ! $account_info_data ) { |
|
54
|
|
|
// Invalid json returned by api. |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ( $account_info_data['url'] !== null ) { |
|
59
|
|
|
// key already associated with another account. |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
public function config() { |
|
68
|
|
|
|
|
69
|
|
|
// Perform validation check for all the parameters. |
|
70
|
|
|
$required_fields = array( |
|
71
|
|
|
'diagnostic', |
|
72
|
|
|
'vocabulary', |
|
73
|
|
|
'language', |
|
74
|
|
|
'country', |
|
75
|
|
|
'publisherName', |
|
76
|
|
|
'publisher', |
|
77
|
|
|
'license' |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
// validate all the fields before processing |
|
81
|
|
|
foreach ( $required_fields as $field ) { |
|
82
|
|
|
if ( ! array_key_exists( $field, $_POST ) ) { |
|
83
|
|
|
wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
84
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$key = (string) $_POST['license']; |
|
90
|
|
|
|
|
91
|
|
|
if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
92
|
|
|
wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
93
|
|
|
|
|
94
|
|
|
// exit if not valid. |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->admin_setup->save_configuration( $this->get_params() ); |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
private function get_params() { |
|
109
|
|
|
|
|
110
|
|
|
$attachment_id = $this->may_be_get_attachment_id(); |
|
111
|
|
|
|
|
112
|
|
|
$params = array( |
|
113
|
|
|
'key' => (string) $_POST['license'], |
|
114
|
|
|
'vocabulary' => (string) $_POST['vocabulary'], |
|
115
|
|
|
'wl-site-language' => (string) $_POST['language'], |
|
116
|
|
|
'wl-country-code' => (string) $_POST['country'], |
|
117
|
|
|
'name' => (string) $_POST['publisherName'], |
|
118
|
|
|
'user_type' => (string) $_POST['publisher'], |
|
119
|
|
|
'logo' => $attachment_id |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
if ( (bool) $_POST['diagnostic'] ) { |
|
123
|
|
|
$params['share-diagnostic'] = 'on'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $params; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return int | bool |
|
131
|
|
|
*/ |
|
132
|
|
|
private function may_be_get_attachment_id() { |
|
133
|
|
|
// if image or image extension not posted then return false. |
|
134
|
|
|
if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
135
|
|
|
return false; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
139
|
|
|
$image_string = (string) $_POST['image']; |
|
140
|
|
|
$image_ext = (string) $_POST['imageExtension']; |
|
141
|
|
|
|
|
142
|
|
|
if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
143
|
|
|
return false; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$image_decoded_string = base64_decode( $image_string ); |
|
147
|
|
|
|
|
148
|
|
|
$upload_dir = wp_upload_dir(); |
|
149
|
|
|
|
|
150
|
|
|
$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
151
|
|
|
|
|
152
|
|
|
file_put_contents( $file_path, $image_decoded_string ); |
|
153
|
|
|
|
|
154
|
|
|
return wp_insert_attachment( array(), $file_path ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.