[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/ -> wp-trackback.php (source)

   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   */
  30  function trackback_response( $error = 0, $error_message = '' ) {
  31      header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
  32  
  33      if ( $error ) {
  34          echo '<?xml version="1.0" encoding="utf-8"?' . ">\n";
  35          echo "<response>\n";
  36          echo "<error>1</error>\n";
  37          echo "<message>$error_message</message>\n";
  38          echo '</response>';
  39          die();
  40      } else {
  41          echo '<?xml version="1.0" encoding="utf-8"?' . ">\n";
  42          echo "<response>\n";
  43          echo "<error>0</error>\n";
  44          echo '</response>';
  45      }
  46  }
  47  
  48  if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) {
  49      $post_id = explode( '/', $_SERVER['REQUEST_URI'] );
  50      $post_id = (int) $post_id[ count( $post_id ) - 1 ];
  51  }
  52  
  53  $trackback_url = isset( $_POST['url'] ) ? $_POST['url'] : '';
  54  $charset       = isset( $_POST['charset'] ) ? $_POST['charset'] : '';
  55  
  56  // These three are stripslashed here so they can be properly escaped after mb_convert_encoding().
  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'] ) : '';
  60  
  61  if ( $charset ) {
  62      $charset = str_replace( array( ',', ' ' ), '', strtoupper( trim( $charset ) ) );
  63  } else {
  64      $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
  65  }
  66  
  67  // No valid uses for UTF-7.
  68  if ( str_contains( $charset, 'UTF-7' ) ) {
  69      die;
  70  }
  71  
  72  // For international trackbacks.
  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 );
  77  }
  78  
  79  // Escape values to use in the trackback.
  80  $title     = wp_slash( $title );
  81  $excerpt   = wp_slash( $excerpt );
  82  $blog_name = wp_slash( $blog_name );
  83  
  84  if ( is_single() || is_page() ) {
  85      $post_id = $posts[0]->ID;
  86  }
  87  
  88  if ( ! isset( $post_id ) || ! (int) $post_id ) {
  89      trackback_response( 1, __( 'I really need an ID for this to work.' ) );
  90  }
  91  
  92  if ( empty( $title ) && empty( $trackback_url ) && empty( $blog_name ) ) {
  93      // If it doesn't look like a trackback at all.
  94      wp_redirect( get_permalink( $post_id ) );
  95      exit;
  96  }
  97  
  98  if ( ! empty( $trackback_url ) && ! empty( $title ) ) {
  99      /**
 100       * Fires before the trackback is added to a post.
 101       *
 102       * @since 4.7.0
 103       *
 104       * @param int    $post_id       Post ID related to the trackback.
 105       * @param string $trackback_url Trackback URL.
 106       * @param string $charset       Character set.
 107       * @param string $title         Trackback title.
 108       * @param string $excerpt       Trackback excerpt.
 109       * @param string $blog_name     Site name.
 110       */
 111      do_action( 'pre_trackback_post', $post_id, $trackback_url, $charset, $title, $excerpt, $blog_name );
 112  
 113      header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
 114  
 115      if ( ! pings_open( $post_id ) ) {
 116          trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) );
 117      }
 118  
 119      $title   = wp_html_excerpt( $title, 250, '&#8230;' );
 120      $excerpt = wp_html_excerpt( $excerpt, 252, '&#8230;' );
 121  
 122      $comment_post_id      = (int) $post_id;
 123      $comment_author       = $blog_name;
 124      $comment_author_email = '';
 125      $comment_author_url   = $trackback_url;
 126      $comment_content      = "<strong>$title</strong>\n\n$excerpt";
 127      $comment_type         = 'trackback';
 128  
 129      $dupe = $wpdb->get_results(
 130          $wpdb->prepare(
 131              "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s",
 132              $comment_post_id,
 133              $comment_author_url
 134          )
 135      );
 136  
 137      if ( $dupe ) {
 138          trackback_response( 1, __( 'There is already a ping from that URL for this post.' ) );
 139      }
 140  
 141      $commentdata = array(
 142          'comment_post_ID' => $comment_post_id,
 143      );
 144  
 145      $commentdata += compact(
 146          'comment_author',
 147          'comment_author_email',
 148          'comment_author_url',
 149          'comment_content',
 150          'comment_type'
 151      );
 152  
 153      $result = wp_new_comment( $commentdata );
 154  
 155      if ( is_wp_error( $result ) ) {
 156          trackback_response( 1, $result->get_error_message() );
 157      }
 158  
 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 );
 169  
 170      trackback_response( 0 );
 171  }


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref