| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 11 if ( empty( $wp ) ) { 12 require_once __DIR__ . '/wp-load.php'; 13 wp( array( 'tb' => '1' ) ); 14 } 15 16 // Always run as an unauthenticated user. 17 wp_set_current_user( 0 ); 18 19 /** 20 * Response to a trackback. 21 * 22 * Responds with an error or success XML message. 23 * 24 * @since 0.71 25 * 26 * @param int|bool $error Whether there was an error. 27 * Default 0. Accepts 0 or 1, true or false. 28 * @param string $error_message Error message if an error occurred. Default empty string. 29 * @return void Never returns if `$error` is truthy, as the function dies after 30 * sending the error response. 31 * @phpstan-return ( $error is 0|false ? void : never ) 32 */ 33 function trackback_response( $error = 0, $error_message = '' ) { 34 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 35 36 if ( $error ) { 37 echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; 38 echo "<response>\n"; 39 echo "<error>1</error>\n"; 40 echo '<message>' . esc_xml( $error_message ) . "</message>\n"; 41 echo '</response>'; 42 die(); 43 } else { 44 echo '<?xml version="1.0" encoding="utf-8"?' . ">\n"; 45 echo "<response>\n"; 46 echo "<error>0</error>\n"; 47 echo '</response>'; 48 } 49 } 50 51 if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) { 52 $post_id = explode( '/', $_SERVER['REQUEST_URI'] ); 53 $post_id = (int) $post_id[ count( $post_id ) - 1 ]; 54 } 55 56 $trackback_url = isset( $_POST['url'] ) ? sanitize_url( $_POST['url'] ) : ''; 57 $charset = isset( $_POST['charset'] ) ? sanitize_text_field( $_POST['charset'] ) : ''; 58 59 // These three are stripslashed here so they can be properly escaped after mb_convert_encoding(). 60 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; 61 $excerpt = isset( $_POST['excerpt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['excerpt'] ) ) : ''; 62 $blog_name = isset( $_POST['blog_name'] ) ? sanitize_text_field( wp_unslash( $_POST['blog_name'] ) ) : ''; 63 64 if ( $charset ) { 65 $charset = str_replace( array( ',', ' ' ), '', strtoupper( trim( $charset ) ) ); 66 67 // Validate the specified "sender" charset is available on the receiving site. 68 if ( function_exists( 'mb_list_encodings' ) && ! in_array( $charset, mb_list_encodings(), true ) ) { 69 $charset = ''; 70 } 71 } 72 73 if ( ! $charset ) { 74 $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; 75 } 76 77 // No valid uses for UTF-7. 78 if ( str_contains( $charset, 'UTF-7' ) ) { 79 die; 80 } 81 82 // For international trackbacks. 83 if ( function_exists( 'mb_convert_encoding' ) ) { 84 $title = mb_convert_encoding( $title, get_option( 'blog_charset' ), $charset ); 85 $excerpt = mb_convert_encoding( $excerpt, get_option( 'blog_charset' ), $charset ); 86 $blog_name = mb_convert_encoding( $blog_name, get_option( 'blog_charset' ), $charset ); 87 } 88 89 // Escape values to use in the trackback. 90 $title = wp_slash( $title ); 91 $excerpt = wp_slash( $excerpt ); 92 $blog_name = wp_slash( $blog_name ); 93 94 if ( is_single() || is_page() ) { 95 $post_id = $posts[0]->ID; 96 } 97 98 if ( ! isset( $post_id ) || ! (int) $post_id ) { 99 trackback_response( 1, __( 'I really need an ID for this to work.' ) ); 100 } 101 102 if ( empty( $title ) && empty( $trackback_url ) && empty( $blog_name ) ) { 103 // If it doesn't look like a trackback at all. 104 wp_redirect( get_permalink( $post_id ) ); 105 exit; 106 } 107 108 if ( ! empty( $trackback_url ) && ! empty( $title ) ) { 109 /** 110 * Fires before the trackback is added to a post. 111 * 112 * @since 4.7.0 113 * 114 * @param int $post_id Post ID related to the trackback. 115 * @param string $trackback_url Trackback URL. 116 * @param string $charset Character set. 117 * @param string $title Trackback title. 118 * @param string $excerpt Trackback excerpt. 119 * @param string $blog_name Site name. 120 */ 121 do_action( 'pre_trackback_post', $post_id, $trackback_url, $charset, $title, $excerpt, $blog_name ); 122 123 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 124 125 if ( ! pings_open( $post_id ) ) { 126 trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) ); 127 } 128 129 $title = wp_html_excerpt( $title, 250, '…' ); 130 $excerpt = wp_html_excerpt( $excerpt, 252, '…' ); 131 132 $comment_post_id = (int) $post_id; 133 $comment_author = $blog_name; 134 $comment_author_email = ''; 135 $comment_author_url = $trackback_url; 136 $comment_content = "<strong>$title</strong>\n\n$excerpt"; 137 $comment_type = 'trackback'; 138 139 $dupe = $wpdb->get_results( 140 $wpdb->prepare( 141 "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", 142 $comment_post_id, 143 $comment_author_url 144 ) 145 ); 146 147 if ( $dupe ) { 148 trackback_response( 1, __( 'There is already a ping from that URL for this post.' ) ); 149 } 150 151 $commentdata = array( 152 'comment_post_ID' => $comment_post_id, 153 ); 154 155 $commentdata += compact( 156 'comment_author', 157 'comment_author_email', 158 'comment_author_url', 159 'comment_content', 160 'comment_type' 161 ); 162 163 $result = wp_new_comment( $commentdata ); 164 165 if ( is_wp_error( $result ) ) { 166 trackback_response( 1, $result->get_error_message() ); 167 } 168 169 $trackback_id = $wpdb->insert_id; 170 171 /** 172 * Fires after a trackback is added to a post. 173 * 174 * @since 1.2.0 175 * 176 * @param int $trackback_id Trackback ID. 177 */ 178 do_action( 'trackback_post', $trackback_id ); 179 180 trackback_response( 0 ); 181 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Jul 17 08:20:15 2026 | Cross-referenced by PHPXref |