diff --git a/CHANGELOG.md b/CHANGELOG.md index 8abfbb2f..7f984868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 17e0e77c..0ae125e8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php new file mode 100644 index 00000000..00269f51 --- /dev/null +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php @@ -0,0 +1,42 @@ + + * + * 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; + } +} diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index 6cdb6ac4..ecaa7832 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -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(), diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-non-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-non-prod-push.php new file mode 100644 index 00000000..8e2501f2 --- /dev/null +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-non-prod-push.php @@ -0,0 +1,98 @@ + + * + * 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 ); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 11baec86..c93ade29 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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(