diff --git a/.gitignore b/.gitignore index af29aa30..532077d3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ composer.lock .phpunit.result.cache .php-cs-fixer.cache .vscode +.DS_Store +.idea diff --git a/README.md b/README.md index d012f93c..673679ee 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,10 @@ This feature disables WordPress sticky posts entirely, including the ability to This feature disables WordPress from sending or receiving trackbacks or pingbacks. +### `enabled_jetpack_safe_mode` + +This feature ensures Jetpack is in safe mode where applicable (currently Pantheon only). + ### `force_two_factor_authentication` This feature forces users with `edit_posts` permissions to use two factor authentication (2fa) for their accounts. diff --git a/src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php b/src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php new file mode 100644 index 00000000..0fcb6fff --- /dev/null +++ b/src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @package wp-alleyvate + */ + +namespace Alley\WP\Alleyvate\Features; + +use Alley\WP\Types\Feature; + +/** + * Enables Jetpack safe mode for non-production environments, currently only for Pantheon. + */ +final class Enable_Jetpack_Safe_Mode implements Feature { + /** + * Boot the feature. + */ + public function boot(): void { + if ( + isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && + 'live' !== $_ENV['PANTHEON_ENVIRONMENT'] + ) { + add_filter( 'jetpack_is_development_site', '__return_true' ); + } + } +} diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index 84ee4da0..dec88521 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -73,6 +73,10 @@ function load(): void { 'disallow_file_edit', new Features\Disallow_File_Edit(), ), + new Feature( + 'enable_jetpack_safe_mode', + new Features\Enable_Jetpack_Safe_Mode(), + ), new Feature( 'login_nonce', new Features\Login_Nonce(), diff --git a/tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php b/tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php new file mode 100644 index 00000000..fea65bb5 --- /dev/null +++ b/tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @package wp-alleyvate + */ + +declare( strict_types=1 ); + +namespace Alley\WP\Alleyvate\Features; + +use Mantle\Testkit\Test_Case; + +/** + * Tests for the enabling of Jetpack safe mode. + */ +final class EnabledJetpackSafeModeTest extends Test_Case { + + /** + * Feature instance. + * + * @var Enable_Jetpack_Safe_Mode + */ + private Enable_Jetpack_Safe_Mode $feature; + + /** + * Set up. + */ + protected function setUp(): void { + parent::setUp(); + + $this->feature = new Enable_Jetpack_Safe_Mode(); + } + + /** + * Test that the feature enabled Jetpack safe mode. + */ + public function test_enable_jetpack_safe_mode(): void { + $this->assertFalse( apply_filters( 'jetpack_is_development_site', false ), 'jetpack_is_development_site should not be true prior to boot.' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound + $this->setEnvironment( 'test' ); + $this->feature->boot(); + $this->assertTrue( apply_filters( 'jetpack_is_development_site', false ), 'jetpack_is_development_site should be true after boot.' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound + } + + /** + * Set the current environment value. + * + * @param string $environment The environment name to use. + */ + protected function setEnvironment( string $environment ): void { + putenv( 'PANTHEON_ENVIRONMENT=' . $environment ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv + $_ENV['PANTHEON_ENVIRONMENT'] = $environment; + } +}