Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable Apple News pushes on non-production environments #44

Merged
merged 25 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
aa79cd2
setting up Apple News push feature
mogmarsh Jun 15, 2023
b06aaee
fixing some tests
mogmarsh Jun 15, 2023
bcde6b5
trying to mock is_production_environment
mogmarsh Jun 15, 2023
9456401
fix tests and phpcs
mogmarsh Jun 15, 2023
68de34f
fixing tests
mogmarsh Jun 16, 2023
4fb7691
add comment
mogmarsh Jun 16, 2023
c6f5a90
Merge remote-tracking branch 'origin/main' into feature/disable-apple…
mogmarsh Jun 16, 2023
55c90aa
add back final
mogmarsh Jun 16, 2023
3c9dea8
trying to fix native_function_invocation
mogmarsh Jun 16, 2023
01b6901
trying to fix native_function_invocation
mogmarsh Jun 16, 2023
94e3686
maybe fixed now
mogmarsh Jun 16, 2023
d1256bf
Apply suggestions from code review
mogmarsh Aug 7, 2023
0257920
Merge remote-tracking branch 'origin/main' into feature/disable-apple…
mogmarsh Aug 7, 2023
df32183
codereview feedback
mogmarsh Aug 7, 2023
cf45ec2
Merge remote-tracking branch 'origin/feature/disable-apple-news-pushe…
mogmarsh Aug 7, 2023
cb189d1
making a few things more readable
mogmarsh Aug 8, 2023
47627af
rename test file
mogmarsh Aug 8, 2023
d382baa
Merge conflicts
anubisthejackle Jul 16, 2024
821bc0a
Fix Feature type
anubisthejackle Jul 16, 2024
dc01a53
Swap to use wp_get_environment_type
anubisthejackle Jul 16, 2024
47ab52b
Convert to wp_get_environment_type
anubisthejackle Jul 16, 2024
b82781b
PHPCS fixes
anubisthejackle Jul 16, 2024
d625a41
Fix PHP CS Fixer
anubisthejackle Jul 16, 2024
dd9c825
Update README
anubisthejackle Jul 16, 2024
2838d59
Update changelog
anubisthejackle Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
renatonascalves marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
Copy link
Member

@srtfisher srtfisher Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. It wasn't, which is why I had to add it here. Maybe worth a look?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh I missed the asof line. That's probably why.


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