Skip to content

Commit

Permalink
Replace mu-plugin with a format consistent with pantheon-systems/pant…
Browse files Browse the repository at this point in the history
…heon-mu-plugin

This update aligns the Pantheon mu-plugin format to our standalone repository (https://github.com/pantheon-systems/pantheon-mu-plugin), and will allow
for the mu-plugin to receive updates from that repo whenever an updated version of WordPress is released.

For more information, see https://pantheon.io/docs/start-states/wordpress#2022-11-01
  • Loading branch information
jazzsequence authored and Pantheon Automation committed Nov 2, 2022
1 parent 842aca1 commit bf18d9e
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 37 deletions.
76 changes: 76 additions & 0 deletions wp-content/mu-plugins/loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Plugin Name: Pantheon MU Plugin Loader
* Description: Loads the MU plugins required to run the site
* Author: Pantheon Systems
* Author URI: https://pantheon.io
* Version: 1.0
*/

if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) {
return;
}

// Add mu-plugins here.
$pantheon_mu_plugins = [
'pantheon-mu-plugin/pantheon.php',
];

foreach ( $pantheon_mu_plugins as $file ) {
require_once WPMU_PLUGIN_DIR . '/' . $file;
}
unset( $file );

add_action( 'pre_current_active_plugins', function () use ( $pantheon_mu_plugins ) {
global $plugins, $wp_list_table;

// Add our own mu-plugins to the page.
foreach ( $pantheon_mu_plugins as $plugin_file ) {
// Do not apply markup/translate as it'll be cached.
$plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false );

if ( empty( $plugin_data['Name'] ) ) {
$plugin_data['Name'] = $plugin_file;
}

$plugins['mustuse'][ $plugin_file ] = $plugin_data;
}

// Recount totals.
$GLOBALS['totals']['mustuse'] = count( $plugins['mustuse'] );

// Only apply the rest if we're actually looking at the page.
if ( $GLOBALS['status'] !== 'mustuse' ) {
return;
}

// Reset the list table's data.
$wp_list_table->items = $plugins['mustuse'];
foreach ( $wp_list_table->items as $plugin_file => $plugin_data ) {
$wp_list_table->items[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
}

$total_this_page = $GLOBALS['totals']['mustuse'];

if ( $GLOBALS['orderby'] ) {
uasort( $wp_list_table->items, [ $wp_list_table, '_order_callback' ] );
}

// Force showing all plugins.
// See https://core.trac.wordpress.org/ticket/27110.
$plugins_per_page = $total_this_page;

$wp_list_table->set_pagination_args( [
'total_items' => $total_this_page,
'per_page' => $plugins_per_page,
] );
});

add_filter( 'network_admin_plugin_action_links', function ( $actions, $plugin_file, $plugin_data, $context ) use ( $pantheon_mu_plugins ) {
if ( $context !== 'mustuse' || ! in_array( $plugin_file, $hm_mu_plugins, true ) ) {
return $actions;
}

$actions[] = sprintf( '<span style="color:#333">File: <code>%s</code></span>', $plugin_file );
return $actions;
}, 10, 4 );
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ protected function setup() {

add_action( 'admin_post_pantheon_cache_flush_site', array( $this, 'flush_site' ) );

add_action( 'send_headers', array( $this, 'cache_add_headers' ) );
if ( ! is_admin() && function_exists( 'is_user_logged_in' ) && ! is_user_logged_in() ) {
add_action( 'send_headers', array( $this, 'cache_add_headers' ) );
}
else {
add_action( 'send_headers', array( $this, 'no_cache_add_headers' ) );
}
add_filter( 'rest_post_dispatch', array( $this, 'filter_rest_post_dispatch_send_cache_control' ), 10, 2 );

add_action( 'admin_notices', function(){
Expand Down Expand Up @@ -328,24 +333,15 @@ public function view_settings_page() {
}

/**
* Get the cache-control header value
* Set a stronger cache-control header for admin or logged in requests.
*
* This removes "max-age=0" which could hypothetically be used by
* Varnish on an immediate subsequent request.
*
* @return void
*/
private function get_cache_control_header_value() {
if ( ! is_admin() && ! is_user_logged_in() ) {
$ttl = absint( $this->options['default_ttl'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}

return sprintf( 'public, max-age=%d', $ttl );
} else {
return 'no-cache, no-store, must-revalidate';
}
public function no_cache_add_headers() {
header( 'cache-control: no-cache, no-store, must-revalidate');
}

/**
Expand All @@ -354,17 +350,23 @@ private function get_cache_control_header_value() {
* @return void
*/
public function cache_add_headers() {
header( sprintf( 'cache-control: %s', $this->get_cache_control_header_value() ) );
$ttl = absint( $this->options['default_ttl'] );
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
*
* @param WP_REST_Response $response Response.
* @return WP_REST_Response Response.
*/
public function filter_rest_post_dispatch_send_cache_control( $response ) {
$response->header( 'Cache-Control', $this->get_cache_control_header_value() );
public function filter_rest_post_dispatch_send_cache_control( $response, $server ) {
$ttl = absint( $this->options['default_ttl'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}
$response->header( 'Cache-Control', 'public, max-age=' . $ttl );
return $response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pantheon
* Plugin URI: https://pantheon.io/
* Description: Building on Pantheon's and WordPress's strengths, together.
* Version: 0.3
* Version: 1.0.0
* Author: Pantheon
* Author URI: https://pantheon.io/
*
Expand All @@ -12,18 +12,18 @@

if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {

require_once 'pantheon/pantheon-page-cache.php';
require_once 'inc/pantheon-page-cache.php';
if ( ! defined( 'DISABLE_PANTHEON_UPDATE_NOTICES' ) || ! DISABLE_PANTHEON_UPDATE_NOTICES ) {
require_once 'pantheon/pantheon-updates.php';
require_once 'inc/pantheon-updates.php';
}
if ( ! defined('RETURN_TO_PANTHEON_BUTTON') || RETURN_TO_PANTHEON_BUTTON ) {
require_once 'pantheon/pantheon-login-form-mods.php';
require_once 'inc/pantheon-login-form-mods.php';
}
if ( 'dev' === $_ENV['PANTHEON_ENVIRONMENT'] && function_exists( 'wp_is_writable' ) ) {
require_once 'pantheon/pantheon-plugin-install-notice.php';
require_once 'inc/pantheon-plugin-install-notice.php';
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once 'pantheon/cli.php';
require_once 'inc/cli.php';
}
if ( ! defined( 'FS_METHOD' ) ) {
/**
Expand Down
12 changes: 0 additions & 12 deletions wp-content/mu-plugins/pantheon/README.md

This file was deleted.

0 comments on commit bf18d9e

Please sign in to comment.