WordPress.org

Make WordPress Core

Ticket #15197: 15197.export-filtering.diff

File 15197.export-filtering.diff, 12.8 KB (added by duck_, 3 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 ( isset( $_GET['limit_category'] ) ) 
     36                        $args['category'] = (int) $_GET['cat']; 
     37 
     38                if ( isset( $_GET['post_limit_author'] ) ) 
     39                        $args['author'] = (int) $_GET['post_author']; 
     40 
     41                if ( isset( $_GET['post_limit_date'] ) ) { 
     42                        $args['start_date'] = $_GET['post_start_date']; 
     43                        $args['end_date'] = $_GET['post_end_date']; 
     44                } 
     45 
     46                $args['published'] = isset( $_GET['published_posts'] ); 
     47        } else if ( 'pages' == $_GET['content'] ) { 
     48                $args['content'] = 'page'; 
     49 
     50                if ( isset( $_GET['page_limit_author'] ) ) 
     51                        $args['author'] = (int) $_GET['page_author']; 
     52 
     53                $args['published'] = isset( $_GET['published_pages'] ); 
     54        } else { 
     55                $args['content'] = $_GET['content']; 
     56        } 
     57 
     58        export_wp( $args ); 
    2959        die(); 
    3060} 
    3161 
    3262require_once ('admin-header.php'); 
    3363 
    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"; 
     64function export_date_options() { 
     65        global $wpdb, $wp_locale; 
     66 
     67        $months = $wpdb->get_results( " 
     68                SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month 
     69                FROM $wpdb->posts 
     70                WHERE post_type = 'post' AND post_status != 'auto-draft' 
     71                ORDER BY post_date DESC 
     72        " ); 
     73 
     74        $month_count = count( $months ); 
     75        if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) 
     76                return; 
     77 
     78        foreach ( $months as $date ) { 
     79                if ( 0 == $date->year ) 
     80                        continue; 
     81 
     82                $month = zeroise( $date->month, 2 ); 
     83                echo '<option value="' . $date->year . '-' . $month . '" />' . $wp_locale->get_month( $month ) . ' ' . $date->year . '</option>'; 
    4384        } 
    4485} 
    45  
    4686?> 
    4787 
    4888<div class="wrap"> 
     
    5292<p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p> 
    5393<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> 
    5494<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> 
     95 
     96<h3><?php _e( 'Choose what to export' ); ?></h3> 
    5597<form action="" method="get"> 
    56 <?php submit_button( __('Download Export File'), 'button' ); ?> 
    5798<input type="hidden" name="download" value="true" /> 
    58 </p> 
     99<table class="form-table"> 
     100        <tr> 
     101                <th scope="row"><label><input type="radio" name="content" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label></th> 
     102                <td><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></td> 
     103        </tr> 
     104 
     105        <tr> 
     106                <th scope="row"><label><input type="radio" name="content" value="posts" /><?php _e( 'Posts' ); ?></label></th> 
     107                <td> 
     108                        <fieldset> 
     109                                <label><input type="checkbox" name="limit_category" value="1" /> <?php _e( 'Limit by category' ); ?></label> 
     110                                        <?php wp_dropdown_categories(); ?><br /> 
     111 
     112                                <label><input type="checkbox" name="post_limit_author" value="1" /> <?php _e( 'Limit by author' ); ?></label> 
     113                                        <?php wp_dropdown_users( array( 'name' => 'post_author', 'multi' => true ) ); ?><br /> 
     114 
     115                                <label><input type="checkbox" name="post_limit_date" value="1" /> <?php _e( 'Limit by date' ); ?></label> 
     116                                        <select name="post_start_date"> 
     117                                                <option value="0"><?php _e( 'Start Date' ); ?></option> 
     118                                                <?php export_date_options(); ?> 
     119                                        </select> 
     120                                        <select name="post_end_date"> 
     121                                                <option value="0"><?php _e( 'End Date' ); ?></option> 
     122                                                <?php export_date_options(); ?> 
     123                                        </select><br /> 
     124 
     125                                <label><input type="checkbox" name="published_posts" value="1" /> <?php _e( 'Published posts only' ); ?></label> 
     126                        </fieldset> 
     127                </td> 
     128        </tr> 
     129 
     130        <tr> 
     131                <th scope="row"><label><input type="radio" name="content" value="pages" /><?php _e( 'Pages' ); ?></label></th> 
     132                <td> 
     133                        <fieldset> 
     134                                <label><input type="checkbox" name="page_limit_author" value="1" /> <?php _e( 'Limit by author' ); ?></label> 
     135                                        <?php wp_dropdown_users( array( 'name' => 'page_author', 'multi' => true ) ); ?><br /> 
     136 
     137                                <label><input type="checkbox" name="published_pages" value="1" /> <?php _e( 'Published pages only' ); ?></label> 
     138                        </fieldset> 
     139                </td> 
     140        </tr> 
     141 
     142<?php foreach ( get_post_types( array( '_builtin' => false, 'public' => true, 'can_export' => true ), 'objects' ) as $post_type ) : ?> 
     143        <tr> 
     144                <th scope="row"><label><input type="radio" name="content" value="<?php echo $post_type->name; ?>" /><?php echo $post_type->label; ?></label></th> 
     145                <td></td> 
     146        </tr> 
     147<?php endforeach; ?> 
     148</table> 
     149 
     150<?php submit_button( __('Download Export File'), 'secondary' ); ?> 
     151 
    59152</form> 
    60153</div> 
    61154 
    62 <?php 
    63  
    64  
    65 include ('admin-footer.php'); 
    66 ?> 
     155<?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, 'published' => 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        $join = ''; 
     44        if ( $args['published'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) 
     45                $where = "{$wpdb->posts}.post_status = 'publish'"; 
     46        else 
     47                $where = "{$wpdb->posts}.post_status != 'auto-draft'"; 
    4048 
    41         $categories = (array) get_categories( array( 'get' => 'all' ) ); 
    42         $tags = (array) get_tags( array( 'get' => 'all' ) ); 
     49        if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) { 
     50                $ptype = get_post_type_object( $args['content'] ); 
     51                if ( ! $ptype->can_export ) 
     52                        $args['content'] = 'post'; 
    4353 
    44         $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) ); 
    45         $taxonomy_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); 
     54                $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $args['content'] ); 
    4655 
    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; 
     56                if ( 'post' == $args['content'] ) { 
     57                        if ( $args['category'] && ($term = term_exists( $args['category'], 'category' )) ) { 
     58                                $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)"; 
     59                                $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_id'] ); 
     60                        } 
     61 
     62                        if ( $args['start_date'] ) 
     63                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) ); 
     64 
     65                        if ( $args['end_date'] ) 
     66                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) ); 
     67                } 
     68        } else { 
     69                $where .= " AND {$wpdb->posts}.post_type != 'revision'"; 
    5470        } 
    5571 
    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; 
     72        if ( $args['author'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) 
     73                $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] ); 
     74 
     75        // grab a snapshot of post IDs, just in case it changes during the export 
     76        $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" ); 
     77 
     78        // get the requested terms ready, empty unless posts filtered by category or all content 
     79        $cats = $tags = $terms = array(); 
     80        if ( isset( $term ) && $term ) { 
     81                $cat = get_term( $term['term_id'], 'category' ); 
     82                $cats = array( $cat->term_id => $cat ); 
     83                unset( $term, $cat ); 
     84        } else if ( 'all' == $args['content'] ) { 
     85                $categories = (array) get_categories( array( 'get' => 'all' ) ); 
     86                $tags = (array) get_tags( array( 'get' => 'all' ) ); 
     87 
     88                $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) ); 
     89                $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); 
     90 
     91                // put categories in order with no child going before its parent 
     92                while ( $cat = array_shift( $categories ) ) { 
     93                        if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) ) 
     94                                $cats[$cat->term_id] = $cat; 
     95                        else 
     96                                $categories[] = $cat; 
     97                } 
     98 
     99                // put terms in order with no child going before its parent 
     100                while ( $t = array_shift( $custom_terms ) ) { 
     101                        if ( $t->parent == 0 || isset( $terms[$t->parent] ) ) 
     102                                $terms[$t->term_id] = $t; 
     103                        else 
     104                                $custom_terms[] = $t; 
     105                } 
     106 
     107                unset( $categories, $custom_taxonomies, $custom_terms ); 
    63108        } 
    64109 
    65110        /** 
     
    271316        <title><?php bloginfo_rss( 'name' ); ?></title> 
    272317        <link><?php bloginfo_rss( 'url' ); ?></link> 
    273318        <description><?php bloginfo_rss( 'description' ); ?></description> 
    274         <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate> 
     319        <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate> 
    275320        <language><?php echo get_option( 'rss_language' ); ?></language> 
    276321        <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version> 
    277322        <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url> 
     
    288333<?php foreach ( $terms as $t ) : ?> 
    289334        <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> 
    290335<?php endforeach; ?> 
    291 <?php wxr_nav_menu_terms(); ?> 
     336<?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?> 
    292337 
    293338        <?php do_action( 'rss2_head' ); ?> 
    294339 
     
    299344        // fetch 20 posts at a time rather than loading the entire table into memory 
    300345        while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) { 
    301346        $where = "WHERE ID IN (" . join( ',', $next_posts ) . ")"; 
    302         $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" ); 
     347        $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" ); 
    303348 
    304349        // Begin Loop 
    305350        foreach ( $posts as $post ) {