-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathverify-user-on-post-api-endpoint.php
81 lines (60 loc) · 2.65 KB
/
verify-user-on-post-api-endpoint.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
<?php
/**
* Plugin Name: TechiePress Callaback URL Receive
* Plugin URI: https://omukiguy.com
* Author: TechiePress
* Author URI: https://omukiguy.com
* Description: Build a POST REST API endpoint to receive data from another API.
* Version: 0.1.0
* License: GPL2 or later
* License URL: http://www.gnu.org/licenses/gpl-2.0.txt
* text-domain: prefix-plugin-name
*
* Credits:
* This Repository shows the code for the Youtube Video series https://www.youtube.com/playlist?list=PLNqG1qGUllk0npc2ZGPU358Q6S1itFCYR on the use of the WordPress REST API to handle Callback URLs from another server.
* Have you have ever wanted to use your WordPress or its plugins to receive data from a callback URL or Webhooks?
* In this video series, we explore how to use the WordPress REST API to receive the sent data from an External API, webhook URL or a callback URL. Then use the code to make it do a bunch of things for you.
* In the following series, you will learn how to make an API or an API.
*/
add_action( 'rest_api_init', 'techiepress_add_callback_url_endpoint' );
function techiepress_add_callback_url_endpoint(){
register_rest_route(
'techiepress/v1/', // Namespace
'receive-callback', // Endpoint
array(
'methods' => 'POST',
'callback' => 'techiepress_receive_callback'
)
);
}
function techiepress_receive_callback( $request_data ) {
$data = array();
$parameters = $request_data->get_params();
$name = $parameters['name'];
$password = $parameters['password'];
if ( isset($name) && isset($password) ) {
$userdata = get_user_by( 'login', $name );
if ( $userdata ) {
$wp_check_password_result = wp_check_password( $password, $userdata->user_pass, $userdata->ID );
if ( $wp_check_password_result ) {
$data['status'] = 'OK';
$data['received_data'] = array(
'name' => $name,
'password' => $password,
'data' => $userdata
);
$data['message'] = 'You have reached the server';
} else {
$data['status'] = 'OK';
$data['message'] = 'You are not authenticated to login!';
}
} else {
$data['status'] = 'OK';
$data['message'] = 'The current user does not exist!';
}
} else {
$data['status'] = 'Failed';
$data['message'] = 'Parameters Missing!';
}
return $data;
}