Make WordPress Core

Ticket #28146: 28146.4.diff

File 28146.4.diff, 7.8 KB (added by sc0ttkclark, 10 years ago)

Use the more performant postin for paginating posts by $post_ids, add a filter to $post_ids for people to include things that would be impossible with the query args filter on it's own -- for instance -- Including attachments whose post_parentin $post_ids

  • src/wp-admin/includes/export.php

    function export_wp( $args = array() ) { 
    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        $query_args = array(
     53                'post_status' => 'any',
     54                'posts_per_page' => -1
     55        );
     56
     57        if ( 'all' != $args['content'] ) {
     58                $args['content'] = (array) $args['content'];
     59
     60                $query_args['post_type'] = array();
    5461
    55                 $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
     62                foreach( $args['content'] as $post_type ) {
     63                        $post_type_object = get_post_type_object( $post_type );
     64
     65                        if ( $post_type_object && $post_type_object->can_export ) {
     66                                $query_args['post_type'][] = $post_type;
     67                        }
     68                }
     69
     70                if ( empty( $query_args['post_type'] ) ) {
     71                        $query_args['post_type'] = 'post';
     72                }
    5673        } 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 );
     74                $query_args['post_type'] = array_values( get_post_types( array( 'can_export' => true ) ) );
    6075        }
    6176
    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'";
     77        if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) {
     78                $query_args['post_status'] = $args['status'];
     79        }
    6680
    67         $join = '';
    6881        if ( $args['category'] && 'post' == $args['content'] ) {
    6982                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'] );
     83                        $query_args['tax_query'] = array(
     84                                array(
     85                                        'taxonomy' => 'category',
     86                                        'terms' => $term->term_id,
     87                                        'include_children' => false
     88                                )
     89                        );
    7290                }
    7391        }
    7492
    7593        if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
    7694                if ( $args['author'] )
    77                         $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
     95                        $query_args['author'] = (int) $args['author'];
     96
     97                $date_query = array();
    7898
    7999                if ( $args['start_date'] )
    80                         $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
     100                        $date_query[]['after'] = $args['start_date'];
    81101
    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'])) ) );
     102                if ( $args['end_date'] ) {
     103                        $date_query[]['before'] =  '@' . strtotime( '+1 month', strtotime( $args['end_date'] ) );
     104                }
     105
     106                if ( ! empty( $date_query ) ) {
     107                        $query_args['date_query'] = $date_query;
     108                }
    84109        }
    85110
    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" );
     111        /**
     112         * Filter WP_Query arguments for which posts to export.
     113         *
     114         * @todo Update since
     115         * @since 4.X
     116         *
     117         * @param array $query_args An array of WP_Query arguments.
     118         * @param array $args An array of export arguments.
     119         */
     120        $query_args = apply_filters( 'export_wp_query_args', $query_args, $args );
     121
     122        $query_args['fields'] = 'ids';
     123
     124        $post_ids = get_posts( $query_args );
     125
     126        /**
     127         * Filter Post IDs to be exported.
     128         *
     129         * @todo Update since
     130         * @since 4.X
     131         *
     132         * @param array $post_ids An array of Post IDs to export.
     133         * @param array $query_args An array of WP_Query arguments.
     134         * @param array $args An array of export arguments.
     135         */
     136        $post_ids = apply_filters( 'export_wp_post_ids', $post_ids, $query_args, $args );
    88137
    89138        /*
    90139         * Get the requested terms ready, empty unless posts filtered by category
    function export_wp( $args = array() ) { 
    95144                $cat = get_term( $term['term_id'], 'category' );
    96145                $cats = array( $cat->term_id => $cat );
    97146                unset( $term, $cat );
    98         } elseif ( 'all' == $args['content'] ) {
     147        } else if ( 'all' == $args['content'] ) {
    99148                $categories = (array) get_categories( array( 'get' => 'all' ) );
    100149                $tags = (array) get_tags( array( 'get' => 'all' ) );
    101150
    102151                $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    103152                $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
    104153
    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;
     154                // Put categories in order with no child going before its parent
     155                if( $categories ) {
     156                        while ( $cat = array_shift( $categories ) ) {
     157                                if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) {
     158                                        $cats[$cat->term_id] = $cat;
     159                                } else {
     160                                        $categories[] = $cat;
     161                                }
     162                        }
    111163                }
    112164
    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;
     165                // Put terms in order with no child going before its parent
     166                if( $custom_terms ) {
     167                        while ( $t = array_shift( $custom_terms ) ) {
     168                                if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) {
     169                                        $terms[$t->term_id] = $t;
     170                                } else {
     171                                        $custom_terms[] = $t;
     172                                }
     173                        }
    119174                }
    120175
     176                // Clean up
    121177                unset( $categories, $custom_taxonomies, $custom_terms );
    122178        }
    123179
    function wxr_filter_postmeta( $return_me, $meta_key ) { 
    374430        do_action( 'rss2_head' );
    375431        ?>
    376432
    377 <?php if ( $post_ids ) {
    378         global $wp_query;
     433<?php
     434        if ( $post_ids ) {
     435                $posts_per_page = 20;
     436
     437                $wp_query_args = array(
     438                        'post_type'      => 'any',
     439                        'post_status'    => 'any',
     440                        'posts_per_page' => $posts_per_page,
     441                        'post__in'       => array_slice( $post_ids, 0, $posts_per_page )
     442                );
     443
     444                $query = new WP_Query( $wp_query_args );
    379445
    380         // Fake being in the loop.
    381         $wp_query->in_the_loop = true;
     446                $page = 0;
    382447
    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" );
     448                // Paginate posts 20 at a time
     449                while ( $query->have_posts() ) {
     450                        // Begin Loop.
     451                        while ( $query->have_posts() ) {
     452                                $query->the_post();
    387453
    388         // Begin Loop.
    389         foreach ( $posts as $post ) {
    390                 setup_postdata( $post );
    391                 $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
     454                                $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
    392455?>
    393456        <item>
    394457                <title><?php
    function wxr_filter_postmeta( $return_me, $meta_key ) { 
    501564<?php   endforeach; ?>
    502565        </item>
    503566<?php
     567                        }
     568
     569                        // Fetch next set of posts
     570                        $wp_query_args['post__in'] = array_slice( $post_ids, ( $page * $posts_per_page ), $posts_per_page );
     571
     572                        $query->query( $wp_query_args );
     573
     574                        $page++;
     575                }
    504576        }
    505         }
    506 } ?>
     577?>
    507578</channel>
    508579</rss>
    509580<?php