Skip to content

Commit fb72116

Browse files
authored
Merge pull request #16 from BeAPI/fix/authentification-rest
fix authentication check for REST API
2 parents e4dd17c + 7cf476e commit fb72116

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

classes/helpers.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Helpers {
2121
public static function is_maintenance_mode() {
2222
$is_maintenance_mode = true;
2323

24-
if ( is_user_logged_in() ) {
24+
if ( self::is_user_authenticated() ) {
2525
$is_maintenance_mode = false;
2626
}
2727

@@ -36,13 +36,63 @@ public static function is_maintenance_mode() {
3636
return apply_filters( 'beapi.maintenance_mode.is_maintenance_mode', $is_maintenance_mode );
3737
}
3838

39+
/**
40+
* Check if the current user is authenticated.
41+
* This method handles both regular requests and REST API requests.
42+
*
43+
* @return bool
44+
* @since 2.1.1
45+
*/
46+
public static function is_user_authenticated() {
47+
// For regular requests, check if user is logged in.
48+
if ( is_user_logged_in() ) {
49+
return true;
50+
}
51+
52+
// For REST API requests, we need to check authentication differently
53+
// because is_user_logged_in() may not work correctly at this point.
54+
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
55+
return false;
56+
}
57+
58+
// Try to get current user (this works even for REST API).
59+
$user = wp_get_current_user();
60+
if ( $user && $user->ID > 0 ) {
61+
return true;
62+
}
63+
64+
// Check if there's a valid authentication cookie.
65+
// This is useful when cookies are sent but not yet processed.
66+
if ( ! defined( 'LOGGED_IN_COOKIE' ) || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
67+
return false;
68+
}
69+
70+
$cookie = wp_parse_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
71+
if ( empty( $cookie['username'] ) || empty( $cookie['expiration'] ) ) {
72+
return false;
73+
}
74+
75+
// Verify the cookie is still valid by checking expiration.
76+
if ( $cookie['expiration'] <= time() ) {
77+
return false;
78+
}
79+
80+
// Verify the user exists.
81+
$user = get_user_by( 'login', $cookie['username'] );
82+
if ( ! $user || $user->ID <= 0 ) {
83+
return false;
84+
}
85+
86+
return true;
87+
}
88+
3989
/**
4090
* Check if the current IP is in whitelist
4191
*
4292
* @return bool
93+
* @author Maxime CULEA
4394
* @since 1.0.0
4495
*
45-
* @author Maxime CULEA
4696
*/
4797
public static function is_allowed_ip() {
4898
/**
@@ -51,15 +101,15 @@ public static function is_allowed_ip() {
51101
* @params array $whitelist_ips : Array of allowed ips
52102
*
53103
* @return array
104+
* @author Maxime CULEA
54105
* @since 1.0.0
55106
*
56-
* @author Maxime CULEA
57107
*/
58108
$whitelist_ips = apply_filters( 'beapi.maintenance_mode.whitelist_ips', [] );
59109
if ( empty( $whitelist_ips ) ) { // No whitelist, then nobody is allowed
60110
return false;
61111
}
62-
112+
63113
// Get user IP
64114
$current_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
65115
if ( empty( $current_ip ) ) {
@@ -86,9 +136,9 @@ public static function is_allowed_ip() {
86136
* Check if during multisite process to avoid not maintenance mode or not
87137
*
88138
* @return bool
139+
* @author Maxime CULEA
89140
* @since 1.0.0
90141
*
91-
* @author Maxime CULEA
92142
*/
93143
public static function is_ms_activate() {
94144
if ( empty( $_SERVER['SCRIPT_NAME'] ) ) {
@@ -104,9 +154,9 @@ public static function is_ms_activate() {
104154
* @param $matches
105155
*
106156
* @return string
157+
* @author Nicolas Juen
107158
* @since 1.0.0
108159
*
109-
* @author Nicolas Juen
110160
*/
111161
private static function maintenance_replace_ip( $matches ) {
112162
return sprintf( '%03d', $matches[1] );
@@ -116,9 +166,9 @@ private static function maintenance_replace_ip( $matches ) {
116166
* Get the maintenance template path
117167
*
118168
* @return string
169+
* @author Maxime CULEA
119170
* @since 1.0.0
120171
*
121-
* @author Maxime CULEA
122172
*/
123173
public static function get_template_path() {
124174
$default = BEAPI_MAINTENANCE_MODE_DIR . 'templates/maintenance.php';
@@ -129,9 +179,9 @@ public static function get_template_path() {
129179
* @params string $default : The path to the custom template
130180
*
131181
* @return array
182+
* @author Maxime CULEA
132183
* @since 1.0.0
133184
*
134-
* @author Maxime CULEA
135185
*/
136186
$template = apply_filters( 'beapi.maintenance_mode.template.path', $default );
137187
if ( empty( $template ) || ! is_file( $template ) ) {

0 commit comments

Comments
 (0)