export.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * Modified version of the WordPress Export Administration API
  4. * that lets us export single posts and selected groups of posts.
  5. *
  6. * @package WordPress
  7. * @subpackage Administration
  8. */
  9. /**
  10. * Generates the WXR export file for download.
  11. *
  12. * @since 2.1.0
  13. *
  14. * @global wpdb $wpdb
  15. * @global WP_Post $post
  16. *
  17. * @param array $post_ids
  18. */
  19. function fl_export_wp( $post_ids = array() ) {
  20. global $wpdb, $post;
  21. if ( empty( $post_ids ) ) {
  22. return;
  23. }
  24. $sitename = sanitize_key( get_bloginfo( 'name' ) );
  25. if ( ! empty( $sitename ) ) {
  26. $sitename .= '.';
  27. }
  28. $date = date( 'Y-m-d' );
  29. $wp_filename = $sitename . 'wordpress.' . $date . '.xml';
  30. /**
  31. * Filter the export filename.
  32. *
  33. * @since 4.4.0
  34. *
  35. * @param string $wp_filename The name of the file for download.
  36. * @param string $sitename The site name.
  37. * @param string $date Today's date, formatted.
  38. */
  39. $filename = apply_filters( 'export_wp_filename', $wp_filename, $sitename, $date );
  40. header( 'Content-Description: File Transfer' );
  41. header( 'Content-Disposition: attachment; filename=' . $filename );
  42. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
  43. /**
  44. * Wrap given string in XML CDATA tag.
  45. *
  46. * @since 2.1.0
  47. *
  48. * @param string $str String to wrap in XML CDATA tag.
  49. * @return string
  50. */
  51. function wxr_cdata( $str ) {
  52. if ( ! seems_utf8( $str ) ) {
  53. $str = utf8_encode( $str );
  54. }
  55. // $str = ent2ncr(esc_html($str));
  56. $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
  57. return $str;
  58. }
  59. /**
  60. * Return the URL of the site
  61. *
  62. * @since 2.5.0
  63. *
  64. * @return string Site URL.
  65. */
  66. function wxr_site_url() {
  67. // Multisite: the base URL.
  68. if ( is_multisite() ) {
  69. return network_home_url();
  70. } else { return get_bloginfo_rss( 'url' );
  71. }
  72. }
  73. /**
  74. * Output a cat_name XML tag from a given category object
  75. *
  76. * @since 2.1.0
  77. *
  78. * @param object $category Category Object
  79. */
  80. function wxr_cat_name( $category ) {
  81. if ( empty( $category->name ) ) {
  82. return;
  83. }
  84. echo '<wp:cat_name>' . wxr_cdata( $category->name ) . '</wp:cat_name>';
  85. }
  86. /**
  87. * Output a category_description XML tag from a given category object
  88. *
  89. * @since 2.1.0
  90. *
  91. * @param object $category Category Object
  92. */
  93. function wxr_category_description( $category ) {
  94. if ( empty( $category->description ) ) {
  95. return;
  96. }
  97. echo '<wp:category_description>' . wxr_cdata( $category->description ) . '</wp:category_description>';
  98. }
  99. /**
  100. * Output a tag_name XML tag from a given tag object
  101. *
  102. * @since 2.3.0
  103. *
  104. * @param object $tag Tag Object
  105. */
  106. function wxr_tag_name( $tag ) {
  107. if ( empty( $tag->name ) ) {
  108. return;
  109. }
  110. echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . '</wp:tag_name>';
  111. }
  112. /**
  113. * Output a tag_description XML tag from a given tag object
  114. *
  115. * @since 2.3.0
  116. *
  117. * @param object $tag Tag Object
  118. */
  119. function wxr_tag_description( $tag ) {
  120. if ( empty( $tag->description ) ) {
  121. return;
  122. }
  123. echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . '</wp:tag_description>';
  124. }
  125. /**
  126. * Output a term_name XML tag from a given term object
  127. *
  128. * @since 2.9.0
  129. *
  130. * @param object $term Term Object
  131. */
  132. function wxr_term_name( $term ) {
  133. if ( empty( $term->name ) ) {
  134. return;
  135. }
  136. echo '<wp:term_name>' . wxr_cdata( $term->name ) . '</wp:term_name>';
  137. }
  138. /**
  139. * Output a term_description XML tag from a given term object
  140. *
  141. * @since 2.9.0
  142. *
  143. * @param object $term Term Object
  144. */
  145. function wxr_term_description( $term ) {
  146. if ( empty( $term->description ) ) {
  147. return;
  148. }
  149. echo '<wp:term_description>' . wxr_cdata( $term->description ) . '</wp:term_description>';
  150. }
  151. /**
  152. * Output list of authors with posts
  153. *
  154. * @since 3.1.0
  155. *
  156. * @global wpdb $wpdb WordPress database abstraction object.
  157. *
  158. * @param array $post_ids Array of post IDs to filter the query by. Optional.
  159. */
  160. function wxr_authors_list( array $post_ids = null ) {
  161. global $wpdb;
  162. if ( ! empty( $post_ids ) ) {
  163. $post_ids = array_map( 'absint', $post_ids );
  164. $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
  165. } else {
  166. $and = '';
  167. }
  168. $authors = array();
  169. // @codingStandardsIgnoreStart
  170. $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
  171. // @codingStandardsIgnoreEnd
  172. foreach ( (array) $results as $result ) {
  173. $authors[] = get_userdata( $result->post_author );
  174. }
  175. $authors = array_filter( $authors );
  176. foreach ( $authors as $author ) {
  177. echo "\t<wp:author>";
  178. echo '<wp:author_id>' . intval( $author->ID ) . '</wp:author_id>';
  179. echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>';
  180. echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>';
  181. echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
  182. echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>';
  183. echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
  184. echo "</wp:author>\n";
  185. }
  186. }
  187. /**
  188. * Ouput all navigation menu terms
  189. *
  190. * @since 3.1.0
  191. */
  192. function wxr_nav_menu_terms() {
  193. $nav_menus = wp_get_nav_menus();
  194. if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) {
  195. return;
  196. }
  197. foreach ( $nav_menus as $menu ) {
  198. echo "\t<wp:term>";
  199. echo '<wp:term_id>' . intval( $menu->term_id ) . '</wp:term_id>';
  200. echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
  201. echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
  202. wxr_term_name( $menu );
  203. echo "</wp:term>\n";
  204. }
  205. }
  206. /**
  207. * Output list of taxonomy terms, in XML tag format, associated with a post
  208. *
  209. * @since 2.3.0
  210. */
  211. function wxr_post_taxonomy() {
  212. $post = get_post();
  213. $taxonomies = get_object_taxonomies( $post->post_type );
  214. if ( empty( $taxonomies ) ) {
  215. return;
  216. }
  217. $terms = wp_get_object_terms( $post->ID, $taxonomies );
  218. foreach ( (array) $terms as $term ) {
  219. echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n";
  220. }
  221. }
  222. /**
  223. *
  224. * @param bool $return_me
  225. * @param string $meta_key
  226. * @return bool
  227. */
  228. function wxr_filter_postmeta( $return_me, $meta_key ) {
  229. if ( '_edit_lock' == $meta_key ) {
  230. $return_me = true;
  231. }
  232. return $return_me;
  233. }
  234. add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
  235. echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . "\" ?>\n";
  236. ?>
  237. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
  238. <!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
  239. <!-- You may use this file to transfer that content from one site to another. -->
  240. <!-- This file is not intended to serve as a complete backup of your site. -->
  241. <!-- To import this information into a WordPress site follow these steps: -->
  242. <!-- 1. Log in to that site as an administrator. -->
  243. <!-- 2. Go to Tools: Import in the WordPress admin panel. -->
  244. <!-- 3. Install the "WordPress" importer from the list. -->
  245. <!-- 4. Activate & Run Importer. -->
  246. <!-- 5. Upload this file using the form provided on that page. -->
  247. <!-- 6. You will first be asked to map the authors in this export file to users -->
  248. <!-- on the site. For each author, you may choose to map to an -->
  249. <!-- existing user on the site or to create a new user. -->
  250. <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
  251. <!-- contained in this file into your site. -->
  252. <?php the_generator( 'export' ); ?>
  253. <rss version="2.0"
  254. xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
  255. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  256. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  257. xmlns:dc="http://purl.org/dc/elements/1.1/"
  258. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
  259. >
  260. <channel>
  261. <title><?php bloginfo_rss( 'name' ); ?></title>
  262. <link><?php bloginfo_rss( 'url' ); ?></link>
  263. <description><?php bloginfo_rss( 'description' ); ?></description>
  264. <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
  265. <language><?php bloginfo_rss( 'language' ); ?></language>
  266. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
  267. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
  268. <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
  269. <?php wxr_authors_list( $post_ids ); ?>
  270. <?php
  271. /** This action is documented in wp-includes/feed-rss2.php */
  272. do_action( 'rss2_head' );
  273. ?>
  274. <?php if ( $post_ids ) {
  275. /**
  276. * @global WP_Query $wp_query
  277. */
  278. global $wp_query;
  279. // Fake being in the loop.
  280. $wp_query->in_the_loop = true;
  281. // Fetch 20 posts at a time rather than loading the entire table into memory.
  282. while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
  283. $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
  284. // @codingStandardsIgnoreStart
  285. $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
  286. // @codingStandardsIgnoreEnd
  287. // Begin Loop.
  288. foreach ( $posts as $post ) {
  289. setup_postdata( $post );
  290. $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
  291. ?>
  292. <item>
  293. <title><?php
  294. /** This filter is documented in wp-includes/feed.php */
  295. echo apply_filters( 'the_title_rss', $post->post_title );
  296. ?></title>
  297. <link><?php the_permalink_rss() ?></link>
  298. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
  299. <dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
  300. <guid isPermaLink="false"><?php the_guid(); ?></guid>
  301. <description></description>
  302. <content:encoded><?php
  303. /**
  304. * Filter the post content used for WXR exports.
  305. *
  306. * @since 2.5.0
  307. *
  308. * @param string $post_content Content of the current post.
  309. */
  310. echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) );
  311. ?></content:encoded>
  312. <excerpt:encoded><?php
  313. /**
  314. * Filter the post excerpt used for WXR exports.
  315. *
  316. * @since 2.6.0
  317. *
  318. * @param string $post_excerpt Excerpt for the current post.
  319. */
  320. echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) );
  321. ?></excerpt:encoded>
  322. <wp:post_id><?php echo intval( $post->ID ); ?></wp:post_id>
  323. <wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
  324. <wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
  325. <wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
  326. <wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
  327. <wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
  328. <wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
  329. <wp:post_parent><?php echo intval( $post->post_parent ); ?></wp:post_parent>
  330. <wp:menu_order><?php echo intval( $post->menu_order ); ?></wp:menu_order>
  331. <wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
  332. <wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
  333. <wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
  334. <?php if ( 'attachment' == $post->post_type ) : ?>
  335. <wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
  336. <?php endif; ?>
  337. <?php wxr_post_taxonomy(); ?>
  338. <?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
  339. foreach ( $postmeta as $meta ) :
  340. /**
  341. * Filter whether to selectively skip post meta used for WXR exports.
  342. *
  343. * Returning a truthy value to the filter will skip the current meta
  344. * object from being exported.
  345. *
  346. * @since 3.3.0
  347. *
  348. * @param bool $skip Whether to skip the current post meta. Default false.
  349. * @param string $meta_key Current meta key.
  350. * @param object $meta Current meta object.
  351. */
  352. if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
  353. continue;
  354. }
  355. ?>
  356. <wp:postmeta>
  357. <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
  358. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  359. </wp:postmeta>
  360. <?php endforeach;
  361. $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
  362. $comments = array_map( 'get_comment', $_comments );
  363. foreach ( $comments as $c ) : ?>
  364. <wp:comment>
  365. <wp:comment_id><?php echo intval( $c->comment_ID ); ?></wp:comment_id>
  366. <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
  367. <wp:comment_author_email><?php echo wxr_cdata( $c->comment_author_email ); ?></wp:comment_author_email>
  368. <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
  369. <wp:comment_author_IP><?php echo wxr_cdata( $c->comment_author_IP ); ?></wp:comment_author_IP>
  370. <wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
  371. <wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
  372. <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
  373. <wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
  374. <wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
  375. <wp:comment_parent><?php echo intval( $c->comment_parent ); ?></wp:comment_parent>
  376. <wp:comment_user_id><?php echo intval( $c->user_id ); ?></wp:comment_user_id>
  377. <?php $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
  378. foreach ( $c_meta as $meta ) :
  379. /**
  380. * Filter whether to selectively skip comment meta used for WXR exports.
  381. *
  382. * Returning a truthy value to the filter will skip the current meta
  383. * object from being exported.
  384. *
  385. * @since 4.0.0
  386. *
  387. * @param bool $skip Whether to skip the current comment meta. Default false.
  388. * @param string $meta_key Current meta key.
  389. * @param object $meta Current meta object.
  390. */
  391. if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
  392. continue;
  393. }
  394. ?>
  395. <wp:commentmeta>
  396. <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
  397. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  398. </wp:commentmeta>
  399. <?php endforeach; ?>
  400. </wp:comment>
  401. <?php endforeach; ?>
  402. </item>
  403. <?php
  404. }
  405. }
  406. }
  407. ?>
  408. </channel>
  409. </rss>
  410. <?php
  411. }