From aa79cd28b1309500b0c10694ae6d3847e42b24e4 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 15 Jun 2023 11:51:44 -0500 Subject: [PATCH 01/21] setting up Apple News push feature --- .../class-disable-apple-news-no-prod-push.php | 59 ++++++++++++ src/alley/wp/alleyvate/load.php | 1 + .../test-disable-apple-news-no-prod-push.php | 93 +++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php create mode 100644 tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php new file mode 100644 index 00000000..985366a2 --- /dev/null +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.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 + */ + +namespace Alley\WP\Alleyvate\Features; + +use Alley\WP\Alleyvate\Feature; + +/** + * Disables Apple News Push on Non Production Environments. + */ +final class Disable_Apple_News_No_Prod_Push implements Feature { + /** + * Boot the feature. + */ + public function boot(): void { + add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); + } + + /** + * 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 ) { + // If we are not on a production environment according to WP_ENV, don't modify the value. + if ( defined( 'WP_ENV' ) && 'production' === WP_ENV ) { + return $skip; + } + // If we are on Pantheon LIVE, don't modify the value. + if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { + return $skip; + } + // If we are on VIP Production, don't modify the value. + if ( defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { + return $skip; + } + // All other cases, return true - but allow it to be filtered. + + /** + * Filters the final value in filter_apple_news_skip_push. + * + * Allow developers to disable this feature. + * + * @since 2.0.0 + * + * @param bool $skip Should we skip the Apple News push? + */ + return apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); + } +} diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index e71a1321..0028c02e 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -27,6 +27,7 @@ function load(): void { * @var Feature[] $features */ $features = [ + 'disable_apple_new_push' => new Features\Disable_Apple_News_No_Prod_Push(), 'disable_comments' => new Features\Disable_Comments(), 'disable_trackbacks' => new Features\Disable_Trackbacks(), 'disallow_file_edit' => new Features\Disallow_File_Edit(), diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php new file mode 100644 index 00000000..f10b58ad --- /dev/null +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -0,0 +1,93 @@ + + * + * 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; + +class Disable_Apple_News_No_Prod_Push_Test extends TestCase { + // Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. + public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = false; + define('WP_ENV', 'production'); + + $result = $instance->filter_apple_news_skip_push($skip); + + $this->assertFalse($result); + } + + // Test that the filter_apple_news_skip_push method returns false when passed false on a Pantheon live environment. + public function testFalseFilterAppleNewsSkipPushPantheonLiveEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = false; + $_ENV['PANTHEON_ENVIRONMENT'] = 'live'; + + $result = $instance->filter_apple_news_skip_push($skip); + + $this->assertFalse($result); + } + + // Test that the filter_apple_news_skip_push method returns false when passed false on a VIP production environment. + public function testFalseFilterAppleNewsSkipPushVipProductionEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = false; + define('VIP_GO_ENV', 'production'); + + $result = $instance->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 non-production environment. + public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = true; + define('WP_ENV', 'production'); + + $result = $instance->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 Pantheon live environment. + public function testTrueFilterAppleNewsSkipPushPantheonLiveEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = true; + $_ENV['PANTHEON_ENVIRONMENT'] = 'live'; + + $result = $instance->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 VIP production environment. + public function testTrueFilterAppleNewsSkipPushVipProductionEnvironment() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = true; + define('VIP_GO_ENV', 'production'); + + $result = $instance->filter_apple_news_skip_push($skip); + + $this->assertTrue($result); + } + + public function testFilterAppleNewsSkipPushOtherEnvironments() { + $instance = new Disable_Apple_News_No_Prod_Push(); + $skip = false; + + $result = $instance->filter_apple_news_skip_push($skip); + + $this->assertTrue($result); + } +} From b06aaee8ac308f25090065184234d7e2c5892268 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 15 Jun 2023 12:09:02 -0500 Subject: [PATCH 02/21] fixing some tests --- .../features/test-disable-apple-news-no-prod-push.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index f10b58ad..f05da4ab 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -15,7 +15,16 @@ use Alley\WP\Alleyvate\Feature; use Mantle\Testkit\Test_Case; -class Disable_Apple_News_No_Prod_Push_Test extends TestCase { +class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { + /** + * Set up. + */ + protected function setUp(): void { + parent::setUp(); + + // Need to remove the current definition of WP_ENV. + } + // Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { $instance = new Disable_Apple_News_No_Prod_Push(); From bcde6b5c22b93aa442f902357c9219437f44fb83 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 15 Jun 2023 13:31:52 -0500 Subject: [PATCH 03/21] trying to mock is_production_environment --- .../class-disable-apple-news-no-prod-push.php | 33 ++++-- .../test-disable-apple-news-no-prod-push.php | 110 +++++++++--------- 2 files changed, 76 insertions(+), 67 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index 985366a2..72933354 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -31,16 +31,8 @@ public function boot(): void { * @param bool $skip Should we skip the Apple News push. */ public function filter_apple_news_skip_push( bool $skip ) { - // If we are not on a production environment according to WP_ENV, don't modify the value. - if ( defined( 'WP_ENV' ) && 'production' === WP_ENV ) { - return $skip; - } - // If we are on Pantheon LIVE, don't modify the value. - if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { - return $skip; - } - // If we are on VIP Production, don't modify the value. - if ( defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { + // If we are on a production environment, don't modify the value. + if ( $this->is_production_environment() ) { return $skip; } // All other cases, return true - but allow it to be filtered. @@ -56,4 +48,25 @@ public function filter_apple_news_skip_push( bool $skip ) { */ return apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); } + + /** + * Detect if we are on a production environment. + * + * @return boolean + */ + private function is_production_environment(): bool { + // If we are not on a production environment according to WP_ENV, return true. + if ( defined( 'WP_ENV' ) && 'production' === WP_ENV ) { + return true; + } + // If we are on Pantheon LIVE, return true. + if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { + return true; + } + // If we are on VIP Production, don't modify the value. + if ( defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { + return true; + } + return false; + } } diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index f05da4ab..57a11162 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -15,88 +15,84 @@ use Alley\WP\Alleyvate\Feature; use Mantle\Testkit\Test_Case; +/** + * Test Disable_Apple_News_No_Prod_Push + */ class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** - * Set up. + * Mocks the is_production_environment method. + * + * @param bool $result The result to mock. + * @return void */ - protected function setUp(): void { - parent::setUp(); - - // Need to remove the current definition of WP_ENV. + protected function mockResult( $result ) { + $mock = $this->getMockBuilder( 'Disable_Apple_News_No_Prod_Push' ) + ->setMethods( [ 'is_production_environment' ] ) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( 'is_production_environment' ) + ->willReturn( $result ); } - // Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. + /** + * Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. + * + * @return void + */ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = false; - define('WP_ENV', 'production'); + $skip = false; + $this->mockResult( true ); - $result = $instance->filter_apple_news_skip_push($skip); + $result = $instance->filter_apple_news_skip_push( $skip ); - $this->assertFalse($result); + $this->assertFalse( $result ); } - // Test that the filter_apple_news_skip_push method returns false when passed false on a Pantheon live environment. - public function testFalseFilterAppleNewsSkipPushPantheonLiveEnvironment() { - $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = false; - $_ENV['PANTHEON_ENVIRONMENT'] = 'live'; - - $result = $instance->filter_apple_news_skip_push($skip); - - $this->assertFalse($result); - } - - // Test that the filter_apple_news_skip_push method returns false when passed false on a VIP production environment. - public function testFalseFilterAppleNewsSkipPushVipProductionEnvironment() { - $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = false; - define('VIP_GO_ENV', 'production'); - - $result = $instance->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 non-production environment. + /** + * Test that the filter_apple_news_skip_push method returns true when passed true on a production environment. + * + * @return void + */ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = true; - define('WP_ENV', 'production'); + $skip = true; + $this->mockResult( true ); - $result = $instance->filter_apple_news_skip_push($skip); + $result = $instance->filter_apple_news_skip_push( $skip ); - $this->assertTrue($result); + $this->assertTrue( $result ); } - // Test that the filter_apple_news_skip_push method returns true when passed true on a Pantheon live environment. - public function testTrueFilterAppleNewsSkipPushPantheonLiveEnvironment() { + /** + * Test that the filter_apple_news_skip_push method returns true when passed false on a non-production environment. + * + * @return void + */ + public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = true; - $_ENV['PANTHEON_ENVIRONMENT'] = 'live'; + $skip = false; + $this->mockResult( false ); - $result = $instance->filter_apple_news_skip_push($skip); + $result = $instance->filter_apple_news_skip_push( $skip ); - $this->assertTrue($result); + $this->assertTrue( $result ); } - // Test that the filter_apple_news_skip_push method returns true when passed true on a VIP production environment. - public function testTrueFilterAppleNewsSkipPushVipProductionEnvironment() { + /** + * Test that the filter_apple_news_skip_push method returns true when passed true on a non-production environment. + * + * @return void + */ + public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = true; - define('VIP_GO_ENV', 'production'); + $skip = true; + $this->mockResult( false ); - $result = $instance->filter_apple_news_skip_push($skip); + $result = $instance->filter_apple_news_skip_push( $skip ); - $this->assertTrue($result); + $this->assertTrue( $result ); } - public function testFilterAppleNewsSkipPushOtherEnvironments() { - $instance = new Disable_Apple_News_No_Prod_Push(); - $skip = false; - - $result = $instance->filter_apple_news_skip_push($skip); - - $this->assertTrue($result); - } } From 94564019c268e4146d078d94d64f71c844549217 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 15 Jun 2023 14:08:30 -0500 Subject: [PATCH 04/21] fix tests and phpcs --- .../class-disable-apple-news-no-prod-push.php | 4 +- .../test-disable-apple-news-no-prod-push.php | 52 +++++++++++-------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index 72933354..57026f4e 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -17,7 +17,7 @@ /** * Disables Apple News Push on Non Production Environments. */ -final class Disable_Apple_News_No_Prod_Push implements Feature { +class Disable_Apple_News_No_Prod_Push implements Feature { /** * Boot the feature. */ @@ -54,7 +54,7 @@ public function filter_apple_news_skip_push( bool $skip ) { * * @return boolean */ - private function is_production_environment(): bool { + protected function is_production_environment(): bool { // If we are not on a production environment according to WP_ENV, return true. if ( defined( 'WP_ENV' ) && 'production' === WP_ENV ) { return true; diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index 57a11162..7a7d7963 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -15,35 +15,48 @@ use Alley\WP\Alleyvate\Feature; use Mantle\Testkit\Test_Case; +// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound + /** - * Test Disable_Apple_News_No_Prod_Push + * Test double for Disable_Apple_News_No_Prod_Push. Mocks production environment. */ -class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { +class TestDoubleProduction extends Disable_Apple_News_No_Prod_Push { /** - * Mocks the is_production_environment method. + * Override of the is_production_environment method to always return true. * - * @param bool $result The result to mock. - * @return void + * @return boolean */ - protected function mockResult( $result ) { - $mock = $this->getMockBuilder( 'Disable_Apple_News_No_Prod_Push' ) - ->setMethods( [ 'is_production_environment' ] ) - ->getMock(); - - $mock->expects( $this->once() ) - ->method( 'is_production_environment' ) - ->willReturn( $result ); + protected function is_production_environment(): bool { + return true; } +} +/** + * Test double for Disable_Apple_News_No_Prod_Push. Mocks non-production environment. + */ +class TestDoubleNonProduction extends Disable_Apple_News_No_Prod_Push { + /** + * Override of the is_production_environment method to always return false. + * + * @return boolean + */ + protected function is_production_environment(): bool { + return false; + } +} + +/** + * Test Disable_Apple_News_No_Prod_Push + */ +class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** * Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. * * @return void */ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { - $instance = new Disable_Apple_News_No_Prod_Push(); + $instance = new TestDoubleProduction(); $skip = false; - $this->mockResult( true ); $result = $instance->filter_apple_news_skip_push( $skip ); @@ -56,9 +69,8 @@ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { * @return void */ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { - $instance = new Disable_Apple_News_No_Prod_Push(); + $instance = new TestDoubleProduction(); $skip = true; - $this->mockResult( true ); $result = $instance->filter_apple_news_skip_push( $skip ); @@ -71,9 +83,8 @@ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { * @return void */ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { - $instance = new Disable_Apple_News_No_Prod_Push(); + $instance = new TestDoubleNonProduction(); $skip = false; - $this->mockResult( false ); $result = $instance->filter_apple_news_skip_push( $skip ); @@ -86,9 +97,8 @@ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { * @return void */ public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { - $instance = new Disable_Apple_News_No_Prod_Push(); + $instance = new TestDoubleNonProduction(); $skip = true; - $this->mockResult( false ); $result = $instance->filter_apple_news_skip_push( $skip ); From 68de34f2c57c658e125c4dba365a435b47049a09 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:47:32 -0500 Subject: [PATCH 05/21] fixing tests --- .../class-disable-apple-news-no-prod-push.php | 10 ++- .../test-disable-apple-news-no-prod-push.php | 79 ++++++++++--------- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index 57026f4e..5f2d0d48 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -18,11 +18,19 @@ * Disables Apple News Push on Non Production Environments. */ class Disable_Apple_News_No_Prod_Push implements Feature { + /** + * Store if this is a production environment. + * + * @var [type] + */ + private $is_production; + /** * Boot the feature. */ public function boot(): void { add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); + $this->is_production = $this->is_production_environment(); } /** @@ -32,7 +40,7 @@ public function boot(): void { */ public function filter_apple_news_skip_push( bool $skip ) { // If we are on a production environment, don't modify the value. - if ( $this->is_production_environment() ) { + if ( $this->is_production ) { return $skip; } // All other cases, return true - but allow it to be filtered. diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index 7a7d7963..089311d3 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -15,92 +15,93 @@ use Alley\WP\Alleyvate\Feature; use Mantle\Testkit\Test_Case; -// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound - /** - * Test double for Disable_Apple_News_No_Prod_Push. Mocks production environment. + * Test Disable_Apple_News_No_Prod_Push */ -class TestDoubleProduction extends Disable_Apple_News_No_Prod_Push { +class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** - * Override of the is_production_environment method to always return true. + * Production instance of Disable_Apple_News_No_Prod_Push * - * @return boolean + * @var Disable_Apple_News_No_Prod_Push */ - protected function is_production_environment(): bool { - return true; - } -} + protected $production_instance; + + /** + * Non-production_ instance of Disable_Apple_News_No_Prod_Push + * + * @var Disable_Apple_News_No_Prod_Push + */ + protected $non_production_instance; -/** - * Test double for Disable_Apple_News_No_Prod_Push. Mocks non-production environment. - */ -class TestDoubleNonProduction extends Disable_Apple_News_No_Prod_Push { /** - * Override of the is_production_environment method to always return false. + * Sets up our mocks. * - * @return boolean + * @return void */ - protected function is_production_environment(): bool { - return false; + protected function setUp(): void { + parent::setUp(); + + $reflection_class = new \ReflectionClass( 'Alley\WP\Alleyvate\Features\Disable_Apple_News_No_Prod_Push' ); + + $production_property = $reflection_class->getProperty( 'is_production' ); + $production_property->setAccessible( true ); + $this->production_instance = new Disable_Apple_News_No_Prod_Push(); + $production_property->setValue( $this->production_instance, true ); + + $non_production_property = $reflection_class->getProperty( 'is_production' ); + $non_production_property->setAccessible( true ); + $this->non_production_instance = new Disable_Apple_News_No_Prod_Push(); + $non_production_property->setValue( $this->non_production_instance, false ); } -} -/** - * Test Disable_Apple_News_No_Prod_Push - */ -class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** - * Test that the filter_apple_news_skip_push method returns false when passed false on a production environment. + * Test that the filter_apple_news_skip_push method returns false when passed false on a production_ environment. * * @return void */ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { - $instance = new TestDoubleProduction(); - $skip = false; + $skip = false; - $result = $instance->filter_apple_news_skip_push( $skip ); + $result = $this->production_instance->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. + * Test that the filter_apple_news_skip_push method returns true when passed true on a production_ environment. * * @return void */ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { - $instance = new TestDoubleProduction(); - $skip = true; + $skip = true; - $result = $instance->filter_apple_news_skip_push( $skip ); + $result = $this->production_instance->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. + * Test that the filter_apple_news_skip_push method returns true when passed false on a non-production_ environment. * * @return void */ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { - $instance = new TestDoubleNonProduction(); - $skip = false; + $skip = false; - $result = $instance->filter_apple_news_skip_push( $skip ); + $result = $this->non_production_instance->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. + * Test that the filter_apple_news_skip_push method returns true when passed true on a non-production_ environment. * * @return void */ public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { - $instance = new TestDoubleNonProduction(); - $skip = true; + $skip = true; - $result = $instance->filter_apple_news_skip_push( $skip ); + $result = $this->non_production_instance->filter_apple_news_skip_push( $skip ); $this->assertTrue( $result ); } From 4fb7691450040cecd5ba721c800dd6182fcacf0c Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:50:50 -0500 Subject: [PATCH 06/21] add comment --- .../features/class-disable-apple-news-no-prod-push.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index 5f2d0d48..41e3f0b0 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -60,6 +60,9 @@ public function filter_apple_news_skip_push( bool $skip ) { /** * Detect if we are on a production environment. * + * Note: I thought about using https://developer.wordpress.org/reference/functions/wp_get_environment_type/ + * but it defaults to 'production' if no value is set, which did not seem ideal for this use case. + * * @return boolean */ protected function is_production_environment(): bool { From 55c90aacd162e5134026aa7c04bdcbdec438b94d Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:53:12 -0500 Subject: [PATCH 07/21] add back final --- .../features/class-disable-apple-news-no-prod-push.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index 41e3f0b0..dcf39700 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -17,7 +17,7 @@ /** * Disables Apple News Push on Non Production Environments. */ -class Disable_Apple_News_No_Prod_Push implements Feature { +final class Disable_Apple_News_No_Prod_Push implements Feature { /** * Store if this is a production environment. * From 3c9dea8f6d22f2eeff9cb998a7924f781aaf6369 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:55:58 -0500 Subject: [PATCH 08/21] trying to fix native_function_invocation --- .../features/class-disable-apple-news-no-prod-push.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index dcf39700..d1614f74 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -29,7 +29,7 @@ final class Disable_Apple_News_No_Prod_Push implements Feature { * Boot the feature. */ public function boot(): void { - add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); + \add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); $this->is_production = $this->is_production_environment(); } @@ -54,7 +54,7 @@ public function filter_apple_news_skip_push( bool $skip ) { * * @param bool $skip Should we skip the Apple News push? */ - return apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); + return \apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); } /** From 01b6901a84f05047723b48751df8c7b91b5e4233 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:57:16 -0500 Subject: [PATCH 09/21] trying to fix native_function_invocation --- .../features/class-disable-apple-news-no-prod-push.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index d1614f74..d4564891 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -67,15 +67,15 @@ public function filter_apple_news_skip_push( bool $skip ) { */ protected function is_production_environment(): bool { // If we are not on a production environment according to WP_ENV, return true. - if ( defined( 'WP_ENV' ) && 'production' === WP_ENV ) { + if ( \defined( 'WP_ENV' ) && 'production' === WP_ENV ) { return true; } // If we are on Pantheon LIVE, return true. - if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { + if ( \isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { return true; } // If we are on VIP Production, don't modify the value. - if ( defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { + if ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { return true; } return false; From 94e3686de702ad43b932108de7a78e237bf41125 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 16 Jun 2023 17:59:50 -0500 Subject: [PATCH 10/21] maybe fixed now --- .../features/class-disable-apple-news-no-prod-push.php | 6 +++--- .../features/test-disable-apple-news-no-prod-push.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index d4564891..d8359f19 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -29,7 +29,7 @@ final class Disable_Apple_News_No_Prod_Push implements Feature { * Boot the feature. */ public function boot(): void { - \add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); + add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); $this->is_production = $this->is_production_environment(); } @@ -54,7 +54,7 @@ public function filter_apple_news_skip_push( bool $skip ) { * * @param bool $skip Should we skip the Apple News push? */ - return \apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); + return apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); } /** @@ -71,7 +71,7 @@ protected function is_production_environment(): bool { return true; } // If we are on Pantheon LIVE, return true. - if ( \isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { + if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { return true; } // If we are on VIP Production, don't modify the value. diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index 089311d3..4eceaec5 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -18,7 +18,7 @@ /** * Test Disable_Apple_News_No_Prod_Push */ -class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { +final class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** * Production instance of Disable_Apple_News_No_Prod_Push * From d1256bf658255d83255cd347debbc1da18ba0e18 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Mon, 7 Aug 2023 10:19:52 -0500 Subject: [PATCH 11/21] Apply suggestions from code review Co-authored-by: Renato Alves <19148962+renatonascalves@users.noreply.github.com> --- .../features/class-disable-apple-news-no-prod-push.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index d8359f19..fd7f139a 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -21,7 +21,7 @@ final class Disable_Apple_News_No_Prod_Push implements Feature { /** * Store if this is a production environment. * - * @var [type] + * @var bool */ private $is_production; @@ -38,7 +38,7 @@ public function boot(): void { * * @param bool $skip Should we skip the Apple News push. */ - public function filter_apple_news_skip_push( bool $skip ) { + public function filter_apple_news_skip_push( bool $skip ): bool { // If we are on a production environment, don't modify the value. if ( $this->is_production ) { return $skip; @@ -75,9 +75,6 @@ protected function is_production_environment(): bool { return true; } // If we are on VIP Production, don't modify the value. - if ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { - return true; - } - return false; + return ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ); } } From df32183b9b557ec267c40bd8c02fc69d0a736867 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Mon, 7 Aug 2023 12:08:44 -0500 Subject: [PATCH 12/21] codereview feedback --- .../class-disable-apple-news-no-prod-push.php | 13 ++----------- src/alley/wp/alleyvate/load.php | 14 +++++++------- .../test-disable-apple-news-no-prod-push.php | 10 ---------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php index d8359f19..da98c3f4 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php @@ -43,18 +43,9 @@ public function filter_apple_news_skip_push( bool $skip ) { if ( $this->is_production ) { return $skip; } - // All other cases, return true - but allow it to be filtered. + // All other cases, return true. - /** - * Filters the final value in filter_apple_news_skip_push. - * - * Allow developers to disable this feature. - * - * @since 2.0.0 - * - * @param bool $skip Should we skip the Apple News push? - */ - return apply_filters( 'alleyvate_disable_apple_news_no_prod_push', true ); + return true; } /** diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index d3310c40..83d5e8f4 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -27,13 +27,13 @@ function load(): void { * @var Feature[] $features */ $features = [ - 'disable_apple_new_push' => new Features\Disable_Apple_News_No_Prod_Push(), - 'disable_comments' => new Features\Disable_Comments(), - 'disable_sticky_posts' => new Features\Disable_Sticky_Posts(), - 'disable_trackbacks' => new Features\Disable_Trackbacks(), - 'disallow_file_edit' => new Features\Disallow_File_Edit(), - 'redirect_guess_shortcircuit' => new Features\Redirect_Guess_Shortcircuit(), - 'user_enumeration_restrictions' => new Features\User_Enumeration_Restrictions(), + 'disable_apple_news_no_prod_push' => new Features\Disable_Apple_News_No_Prod_Push(), + 'disable_comments' => new Features\Disable_Comments(), + 'disable_sticky_posts' => new Features\Disable_Sticky_Posts(), + 'disable_trackbacks' => new Features\Disable_Trackbacks(), + 'disallow_file_edit' => new Features\Disallow_File_Edit(), + 'redirect_guess_shortcircuit' => new Features\Redirect_Guess_Shortcircuit(), + 'user_enumeration_restrictions' => new Features\User_Enumeration_Restrictions(), ]; foreach ( $features as $handle => $feature ) { diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index 4eceaec5..ecdb118a 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -35,8 +35,6 @@ final class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { /** * Sets up our mocks. - * - * @return void */ protected function setUp(): void { parent::setUp(); @@ -56,8 +54,6 @@ protected function setUp(): void { /** * Test that the filter_apple_news_skip_push method returns false when passed false on a production_ environment. - * - * @return void */ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { $skip = false; @@ -69,8 +65,6 @@ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { /** * Test that the filter_apple_news_skip_push method returns true when passed true on a production_ environment. - * - * @return void */ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { $skip = true; @@ -82,8 +76,6 @@ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { /** * Test that the filter_apple_news_skip_push method returns true when passed false on a non-production_ environment. - * - * @return void */ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { $skip = false; @@ -95,8 +87,6 @@ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { /** * Test that the filter_apple_news_skip_push method returns true when passed true on a non-production_ environment. - * - * @return void */ public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { $skip = true; From cb189d15fd090c1242b1edc3a4bbda3a056e893b Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Tue, 8 Aug 2023 12:29:06 -0500 Subject: [PATCH 13/21] making a few things more readable --- ...lass-disable-apple-news-non-prod-push.php} | 9 ++++++--- src/alley/wp/alleyvate/load.php | 14 ++++++------- .../test-disable-apple-news-no-prod-push.php | 20 +++++++++---------- 3 files changed, 23 insertions(+), 20 deletions(-) rename src/alley/wp/alleyvate/features/{class-disable-apple-news-no-prod-push.php => class-disable-apple-news-non-prod-push.php} (89%) diff --git a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php b/src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php similarity index 89% rename from src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php rename to src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php index ec6ac3d6..c0bc901f 100644 --- a/src/alley/wp/alleyvate/features/class-disable-apple-news-no-prod-push.php +++ b/src/alley/wp/alleyvate/features/class-disable-apple-news-non-prod-push.php @@ -1,6 +1,6 @@ * @@ -17,7 +17,7 @@ /** * Disables Apple News Push on Non Production Environments. */ -final class Disable_Apple_News_No_Prod_Push implements Feature { +final class Disable_Apple_News_Non_Prod_Push implements Feature { /** * Store if this is a production environment. * @@ -66,6 +66,9 @@ protected function is_production_environment(): bool { return true; } // If we are on VIP Production, don't modify the value. - return ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ); + if ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { + return true; + } + return false; } } diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index 83d5e8f4..8fe5efc0 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -27,13 +27,13 @@ function load(): void { * @var Feature[] $features */ $features = [ - 'disable_apple_news_no_prod_push' => new Features\Disable_Apple_News_No_Prod_Push(), - 'disable_comments' => new Features\Disable_Comments(), - 'disable_sticky_posts' => new Features\Disable_Sticky_Posts(), - 'disable_trackbacks' => new Features\Disable_Trackbacks(), - 'disallow_file_edit' => new Features\Disallow_File_Edit(), - 'redirect_guess_shortcircuit' => new Features\Redirect_Guess_Shortcircuit(), - 'user_enumeration_restrictions' => new Features\User_Enumeration_Restrictions(), + 'disable_apple_news_non_prod_push' => new Features\Disable_Apple_News_Non_Prod_Push(), + 'disable_comments' => new Features\Disable_Comments(), + 'disable_sticky_posts' => new Features\Disable_Sticky_Posts(), + 'disable_trackbacks' => new Features\Disable_Trackbacks(), + 'disallow_file_edit' => new Features\Disallow_File_Edit(), + 'redirect_guess_shortcircuit' => new Features\Redirect_Guess_Shortcircuit(), + 'user_enumeration_restrictions' => new Features\User_Enumeration_Restrictions(), ]; foreach ( $features as $handle => $feature ) { diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php index ecdb118a..8f06754f 100644 --- a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php +++ b/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php @@ -1,6 +1,6 @@ * @@ -16,20 +16,20 @@ use Mantle\Testkit\Test_Case; /** - * Test Disable_Apple_News_No_Prod_Push + * Test Disable_Apple_News_Non_Prod_Push */ -final class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { +final class Disable_Apple_News_Non_Prod_Push_Test extends Test_Case { /** - * Production instance of Disable_Apple_News_No_Prod_Push + * Production instance of Disable_Apple_News_Non_Prod_Push * - * @var Disable_Apple_News_No_Prod_Push + * @var Disable_Apple_News_Non_Prod_Push */ protected $production_instance; /** - * Non-production_ instance of Disable_Apple_News_No_Prod_Push + * Non-production_ instance of Disable_Apple_News_Non_Prod_Push * - * @var Disable_Apple_News_No_Prod_Push + * @var Disable_Apple_News_Non_Prod_Push */ protected $non_production_instance; @@ -39,16 +39,16 @@ final class Disable_Apple_News_No_Prod_Push_Test extends Test_Case { protected function setUp(): void { parent::setUp(); - $reflection_class = new \ReflectionClass( 'Alley\WP\Alleyvate\Features\Disable_Apple_News_No_Prod_Push' ); + $reflection_class = new \ReflectionClass( 'Alley\WP\Alleyvate\Features\Disable_Apple_News_Non_Prod_Push' ); $production_property = $reflection_class->getProperty( 'is_production' ); $production_property->setAccessible( true ); - $this->production_instance = new Disable_Apple_News_No_Prod_Push(); + $this->production_instance = new Disable_Apple_News_Non_Prod_Push(); $production_property->setValue( $this->production_instance, true ); $non_production_property = $reflection_class->getProperty( 'is_production' ); $non_production_property->setAccessible( true ); - $this->non_production_instance = new Disable_Apple_News_No_Prod_Push(); + $this->non_production_instance = new Disable_Apple_News_Non_Prod_Push(); $non_production_property->setValue( $this->non_production_instance, false ); } From 47627af6d31a23970ba87f2d4a356483602a02f4 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Tue, 8 Aug 2023 12:30:27 -0500 Subject: [PATCH 14/21] rename test file --- ...no-prod-push.php => test-disable-apple-news-non-prod-push.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/alley/wp/alleyvate/features/{test-disable-apple-news-no-prod-push.php => test-disable-apple-news-non-prod-push.php} (100%) diff --git a/tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php b/tests/alley/wp/alleyvate/features/test-disable-apple-news-non-prod-push.php similarity index 100% rename from tests/alley/wp/alleyvate/features/test-disable-apple-news-no-prod-push.php rename to tests/alley/wp/alleyvate/features/test-disable-apple-news-non-prod-push.php From 821bc0a3eed0b9f7a091a8110edaa61a718836e8 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 11:14:42 -0400 Subject: [PATCH 15/21] Fix Feature type --- .../features/class-disable-apple-news-non-prod-push.php | 2 +- src/alley/wp/alleyvate/load.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index c0bc901f..8cf535a6 100644 --- 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 @@ -12,7 +12,7 @@ namespace Alley\WP\Alleyvate\Features; -use Alley\WP\Alleyvate\Feature; +use Alley\WP\Types\Feature; /** * Disables Apple News Push on Non Production Environments. diff --git a/src/alley/wp/alleyvate/load.php b/src/alley/wp/alleyvate/load.php index c9d85676..ecaa7832 100644 --- a/src/alley/wp/alleyvate/load.php +++ b/src/alley/wp/alleyvate/load.php @@ -35,7 +35,7 @@ function load(): void { ), new Feature( 'disable_apple_news_non_prod_push', - new Features\Disable_Apple_News_Non_Prod_push(), + new Features\Disable_Apple_News_Non_Prod_Push(), ), new Feature( 'disable_attachment_routing', From dc01a53e2008c744ec56821fe0b6623692e9b6c6 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 11:16:00 -0400 Subject: [PATCH 16/21] Swap to use wp_get_environment_type --- ...class-disable-apple-news-non-prod-push.php | 36 ++----------------- 1 file changed, 2 insertions(+), 34 deletions(-) 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 index 8cf535a6..a19d3b25 100644 --- 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 @@ -18,19 +18,11 @@ * Disables Apple News Push on Non Production Environments. */ final class Disable_Apple_News_Non_Prod_Push implements Feature { - /** - * Store if this is a production environment. - * - * @var bool - */ - private $is_production; - /** * Boot the feature. */ public function boot(): void { add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 1, 100 ); - $this->is_production = $this->is_production_environment(); } /** @@ -40,35 +32,11 @@ public function boot(): void { */ public function filter_apple_news_skip_push( bool $skip ): bool { // If we are on a production environment, don't modify the value. - if ( $this->is_production ) { + if ( 'production' === wp_get_environment_type() ) { return $skip; } - // All other cases, return true. + // All other cases, return true. return true; } - - /** - * Detect if we are on a production environment. - * - * Note: I thought about using https://developer.wordpress.org/reference/functions/wp_get_environment_type/ - * but it defaults to 'production' if no value is set, which did not seem ideal for this use case. - * - * @return boolean - */ - protected function is_production_environment(): bool { - // If we are not on a production environment according to WP_ENV, return true. - if ( \defined( 'WP_ENV' ) && 'production' === WP_ENV ) { - return true; - } - // If we are on Pantheon LIVE, return true. - if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) { - return true; - } - // If we are on VIP Production, don't modify the value. - if ( \defined( 'VIP_GO_ENV' ) && VIP_GO_ENV === 'production' ) { - return true; - } - return false; - } } From 47ab52b606c687edc9c5c41d847cabc22065c937 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 11:37:57 -0400 Subject: [PATCH 17/21] Convert to wp_get_environment_type --- ...class-disable-apple-news-non-prod-push.php | 2 +- .../test-disable-apple-news-non-prod-push.php | 47 ++++++++----------- tests/bootstrap.php | 7 +++ 3 files changed, 28 insertions(+), 28 deletions(-) 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 index a19d3b25..00269f51 100644 --- 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 @@ -22,7 +22,7 @@ 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' ], 1, 100 ); + add_filter( 'apple_news_skip_push', [ $this, 'filter_apple_news_skip_push' ], 9999 ); } /** 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 index 8f06754f..36ee13ce 100644 --- 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 @@ -20,36 +20,17 @@ */ final class Disable_Apple_News_Non_Prod_Push_Test extends Test_Case { /** - * Production instance of Disable_Apple_News_Non_Prod_Push + * The Feature class. * * @var Disable_Apple_News_Non_Prod_Push */ - protected $production_instance; + protected $feature; /** - * Non-production_ instance of Disable_Apple_News_Non_Prod_Push - * - * @var Disable_Apple_News_Non_Prod_Push - */ - protected $non_production_instance; - - /** - * Sets up our mocks. + * Setup before test. */ protected function setUp(): void { - parent::setUp(); - - $reflection_class = new \ReflectionClass( 'Alley\WP\Alleyvate\Features\Disable_Apple_News_Non_Prod_Push' ); - - $production_property = $reflection_class->getProperty( 'is_production' ); - $production_property->setAccessible( true ); - $this->production_instance = new Disable_Apple_News_Non_Prod_Push(); - $production_property->setValue( $this->production_instance, true ); - - $non_production_property = $reflection_class->getProperty( 'is_production' ); - $non_production_property->setAccessible( true ); - $this->non_production_instance = new Disable_Apple_News_Non_Prod_Push(); - $non_production_property->setValue( $this->non_production_instance, false ); + $this->feature = new Disable_Apple_News_Non_Prod_Push(); } /** @@ -58,7 +39,10 @@ protected function setUp(): void { public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { $skip = false; - $result = $this->production_instance->filter_apple_news_skip_push( $skip ); + putenv( 'WP_ENVIRONMENT_TYPE=production' ); + $_ENV['WP_ENVIRONMENT_TYPE'] = 'production'; + + $result = $this->feature->filter_apple_news_skip_push( $skip ); $this->assertFalse( $result ); } @@ -69,7 +53,10 @@ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { $skip = true; - $result = $this->production_instance->filter_apple_news_skip_push( $skip ); + putenv( 'WP_ENVIRONMENT_TYPE=production' ); + $_ENV['WP_ENVIRONMENT_TYPE'] = 'production'; + + $result = $this->feature->filter_apple_news_skip_push( $skip ); $this->assertTrue( $result ); } @@ -80,7 +67,10 @@ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { $skip = false; - $result = $this->non_production_instance->filter_apple_news_skip_push( $skip ); + putenv( 'WP_ENVIRONMENT_TYPE=local' ); + $_ENV['WP_ENVIRONMENT_TYPE'] = 'local'; + + $result = $this->feature->filter_apple_news_skip_push( $skip ); $this->assertTrue( $result ); } @@ -91,7 +81,10 @@ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { $skip = true; - $result = $this->non_production_instance->filter_apple_news_skip_push( $skip ); + putenv( 'WP_ENVIRONMENT_TYPE=local' ); + $_ENV['WP_ENVIRONMENT_TYPE'] = '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..3628dd4e 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 ); + \Mantle\Testing\manager() // Fires on 'muplugins_loaded'. ->loaded( From b82781bca4a5e73d875b64a3ef567d2b0735521a Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 11:41:06 -0400 Subject: [PATCH 18/21] PHPCS fixes --- .../test-disable-apple-news-non-prod-push.php | 24 ++++++++++++------- tests/bootstrap.php | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) 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 index 36ee13ce..8e2501f2 100644 --- 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 @@ -33,14 +33,24 @@ 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; - putenv( 'WP_ENVIRONMENT_TYPE=production' ); - $_ENV['WP_ENVIRONMENT_TYPE'] = 'production'; + $this->setEnvironment( 'production' ); $result = $this->feature->filter_apple_news_skip_push( $skip ); @@ -53,8 +63,7 @@ public function testFalseFilterAppleNewsSkipPushProductionEnvironment() { public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { $skip = true; - putenv( 'WP_ENVIRONMENT_TYPE=production' ); - $_ENV['WP_ENVIRONMENT_TYPE'] = 'production'; + $this->setEnvironment( 'production' ); $result = $this->feature->filter_apple_news_skip_push( $skip ); @@ -67,8 +76,7 @@ public function testTrueFilterAppleNewsSkipPushProductionEnvironment() { public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { $skip = false; - putenv( 'WP_ENVIRONMENT_TYPE=local' ); - $_ENV['WP_ENVIRONMENT_TYPE'] = 'local'; + $this->setEnvironment( 'local' ); $result = $this->feature->filter_apple_news_skip_push( $skip ); @@ -81,12 +89,10 @@ public function testFalseFilterAppleNewsSkipPushOtherEnvironments() { public function testTrueFilterAppleNewsSkipPushOtherEnvironments() { $skip = true; - putenv( 'WP_ENVIRONMENT_TYPE=local' ); - $_ENV['WP_ENVIRONMENT_TYPE'] = 'local'; + $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 3628dd4e..8df3877d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -15,7 +15,7 @@ * the method caches the initial result and always returns it, even if * we modify the environment variable. */ -define( 'WP_RUN_CORE_TESTS', true ); +define( 'WP_RUN_CORE_TESTS', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound \Mantle\Testing\manager() // Fires on 'muplugins_loaded'. From d625a41366410d87fc54c5e6416f0aaca1d11abb Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 11:46:23 -0400 Subject: [PATCH 19/21] Fix PHP CS Fixer --- tests/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8df3877d..c93ade29 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -15,7 +15,7 @@ * 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 +\define( 'WP_RUN_CORE_TESTS', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound \Mantle\Testing\manager() // Fires on 'muplugins_loaded'. From dd9c825501fd50842345413059227d286efed90c Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 12:43:09 -0400 Subject: [PATCH 20/21] Update README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) 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. From 2838d5907b4cc57eba1e652b8a66754f380d7fa9 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Tue, 16 Jul 2024 12:45:24 -0400 Subject: [PATCH 21/21] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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