PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/export.php

https://bitbucket.org/aqge/deptandashboard
PHP | 435 lines | 291 code | 46 blank | 98 comment | 51 complexity | f3319633f2a028a266d2329fb1ab4999 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  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.1' );
  16. /**
  17. * Generates the WXR export file for download
  18. *
  19. * @since 2.1.0
  20. *
  21. * @param array $args Filters defining what should be included in the export
  22. */
  23. function export_wp( $args = array() ) {
  24. global $wpdb, $post;
  25. $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
  26. 'start_date' => false, 'end_date' => false, 'status' => false,
  27. );
  28. $args = wp_parse_args( $args, $defaults );
  29. do_action( 'export_wp' );
  30. $sitename = sanitize_key( get_bloginfo( 'name' ) );
  31. if ( ! empty($sitename) ) $sitename .= '.';
  32. $filename = $sitename . 'wordpress.' . date( 'Y-m-d' ) . '.xml';
  33. header( 'Content-Description: File Transfer' );
  34. header( 'Content-Disposition: attachment; filename=' . $filename );
  35. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
  36. if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
  37. $ptype = get_post_type_object( $args['content'] );
  38. if ( ! $ptype->can_export )
  39. $args['content'] = 'post';
  40. $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
  41. } else {
  42. $post_types = get_post_types( array( 'can_export' => true ) );
  43. $esses = array_fill( 0, count($post_types), '%s' );
  44. $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
  45. }
  46. if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
  47. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
  48. else
  49. $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
  50. $join = '';
  51. if ( $args['category'] && 'post' == $args['content'] ) {
  52. if ( $term = term_exists( $args['category'], 'category' ) ) {
  53. $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
  54. $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
  55. }
  56. }
  57. if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
  58. if ( $args['author'] )
  59. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
  60. if ( $args['start_date'] )
  61. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
  62. if ( $args['end_date'] )
  63. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
  64. }
  65. // grab a snapshot of post IDs, just in case it changes during the export
  66. $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
  67. // get the requested terms ready, empty unless posts filtered by category or all content
  68. $cats = $tags = $terms = array();
  69. if ( isset( $term ) && $term ) {
  70. $cat = get_term( $term['term_id'], 'category' );
  71. $cats = array( $cat->term_id => $cat );
  72. unset( $term, $cat );
  73. } else if ( 'all' == $args['content'] ) {
  74. $categories = (array) get_categories( array( 'get' => 'all' ) );
  75. $tags = (array) get_tags( array( 'get' => 'all' ) );
  76. $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
  77. $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
  78. // put categories in order with no child going before its parent
  79. while ( $cat = array_shift( $categories ) ) {
  80. if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
  81. $cats[$cat->term_id] = $cat;
  82. else
  83. $categories[] = $cat;
  84. }
  85. // put terms in order with no child going before its parent
  86. while ( $t = array_shift( $custom_terms ) ) {
  87. if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
  88. $terms[$t->term_id] = $t;
  89. else
  90. $custom_terms[] = $t;
  91. }
  92. unset( $categories, $custom_taxonomies, $custom_terms );
  93. }
  94. /**
  95. * Wrap given string in XML CDATA tag.
  96. *
  97. * @since 2.1.0
  98. *
  99. * @param string $str String to wrap in XML CDATA tag.
  100. */
  101. function wxr_cdata( $str ) {
  102. if ( seems_utf8( $str ) == false )
  103. $str = utf8_encode( $str );
  104. // $str = ent2ncr(esc_html($str));
  105. $str = "<![CDATA[$str" . ( ( substr( $str, -1 ) == ']' ) ? ' ' : '' ) . ']]>';
  106. return $str;
  107. }
  108. /**
  109. * Return the URL of the site
  110. *
  111. * @since 2.5.0
  112. *
  113. * @return string Site URL.
  114. */
  115. function wxr_site_url() {
  116. // ms: the base url
  117. if ( is_multisite() )
  118. return network_home_url();
  119. // wp: the blog url
  120. else
  121. return get_bloginfo_rss( 'url' );
  122. }
  123. /**
  124. * Output a cat_name XML tag from a given category object
  125. *
  126. * @since 2.1.0
  127. *
  128. * @param object $category Category Object
  129. */
  130. function wxr_cat_name( $category ) {
  131. if ( empty( $category->name ) )
  132. return;
  133. echo '<wp:cat_name>' . wxr_cdata( $category->name ) . '</wp:cat_name>';
  134. }
  135. /**
  136. * Output a category_description XML tag from a given category object
  137. *
  138. * @since 2.1.0
  139. *
  140. * @param object $category Category Object
  141. */
  142. function wxr_category_description( $category ) {
  143. if ( empty( $category->description ) )
  144. return;
  145. echo '<wp:category_description>' . wxr_cdata( $category->description ) . '</wp:category_description>';
  146. }
  147. /**
  148. * Output a tag_name XML tag from a given tag object
  149. *
  150. * @since 2.3.0
  151. *
  152. * @param object $tag Tag Object
  153. */
  154. function wxr_tag_name( $tag ) {
  155. if ( empty( $tag->name ) )
  156. return;
  157. echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . '</wp:tag_name>';
  158. }
  159. /**
  160. * Output a tag_description XML tag from a given tag object
  161. *
  162. * @since 2.3.0
  163. *
  164. * @param object $tag Tag Object
  165. */
  166. function wxr_tag_description( $tag ) {
  167. if ( empty( $tag->description ) )
  168. return;
  169. echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . '</wp:tag_description>';
  170. }
  171. /**
  172. * Output a term_name XML tag from a given term object
  173. *
  174. * @since 2.9.0
  175. *
  176. * @param object $term Term Object
  177. */
  178. function wxr_term_name( $term ) {
  179. if ( empty( $term->name ) )
  180. return;
  181. echo '<wp:term_name>' . wxr_cdata( $term->name ) . '</wp:term_name>';
  182. }
  183. /**
  184. * Output a term_description XML tag from a given term object
  185. *
  186. * @since 2.9.0
  187. *
  188. * @param object $term Term Object
  189. */
  190. function wxr_term_description( $term ) {
  191. if ( empty( $term->description ) )
  192. return;
  193. echo '<wp:term_description>' . wxr_cdata( $term->description ) . '</wp:term_description>';
  194. }
  195. /**
  196. * Output list of authors with posts
  197. *
  198. * @since 3.1.0
  199. */
  200. function wxr_authors_list() {
  201. global $wpdb;
  202. $authors = array();
  203. $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts" );
  204. foreach ( (array) $results as $result )
  205. $authors[] = get_userdata( $result->post_author );
  206. $authors = array_filter( $authors );
  207. foreach ( $authors as $author ) {
  208. echo "\t<wp:author>";
  209. echo '<wp:author_id>' . $author->ID . '</wp:author_id>';
  210. echo '<wp:author_login>' . $author->user_login . '</wp:author_login>';
  211. echo '<wp:author_email>' . $author->user_email . '</wp:author_email>';
  212. echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
  213. echo '<wp:author_first_name>' . wxr_cdata( $author->user_firstname ) . '</wp:author_first_name>';
  214. echo '<wp:author_last_name>' . wxr_cdata( $author->user_lastname ) . '</wp:author_last_name>';
  215. echo "</wp:author>\n";
  216. }
  217. }
  218. /**
  219. * Ouput all navigation menu terms
  220. *
  221. * @since 3.1.0
  222. */
  223. function wxr_nav_menu_terms() {
  224. $nav_menus = wp_get_nav_menus();
  225. if ( empty( $nav_menus ) || ! is_array( $nav_menus ) )
  226. return;
  227. foreach ( $nav_menus as $menu ) {
  228. echo "\t<wp:term><wp:term_id>{$menu->term_id}</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug>{$menu->slug}</wp:term_slug>";
  229. wxr_term_name( $menu );
  230. echo "</wp:term>\n";
  231. }
  232. }
  233. /**
  234. * Output list of taxonomy terms, in XML tag format, associated with a post
  235. *
  236. * @since 2.3.0
  237. */
  238. function wxr_post_taxonomy() {
  239. global $post;
  240. $taxonomies = get_object_taxonomies( $post->post_type );
  241. if ( empty( $taxonomies ) )
  242. return;
  243. $terms = wp_get_object_terms( $post->ID, $taxonomies );
  244. foreach ( (array) $terms as $term ) {
  245. echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n";
  246. }
  247. }
  248. function wxr_filter_postmeta( $return_me, $meta_key ) {
  249. if ( '_edit_lock' == $meta_key )
  250. $return_me = true;
  251. return $return_me;
  252. }
  253. add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
  254. echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
  255. ?>
  256. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
  257. <!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
  258. <!-- You may use this file to transfer that content from one site to another. -->
  259. <!-- This file is not intended to serve as a complete backup of your site. -->
  260. <!-- To import this information into a WordPress site follow these steps: -->
  261. <!-- 1. Log in to that site as an administrator. -->
  262. <!-- 2. Go to Tools: Import in the WordPress admin panel. -->
  263. <!-- 3. Install the "WordPress" importer from the list. -->
  264. <!-- 4. Activate & Run Importer. -->
  265. <!-- 5. Upload this file using the form provided on that page. -->
  266. <!-- 6. You will first be asked to map the authors in this export file to users -->
  267. <!-- on the site. For each author, you may choose to map to an -->
  268. <!-- existing user on the site or to create a new user. -->
  269. <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
  270. <!-- contained in this file into your site. -->
  271. <?php the_generator( 'export' ); ?>
  272. <rss version="2.0"
  273. xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
  274. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  275. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  276. xmlns:dc="http://purl.org/dc/elements/1.1/"
  277. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
  278. >
  279. <channel>
  280. <title><?php bloginfo_rss( 'name' ); ?></title>
  281. <link><?php bloginfo_rss( 'url' ); ?></link>
  282. <description><?php bloginfo_rss( 'description' ); ?></description>
  283. <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
  284. <language><?php echo get_option( 'rss_language' ); ?></language>
  285. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
  286. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
  287. <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
  288. <?php wxr_authors_list(); ?>
  289. <?php foreach ( $cats as $c ) : ?>
  290. <wp:category><wp:term_id><?php echo $c->term_id ?></wp:term_id><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->slug : ''; ?></wp:category_parent><?php wxr_cat_name( $c ); ?><?php wxr_category_description( $c ); ?></wp:category>
  291. <?php endforeach; ?>
  292. <?php foreach ( $tags as $t ) : ?>
  293. <wp:tag><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name( $t ); ?><?php wxr_tag_description( $t ); ?></wp:tag>
  294. <?php endforeach; ?>
  295. <?php foreach ( $terms as $t ) : ?>
  296. <wp:term><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $terms[$t->parent]->slug : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
  297. <?php endforeach; ?>
  298. <?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
  299. <?php do_action( 'rss2_head' ); ?>
  300. <?php if ( $post_ids ) {
  301. global $wp_query;
  302. $wp_query->in_the_loop = true; // Fake being in the loop.
  303. // fetch 20 posts at a time rather than loading the entire table into memory
  304. while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
  305. $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
  306. $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
  307. // Begin Loop
  308. foreach ( $posts as $post ) {
  309. setup_postdata( $post );
  310. $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
  311. ?>
  312. <item>
  313. <title><?php echo apply_filters( 'the_title_rss', $post->post_title ); ?></title>
  314. <link><?php the_permalink_rss() ?></link>
  315. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
  316. <dc:creator><?php echo get_the_author_meta( 'login' ); ?></dc:creator>
  317. <guid isPermaLink="false"><?php esc_url( the_guid() ); ?></guid>
  318. <description></description>
  319. <content:encoded><?php echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) ); ?></content:encoded>
  320. <excerpt:encoded><?php echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) ); ?></excerpt:encoded>
  321. <wp:post_id><?php echo $post->ID; ?></wp:post_id>
  322. <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
  323. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
  324. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
  325. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
  326. <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
  327. <wp:status><?php echo $post->post_status; ?></wp:status>
  328. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
  329. <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
  330. <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
  331. <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
  332. <wp:is_sticky><?php echo $is_sticky; ?></wp:is_sticky>
  333. <?php if ( $post->post_type == 'attachment' ) : ?>
  334. <wp:attachment_url><?php echo wp_get_attachment_url( $post->ID ); ?></wp:attachment_url>
  335. <?php endif; ?>
  336. <?php wxr_post_taxonomy(); ?>
  337. <?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
  338. foreach ( $postmeta as $meta ) :
  339. if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) )
  340. continue;
  341. ?>
  342. <wp:postmeta>
  343. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  344. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  345. </wp:postmeta>
  346. <?php endforeach; ?>
  347. <?php $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
  348. foreach ( $comments as $c ) : ?>
  349. <wp:comment>
  350. <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
  351. <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
  352. <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
  353. <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
  354. <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
  355. <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
  356. <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
  357. <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
  358. <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
  359. <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
  360. <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
  361. <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
  362. <?php $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
  363. foreach ( $c_meta as $meta ) : ?>
  364. <wp:commentmeta>
  365. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  366. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  367. </wp:commentmeta>
  368. <?php endforeach; ?>
  369. </wp:comment>
  370. <?php endforeach; ?>
  371. </item>
  372. <?php
  373. }
  374. }
  375. } ?>
  376. </channel>
  377. </rss>
  378. <?php
  379. }