[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> export.php (source)

   1  <?php
   2  /**
   3   * WordPress Export Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Version number for the export format.
  11   *
  12   * Bump this when something changes that might affect compatibility.
  13   *
  14   * @since 2.5.0
  15   */
  16  define( 'WXR_VERSION', '1.2' );
  17  
  18  /**
  19   * Generates the WXR export file for download.
  20   *
  21   * Default behavior is to export all content, however, note that post content will only
  22   * be exported for post types with the `can_export` argument enabled. Any posts with the
  23   * 'auto-draft' status will be skipped.
  24   *
  25   * @since 2.1.0
  26   * @since 5.7.0 Added the `post_modified` and `post_modified_gmt` fields to the export file.
  27   *
  28   * @global wpdb    $wpdb WordPress database abstraction object.
  29   * @global WP_Post $post Global post object.
  30   *
  31   * @param array $args {
  32   *     Optional. Arguments for generating the WXR export file for download. Default empty array.
  33   *
  34   *     @type string $content    Type of content to export. If set, only the post content of this post type
  35   *                              will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined
  36   *                              custom post. If an invalid custom post type is supplied, every post type for
  37   *                              which `can_export` is enabled will be exported instead. If a valid custom post
  38   *                              type is supplied but `can_export` is disabled, then 'posts' will be exported
  39   *                              instead. When 'all' is supplied, only post types with `can_export` enabled will
  40   *                              be exported. Default 'all'.
  41   *     @type string $author     Author to export content for. Only used when `$content` is 'post', 'page', or
  42   *                              'attachment'. Accepts false (all) or a specific author ID. Default false (all).
  43   *     @type string $category   Category (slug) to export content for. Used only when `$content` is 'post'. If
  44   *                              set, only post content assigned to `$category` will be exported. Accepts false
  45   *                              or a specific category slug. Default is false (all categories).
  46   *     @type string $start_date Start date to export content from. Expected date format is 'Y-m-d'. Used only
  47   *                              when `$content` is 'post', 'page' or 'attachment'. Default false (since the
  48   *                              beginning of time).
  49   *     @type string $end_date   End date to export content to. Expected date format is 'Y-m-d'. Used only when
  50   *                              `$content` is 'post', 'page' or 'attachment'. Default false (latest publish date).
  51   *     @type string $status     Post status to export posts for. Used only when `$content` is 'post' or 'page'.
  52   *                              Accepts false (all statuses except 'auto-draft'), or a specific status, i.e.
  53   *                              'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', or
  54   *                              'trash'. Default false (all statuses except 'auto-draft').
  55   * }
  56   */
  57  function export_wp( $args = array() ) {
  58      global $wpdb, $post;
  59  
  60      $defaults = array(
  61          'content'    => 'all',
  62          'author'     => false,
  63          'category'   => false,
  64          'start_date' => false,
  65          'end_date'   => false,
  66          'status'     => false,
  67      );
  68      $args     = wp_parse_args( $args, $defaults );
  69  
  70      /**
  71       * Fires at the beginning of an export, before any headers are sent.
  72       *
  73       * @since 2.3.0
  74       *
  75       * @param array $args An array of export arguments.
  76       */
  77      do_action( 'export_wp', $args );
  78  
  79      $sitename = sanitize_key( get_bloginfo( 'name' ) );
  80      if ( ! empty( $sitename ) ) {
  81          $sitename .= '.';
  82      }
  83      $date        = gmdate( 'Y-m-d' );
  84      $wp_filename = $sitename . 'WordPress.' . $date . '.xml';
  85      /**
  86       * Filters the export filename.
  87       *
  88       * @since 4.4.0
  89       *
  90       * @param string $wp_filename The name of the file for download.
  91       * @param string $sitename    The site name.
  92       * @param string $date        Today's date, formatted.
  93       */
  94      $filename = apply_filters( 'export_wp_filename', $wp_filename, $sitename, $date );
  95  
  96      header( 'Content-Description: File Transfer' );
  97      header( 'Content-Disposition: attachment; filename=' . $filename );
  98      header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
  99  
 100      if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) {
 101          $ptype = get_post_type_object( $args['content'] );
 102          if ( ! $ptype->can_export ) {
 103              $args['content'] = 'post';
 104          }
 105  
 106          $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
 107      } else {
 108          $post_types = get_post_types( array( 'can_export' => true ) );
 109          $esses      = array_fill( 0, count( $post_types ), '%s' );
 110  
 111          // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
 112          $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
 113      }
 114  
 115      if ( $args['status'] && ( 'post' === $args['content'] || 'page' === $args['content'] ) ) {
 116          $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
 117      } else {
 118          $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
 119      }
 120  
 121      $join = '';
 122      if ( $args['category'] && 'post' === $args['content'] ) {
 123          $term = term_exists( $args['category'], 'category' );
 124          if ( $term ) {
 125              $join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
 126              $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
 127          }
 128      }
 129  
 130      if ( in_array( $args['content'], array( 'post', 'page', 'attachment' ), true ) ) {
 131          if ( $args['author'] ) {
 132              $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
 133          }
 134  
 135          if ( $args['start_date'] ) {
 136              $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", gmdate( 'Y-m-d', strtotime( $args['start_date'] ) ) );
 137          }
 138  
 139          if ( $args['end_date'] ) {
 140              $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", gmdate( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
 141          }
 142      }
 143  
 144      // Grab a snapshot of post IDs, just in case it changes during the export.
 145      $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
 146  
 147      // Get IDs for the attachments of each post, unless all content is already being exported.
 148      if ( ! in_array( $args['content'], array( 'all', 'attachment' ), true ) ) {
 149          // Array to hold all additional IDs (attachments and thumbnails).
 150          $additional_ids = array();
 151  
 152          // Create a copy of the post IDs array to avoid modifying the original array.
 153          $processing_ids = $post_ids;
 154  
 155          while ( $next_posts = array_splice( $processing_ids, 0, 20 ) ) {
 156              $posts_in     = array_map( 'absint', $next_posts );
 157              $placeholders = array_fill( 0, count( $posts_in ), '%d' );
 158  
 159              // Create a string for the placeholders.
 160              $in_placeholder = implode( ',', $placeholders );
 161  
 162              // Prepare the SQL statement for attachment ids.
 163              $attachment_ids = $wpdb->get_col(
 164                  $wpdb->prepare(
 165                      "
 166                  SELECT ID
 167                  FROM $wpdb->posts
 168                  WHERE post_parent IN ($in_placeholder) AND post_type = 'attachment'
 169                      ",
 170                      $posts_in
 171                  )
 172              );
 173  
 174              $thumbnails_ids = $wpdb->get_col(
 175                  $wpdb->prepare(
 176                      "
 177                  SELECT meta_value
 178                  FROM $wpdb->postmeta
 179                  WHERE $wpdb->postmeta.post_id IN ($in_placeholder)
 180                  AND $wpdb->postmeta.meta_key = '_thumbnail_id'
 181                      ",
 182                      $posts_in
 183                  )
 184              );
 185  
 186              // Cast thumbnail IDs to integers to prevent second-order SQL injection via user-controlled meta values.
 187              $thumbnails_ids = array_filter( array_map( 'absint', $thumbnails_ids ) );
 188  
 189              $additional_ids = array_merge( $additional_ids, $attachment_ids, $thumbnails_ids );
 190          }
 191  
 192          // Merge the additional IDs back with the original post IDs after processing all posts.
 193          // Cast to integers as defense-in-depth, since $additional_ids may include values sourced from postmeta.
 194          $post_ids = array_unique( array_map( 'absint', array_merge( $post_ids, $additional_ids ) ) );
 195      }
 196  
 197      /*
 198       * Get the requested terms ready, empty unless posts filtered by category
 199       * or all content.
 200       */
 201      $cats  = array();
 202      $tags  = array();
 203      $terms = array();
 204      if ( isset( $term ) && $term ) {
 205          $cat  = get_term( $term['term_id'], 'category' );
 206          $cats = array( $cat->term_id => $cat );
 207          unset( $term, $cat );
 208      } elseif ( 'all' === $args['content'] ) {
 209          $categories = (array) get_categories( array( 'get' => 'all' ) );
 210          $tags       = (array) get_tags( array( 'get' => 'all' ) );
 211  
 212          $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
 213          $custom_terms      = (array) get_terms(
 214              array(
 215                  'taxonomy' => $custom_taxonomies,
 216                  'get'      => 'all',
 217              )
 218          );
 219  
 220          // Put categories in order with no child going before its parent.
 221          while ( $cat = array_shift( $categories ) ) {
 222              if ( ! $cat->parent || isset( $cats[ $cat->parent ] ) ) {
 223                  $cats[ $cat->term_id ] = $cat;
 224              } else {
 225                  $categories[] = $cat;
 226              }
 227          }
 228  
 229          // Put terms in order with no child going before its parent.
 230          while ( $t = array_shift( $custom_terms ) ) {
 231              if ( ! $t->parent || isset( $terms[ $t->parent ] ) ) {
 232                  $terms[ $t->term_id ] = $t;
 233              } else {
 234                  $custom_terms[] = $t;
 235              }
 236          }
 237  
 238          unset( $categories, $custom_taxonomies, $custom_terms );
 239      }
 240  
 241      /**
 242       * Wraps given string in XML CDATA tag.
 243       *
 244       * @since 2.1.0
 245       *
 246       * @param string|null $str String to wrap in XML CDATA tag. May be null.
 247       * @return string
 248       */
 249  	function wxr_cdata( $str ) {
 250          $str = (string) $str;
 251  
 252          if ( ! wp_is_valid_utf8( $str ) ) {
 253              $str = utf8_encode( $str );
 254          }
 255          // $str = ent2ncr(esc_html($str));
 256          $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
 257  
 258          return $str;
 259      }
 260  
 261      /**
 262       * Returns the URL of the site.
 263       *
 264       * @since 2.5.0
 265       *
 266       * @return string Site URL.
 267       */
 268  	function wxr_site_url() {
 269          if ( is_multisite() ) {
 270              // Multisite: the base URL.
 271              return network_home_url();
 272          } else {
 273              // WordPress (single site): the site URL.
 274              return get_bloginfo_rss( 'url' );
 275          }
 276      }
 277  
 278      /**
 279       * Outputs a cat_name XML tag from a given category object.
 280       *
 281       * @since 2.1.0
 282       *
 283       * @param WP_Term $category Category Object.
 284       */
 285  	function wxr_cat_name( $category ) {
 286          if ( empty( $category->name ) ) {
 287              return;
 288          }
 289  
 290          echo '<wp:cat_name>' . wxr_cdata( $category->name ) . "</wp:cat_name>\n";
 291      }
 292  
 293      /**
 294       * Outputs a category_description XML tag from a given category object.
 295       *
 296       * @since 2.1.0
 297       *
 298       * @param WP_Term $category Category Object.
 299       */
 300  	function wxr_category_description( $category ) {
 301          if ( empty( $category->description ) ) {
 302              return;
 303          }
 304  
 305          echo '<wp:category_description>' . wxr_cdata( $category->description ) . "</wp:category_description>\n";
 306      }
 307  
 308      /**
 309       * Outputs a tag_name XML tag from a given tag object.
 310       *
 311       * @since 2.3.0
 312       *
 313       * @param WP_Term $tag Tag Object.
 314       */
 315  	function wxr_tag_name( $tag ) {
 316          if ( empty( $tag->name ) ) {
 317              return;
 318          }
 319  
 320          echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . "</wp:tag_name>\n";
 321      }
 322  
 323      /**
 324       * Outputs a tag_description XML tag from a given tag object.
 325       *
 326       * @since 2.3.0
 327       *
 328       * @param WP_Term $tag Tag Object.
 329       */
 330  	function wxr_tag_description( $tag ) {
 331          if ( empty( $tag->description ) ) {
 332              return;
 333          }
 334  
 335          echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . "</wp:tag_description>\n";
 336      }
 337  
 338      /**
 339       * Outputs a term_name XML tag from a given term object.
 340       *
 341       * @since 2.9.0
 342       *
 343       * @param WP_Term $term Term Object.
 344       */
 345  	function wxr_term_name( $term ) {
 346          if ( empty( $term->name ) ) {
 347              return;
 348          }
 349  
 350          echo '<wp:term_name>' . wxr_cdata( $term->name ) . "</wp:term_name>\n";
 351      }
 352  
 353      /**
 354       * Outputs a term_description XML tag from a given term object.
 355       *
 356       * @since 2.9.0
 357       *
 358       * @param WP_Term $term Term Object.
 359       */
 360  	function wxr_term_description( $term ) {
 361          if ( empty( $term->description ) ) {
 362              return;
 363          }
 364  
 365          echo "\t\t<wp:term_description>" . wxr_cdata( $term->description ) . "</wp:term_description>\n";
 366      }
 367  
 368      /**
 369       * Outputs term meta XML tags for a given term object.
 370       *
 371       * @since 4.6.0
 372       *
 373       * @global wpdb $wpdb WordPress database abstraction object.
 374       *
 375       * @param WP_Term $term Term object.
 376       */
 377  	function wxr_term_meta( $term ) {
 378          global $wpdb;
 379  
 380          $termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) );
 381  
 382          foreach ( $termmeta as $meta ) {
 383              /**
 384               * Filters whether to selectively skip term meta used for WXR exports.
 385               *
 386               * Returning a truthy value from the filter will skip the current meta
 387               * object from being exported.
 388               *
 389               * @since 4.6.0
 390               *
 391               * @param bool   $skip     Whether to skip the current piece of term meta. Default false.
 392               * @param string $meta_key Current meta key.
 393               * @param object $meta     Current meta object.
 394               */
 395              if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) {
 396                  printf( "\t\t<wp:termmeta>\n\t\t\t<wp:meta_key>%s</wp:meta_key>\n\t\t\t<wp:meta_value>%s</wp:meta_value>\n\t\t</wp:termmeta>\n", wxr_cdata( $meta->meta_key ), wxr_cdata( $meta->meta_value ) );
 397              }
 398          }
 399      }
 400  
 401      /**
 402       * Outputs list of authors with posts.
 403       *
 404       * @since 3.1.0
 405       *
 406       * @global wpdb $wpdb WordPress database abstraction object.
 407       *
 408       * @param int[] $post_ids Optional. Array of post IDs to filter the query by.
 409       */
 410  	function wxr_authors_list( ?array $post_ids = null ) {
 411          global $wpdb;
 412  
 413          if ( ! empty( $post_ids ) ) {
 414              $post_ids       = array_map( 'absint', $post_ids );
 415              $post_id_chunks = array_chunk( $post_ids, 20 );
 416          } else {
 417              $post_id_chunks = array( array() );
 418          }
 419  
 420          $authors = array();
 421  
 422          foreach ( $post_id_chunks as $next_posts ) {
 423              $and = ! empty( $next_posts ) ? 'AND ID IN (' . implode( ', ', $next_posts ) . ')' : '';
 424  
 425              $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
 426  
 427              foreach ( (array) $results as $result ) {
 428                  $authors[] = get_userdata( $result->post_author );
 429              }
 430          }
 431  
 432          $authors = array_filter( $authors );
 433          $authors = array_unique( $authors, SORT_REGULAR ); // Remove duplicate authors.
 434  
 435          foreach ( $authors as $author ) {
 436              echo "\t<wp:author>";
 437              echo '<wp:author_id>' . (int) $author->ID . '</wp:author_id>';
 438              echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>';
 439              echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>';
 440              echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
 441              echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>';
 442              echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
 443              echo "</wp:author>\n";
 444          }
 445      }
 446  
 447      /**
 448       * Outputs all navigation menu terms.
 449       *
 450       * @since 3.1.0
 451       */
 452  	function wxr_nav_menu_terms() {
 453          $nav_menus = wp_get_nav_menus();
 454          if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) {
 455              return;
 456          }
 457  
 458          foreach ( $nav_menus as $menu ) {
 459              echo "\t<wp:term>";
 460              echo '<wp:term_id>' . (int) $menu->term_id . '</wp:term_id>';
 461              echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
 462              echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
 463              wxr_term_name( $menu );
 464              echo "</wp:term>\n";
 465          }
 466      }
 467  
 468      /**
 469       * Outputs list of taxonomy terms, in XML tag format, associated with a post.
 470       *
 471       * @since 2.3.0
 472       */
 473  	function wxr_post_taxonomy() {
 474          $post = get_post();
 475  
 476          $taxonomies = get_object_taxonomies( $post->post_type );
 477          if ( empty( $taxonomies ) ) {
 478              return;
 479          }
 480          $terms = wp_get_object_terms( $post->ID, $taxonomies );
 481  
 482          foreach ( (array) $terms as $term ) {
 483              echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n";
 484          }
 485      }
 486  
 487      /**
 488       * Determines whether to selectively skip post meta used for WXR exports.
 489       *
 490       * @since 3.3.0
 491       *
 492       * @param bool   $return_me Whether to skip the current post meta. Default false.
 493       * @param string $meta_key  Meta key.
 494       * @return bool
 495       */
 496  	function wxr_filter_postmeta( $return_me, $meta_key ) {
 497          if ( '_edit_lock' === $meta_key ) {
 498              $return_me = true;
 499          }
 500          return $return_me;
 501      }
 502      add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
 503  
 504      echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . "\" ?>\n";
 505  
 506      ?>
 507  <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
 508  <!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
 509  <!-- You may use this file to transfer that content from one site to another. -->
 510  <!-- This file is not intended to serve as a complete backup of your site. -->
 511  
 512  <!-- To import this information into a WordPress site follow these steps: -->
 513  <!-- 1. Log in to that site as an administrator. -->
 514  <!-- 2. Go to Tools: Import in the WordPress admin panel. -->
 515  <!-- 3. Install the "WordPress" importer from the list. -->
 516  <!-- 4. Activate & Run Importer. -->
 517  <!-- 5. Upload this file using the form provided on that page. -->
 518  <!-- 6. You will first be asked to map the authors in this export file to users -->
 519  <!--    on the site. For each author, you may choose to map to an -->
 520  <!--    existing user on the site or to create a new user. -->
 521  <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
 522  <!--    contained in this file into your site. -->
 523  
 524      <?php the_generator( 'export' ); ?>
 525  <rss version="2.0"
 526      xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
 527      xmlns:content="http://purl.org/rss/1.0/modules/content/"
 528      xmlns:wfw="http://wellformedweb.org/CommentAPI/"
 529      xmlns:dc="http://purl.org/dc/elements/1.1/"
 530      xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
 531  >
 532  
 533  <channel>
 534      <title><?php bloginfo_rss( 'name' ); ?></title>
 535      <link><?php bloginfo_rss( 'url' ); ?></link>
 536      <description><?php bloginfo_rss( 'description' ); ?></description>
 537      <pubDate><?php echo gmdate( 'D, d M Y H:i:s +0000' ); ?></pubDate>
 538      <language><?php bloginfo_rss( 'language' ); ?></language>
 539      <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
 540      <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
 541      <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
 542  
 543      <?php wxr_authors_list( $post_ids ); ?>
 544  
 545      <?php foreach ( $cats as $c ) : ?>
 546      <wp:category>
 547          <wp:term_id><?php echo (int) $c->term_id; ?></wp:term_id>
 548          <wp:category_nicename><?php echo wxr_cdata( $c->slug ); ?></wp:category_nicename>
 549          <wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ); ?></wp:category_parent>
 550          <?php
 551          wxr_cat_name( $c );
 552          wxr_category_description( $c );
 553          wxr_term_meta( $c );
 554          ?>
 555      </wp:category>
 556      <?php endforeach; ?>
 557      <?php foreach ( $tags as $t ) : ?>
 558      <wp:tag>
 559          <wp:term_id><?php echo (int) $t->term_id; ?></wp:term_id>
 560          <wp:tag_slug><?php echo wxr_cdata( $t->slug ); ?></wp:tag_slug>
 561          <?php
 562          wxr_tag_name( $t );
 563          wxr_tag_description( $t );
 564          wxr_term_meta( $t );
 565          ?>
 566      </wp:tag>
 567      <?php endforeach; ?>
 568      <?php foreach ( $terms as $t ) : ?>
 569      <wp:term>
 570          <wp:term_id><?php echo (int) $t->term_id; ?></wp:term_id>
 571          <wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
 572          <wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
 573          <wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
 574          <?php
 575          wxr_term_name( $t );
 576          wxr_term_description( $t );
 577          wxr_term_meta( $t );
 578          ?>
 579      </wp:term>
 580      <?php endforeach; ?>
 581      <?php
 582      if ( 'all' === $args['content'] ) {
 583          wxr_nav_menu_terms();
 584      }
 585      ?>
 586  
 587      <?php
 588      /** This action is documented in wp-includes/feed-rss2.php */
 589      do_action( 'rss2_head' );
 590      ?>
 591  
 592      <?php
 593      if ( $post_ids ) {
 594          /**
 595           * @global WP_Query $wp_query WordPress Query object.
 596           */
 597          global $wp_query;
 598  
 599          // Fake being in the loop.
 600          $wp_query->in_the_loop = true;
 601  
 602          // Fetch 20 posts at a time rather than loading the entire table into memory.
 603          while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
 604              // Re-sanitize immediately before use, as defense-in-depth against the IDs being interpolated directly into SQL below.
 605              $next_posts = array_map( 'absint', $next_posts );
 606              $where      = 'WHERE ID IN (' . implode( ',', $next_posts ) . ')';
 607              $posts      = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
 608  
 609              // Begin Loop.
 610              foreach ( $posts as $post ) {
 611                  setup_postdata( $post );
 612  
 613                  /**
 614                   * Filters the post title used for WXR exports.
 615                   *
 616                   * @since 5.7.0
 617                   *
 618                   * @param string $post_title Title of the current post.
 619                   */
 620                  $title = wxr_cdata( apply_filters( 'the_title_export', $post->post_title ) );
 621  
 622                  /**
 623                   * Filters the post content used for WXR exports.
 624                   *
 625                   * @since 2.5.0
 626                   *
 627                   * @param string $post_content Content of the current post.
 628                   */
 629                  $content = wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) );
 630  
 631                  /**
 632                   * Filters the post excerpt used for WXR exports.
 633                   *
 634                   * @since 2.6.0
 635                   *
 636                   * @param string $post_excerpt Excerpt for the current post.
 637                   */
 638                  $excerpt = wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) );
 639  
 640                  $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
 641                  ?>
 642      <item>
 643          <title><?php echo $title; ?></title>
 644          <link><?php the_permalink_rss(); ?></link>
 645          <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
 646          <dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
 647          <guid isPermaLink="false"><?php the_guid(); ?></guid>
 648          <description></description>
 649          <content:encoded><?php echo $content; ?></content:encoded>
 650          <excerpt:encoded><?php echo $excerpt; ?></excerpt:encoded>
 651          <wp:post_id><?php echo (int) $post->ID; ?></wp:post_id>
 652          <wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
 653          <wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
 654          <wp:post_modified><?php echo wxr_cdata( $post->post_modified ); ?></wp:post_modified>
 655          <wp:post_modified_gmt><?php echo wxr_cdata( $post->post_modified_gmt ); ?></wp:post_modified_gmt>
 656          <wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
 657          <wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
 658          <wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
 659          <wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
 660          <wp:post_parent><?php echo (int) $post->post_parent; ?></wp:post_parent>
 661          <wp:menu_order><?php echo (int) $post->menu_order; ?></wp:menu_order>
 662          <wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
 663          <wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
 664          <wp:is_sticky><?php echo (int) $is_sticky; ?></wp:is_sticky>
 665                  <?php    if ( 'attachment' === $post->post_type ) : ?>
 666          <wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
 667      <?php endif; ?>
 668                  <?php wxr_post_taxonomy(); ?>
 669                  <?php
 670                  $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
 671                  foreach ( $postmeta as $meta ) :
 672                      /**
 673                       * Filters whether to selectively skip post meta used for WXR exports.
 674                       *
 675                       * Returning a truthy value from the filter will skip the current meta
 676                       * object from being exported.
 677                       *
 678                       * @since 3.3.0
 679                       *
 680                       * @param bool   $skip     Whether to skip the current post meta. Default false.
 681                       * @param string $meta_key Current meta key.
 682                       * @param object $meta     Current meta object.
 683                       */
 684                      if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
 685                          continue;
 686                      }
 687                      ?>
 688          <wp:postmeta>
 689          <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
 690          <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
 691          </wp:postmeta>
 692                      <?php
 693      endforeach;
 694  
 695                  $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
 696                  $comments  = array_filter(
 697                      array_map( 'get_comment', $_comments ),
 698                      static function ( $comment ) {
 699                          return $comment instanceof WP_Comment;
 700                      }
 701                  );
 702                  foreach ( $comments as $c ) :
 703                      ?>
 704          <wp:comment>
 705              <wp:comment_id><?php echo (int) $c->comment_ID; ?></wp:comment_id>
 706              <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
 707              <wp:comment_author_email><?php echo wxr_cdata( $c->comment_author_email ); ?></wp:comment_author_email>
 708              <wp:comment_author_url><?php echo sanitize_url( $c->comment_author_url ); ?></wp:comment_author_url>
 709              <wp:comment_author_IP><?php echo wxr_cdata( $c->comment_author_IP ); ?></wp:comment_author_IP>
 710              <wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
 711              <wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
 712              <wp:comment_content><?php echo wxr_cdata( $c->comment_content ); ?></wp:comment_content>
 713              <wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
 714              <wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
 715              <wp:comment_parent><?php echo (int) $c->comment_parent; ?></wp:comment_parent>
 716              <wp:comment_user_id><?php echo (int) $c->user_id; ?></wp:comment_user_id>
 717                      <?php
 718                      $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
 719                      foreach ( $c_meta as $meta ) :
 720                          /**
 721                           * Filters whether to selectively skip comment meta used for WXR exports.
 722                           *
 723                           * Returning a truthy value from the filter will skip the current meta
 724                           * object from being exported.
 725                           *
 726                           * @since 4.0.0
 727                           *
 728                           * @param bool   $skip     Whether to skip the current comment meta. Default false.
 729                           * @param string $meta_key Current meta key.
 730                           * @param object $meta     Current meta object.
 731                           */
 732                          if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
 733                              continue;
 734                          }
 735                          ?>
 736      <wp:commentmeta>
 737      <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
 738              <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
 739              </wp:commentmeta>
 740                      <?php    endforeach; ?>
 741          </wp:comment>
 742              <?php    endforeach; ?>
 743          </item>
 744                  <?php
 745              }
 746          }
 747      }
 748      ?>
 749  </channel>
 750  </rss>
 751      <?php
 752  }


Generated : Mon Oct 12 08:20:33 2026 Cross-referenced by PHPXref