Skip to content

Commit

Permalink
Super Cache: fix how the plugin uses apache_request_headers (#39951)
Browse files Browse the repository at this point in the history
* Fix up apache_request_headers()

* changelog

* Fix static analysis issues

* Fix the changelog message

---------

Co-authored-by: Peter Petrov <[email protected]>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12408852739

Upstream-Ref: Automattic/jetpack@440a234
  • Loading branch information
donnchawp authored and matticbot committed Dec 19, 2024
1 parent 3244117 commit 4fa6a9d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This is an alpha version! The changes listed here are not final.
- Lossless image optimization for images (should improve performance with no visible changes).
- Move trailing space out of i18n message.
- Revert recent SVG image optimizations.
- Super Cache: Fixed the apache_request_headers fallback so it works when that command is disabled

## [1.12.4] - 2024-07-17
### Removed
Expand Down
28 changes: 15 additions & 13 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ function wp_cache_serve_cache_file() {
// don't try to match modified dates if using dynamic code.
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
wp_cache_debug( 'wp_cache_serve_cache_file: checking age of cached vs served files.' );
$headers = apache_request_headers();
$headers = wpsc_apache_request_headers();
$remote_mod_time = isset( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;

if ( $remote_mod_time === null && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
Expand Down Expand Up @@ -1751,7 +1751,7 @@ function wp_cache_user_agent_is_rejected() {
return false;
}

$headers = apache_request_headers();
$headers = wpsc_apache_request_headers();
if ( empty( $headers['User-Agent'] ) ) {
return false;
}
Expand Down Expand Up @@ -3582,23 +3582,25 @@ function wpsc_is_get_query() {
return $is_get_query;
}

if ( ! function_exists( 'apache_request_headers' ) ) {
/**
* A fallback for get request headers.
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
*
* @return array List of request headers
*/
function apache_request_headers() {
$headers = array();
/**
* A fallback for get request headers.
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
*
* @return array List of request headers
*/
function wpsc_apache_request_headers() {

if ( ! function_exists( 'apache_request_headers' ) || ! is_callable( 'apache_request_headers' ) ) {
$headers = array();
foreach ( array_keys( $_SERVER ) as $skey ) {
if ( str_starts_with( $skey, 'HTTP_' ) ) {
$header = implode( '-', array_map( 'ucfirst', array_slice( explode( '_', strtolower( $skey ) ), 1 ) ) );
$headers[ $header ] = $_SERVER[ $skey ];
}
}

return $headers;
} else {
$headers = apache_request_headers();
}

return $headers;
}
6 changes: 0 additions & 6 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,8 +1676,6 @@ function wp_cache_sanitize_value($text, & $array) {
function wp_cache_update_rejected_ua() {
global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;

if ( !function_exists( 'apache_request_headers' ) ) return;

if ( isset( $_POST[ 'wp_rejected_user_agent' ] ) && $valid_nonce ) {
$_POST[ 'wp_rejected_user_agent' ] = str_replace( ' ', '___', $_POST[ 'wp_rejected_user_agent' ] );
$text = str_replace( '___', ' ', wp_cache_sanitize_value( $_POST[ 'wp_rejected_user_agent' ], $cache_rejected_user_agent ) );
Expand All @@ -1692,10 +1690,6 @@ function wp_cache_update_rejected_ua() {
function wpsc_edit_rejected_ua() {
global $cache_rejected_user_agent;

if ( ! function_exists( 'apache_request_headers' ) ) {
return;
}

$admin_url = admin_url( 'options-general.php?page=wpsupercache' );
wp_cache_update_rejected_ua();
wpsc_render_partial(
Expand Down

0 comments on commit 4fa6a9d

Please sign in to comment.