-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemojized-endpoints.php
50 lines (43 loc) · 1.07 KB
/
emojized-endpoints.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
<?php
add_action( 'rest_api_init', 'emojized_routes');
function emojized_routes()
{ register_rest_route( 'emojized/v1', '/count/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'emojized_post_id',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key)
{
return is_numeric( $param );
}
),
),
) );
}
function emojized_post_id($data)
{
$id = $data['id'];
$count = get_post_meta($id, 'count', true);
$counted_once = get_post_meta($id, 'counted_once', true);
if($counted_once == 'on')
{
$voters = get_post_meta($id, 'voters', true);
$ip = $_SERVER['REMOTE_ADDR'];
$ip_hashed = substr(base_convert(md5($ip), 16, 32), 0, 12);
$voters_array = explode(';', $voters);
if
(!in_array( $ip_hashed, $voters_array))
{
$count++;
$voters_and_new_vote = array_merge($voters, array($ip_hashed));
update_post_meta($id, 'voters', $voters.$ip_hashed.';');
}
}
else
{
$count++;
}
update_post_meta($id, 'count', $count);
return $count;
}
?>