export.php 23 KB

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