author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
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 |
||
16 |
/** |
|
17 |
* Response to a trackback. |
|
18 |
* |
|
19 |
* Responds with an error or success XML message. |
|
20 |
* |
|
21 |
* @since 0.71 |
|
22 |
* |
|
16 | 23 |
* @param int|bool $error Whether there was an error. |
24 |
* Default '0'. Accepts '0' or '1', true or false. |
|
25 |
* @param string $error_message Error message if an error occurred. |
|
0 | 26 |
*/ |
9 | 27 |
function trackback_response( $error = 0, $error_message = '' ) { |
28 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
|
29 |
if ( $error ) { |
|
30 |
echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; |
|
0 | 31 |
echo "<response>\n"; |
32 |
echo "<error>1</error>\n"; |
|
33 |
echo "<message>$error_message</message>\n"; |
|
9 | 34 |
echo '</response>'; |
0 | 35 |
die(); |
36 |
} else { |
|
9 | 37 |
echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; |
0 | 38 |
echo "<response>\n"; |
39 |
echo "<error>0</error>\n"; |
|
9 | 40 |
echo '</response>'; |
0 | 41 |
} |
42 |
} |
|
43 |
||
44 |
// Trackback is done by a POST. |
|
45 |
$request_array = 'HTTP_POST_VARS'; |
|
46 |
||
9 | 47 |
if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) { |
48 |
$tb_id = explode( '/', $_SERVER['REQUEST_URI'] ); |
|
49 |
$tb_id = intval( $tb_id[ count( $tb_id ) - 1 ] ); |
|
0 | 50 |
} |
51 |
||
9 | 52 |
$tb_url = isset( $_POST['url'] ) ? $_POST['url'] : ''; |
53 |
$charset = isset( $_POST['charset'] ) ? $_POST['charset'] : ''; |
|
0 | 54 |
|
55 |
// These three are stripslashed here so they can be properly escaped after mb_convert_encoding(). |
|
9 | 56 |
$title = isset( $_POST['title'] ) ? wp_unslash( $_POST['title'] ) : ''; |
57 |
$excerpt = isset( $_POST['excerpt'] ) ? wp_unslash( $_POST['excerpt'] ) : ''; |
|
58 |
$blog_name = isset( $_POST['blog_name'] ) ? wp_unslash( $_POST['blog_name'] ) : ''; |
|
0 | 59 |
|
9 | 60 |
if ( $charset ) { |
61 |
$charset = str_replace( array( ',', ' ' ), '', strtoupper( trim( $charset ) ) ); |
|
62 |
} else { |
|
0 | 63 |
$charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; |
9 | 64 |
} |
0 | 65 |
|
66 |
// No valid uses for UTF-7. |
|
9 | 67 |
if ( false !== strpos( $charset, 'UTF-7' ) ) { |
0 | 68 |
die; |
9 | 69 |
} |
0 | 70 |
|
71 |
// For international trackbacks. |
|
9 | 72 |
if ( function_exists( 'mb_convert_encoding' ) ) { |
73 |
$title = mb_convert_encoding( $title, get_option( 'blog_charset' ), $charset ); |
|
74 |
$excerpt = mb_convert_encoding( $excerpt, get_option( 'blog_charset' ), $charset ); |
|
75 |
$blog_name = mb_convert_encoding( $blog_name, get_option( 'blog_charset' ), $charset ); |
|
0 | 76 |
} |
77 |
||
78 |
// Now that mb_convert_encoding() has been given a swing, we need to escape these three. |
|
9 | 79 |
$title = wp_slash( $title ); |
80 |
$excerpt = wp_slash( $excerpt ); |
|
81 |
$blog_name = wp_slash( $blog_name ); |
|
0 | 82 |
|
9 | 83 |
if ( is_single() || is_page() ) { |
0 | 84 |
$tb_id = $posts[0]->ID; |
9 | 85 |
} |
0 | 86 |
|
9 | 87 |
if ( ! isset( $tb_id ) || ! intval( $tb_id ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
trackback_response( 1, __( 'I really need an ID for this to work.' ) ); |
9 | 89 |
} |
0 | 90 |
|
9 | 91 |
if ( empty( $title ) && empty( $tb_url ) && empty( $blog_name ) ) { |
0 | 92 |
// If it doesn't look like a trackback at all. |
9 | 93 |
wp_redirect( get_permalink( $tb_id ) ); |
0 | 94 |
exit; |
95 |
} |
|
96 |
||
9 | 97 |
if ( ! empty( $tb_url ) && ! empty( $title ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
/** |
9 | 99 |
* Fires before the trackback is added to a post. |
100 |
* |
|
101 |
* @since 4.7.0 |
|
102 |
* |
|
103 |
* @param int $tb_id Post ID related to the trackback. |
|
104 |
* @param string $tb_url Trackback URL. |
|
105 |
* @param string $charset Character Set. |
|
106 |
* @param string $title Trackback Title. |
|
107 |
* @param string $excerpt Trackback Excerpt. |
|
108 |
* @param string $blog_name Blog Name. |
|
109 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
do_action( 'pre_trackback_post', $tb_id, $tb_url, $charset, $title, $excerpt, $blog_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
|
9 | 112 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); |
0 | 113 |
|
9 | 114 |
if ( ! pings_open( $tb_id ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) ); |
9 | 116 |
} |
0 | 117 |
|
9 | 118 |
$title = wp_html_excerpt( $title, 250, '…' ); |
0 | 119 |
$excerpt = wp_html_excerpt( $excerpt, 252, '…' ); |
120 |
||
9 | 121 |
$comment_post_ID = (int) $tb_id; |
122 |
$comment_author = $blog_name; |
|
0 | 123 |
$comment_author_email = ''; |
9 | 124 |
$comment_author_url = $tb_url; |
125 |
$comment_content = "<strong>$title</strong>\n\n$excerpt"; |
|
126 |
$comment_type = 'trackback'; |
|
0 | 127 |
|
9 | 128 |
$dupe = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $comment_post_ID, $comment_author_url ) ); |
129 |
if ( $dupe ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
trackback_response( 1, __( 'We already have a ping from that URL for this post.' ) ); |
9 | 131 |
} |
0 | 132 |
|
9 | 133 |
$commentdata = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type' ); |
0 | 134 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
$result = wp_new_comment( $commentdata ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
if ( is_wp_error( $result ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
trackback_response( 1, $result->get_error_message() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
|
0 | 141 |
$trackback_id = $wpdb->insert_id; |
142 |
||
143 |
/** |
|
144 |
* Fires after a trackback is added to a post. |
|
145 |
* |
|
146 |
* @since 1.2.0 |
|
147 |
* |
|
148 |
* @param int $trackback_id Trackback ID. |
|
149 |
*/ |
|
150 |
do_action( 'trackback_post', $trackback_id ); |
|
151 |
trackback_response( 0 ); |
|
152 |
} |