Make WordPress Core

Ticket #28146: 28146.3.diff

File 28146.3.diff, 6.8 KB (added by sc0ttkclark, 10 years ago)

Refactor export_wp to use a WP_Query loop, add filter for $query_args to override from export_filters field input

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

    function export_wp( $args = array() ) { 
    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        $query_args = array(
     51                'post_status' => 'any',
     52                'posts_per_page' => -1
     53        );
     54
     55        if ( 'all' != $args['content'] ) {
     56                $args['content'] = (array) $args['content'];
     57
     58                $query_args['post_type'] = array();
     59
     60                foreach( $args['content'] as $post_type ) {
     61                        $post_type_object = get_post_type_object( $post_type );
     62
     63                        if ( $post_type_object && $post_type_object->can_export ) {
     64                                $query_args['post_type'][] = $post_type;
     65                        }
     66                }
    5467
    55                 $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
     68                if ( empty( $query_args['post_type'] ) ) {
     69                        $query_args['post_type'] = 'post';
     70                }
    5671        } 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 );
     72                $query_args['post_type'] = array_values( get_post_types( array( 'can_export' => true ) ) );
    6073        }
    6174
    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'";
     75        if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) {
     76                $query_args['post_status'] = $args['status'];
     77        }
    6678
    67         $join = '';
    6879        if ( $args['category'] && 'post' == $args['content'] ) {
    6980                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'] );
     81                        $query_args['tax_query'] = array(
     82                                array(
     83                                        'taxonomy' => 'category',
     84                                        'terms' => $term->term_id,
     85                                        'include_children' => false
     86                                )
     87                        );
    7288                }
    7389        }
    7490
    7591        if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
    7692                if ( $args['author'] )
    77                         $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
     93                        $query_args['author'] = (int) $args['author'];
     94
     95                $date_query = array();
    7896
    7997                if ( $args['start_date'] )
    80                         $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
     98                        $date_query[]['after'] = $args['start_date'];
    8199
    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'])) ) );
     100                if ( $args['end_date'] ) {
     101                        $date_query[]['before'] =  '@' . strtotime( '+1 month', strtotime( $args['end_date'] ) );
     102                }
     103
     104                if ( ! empty( $date_query ) ) {
     105                        $query_args['date_query'] = $date_query;
     106                }
    84107        }
    85108
    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" );
     109        /**
     110         * Filter the query arguments for the exported data
     111         *
     112         * @todo Update since
     113         * @since 4.X
     114         *
     115         * @param array $query_args WP_Query args to use for the exported data.
     116         * @param array $args Filters defining what should be included in the export.
     117         */
     118        $query_args = apply_filters( 'export_wp_query_args', $query_args, $args );
     119
     120        $post_query_args = $query_args;
     121        $post_query_args['fields'] = 'ids';
     122
     123        $post_ids = get_posts( $post_query_args );
    88124
    89125        /*
    90126         * Get the requested terms ready, empty unless posts filtered by category
    function export_wp( $args = array() ) { 
    95131                $cat = get_term( $term['term_id'], 'category' );
    96132                $cats = array( $cat->term_id => $cat );
    97133                unset( $term, $cat );
    98         } elseif ( 'all' == $args['content'] ) {
     134        } else if ( 'all' == $args['content'] ) {
    99135                $categories = (array) get_categories( array( 'get' => 'all' ) );
    100136                $tags = (array) get_tags( array( 'get' => 'all' ) );
    101137
    102138                $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    103139                $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
    104140
    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;
     141                // Put categories in order with no child going before its parent
     142                if( $categories ) {
     143                        while ( $cat = array_shift( $categories ) ) {
     144                                if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) {
     145                                        $cats[$cat->term_id] = $cat;
     146                                } else {
     147                                        $categories[] = $cat;
     148                                }
     149                        }
    111150                }
    112151
    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;
     152                // Put terms in order with no child going before its parent
     153                if( $custom_terms ) {
     154                        while ( $t = array_shift( $custom_terms ) ) {
     155                                if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) {
     156                                        $terms[$t->term_id] = $t;
     157                                } else {
     158                                        $custom_terms[] = $t;
     159                                }
     160                        }
    119161                }
    120162
     163                // Clean up
    121164                unset( $categories, $custom_taxonomies, $custom_terms );
    122165        }
    123166
    function wxr_filter_postmeta( $return_me, $meta_key ) { 
    375418        ?>
    376419
    377420<?php if ( $post_ids ) {
    378         global $wp_query;
     421        $query_args['posts_per_page'] = 20;
     422        $query_args['paged'] = 1;
    379423
    380         // Fake being in the loop.
    381         $wp_query->in_the_loop = true;
     424        $query = new WP_Query( $query_args );
    382425
    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" );
     426        // Paginate posts 20 at a time
     427        while ( $query->have_posts() ) {
     428                // Begin Loop.
     429                while ( $query->have_posts() ) {
     430                        $query->the_post();
    387431
    388         // Begin Loop.
    389         foreach ( $posts as $post ) {
    390                 setup_postdata( $post );
    391                 $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
     432                        $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
    392433?>
    393434        <item>
    394435                <title><?php
    function wxr_filter_postmeta( $return_me, $meta_key ) { 
    501542<?php   endforeach; ?>
    502543        </item>
    503544<?php
    504         }
     545                }
     546
     547                // Fetch next page of posts
     548                $query_args['paged']++;
     549
     550                $query->query( $query_args );
    505551        }
    506552} ?>
    507553</channel>