forked from auth0/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
160 lines (135 loc) · 3.79 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
<?php
/**
* Global WP-Auth0 functions.
*
* @since 3.10.0
*/
/**
* Return a stored option value.
*
* @since 3.10.0
*
* @param string $key - Settings key to get.
* @param mixed $default - Default value to return if not found.
*
* @return mixed
*/
function wp_auth0_get_option( $key, $default = null ) {
return WP_Auth0_Options::Instance()->get( $key, $default );
}
/**
* Determine if we're on the wp-login.php page and if the current action matches a given set.
*
* @since 3.11.0
*
* @param array $actions - An array of actions to check the current action against.
*
* @return bool
*/
function wp_auth0_is_current_login_action( array $actions ) {
// Not on wp-login.php.
if (
( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' !== $GLOBALS['pagenow'] ) &&
! function_exists( 'login_header' )
) {
return false;
}
$current_action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : null;
return in_array( $current_action, $actions );
}
/**
* Generate a valid WordPress login override URL, if plugin settings allow.
*
* @param null $login_url - An existing URL to modify; default is wp-login.php.
*
* @return string
*/
function wp_auth0_login_override_url( $login_url = null ) {
$wle = wp_auth0_get_option( 'wordpress_login_enabled' );
if ( 'no' === $wle ) {
return '';
}
$wle_code = '';
if ( 'code' === $wle ) {
$wle_code = wp_auth0_get_option( 'wle_code' );
}
$login_url = $login_url ?: wp_login_url();
return add_query_arg( 'wle', $wle_code, $login_url );
}
/**
* Can the core WP login form be shown?
*
* @return bool
*/
function wp_auth0_can_show_wp_login_form() {
if ( ! WP_Auth0::ready() ) {
return true;
}
if ( wp_auth0_is_current_login_action( array( 'resetpass', 'rp', 'validate_2fa', 'postpass' ) ) ) {
return true;
}
if ( get_query_var( 'auth0_login_successful' ) ) {
return true;
}
if ( ! isset( $_REQUEST['wle'] ) ) {
return false;
}
$wle_setting = wp_auth0_get_option( 'wordpress_login_enabled' );
if ( 'no' === $wle_setting ) {
return false;
}
if ( in_array( $wle_setting, array( 'link', 'isset' ) ) ) {
return true;
}
$wle_code = wp_auth0_get_option( 'wle_code' );
if ( 'code' === $wle_setting && $wle_code === $_REQUEST['wle'] ) {
return true;
}
return false;
}
/**
*
* @return bool
*/
function wp_auth0_uses_secure_callback() {
$callback_url = WP_Auth0_Options::Instance()->get_wp_auth0_url();
return 'https' === parse_url( $callback_url, PHP_URL_SCHEME );
}
if ( ! function_exists( 'get_auth0userinfo' ) ) {
function get_auth0userinfo( $user_id ) {
$profile = WP_Auth0_UsersRepo::get_meta( $user_id, 'auth0_obj' );
return $profile ? WP_Auth0_Serializer::unserialize( $profile ) : false;
}
}
if ( ! function_exists( 'get_currentauth0user' ) ) {
function get_currentauth0user() {
return (object) array(
'auth0_obj' => get_auth0userinfo( get_current_user_id() ),
'last_update' => WP_Auth0_UsersRepo::get_meta( get_current_user_id(), 'last_update' ),
'auth0_id' => WP_Auth0_UsersRepo::get_meta( get_current_user_id(), 'auth0_id' ),
);
}
}
if ( ! function_exists( 'get_auth0_curatedBlogName' ) ) {
function get_auth0_curatedBlogName() {
$name = get_bloginfo( 'name' );
// WordPress can have a blank site title, which will cause initial client creation to fail
if ( empty( $name ) ) {
$name = wp_parse_url( home_url(), PHP_URL_HOST );
if ( $port = wp_parse_url( home_url(), PHP_URL_PORT ) ) {
$name .= ':' . $port;
}
}
$name = preg_replace( '/[^A-Za-z0-9 ]/', '', $name );
$name = preg_replace( '/\s+/', ' ', $name );
$name = str_replace( ' ', '-', $name );
return $name;
}
}
if ( ! function_exists( 'get_currentauth0userinfo' ) ) {
function get_currentauth0userinfo() {
global $currentauth0_user;
$currentauth0_user = get_auth0userinfo( get_current_user_id() );
return $currentauth0_user;
}
}