model ) ) { return $this->model; } $this->model = new Security_Headers(); return $this->model; } /** * Checks if a specific header is submitted based on the provided conditions. * * @param string $header The header to check. * @param mixed $somewhere Additional condition to evaluate. * * @return bool True if the header is submitted, false otherwise. */ protected function maybe_submit_header( $header, $somewhere ): bool { if ( false === $somewhere ) { return true; } $collection = headers_list(); $match = false; foreach ( $collection as $item ) { if ( stristr( $item, $header ) ) { $match = true; } } return $match; } /** * Performs a HEAD request to a URL and caches the response. * * @param string $url The URL to request. * @param string $origin The origin of the request. * @param int|null $ttl Time-to-live for the cache entry. * * @return array|mixed The headers from the response or the WP_Error object if the request fails. */ protected function head_request( $url, $origin, $ttl = null ) { $model = $this->get_model(); $cached = $model->get_data_values( 'head_requests' ); if ( ! is_array( $cached ) ) { $cached = array(); } if ( isset( $cached[ $url ] ) ) { $cache = $cached[ $url ]; if ( $cache['ttl'] > time() ) { return $cache['data']; } } // No cache or cache expired. $request = wp_remote_head( $url, array( 'user-agent' => 'WP Defender self ping - ' . $origin ) ); if ( ! is_wp_error( $request ) ) { $headers = wp_remote_retrieve_headers( $request ); $headers = $headers->getAll(); if ( null === $ttl ) { $ttl = strtotime( '+1 day' ); } $headers['response_code'] = wp_remote_retrieve_response_code( $request ); $cached[ $url ] = array( 'ttl' => apply_filters( 'wd_head_request_ttl', $ttl ), 'data' => $headers, ); $model->set_data_values( 'head_requests', $cached ); $this->log( sprintf( 'Fetched header for %s into cache', $url ), wd_internal_log() ); return $headers; } return $request; } }