| 1 |
<?php |
| 2 |
/** |
| 3 |
* Surge integration file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Integration; |
| 9 |
|
| 10 |
use function Activitypub\is_plugin_active; |
| 11 |
|
| 12 |
/** |
| 13 |
* Surge Cache integration. |
| 14 |
* |
| 15 |
* This class handles the compatibility with the Surge plugin. |
| 16 |
* |
| 17 |
* @see https://wordpress.org/plugins/surge/ |
| 18 |
*/ |
| 19 |
class Surge { |
| 20 |
|
| 21 |
/** |
| 22 |
* The pattern to find the Surge cache config constant. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public static $cache_config_pattern = '/define\s*\(\s*[\'"](WP_CACHE_CONFIG)[\'"]\s*,\s*[\'"](.*?)[\'"]\s*\)\s*;/i'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize the Surge integration. |
| 30 |
*/ |
| 31 |
public static function init() { |
| 32 |
\add_action( 'activate_surge/surge.php', array( self::class, 'add_cache_config' ) ); |
| 33 |
\add_action( 'deactivate_surge/surge.php', array( self::class, 'remove_cache_config' ) ); |
| 34 |
|
| 35 |
\add_filter( 'site_status_tests', array( self::class, 'maybe_add_site_health' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Add the Surge cache config. |
| 40 |
*/ |
| 41 |
public static function add_cache_config() { |
| 42 |
// Check if surge is installed and active. |
| 43 |
if ( ! is_plugin_active( 'surge/surge.php' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Check if the constant already exists. |
| 48 |
if ( \defined( 'WP_CACHE_CONFIG' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$file = self::get_config_file_path(); |
| 53 |
|
| 54 |
if ( ! \wp_is_writable( $file ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! \function_exists( 'WP_Filesystem' ) ) { |
| 59 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 60 |
} |
| 61 |
|
| 62 |
global $wp_filesystem; |
| 63 |
\WP_Filesystem(); |
| 64 |
|
| 65 |
$config = $wp_filesystem->get_contents( $file ); |
| 66 |
|
| 67 |
// Check if the constant already exists. |
| 68 |
if ( \preg_match( self::$cache_config_pattern, $config ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// Add a WP_CACHE_CONFIG to wp-config.php. |
| 73 |
$anchor = "/* That's all, stop editing!"; |
| 74 |
if ( false !== \strpos( $config, $anchor ) ) { |
| 75 |
$config = \str_replace( $anchor, self::get_cache_config() . PHP_EOL . PHP_EOL . $anchor, $config ); |
| 76 |
} elseif ( false !== \strpos( $config, '<?php' ) ) { |
| 77 |
$config = \str_replace( '<?php', '<?php' . PHP_EOL . PHP_EOL . self::get_cache_config() . PHP_EOL, $config ); |
| 78 |
} |
| 79 |
|
| 80 |
$wp_filesystem->put_contents( $file, $config, FS_CHMOD_FILE ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Remove the Surge cache config. |
| 85 |
*/ |
| 86 |
public static function remove_cache_config() { |
| 87 |
if ( ! \defined( 'WP_CACHE_CONFIG' ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$file = self::get_config_file_path(); |
| 92 |
|
| 93 |
if ( ! \wp_is_writable( $file ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
global $wp_filesystem; |
| 98 |
\WP_Filesystem(); |
| 99 |
|
| 100 |
$config = $wp_filesystem->get_contents( $file ); |
| 101 |
|
| 102 |
// Remove the define line. |
| 103 |
$config = \preg_replace( PHP_EOL . self::$cache_config_pattern . PHP_EOL, '', $config ); |
| 104 |
|
| 105 |
$wp_filesystem->put_contents( $file, $config, FS_CHMOD_FILE ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get the config file. |
| 110 |
* |
| 111 |
* @return string|false The config file or false. |
| 112 |
*/ |
| 113 |
public static function get_config_file_path() { |
| 114 |
$config_file = false; |
| 115 |
|
| 116 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors |
| 117 |
if ( @\file_exists( ABSPATH . 'wp-config.php' ) ) { |
| 118 |
|
| 119 |
/** The config file resides in ABSPATH */ |
| 120 |
$config_file = ABSPATH . 'wp-config.php'; |
| 121 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors |
| 122 |
} elseif ( @\file_exists( \dirname( ABSPATH ) . '/wp-config.php' ) && ! @\file_exists( \dirname( ABSPATH ) . '/wp-settings.php' ) ) { |
| 123 |
|
| 124 |
/** The config file resides one level above ABSPATH but is not part of another installation */ |
| 125 |
$config_file = \dirname( ABSPATH ) . '/wp-config.php'; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Filter the config file path. |
| 130 |
* |
| 131 |
* @param string|false $config_file The config file path. |
| 132 |
*/ |
| 133 |
return \apply_filters( 'activitypub_surge_cache_config_file', $config_file ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Maybe add the Surge cache config to the site health. |
| 138 |
* |
| 139 |
* @param array $tests The site health tests. |
| 140 |
* |
| 141 |
* @return array The site health tests with the Surge cache config test. |
| 142 |
*/ |
| 143 |
public static function maybe_add_site_health( $tests ) { |
| 144 |
if ( ! is_plugin_active( 'surge/surge.php' ) ) { |
| 145 |
return $tests; |
| 146 |
} |
| 147 |
|
| 148 |
$tests['direct']['activitypub_test_surge_integration'] = array( |
| 149 |
'label' => \__( 'Surge Test', 'activitypub' ), |
| 150 |
'test' => array( self::class, 'test_surge_integration' ), |
| 151 |
); |
| 152 |
|
| 153 |
return $tests; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Surge integration test. |
| 158 |
* |
| 159 |
* @return array The test result. |
| 160 |
*/ |
| 161 |
public static function test_surge_integration() { |
| 162 |
$result = array( |
| 163 |
'label' => \__( 'Compatibility with Surge', 'activitypub' ), |
| 164 |
'status' => 'good', |
| 165 |
'badge' => array( |
| 166 |
'label' => \__( 'ActivityPub', 'activitypub' ), |
| 167 |
'color' => 'green', |
| 168 |
), |
| 169 |
'description' => \sprintf( |
| 170 |
'<p>%s</p>', |
| 171 |
\__( 'Surge is well configured to work with ActivityPub.', 'activitypub' ) |
| 172 |
), |
| 173 |
'actions' => '', |
| 174 |
'test' => 'test_surge_integration', |
| 175 |
); |
| 176 |
|
| 177 |
if ( ! \defined( 'WP_CACHE_CONFIG' ) ) { |
| 178 |
$result['status'] = 'critical'; |
| 179 |
$result['label'] = \__( 'Surge might not be properly configured.', 'activitypub' ); |
| 180 |
$result['badge']['color'] = 'red'; |
| 181 |
$result['description'] = \sprintf( |
| 182 |
'<p>%s</p>', |
| 183 |
\__( 'Surge isn’t currently set up to work with ActivityPub. While this isn’t a major problem, it’s a good idea to enable support. Without it, some technical files (like JSON) might accidentally show up in your website’s cache and be visible to visitors.', 'activitypub' ) |
| 184 |
); |
| 185 |
$result['actions'] = \sprintf( |
| 186 |
'<p>%s</p>', |
| 187 |
\sprintf( |
| 188 |
// translators: %s: Plugin directory path. |
| 189 |
\__( 'To enable the ActivityPub integration with Surge, add the following line to your <code>wp-config.php</code> file: <br /><code>%s</code>', 'activitypub' ), |
| 190 |
self::get_cache_config() |
| 191 |
) |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
return $result; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get the cache config. |
| 200 |
* |
| 201 |
* @return string The cache config. |
| 202 |
*/ |
| 203 |
public static function get_cache_config() { |
| 204 |
return \sprintf( "define( 'WP_CACHE_CONFIG', '%s/integration/surge-cache-config.php' );", \rtrim( ACTIVITYPUB_PLUGIN_DIR, '/' ) ); |
| 205 |
} |
| 206 |
} |
| 207 |
|