PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/export.php

https://github.com/da319/CULS
PHP | 409 lines | 255 code | 56 blank | 98 comment | 58 complexity | 958cdaf27f6403f71cd76b9d471f6af7 MD5 | raw file
  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 unknown
  14. * @var string
  15. */
  16. define('WXR_VERSION', '1.0');
  17. /**
  18. * {@internal Missing Short Description}}
  19. *
  20. * @since unknown
  21. *
  22. * @param unknown_type $args
  23. */
  24. function export_wp( $args = array() ) {
  25. global $wpdb, $post_ids, $post, $wp_taxonomies;
  26. if ( ! is_array( $args ) )
  27. $args = array( 'author' => $args );
  28. $defaults = array( 'author' => null, 'taxonomy' => null, 'post_type' => null, 'post_status' => null, 'start_date' => null, 'end_date' => null );
  29. $args = wp_parse_args( $args, $defaults );
  30. extract($args);
  31. do_action('export_wp');
  32. if( strlen( $start_date ) > 4 && strlen( $end_date ) > 4 )
  33. $filename = 'wordpress.' . $start_date . '.' . $end_date . '.xml';
  34. else
  35. $filename = 'wordpress.' . date( 'Y-m-d' ) . '.xml';
  36. header( 'Content-Description: File Transfer' );
  37. header( 'Content-Disposition: attachment; filename=' . $filename );
  38. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
  39. if ( $post_type && $post_type != 'all' )
  40. $where = $wpdb->prepare("WHERE post_type = %s ", $post_type);
  41. else
  42. $where = "WHERE post_type != 'revision' ";
  43. if ( $author && $author != 'all' ) {
  44. $author_id = (int) $author;
  45. $where .= $wpdb->prepare( "AND post_author = %d ", $author_id );
  46. }
  47. if ( $start_date && $start_date != 'all' )
  48. $where .= $wpdb->prepare( "AND post_date >= %s ", $start_date );
  49. if ( $end_date && $end_date != 'all' )
  50. $where .= $wpdb->prepare( "AND post_date < %s ", $end_date );
  51. if ( $taxonomy && is_array( $taxonomy ) ) {
  52. foreach ( $taxonomy as $term_id ) {
  53. if ( $term_id != 'all' )
  54. $where .= $wpdb->prepare( "AND ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d) ", $term_id );
  55. }
  56. }
  57. if ( $post_status && $post_status != 'all' )
  58. $where .= $wpdb->prepare( "AND post_status = %s", $post_status );
  59. // grab a snapshot of post IDs, just in case it changes during the export
  60. $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
  61. $categories = (array) get_categories( array( 'get' => 'all' ) );
  62. $tags = (array) get_tags( array( 'get' => 'all' ) );
  63. $custom_taxonomies = $wp_taxonomies;
  64. unset( $custom_taxonomies['category'] );
  65. unset( $custom_taxonomies['post_tag'] );
  66. unset( $custom_taxonomies['link_category'] );
  67. $custom_taxonomies = array_keys( $custom_taxonomies );
  68. $terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
  69. /**
  70. * {@internal Missing Short Description}}
  71. *
  72. * @since unknown
  73. *
  74. * @param unknown_type $categories
  75. */
  76. function wxr_missing_parents( $categories ) {
  77. if ( ! is_array( $categories ) || empty( $categories ) )
  78. return array();
  79. foreach ( $categories as $category ){
  80. $parents[$category->term_id] = $category->parent;
  81. }
  82. $parents = array_unique( array_diff( $parents, array_keys( $parents ) ) );
  83. if ( $zero = array_search( '0', $parents ) )
  84. unset( $parents[$zero] );
  85. return $parents;
  86. }
  87. while ( $parents = wxr_missing_parents( $categories ) ) {
  88. $found_parents = get_categories( array( 'include' => join( ', ', $parents) ) );
  89. if ( is_array( $found_parents ) && count( $found_parents ) )
  90. $categories = array_merge( $categories, $found_parents );
  91. else
  92. break;
  93. }
  94. // Put them in order to be inserted with no child going before its parent
  95. $pass = 0;
  96. $passes = 1000 + count( $categories );
  97. while ( ( $cat = array_shift( $categories ) ) && ++$pass < $passes ) {
  98. if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
  99. $cats[$cat->term_id] = $cat;
  100. else
  101. $categories[] = $cat;
  102. }
  103. unset( $categories );
  104. /**
  105. * Place string in CDATA tag.
  106. *
  107. * @since unknown
  108. *
  109. * @param string $str String to place in XML CDATA tag.
  110. */
  111. function wxr_cdata( $str ) {
  112. if ( seems_utf8( $str ) == false )
  113. $str = utf8_encode( $str );
  114. // $str = ent2ncr(esc_html($str));
  115. $str = "<![CDATA[$str" . ( ( substr( $str, -1 ) == ']' ) ? ' ' : '') . "]]>";
  116. return $str;
  117. }
  118. /**
  119. * {@internal Missing Short Description}}
  120. *
  121. * @since unknown
  122. *
  123. * @return string Site URL.
  124. */
  125. function wxr_site_url() {
  126. global $current_site;
  127. // mu: the base url
  128. if ( isset( $current_site->domain ) )
  129. return network_home_url();
  130. // wp: the blog url
  131. else
  132. return get_bloginfo_rss( 'url' );
  133. }
  134. /**
  135. * {@internal Missing Short Description}}
  136. *
  137. * @since unknown
  138. *
  139. * @param object $c Category Object
  140. */
  141. function wxr_cat_name( $c ) {
  142. if ( empty( $c->name ) )
  143. return;
  144. echo '<wp:cat_name>' . wxr_cdata( $c->name ) . '</wp:cat_name>';
  145. }
  146. /**
  147. * {@internal Missing Short Description}}
  148. *
  149. * @since unknown
  150. *
  151. * @param object $c Category Object
  152. */
  153. function wxr_category_description( $c ) {
  154. if ( empty( $c->description ) )
  155. return;
  156. echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
  157. }
  158. /**
  159. * {@internal Missing Short Description}}
  160. *
  161. * @since unknown
  162. *
  163. * @param object $t Tag Object
  164. */
  165. function wxr_tag_name( $t ) {
  166. if ( empty( $t->name ) )
  167. return;
  168. echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
  169. }
  170. /**
  171. * {@internal Missing Short Description}}
  172. *
  173. * @since unknown
  174. *
  175. * @param object $t Tag Object
  176. */
  177. function wxr_tag_description( $t ) {
  178. if ( empty( $t->description ) )
  179. return;
  180. echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
  181. }
  182. /**
  183. * {@internal Missing Short Description}}
  184. *
  185. * @since unknown
  186. *
  187. * @param object $t Term Object
  188. */
  189. function wxr_term_name( $t ) {
  190. if ( empty( $t->name ) )
  191. return;
  192. echo '<wp:term_name>' . wxr_cdata($t->name) . '</wp:term_name>';
  193. }
  194. /**
  195. * {@internal Missing Short Description}}
  196. *
  197. * @since unknown
  198. *
  199. * @param object $t Term Object
  200. */
  201. function wxr_term_description( $t ) {
  202. if ( empty( $t->description ) )
  203. return;
  204. echo '<wp:term_description>' . wxr_cdata($t->description) . '</wp:term_description>';
  205. }
  206. /**
  207. * {@internal Missing Short Description}}
  208. *
  209. * @since unknown
  210. */
  211. function wxr_post_taxonomy() {
  212. global $post;
  213. $the_list = '';
  214. $filter = 'rss';
  215. $taxonomies = get_object_taxonomies( 'post' );
  216. $terms = wp_get_post_terms( $post->ID, $taxonomies );
  217. foreach ( (array) $terms as $term ) {
  218. $domain = ( 'post_tag' == $term->taxonomy ) ? 'tag' : $term->taxonomy;
  219. $term_name = sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, $filter );
  220. // Back compat.
  221. if ( 'category' == $term->taxonomy )
  222. $the_list .= "\n\t\t<category><![CDATA[$term_name]]></category>\n";
  223. elseif ( 'post_tag' == $term->taxonomy )
  224. $the_list .= "\n\t\t<category domain=\"$domain\"><![CDATA[$term_name]]></category>\n";
  225. // forwards compatibility as above
  226. $the_list .= "\n\t\t<category domain=\"$domain\" nicename=\"{$term->slug}\"><![CDATA[$term_name]]></category>\n";
  227. }
  228. echo $the_list;
  229. }
  230. echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
  231. ?>
  232. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
  233. <!-- It contains information about your blog's posts, comments, and categories. -->
  234. <!-- You may use this file to transfer that content from one site to another. -->
  235. <!-- This file is not intended to serve as a complete backup of your blog. -->
  236. <!-- To import this information into a WordPress blog follow these steps. -->
  237. <!-- 1. Log in to that blog as an administrator. -->
  238. <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
  239. <!-- 3. Choose "WordPress" from the list. -->
  240. <!-- 4. Upload this file using the form provided on that page. -->
  241. <!-- 5. You will first be asked to map the authors in this export file to users -->
  242. <!-- on the blog. For each author, you may choose to map to an -->
  243. <!-- existing user on the blog or to create a new user -->
  244. <!-- 6. WordPress will then import each of the posts, comments, and categories -->
  245. <!-- contained in this file into your blog -->
  246. <?php the_generator( 'export' );?>
  247. <rss version="2.0"
  248. xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
  249. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  250. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  251. xmlns:dc="http://purl.org/dc/elements/1.1/"
  252. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
  253. >
  254. <channel>
  255. <title><?php bloginfo_rss( 'name' ); ?></title>
  256. <link><?php bloginfo_rss( 'url' ) ?></link>
  257. <description><?php bloginfo_rss( 'description' ) ?></description>
  258. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate>
  259. <generator>http://wordpress.org/?v=<?php bloginfo_rss( 'version' ); ?></generator>
  260. <language><?php echo get_option( 'rss_language' ); ?></language>
  261. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
  262. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
  263. <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
  264. <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
  265. <wp:category><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->name : ''; ?></wp:category_parent><?php wxr_cat_name( $c ); ?><?php wxr_category_description( $c ); ?></wp:category>
  266. <?php endforeach; endif; ?>
  267. <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
  268. <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name( $t ); ?><?php wxr_tag_description( $t ); ?></wp:tag>
  269. <?php endforeach; endif; ?>
  270. <?php if ( $terms ) : foreach ( $terms as $t ) : ?>
  271. <wp:term><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 ? $custom_taxonomies[$t->parent]->name : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
  272. <?php endforeach; endif; ?>
  273. <?php do_action( 'rss2_head' ); ?>
  274. <?php if ( $post_ids ) {
  275. global $wp_query;
  276. $wp_query->in_the_loop = true; // Fake being in the loop.
  277. // fetch 20 posts at a time rather than loading the entire table into memory
  278. while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
  279. $where = "WHERE ID IN (" . join( ',', $next_posts ) . ")";
  280. $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
  281. // Begin Loop
  282. foreach ($posts as $post) {
  283. setup_postdata( $post );
  284. $is_sticky = 0;
  285. if ( is_sticky( $post->ID ) )
  286. $is_sticky = 1;
  287. ?>
  288. <item>
  289. <title><?php echo apply_filters( 'the_title_rss', $post->post_title ); ?></title>
  290. <link><?php the_permalink_rss() ?></link>
  291. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
  292. <dc:creator><?php echo wxr_cdata( get_the_author() ); ?></dc:creator>
  293. <?php wxr_post_taxonomy() ?>
  294. <guid isPermaLink="false"><?php esc_url( the_guid() ); ?></guid>
  295. <description></description>
  296. <content:encoded><?php echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) ); ?></content:encoded>
  297. <excerpt:encoded><?php echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) ); ?></excerpt:encoded>
  298. <wp:post_id><?php echo $post->ID; ?></wp:post_id>
  299. <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
  300. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
  301. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
  302. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
  303. <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
  304. <wp:status><?php echo $post->post_status; ?></wp:status>
  305. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
  306. <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
  307. <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
  308. <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
  309. <wp:is_sticky><?php echo $is_sticky; ?></wp:is_sticky>
  310. <?php
  311. if ( $post->post_type == 'attachment' ) { ?>
  312. <wp:attachment_url><?php echo wp_get_attachment_url( $post->ID ); ?></wp:attachment_url>
  313. <?php } ?>
  314. <?php
  315. $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
  316. if ( $postmeta ) {
  317. ?>
  318. <?php foreach( $postmeta as $meta ) { ?>
  319. <wp:postmeta>
  320. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  321. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  322. </wp:postmeta>
  323. <?php } ?>
  324. <?php } ?>
  325. <?php
  326. $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID ) );
  327. if ( $comments ) { foreach ( $comments as $c ) { ?>
  328. <wp:comment>
  329. <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
  330. <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
  331. <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
  332. <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
  333. <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
  334. <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
  335. <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
  336. <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
  337. <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
  338. <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
  339. <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
  340. <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
  341. </wp:comment>
  342. <?php } } ?>
  343. </item>
  344. <?php
  345. }
  346. }
  347. } ?>
  348. </channel>
  349. </rss>
  350. <?php
  351. }
  352. ?>