· 5 years ago · May 13, 2020, 01:00 PM
1<?php
2require __DIR__ . '/vendor/autoload.php';
3use Automattic\WooCommerce\Client;
4
5global $wpdb;
6
7add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' );
8
9function my_enqueue_assets() {
10 wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
11
12}
13
14function getAllUsers() {
15 return get_users();
16}
17
18function getAllUsers2() {
19 return get_users(
20 [
21 'role__in' => [ 'Editor']
22 ]
23 );
24}
25
26
27
28function getCoupon(WP_REST_REQUEST $request) {
29 $woocommerce = new Client(
30 'https://rinteenkulma.starttipaja.net',
31 'ck_8a73d552b3ccae3b2a30e9a17e83f04a7ed95f40',
32 'cs_7a34e81df59a0ec0edfd46dc7c1f20002628201c',
33 [
34 'wp_api' => true,
35 'version' => 'wc/v3',
36 'query_string_auth' => true
37 ]
38 );
39
40 $code = $request->get_param('code');
41 $meta = [];
42 if($code == null) {
43 return null;
44 }
45
46 $data = $woocommerce->get('coupons', ['code' => $code]);
47
48 if($data != null && is_array($data)) {
49 $data = $data[0];
50 $allowed_keys = [
51 'mwb_gw_coupon_amount',
52 'mwb_gw_giftcard_coupon_mail_to',
53 'giftcard_company_name',
54 'giftcard_edition',
55 'giftcard_phonenumber'
56 ];
57
58 $tmp_metadata = $data->meta_data;
59 unset($data->meta_data);
60
61 foreach($tmp_metadata as $m) {
62 if(in_array($m->key, $allowed_keys)) {
63 $meta[$m->key] = $m->value;
64 }
65 }
66 $data->meta_data = $meta;
67
68 }
69
70 return $data;
71}
72
73function exampleFunctionForIlia(WP_REST_REQUEST $request) {
74 //TODO parameter handling
75 $name = $request->get_param('name');
76 $email = $request->get_param('email');
77 $mobile = $request->get_param('mobile');
78
79 include 'config_example';
80
81
82 $name = mysqli_real_escape_string($connect, $name);
83 $email = mysqli_real_escape_string($connect, $email);
84 $mobile = mysqli_real_escape_string($connect, $mobile);
85
86 $query = "INSERT INTO registereduser (name, email,mobile)
87 VALUES('$name', '$email','$mobile')";
88 $results = mysqli_query($connect, $query);
89 if($results > 0)
90 {
91 return "user added successfully";
92 }
93 else {
94 return "user added failed";
95 }
96}
97
98function fetchUsersTable(){
99 // $table_name = $wbdb->prefix . 'wp_lahjakorttimobile_users';
100
101 /* $field_name = 'phonenumber';
102 $prepared_statement = $wpdb->prepare( "SELECT {$field_name} FROM {$table_name}");
103 $values = $wpdb->get_col( $prepared_statement );*/
104 $table_name = 'wp_lahjakorttimobile_users';
105 $values = $wpdb->get_results("SELECT * FROM {$table_name}");
106 return $values;
107}
108function getCouponsWhereNumber(WP_REST_REQUEST $request){
109 $woocommerce = new Client(
110 'https://rinteenkulma.starttipaja.net',
111 'ck_8a73d552b3ccae3b2a30e9a17e83f04a7ed95f40',
112 'cs_7a34e81df59a0ec0edfd46dc7c1f20002628201c',
113 [
114 'wp_api' => true,
115 'version' => 'wc/v3',
116 'query_string_auth' => true
117 ]
118 );
119
120 $finalList = [];
121 $phone_number = $request->get_param('phone_number');
122
123 if($phone_number == null) {
124 return null;
125 }
126
127 $phone_number = str_replace(' ', '', $phone_number);
128
129 //query
130 $data = $woocommerce->get('coupons', ['per_page' => 100]);
131
132 if($data != null && is_array($data)) {
133
134 $allowed_keys = [
135 'mwb_gw_coupon_amount',
136 'mwb_gw_giftcard_coupon_mail_to',
137 'giftcard_company_name',
138 'giftcard_edition',
139 'giftcard_phonenumber'
140 ];
141 for($i=0; $i < count($data); $i++){
142 $meta = []; //tyhjennetään muuttuja aina aluksi ettei jää edelliseltä rundilta tavaraa
143 $d = $data[$i];
144
145 $tmp_metadata = $d->meta_data;
146 unset($d->meta_data);
147
148 foreach($tmp_metadata as $m) {
149 if(in_array($m->key, $allowed_keys)) {
150 $meta[$m->key] = $m->value;
151 }
152 }
153 $meta_phone = str_replace(' ', '', $meta['giftcard_phonenumber']);
154
155 if(!array_key_exists('giftcard_phonenumber', $meta) || $meta_phone == '' || $meta_phone != $phone_number) {
156 continue; //jos ei ole kenttää ei ole, se on tyhjä tai sitten eri numero kuin haluttu niin skipataan seuraavaan listan itemiin
157 }
158
159 $d->meta_data = $meta;
160 $finalList[] = $d;
161 }
162 }
163 return $finalList;
164}
165
166function getAllCoupons() {
167 $woocommerce = new Client(
168 'https://rinteenkulma.starttipaja.net',
169 'ck_8a73d552b3ccae3b2a30e9a17e83f04a7ed95f40',
170 'cs_7a34e81df59a0ec0edfd46dc7c1f20002628201c',
171 [
172 'wp_api' => true,
173 'version' => 'wc/v3',
174 'query_string_auth' => true
175 ]
176 );
177
178
179 $meta = [];
180 $finalList = [];
181
182 $data = $woocommerce->get('coupons');
183
184 if($data != null && is_array($data)) {
185
186 $allowed_keys = [
187 'mwb_gw_coupon_amount',
188 'mwb_gw_giftcard_coupon_mail_to',
189 'giftcard_company_name',
190 'giftcard_edition',
191 'giftcard_phonenumber'
192 ];
193 for($i=0; $i < count($data); $i++){
194 // $data = $data[$i];
195 $tmp_metadata = $data[$i]->meta_data;
196 unset($data[$i]->meta_data);
197
198 foreach($tmp_metadata as $m) {
199 if(in_array($m->key, $allowed_keys)) {
200 $meta[$i][$m->key] = $m->value;
201 }
202 }
203 $data[$i]->meta_data = $meta[$i];
204 array_push($finalList, $data[$i]);
205 // array_push($data, $data[$i]);
206
207 }
208 }
209
210 return $data;
211}
212
213
214//add all custom api request in this function
215function registerCustomApiRequests() {
216 register_rest_route(
217 'starttipaja/v1',
218 '/users/',
219 [
220 'method' => 'GET',
221 'callback' => 'getAllUsers'
222 ]
223 );
224 register_rest_route(
225 'starttipaja/v1',
226 '/users2/',
227 [
228 'method' => 'GET',
229 'callback' => 'getAllUsers2'
230 ]
231 );
232 register_rest_route(
233 'starttipaja/v1',
234 '/database/',
235 [
236 'method' => 'GET',
237 'callback' => 'fetchUsersTable'
238 ]
239 );
240
241 register_rest_route(
242 'starttipaja/v1',
243 '/coupon/',
244 [
245 'method' => 'GET',
246 'callback' => 'getCoupon',
247 'args' => [
248 'code' => [
249 'validate_callback' => function($param, $request, $key) {
250 return is_string($param);
251 }
252 ]
253 ]
254 ]
255
256 );
257 register_rest_route(
258 'starttipaja/v1',
259 '/allcoupons/',
260 [
261 'method' => 'GET',
262 'callback' => 'getAllCoupons',
263
264
265 ]);
266
267 register_rest_route(
268 'starttipaja/v1',
269 '/numbercoupons/',
270 [
271 'method' => 'GET',
272 'callback' => 'getCouponsWhereNumber',
273 'args' => [
274 'phone_number' => [
275 'validate_callback' => function($param, $request, $key) {
276 return is_string($param);
277 }
278 ]
279 ]
280
281 ]);
282 register_rest_route(
283 'starttipaja/v1',
284 '/insertDataExample/',
285 [
286 'method' => 'POST',
287 'callback' => 'exampleFunctionForIlia',
288 'args' => [
289 'name',
290 'email',
291 'mobile'
292 ]
293 ]
294 );
295}
296
297//register custom api urls
298add_action('rest_api_init', 'registerCustomApiRequests');