wp/wp-admin/includes/export.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    21  * Default behavior is to export all content, however, note that post content will only
    21  * Default behavior is to export all content, however, note that post content will only
    22  * be exported for post types with the `can_export` argument enabled. Any posts with the
    22  * be exported for post types with the `can_export` argument enabled. Any posts with the
    23  * 'auto-draft' status will be skipped.
    23  * 'auto-draft' status will be skipped.
    24  *
    24  *
    25  * @since 2.1.0
    25  * @since 2.1.0
       
    26  * @since 5.7.0 Added the `post_modified` and `post_modified_gmt` fields to the export file.
    26  *
    27  *
    27  * @global wpdb    $wpdb WordPress database abstraction object.
    28  * @global wpdb    $wpdb WordPress database abstraction object.
    28  * @global WP_Post $post Global post object.
    29  * @global WP_Post $post Global post object.
    29  *
    30  *
    30  * @param array $args {
    31  * @param array $args {
   166 			)
   167 			)
   167 		);
   168 		);
   168 
   169 
   169 		// Put categories in order with no child going before its parent.
   170 		// Put categories in order with no child going before its parent.
   170 		while ( $cat = array_shift( $categories ) ) {
   171 		while ( $cat = array_shift( $categories ) ) {
   171 			if ( 0 == $cat->parent || isset( $cats[ $cat->parent ] ) ) {
   172 			if ( ! $cat->parent || isset( $cats[ $cat->parent ] ) ) {
   172 				$cats[ $cat->term_id ] = $cat;
   173 				$cats[ $cat->term_id ] = $cat;
   173 			} else {
   174 			} else {
   174 				$categories[] = $cat;
   175 				$categories[] = $cat;
   175 			}
   176 			}
   176 		}
   177 		}
   177 
   178 
   178 		// Put terms in order with no child going before its parent.
   179 		// Put terms in order with no child going before its parent.
   179 		while ( $t = array_shift( $custom_terms ) ) {
   180 		while ( $t = array_shift( $custom_terms ) ) {
   180 			if ( 0 == $t->parent || isset( $terms[ $t->parent ] ) ) {
   181 			if ( ! $t->parent || isset( $terms[ $t->parent ] ) ) {
   181 				$terms[ $t->term_id ] = $t;
   182 				$terms[ $t->term_id ] = $t;
   182 			} else {
   183 			} else {
   183 				$custom_terms[] = $t;
   184 				$custom_terms[] = $t;
   184 			}
   185 			}
   185 		}
   186 		}
   225 	/**
   226 	/**
   226 	 * Output a cat_name XML tag from a given category object
   227 	 * Output a cat_name XML tag from a given category object
   227 	 *
   228 	 *
   228 	 * @since 2.1.0
   229 	 * @since 2.1.0
   229 	 *
   230 	 *
   230 	 * @param object $category Category Object
   231 	 * @param WP_Term $category Category Object
   231 	 */
   232 	 */
   232 	function wxr_cat_name( $category ) {
   233 	function wxr_cat_name( $category ) {
   233 		if ( empty( $category->name ) ) {
   234 		if ( empty( $category->name ) ) {
   234 			return;
   235 			return;
   235 		}
   236 		}
   240 	/**
   241 	/**
   241 	 * Output a category_description XML tag from a given category object
   242 	 * Output a category_description XML tag from a given category object
   242 	 *
   243 	 *
   243 	 * @since 2.1.0
   244 	 * @since 2.1.0
   244 	 *
   245 	 *
   245 	 * @param object $category Category Object
   246 	 * @param WP_Term $category Category Object
   246 	 */
   247 	 */
   247 	function wxr_category_description( $category ) {
   248 	function wxr_category_description( $category ) {
   248 		if ( empty( $category->description ) ) {
   249 		if ( empty( $category->description ) ) {
   249 			return;
   250 			return;
   250 		}
   251 		}
   255 	/**
   256 	/**
   256 	 * Output a tag_name XML tag from a given tag object
   257 	 * Output a tag_name XML tag from a given tag object
   257 	 *
   258 	 *
   258 	 * @since 2.3.0
   259 	 * @since 2.3.0
   259 	 *
   260 	 *
   260 	 * @param object $tag Tag Object
   261 	 * @param WP_Term $tag Tag Object
   261 	 */
   262 	 */
   262 	function wxr_tag_name( $tag ) {
   263 	function wxr_tag_name( $tag ) {
   263 		if ( empty( $tag->name ) ) {
   264 		if ( empty( $tag->name ) ) {
   264 			return;
   265 			return;
   265 		}
   266 		}
   270 	/**
   271 	/**
   271 	 * Output a tag_description XML tag from a given tag object
   272 	 * Output a tag_description XML tag from a given tag object
   272 	 *
   273 	 *
   273 	 * @since 2.3.0
   274 	 * @since 2.3.0
   274 	 *
   275 	 *
   275 	 * @param object $tag Tag Object
   276 	 * @param WP_Term $tag Tag Object
   276 	 */
   277 	 */
   277 	function wxr_tag_description( $tag ) {
   278 	function wxr_tag_description( $tag ) {
   278 		if ( empty( $tag->description ) ) {
   279 		if ( empty( $tag->description ) ) {
   279 			return;
   280 			return;
   280 		}
   281 		}
   285 	/**
   286 	/**
   286 	 * Output a term_name XML tag from a given term object
   287 	 * Output a term_name XML tag from a given term object
   287 	 *
   288 	 *
   288 	 * @since 2.9.0
   289 	 * @since 2.9.0
   289 	 *
   290 	 *
   290 	 * @param object $term Term Object
   291 	 * @param WP_Term $term Term Object
   291 	 */
   292 	 */
   292 	function wxr_term_name( $term ) {
   293 	function wxr_term_name( $term ) {
   293 		if ( empty( $term->name ) ) {
   294 		if ( empty( $term->name ) ) {
   294 			return;
   295 			return;
   295 		}
   296 		}
   300 	/**
   301 	/**
   301 	 * Output a term_description XML tag from a given term object
   302 	 * Output a term_description XML tag from a given term object
   302 	 *
   303 	 *
   303 	 * @since 2.9.0
   304 	 * @since 2.9.0
   304 	 *
   305 	 *
   305 	 * @param object $term Term Object
   306 	 * @param WP_Term $term Term Object
   306 	 */
   307 	 */
   307 	function wxr_term_description( $term ) {
   308 	function wxr_term_description( $term ) {
   308 		if ( empty( $term->description ) ) {
   309 		if ( empty( $term->description ) ) {
   309 			return;
   310 			return;
   310 		}
   311 		}
   370 
   371 
   371 		$authors = array_filter( $authors );
   372 		$authors = array_filter( $authors );
   372 
   373 
   373 		foreach ( $authors as $author ) {
   374 		foreach ( $authors as $author ) {
   374 			echo "\t<wp:author>";
   375 			echo "\t<wp:author>";
   375 			echo '<wp:author_id>' . intval( $author->ID ) . '</wp:author_id>';
   376 			echo '<wp:author_id>' . (int) $author->ID . '</wp:author_id>';
   376 			echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>';
   377 			echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>';
   377 			echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>';
   378 			echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>';
   378 			echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
   379 			echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
   379 			echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>';
   380 			echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>';
   380 			echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
   381 			echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
   393 			return;
   394 			return;
   394 		}
   395 		}
   395 
   396 
   396 		foreach ( $nav_menus as $menu ) {
   397 		foreach ( $nav_menus as $menu ) {
   397 			echo "\t<wp:term>";
   398 			echo "\t<wp:term>";
   398 			echo '<wp:term_id>' . intval( $menu->term_id ) . '</wp:term_id>';
   399 			echo '<wp:term_id>' . (int) $menu->term_id . '</wp:term_id>';
   399 			echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
   400 			echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
   400 			echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
   401 			echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
   401 			wxr_term_name( $menu );
   402 			wxr_term_name( $menu );
   402 			echo "</wp:term>\n";
   403 			echo "</wp:term>\n";
   403 		}
   404 		}
   476 
   477 
   477 	<?php wxr_authors_list( $post_ids ); ?>
   478 	<?php wxr_authors_list( $post_ids ); ?>
   478 
   479 
   479 	<?php foreach ( $cats as $c ) : ?>
   480 	<?php foreach ( $cats as $c ) : ?>
   480 	<wp:category>
   481 	<wp:category>
   481 		<wp:term_id><?php echo intval( $c->term_id ); ?></wp:term_id>
   482 		<wp:term_id><?php echo (int) $c->term_id; ?></wp:term_id>
   482 		<wp:category_nicename><?php echo wxr_cdata( $c->slug ); ?></wp:category_nicename>
   483 		<wp:category_nicename><?php echo wxr_cdata( $c->slug ); ?></wp:category_nicename>
   483 		<wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ); ?></wp:category_parent>
   484 		<wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ); ?></wp:category_parent>
   484 		<?php
   485 		<?php
   485 		wxr_cat_name( $c );
   486 		wxr_cat_name( $c );
   486 		wxr_category_description( $c );
   487 		wxr_category_description( $c );
   488 		?>
   489 		?>
   489 	</wp:category>
   490 	</wp:category>
   490 	<?php endforeach; ?>
   491 	<?php endforeach; ?>
   491 	<?php foreach ( $tags as $t ) : ?>
   492 	<?php foreach ( $tags as $t ) : ?>
   492 	<wp:tag>
   493 	<wp:tag>
   493 		<wp:term_id><?php echo intval( $t->term_id ); ?></wp:term_id>
   494 		<wp:term_id><?php echo (int) $t->term_id; ?></wp:term_id>
   494 		<wp:tag_slug><?php echo wxr_cdata( $t->slug ); ?></wp:tag_slug>
   495 		<wp:tag_slug><?php echo wxr_cdata( $t->slug ); ?></wp:tag_slug>
   495 		<?php
   496 		<?php
   496 		wxr_tag_name( $t );
   497 		wxr_tag_name( $t );
   497 		wxr_tag_description( $t );
   498 		wxr_tag_description( $t );
   498 		wxr_term_meta( $t );
   499 		wxr_term_meta( $t );
   499 		?>
   500 		?>
   500 	</wp:tag>
   501 	</wp:tag>
   501 	<?php endforeach; ?>
   502 	<?php endforeach; ?>
   502 	<?php foreach ( $terms as $t ) : ?>
   503 	<?php foreach ( $terms as $t ) : ?>
   503 	<wp:term>
   504 	<wp:term>
   504 		<wp:term_id><?php echo intval( $t->term_id ); ?></wp:term_id>
   505 		<wp:term_id><?php echo (int) $t->term_id; ?></wp:term_id>
   505 		<wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
   506 		<wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
   506 		<wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
   507 		<wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
   507 		<wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
   508 		<wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
   508 		<?php
   509 		<?php
   509 		wxr_term_name( $t );
   510 		wxr_term_name( $t );
   532 		// Fake being in the loop.
   533 		// Fake being in the loop.
   533 		$wp_query->in_the_loop = true;
   534 		$wp_query->in_the_loop = true;
   534 
   535 
   535 		// Fetch 20 posts at a time rather than loading the entire table into memory.
   536 		// Fetch 20 posts at a time rather than loading the entire table into memory.
   536 		while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
   537 		while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
   537 			$where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
   538 			$where = 'WHERE ID IN (' . implode( ',', $next_posts ) . ')';
   538 			$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
   539 			$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
   539 
   540 
   540 			// Begin Loop.
   541 			// Begin Loop.
   541 			foreach ( $posts as $post ) {
   542 			foreach ( $posts as $post ) {
   542 				setup_postdata( $post );
   543 				setup_postdata( $post );
   543 
   544 
   544 				/** This filter is documented in wp-includes/feed.php */
   545 				/**
   545 				$title = apply_filters( 'the_title_rss', $post->post_title );
   546 				 * Filters the post title used for WXR exports.
       
   547 				 *
       
   548 				 * @since 5.7.0
       
   549 				 *
       
   550 				 * @param string $post_title Title of the current post.
       
   551 				 */
       
   552 				$title = wxr_cdata( apply_filters( 'the_title_export', $post->post_title ) );
   546 
   553 
   547 				/**
   554 				/**
   548 				 * Filters the post content used for WXR exports.
   555 				 * Filters the post content used for WXR exports.
   549 				 *
   556 				 *
   550 				 * @since 2.5.0
   557 				 * @since 2.5.0
   571 		<dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
   578 		<dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
   572 		<guid isPermaLink="false"><?php the_guid(); ?></guid>
   579 		<guid isPermaLink="false"><?php the_guid(); ?></guid>
   573 		<description></description>
   580 		<description></description>
   574 		<content:encoded><?php echo $content; ?></content:encoded>
   581 		<content:encoded><?php echo $content; ?></content:encoded>
   575 		<excerpt:encoded><?php echo $excerpt; ?></excerpt:encoded>
   582 		<excerpt:encoded><?php echo $excerpt; ?></excerpt:encoded>
   576 		<wp:post_id><?php echo intval( $post->ID ); ?></wp:post_id>
   583 		<wp:post_id><?php echo (int) $post->ID; ?></wp:post_id>
   577 		<wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
   584 		<wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
   578 		<wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
   585 		<wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
       
   586 		<wp:post_modified><?php echo wxr_cdata( $post->post_modified ); ?></wp:post_modified>
       
   587 		<wp:post_modified_gmt><?php echo wxr_cdata( $post->post_modified_gmt ); ?></wp:post_modified_gmt>
   579 		<wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
   588 		<wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
   580 		<wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
   589 		<wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
   581 		<wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
   590 		<wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
   582 		<wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
   591 		<wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
   583 		<wp:post_parent><?php echo intval( $post->post_parent ); ?></wp:post_parent>
   592 		<wp:post_parent><?php echo (int) $post->post_parent; ?></wp:post_parent>
   584 		<wp:menu_order><?php echo intval( $post->menu_order ); ?></wp:menu_order>
   593 		<wp:menu_order><?php echo (int) $post->menu_order; ?></wp:menu_order>
   585 		<wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
   594 		<wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
   586 		<wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
   595 		<wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
   587 		<wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
   596 		<wp:is_sticky><?php echo (int) $is_sticky; ?></wp:is_sticky>
   588 				<?php	if ( 'attachment' === $post->post_type ) : ?>
   597 				<?php	if ( 'attachment' === $post->post_type ) : ?>
   589 		<wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
   598 		<wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
   590 	<?php endif; ?>
   599 	<?php endif; ?>
   591 				<?php wxr_post_taxonomy(); ?>
   600 				<?php wxr_post_taxonomy(); ?>
   592 				<?php
   601 				<?php
   618 				$_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
   627 				$_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
   619 				$comments  = array_map( 'get_comment', $_comments );
   628 				$comments  = array_map( 'get_comment', $_comments );
   620 				foreach ( $comments as $c ) :
   629 				foreach ( $comments as $c ) :
   621 					?>
   630 					?>
   622 		<wp:comment>
   631 		<wp:comment>
   623 			<wp:comment_id><?php echo intval( $c->comment_ID ); ?></wp:comment_id>
   632 			<wp:comment_id><?php echo (int) $c->comment_ID; ?></wp:comment_id>
   624 			<wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
   633 			<wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
   625 			<wp:comment_author_email><?php echo wxr_cdata( $c->comment_author_email ); ?></wp:comment_author_email>
   634 			<wp:comment_author_email><?php echo wxr_cdata( $c->comment_author_email ); ?></wp:comment_author_email>
   626 			<wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
   635 			<wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
   627 			<wp:comment_author_IP><?php echo wxr_cdata( $c->comment_author_IP ); ?></wp:comment_author_IP>
   636 			<wp:comment_author_IP><?php echo wxr_cdata( $c->comment_author_IP ); ?></wp:comment_author_IP>
   628 			<wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
   637 			<wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
   629 			<wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
   638 			<wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
   630 			<wp:comment_content><?php echo wxr_cdata( $c->comment_content ); ?></wp:comment_content>
   639 			<wp:comment_content><?php echo wxr_cdata( $c->comment_content ); ?></wp:comment_content>
   631 			<wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
   640 			<wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
   632 			<wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
   641 			<wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
   633 			<wp:comment_parent><?php echo intval( $c->comment_parent ); ?></wp:comment_parent>
   642 			<wp:comment_parent><?php echo (int) $c->comment_parent; ?></wp:comment_parent>
   634 			<wp:comment_user_id><?php echo intval( $c->user_id ); ?></wp:comment_user_id>
   643 			<wp:comment_user_id><?php echo (int) $c->user_id; ?></wp:comment_user_id>
   635 					<?php
   644 					<?php
   636 					$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
   645 					$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
   637 					foreach ( $c_meta as $meta ) :
   646 					foreach ( $c_meta as $meta ) :
   638 						/**
   647 						/**
   639 						 * Filters whether to selectively skip comment meta used for WXR exports.
   648 						 * Filters whether to selectively skip comment meta used for WXR exports.