Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created October 2, 2018 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/44bbf6422938f23417742395d445de03 to your computer and use it in GitHub Desktop.
Save dingo-d/44bbf6422938f23417742395d445de03 to your computer and use it in GitHub Desktop.
An example of a custom permission callback
<?php
public function user_get_check( \WP_REST_Request $request ) {
// To avoid CORS issue.
if ( $request->get_method() === 'OPTIONS' ) {
return true;
}
$auth_array = $request->get_headers();
if ( ! isset( $auth_array['authorization'] ) || empty( $auth_array['authorization'] ) ) {
return $this->error_handler( 'user_token_missing' );
}
$user_token_arr = array_values( array_filter( explode( 'Bearer ', $auth_array['authorization'][0] ) ) );
$user_token = trim( $user_token_arr[0] );
/**
* This is a wrapper for
*
* wp_parse_auth_cookie( $token, 'logged_in' ) and
* wp_validate_auth_cookie( $token, 'logged_in' ) functions.
*/
$token = $this->users_auth->is_auth_token_valid( $user_token );
if ( ! $token ) {
return $this->error_handler( 'user_token_auth_fail' );
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment