export.php 24 KB

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