| @@ -116,18 +116,28 @@ | ||
| 116 | 116 | /** |
| 117 | 117 | * get_permission_callback_method |
| 118 | 118 | * |
| 119 | 119 | * Returns a reference to the permission callback for the method if exists or the default one if it doesn't. |
| 120 | + * Looks up inherited methods so module Route_Base manage_options gates are honoured. | |
| 121 | + * | |
| 120 | 122 | * @param string $method The REST method name |
| 121 | 123 | * |
| 122 | - * @return callable If a method called (rest-method)_permission_callback exists, returns a reference to it, otherwise | |
| 123 | - * returns a reference to the default member method /permission_callback/. | |
| 124 | + * @return callable If a method called (rest-method)_permission_callback exists, returns a reference to it, | |
| 125 | + * otherwise get_permission_callback when present, otherwise permission_callback. | |
| 124 | 126 | */ |
| 125 | 127 | public function get_permission_callback_method( string $method ): callable { |
| 126 | 128 | $method_name = strtolower( $method ); |
| 127 | 129 | $permission_callback_method = $method_name . '_permission_callback'; |
| 128 | - $permission_callback = $this->method_exists_in_current_class( $permission_callback_method ) ? $permission_callback_method : 'permission_callback'; | |
| 129 | - return [ $this, $permission_callback ]; | |
| 130 | + | |
| 131 | + if ( method_exists( $this, $permission_callback_method ) ) { | |
| 132 | + return [ $this, $permission_callback_method ]; | |
| 133 | + } | |
| 134 | + | |
| 135 | + if ( method_exists( $this, 'get_permission_callback' ) ) { | |
| 136 | + return [ $this, 'get_permission_callback' ]; | |
| 137 | + } | |
| 138 | + | |
| 139 | + return [ $this, 'permission_callback' ]; | |
| 130 | 140 | } |
| 131 | 141 | |
| 132 | 142 | /** |
| 133 | 143 | * maybe_add_args_to_config |
| @@ -148,20 +158,20 @@ | ||
| 148 | 158 | $method_args = $method_name . '_args'; |
| 149 | 159 | if ( $this->method_exists_in_current_class( $method_args ) ) { |
| 150 | 160 | $config['args'] = $this->{$method_args}(); |
| 151 | 161 | } |
| 152 | - $config['consumes'] =[ 'application/json' ]; | |
| 153 | - if ( $this->method_exists_in_current_class( $method . '_consumes' ) ) { | |
| 162 | + $config['consumes'] = [ 'application/json' ]; | |
| 163 | + if ( $this->method_exists_in_current_class( $method . '_consumes' ) ) { | |
| 154 | 164 | $config['consumes'] = $this->{$method . '_consumes'}(); |
| 155 | 165 | } |
| 156 | 166 | $config['produces'] = [ 'application/json' ]; |
| 157 | - if ( $this->method_exists_in_current_class( $method . '_produces' ) ) { | |
| 167 | + if ( $this->method_exists_in_current_class( $method . '_produces' ) ) { | |
| 158 | 168 | $config['produces'] = $this->{$method . '_produces'}(); |
| 159 | 169 | } |
| 160 | - if ( $this->method_exists_in_current_class( $method . '_summary' ) ) { | |
| 170 | + if ( $this->method_exists_in_current_class( $method . '_summary' ) ) { | |
| 161 | 171 | $config['summary'] = $this->{$method . '_summary'}(); |
| 162 | 172 | } |
| 163 | - if ( $this->method_exists_in_current_class( $method . '_description' ) ) { | |
| 173 | + if ( $this->method_exists_in_current_class( $method . '_description' ) ) { | |
| 164 | 174 | $config['description'] = $this->{$method . '_description'}(); |
| 165 | 175 | } |
| 166 | 176 | return $config; |
| 167 | 177 | } |
| @@ -209,9 +219,8 @@ | ||
| 209 | 219 | 'permission_callback' => $this->get_permission_callback_method( $method ), |
| 210 | 220 | ]; |
| 211 | 221 | $this->maybe_add_response_to_swagger( $method ); |
| 212 | 222 | return $this->maybe_add_args_to_config( $method, $config ); |
| 213 | - | |
| 214 | 223 | } |
| 215 | 224 | |
| 216 | 225 | /** |
| 217 | 226 | * method_exists_in_current_class |
| @@ -224,9 +233,9 @@ | ||
| 224 | 233 | private function method_exists_in_current_class( string $method ): bool { |
| 225 | 234 | $class_name = get_class( $this ); |
| 226 | 235 | try { |
| 227 | 236 | $reflection = new ReflectionClass( $class_name ); |
| 228 | - } catch( \ReflectionException $e ) { | |
| 237 | + } catch ( \ReflectionException $e ) { | |
| 229 | 238 | return false; |
| 230 | 239 | } |
| 231 | 240 | if ( ! $reflection->hasMethod( $method ) ) { |
| 232 | 241 | return false; |
| @@ -282,9 +291,9 @@ | ||
| 282 | 291 | * @return WP_Error The WordPress error object with the error message and status code supplied |
| 283 | 292 | */ |
| 284 | 293 | public function respond_wrong_method( $message = null, int $code = 404 ): WP_Error { |
| 285 | 294 | if ( null === $message ) { |
| 286 | - $message = __( 'No route was found matching the URL and request method', 'cloud-backup' ); | |
| 295 | + $message = __( 'No route was found matching the URL and request method', 'image-optimization' ); | |
| 287 | 296 | } |
| 288 | 297 | |
| 289 | 298 | return new WP_Error( 'rest_no_route', $message, [ 'status' => $code ] ); |
| 290 | 299 | } |
| @@ -393,9 +402,13 @@ | ||
| 393 | 402 | } |
| 394 | 403 | } |
| 395 | 404 | |
| 396 | 405 | public function verify_nonce_and_capability( $nonce = '', $name = '', $capability = 'manage_options' ) { |
| 397 | - $this->verify_nonce( $nonce, $name ); | |
| 406 | + $valid = $this->verify_nonce( $nonce, $name ); | |
| 407 | + | |
| 408 | + if ( is_wp_error( $valid ) ) { | |
| 409 | + return $valid; | |
| 410 | + } | |
| 398 | 411 | |
| 399 | 412 | if ( ! current_user_can( $capability ) ) { |
| 400 | 413 | return $this->respond_error_json([ |
| 401 | 414 | 'message' => esc_html__( 'You do not have sufficient permissions to access this data.', 'image-optimization' ), |