Ticket #15197: 15197.export-filtering.diff

File 15197.export-filtering.diff, 12.8 KB (added by duck_, 2 years ago)
Line 
1Index: wp-admin/export.php
2===================================================================
3--- wp-admin/export.php (revision 16566)
4+++ wp-admin/export.php (working copy)
5@@ -16,7 +16,7 @@
6 require_once('./includes/export.php');
7 $title = __('Export');
8 
9-add_contextual_help($current_screen,
10+add_contextual_help( $current_screen,
11        '<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>' .
12        '<p>' . __('Once generated, your WXR file can be imported by another WordPress site or by another blogging platform able to access this format.') . '</p>' .
13        '<p><strong>' . __('For more information:') . '</strong></p>' .
14@@ -25,24 +25,64 @@
15 );
16 
17 if ( isset( $_GET['download'] ) ) {
18-       export_wp();
19+       $args = array();
20+
21+       if ( ! isset( $_GET['content'] ) || 'all' == $_GET['content'] ) {
22+               $args['content'] = 'all';
23+       } else if ( 'posts' == $_GET['content'] ) {
24+               $args['content'] = 'post';
25+
26+               if ( isset( $_GET['limit_category'] ) )
27+                       $args['category'] = (int) $_GET['cat'];
28+
29+               if ( isset( $_GET['post_limit_author'] ) )
30+                       $args['author'] = (int) $_GET['post_author'];
31+
32+               if ( isset( $_GET['post_limit_date'] ) ) {
33+                       $args['start_date'] = $_GET['post_start_date'];
34+                       $args['end_date'] = $_GET['post_end_date'];
35+               }
36+
37+               $args['published'] = isset( $_GET['published_posts'] );
38+       } else if ( 'pages' == $_GET['content'] ) {
39+               $args['content'] = 'page';
40+
41+               if ( isset( $_GET['page_limit_author'] ) )
42+                       $args['author'] = (int) $_GET['page_author'];
43+
44+               $args['published'] = isset( $_GET['published_pages'] );
45+       } else {
46+               $args['content'] = $_GET['content'];
47+       }
48+
49+       export_wp( $args );
50        die();
51 }
52 
53 require_once ('admin-header.php');
54 
55-$dateoptions = $edateoptions = '';
56-$types = "'" . implode("', '", get_post_types( array( 'public' => true, 'can_export' => true ), 'names' )) . "'";
57-$stati = "'" . implode("', '", get_post_stati( array( 'internal' => false ), 'names' )) . "'";
58-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 ") ) {
59-       foreach ( $monthyears as $k => $monthyear )
60-               $monthyears[$k]->lmonth = $wp_locale->get_month( $monthyear->month, 2 );
61-       for( $s = 0, $e = count( $monthyears ) - 1; $e >= 0; $s++, $e-- ) {
62-               $dateoptions .= "\t<option value=\"" . $monthyears[$s]->year . '-' . zeroise( $monthyears[$s]->month, 2 ) . '">' . $monthyears[$s]->lmonth . ' ' . $monthyears[$s]->year . "</option>\n";
63-               $edateoptions .= "\t<option value=\"" . $monthyears[$e]->eyear . '-' . zeroise( $monthyears[$e]->emonth, 2 ) . '">' . $monthyears[$e]->lmonth . ' ' . $monthyears[$e]->year . "</option>\n";
64+function 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>';
84        }
85 }
86-
87 ?>
88 
89 <div class="wrap">
90@@ -52,15 +92,64 @@
91 <p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
92 <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>
93 <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>
94+
95+<h3><?php _e( 'Choose what to export' ); ?></h3>
96 <form action="" method="get">
97-<?php submit_button( __('Download Export File'), 'button' ); ?>
98 <input type="hidden" name="download" value="true" />
99-</p>
100+<table class="form-table">
101+       <tr>
102+               <th scope="row"><label><input type="radio" name="content" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label></th>
103+               <td><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></td>
104+       </tr>
105+
106+       <tr>
107+               <th scope="row"><label><input type="radio" name="content" value="posts" /><?php _e( 'Posts' ); ?></label></th>
108+               <td>
109+                       <fieldset>
110+                               <label><input type="checkbox" name="limit_category" value="1" /> <?php _e( 'Limit by category' ); ?></label>
111+                                       <?php wp_dropdown_categories(); ?><br />
112+
113+                               <label><input type="checkbox" name="post_limit_author" value="1" /> <?php _e( 'Limit by author' ); ?></label>
114+                                       <?php wp_dropdown_users( array( 'name' => 'post_author', 'multi' => true ) ); ?><br />
115+
116+                               <label><input type="checkbox" name="post_limit_date" value="1" /> <?php _e( 'Limit by date' ); ?></label>
117+                                       <select name="post_start_date">
118+                                               <option value="0"><?php _e( 'Start Date' ); ?></option>
119+                                               <?php export_date_options(); ?>
120+                                       </select>
121+                                       <select name="post_end_date">
122+                                               <option value="0"><?php _e( 'End Date' ); ?></option>
123+                                               <?php export_date_options(); ?>
124+                                       </select><br />
125+
126+                               <label><input type="checkbox" name="published_posts" value="1" /> <?php _e( 'Published posts only' ); ?></label>
127+                       </fieldset>
128+               </td>
129+       </tr>
130+
131+       <tr>
132+               <th scope="row"><label><input type="radio" name="content" value="pages" /><?php _e( 'Pages' ); ?></label></th>
133+               <td>
134+                       <fieldset>
135+                               <label><input type="checkbox" name="page_limit_author" value="1" /> <?php _e( 'Limit by author' ); ?></label>
136+                                       <?php wp_dropdown_users( array( 'name' => 'page_author', 'multi' => true ) ); ?><br />
137+
138+                               <label><input type="checkbox" name="published_pages" value="1" /> <?php _e( 'Published pages only' ); ?></label>
139+                       </fieldset>
140+               </td>
141+       </tr>
142+
143+<?php foreach ( get_post_types( array( '_builtin' => false, 'public' => true, 'can_export' => true ), 'objects' ) as $post_type ) : ?>
144+       <tr>
145+               <th scope="row"><label><input type="radio" name="content" value="<?php echo $post_type->name; ?>" /><?php echo $post_type->label; ?></label></th>
146+               <td></td>
147+       </tr>
148+<?php endforeach; ?>
149+</table>
150+
151+<?php submit_button( __('Download Export File'), 'secondary' ); ?>
152+
153 </form>
154 </div>
155 
156-<?php
157-
158-
159-include ('admin-footer.php');
160-?>
161+<?php include ('admin-footer.php'); ?>
162Index: wp-admin/includes/export.php
163===================================================================
164--- wp-admin/includes/export.php        (revision 16566)
165+++ wp-admin/includes/export.php        (working copy)
166@@ -25,6 +25,11 @@
167 function export_wp( $args = array() ) {
168        global $wpdb, $post;
169 
170+       $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
171+               'start_date' => false, 'end_date' => false, 'published' => false,
172+       );
173+       $args = wp_parse_args( $args, $defaults );
174+
175        do_action( 'export_wp' );
176 
177        $sitename = sanitize_key( get_bloginfo( 'name' ) );
178@@ -35,31 +40,71 @@
179        header( 'Content-Disposition: attachment; filename=' . $filename );
180        header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
181 
182-       // grab a snapshot of post IDs, just in case it changes during the export
183-       $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" );
184+       $join = '';
185+       if ( $args['published'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
186+               $where = "{$wpdb->posts}.post_status = 'publish'";
187+       else
188+               $where = "{$wpdb->posts}.post_status != 'auto-draft'";
189 
190-       $categories = (array) get_categories( array( 'get' => 'all' ) );
191-       $tags = (array) get_tags( array( 'get' => 'all' ) );
192+       if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
193+               $ptype = get_post_type_object( $args['content'] );
194+               if ( ! $ptype->can_export )
195+                       $args['content'] = 'post';
196 
197-       $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
198-       $taxonomy_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
199+               $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $args['content'] );
200 
201-       // put categories in order with no child going before its parent
202-       $cats = array();
203-       while ( $cat = array_shift( $categories ) ) {
204-               if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
205-                       $cats[$cat->term_id] = $cat;
206-               else
207-                       $categories[] = $cat;
208+               if ( 'post' == $args['content'] ) {
209+                       if ( $args['category'] && ($term = term_exists( $args['category'], 'category' )) ) {
210+                               $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
211+                               $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_id'] );
212+                       }
213+
214+                       if ( $args['start_date'] )
215+                               $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
216+
217+                       if ( $args['end_date'] )
218+                               $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
219+               }
220+       } else {
221+               $where .= " AND {$wpdb->posts}.post_type != 'revision'";
222        }
223 
224-       // put terms in order with no child going before its parent
225-       $terms = array();
226-       while ( $t = array_shift( $taxonomy_terms ) ) {
227-               if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
228-                       $terms[$t->term_id] = $t;
229-               else
230-                       $taxonomy_terms[] = $t;
231+       if ( $args['author'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
232+               $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
233+
234+       // grab a snapshot of post IDs, just in case it changes during the export
235+       $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
236+
237+       // get the requested terms ready, empty unless posts filtered by category or all content
238+       $cats = $tags = $terms = array();
239+       if ( isset( $term ) && $term ) {
240+               $cat = get_term( $term['term_id'], 'category' );
241+               $cats = array( $cat->term_id => $cat );
242+               unset( $term, $cat );
243+       } else if ( 'all' == $args['content'] ) {
244+               $categories = (array) get_categories( array( 'get' => 'all' ) );
245+               $tags = (array) get_tags( array( 'get' => 'all' ) );
246+
247+               $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
248+               $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
249+
250+               // put categories in order with no child going before its parent
251+               while ( $cat = array_shift( $categories ) ) {
252+                       if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
253+                               $cats[$cat->term_id] = $cat;
254+                       else
255+                               $categories[] = $cat;
256+               }
257+
258+               // put terms in order with no child going before its parent
259+               while ( $t = array_shift( $custom_terms ) ) {
260+                       if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
261+                               $terms[$t->term_id] = $t;
262+                       else
263+                               $custom_terms[] = $t;
264+               }
265+
266+               unset( $categories, $custom_taxonomies, $custom_terms );
267        }
268 
269        /**
270@@ -271,7 +316,7 @@
271        <title><?php bloginfo_rss( 'name' ); ?></title>
272        <link><?php bloginfo_rss( 'url' ); ?></link>
273        <description><?php bloginfo_rss( 'description' ); ?></description>
274-       <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate>
275+       <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
276        <language><?php echo get_option( 'rss_language' ); ?></language>
277        <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
278        <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
279@@ -288,7 +333,7 @@
280 <?php foreach ( $terms as $t ) : ?>
281        <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>
282 <?php endforeach; ?>
283-<?php wxr_nav_menu_terms(); ?>
284+<?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
285 
286        <?php do_action( 'rss2_head' ); ?>
287 
288@@ -299,7 +344,7 @@
289        // fetch 20 posts at a time rather than loading the entire table into memory
290        while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
291        $where = "WHERE ID IN (" . join( ',', $next_posts ) . ")";
292-       $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
293+       $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
294 
295        // Begin Loop
296        foreach ( $posts as $post ) {