Skip to content

Commit

Permalink
Introduce the --with_attachments flag to control the behavior of atta…
Browse files Browse the repository at this point in the history
…chments export, specifically in the case of --post__in
  • Loading branch information
Raphaël Droz committed Sep 1, 2017
1 parent 68ee7f7 commit 37c5135
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/Export_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Export_Command extends WP_CLI_Command {
*
* [--post__in=<pid>]
* : 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=<pid>]
* : Export only posts with IDs greater than or equal to this post ID.
Expand Down Expand Up @@ -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,
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/WP_Export_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 37c5135

Please sign in to comment.