Skip to content
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
37 changes: 20 additions & 17 deletions integration/class-litespeed-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,32 @@ class Litespeed_Cache {
*/
public static $marker = 'ActivityPub LiteSpeed Cache';

/**
* The LiteSpeed Cache plugin slug.
*
* @var string
*/
public static $plugin_slug = 'litespeed-cache/litespeed-cache.php';

/**
* Initialize the integration.
*/
public static function init() {
// Add rules if LiteSpeed Cache is active and rules aren't set.
if ( is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) && ! \get_option( self::$option_name ) ) {
self::add_htaccess_rules();
}
if ( is_plugin_active( self::$plugin_slug ) ) {
if ( ! \get_option( self::$option_name ) ) {
self::add_htaccess_rules();
}

\add_filter( 'site_status_tests', array( self::class, 'add_site_health_test' ) );

// Remove rules if LiteSpeed Cache is not active but rules were previously set.
if ( ! is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) && \get_option( self::$option_name ) ) {
// Remove rules if LiteSpeed Cache is not active but rules were previously set.
} elseif ( \get_option( self::$option_name ) ) {
self::remove_htaccess_rules();
\delete_option( self::$option_name );
}

// Clean up when LiteSpeed Cache plugin is deleted.
\add_action( 'deleted_plugin', array( self::class, 'on_plugin_deleted' ) );

\add_filter( 'site_status_tests', array( self::class, 'maybe_add_site_health' ) );
}

/**
Expand All @@ -68,7 +75,7 @@ public static function init() {
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
*/
public static function on_plugin_deleted( $plugin_file ) {
if ( 'litespeed-cache/litespeed-cache.php' === $plugin_file && \get_option( self::$option_name ) ) {
if ( self::$plugin_slug === $plugin_file && \get_option( self::$option_name ) ) {
self::remove_htaccess_rules();
}
}
Expand Down Expand Up @@ -96,17 +103,13 @@ public static function remove_htaccess_rules() {
}

/**
* Maybe add the LiteSpeed Cache config to the site health.
* Add the LiteSpeed Cache config test to site health.
*
* @param array $tests The site health tests.
*
* @return array The site health tests with the LiteSpeed Cache config test.
*/
public static function maybe_add_site_health( $tests ) {
if ( ! is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
return $tests;
}

public static function add_site_health_test( $tests ) {
$tests['direct']['activitypub_test_litespeed_cache_integration'] = array(
'label' => \__( 'LiteSpeed Cache Test', 'activitypub' ),
'test' => array( self::class, 'test_litespeed_cache_integration' ),
Expand Down Expand Up @@ -162,7 +165,7 @@ public static function test_litespeed_cache_integration() {
*
* @return bool True on success, false on failure.
*/
public static function append_with_markers( $marker, $rules ) {
private static function append_with_markers( $marker, $rules ) {
$htaccess_file = self::get_htaccess_file_path();

if ( ! \wp_is_writable( $htaccess_file ) ) {
Expand Down Expand Up @@ -205,7 +208,7 @@ public static function append_with_markers( $marker, $rules ) {
*
* @return string|false The htaccess file or false.
*/
public static function get_htaccess_file_path() {
private static function get_htaccess_file_path() {
$htaccess_file = false;

// phpcs:ignore WordPress.PHP.NoSilencedErrors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function test_cleanup_when_litespeed_deactivated() {
\add_filter(
'activitypub_is_plugin_active',
function ( $is_active, $plugin ) {
if ( 'litespeed-cache/litespeed-cache.php' === $plugin ) {
if ( Litespeed_Cache::$plugin_slug === $plugin ) {
return false;
}
return $is_active;
Expand Down Expand Up @@ -269,7 +269,7 @@ public function test_cleanup_when_litespeed_deleted() {
$this->assertStringContainsString( Litespeed_Cache::$rules, $contents_before );

// Simulate LiteSpeed Cache plugin deletion.
\do_action( 'deleted_plugin', 'litespeed-cache/litespeed-cache.php', false );
\do_action( 'deleted_plugin', Litespeed_Cache::$plugin_slug, false );

// Verify cleanup.
$this->assertFalse( \get_option( Litespeed_Cache::$option_name ), 'Option should be deleted when plugin is deleted' );
Expand Down