author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Handle Trackbacks and Pingbacks Sent to WordPress |
|
4 |
* |
|
5 |
* @since 0.71 |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Trackbacks |
|
9 |
*/ |
|
10 |
||
9 | 11 |
if ( empty( $wp ) ) { |
16 | 12 |
require_once __DIR__ . '/wp-load.php'; |
0 | 13 |
wp( array( 'tb' => '1' ) ); |
14 |
} |
|
15 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
16 |
// Always run as an unauthenticated user. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
wp_set_current_user( 0 ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
18 |
|
0 | 19 |
/** |
20 |
* Response to a trackback. |
|
21 |
* |
|
22 |
* Responds with an error or success XML message. |
|
23 |
* |
|
24 |
* @since 0.71 |
|
25 |
* |
|
16 | 26 |
* @param int|bool $error Whether there was an error. |
27 |
* Default '0'. Accepts '0' or '1', true or false. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
* @param string $error_message Error message if an error occurred. Default empty string. |
0 | 29 |
*/ |
9 | 30 |
function trackback_response( $error = 0, $error_message = '' ) { |
31 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
|
9 | 33 |
if ( $error ) { |
34 |
echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; |
|
0 | 35 |
echo "<response>\n"; |
36 |
echo "<error>1</error>\n"; |
|
37 |
echo "<message>$error_message</message>\n"; |
|
9 | 38 |
echo '</response>'; |
0 | 39 |
die(); |
40 |
} else { |
|
9 | 41 |
echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; |
0 | 42 |
echo "<response>\n"; |
43 |
echo "<error>0</error>\n"; |
|
9 | 44 |
echo '</response>'; |
0 | 45 |
} |
46 |
} |
|
47 |
||
9 | 48 |
if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
$post_id = explode( '/', $_SERVER['REQUEST_URI'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
$post_id = (int) $post_id[ count( $post_id ) - 1 ]; |
0 | 51 |
} |
52 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
$trackback_url = isset( $_POST['url'] ) ? $_POST['url'] : ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
$charset = isset( $_POST['charset'] ) ? $_POST['charset'] : ''; |
0 | 55 |
|
56 |
// These three are stripslashed here so they can be properly escaped after mb_convert_encoding(). |
|
9 | 57 |
$title = isset( $_POST['title'] ) ? wp_unslash( $_POST['title'] ) : ''; |
58 |
$excerpt = isset( $_POST['excerpt'] ) ? wp_unslash( $_POST['excerpt'] ) : ''; |
|
59 |
$blog_name = isset( $_POST['blog_name'] ) ? wp_unslash( $_POST['blog_name'] ) : ''; |
|
0 | 60 |
|
9 | 61 |
if ( $charset ) { |
62 |
$charset = str_replace( array( ',', ' ' ), '', strtoupper( trim( $charset ) ) ); |
|
63 |
} else { |
|
0 | 64 |
$charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; |
9 | 65 |
} |
0 | 66 |
|
67 |
// No valid uses for UTF-7. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
if ( str_contains( $charset, 'UTF-7' ) ) { |
0 | 69 |
die; |
9 | 70 |
} |
0 | 71 |
|
72 |
// For international trackbacks. |
|
9 | 73 |
if ( function_exists( 'mb_convert_encoding' ) ) { |
74 |
$title = mb_convert_encoding( $title, get_option( 'blog_charset' ), $charset ); |
|
75 |
$excerpt = mb_convert_encoding( $excerpt, get_option( 'blog_charset' ), $charset ); |
|
76 |
$blog_name = mb_convert_encoding( $blog_name, get_option( 'blog_charset' ), $charset ); |
|
0 | 77 |
} |
78 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
// Escape values to use in the trackback. |
9 | 80 |
$title = wp_slash( $title ); |
81 |
$excerpt = wp_slash( $excerpt ); |
|
82 |
$blog_name = wp_slash( $blog_name ); |
|
0 | 83 |
|
9 | 84 |
if ( is_single() || is_page() ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
$post_id = $posts[0]->ID; |
9 | 86 |
} |
0 | 87 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
88 |
if ( ! isset( $post_id ) || ! (int) $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
trackback_response( 1, __( 'I really need an ID for this to work.' ) ); |
9 | 90 |
} |
0 | 91 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
if ( empty( $title ) && empty( $trackback_url ) && empty( $blog_name ) ) { |
0 | 93 |
// If it doesn't look like a trackback at all. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
wp_redirect( get_permalink( $post_id ) ); |
0 | 95 |
exit; |
96 |
} |
|
97 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
if ( ! empty( $trackback_url ) && ! empty( $title ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
/** |
9 | 100 |
* Fires before the trackback is added to a post. |
101 |
* |
|
102 |
* @since 4.7.0 |
|
103 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
* @param int $post_id Post ID related to the trackback. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
* @param string $trackback_url Trackback URL. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
* @param string $charset Character set. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
* @param string $title Trackback title. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
* @param string $excerpt Trackback excerpt. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
* @param string $blog_name Site name. |
9 | 110 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
do_action( 'pre_trackback_post', $post_id, $trackback_url, $charset, $title, $excerpt, $blog_name ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
|
9 | 113 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
0 | 114 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
if ( ! pings_open( $post_id ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) ); |
9 | 117 |
} |
0 | 118 |
|
9 | 119 |
$title = wp_html_excerpt( $title, 250, '…' ); |
0 | 120 |
$excerpt = wp_html_excerpt( $excerpt, 252, '…' ); |
121 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
$comment_post_id = (int) $post_id; |
9 | 123 |
$comment_author = $blog_name; |
0 | 124 |
$comment_author_email = ''; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
125 |
$comment_author_url = $trackback_url; |
9 | 126 |
$comment_content = "<strong>$title</strong>\n\n$excerpt"; |
127 |
$comment_type = 'trackback'; |
|
0 | 128 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
$dupe = $wpdb->get_results( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
$wpdb->prepare( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
"SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
$comment_post_id, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
$comment_author_url |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
|
9 | 137 |
if ( $dupe ) { |
19 | 138 |
trackback_response( 1, __( 'There is already a ping from that URL for this post.' ) ); |
9 | 139 |
} |
0 | 140 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
$commentdata = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
'comment_post_ID' => $comment_post_id, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
$commentdata += compact( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
'comment_author', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
'comment_author_email', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
'comment_author_url', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
'comment_content', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
'comment_type' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
); |
0 | 152 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
$result = wp_new_comment( $commentdata ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
if ( is_wp_error( $result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
trackback_response( 1, $result->get_error_message() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
|
0 | 159 |
$trackback_id = $wpdb->insert_id; |
160 |
||
161 |
/** |
|
162 |
* Fires after a trackback is added to a post. |
|
163 |
* |
|
164 |
* @since 1.2.0 |
|
165 |
* |
|
166 |
* @param int $trackback_id Trackback ID. |
|
167 |
*/ |
|
168 |
do_action( 'trackback_post', $trackback_id ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
|
0 | 170 |
trackback_response( 0 ); |
171 |
} |