From a1dccc743b35ef4bdee879e38b4a6bfd17222cf4 Mon Sep 17 00:00:00 2001 From: Grant Mangham Date: Wed, 17 Jul 2024 13:56:15 -0700 Subject: [PATCH 1/3] adds jetpack safe mode feature --- .gitignore | 2 + README.md | 4 ++ .../class-enable-jetpack-safe-mode.php | 32 ++++++++++ src/alley/wp/alleyvate/load.php | 4 ++ .../test-enable-jetpack-safe-mode.php | 59 +++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php create mode 100644 tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php 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 17e0e77c..8466449f 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,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..e621aeb5 --- /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'] ) && + $_ENV['PANTHEON_ENVIRONMENT'] !== 'live' + ) { + 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 6cdb6ac4..de94246c 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -65,6 +65,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/test-enable-jetpack-safe-mode.php b/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php new file mode 100644 index 00000000..f68d3ea3 --- /dev/null +++ b/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.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 Test_Enabled_Jetpack_Safe_Mode 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.' ); + $this->setEnvironment( 'test' ); + $this->feature->boot(); + $this->assertTrue( apply_filters( 'jetpack_is_development_site', false ), 'jetpack_is_development_site should be true after boot.' ); + } + + /** + * 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; + } +} From edcceec75da65d60d3453625179a8cdb400cadd8 Mon Sep 17 00:00:00 2001 From: Grant Mangham Date: Wed, 17 Jul 2024 14:02:21 -0700 Subject: [PATCH 2/3] phpcs --- .../wp/alleyvate/features/class-enable-jetpack-safe-mode.php | 2 +- .../wp/alleyvate/features/test-enable-jetpack-safe-mode.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 index e621aeb5..0fcb6fff 100644 --- a/src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php +++ b/src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php @@ -24,7 +24,7 @@ final class Enable_Jetpack_Safe_Mode implements Feature { public function boot(): void { if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && - $_ENV['PANTHEON_ENVIRONMENT'] !== 'live' + 'live' !== $_ENV['PANTHEON_ENVIRONMENT'] ) { add_filter( 'jetpack_is_development_site', '__return_true' ); } diff --git a/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php b/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php index f68d3ea3..c065dc1b 100644 --- a/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php +++ b/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php @@ -41,10 +41,10 @@ protected function setUp(): void { * 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.' ); + $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.' ); + $this->assertTrue( apply_filters( 'jetpack_is_development_site', false ), 'jetpack_is_development_site should be true after boot.' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound } /** From e2616e1804b404a7b3161e31a9027348091bbc01 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 4 Sep 2024 12:28:56 -0400 Subject: [PATCH 3/3] Migrating test to psr-4 --- .../WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php => Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php} (96%) diff --git a/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php b/tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php similarity index 96% rename from tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php rename to tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php index c065dc1b..fea65bb5 100644 --- a/tests/alley/wp/alleyvate/features/test-enable-jetpack-safe-mode.php +++ b/tests/Alley/WP/Alleyvate/Features/EnabledJetpackSafeModeTest.php @@ -19,7 +19,7 @@ /** * Tests for the enabling of Jetpack safe mode. */ -final class Test_Enabled_Jetpack_Safe_Mode extends Test_Case { +final class EnabledJetpackSafeModeTest extends Test_Case { /** * Feature instance.