Make WordPress Core

Ticket #27048: export_multiple_post_types2.diff

File export_multiple_post_types2.diff, 15.3 KB (added by hlashbrooke, 9 years ago)

Moving to WP_Query instead of raw DB queries

  • wp-admin/export.php

     
    2626<script type="text/javascript">
    2727//<![CDATA[
    2828        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                                }
    3745                        }
    3846                });
     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                });
    3954        });
    4055//]]>
    4156</script>
     
    5873
    5974if ( isset( $_GET['download'] ) ) {
    6075        $args = array();
     76        $args['content'] = array();
    6177
    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        }
    6687
     88        if ( in_array( 'post', $args['content'] ) ) {
    6789                if ( $_GET['cat'] )
    6890                        $args['category'] = (int) $_GET['cat'];
     91        }
    6992
    70                 if ( $_GET['post_author'] )
    71                         $args['author'] = (int) $_GET['post_author'];
     93        if ( $_GET['post_author'] )
     94                $args['author'] = (int) $_GET['post_author'];
    7295
    73                 if ( $_GET['post_start_date'] || $_GET['post_end_date'] ) {
    74                         $args['start_date'] = $_GET['post_start_date'];
    75                         $args['end_date'] = $_GET['post_end_date'];
    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        }
    77100
    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'];
    82103
    83                 if ( $_GET['page_author'] )
    84                         $args['author'] = (int) $_GET['page_author'];
    85104
    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 
    97105        /**
    98106         * Filter the export args.
    99107         *
     
    153161<h3><?php _e( 'Choose what to export' ); ?></h3>
    154162<form action="" method="get" id="export-filters">
    155163<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>
    158165
    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>
    160171<ul id="post-filters" class="export-filters">
    161172        <li>
    162173                <label><?php _e( 'Categories:' ); ?></label>
    163174                <?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?>
    164175        </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">
    165186        <li>
    166187                <label><?php _e( 'Authors:' ); ?></label>
    167188<?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}" );
    169190                wp_dropdown_users( array( 'include' => $authors, 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) );
    170191?>
    171192        </li>
     
    192213        </li>
    193214</ul>
    194215
    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>
    199216<?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 <?php
    231217/**
    232218 * Fires after the export filters form.
    233219 *
  • wp-admin/includes/export.php

     
    3939         */
    4040        do_action( 'export_wp', $args );
    4141
     42        // Set export file name
    4243        $sitename = sanitize_key( get_bloginfo( 'name' ) );
    4344        if ( ! empty($sitename) ) $sitename .= '.';
    4445        $filename = $sitename . 'wordpress.' . date( 'Y-m-d' ) . '.xml';
    4546
     47        // Set content headers
    4648        header( 'Content-Description: File Transfer' );
    4749        header( 'Content-Disposition: attachment; filename=' . $filename );
    4850        header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    4951
    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();
    5455
    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 ) {
    6157
    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                // Get queried post types
     59                foreach( $args['content'] as $post_type ) {
     60                        if( ! post_type_exists( $post_type ) )
     61                                continue;
    6662
    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;
    7269                }
    73         }
    7470
    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;
    7873
    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 ) {
    8175
    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                }
    84162        }
    85163
    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
    90165        $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' ) );
    98166
    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 ) {
    101168
    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                        }
    108180                }
    109181
    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' ) );
    116185                }
    117186
    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 );
    119223        }
    120224
    121225        /**
     
    355459<?php foreach ( $terms as $t ) : ?>
    356460        <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>
    357461<?php endforeach; ?>
    358 <?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
     462<?php if ( in_array( 'menus', $args['content'] ) ) wxr_nav_menu_terms(); ?>
    359463
    360464        <?php
    361465        /** This action is documented in wp-includes/feed-rss2.php */
     
    472576        }
    473577} ?>
    474578</channel>
    475 </rss>
    476 <?php
    477 }
     579</rss><?php
     580}
     581 No newline at end of file