-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Pings/Trackbacks: Disable pings in non-production environments #11476
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
Open
arcangelini
wants to merge
6
commits into
WordPress:trunk
Choose a base branch
from
arcangelini:disable-pings-non-production
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+338
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a7bcdf
Disable pings/trackbacks in non-production environments
arcangelini 44bf1dd
Fix sendHeaders test for environment-based pings filter
arcangelini d048fba
Replace pings_open filter with pre_trackback_post hook
arcangelini beec383
Improve docblocks for ping environment functions
arcangelini f5961fc
Address PR review feedback
arcangelini 3ba7f52
Address second round of PR review feedback
arcangelini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
tests/phpunit/tests/comment/disablePingsForEnvironment.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for disabling pings in non-production environments. | ||
| * | ||
| * @group comment | ||
| * @covers ::wp_should_disable_pings_for_environment | ||
| * @covers ::wp_maybe_disable_outgoing_pings_for_environment | ||
| * @covers ::wp_maybe_disable_trackback_for_environment | ||
| * @covers ::wp_maybe_disable_xmlrpc_pingback_for_environment | ||
| * | ||
| * @ticket 64837 | ||
| */ | ||
| class Tests_Comment_DisablePingsForEnvironment extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Stores the original WP_ENVIRONMENT_TYPE env value for cleanup. | ||
| * | ||
| * @var string|false | ||
| */ | ||
| private $original_env; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->original_env = getenv( 'WP_ENVIRONMENT_TYPE' ); | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| if ( false === $this->original_env ) { | ||
| putenv( 'WP_ENVIRONMENT_TYPE' ); | ||
| } else { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=' . $this->original_env ); | ||
| } | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_should_disable_returns_true_for_local() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=local' ); | ||
| $this->assertTrue( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_should_disable_returns_true_for_development() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
| $this->assertTrue( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_should_disable_returns_true_for_staging() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=staging' ); | ||
| $this->assertTrue( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_should_disable_returns_false_for_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=production' ); | ||
| $this->assertFalse( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_filter_can_enable_pings_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=local' ); | ||
| add_filter( 'wp_should_disable_pings_for_environment', '__return_false' ); | ||
|
|
||
| $this->assertFalse( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_filter_can_disable_pings_in_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=production' ); | ||
| add_filter( 'wp_should_disable_pings_for_environment', '__return_true' ); | ||
|
|
||
| $this->assertTrue( wp_should_disable_pings_for_environment() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_filter_receives_environment_type() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=staging' ); | ||
|
|
||
| $received_type = null; | ||
| add_filter( | ||
| 'wp_should_disable_pings_for_environment', | ||
| function ( $should_disable, $environment_type ) use ( &$received_type ) { | ||
| $received_type = $environment_type; | ||
| return $should_disable; | ||
| }, | ||
| 10, | ||
| 2 | ||
| ); | ||
|
|
||
| wp_should_disable_pings_for_environment(); | ||
|
|
||
| $this->assertSame( 'staging', $received_type ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_outgoing_pingbacks_removed_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| // Re-register the defaults to ensure a clean state. | ||
| add_action( 'do_all_pings', 'do_all_pingbacks', 10, 0 ); | ||
|
|
||
| // Fire the priority-1 callback. | ||
| wp_maybe_disable_outgoing_pings_for_environment(); | ||
|
|
||
| $this->assertFalse( has_action( 'do_all_pings', 'do_all_pingbacks' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_outgoing_trackbacks_removed_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| add_action( 'do_all_pings', 'do_all_trackbacks', 10, 0 ); | ||
|
|
||
| wp_maybe_disable_outgoing_pings_for_environment(); | ||
|
|
||
| $this->assertFalse( has_action( 'do_all_pings', 'do_all_trackbacks' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_outgoing_generic_ping_removed_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| add_action( 'do_all_pings', 'generic_ping', 10, 0 ); | ||
|
|
||
| wp_maybe_disable_outgoing_pings_for_environment(); | ||
|
|
||
| $this->assertFalse( has_action( 'do_all_pings', 'generic_ping' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_enclosures_not_removed_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| add_action( 'do_all_pings', 'do_all_enclosures', 10, 0 ); | ||
|
|
||
| wp_maybe_disable_outgoing_pings_for_environment(); | ||
|
|
||
| $this->assertTrue( has_action( 'do_all_pings', 'do_all_enclosures', 10 ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_outgoing_pings_preserved_in_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=production' ); | ||
|
|
||
| add_action( 'do_all_pings', 'do_all_pingbacks', 10, 0 ); | ||
| add_action( 'do_all_pings', 'do_all_trackbacks', 10, 0 ); | ||
| add_action( 'do_all_pings', 'generic_ping', 10, 0 ); | ||
|
|
||
| wp_maybe_disable_outgoing_pings_for_environment(); | ||
|
|
||
| $this->assertTrue( has_action( 'do_all_pings', 'do_all_pingbacks', 10 ), 'do_all_pingbacks should still be hooked at priority 10.' ); | ||
| $this->assertTrue( has_action( 'do_all_pings', 'do_all_trackbacks', 10 ), 'do_all_trackbacks should still be hooked at priority 10.' ); | ||
| $this->assertTrue( has_action( 'do_all_pings', 'generic_ping', 10 ), 'generic_ping should still be hooked at priority 10.' ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_trackback_hook_is_registered() { | ||
| $this->assertTrue( has_action( 'pre_trackback_post', 'wp_maybe_disable_trackback_for_environment', 10 ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_pings_open_unaffected_by_environment() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=local' ); | ||
|
|
||
| $post = self::factory()->post->create_and_get( | ||
| array( 'ping_status' => 'open' ) | ||
| ); | ||
|
|
||
| $this->assertTrue( pings_open( $post ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_xmlrpc_pingback_removed_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| $methods = array( | ||
| 'pingback.ping' => 'this:pingback_ping', | ||
| 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks', | ||
| 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', | ||
| ); | ||
|
|
||
| $filtered = wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ); | ||
|
|
||
| $this->assertArrayNotHasKey( 'pingback.ping', $filtered ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_xmlrpc_pingback_preserved_in_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=production' ); | ||
|
|
||
| $methods = array( | ||
| 'pingback.ping' => 'this:pingback_ping', | ||
| 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', | ||
| ); | ||
|
|
||
| $filtered = wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ); | ||
|
|
||
| $this->assertArrayHasKey( 'pingback.ping', $filtered ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64837 | ||
| */ | ||
| public function test_xmlrpc_other_methods_preserved_in_non_production() { | ||
| putenv( 'WP_ENVIRONMENT_TYPE=development' ); | ||
|
|
||
| $methods = array( | ||
| 'pingback.ping' => 'this:pingback_ping', | ||
| 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks', | ||
| 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', | ||
| 'wp.getPost' => 'this:wp_getPost', | ||
| ); | ||
|
|
||
| $filtered = wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ); | ||
|
|
||
| $this->assertArrayHasKey( 'pingback.extensions.getPingbacks', $filtered ); | ||
| $this->assertArrayHasKey( 'wp.getUsersBlogs', $filtered ); | ||
| $this->assertArrayHasKey( 'wp.getPost', $filtered ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we intentionally leaving the readonly
pingback.extensions.getPingbacks()intact for non-production environments?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly it was a bit of an oversight initially, but thinking about it more I'm leaning toward leaving it alone. It's a read-only SELECT against the comments table, so it feels like it sits outside what this PR is really targeting (staging pinging prod, or accepting incoming pings).
The other thing that stopped me was that existing pingbacks are still visible in the admin, REST API, get_comments(), etc. on non-prod. Stripping just this one read method while leaving all those alone felt inconsistent. I am open on this though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe we should leave it alone. Maybe the site owner disabled pingbacks after having them enabled for a while before.