Make WordPress Core

Ticket #27048: export_multiple_post_types.diff

File export_multiple_post_types.diff, 9.7 KB (added by hlashbrooke, 9 years ago)

First pass at updating UI and export function

  • 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                                        $('.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                                }
    3745                        }
    3846                });
     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                });
    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>
     164<p><label><input type="checkbox" class="exportall" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label></p>
    157165<p class="description"><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></p>
    158166
    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>
    160168<ul id="post-filters" class="export-filters">
    161169        <li>
    162170                <label><?php _e( 'Categories:' ); ?></label>
    163171                <?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?>
    164172        </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">
    165183        <li>
    166184                <label><?php _e( 'Authors:' ); ?></label>
    167185<?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}" );
    169187                wp_dropdown_users( array( 'include' => $authors, 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) );
    170188?>
    171189        </li>
     
    192210        </li>
    193211</ul>
    194212
    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>
    199213<?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
    231214/**
    232215 * Fires after the export filters form.
    233216 *
  • wp-admin/includes/export.php

     
    4747        header( 'Content-Disposition: attachment; filename=' . $filename );
    4848        header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    4949
    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 = '';
    5451
    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;
    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                        $ptype = get_post_type_object( $post_type );
    6659
    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;
    7264                }
     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                }
    7370        }
    7471
    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
    7686                if ( $args['author'] )
    7787                        $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
    7888
     
    8494        }
    8595
    8696        // 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 );
    88101
    89102        // get the requested terms ready, empty unless posts filtered by category or all content
    90103        $cats = $tags = $terms = array();