diff --git a/src/Export_Command.php b/src/Export_Command.php index 17951770a..0dd4c99b5 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -68,6 +68,10 @@ class Export_Command extends WP_CLI_Command { * * [--post__in=] * : Export all posts specified as a comma- or space-separated list of IDs. + * Post's attachments won't be exported unless --with_attachments is specified. + * + * [--with_attachments] + * : Force including attachments in case --post__in has been specified. * * [--start_id=] * : Export only posts with IDs greater than or equal to this post ID. @@ -116,6 +120,7 @@ public function __invoke( $_, $assoc_args ) { 'category' => NULL, 'post_status' => NULL, 'post__in' => NULL, + 'with_attachments' => TRUE, // or FALSE if user requested some post__in 'start_id' => NULL, 'skip_comments' => NULL, 'max_file_size' => 15, @@ -127,6 +132,10 @@ public function __invoke( $_, $assoc_args ) { WP_CLI::error( '--stdout and --dir cannot be used together.' ); } + if ( !empty( $assoc_args['post__in'] ) && empty( $assoc_args['with_attachments'] ) ) { + $defaults['with_attachments'] = FALSE; + } + $assoc_args = wp_parse_args( $assoc_args, $defaults ); $this->validate_args( $assoc_args ); @@ -315,6 +324,18 @@ private function check_post__in( $post__in ) { return true; } + private function check_with_attachments( $with ) { + if ( is_null( $with ) ) + return true; + + if ( (int) $with <> 0 && (int) $with <> 1 ) { + WP_CLI::warning( 'with_attachments needs to be 0 (no) or 1 (yes).' ); + return false; + } + $this->export_args['with_attachments'] = ((int) $with) == 1 ; + return true; + } + private function check_start_id( $start_id ) { if ( is_null( $start_id ) ) { return true; diff --git a/src/WP_Export_Query.php b/src/WP_Export_Query.php index c319e2c28..97250b061 100644 --- a/src/WP_Export_Query.php +++ b/src/WP_Export_Query.php @@ -140,8 +140,8 @@ public function posts() { private function calculate_post_ids() { global $wpdb; if ( is_array( $this->filters['post_ids'] ) ) { - if ( getenv( 'WP_WITH_ATTACHMENT' ) ) { - $attachment_post_ids = $this->attachments_for_specific_post_types( $this->filters['post_ids'] ); + if ( $this->filters['with_attachments'] ) { + $attachment_post_ids = $this->include_attachments_ids( $this->filters['post_ids'] ); $this->filters['post_ids'] = array_merge( $this->filters['post_ids'], $attachment_post_ids ); } return $this->filters['post_ids']; @@ -159,7 +159,9 @@ private function calculate_post_ids() { $join = implode( ' ', array_filter( $this->joins ) ); $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} AS p $join $where" ); - $post_ids = array_merge( $post_ids, $this->attachments_for_specific_post_types( $post_ids ) ); + if ( $this->filters['post_type'] ) { + $post_ids = array_merge( $post_ids, $this->include_attachments_ids( $post_ids ) ); + } return $post_ids; } @@ -264,7 +266,7 @@ private function category_where() { $this->wheres[] = $wpdb->prepare( 'tr.term_taxonomy_id = %d', $category->term_taxonomy_id ); } - private function attachments_for_specific_post_types( $post_ids ) { + private function include_attachments_ids( $post_ids ) { global $wpdb; if ( ! $post_ids ) { return array();