| @@ -114,18 +114,28 @@ | ||
| 114 | 114 | /** |
| 115 | 115 | * get_permission_callback_method |
| 116 | 116 | * |
| 117 | 117 | * Returns a reference to the permission callback for the method if exists or the default one if it doesn't. |
| 118 | + * Looks up inherited methods so module Route_Base manage_options gates are honoured. | |
| 119 | + * | |
| 118 | 120 | * @param string $method The REST method name |
| 119 | 121 | * |
| 120 | - * @return callable If a method called (rest-method)_permission_callback exists, returns a reference to it, otherwise | |
| 121 | - * returns a reference to the default member method /permission_callback/. | |
| 122 | + * @return callable If a method called (rest-method)_permission_callback exists, returns a reference to it, | |
| 123 | + * otherwise get_permission_callback when present, otherwise permission_callback. | |
| 122 | 124 | */ |
| 123 | 125 | public function get_permission_callback_method( string $method ): callable { |
| 124 | 126 | $method_name = strtolower( $method ); |
| 125 | 127 | $permission_callback_method = $method_name . '_permission_callback'; |
| 126 | - $permission_callback = $this->method_exists_in_current_class( $permission_callback_method ) ? $permission_callback_method : 'permission_callback'; | |
| 127 | - return [ $this, $permission_callback ]; | |
| 128 | + | |
| 129 | + if ( method_exists( $this, $permission_callback_method ) ) { | |
| 130 | + return [ $this, $permission_callback_method ]; | |
| 131 | + } | |
| 132 | + | |
| 133 | + if ( method_exists( $this, 'get_permission_callback' ) ) { | |
| 134 | + return [ $this, 'get_permission_callback' ]; | |
| 135 | + } | |
| 136 | + | |
| 137 | + return [ $this, 'permission_callback' ]; | |
| 128 | 138 | } |
| 129 | 139 | |
| 130 | 140 | /** |
| 131 | 141 | * maybe_add_args_to_config |
| @@ -355,10 +365,23 @@ | ||
| 355 | 365 | ]); |
| 356 | 366 | } |
| 357 | 367 | } |
| 358 | 368 | |
| 369 | + public function verify_capability( $capability = 'manage_options' ) { | |
| 370 | + if ( ! current_user_can( $capability ) ) { | |
| 371 | + return $this->respond_error_json([ | |
| 372 | + 'message' => esc_html__( 'You do not have sufficient permissions to access this data.', 'image-optimization' ), | |
| 373 | + 'code' => 'bad_request', | |
| 374 | + ]); | |
| 375 | + } | |
| 376 | + } | |
| 377 | + | |
| 359 | 378 | public function verify_nonce_and_capability( $nonce = '', $name = '', $capability = 'manage_options' ) { |
| 360 | - $this->verify_nonce( $nonce, $name ); | |
| 379 | + $valid = $this->verify_nonce( $nonce, $name ); | |
| 380 | + | |
| 381 | + if ( is_wp_error( $valid ) ) { | |
| 382 | + return $valid; | |
| 383 | + } | |
| 361 | 384 | |
| 362 | 385 | if ( ! current_user_can( $capability ) ) { |
| 363 | 386 | return $this->respond_error_json([ |
| 364 | 387 | 'message' => esc_html__( 'You do not have sufficient permissions to access this data.', 'image-optimization' ), |