2003-12-11 01:22:36 +01:00
< ? php
2008-05-25 17:50:15 +02:00
/**
2013-09-22 20:38:56 +02:00
* Handle Trackbacks and Pingbacks Sent to WordPress
*
* @ since 0.71
2008-05-25 17:50:15 +02:00
*
* @ package WordPress
2013-09-22 20:38:56 +02:00
* @ subpackage Trackbacks
2008-05-25 17:50:15 +02:00
*/
2004-09-19 00:47:43 +02:00
2005-06-11 01:15:13 +02:00
if ( empty ( $wp )) {
2013-09-25 02:17:40 +02:00
require_once ( dirname ( __FILE__ ) . '/wp-load.php' );
2010-01-08 09:34:39 +01:00
wp ( array ( 'tb' => '1' ) );
2004-09-19 00:47:43 +02:00
}
2008-05-25 17:50:15 +02:00
/**
2013-09-22 20:38:56 +02:00
* Response to a trackback .
*
* Responds with an error or success XML message .
2008-05-25 17:50:15 +02:00
*
2013-09-22 20:38:56 +02:00
* @ since 0.71
*
2014-11-30 22:22:07 +01:00
* @ param mixed $error Whether there was an error .
* Default '0' . Accepts '0' or '1' , true or false .
* @ param string $error_message Error message if an error occurred .
2008-05-25 17:50:15 +02:00
*/
2004-09-07 04:34:12 +02:00
function trackback_response ( $error = 0 , $error_message = '' ) {
header ( 'Content-Type: text/xml; charset=' . get_option ( 'blog_charset' ) );
if ( $error ) {
echo '<?xml version="1.0" encoding="utf-8"?' . " > \n " ;
echo " <response> \n " ;
echo " <error>1</error> \n " ;
echo " <message> $error_message </message> \n " ;
echo " </response> " ;
2004-09-23 14:27:52 +02:00
die ();
2004-09-07 04:34:12 +02:00
} else {
echo '<?xml version="1.0" encoding="utf-8"?' . " > \n " ;
echo " <response> \n " ;
echo " <error>0</error> \n " ;
echo " </response> " ;
}
}
2003-12-22 04:10:54 +01:00
2013-09-22 20:38:56 +02:00
// Trackback is done by a POST.
2003-12-22 04:10:54 +01:00
$request_array = 'HTTP_POST_VARS' ;
2004-12-16 03:57:05 +01:00
2009-11-26 12:29:54 +01:00
if ( ! isset ( $_GET [ 'tb_id' ]) || ! $_GET [ 'tb_id' ] ) {
2004-09-07 04:34:12 +02:00
$tb_id = explode ( '/' , $_SERVER [ 'REQUEST_URI' ]);
2005-04-20 05:37:23 +02:00
$tb_id = intval ( $tb_id [ count ( $tb_id ) - 1 ] );
2004-09-07 04:34:12 +02:00
}
2004-12-16 03:57:05 +01:00
2009-11-26 12:29:54 +01:00
$tb_url = isset ( $_POST [ 'url' ]) ? $_POST [ 'url' ] : '' ;
$charset = isset ( $_POST [ 'charset' ]) ? $_POST [ 'charset' ] : '' ;
2007-01-05 06:45:46 +01:00
2013-09-22 20:38:56 +02:00
// These three are stripslashed here so they can be properly escaped after mb_convert_encoding().
2013-03-03 22:11:40 +01:00
$title = isset ( $_POST [ 'title' ]) ? wp_unslash ( $_POST [ 'title' ]) : '' ;
$excerpt = isset ( $_POST [ 'excerpt' ]) ? wp_unslash ( $_POST [ 'excerpt' ]) : '' ;
$blog_name = isset ( $_POST [ 'blog_name' ]) ? wp_unslash ( $_POST [ 'blog_name' ]) : '' ;
2004-10-04 10:03:52 +02:00
if ( $charset )
2009-10-14 18:21:28 +02:00
$charset = str_replace ( array ( ',' , ' ' ), '' , strtoupper ( trim ( $charset ) ) );
2004-10-04 10:03:52 +02:00
else
2005-04-21 00:06:28 +02:00
$charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS' ;
2004-10-04 10:03:52 +02:00
2013-09-22 20:38:56 +02:00
// No valid uses for UTF-7.
2008-03-28 07:51:19 +01:00
if ( false !== strpos ( $charset , 'UTF-7' ) )
die ;
2013-09-22 20:38:56 +02:00
// For international trackbacks.
if ( function_exists ( 'mb_convert_encoding' ) ) {
2006-08-30 23:46:31 +02:00
$title = mb_convert_encoding ( $title , get_option ( 'blog_charset' ), $charset );
$excerpt = mb_convert_encoding ( $excerpt , get_option ( 'blog_charset' ), $charset );
$blog_name = mb_convert_encoding ( $blog_name , get_option ( 'blog_charset' ), $charset );
2004-10-04 10:03:52 +02:00
}
2003-12-22 04:10:54 +01:00
2013-09-22 20:38:56 +02:00
// Now that mb_convert_encoding() has been given a swing, we need to escape these three.
2013-03-03 22:11:40 +01:00
$title = wp_slash ( $title );
$excerpt = wp_slash ( $excerpt );
$blog_name = wp_slash ( $blog_name );
2013-03-01 17:28:40 +01:00
2006-11-19 08:56:05 +01:00
if ( is_single () || is_page () )
$tb_id = $posts [ 0 ] -> ID ;
2004-09-07 04:34:12 +02:00
2009-11-26 12:29:54 +01:00
if ( ! isset ( $tb_id ) || ! intval ( $tb_id ) )
2016-10-06 20:08:36 +02:00
trackback_response ( 1 , __ ( 'I really need an ID for this to work.' ) );
2003-12-22 04:10:54 +01:00
if ( empty ( $title ) && empty ( $tb_url ) && empty ( $blog_name )) {
2013-09-22 20:38:56 +02:00
// If it doesn't look like a trackback at all.
2006-06-27 07:38:56 +02:00
wp_redirect ( get_permalink ( $tb_id ));
2004-09-07 04:34:12 +02:00
exit ;
2003-12-11 01:22:36 +01:00
}
2007-01-25 03:03:55 +01:00
if ( ! empty ( $tb_url ) && ! empty ( $title ) ) {
2016-10-14 21:38:17 +02:00
/**
* Fires before the trackback is added to a post .
*
* @ since 4.7 . 0
*
* @ param int $tb_id Post ID related to the trackback .
* @ param string $tb_url Trackback URL .
* @ param string $charset Character Set .
* @ param string $title Trackback Title .
* @ param string $excerpt Trackback Excerpt .
* @ param string $blog_name Blog Name .
*/
do_action ( 'pre_trackback_post' , $tb_id , $tb_url , $charset , $title , $excerpt , $blog_name );
2004-09-07 04:34:12 +02:00
header ( 'Content-Type: text/xml; charset=' . get_option ( 'blog_charset' ) );
2003-12-11 01:22:36 +01:00
2008-02-04 21:27:45 +01:00
if ( ! pings_open ( $tb_id ) )
2016-10-06 20:08:36 +02:00
trackback_response ( 1 , __ ( 'Sorry, trackbacks are closed for this item.' ) );
2003-12-11 01:22:36 +01:00
2013-05-09 02:22:02 +02:00
$title = wp_html_excerpt ( $title , 250 , '…' );
$excerpt = wp_html_excerpt ( $excerpt , 252 , '…' );
2003-12-11 01:22:36 +01:00
2007-03-23 01:59:21 +01:00
$comment_post_ID = ( int ) $tb_id ;
2004-09-22 21:44:35 +02:00
$comment_author = $blog_name ;
2004-09-07 04:34:12 +02:00
$comment_author_email = '' ;
2003-12-11 01:22:36 +01:00
$comment_author_url = $tb_url ;
2004-09-07 04:34:12 +02:00
$comment_content = " <strong> $title </strong> \n \n $excerpt " ;
$comment_type = 'trackback' ;
2003-12-11 01:22:36 +01:00
2008-04-14 18:13:25 +02:00
$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 ) );
2005-01-11 03:56:43 +01:00
if ( $dupe )
2016-10-06 20:08:36 +02:00
trackback_response ( 1 , __ ( 'We already have a ping from that URL for this post.' ) );
2005-01-11 03:56:43 +01:00
2004-09-22 21:44:35 +02:00
$commentdata = compact ( 'comment_post_ID' , 'comment_author' , 'comment_author_email' , 'comment_author_url' , 'comment_content' , 'comment_type' );
2003-12-11 01:22:36 +01:00
2004-09-07 04:34:12 +02:00
wp_new_comment ( $commentdata );
2013-09-05 18:22:14 +02:00
$trackback_id = $wpdb -> insert_id ;
/**
2013-09-06 03:37:59 +02:00
* Fires after a trackback is added to a post .
2013-09-05 18:22:14 +02:00
*
* @ since 1.2 . 0
2013-09-22 20:38:56 +02:00
*
2013-09-05 18:22:14 +02:00
* @ param int $trackback_id Trackback ID .
*/
do_action ( 'trackback_post' , $trackback_id );
trackback_response ( 0 );
2003-12-11 01:22:36 +01:00
}