/core/actions/export.php

  1. <?php 
  2.  
  3. /** 
  4. * Export 
  5. * 
  6. * @description:  
  7. * @since: 3.6 
  8. * @created: 25/01/13 
  9. */ 
  10.  
  11. // Exit if accessed directly 
  12. if ( !defined( 'ABSPATH' ) ) exit; 
  13.  
  14.  
  15. // vars 
  16. $defaults = array( 
  17. 'acf_posts' => array(),  
  18. 'nonce' => '' 
  19. ); 
  20. $my_options = array_merge( $defaults, $_POST ); 
  21.  
  22.  
  23. // validate nonce 
  24. if( !wp_verify_nonce($my_options['nonce'], 'export') ) 
  25. wp_die(__("Error", 'acf')); 
  26.  
  27.  
  28. // check for posts 
  29. if( empty($my_options['acf_posts']) ) 
  30. wp_die(__("No ACF groups selected", 'acf')); 
  31.  
  32.  
  33. /** 
  34. * Version number for the export format. 
  35. * 
  36. * Bump this when something changes that might affect compatibility. 
  37. * 
  38. * @since 2.5.0 
  39. */ 
  40. define( 'WXR_VERSION', '1.1' ); 
  41.  
  42.  
  43. /** 
  44. * fix_line_breaks 
  45. * 
  46. * This function will loop through all array pieces and correct double line breaks from DB to XML 
  47. * 
  48. * @type function 
  49. * @date 2/12/2013 
  50. * @since 5.0.0 
  51. * 
  52. * @param $v (mixed) 
  53. * @return $v (mixed) 
  54. */ 
  55.  
  56. function fix_line_breaks( $v ) 
  57. if( is_array($v) ) 
  58. foreach( array_keys($v) as $k ) 
  59. $v[ $k ] = fix_line_breaks( $v[ $k ] ); 
  60. elseif( is_string($v) ) 
  61. $v = str_replace("\r\n", "\r", $v); 
  62.  
  63. return $v; 
  64.  
  65.  
  66. /** 
  67. * Wrap given string in XML CDATA tag. 
  68. * 
  69. * @since 2.1.0 
  70. * 
  71. * @param string $str String to wrap in XML CDATA tag. 
  72. */ 
  73. function wxr_cdata( $str ) { 
  74. if ( seems_utf8( $str ) == false ) 
  75. $str = utf8_encode( $str ); 
  76.  
  77. // $str = ent2ncr(esc_html($str)); 
  78. $str = "<![CDATA[$str" . ( ( substr( $str, -1 ) == ']' ) ? ' ' : '' ) . ']]>'; 
  79.  
  80. return $str; 
  81.  
  82. /** 
  83. * Return the URL of the site 
  84. * 
  85. * @since 2.5.0 
  86. * 
  87. * @return string Site URL. 
  88. */ 
  89. function wxr_site_url() { 
  90. // ms: the base url 
  91. if ( is_multisite() ) 
  92. return network_home_url(); 
  93. // wp: the blog url 
  94. else 
  95. return get_site_url(); 
  96.  
  97. /** 
  98. * Output a tag_description XML tag from a given tag object 
  99. * 
  100. * @since 2.3.0 
  101. * 
  102. * @param object $tag Tag Object 
  103. */ 
  104. function wxr_tag_description( $tag ) { 
  105. if ( empty( $tag->description ) ) 
  106. return; 
  107.  
  108. echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . '</wp:tag_description>'; 
  109.  
  110. /** 
  111. * Output a term_name XML tag from a given term object 
  112. * 
  113. * @since 2.9.0 
  114. * 
  115. * @param object $term Term Object 
  116. */ 
  117. function wxr_term_name( $term ) { 
  118. if ( empty( $term->name ) ) 
  119. return; 
  120.  
  121. echo '<wp:term_name>' . wxr_cdata( $term->name ) . '</wp:term_name>'; 
  122.  
  123. /** 
  124. * Output a term_description XML tag from a given term object 
  125. * 
  126. * @since 2.9.0 
  127. * 
  128. * @param object $term Term Object 
  129. */ 
  130. function wxr_term_description( $term ) { 
  131. if ( empty( $term->description ) ) 
  132. return; 
  133.  
  134. echo '<wp:term_description>' . wxr_cdata( $term->description ) . '</wp:term_description>'; 
  135.  
  136. /** 
  137. * Output list of authors with posts 
  138. * 
  139. * @since 3.1.0 
  140. */ 
  141. function wxr_authors_list() { 
  142. global $wpdb; 
  143.  
  144. $authors = array(); 
  145. $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts" ); 
  146. foreach ( (array) $results as $result ) 
  147. $authors[] = get_userdata( $result->post_author ); 
  148.  
  149. $authors = array_filter( $authors ); 
  150.  
  151. foreach( $authors as $author ) { 
  152. echo "\t<wp:author>"; 
  153. echo '<wp:author_id>' . $author->ID . '</wp:author_id>'; 
  154. echo '<wp:author_login>' . $author->user_login . '</wp:author_login>'; 
  155. echo '<wp:author_email>' . $author->user_email . '</wp:author_email>'; 
  156. echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>'; 
  157. echo '<wp:author_first_name>' . wxr_cdata( $author->user_firstname ) . '</wp:author_first_name>'; 
  158. echo '<wp:author_last_name>' . wxr_cdata( $author->user_lastname ) . '</wp:author_last_name>'; 
  159. echo "</wp:author>\n"; 
  160.  
  161. header( 'Content-Description: File Transfer' ); 
  162. header( 'Content-Disposition: attachment; filename=advanced-custom-field-export.xml' ); 
  163. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); 
  164.  
  165.  
  166. echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n"; 
  167.  
  168. ?> 
  169. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. --> 
  170. <!-- It contains information about your site's posts, pages, comments, categories, and other content. --> 
  171. <!-- You may use this file to transfer that content from one site to another. --> 
  172. <!-- This file is not intended to serve as a complete backup of your site. --> 
  173.  
  174. <!-- To import this information into a WordPress site follow these steps: --> 
  175. <!-- 1. Log in to that site as an administrator. --> 
  176. <!-- 2. Go to Tools: Import in the WordPress admin panel. --> 
  177. <!-- 3. Install the "WordPress" importer from the list. --> 
  178. <!-- 4. Activate & Run Importer. --> 
  179. <!-- 5. Upload this file using the form provided on that page. --> 
  180. <!-- 6. You will first be asked to map the authors in this export file to users --> 
  181. <!-- on the site. For each author, you may choose to map to an --> 
  182. <!-- existing user on the site or to create a new user. --> 
  183. <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. --> 
  184. <!-- contained in this file into your site. --> 
  185.  
  186. <?php the_generator( 'export' ); ?> 
  187. <rss version="2.0" 
  188. xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/" 
  189. xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  190. xmlns:wfw="http://wellformedweb.org/CommentAPI/" 
  191. xmlns:dc="http://purl.org/dc/elements/1.1/" 
  192. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/" 
  193.  
  194. <channel> 
  195. <title><?php bloginfo_rss( 'name' ); ?></title> 
  196. <link><?php bloginfo_rss( 'url' ); ?></link> 
  197. <description><?php bloginfo_rss( 'description' ); ?></description> 
  198. <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate> 
  199. <language><?php echo get_option( 'rss_language' ); ?></language> 
  200. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version> 
  201. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url> 
  202. <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url> 
  203. <?php wxr_authors_list(); ?> 
  204. <?php if ( $my_options['acf_posts'] ) { 
  205.  
  206. global $wp_query, $wpdb, $post; 
  207. $wp_query->in_the_loop = true; // Fake being in the loop. 
  208.  
  209. // create SQL with %d placeholders 
  210. $where = 'WHERE ID IN (' . substr(str_repeat('%d, ', count($my_options['acf_posts'])), 0, -1) . ')'; 
  211.  
  212. // now prepare the SQL based on the %d + $_POST data 
  213. $posts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->posts} $where", $my_options['acf_posts'])); 
  214.  
  215. // Begin Loop 
  216. foreach ( $posts as $post ) { 
  217. setup_postdata( $post ); 
  218. ?> 
  219. <item> 
  220. <title><?php echo apply_filters( 'the_title_rss', $post->post_title ); ?></title> 
  221. <link><?php the_permalink_rss() ?></link> 
  222. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate> 
  223. <dc:creator><?php echo get_the_author_meta( 'login' ); ?></dc:creator> 
  224. <guid isPermaLink="false"><?php esc_url( the_guid() ); ?></guid> 
  225. <wp:post_id><?php echo $post->ID; ?></wp:post_id> 
  226. <wp:post_date><?php echo $post->post_date; ?></wp:post_date> 
  227. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt> 
  228. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status> 
  229. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status> 
  230. <wp:post_name><?php echo $post->post_name; ?></wp:post_name> 
  231. <wp:status><?php echo $post->post_status; ?></wp:status> 
  232. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent> 
  233. <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order> 
  234. <wp:post_type><?php echo $post->post_type; ?></wp:post_type> 
  235. <wp:post_password><?php echo $post->post_password; ?></wp:post_password> 
  236. <?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) ); 
  237. foreach( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' ) :  
  238.  
  239. $meta->meta_value = maybe_unserialize( $meta->meta_value ); 
  240. $meta->meta_value = fix_line_breaks( $meta->meta_value ); 
  241. $meta->meta_value = maybe_serialize( $meta->meta_value ); 
  242.  
  243. ?> 
  244. <wp:postmeta> 
  245. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key> 
  246. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value> 
  247. </wp:postmeta> 
  248. <?php endif; endforeach; ?> 
  249. </item> 
  250. <?php 
  251. }  
  252. ?> 
  253. </channel> 
  254. </rss> 
.