Skip to content

Commit

Permalink
Add options to set different cache values for uploads
Browse files Browse the repository at this point in the history
- allow static files to have a different max-age
- enable a max-age for non-private responses
  • Loading branch information
leedxw committed Jun 28, 2024
1 parent b3160e3 commit be5acc7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
8 changes: 8 additions & 0 deletions dmometasettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public function set_defaults()
if (!get_option('dxw_members_only_max_age')) {
add_option('dxw_members_only_max_age', 0);
}

if (!get_option('dxw_members_only_max_age_static')) {
add_option('dxw_members_only_max_age_static', 0);
}

if (!get_option('dxw_members_only_max_age_public')) {
add_option('dxw_members_only_max_age_public', 86400);
}
}

/**
Expand Down
8 changes: 5 additions & 3 deletions redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ function dxw_members_only_referrer_in_allow_list()
}

$max_age = absint(get_option('dxw_members_only_max_age'));
$max_age_static = absint(get_option('dxw_members_only_max_age_static'));
$max_age_static = absint(get_option('dxw_members_only_max_age_public'));

do_action('dxw_members_only_redirect');
if (
Expand Down Expand Up @@ -155,14 +157,14 @@ function dxw_members_only_referrer_in_allow_list()

// IP whitelist
if (dxw_members_only_current_ip_in_whitelist()) {
header('Cache-Control: private, max-age=' . $max_age);
header('Cache-Control: private, max-age=' . $max_age_static);
dxw_members_only_serve_uploads();
return;
}

// Referrer whitelist
if (dxw_members_only_referrer_in_allow_list()) {
header('Cache-Control: private, max-age=' . $max_age);
header('Cache-Control: private, max-age=' . $max_age_static);
dxw_members_only_serve_uploads();
return;
}
Expand Down Expand Up @@ -204,7 +206,7 @@ function dxw_members_only_referrer_in_allow_list()
}

if ($hit) {
header('Cache-Control: public');
header('Cache-Control: public, max-age=' . $max_age_public);
dxw_members_only_serve_uploads();
return;
}
Expand Down
25 changes: 23 additions & 2 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,39 @@ function dxw_members_only_options_page()

</table>

<h3><?php _e('Max Age', 'dxwmembersonly') ?></h3>
<h3><?php _e('Browser Cache Max Age', 'dxwmembersonly') ?></h3>
<p><?php _e('A Cache-Control header will be set that prevents server caching on non-public responses, but allows browsers to cache for a maximum number of seconds.', 'dxwmembersonly') ?></p>

<table class="form-table">
<tr valign="top">
<th scope="row"><label for="dxw_members_only_max_age"><?php _e('Max age for cache-control header', 'dxwmembersonly') ?></label></th>
<th scope="row"><label for="dxw_members_only_max_age"><?php _e('Browser cache max age in seconds', 'dxwmembersonly') ?></label></th>
<td>
<input type="number" min="0" step="1" name="dxw_members_only_max_age" id="dxw_members_only_max_age" value="<?php form_option('dxw_members_only_max_age') ?>" class="regular-text">
<span class="description">Defaults to 0 if not set.</span>
</td>
</tr>
</table>

<table class="form-table">
<tr valign="top">
<th scope="row"><label for="dxw_members_only_max_age_static"><?php _e('Browser cache max age in seconds (static content)', 'dxwmembersonly') ?></label></th>
<td>
<input type="number" min="0" step="1" name="dxw_members_only_max_age_static" id="dxw_members_only_max_age_static" value="<?php form_option('dxw_members_only_max_age_static') ?>" class="regular-text">
<span class="description">Defaults to 0 if not set.</span>
</td>
</tr>
</table>

<table class="form-table">
<tr valign="top">
<th scope="row"><label for="dxw_members_only_max_age_public"><?php _e('Browser cache max age in seconds (public content)', 'dxwmembersonly') ?></label></th>
<td>
<input type="number" min="0" step="1" name="dxw_members_only_max_age_public" id="dxw_members_only_max_age_public" value="<?php form_option('dxw_members_only_max_age_public') ?>" class="regular-text">
<span class="description">Defaults to 86400 if not set.</span>
</td>
</tr>
</table>

<?php submit_button() ?>
</form>
</div>
Expand Down

0 comments on commit be5acc7

Please sign in to comment.