Skip to content

Commit 626d9af

Browse files
committed
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
1 parent 66b98b9 commit 626d9af

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

src/wp-includes/comment.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,7 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) {
28902890

28912891
$pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote );
28922892
$pingback_link_offset_squote = strpos( $contents, $pingback_str_squote );
2893+
28932894
if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
28942895
$quote = ( $pingback_link_offset_dquote ) ? '"' : '\'';
28952896
$pingback_link_offset = ( '"' === $quote ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
@@ -3079,9 +3080,11 @@ function generic_ping( $post_id = 0 ) {
30793080
*
30803081
* @since 0.71
30813082
* @since 4.7.0 `$post` can be a WP_Post object.
3083+
* @since 6.8.0 Returns an array of pingback statuses indexed by link.
30823084
*
30833085
* @param string $content Post content to check for links. If empty will retrieve from post.
30843086
* @param int|WP_Post $post Post ID or object.
3087+
* @return array<string, bool> An array of pingback statuses indexed by link.
30853088
*/
30863089
function pingback( $content, $post ) {
30873090
require_once ABSPATH . WPINC . '/class-IXR.php';
@@ -3093,7 +3096,7 @@ function pingback( $content, $post ) {
30933096
$post = get_post( $post );
30943097

30953098
if ( ! $post ) {
3096-
return;
3099+
return array();
30973100
}
30983101

30993102
$pung = get_pung( $post );
@@ -3108,6 +3111,7 @@ function pingback( $content, $post ) {
31083111
*/
31093112
$post_links_temp = wp_extract_urls( $content );
31103113

3114+
$ping_status = array();
31113115
/*
31123116
* Step 2.
31133117
* Walking through the links array.
@@ -3179,11 +3183,18 @@ function pingback( $content, $post ) {
31793183
// When set to true, this outputs debug messages by itself.
31803184
$client->debug = false;
31813185

3182-
if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered.
3186+
$status = $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto );
3187+
3188+
if ( $status // Ping registered.
3189+
|| ( isset( $client->error->code ) && 48 === $client->error->code ) // Already registered.
3190+
) {
31833191
add_ping( $post, $pagelinkedto );
31843192
}
3193+
$ping_status[ $pagelinkedto ] = $status;
31853194
}
31863195
}
3196+
3197+
return $ping_status;
31873198
}
31883199

31893200
/**
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* @group comment
5+
* @covers ::pingback
6+
*/
7+
class Tests_Comment_Pingback extends WP_UnitTestCase {
8+
9+
protected static $post_id;
10+
protected $response = array();
11+
12+
public function set_up() {
13+
parent::set_up();
14+
15+
add_filter( 'pre_http_request', array( $this, 'request_response' ) );
16+
}
17+
18+
public function tear_down() {
19+
remove_filter( 'pre_http_request', array( $this, 'request_response' ) );
20+
parent::tear_down();
21+
}
22+
23+
public function test_pingback() {
24+
$content = <<<HTML
25+
<a href="http://example.org">test</a>
26+
<a href="http://example1.org/test">test</a>
27+
<a href="http://example3.org/">test</a>
28+
HTML;
29+
30+
$body = <<<BODY
31+
<a rel="pingback" href="https://example1.org/test/pingback">test</a>
32+
BODY;
33+
34+
$this->response = array(
35+
'body' => $body,
36+
'response' => array( 'code' => 200 ),
37+
);
38+
39+
self::$post_id = self::factory()->post->create(
40+
array( 'post_content' => $content )
41+
);
42+
43+
$post = get_post( self::$post_id );
44+
$this->assertEquals( array( 'http://example1.org/test' => false ), pingback( $post->post_content, self::$post_id ) );
45+
}
46+
47+
public function test_pingback_no_ping_back() {
48+
$content = <<<HTML
49+
<a href="http://example.org">test</a>
50+
<a href="http://example1.org/test">test</a>
51+
<a href="http://example3.org/">test</a>
52+
HTML;
53+
54+
$body = <<<BODY
55+
<a href="https://example1.org/test">test</a>
56+
BODY;
57+
58+
$this->response = array(
59+
'body' => $body,
60+
'response' => array( 'code' => 200 ),
61+
);
62+
63+
self::$post_id = self::factory()->post->create(
64+
array( 'post_content' => $content )
65+
);
66+
67+
$post = get_post( self::$post_id );
68+
$this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) );
69+
}
70+
71+
public function test_pingback_error_response() {
72+
$content = <<<HTML
73+
<a href="http://example.org">test</a>
74+
<a href="http://example1.org/test">test</a>
75+
<a href="http://example3.org/">test</a>
76+
HTML;
77+
78+
$this->response = new WP_Error();
79+
80+
self::$post_id = self::factory()->post->create(
81+
array( 'post_content' => $content )
82+
);
83+
84+
$post = get_post( self::$post_id );
85+
$this->assertEquals( array(), pingback( $post->post_content, self::$post_id ) );
86+
}
87+
88+
public function request_response() {
89+
return $this->response;
90+
}
91+
}

0 commit comments

Comments
 (0)