Make WordPress Core

Ticket #15197: 15197.export-filtering.002.diff

File 15197.export-filtering.002.diff, 15.4 KB (added by duck_, 14 years ago)
  • wp-admin/export.php

     
    1616require_once('./includes/export.php');
    1717$title = __('Export');
    1818
    19 add_contextual_help($current_screen,
     19add_contextual_help( $current_screen,
    2020        '<p>' . __('You can export a file of your site&#8217;s content in order to import it into another installation or platform. The export file will be an XML file format called WXR. Posts, pages, comments, custom fields, categories, and tags can be included. You can set filters to have the WXR file only include a certain date, author, category, tag, all posts or all pages, certain publishing statuses.') . '</p>' .
    2121        '<p>' . __('Once generated, your WXR file can be imported by another WordPress site or by another blogging platform able to access this format.') . '</p>' .
    2222        '<p><strong>' . __('For more information:') . '</strong></p>' .
     
    2525);
    2626
    2727if ( isset( $_GET['download'] ) ) {
    28         export_wp();
     28        $args = array();
     29
     30        if ( ! isset( $_GET['content'] ) || 'all' == $_GET['content'] ) {
     31                $args['content'] = 'all';
     32        } else if ( 'posts' == $_GET['content'] ) {
     33                $args['content'] = 'post';
     34
     35                if ( $_GET['cat'] )
     36                        $args['category'] = (int) $_GET['cat'];
     37
     38                if ( $_GET['post_author'] )
     39                        $args['author'] = (int) $_GET['post_author'];
     40
     41                if ( $_GET['post_start_date'] || $_GET['post_end_date'] ) {
     42                        $args['start_date'] = $_GET['post_start_date'];
     43                        $args['end_date'] = $_GET['post_end_date'];
     44                }
     45
     46                if ( $_GET['post_status'] )
     47                        $args['status'] = $_GET['post_status'];
     48        } else if ( 'pages' == $_GET['content'] ) {
     49                $args['content'] = 'page';
     50
     51                if ( $_GET['page_author'] )
     52                        $args['author'] = (int) $_GET['page_author'];
     53
     54                if ( $_GET['page_start_date'] || $_GET['page_end_date'] ) {
     55                        $args['start_date'] = $_GET['page_start_date'];
     56                        $args['end_date'] = $_GET['page_end_date'];
     57                }
     58
     59                if ( $_GET['page_status'] )
     60                        $args['status'] = $_GET['page_status'];
     61        } else {
     62                $args['content'] = $_GET['content'];
     63        }
     64
     65        export_wp( $args );
    2966        die();
    3067}
    3168
    3269require_once ('admin-header.php');
    3370
    34 $dateoptions = $edateoptions = '';
    35 $types = "'" . implode("', '", get_post_types( array( 'public' => true, 'can_export' => true ), 'names' )) . "'";
    36 $stati = "'" . implode("', '", get_post_stati( array( 'internal' => false ), 'names' )) . "'";
    37 if ( $monthyears = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, YEAR(DATE_ADD(post_date, INTERVAL 1 MONTH)) AS `eyear`, MONTH(DATE_ADD(post_date, INTERVAL 1 MONTH)) AS `emonth` FROM $wpdb->posts WHERE post_type IN ($types) AND post_status IN ($stati) ORDER BY post_date ASC ") ) {
    38         foreach ( $monthyears as $k => $monthyear )
    39                 $monthyears[$k]->lmonth = $wp_locale->get_month( $monthyear->month, 2 );
    40         for( $s = 0, $e = count( $monthyears ) - 1; $e >= 0; $s++, $e-- ) {
    41                 $dateoptions .= "\t<option value=\"" . $monthyears[$s]->year . '-' . zeroise( $monthyears[$s]->month, 2 ) . '">' . $monthyears[$s]->lmonth . ' ' . $monthyears[$s]->year . "</option>\n";
    42                 $edateoptions .= "\t<option value=\"" . $monthyears[$e]->eyear . '-' . zeroise( $monthyears[$e]->emonth, 2 ) . '">' . $monthyears[$e]->lmonth . ' ' . $monthyears[$e]->year . "</option>\n";
     71function export_date_options() {
     72        global $wpdb, $wp_locale;
     73
     74        $months = $wpdb->get_results( "
     75                SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
     76                FROM $wpdb->posts
     77                WHERE post_type = 'post' AND post_status != 'auto-draft'
     78                ORDER BY post_date DESC
     79        " );
     80
     81        $month_count = count( $months );
     82        if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
     83                return;
     84
     85        foreach ( $months as $date ) {
     86                if ( 0 == $date->year )
     87                        continue;
     88
     89                $month = zeroise( $date->month, 2 );
     90                echo '<option value="' . $date->year . '-' . $month . '" />' . $wp_locale->get_month( $month ) . ' ' . $date->year . '</option>';
    4391        }
    4492}
    45 
    4693?>
    4794
    4895<div class="wrap">
     
    5198
    5299<p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
    53100<p><?php _e('This format, which we call WordPress eXtended RSS or WXR, will contain your posts, pages, comments, custom fields, categories, and tags.'); ?></p>
    54 <p><?php _e('Once you&#8217;ve saved the download file, you can use the Import function on another WordPress site to import this site.'); ?></p>
    55 <form action="" method="get">
    56 <?php submit_button( __('Download Export File'), 'button' ); ?>
     101<p><?php _e('Once you&#8217;ve saved the download file, you can use the Import function in another WordPress installation to import this site.'); ?></p>
     102
     103<h3><?php _e( 'Choose what to export' ); ?></h3>
     104<form action="" method="get" id="export-filters">
    57105<input type="hidden" name="download" value="true" />
    58 </p>
     106<p><label><input type="radio" name="content" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label>
     107<span class="description"><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></span></p>
     108
     109<p><label><input type="radio" name="content" value="posts" /> <?php _e( 'Posts' ); ?></label></p>
     110<ul id="post-filters" class="export-filters">
     111        <li>
     112                <label><?php _e( 'Categories:' ); ?></label>
     113                <?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?>
     114        </li>
     115        <li>
     116                <label><?php _e( 'Authors:' ); ?></label>
     117                <?php wp_dropdown_users( array( 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) ); ?>
     118        </li>
     119        <li>
     120                <label><?php _e( 'Date range:' ); ?></label>
     121                <select name="post_start_date">
     122                        <option value="0"><?php _e( 'Start Date' ); ?></option>
     123                        <?php export_date_options(); ?>
     124                </select>
     125                <select name="post_end_date">
     126                        <option value="0"><?php _e( 'End Date' ); ?></option>
     127                        <?php export_date_options(); ?>
     128                </select>
     129        </li>
     130        <li>
     131                <label><?php _e( 'Status:' ); ?></label>
     132                <select name="post_status">
     133                        <option value="0"><?php _e( 'All' ); ?></option>
     134                        <?php $post_stati = get_post_stati( array( 'internal' => false ), 'objects' );
     135                        foreach ( $post_stati as $status ) : ?>
     136                        <option value="<?php echo esc_attr( $status->name ); ?>"><?php echo esc_html( $status->label ); ?></option>
     137                        <?php endforeach; ?>
     138                </select>
     139        </li>
     140</ul>
     141
     142<p><label><input type="radio" name="content" value="pages" /> <?php _e( 'Pages' ); ?></label></p>
     143<ul id="page-filters" class="export-filters">
     144        <li>
     145                <label><?php _e( 'Author:' ); ?></label>
     146                <?php wp_dropdown_users( array( 'name' => 'page_author', 'multi' => true, 'show_option_all' => __('All') ) ); ?>
     147        </li>
     148        <li>
     149                <label><?php _e( 'Date range:' ); ?></label>
     150                <select name="page_start_date">
     151                        <option value="0"><?php _e( 'Start Date' ); ?></option>
     152                        <?php export_date_options(); ?>
     153                </select>
     154                <select name="page_end_date">
     155                        <option value="0"><?php _e( 'End Date' ); ?></option>
     156                        <?php export_date_options(); ?>
     157                </select>
     158        </li>
     159        <li>
     160                <label><?php _e( 'Status:' ); ?></label>
     161                <select name="page_status">
     162                        <option value="0"><?php _e( 'All' ); ?></option>
     163                        <?php foreach ( $post_stati as $status ) : ?>
     164                        <option value="<?php echo esc_attr( $status->name ); ?>"><?php echo esc_html( $status->label ); ?></option>
     165                        <?php endforeach; ?>
     166                </select>
     167        </li>
     168</ul>
     169
     170<?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?>
     171<p><label><input type="radio" name="content" value="<?php echo esc_attr( $post_type->name ); ?>" /> <?php echo esc_html( $post_type->label ); ?></label></p>
     172<?php endforeach; ?>
     173
     174<?php submit_button( __('Download Export File'), 'secondary' ); ?>
    59175</form>
    60176</div>
    61177
    62 <?php
    63 
    64 
    65 include ('admin-footer.php');
    66 ?>
     178<?php include('admin-footer.php'); ?>
  • wp-admin/includes/export.php

     
    2525function export_wp( $args = array() ) {
    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
    3035        $sitename = sanitize_key( get_bloginfo( 'name' ) );
     
    3540        header( 'Content-Disposition: attachment; filename=' . $filename );
    3641        header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    3742
    38         // 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" );
     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';
    4047
    41         $categories = (array) get_categories( array( 'get' => 'all' ) );
    42         $tags = (array) get_tags( array( 'get' => 'all' ) );
     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        }
    4354
    44         $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    45         $taxonomy_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
     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'";
    4659
    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;
     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                }
    5466        }
    5567
    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;
     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'])) ) );
    6377        }
    6478
     79        // grab a snapshot of post IDs, just in case it changes during the export
     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 );
     112        }
     113
    65114        /**
    66115         * Wrap given string in XML CDATA tag.
    67116         *
     
    271320        <title><?php bloginfo_rss( 'name' ); ?></title>
    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>
    277326        <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
     
    288337<?php foreach ( $terms as $t ) : ?>
    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' ); ?>
    294343
     
    299348        // fetch 20 posts at a time rather than loading the entire table into memory
    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
    305354        foreach ( $posts as $post ) {
     
    332381<?php   endif; ?>
    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>
    338387                        <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  • wp-includes/post.php

     
    5454                'hierarchical' => false,
    5555                'rewrite' => false,
    5656                'query_var' => false,
    57                 'can_export' => false,
    5857                'show_in_nav_menus' => false,
    5958        ) );
    6059
     
    7170                'hierarchical' => false,
    7271                'rewrite' => false,
    7372                'query_var' => false,
     73                'can_export' => false,
    7474        ) );
    7575
    7676        register_post_type( 'nav_menu_item', array(
  • wp-admin/css/wp-admin.dev.css

     
    25132513
    25142514#front-page-warning,
    25152515#front-static-pages ul,
     2516ul.export-filters,
    25162517.inline-editor ul.cat-checklist ul,
    25172518.categorydiv ul.categorychecklist ul,
    25182519.customlinkdiv ul.categorychecklist ul,