-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
83 lines (73 loc) · 2.34 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
*
* API endpoints and stuff for Robot
*
* http://{domain}/wp-json/robot/token/blabla
*
*
*/
class SNRobot_api extends WP_REST_Controller
{
public function __construct(){
add_action('rest_api_init', function () {
//Get specific market.
register_rest_route('robots', '/getinfo/(?P<token>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => array($this, 'get_robot_info'),
));
register_rest_route('robots', '/addinfo/(?P<token>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => array($this, 'add_robot_info'),
));
register_rest_route('robots', '/popular', array(
'methods' => 'GET',
'callback' => array($this,'robot_popular'),
));
});
}
public function robot_popular()
{
return "wat";
}
/**
* Add info, like ip-number, position etc.
*/
public function add_robot_info($request){
$parameters = $request->get_params();
$the_token = sanitize_text_field(urldecode($parameters["token"]));
$ip_number = sanitize_text_field(urldecode($parameters["ipnumber"]));
$args = array(
'meta_key' => 'snillrik_robot_token',
'meta_value' => $the_token,
'post_type' => 'snillrik_robot'
);
$robot = get_posts($args);
$robot = isset($robot[0]) ? $robot[0] : false;
if($robot){
$tempsessionid = wp_generate_password(43, false, false);
update_post_meta($robot->ID, "snillrik_robot_ip", $ip_number);
update_post_meta($robot->ID, "snillrik_robot_sessiontoken", $tempsessionid);
return wp_send_json(array(
"response" => "ok",
"sessiontoken"=>$tempsessionid
));
}
else
return "Noo, something is wrong, robot does not exist!";
}
/**
* The market page ie, the page show to the world.
*/
public function get_robot_info($request){
$parameters = $request->get_params();
$the_token = sanitize_text_field(urldecode($parameters["token"]));
$args = array(
'meta_key' => 'snillrik_robot_token',
'meta_value' => $the_token,
'post_type' => 'snillrik_robot'
);
$robot = get_posts($args);
return array("content" => $robot);
}
}