Skip to content

Commit

Permalink
Proposal to add a Option field for REST API TTL
Browse files Browse the repository at this point in the history
For a general use case where a customer wants to set a specific cache TTL for WP REST API endpoints, we add a specific field for the REST API TTL value

Alternatively, we can implement a custom plugin to set specific cache headers for these endpoints too, but that will be covered by another proposal or docs guide

The intention here is to avoid modifying wp-config.php to achieve this, and specifically for those who want to disable cache for
  • Loading branch information
eabquina committed Apr 10, 2019
1 parent b6574b3 commit 57ba357
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions wp-content/mu-plugins/pantheon/pantheon-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ protected function __construct() {
protected function setup() {
$this->options = get_option( self::SLUG, array() );
$this->default_options = array(
'default_ttl' => 600
'default_ttl' => 600,
'default_ttl_rest' => 600
);
$this->options = wp_parse_args( $this->options, $this->default_options );

Expand Down Expand Up @@ -118,6 +119,7 @@ public function action_admin_init() {
register_setting( self::SLUG, self::SLUG, array( self::$instance, 'sanitize_options' ) );
add_settings_section( 'general', false, '__return_false', self::SLUG );
add_settings_field( 'default_ttl', null, array( self::$instance, 'default_ttl_field' ), self::SLUG, 'general' );
add_settings_field( 'default_ttl_rest', null, array( self::$instance, 'default_ttl_field_rest' ), self::SLUG, 'general' );
}


Expand Down Expand Up @@ -166,6 +168,16 @@ public function default_ttl_field() {
echo '<input type="text" name="' . self::SLUG . '[default_ttl]" value="' . $this->options['default_ttl'] . '" size="5" /> ' . __( 'seconds', 'pantheon-cache' );
}

/**
* Add the HTML for the default TTL field for REST endpoints.
*
* @return void
*/
public function default_ttl_field_rest() {
echo '<p>' . __( 'Specific TTL value for REST endpoints. Suggestion is to at least have 60 at minimum.', 'pantheon-cache' ) . '</p>';
echo '<input type="text" name="' . self::SLUG . '[default_ttl_rest]" value="' . $this->options['default_ttl_rest'] . '" size="5" /> ' . __( 'seconds', 'pantheon-cache' );
}


/**
* Sanitize our options.
Expand All @@ -178,9 +190,15 @@ public function sanitize_options( $in ) {

// Validate default_ttl
$out['default_ttl'] = absint( $in['default_ttl'] );
$out['default_ttl_rest'] = absint( $in['default_ttl_rest'] );

if ( $out['default_ttl'] < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$out['default_ttl'] = 60;
}

if ( $out['default_ttl_rest'] < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$out['default_ttl_rest'] = 60;
}

return $out;
}
Expand Down Expand Up @@ -274,15 +292,14 @@ public function cache_add_headers() {
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}

header( 'cache-control: public, max-age=' . $ttl );
}

/**
* Send the cache control header for REST API requests
*/
public function filter_rest_post_dispatch_send_cache_control( $response, $server ) {
$ttl = absint( $this->options['default_ttl'] );
$ttl = absint( $this->options['default_ttl_rest'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}
Expand Down

0 comments on commit 57ba357

Please sign in to comment.