Ticket #27048: export_multiple_post_types2.diff
File export_multiple_post_types2.diff, 15.3 KB (added by , 9 years ago) |
---|
-
wp-admin/export.php
26 26 <script type="text/javascript"> 27 27 //<![CDATA[ 28 28 jQuery(document).ready(function($){ 29 var form = $('#export-filters'), 30 filters = form.find('.export-filters'); 31 filters.hide(); 32 form.find('input:radio').change(function() { 33 filters.slideUp('fast'); 34 switch ( $(this).val() ) { 35 case 'posts': $('#post-filters').slideDown(); break; 36 case 'pages': $('#page-filters').slideDown(); break; 29 var form = $('#export-filters'); 30 form.find('input:checkbox').change(function() { 31 if( 'all' != $(this).val() ) { 32 var checked = $(this).attr('checked'); 33 if('checked' != checked) { 34 $('.selectall').removeAttr('checked'); 35 } 36 switch ( $(this).val() ) { 37 case 'posts': 38 if('checked' != checked) { 39 $('#post-filters').slideUp(); 40 } else { 41 $('#post-filters').slideDown(); 42 } 43 break; 44 } 37 45 } 38 46 }); 47 $('.selectall' ).click(function() { 48 var checked = this.checked; 49 form.find('input:checkbox').each(function() { 50 $(this).attr('checked',checked); 51 $(this).change(); 52 }); 53 }); 39 54 }); 40 55 //]]> 41 56 </script> … … 58 73 59 74 if ( isset( $_GET['download'] ) ) { 60 75 $args = array(); 76 $args['content'] = array(); 61 77 62 if ( ! isset( $_GET['content'] ) || 'all' == $_GET['content'] ) { 63 $args['content'] = 'all'; 64 } else if ( 'posts' == $_GET['content'] ) { 65 $args['content'] = 'post'; 78 if ( isset( $_GET['content'] ) && count( $_GET['content'] ) > 0 ) { 79 foreach( $_GET['content'] as $post_type ) { 80 switch( $post_type ) { 81 case 'posts': $post_type = 'post'; break; 82 case 'pages': $post_type = 'page'; break; 83 } 84 $args['content'][] = $post_type; 85 } 86 } 66 87 88 if ( in_array( 'post', $args['content'] ) ) { 67 89 if ( $_GET['cat'] ) 68 90 $args['category'] = (int) $_GET['cat']; 91 } 69 92 70 71 93 if ( $_GET['post_author'] ) 94 $args['author'] = (int) $_GET['post_author']; 72 95 73 74 75 76 96 if ( $_GET['post_start_date'] || $_GET['post_end_date'] ) { 97 $args['start_date'] = $_GET['post_start_date']; 98 $args['end_date'] = $_GET['post_end_date']; 99 } 77 100 78 if ( $_GET['post_status'] ) 79 $args['status'] = $_GET['post_status']; 80 } else if ( 'pages' == $_GET['content'] ) { 81 $args['content'] = 'page'; 101 if ( $_GET['post_status'] ) 102 $args['status'] = $_GET['post_status']; 82 103 83 if ( $_GET['page_author'] )84 $args['author'] = (int) $_GET['page_author'];85 104 86 if ( $_GET['page_start_date'] || $_GET['page_end_date'] ) {87 $args['start_date'] = $_GET['page_start_date'];88 $args['end_date'] = $_GET['page_end_date'];89 }90 91 if ( $_GET['page_status'] )92 $args['status'] = $_GET['page_status'];93 } else {94 $args['content'] = $_GET['content'];95 }96 97 105 /** 98 106 * Filter the export args. 99 107 * … … 153 161 <h3><?php _e( 'Choose what to export' ); ?></h3> 154 162 <form action="" method="get" id="export-filters"> 155 163 <input type="hidden" name="download" value="true" /> 156 <p><label><input type="radio" name="content" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label></p> 157 <p class="description"><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></p> 164 <p><label><input type="checkbox" class="selectall" value="all" checked="checked" /> <?php _e( 'Select all' ); ?></label></p> 158 165 159 <p><label><input type="radio" name="content" value="posts" /> <?php _e( 'Posts' ); ?></label></p> 166 <hr/> 167 168 <p><label><input type="checkbox" name="content[]" value="menus" checked="checked" /> <?php _e( 'Menus' ); ?></label></p> 169 170 <p><label><input type="checkbox" name="content[]" value="posts" checked="checked" /> <?php _e( 'Posts' ); ?></label></p> 160 171 <ul id="post-filters" class="export-filters"> 161 172 <li> 162 173 <label><?php _e( 'Categories:' ); ?></label> 163 174 <?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?> 164 175 </li> 176 </ul> 177 178 <p><label><input type="checkbox" name="content[]" value="pages" checked="checked" /> <?php _e( 'Pages' ); ?></label></p> 179 180 <?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?> 181 <p><label><input type="checkbox" name="content[]" value="<?php echo esc_attr( $post_type->name ); ?>" checked="checked" /> <?php echo esc_html( $post_type->label ); ?></label></p> 182 <?php endforeach; ?> 183 184 <h3><?php _e( 'Filter exported content' ); ?></h3> 185 <ul class="export-filter"> 165 186 <li> 166 187 <label><?php _e( 'Authors:' ); ?></label> 167 188 <?php 168 $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = 'post'" );189 $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts}" ); 169 190 wp_dropdown_users( array( 'include' => $authors, 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) ); 170 191 ?> 171 192 </li> … … 192 213 </li> 193 214 </ul> 194 215 195 <p><label><input type="radio" name="content" value="pages" /> <?php _e( 'Pages' ); ?></label></p>196 <ul id="page-filters" class="export-filters">197 <li>198 <label><?php _e( 'Authors:' ); ?></label>199 216 <?php 200 $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = 'page'" );201 wp_dropdown_users( array( 'include' => $authors, 'name' => 'page_author', 'multi' => true, 'show_option_all' => __('All') ) );202 ?>203 </li>204 <li>205 <label><?php _e( 'Date range:' ); ?></label>206 <select name="page_start_date">207 <option value="0"><?php _e( 'Start Date' ); ?></option>208 <?php export_date_options( 'page' ); ?>209 </select>210 <select name="page_end_date">211 <option value="0"><?php _e( 'End Date' ); ?></option>212 <?php export_date_options( 'page' ); ?>213 </select>214 </li>215 <li>216 <label><?php _e( 'Status:' ); ?></label>217 <select name="page_status">218 <option value="0"><?php _e( 'All' ); ?></option>219 <?php foreach ( $post_stati as $status ) : ?>220 <option value="<?php echo esc_attr( $status->name ); ?>"><?php echo esc_html( $status->label ); ?></option>221 <?php endforeach; ?>222 </select>223 </li>224 </ul>225 226 <?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?>227 <p><label><input type="radio" name="content" value="<?php echo esc_attr( $post_type->name ); ?>" /> <?php echo esc_html( $post_type->label ); ?></label></p>228 <?php endforeach; ?>229 230 <?php231 217 /** 232 218 * Fires after the export filters form. 233 219 * -
wp-admin/includes/export.php
39 39 */ 40 40 do_action( 'export_wp', $args ); 41 41 42 // Set export file name 42 43 $sitename = sanitize_key( get_bloginfo( 'name' ) ); 43 44 if ( ! empty($sitename) ) $sitename .= '.'; 44 45 $filename = $sitename . 'wordpress.' . date( 'Y-m-d' ) . '.xml'; 45 46 47 // Set content headers 46 48 header( 'Content-Description: File Transfer' ); 47 49 header( 'Content-Disposition: attachment; filename=' . $filename ); 48 50 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); 49 51 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'; 52 $post_types = array(); 53 $post_ids = array(); 54 $types_to_query = array(); 54 55 55 $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] ); 56 } 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 ); 60 } 56 if( count( $args['content'] ) > 0 ) { 61 57 62 if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )63 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );64 else65 $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";58 // Get queried post types 59 foreach( $args['content'] as $post_type ) { 60 if( ! post_type_exists( $post_type ) ) 61 continue; 66 62 67 $join = ''; 68 if ( $args['category'] && 'post' == $args['content'] ) { 69 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'] ); 63 $ptype = get_post_type_object( $post_type ); 64 65 if ( ! $ptype->can_export ) 66 continue; 67 68 $post_types[] = $post_type; 72 69 } 73 }74 70 75 if ( 'post' == $args['content'] || 'page' == $args['content'] ) { 76 if ( $args['author'] ) 77 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] ); 71 $query_args = $post_query_args = array(); 72 $post_cat = false; 78 73 79 if ( $args['start_date'] ) 80 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) ); 74 if( count( $post_types ) > 0 ) { 81 75 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'])) ) ); 76 $types_to_query = $post_types; 77 78 // Build up query arguments 79 $query_args['posts_per_page'] = -1; 80 81 // Get queried post stati 82 if( $args['status'] ) { 83 $query_args['post_status'] = $args['status']; 84 } else { 85 $post_stati = get_post_stati(); 86 $statuses = array(); 87 foreach( $post_stati as $status ) { 88 if( 'auto-draft' != $status ) { 89 $statuses[] = $status; 90 } 91 } 92 if( count( $statuses ) > 0 ) { 93 $query_args['post_status'] = $statuses; 94 } 95 } 96 97 // Get queried author 98 if( $args['author'] ) { 99 $query_args['author'] = $args['author']; 100 } 101 102 // Get queried start date 103 if( $args['start_date'] ) { 104 $start_date = explode( '-', $args['start_date'] ); 105 $query_args['date_query'][0]['after'] = array( 106 'year' => $start_date[0], 107 'month' => $start_date[1], 108 'day' => 1 109 ); 110 } 111 112 // Get queried end date 113 if( $args['end_date'] ) { 114 $end_date = explode( '-', $args['end_date'] ); 115 $last_day = date( 't', strtotime( $args['end_date'] ) ); 116 $query_args['date_query'][0]['before'] = array( 117 'year' => $end_date[0], 118 'month' => $end_date[1], 119 'day' => $last_day 120 ); 121 } 122 123 // Include first and last days of month in date queries 124 if( isset( $query_args['date_query']) ) { 125 $query_args['date_query'][0]['inclusive'] = true; 126 } 127 128 // Get queried category and fetch posts of type 'post' for that category 129 if ( $args['category'] && in_array( 'post', $post_types ) ) { 130 if ( $post_cat = term_exists( $args['category'], 'category' ) ) { 131 132 $post_query_args = $query_args; 133 134 $post_query_args['post_type'] = 'post'; 135 $post_query_args['category'] = $args['category']; 136 137 $posts = get_posts( $post_query_args ); 138 foreach( $posts as $post ) { 139 $post_ids[] = $post->ID; 140 } 141 142 // Remove 'post' from post types to query 143 $types_to_query = array(); 144 foreach( $post_types as $type ) { 145 if( 'post' != $type ) { 146 $types_to_query[] = $type; 147 } 148 } 149 } 150 } 151 152 // Query all remaininng post types 153 if( count( $types_to_query ) > 0 ) { 154 $query_args['post_type'] = $types_to_query; 155 156 $posts = get_posts( $query_args ); 157 foreach( $posts as $post ) { 158 $post_ids[] = $post->ID; 159 } 160 } 161 } 84 162 } 85 163 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" ); 88 89 // get the requested terms ready, empty unless posts filtered by category or all content 164 // Get the requested terms ready 90 165 $cats = $tags = $terms = array(); 91 if ( isset( $term ) && $term ) {92 $cat = get_term( $term['term_id'], 'category' );93 $cats = array( $cat->term_id => $cat );94 unset( $term, $cat );95 } else if ( 'all' == $args['content'] ) {96 $categories = (array) get_categories( array( 'get' => 'all' ) );97 $tags = (array) get_tags( array( 'get' => 'all' ) );98 166 99 $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) ); 100 $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); 167 if( count( $post_types ) > 0 ) { 101 168 102 // put categories in order with no child going before its parent 103 while ( $cat = array_shift( $categories ) ) { 104 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) 105 $cats[$cat->term_id] = $cat; 106 else 107 $categories[] = $cat; 169 $custom_taxonomies = $custom_terms = $categories = array(); 170 171 // Get taxonomies for each post type 172 foreach( $post_types as $type ) { 173 174 if( 'post' == $type ) continue; 175 176 $taxonomies = get_object_taxonomies( $type ); 177 foreach( $taxonomies as $tax ) { 178 array_unshift( $custom_taxonomies, $tax ); 179 } 108 180 } 109 181 110 // put terms in order with no child going before its parent 111 while ( $t = array_shift( $custom_terms ) ) { 112 if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) 113 $terms[$t->term_id] = $t; 114 else 115 $custom_terms[] = $t; 182 // Get terms for each custom taxonomy 183 if( count( $custom_taxonomies ) > 0 ) { 184 $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); 116 185 } 117 186 118 unset( $categories, $custom_taxonomies, $custom_terms ); 187 // Get tags and categories for posts 188 if( in_array( 'post', $post_types ) ) { 189 if ( $post_cat ) { 190 $cat = get_term( $post_cat['term_id'], 'category' ); 191 $cats = array( $cat->term_id => $cat ); 192 unset( $cat ); 193 } else { 194 $categories = (array) get_categories( array( 'get' => 'all' ) ); 195 } 196 $tags = (array) get_tags( array( 'get' => 'all' ) ); 197 } 198 199 // Put categories in order with no child going before its parent 200 if( count( $categories ) > 0 ) { 201 while ( $cat = array_shift( $categories ) ) { 202 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) { 203 $cats[$cat->term_id] = $cat; 204 } else { 205 $categories[] = $cat; 206 } 207 } 208 } 209 210 // Put terms in order with no child going before its parent 211 if( count( $custom_terms ) > 0 ) { 212 while ( $t = array_shift( $custom_terms ) ) { 213 if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) { 214 $terms[$t->term_id] = $t; 215 } else { 216 $custom_terms[] = $t; 217 } 218 } 219 } 220 221 // Clean up 222 unset( $categories, $custom_taxonomies, $custom_terms, $taxonomies, $tax ); 119 223 } 120 224 121 225 /** … … 355 459 <?php foreach ( $terms as $t ) : ?> 356 460 <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> 357 461 <?php endforeach; ?> 358 <?php if ( 'all' == $args['content']) wxr_nav_menu_terms(); ?>462 <?php if ( in_array( 'menus', $args['content'] ) ) wxr_nav_menu_terms(); ?> 359 463 360 464 <?php 361 465 /** This action is documented in wp-includes/feed-rss2.php */ … … 472 576 } 473 577 } ?> 474 578 </channel> 475 </rss> 476 <?php 477 } 579 </rss><?php 580 } 581 No newline at end of file