Skip to content

Commit

Permalink
Merge branch 'main' into 97-set-jetpack-to-staging-on-non-production-…
Browse files Browse the repository at this point in the history
…sites
  • Loading branch information
vancoder committed Jul 17, 2024
2 parents a1dccc7 + e345f16 commit 42f53fd
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

## Unreleased

Nothing yet.
* `disable_apple_news_non_prod_push`: Added a feature to disable pushing to Apple News when not on a production environment.

## 3.1.0

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ queries with the relevant filters to disable them:

This feature removes selected nodes from the admin bar.

### `disable_apple_news_non_prod_push`

This feature disables pushing to Apple News when not on a production environment. This is determined by setting the `WP_ENVIRONMENT_TYPE` environment variable, or the `WP_ENVIRONMENT_TYPE` constant.

### `disable_attachment_routing`

This feature disables WordPress attachment pages entirely from the front end of the site.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Class file for Disable_Apple_News_Non_Prod_Push
*
* (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;

/**
* Disables Apple News Push on Non Production Environments.
*/
final class Disable_Apple_News_Non_Prod_Push implements Feature {
/**
* Boot the feature.
*/
public function boot(): void {
add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 9999 );
}

/**
* Filter the Apple News push skip flag. If we are not on a production environment, skip the push.
*
* @param bool $skip Should we skip the Apple News push.
*/
public function filter_apple_news_skip_push( bool $skip ): bool {
// If we are on a production environment, don't modify the value.
if ( 'production' === wp_get_environment_type() ) {
return $skip;
}

// All other cases, return true.
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 @@ -33,6 +33,10 @@ function load(): void {
'clean_admin_bar',
new Features\Clean_Admin_Bar(),
),
new Feature(
'disable_apple_news_non_prod_push',
new Features\Disable_Apple_News_Non_Prod_Push(),
),
new Feature(
'disable_attachment_routing',
new Features\Disable_Attachment_Routing(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Class file for Disable_Apple_News_Non_Prod_Push
*
* (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\Alleyvate\Feature;
use Mantle\Testkit\Test_Case;

/**
* Test Disable_Apple_News_Non_Prod_Push
*/
final class Disable_Apple_News_Non_Prod_Push_Test extends Test_Case {
/**
* The Feature class.
*
* @var Disable_Apple_News_Non_Prod_Push
*/
protected $feature;

/**
* Setup before test.
*/
protected function setUp(): void {
$this->feature = new Disable_Apple_News_Non_Prod_Push();
}

/**
* Set the current environment value.
*
* @param string $environment The environment name to use.
*/
protected function setEnvironment( string $environment ): void {
// Required because `wp_get_environment_type` uses `getenv` to retrieve the value.
putenv( 'WP_ENVIRONMENT_TYPE=' . $environment ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv
$_ENV['WP_ENVIRONMENT_TYPE'] = $environment;
}

/**
* Test that the filter_apple_news_skip_push method returns false when passed false on a production_ environment.
*/
public function testFalseFilterAppleNewsSkipPushProductionEnvironment() {
$skip = false;

$this->setEnvironment( 'production' );

$result = $this->feature->filter_apple_news_skip_push( $skip );

$this->assertFalse( $result );
}

/**
* Test that the filter_apple_news_skip_push method returns true when passed true on a production_ environment.
*/
public function testTrueFilterAppleNewsSkipPushProductionEnvironment() {
$skip = true;

$this->setEnvironment( 'production' );

$result = $this->feature->filter_apple_news_skip_push( $skip );

$this->assertTrue( $result );
}

/**
* Test that the filter_apple_news_skip_push method returns true when passed false on a non-production_ environment.
*/
public function testFalseFilterAppleNewsSkipPushOtherEnvironments() {
$skip = false;

$this->setEnvironment( 'local' );

$result = $this->feature->filter_apple_news_skip_push( $skip );

$this->assertTrue( $result );
}

/**
* Test that the filter_apple_news_skip_push method returns true when passed true on a non-production_ environment.
*/
public function testTrueFilterAppleNewsSkipPushOtherEnvironments() {
$skip = true;

$this->setEnvironment( 'local' );

$result = $this->feature->filter_apple_news_skip_push( $skip );

$this->assertTrue( $result );
}
}
7 changes: 7 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
* @package wp-alleyvate
*/

/*
* This is required to override `wp_get_environment_type()`. Otherwise
* the method caches the initial result and always returns it, even if
* we modify the environment variable.
*/
\define( 'WP_RUN_CORE_TESTS', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound

\Mantle\Testing\manager()
// Fires on 'muplugins_loaded'.
->loaded(
Expand Down

0 comments on commit 42f53fd

Please sign in to comment.