wp/wp-admin/includes/export.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    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  *
    26  *
    27  * @global wpdb    $wpdb WordPress database abstraction object.
    27  * @global wpdb    $wpdb WordPress database abstraction object.
    28  * @global WP_Post $post Global `$post`.
    28  * @global WP_Post $post Global post object.
    29  *
    29  *
    30  * @param array $args {
    30  * @param array $args {
    31  *     Optional. Arguments for generating the WXR export file for download. Default empty array.
    31  *     Optional. Arguments for generating the WXR export file for download. Default empty array.
    32  *
    32  *
    33  *     @type string $content        Type of content to export. If set, only the post content of this post type
    33  *     @type string $content        Type of content to export. If set, only the post content of this post type
    77 
    77 
    78 	$sitename = sanitize_key( get_bloginfo( 'name' ) );
    78 	$sitename = sanitize_key( get_bloginfo( 'name' ) );
    79 	if ( ! empty( $sitename ) ) {
    79 	if ( ! empty( $sitename ) ) {
    80 		$sitename .= '.';
    80 		$sitename .= '.';
    81 	}
    81 	}
    82 	$date        = date( 'Y-m-d' );
    82 	$date        = gmdate( 'Y-m-d' );
    83 	$wp_filename = $sitename . 'WordPress.' . $date . '.xml';
    83 	$wp_filename = $sitename . 'WordPress.' . $date . '.xml';
    84 	/**
    84 	/**
    85 	 * Filters the export filename.
    85 	 * Filters the export filename.
    86 	 *
    86 	 *
    87 	 * @since 4.4.0
    87 	 * @since 4.4.0
    94 
    94 
    95 	header( 'Content-Description: File Transfer' );
    95 	header( 'Content-Description: File Transfer' );
    96 	header( 'Content-Disposition: attachment; filename=' . $filename );
    96 	header( 'Content-Disposition: attachment; filename=' . $filename );
    97 	header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    97 	header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    98 
    98 
    99 	if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
    99 	if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) {
   100 		$ptype = get_post_type_object( $args['content'] );
   100 		$ptype = get_post_type_object( $args['content'] );
   101 		if ( ! $ptype->can_export ) {
   101 		if ( ! $ptype->can_export ) {
   102 			$args['content'] = 'post';
   102 			$args['content'] = 'post';
   103 		}
   103 		}
   104 
   104 
   105 		$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
   105 		$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
   106 	} else {
   106 	} else {
   107 		$post_types = get_post_types( array( 'can_export' => true ) );
   107 		$post_types = get_post_types( array( 'can_export' => true ) );
   108 		$esses      = array_fill( 0, count( $post_types ), '%s' );
   108 		$esses      = array_fill( 0, count( $post_types ), '%s' );
   109 		$where      = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
   109 
   110 	}
   110 		// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
   111 
   111 		$where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
   112 	if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) ) {
   112 	}
       
   113 
       
   114 	if ( $args['status'] && ( 'post' === $args['content'] || 'page' === $args['content'] ) ) {
   113 		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
   115 		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
   114 	} else {
   116 	} else {
   115 		$where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
   117 		$where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
   116 	}
   118 	}
   117 
   119 
   118 	$join = '';
   120 	$join = '';
   119 	if ( $args['category'] && 'post' == $args['content'] ) {
   121 	if ( $args['category'] && 'post' === $args['content'] ) {
   120 		if ( $term = term_exists( $args['category'], 'category' ) ) {
   122 		$term = term_exists( $args['category'], 'category' );
       
   123 		if ( $term ) {
   121 			$join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
   124 			$join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
   122 			$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
   125 			$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
   123 		}
   126 		}
   124 	}
   127 	}
   125 
   128 
   126 	if ( 'post' == $args['content'] || 'page' == $args['content'] || 'attachment' == $args['content'] ) {
   129 	if ( in_array( $args['content'], array( 'post', 'page', 'attachment' ), true ) ) {
   127 		if ( $args['author'] ) {
   130 		if ( $args['author'] ) {
   128 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
   131 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
   129 		}
   132 		}
   130 
   133 
   131 		if ( $args['start_date'] ) {
   134 		if ( $args['start_date'] ) {
   132 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime( $args['start_date'] ) ) );
   135 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", gmdate( 'Y-m-d', strtotime( $args['start_date'] ) ) );
   133 		}
   136 		}
   134 
   137 
   135 		if ( $args['end_date'] ) {
   138 		if ( $args['end_date'] ) {
   136 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
   139 			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", gmdate( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
   137 		}
   140 		}
   138 	}
   141 	}
   139 
   142 
   140 	// Grab a snapshot of post IDs, just in case it changes during the export.
   143 	// Grab a snapshot of post IDs, just in case it changes during the export.
   141 	$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
   144 	$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
   142 
   145 
   143 	/*
   146 	/*
   144 	 * Get the requested terms ready, empty unless posts filtered by category
   147 	 * Get the requested terms ready, empty unless posts filtered by category
   145 	 * or all content.
   148 	 * or all content.
   146 	 */
   149 	 */
   147 	$cats = $tags = $terms = array();
   150 	$cats  = array();
       
   151 	$tags  = array();
       
   152 	$terms = array();
   148 	if ( isset( $term ) && $term ) {
   153 	if ( isset( $term ) && $term ) {
   149 		$cat  = get_term( $term['term_id'], 'category' );
   154 		$cat  = get_term( $term['term_id'], 'category' );
   150 		$cats = array( $cat->term_id => $cat );
   155 		$cats = array( $cat->term_id => $cat );
   151 		unset( $term, $cat );
   156 		unset( $term, $cat );
   152 	} elseif ( 'all' == $args['content'] ) {
   157 	} elseif ( 'all' === $args['content'] ) {
   153 		$categories = (array) get_categories( array( 'get' => 'all' ) );
   158 		$categories = (array) get_categories( array( 'get' => 'all' ) );
   154 		$tags       = (array) get_tags( array( 'get' => 'all' ) );
   159 		$tags       = (array) get_tags( array( 'get' => 'all' ) );
   155 
   160 
   156 		$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
   161 		$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
   157 		$custom_terms      = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
   162 		$custom_terms      = (array) get_terms(
       
   163 			array(
       
   164 				'taxonomy' => $custom_taxonomies,
       
   165 				'get'      => 'all',
       
   166 			)
       
   167 		);
   158 
   168 
   159 		// Put categories in order with no child going before its parent.
   169 		// Put categories in order with no child going before its parent.
   160 		while ( $cat = array_shift( $categories ) ) {
   170 		while ( $cat = array_shift( $categories ) ) {
   161 			if ( $cat->parent == 0 || isset( $cats[ $cat->parent ] ) ) {
   171 			if ( 0 == $cat->parent || isset( $cats[ $cat->parent ] ) ) {
   162 				$cats[ $cat->term_id ] = $cat;
   172 				$cats[ $cat->term_id ] = $cat;
   163 			} else {
   173 			} else {
   164 				$categories[] = $cat;
   174 				$categories[] = $cat;
   165 			}
   175 			}
   166 		}
   176 		}
   167 
   177 
   168 		// Put terms in order with no child going before its parent.
   178 		// Put terms in order with no child going before its parent.
   169 		while ( $t = array_shift( $custom_terms ) ) {
   179 		while ( $t = array_shift( $custom_terms ) ) {
   170 			if ( $t->parent == 0 || isset( $terms[ $t->parent ] ) ) {
   180 			if ( 0 == $t->parent || isset( $terms[ $t->parent ] ) ) {
   171 				$terms[ $t->term_id ] = $t;
   181 				$terms[ $t->term_id ] = $t;
   172 			} else {
   182 			} else {
   173 				$custom_terms[] = $t;
   183 				$custom_terms[] = $t;
   174 			}
   184 			}
   175 		}
   185 		}
   316 
   326 
   317 		foreach ( $termmeta as $meta ) {
   327 		foreach ( $termmeta as $meta ) {
   318 			/**
   328 			/**
   319 			 * Filters whether to selectively skip term meta used for WXR exports.
   329 			 * Filters whether to selectively skip term meta used for WXR exports.
   320 			 *
   330 			 *
   321 			 * Returning a truthy value to the filter will skip the current meta
   331 			 * Returning a truthy value from the filter will skip the current meta
   322 			 * object from being exported.
   332 			 * object from being exported.
   323 			 *
   333 			 *
   324 			 * @since 4.6.0
   334 			 * @since 4.6.0
   325 			 *
   335 			 *
   326 			 * @param bool   $skip     Whether to skip the current piece of term meta. Default false.
   336 			 * @param bool   $skip     Whether to skip the current piece of term meta. Default false.
   416 	 * @param bool   $return_me
   426 	 * @param bool   $return_me
   417 	 * @param string $meta_key
   427 	 * @param string $meta_key
   418 	 * @return bool
   428 	 * @return bool
   419 	 */
   429 	 */
   420 	function wxr_filter_postmeta( $return_me, $meta_key ) {
   430 	function wxr_filter_postmeta( $return_me, $meta_key ) {
   421 		if ( '_edit_lock' == $meta_key ) {
   431 		if ( '_edit_lock' === $meta_key ) {
   422 			$return_me = true;
   432 			$return_me = true;
   423 		}
   433 		}
   424 		return $return_me;
   434 		return $return_me;
   425 	}
   435 	}
   426 	add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
   436 	add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
   456 
   466 
   457 <channel>
   467 <channel>
   458 	<title><?php bloginfo_rss( 'name' ); ?></title>
   468 	<title><?php bloginfo_rss( 'name' ); ?></title>
   459 	<link><?php bloginfo_rss( 'url' ); ?></link>
   469 	<link><?php bloginfo_rss( 'url' ); ?></link>
   460 	<description><?php bloginfo_rss( 'description' ); ?></description>
   470 	<description><?php bloginfo_rss( 'description' ); ?></description>
   461 	<pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
   471 	<pubDate><?php echo gmdate( 'D, d M Y H:i:s +0000' ); ?></pubDate>
   462 	<language><?php bloginfo_rss( 'language' ); ?></language>
   472 	<language><?php bloginfo_rss( 'language' ); ?></language>
   463 	<wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
   473 	<wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
   464 	<wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
   474 	<wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
   465 	<wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
   475 	<wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
   466 
   476 
   489 		?>
   499 		?>
   490 	</wp:tag>
   500 	</wp:tag>
   491 	<?php endforeach; ?>
   501 	<?php endforeach; ?>
   492 	<?php foreach ( $terms as $t ) : ?>
   502 	<?php foreach ( $terms as $t ) : ?>
   493 	<wp:term>
   503 	<wp:term>
   494 		<wp:term_id><?php echo wxr_cdata( $t->term_id ); ?></wp:term_id>
   504 		<wp:term_id><?php echo intval( $t->term_id ); ?></wp:term_id>
   495 		<wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
   505 		<wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
   496 		<wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
   506 		<wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
   497 		<wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
   507 		<wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[ $t->parent ]->slug : '' ); ?></wp:term_parent>
   498 		<?php
   508 		<?php
   499 		wxr_term_name( $t );
   509 		wxr_term_name( $t );
   501 		wxr_term_meta( $t );
   511 		wxr_term_meta( $t );
   502 		?>
   512 		?>
   503 	</wp:term>
   513 	</wp:term>
   504 	<?php endforeach; ?>
   514 	<?php endforeach; ?>
   505 	<?php
   515 	<?php
   506 	if ( 'all' == $args['content'] ) {
   516 	if ( 'all' === $args['content'] ) {
   507 		wxr_nav_menu_terms();}
   517 		wxr_nav_menu_terms();}
   508 	?>
   518 	?>
   509 
   519 
   510 	<?php
   520 	<?php
   511 	/** This action is documented in wp-includes/feed-rss2.php */
   521 	/** This action is documented in wp-includes/feed-rss2.php */
   513 	?>
   523 	?>
   514 
   524 
   515 	<?php
   525 	<?php
   516 	if ( $post_ids ) {
   526 	if ( $post_ids ) {
   517 		/**
   527 		/**
   518 		 * @global WP_Query $wp_query
   528 		 * @global WP_Query $wp_query WordPress Query object.
   519 		 */
   529 		 */
   520 		global $wp_query;
   530 		global $wp_query;
   521 
   531 
   522 		// Fake being in the loop.
   532 		// Fake being in the loop.
   523 		$wp_query->in_the_loop = true;
   533 		$wp_query->in_the_loop = true;
   573 		<wp:post_parent><?php echo intval( $post->post_parent ); ?></wp:post_parent>
   583 		<wp:post_parent><?php echo intval( $post->post_parent ); ?></wp:post_parent>
   574 		<wp:menu_order><?php echo intval( $post->menu_order ); ?></wp:menu_order>
   584 		<wp:menu_order><?php echo intval( $post->menu_order ); ?></wp:menu_order>
   575 		<wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
   585 		<wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
   576 		<wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
   586 		<wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
   577 		<wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
   587 		<wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
   578 				<?php	if ( $post->post_type == 'attachment' ) : ?>
   588 				<?php	if ( 'attachment' === $post->post_type ) : ?>
   579 		<wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
   589 		<wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
   580 	<?php endif; ?>
   590 	<?php endif; ?>
   581 				<?php wxr_post_taxonomy(); ?>
   591 				<?php wxr_post_taxonomy(); ?>
   582 				<?php
   592 				<?php
   583 				$postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
   593 				$postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
   584 				foreach ( $postmeta as $meta ) :
   594 				foreach ( $postmeta as $meta ) :
   585 					/**
   595 					/**
   586 					 * Filters whether to selectively skip post meta used for WXR exports.
   596 					 * Filters whether to selectively skip post meta used for WXR exports.
   587 					 *
   597 					 *
   588 					 * Returning a truthy value to the filter will skip the current meta
   598 					 * Returning a truthy value from the filter will skip the current meta
   589 					 * object from being exported.
   599 					 * object from being exported.
   590 					 *
   600 					 *
   591 					 * @since 3.3.0
   601 					 * @since 3.3.0
   592 					 *
   602 					 *
   593 					 * @param bool   $skip     Whether to skip the current post meta. Default false.
   603 					 * @param bool   $skip     Whether to skip the current post meta. Default false.
   626 					$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
   636 					$c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
   627 					foreach ( $c_meta as $meta ) :
   637 					foreach ( $c_meta as $meta ) :
   628 						/**
   638 						/**
   629 						 * Filters whether to selectively skip comment meta used for WXR exports.
   639 						 * Filters whether to selectively skip comment meta used for WXR exports.
   630 						 *
   640 						 *
   631 						 * Returning a truthy value to the filter will skip the current meta
   641 						 * Returning a truthy value from the filter will skip the current meta
   632 						 * object from being exported.
   642 						 * object from being exported.
   633 						 *
   643 						 *
   634 						 * @since 4.0.0
   644 						 * @since 4.0.0
   635 						 *
   645 						 *
   636 						 * @param bool   $skip     Whether to skip the current comment meta. Default false.
   646 						 * @param bool   $skip     Whether to skip the current comment meta. Default false.