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