Skip to content

Commit

Permalink
adds jetpack safe mode feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vancoder committed Jul 17, 2024
1 parent d6baa5b commit a1dccc7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.vscode
.DS_Store
.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions src/alley/wp/alleyvate/features/class-enable-jetpack-safe-mode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Class file for Enable_Jetpack_Safe_Mode
*
* (c) Alley <[email protected]>
*
* 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' );
}
}
}
4 changes: 4 additions & 0 deletions src/alley/wp/alleyvate/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Class file for Test_Enable_Jetpack_Safe_Mode
*
* (c) Alley <[email protected]>
*
* 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;
}
}

0 comments on commit a1dccc7

Please sign in to comment.