author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
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 ) ) ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
63 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
64 |
// Validate the specified "sender" charset is available on the receiving site. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
65 |
if ( function_exists( 'mb_list_encodings' ) && ! in_array( $charset, mb_list_encodings(), true ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
66 |
$charset = ''; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
67 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
68 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
69 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
70 |
if ( ! $charset ) { |
0 | 71 |
$charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; |
9 | 72 |
} |
0 | 73 |
|
74 |
// 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
|
75 |
if ( str_contains( $charset, 'UTF-7' ) ) { |
0 | 76 |
die; |
9 | 77 |
} |
0 | 78 |
|
79 |
// For international trackbacks. |
|
9 | 80 |
if ( function_exists( 'mb_convert_encoding' ) ) { |
81 |
$title = mb_convert_encoding( $title, get_option( 'blog_charset' ), $charset ); |
|
82 |
$excerpt = mb_convert_encoding( $excerpt, get_option( 'blog_charset' ), $charset ); |
|
83 |
$blog_name = mb_convert_encoding( $blog_name, get_option( 'blog_charset' ), $charset ); |
|
0 | 84 |
} |
85 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
// Escape values to use in the trackback. |
9 | 87 |
$title = wp_slash( $title ); |
88 |
$excerpt = wp_slash( $excerpt ); |
|
89 |
$blog_name = wp_slash( $blog_name ); |
|
0 | 90 |
|
9 | 91 |
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
|
92 |
$post_id = $posts[0]->ID; |
9 | 93 |
} |
0 | 94 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
if ( ! isset( $post_id ) || ! (int) $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
trackback_response( 1, __( 'I really need an ID for this to work.' ) ); |
9 | 97 |
} |
0 | 98 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
if ( empty( $title ) && empty( $trackback_url ) && empty( $blog_name ) ) { |
0 | 100 |
// 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
|
101 |
wp_redirect( get_permalink( $post_id ) ); |
0 | 102 |
exit; |
103 |
} |
|
104 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
if ( ! empty( $trackback_url ) && ! empty( $title ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
/** |
9 | 107 |
* Fires before the trackback is added to a post. |
108 |
* |
|
109 |
* @since 4.7.0 |
|
110 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
* @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
|
112 |
* @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
|
113 |
* @param string $charset Character set. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
* @param string $title Trackback title. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
* @param string $excerpt Trackback excerpt. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
* @param string $blog_name Site name. |
9 | 117 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
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
|
119 |
|
9 | 120 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
0 | 121 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
if ( ! pings_open( $post_id ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) ); |
9 | 124 |
} |
0 | 125 |
|
9 | 126 |
$title = wp_html_excerpt( $title, 250, '…' ); |
0 | 127 |
$excerpt = wp_html_excerpt( $excerpt, 252, '…' ); |
128 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
$comment_post_id = (int) $post_id; |
9 | 130 |
$comment_author = $blog_name; |
0 | 131 |
$comment_author_email = ''; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
$comment_author_url = $trackback_url; |
9 | 133 |
$comment_content = "<strong>$title</strong>\n\n$excerpt"; |
134 |
$comment_type = 'trackback'; |
|
0 | 135 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
$dupe = $wpdb->get_results( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
$wpdb->prepare( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
"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
|
139 |
$comment_post_id, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
$comment_author_url |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
|
9 | 144 |
if ( $dupe ) { |
19 | 145 |
trackback_response( 1, __( 'There is already a ping from that URL for this post.' ) ); |
9 | 146 |
} |
0 | 147 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
$commentdata = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
'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
|
150 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
$commentdata += compact( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
'comment_author', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
'comment_author_email', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
'comment_author_url', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
'comment_content', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
'comment_type' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
); |
0 | 159 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
$result = wp_new_comment( $commentdata ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
if ( is_wp_error( $result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
trackback_response( 1, $result->get_error_message() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
|
0 | 166 |
$trackback_id = $wpdb->insert_id; |
167 |
||
168 |
/** |
|
169 |
* Fires after a trackback is added to a post. |
|
170 |
* |
|
171 |
* @since 1.2.0 |
|
172 |
* |
|
173 |
* @param int $trackback_id Trackback ID. |
|
174 |
*/ |
|
175 |
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
|
176 |
|
0 | 177 |
trackback_response( 0 ); |
178 |
} |