Make WordPress Core


Ignore:
Timestamp:
12/01/2010 05:21:58 PM (13 years ago)
Author:
ryan
Message:

Export filtering. Props duck_. fixes #15197

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/export.php

    r15961 r16652  
    2626    global $wpdb, $post;
    2727
     28    $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
     29        'start_date' => false, 'end_date' => false, 'status' => false,
     30    );
     31    $args = wp_parse_args( $args, $defaults );
     32
    2833    do_action( 'export_wp' );
    2934
     
    3641    header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    3742
     43    if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
     44        $ptype = get_post_type_object( $args['content'] );
     45        if ( ! $ptype->can_export )
     46            $args['content'] = 'post';
     47
     48        $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
     49    } else {
     50        $post_types = get_post_types( array( 'can_export' => true ) );
     51        $esses = array_fill( 0, count($post_types), '%s' );
     52        $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (". implode(',',$esses) .")", $post_types );
     53    }
     54
     55    if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
     56        $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
     57    else
     58        $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
     59
     60    $join = '';
     61    if ( $args['category'] && 'post' == $args['content'] ) {
     62        if ( $term = term_exists( $args['category'], 'category' ) ) {
     63            $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
     64            $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_id'] );
     65        }
     66    }
     67
     68    if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
     69        if ( $args['author'] )
     70            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
     71
     72        if ( $args['start_date'] )
     73            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
     74
     75        if ( $args['end_date'] )
     76            $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
     77    }
     78
    3879    // grab a snapshot of post IDs, just in case it changes during the export
    39     $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type != 'revision' AND post_status != 'auto-draft' ORDER BY post_date_gmt ASC" );
    40 
    41     $categories = (array) get_categories( array( 'get' => 'all' ) );
    42     $tags = (array) get_tags( array( 'get' => 'all' ) );
    43 
    44     $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    45     $taxonomy_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
    46 
    47     // put categories in order with no child going before its parent
    48     $cats = array();
    49     while ( $cat = array_shift( $categories ) ) {
    50         if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
    51             $cats[$cat->term_id] = $cat;
    52         else
    53             $categories[] = $cat;
    54     }
    55 
    56     // put terms in order with no child going before its parent
    57     $terms = array();
    58     while ( $t = array_shift( $taxonomy_terms ) ) {
    59         if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
    60             $terms[$t->term_id] = $t;
    61         else
    62             $taxonomy_terms[] = $t;
     80    $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
     81
     82    // get the requested terms ready, empty unless posts filtered by category or all content
     83    $cats = $tags = $terms = array();
     84    if ( isset( $term ) && $term ) {
     85        $cat = get_term( $term['term_id'], 'category' );
     86        $cats = array( $cat->term_id => $cat );
     87        unset( $term, $cat );
     88    } else if ( 'all' == $args['content'] ) {
     89        $categories = (array) get_categories( array( 'get' => 'all' ) );
     90        $tags = (array) get_tags( array( 'get' => 'all' ) );
     91
     92        $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
     93        $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
     94
     95        // put categories in order with no child going before its parent
     96        while ( $cat = array_shift( $categories ) ) {
     97            if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
     98                $cats[$cat->term_id] = $cat;
     99            else
     100                $categories[] = $cat;
     101        }
     102
     103        // put terms in order with no child going before its parent
     104        while ( $t = array_shift( $custom_terms ) ) {
     105            if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
     106                $terms[$t->term_id] = $t;
     107            else
     108                $custom_terms[] = $t;
     109        }
     110
     111        unset( $categories, $custom_taxonomies, $custom_terms );
    63112    }
    64113
     
    272321    <link><?php bloginfo_rss( 'url' ); ?></link>
    273322    <description><?php bloginfo_rss( 'description' ); ?></description>
    274     <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate>
     323    <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
    275324    <language><?php echo get_option( 'rss_language' ); ?></language>
    276325    <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
     
    289338    <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>
    290339<?php endforeach; ?>
    291 <?php wxr_nav_menu_terms(); ?>
     340<?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
    292341
    293342    <?php do_action( 'rss2_head' ); ?>
     
    300349    while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
    301350    $where = "WHERE ID IN (" . join( ',', $next_posts ) . ")";
    302     $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
     351    $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
    303352
    304353    // Begin Loop
     
    333382<?php   wxr_post_taxonomy(); ?>
    334383<?php   $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
    335         if ( $postmeta ) : foreach( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' && $meta->meta_key != '_edit_last' ) : ?>
     384        if ( $postmeta ) : foreach( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' ) : ?>
    336385        <wp:postmeta>
    337386            <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
Note: See TracChangeset for help on using the changeset viewer.