| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_REST_Authentication |
| 5 |
* |
| 6 |
* @since 3.3.0 |
| 7 |
* @depecated 4.1.6.8 |
| 8 |
*/ |
| 9 |
class LP_REST_Authentication { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected static $wp_rest_nonce = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected static $wp_current_user_id = 0; |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
add_action( 'rest_authentication_errors', array( $this, 'rest_cookie_check_errors' ), 0 ); |
| 23 |
// add_filter( 'determine_current_user', array( $this, 'authenticate' ), 15 ); |
| 24 |
// add_filter( 'rest_pre_dispatch', array( $this, 'check_user_permissions' ), 10, 3 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param $result |
| 29 |
* |
| 30 |
* @return mixed |
| 31 |
*/ |
| 32 |
public function rest_cookie_check_errors( $result ) { |
| 33 |
if ( ! empty( $result ) ) { |
| 34 |
return $result; |
| 35 |
} |
| 36 |
|
| 37 |
if ( is_user_logged_in() ) { |
| 38 |
self::$wp_rest_nonce = wp_create_nonce( 'wp_rest' ); |
| 39 |
self::$wp_current_user_id = get_current_user_id(); |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if request is rest api. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function is_rest_api_request() { |
| 51 |
if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
$rest_prefix = trailingslashit( rest_get_url_prefix() ); |
| 56 |
$request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 57 |
|
| 58 |
return apply_filters( 'learn-press/is-rest-api-request', false !== strpos( $request_uri, $rest_prefix . 'lp/' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param int $user_id |
| 63 |
* |
| 64 |
* @return int |
| 65 |
*/ |
| 66 |
public function authenticate( $user_id ) { |
| 67 |
if ( $user_id || ! $this->is_rest_api_request() ) { |
| 68 |
return $user_id; |
| 69 |
} |
| 70 |
|
| 71 |
if ( is_ssl() ) { |
| 72 |
$user_id = $this->perform_basic_authentication(); |
| 73 |
} |
| 74 |
|
| 75 |
if ( $user_id ) { |
| 76 |
return $user_id; |
| 77 |
} |
| 78 |
|
| 79 |
return $this->perform_oauth_authentication(); |
| 80 |
} |
| 81 |
|
| 82 |
public function perform_basic_authentication() { |
| 83 |
return 2; |
| 84 |
} |
| 85 |
|
| 86 |
public function perform_oauth_authentication() { |
| 87 |
|
| 88 |
return 2; |
| 89 |
|
| 90 |
$params = $this->get_oauth_parameters(); |
| 91 |
if ( empty( $params ) ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
// Fetch WP user by consumer key. |
| 96 |
$this->user = $this->get_user_data_by_consumer_key( $params['oauth_consumer_key'] ); |
| 97 |
|
| 98 |
if ( empty( $this->user ) ) { |
| 99 |
$this->set_error( new WP_Error( 'learnpress_rest_authentication_error', __( 'Consumer key is invalid.', 'learnpress' ), array( 'status' => 401 ) ) ); |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Perform OAuth validation. |
| 105 |
$signature = $this->check_oauth_signature( $this->user, $params ); |
| 106 |
if ( is_wp_error( $signature ) ) { |
| 107 |
$this->set_error( $signature ); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
$timestamp_and_nonce = $this->check_oauth_timestamp_and_nonce( $this->user, $params['oauth_timestamp'], $params['oauth_nonce'] ); |
| 112 |
if ( is_wp_error( $timestamp_and_nonce ) ) { |
| 113 |
$this->set_error( $timestamp_and_nonce ); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
return $this->user->user_id; |
| 118 |
} |
| 119 |
|
| 120 |
public function get_user_by_consumer_key() { |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public function check_user_permissions() { |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public static function get_wp_rest_nonce() { |
| 132 |
return self::$wp_rest_nonce; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @return int |
| 137 |
*/ |
| 138 |
public static function get_wp_user_id() { |
| 139 |
return self::$wp_current_user_id; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check permission of a user on a post type. |
| 144 |
* |
| 145 |
* @param string $post_type |
| 146 |
* @param string $permission |
| 147 |
* @param int $user_id |
| 148 |
* |
| 149 |
* @return boolean |
| 150 |
*/ |
| 151 |
public static function check_post_permissions( $post_type, $permission = 'read', $user_id = 0 ) { |
| 152 |
if ( ! $user_id ) { |
| 153 |
$user_id = self::get_wp_user_id(); |
| 154 |
} |
| 155 |
|
| 156 |
$permissions = array( |
| 157 |
'read' => 'read_private_posts', |
| 158 |
'create' => 'publish_posts', |
| 159 |
'edit' => 'edit_post', |
| 160 |
'delete' => 'delete_post', |
| 161 |
'batch' => 'edit_others_posts', |
| 162 |
); |
| 163 |
|
| 164 |
if ( 'revision' === $post_type ) { |
| 165 |
$user_permission = false; |
| 166 |
} else { |
| 167 |
$cap = $permissions[ $permission ]; |
| 168 |
$post_type_object = get_post_type_object( $post_type ); |
| 169 |
$user_permission = current_user_can( $post_type_object->cap->$cap, $user_id ); |
| 170 |
} |
| 171 |
|
| 172 |
return apply_filters( 'learn-press/rest/check-permission', $user_permission, $permission, $user_id, $post_type ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Check permission if user is logged in. |
| 177 |
* |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
public static function check_logged_in_permission() { |
| 181 |
return ! ! self::get_wp_user_id(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return new LP_REST_Authentication(); |
| 186 |
|