Skip to content

Commit

Permalink
Fix undefined array key warnings (See: https://wordpress.org/support/…
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkorthuis committed Mar 20, 2024
1 parent 689887c commit 5018a1c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions includes/api/class-endpoint-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function build_cache_key() {
$this->build_request_uri();
$this->set_cacheable_request_headers();
// No filter_input, see https://stackoverflow.com/questions/25232975/php-filter-inputinput-server-request-method-returns-null/36205923.
$request_method = filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : '';
// For backwards compatibility empty string for request method = GET.
if ( 'GET' === $request_method ) {
$request_method = '';
Expand Down Expand Up @@ -263,7 +263,7 @@ public function save_cache( $result ) {
}

// No filter_input, see https://stackoverflow.com/questions/25232975/php-filter-inputinput-server-request-method-returns-null/36205923.
$request_method = filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'GET';

// Force result to be valid JSON.
$result = json_decode( wp_json_encode( $result ) );
Expand Down Expand Up @@ -318,7 +318,8 @@ public function skip_caching() {
// Default only cache GET-requests.
$allowed_request_methods = get_option( 'wp_rest_cache_allowed_request_methods', [ 'GET' ] );
// No filter_input, see https://stackoverflow.com/questions/25232975/php-filter-inputinput-server-request-method-returns-null/36205923.
if ( ! in_array( filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ), $allowed_request_methods, true ) ) {
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'GET';
if ( ! in_array( $request_method, $allowed_request_methods, true ) ) {
return true;
}

Expand Down Expand Up @@ -459,6 +460,7 @@ public function get_api_cache() {
*/
private function rest_send_cors_headers( $value ) {
$origin = get_http_origin();
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'GET';

if ( $origin ) {
// Requests from file:// and data: URLs send "Origin: null".
Expand All @@ -469,7 +471,7 @@ private function rest_send_cors_headers( $value ) {
header( 'Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
header( 'Vary: Origin', false );
} elseif ( ! headers_sent() && 'GET' === $_SERVER['REQUEST_METHOD'] ) {
} elseif ( ! headers_sent() && 'GET' === $request_method ) {
header( 'Vary: Origin' );
}

Expand Down

0 comments on commit 5018a1c

Please sign in to comment.