Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 7 additions & 7 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* If the comment author was approved before, then the comment is automatically
* approved.
*
* Pingbacks originating from this site are automatically approved, as the link
* they report was created by someone who can already publish here.
* Pingbacks originating from the same site are automatically approved, as the
* link they report was created by someone who can already publish here.
*
* If all checks pass, the function will return true.
*
* @since 1.2.0
* @since 7.2.0 Pingbacks from this site are no longer held for moderation.
* @since 7.1.0 Pingbacks from the same site are no longer held for moderation.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
Expand Down Expand Up @@ -183,18 +183,18 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
/**
* Filters whether a pingback is approved without being held for moderation.
*
* Defaults to true for pingbacks originating from a published post on this
* Defaults to true for pingbacks originating from a published post on the same
* site, and false for every other pingback. Trackbacks are never considered,
* as they cannot be verified.
*
* @since 7.2.0
* @since 7.1.0
*
* @param bool $approve_pingback Whether to approve the pingback.
* @param bool $approve_pingback Whether to auto-approve the pingback.
* @param int $source_id ID of the post on this site the pingback
* originated from, or 0 if it came from elsewhere.
* @param string $url The URL the pingback was sent from.
*/
return (bool) apply_filters( 'auto_approve_pingback', $approve_pingback, $source_id, $url );
return (bool) apply_filters( 'wp_auto_approve_pingback', $approve_pingback, $source_id, $url );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about something more generic like wp_auto_approve_self_ping? (Or mention or comment or...)

I feel like we aren't far away from registered custom comment types and it would be nice if those (e.g. webmention) could also easily opt in to something like this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This filter right now allows filtering the condition for any ringback and not just self pingback, the default value is different between pingbacks though. So I think the current name is better personally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ahh, fair. That makes sense.

Still brainstorming, because I can see it being useful for something other than pingbacks one day: wp_auto_approve_ping or wp_auto_approve_comment_mention? But really, this is fine too. :)

} else {
return false;
}
Expand Down
57 changes: 54 additions & 3 deletions tests/phpunit/tests/comment/checkComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,54 @@ public function test_should_return_false_for_a_comment_whose_author_url_is_a_pos
}

/**
* Test auto approvals can be turned off via the `wp_auto_approve_pingback` filter.
*
* @ticket 65016
*/
public function test_auto_approve_pingback_should_be_able_to_hold_a_pingback_from_this_site() {
update_option( 'comment_previously_approved', '1' );

$source_url = get_permalink( self::factory()->post->create() );

add_filter( 'auto_approve_pingback', '__return_false' );
add_filter( 'wp_auto_approve_pingback', '__return_false' );

$this->assertFalse( check_comment( 'Site Title', '', $source_url, 'Excerpt.', '192.168.0.1', '', 'pingback' ) );
}

/**
* Test auto approvals can be turned on via the `wp_auto_approve_pingback` filter.
*
* @ticket 65016
*/
public function test_auto_approve_pingback_should_be_able_to_approve_a_pingback_from_another_site() {
update_option( 'comment_previously_approved', '1' );

add_filter( 'auto_approve_pingback', '__return_true' );
add_filter( 'wp_auto_approve_pingback', '__return_true' );

$this->assertTrue( check_comment( 'Site Title', '', 'http://example.com/a-post/', 'Excerpt.', '192.168.0.1', '', 'pingback' ) );
}

/**
* Ensure pingbacks from Multisite sub-sites are not auto approved.
*
* @ticket 65016
* @group ms-required
*/
public function test_auto_approve_pingback_should_not_approve_from_a_different_ms_site() {
update_option( 'comment_previously_approved', '1' );

$new_blog = self::factory()->blog->create();

switch_to_blog( $new_blog );
$source_url = get_permalink( self::factory()->post->create() );
restore_current_blog();

$this->assertFalse( check_comment( 'Site Title', '', $source_url, 'Excerpt.', '192.168.0.1', '', 'pingback' ) );
}

/**
* Ensure the `wp_auto_approve_pingback` filter receives the post ID for same site pings.
*
* @ticket 65016
*/
public function test_auto_approve_pingback_should_receive_the_source_post_id() {
Expand All @@ -340,7 +364,7 @@ public function test_auto_approve_pingback_should_receive_the_source_post_id() {

$observed = null;
add_filter(
'auto_approve_pingback',
'wp_auto_approve_pingback',
static function ( $approve, $source_id ) use ( &$observed ) {
$observed = $source_id;
return $approve;
Expand All @@ -354,6 +378,33 @@ static function ( $approve, $source_id ) use ( &$observed ) {
$this->assertSame( $post_id, $observed );
}

/**
* Ensure the `wp_auto_approve_pingback` filter does not receive a post ID for off-site pings.
*
* @ticket 65016
*/
public function test_auto_approve_pingback_should_receive_the_post_id_zero_for_off_site_pings() {
update_option( 'comment_previously_approved', '1' );

$post_permalink = get_permalink( self::factory()->post->create() );
$source_url = str_replace( home_url( '/' ), 'http://wordpress.org/', $post_permalink );

$observed = null;
add_filter(
'wp_auto_approve_pingback',
static function ( $approve, $source_id ) use ( &$observed ) {
$observed = $source_id;
return $approve;
},
10,
2
);

check_comment( 'Site Title', '', $source_url, 'Excerpt.', '192.168.0.1', '', 'pingback' );

$this->assertSame( 0, $observed );
}

/**
* Data provider.
*
Expand Down
Loading