-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
219 lines (172 loc) · 6.15 KB
/
functions.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/*
* Plugin Name: WordPress SSO
* Author: Webkul
* Author URI: https://webkul.com
* Description: Login API for third-party to login using WordPress.
* Version: 1.1.0
* Domain Path: plugins/Wordpress-SSO
*/
ob_start();
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
! defined( 'LOGIN_WITH_WP_PATH' ) && define( 'LOGIN_WITH_WP_PATH', plugin_dir_path(__FILE__) );
! defined( 'LOGIN_WITH_WP_URL' ) && define( 'LOGIN_WITH_WP_URL', plugin_dir_url(__FILE__) );
/**
* Create tables and flush the rewrite rules on activation.
*/
function sso_api_activation() {
flush_rewrite_rules();
require_once( sprintf("%s/install.php", dirname(__FILE__)) );
}
register_activation_hook( __FILE__, 'sso_api_activation' );
/**
* Version number for our API.
*
* @var string
*/
define( 'JSON_API_VERSION', '1.0.0' );
/**
* Include our files for the API.
*/
include_once( dirname( __FILE__ ) . '/includes/class-wp-json-access-token.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wp-generate-token.php' );
include_once( dirname( __FILE__ ) . '/includes/admin/class-integrate-menu.php' );
add_filter( 'rewrite_rules_array', 'wp_insert_custom_rules' );
add_filter( 'query_vars', 'wp_insert_custom_vars' );
// Adding the id var so that WP recognizes it
function wp_insert_custom_vars($vars){
$vars[] = 'main_page';
$vars[] = 'pagename';
$vars[] = 'client_id';
return $vars;
}
function wp_insert_custom_rules($rules) {
$newrules = array();
$newrules=array(
'my-account/(.+)/(.+)?' => 'index.php?pagename=my-account&$matches[1]=$matches[1]&$matches[1]=$matches[2]',
'my-account/(.+)/?' => 'index.php?pagename=my-account&$matches[1]=$matches[1]',
'(.+)/(.+)/?' => 'index.php?pagename=$matches[1]&main_page=$matches[2]'
);
return $newrules + $rules;
}
add_action( 'wp_head', function() {
$a = get_query_var( 'main_page' );
global $wp_query;
if ( !empty ( $a ) && $a == 'auth' ) {
add_shortcode( 'uv_login', 'wk_uvdesk_auth_form' );
}
if ( get_query_var( 'pagename' ) == 'uv-login' && empty(get_query_var( 'main_page' )) ) {
add_shortcode( 'uv_login', 'wk_uvdesk_login' );
}
});
add_action( 'template_redirect', function() {
global $wpdb;
if ( isset($_GET['oauth_consumer_key']) ) {
$app_data = $wpdb->get_row( $wpdb->prepare( "SELECT count(*) as count FROM {$wpdb->prefix}oauth_clients WHERE consumer_key = %s", $_GET['oauth_consumer_key'] ), ARRAY_A );
if ( get_query_var( 'pagename' ) == 'uv-login' && empty(get_query_var( 'main_page' )) ) :
if ( $app_data['count'] == 1 && is_user_logged_in() ) {
wp_redirect(home_url('/uv-login/auth?oauth_consumer_key='.$_GET['oauth_consumer_key']));
exit;
}
endif;
if ( get_query_var( 'pagename' ) == 'uv-login' && get_query_var( 'main_page' ) == 'auth' ) :
if ( $app_data['count'] == 1 && !is_user_logged_in() ) {
wp_redirect(home_url('/uv-login?oauth_consumer_key='.$_GET['oauth_consumer_key']));
exit;
}
endif;
}
});
// login with woocommerce form
function wk_uvdesk_login()
{
global $wpdb;
$app_data = $wpdb->get_row( $wpdb->prepare( "SELECT count(*) as count FROM {$wpdb->prefix}oauth_clients WHERE consumer_key = %s", $_GET['oauth_consumer_key'] ), ARRAY_A );
if ( $app_data['count'] == 1 )
{
if( isset( $_GET['login'] ) && $_GET['login'] == 'failed' ){
wc_print_notice( 'Wrong Email or Password.', 'error' );
}
if ( ! is_user_logged_in() )
{
$args = array(
'redirect' => home_url('/uv-login/auth?oauth_consumer_key='.$_GET['oauth_consumer_key']),
'form_id' => 'login-with-wp',
'label_username' => __( 'Username or email address' ),
'label_password' => __( 'Password' ),
'label_log_in' => __( 'Login' )
);
wp_login_form( $args );
}
}
else
{
wp_redirect(site_url());
exit;
}
}
// deny or allow form
function wk_uvdesk_auth_form()
{
global $wpdb;
$table_name = $wpdb->prefix.'oauth_clients';
$table_name1 = $wpdb->prefix.'oauth_codes';
$client_id = $_GET['oauth_consumer_key'];
$redirect_uri = $wpdb->get_results( "Select redirect_uri, deny_uri from $table_name where consumer_key = $client_id ", ARRAY_A );
$user = wp_get_current_user();
if( $user->roles && in_array('customer',$user->roles)) {
wc_print_notice( 'Authorized Client.', 'success' );
if ( isset( $_POST['allow_auth']))
{
$auth_code = WK_TokenGenerator::wk_generateToken();
$check_val = $wpdb->insert( $table_name1, array(
'auth_code' => $auth_code,
'oauth_consumer_key' => $client_id,
'redirect_uri' => $redirect_uri[0]['redirect_uri'],
'user_id' => get_current_user_id(),
'expire' => strtotime("+1 minutes")
));
if ( $check_val )
{
wp_redirect($redirect_uri[0]['redirect_uri'].'?auth_code='.$auth_code);
exit;
}
}
if ( isset( $_POST['deny_auth'] ) )
{
wp_redirect($redirect_uri[0]['deny_uri']);
exit;
}
?>
<form method="post">
<p>Allow app to use data - </p>
<p><label><input type="submit" name="allow_auth" value="Allow">
<label><input type="submit" name="deny_auth" value="Deny"></p>
</form>
<?php
}
else {
wc_print_notice( 'You are not valid customer. <a href='.wp_logout_url(site_url().'/uv-login?oauth_consumer_key='.$_GET['oauth_consumer_key']).' class="wc-forward">Logout</a>', 'error' );
}
}
// login failed redirect
add_action( 'wp_login_failed', 'wk_front_end_login_failed' );
function wk_front_end_login_failed( $username )
{
$referrer = $_SERVER['HTTP_REFERER'];
$referrer = explode('?', $referrer);
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') )
{
wp_redirect( $referrer[0] . '?'.$referrer[1].'&login=failed' );
exit;
}
}
// enqueue style
add_action( 'wp_enqueue_scripts', function()
{
wp_enqueue_style('loginwp-css', LOGIN_WITH_WP_URL.'/assets/css/style.css');
});
$wp_json_customer_login = new SSO_JSON_Access_Token( $server );
add_action( 'rest_api_init', array( $wp_json_customer_login, 'sso_register_routes' ) );