From 626d9af280566d008ccf501952f119fb0ce61662 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 12 Feb 2025 23:44:00 +0000 Subject: [PATCH] Pings/Trackbacks: Add return value to `pingback()`. This facilitates debugging and better response / error handling, among other things. Props audrasjb, coquardcyr, dshanske, ironprogrammer, NathanAtmoz, pbearne, shulard, soulseekah. Fixes #38197. git-svn-id: https://develop.svn.wordpress.org/trunk@59818 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 15 +++- tests/phpunit/tests/comment/pingback.php | 91 ++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/comment/pingback.php diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 00bda2615d..e9442b90a9 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2890,6 +2890,7 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) { $pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote ); $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote ); + if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { $quote = ( $pingback_link_offset_dquote ) ? '"' : '\''; $pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; @@ -3079,9 +3080,11 @@ function generic_ping( $post_id = 0 ) { * * @since 0.71 * @since 4.7.0 `$post` can be a WP_Post object. + * @since 6.8.0 Returns an array of pingback statuses indexed by link. * * @param string $content Post content to check for links. If empty will retrieve from post. * @param int|WP_Post $post Post ID or object. + * @return array An array of pingback statuses indexed by link. */ function pingback( $content, $post ) { require_once ABSPATH . WPINC . '/class-IXR.php'; @@ -3093,7 +3096,7 @@ function pingback( $content, $post ) { $post = get_post( $post ); if ( ! $post ) { - return; + return array(); } $pung = get_pung( $post ); @@ -3108,6 +3111,7 @@ function pingback( $content, $post ) { */ $post_links_temp = wp_extract_urls( $content ); + $ping_status = array(); /* * Step 2. * Walking through the links array. @@ -3179,11 +3183,18 @@ function pingback( $content, $post ) { // When set to true, this outputs debug messages by itself. $client->debug = false; - if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered. + $status = $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ); + + if ( $status // Ping registered. + || ( isset( $client->error->code ) && 48 === $client->error->code ) // Already registered. + ) { add_ping( $post, $pagelinkedto ); } + $ping_status[ $pagelinkedto ] = $status; } } + + return $ping_status; } /** diff --git a/tests/phpunit/tests/comment/pingback.php b/tests/phpunit/tests/comment/pingback.php new file mode 100644 index 0000000000..769552ad79 --- /dev/null +++ b/tests/phpunit/tests/comment/pingback.php @@ -0,0 +1,91 @@ +test +test +test +HTML; + + $body = <<test +BODY; + + $this->response = array( + 'body' => $body, + 'response' => array( 'code' => 200 ), + ); + + self::$post_id = self::factory()->post->create( + array( 'post_content' => $content ) + ); + + $post = get_post( self::$post_id ); + $this->assertEquals( array( 'http://example1.org/test' => false ), pingback( $post->post_content, self::$post_id ) ); + } + + public function test_pingback_no_ping_back() { + $content = <<test +test +test +HTML; + + $body = <<test +BODY; + + $this->response = array( + 'body' => $body, + 'response' => array( 'code' => 200 ), + ); + + self::$post_id = self::factory()->post->create( + array( 'post_content' => $content ) + ); + + $post = get_post( self::$post_id ); + $this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) ); + } + + public function test_pingback_error_response() { + $content = <<test +test +test +HTML; + + $this->response = new WP_Error(); + + self::$post_id = self::factory()->post->create( + array( 'post_content' => $content ) + ); + + $post = get_post( self::$post_id ); + $this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) ); + } + + public function request_response() { + return $this->response; + } +}