Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EPC conflicts #15

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion includes/CacheTypes/Skip404.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ public function __construct() {
new OptionListener( Performance::OPTION_SKIP_404, [ __CLASS__, 'maybeAddRules' ] );

add_filter( 'newfold_update_htaccess', [ $this, 'onUpdateHtaccess' ] );
add_action( 'admin_init', [ $this, 'registerSettings' ] );
add_action( 'admin_init', [ $this, 'registerSettings' ], 11 );
}

/**
* Register our setting to the main performance settings section.
*/
public function registerSettings() {

global $wp_settings_fields;

add_settings_field(
Performance::OPTION_SKIP_404,
__( 'Skip WordPress 404 Handling For Static Files', 'newfold-performance-module' ),
Expand All @@ -54,13 +56,25 @@ public function registerSettings() {

register_setting( 'general', Performance::OPTION_SKIP_404 );

// Remove the setting from EPC if it exists - TODO: Remove when no longer using EPC
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
unset( $wp_settings_fields['general']['epc_settings_section'] );
unregister_setting( 'general', 'epc_skip_404_handling' );
}

}

/**
* When updating .htaccess, also update our rules as appropriate.
*/
public function onUpdateHtaccess() {
self::maybeAddRules( getSkip404Option() );

// Remove the old option from EPC, if it exists
if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'epc_skip_404_handling', 0 ) ) ) {
update_option( 'epc_skip_404_handling', 0 );
delete_option( 'epc_skip_404_handling' );
}
}

/**
Expand Down
28 changes: 21 additions & 7 deletions includes/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Performance {
/**
* Constructor.
*
* @param Container $container
* @param Container $container
*/
public function __construct( Container $container ) {

Expand All @@ -71,7 +71,7 @@ public function __construct( Container $container ) {

// Ensure that purgeable cache types are enabled before showing the UI.
if ( $cachePurger->canPurge() ) {
add_action( 'admin_bar_menu', [ $this, 'adminBarMenu' ], 999 );
add_action( 'admin_bar_menu', [ $this, 'adminBarMenu' ], 100 );
}

$container->set( 'cachePurger', $cachePurger );
Expand Down Expand Up @@ -102,7 +102,7 @@ function () {
*/
public function hooks( Container $container ) {

add_action( 'admin_init', [ $this, 'registerSettings' ] );
add_action( 'admin_init', [ $this, 'registerSettings' ], 11 );

new OptionListener( self::OPTION_CACHE_LEVEL, [ $this, 'onCacheLevelChange' ] );

Expand Down Expand Up @@ -143,18 +143,26 @@ public function onRewrite() {
/**
* On cache level change, update the response headers.
*
* @param int|null $cacheLevel The cache level.
* @param int|null $cacheLevel The cache level.
*/
public function onCacheLevelChange( $cacheLevel ) {
/**
* @var ResponseHeaderManager $responseHeaderManager
*/
$responseHeaderManager = $this->container->get( 'responseHeaderManager' );
$responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', absint( $cacheLevel ) );

// Remove the old option from EPC, if it exists
if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) {
update_option( 'endurance_cache_level', 0 );
delete_option( 'endurance_cache_level' );
}
}

public function registerSettings() {

global $wp_settings_fields;

$section_name = self::SETTINGS_SECTION;

add_settings_section(
Expand All @@ -173,18 +181,24 @@ public function registerSettings() {
);

register_setting( 'general', self::OPTION_CACHE_LEVEL );

// Remove the setting from EPC if it exists - TODO: Remove when no longer using EPC
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
unset( $wp_settings_fields['general']['epc_settings_section'] );
unregister_setting( 'general', 'endurance_cache_level' );
}
}

/**
* Add options to the WordPress admin bar.
*
* @param \WP_Admin_Bar $wp_admin_bar
* @param \WP_Admin_Bar $wp_admin_bar
*/
public function adminBarMenu( \WP_Admin_Bar $wp_admin_bar ) {

// If the EPC MU plugin exists, don't add an extra cache clearing option.
// If the EPC MU plugin exists, remove its cache clearing options.
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
return;
$wp_admin_bar->remove_node( 'epc_purge_menu' );
}

if ( current_user_can( 'manage_options' ) ) {
Expand Down