Last active
January 9, 2020 20:15
-
-
Save wpmudev-sls/d8f3454da8a841629bddbca1e03a891f to your computer and use it in GitHub Desktop.
Fix REST API does not return when activated members plugi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Fix REST API does not return | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Fix REST API does not return when activated members plugin- 1154550707006982 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined('ABSPATH') || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( 'after_setup_theme', 'wpmudev_fix_rest_api_does_not_return', 0 ); | |
function wpmudev_fix_rest_api_does_not_return(){ | |
if( class_exists('Members_Plugin') && members_is_private_rest_api() ){ | |
class WPMUDEV_Fix_Member_Rest_API{ | |
public function __construct(){ | |
add_filter( 'wp_rest_server_class', array( $this, 'wp_rest_server_class' ) ); | |
add_filter( 'determine_current_user', array( $this, 'determine_current_user' ), 10, 3 ); | |
} | |
public function determine_current_user($user_id){ | |
// remove members filter | |
remove_filter( 'rest_authentication_errors', 'members_private_rest_api', 95 ); | |
add_filter( 'rest_pre_echo_response', array( $this, 'rest_pre_echo_response' ), 10, 3 ); | |
return $user_id; | |
} | |
public function rest_pre_echo_response( $result, $rest_server, $request ){ | |
if( ! is_user_logged_in() ){ | |
$error = new WP_Error( | |
'rest_not_logged_in', | |
esc_html( | |
apply_filters( | |
'members_rest_api_error_message', | |
__( 'You are not currently logged in.', 'members' ) | |
) | |
), | |
array( 'status' => 401 ) | |
); | |
$error_data = $error->get_error_data(); | |
if ( is_array( $error_data ) && isset( $error_data['status'] ) ) { | |
$status = $error_data['status']; | |
} else { | |
$status = 500; | |
} | |
$errors = array(); | |
foreach ( (array) $error->errors as $code => $messages ) { | |
foreach ( (array) $messages as $message ) { | |
$errors[] = array( | |
'code' => $code, | |
'message' => $message, | |
'data' => $error->get_error_data( $code ), | |
); | |
} | |
} | |
$data = $errors[0]; | |
if ( count( $errors ) > 1 ) { | |
// Remove the primary error. | |
array_shift( $errors ); | |
$data['additional_errors'] = $errors; | |
} | |
$result = new WP_REST_Response( $data, $status ); | |
} | |
return $result; | |
} | |
/** | |
* Prevent caching of unauthenticated status. See comment below. | |
* | |
* We don't actually care about the `wp_rest_server_class` filter, it just | |
* happens right after the constant we do care about is defined. | |
* | |
* @source https://github.com/georgestephanis/application-passwords | |
*/ | |
public function wp_rest_server_class( $class ) { | |
global $current_user; | |
if ( defined( 'REST_REQUEST' ) | |
&& REST_REQUEST | |
&& $current_user instanceof WP_User | |
&& 0 === $current_user->ID ) { | |
/* | |
* For our authentication to work, we need to remove the cached lack | |
* of a current user, so the next time it checks, we can detect that | |
* this is a rest api request and allow our override to happen. This | |
* is because the constant is defined later than the first get current | |
* user call may run. | |
*/ | |
$current_user = null; | |
} | |
return $class; | |
} | |
} | |
$run = new WPMUDEV_Fix_Member_Rest_API(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment