Ticket #28146: 28146.3.diff
File 28146.3.diff, 6.8 KB (added by , 10 years ago) |
---|
-
src/wp-admin/includes/export.php
function export_wp( $args = array() ) { 47 47 header( 'Content-Disposition: attachment; filename=' . $filename ); 48 48 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); 49 49 50 if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) { 51 $ptype = get_post_type_object( $args['content'] ); 52 if ( ! $ptype->can_export ) 53 $args['content'] = 'post'; 50 $query_args = array( 51 'post_status' => 'any', 52 'posts_per_page' => -1 53 ); 54 55 if ( 'all' != $args['content'] ) { 56 $args['content'] = (array) $args['content']; 57 58 $query_args['post_type'] = array(); 59 60 foreach( $args['content'] as $post_type ) { 61 $post_type_object = get_post_type_object( $post_type ); 62 63 if ( $post_type_object && $post_type_object->can_export ) { 64 $query_args['post_type'][] = $post_type; 65 } 66 } 54 67 55 $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] ); 68 if ( empty( $query_args['post_type'] ) ) { 69 $query_args['post_type'] = 'post'; 70 } 56 71 } else { 57 $post_types = get_post_types( array( 'can_export' => true ) ); 58 $esses = array_fill( 0, count($post_types), '%s' ); 59 $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types ); 72 $query_args['post_type'] = array_values( get_post_types( array( 'can_export' => true ) ) ); 60 73 } 61 74 62 if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) 63 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] ); 64 else 65 $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'"; 75 if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) { 76 $query_args['post_status'] = $args['status']; 77 } 66 78 67 $join = '';68 79 if ( $args['category'] && 'post' == $args['content'] ) { 69 80 if ( $term = term_exists( $args['category'], 'category' ) ) { 70 $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)"; 71 $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] ); 81 $query_args['tax_query'] = array( 82 array( 83 'taxonomy' => 'category', 84 'terms' => $term->term_id, 85 'include_children' => false 86 ) 87 ); 72 88 } 73 89 } 74 90 75 91 if ( 'post' == $args['content'] || 'page' == $args['content'] ) { 76 92 if ( $args['author'] ) 77 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] ); 93 $query_args['author'] = (int) $args['author']; 94 95 $date_query = array(); 78 96 79 97 if ( $args['start_date'] ) 80 $ where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );98 $date_query[]['after'] = $args['start_date']; 81 99 82 if ( $args['end_date'] ) 83 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) ); 100 if ( $args['end_date'] ) { 101 $date_query[]['before'] = '@' . strtotime( '+1 month', strtotime( $args['end_date'] ) ); 102 } 103 104 if ( ! empty( $date_query ) ) { 105 $query_args['date_query'] = $date_query; 106 } 84 107 } 85 108 86 // Grab a snapshot of post IDs, just in case it changes during the export. 87 $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" ); 109 /** 110 * Filter the query arguments for the exported data 111 * 112 * @todo Update since 113 * @since 4.X 114 * 115 * @param array $query_args WP_Query args to use for the exported data. 116 * @param array $args Filters defining what should be included in the export. 117 */ 118 $query_args = apply_filters( 'export_wp_query_args', $query_args, $args ); 119 120 $post_query_args = $query_args; 121 $post_query_args['fields'] = 'ids'; 122 123 $post_ids = get_posts( $post_query_args ); 88 124 89 125 /* 90 126 * Get the requested terms ready, empty unless posts filtered by category … … function export_wp( $args = array() ) { 95 131 $cat = get_term( $term['term_id'], 'category' ); 96 132 $cats = array( $cat->term_id => $cat ); 97 133 unset( $term, $cat ); 98 } else if ( 'all' == $args['content'] ) {134 } else if ( 'all' == $args['content'] ) { 99 135 $categories = (array) get_categories( array( 'get' => 'all' ) ); 100 136 $tags = (array) get_tags( array( 'get' => 'all' ) ); 101 137 102 138 $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) ); 103 139 $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); 104 140 105 // Put categories in order with no child going before its parent. 106 while ( $cat = array_shift( $categories ) ) { 107 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) 108 $cats[$cat->term_id] = $cat; 109 else 110 $categories[] = $cat; 141 // Put categories in order with no child going before its parent 142 if( $categories ) { 143 while ( $cat = array_shift( $categories ) ) { 144 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) { 145 $cats[$cat->term_id] = $cat; 146 } else { 147 $categories[] = $cat; 148 } 149 } 111 150 } 112 151 113 // Put terms in order with no child going before its parent. 114 while ( $t = array_shift( $custom_terms ) ) { 115 if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) 116 $terms[$t->term_id] = $t; 117 else 118 $custom_terms[] = $t; 152 // Put terms in order with no child going before its parent 153 if( $custom_terms ) { 154 while ( $t = array_shift( $custom_terms ) ) { 155 if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) { 156 $terms[$t->term_id] = $t; 157 } else { 158 $custom_terms[] = $t; 159 } 160 } 119 161 } 120 162 163 // Clean up 121 164 unset( $categories, $custom_taxonomies, $custom_terms ); 122 165 } 123 166 … … function wxr_filter_postmeta( $return_me, $meta_key ) { 375 418 ?> 376 419 377 420 <?php if ( $post_ids ) { 378 global $wp_query; 421 $query_args['posts_per_page'] = 20; 422 $query_args['paged'] = 1; 379 423 380 // Fake being in the loop. 381 $wp_query->in_the_loop = true; 424 $query = new WP_Query( $query_args ); 382 425 383 // Fetch 20 posts at a time rather than loading the entire table into memory. 384 while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) { 385 $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')'; 386 $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" ); 426 // Paginate posts 20 at a time 427 while ( $query->have_posts() ) { 428 // Begin Loop. 429 while ( $query->have_posts() ) { 430 $query->the_post(); 387 431 388 // Begin Loop. 389 foreach ( $posts as $post ) { 390 setup_postdata( $post ); 391 $is_sticky = is_sticky( $post->ID ) ? 1 : 0; 432 $is_sticky = is_sticky( $post->ID ) ? 1 : 0; 392 433 ?> 393 434 <item> 394 435 <title><?php … … function wxr_filter_postmeta( $return_me, $meta_key ) { 501 542 <?php endforeach; ?> 502 543 </item> 503 544 <?php 504 } 545 } 546 547 // Fetch next page of posts 548 $query_args['paged']++; 549 550 $query->query( $query_args ); 505 551 } 506 552 } ?> 507 553 </channel>