Ticket #27048: export_multiple_post_types.diff
File export_multiple_post_types.diff, 9.7 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 $('.exportall').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 $('.exportall' ).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>164 <p><label><input type="checkbox" class="exportall" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label></p> 157 165 <p class="description"><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></p> 158 166 159 <p><label><input type=" radio" name="content" value="posts" /> <?php _e( 'Posts' ); ?></label></p>167 <p><label><input type="checkbox" name="content[]" value="posts" checked="checked" /> <?php _e( 'Posts' ); ?></label></p> 160 168 <ul id="post-filters" class="export-filters"> 161 169 <li> 162 170 <label><?php _e( 'Categories:' ); ?></label> 163 171 <?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?> 164 172 </li> 173 </ul> 174 175 <p><label><input type="checkbox" name="content[]" value="pages" checked="checked" /> <?php _e( 'Pages' ); ?></label></p> 176 177 <?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?> 178 <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> 179 <?php endforeach; ?> 180 181 <h3><?php _e( 'Filter exported content' ); ?></h3> 182 <ul class="export-filter"> 165 183 <li> 166 184 <label><?php _e( 'Authors:' ); ?></label> 167 185 <?php 168 $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = 'post'" );186 $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts}" ); 169 187 wp_dropdown_users( array( 'include' => $authors, 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) ); 170 188 ?> 171 189 </li> … … 192 210 </li> 193 211 </ul> 194 212 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 213 <?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 214 /** 232 215 * Fires after the export filters form. 233 216 * -
wp-admin/includes/export.php
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 $where = ''; 54 51 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 } 52 $post_types = array(); 53 if( count( $args['content'] ) > 0 ) { 54 foreach( $args['content'] as $post_type ) { 55 if( ! post_type_exists( $post_type ) ) 56 continue; 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 else 65 $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'"; 58 $ptype = get_post_type_object( $post_type ); 66 59 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'] ); 60 if ( ! $ptype->can_export ) 61 continue; 62 63 $post_types[] = $post_type; 72 64 } 65 66 if( count( $post_types ) > 0 ) { 67 $esses = array_fill( 0, count($post_types), '%s' ); 68 $where .= $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types ); 69 } 73 70 } 74 71 75 if ( 'post' == $args['content'] || 'page' == $args['content'] ) { 72 if( $where ) { 73 if ( $args['status'] ) 74 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] ); 75 else 76 $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'"; 77 78 $join = ''; 79 if ( $args['category'] ) { 80 if ( $term = term_exists( $args['category'], 'category' ) ) { 81 $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)"; 82 $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] ); 83 } 84 } 85 76 86 if ( $args['author'] ) 77 87 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] ); 78 88 … … 84 94 } 85 95 86 96 // 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" ); 97 $query = "SELECT ID FROM {$wpdb->posts} $join"; 98 if( $where ) 99 $query .= " WHERE $where"; 100 $post_ids = $wpdb->get_col( $query ); 88 101 89 102 // get the requested terms ready, empty unless posts filtered by category or all content 90 103 $cats = $tags = $terms = array();