diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 4ad7d5bbcbcee..73289f7959fd7 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -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. * @@ -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 ); } else { return false; } diff --git a/tests/phpunit/tests/comment/checkComment.php b/tests/phpunit/tests/comment/checkComment.php index f67efa739f6c1..05a6242facf62 100644 --- a/tests/phpunit/tests/comment/checkComment.php +++ b/tests/phpunit/tests/comment/checkComment.php @@ -306,6 +306,8 @@ 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() { @@ -313,23 +315,45 @@ public function test_auto_approve_pingback_should_be_able_to_hold_a_pingback_fro $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() { @@ -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; @@ -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. *