Skip to content

Commit

Permalink
Merge pull request #16 from drzraf/post_ids-attachments
Browse files Browse the repository at this point in the history
support exporting attachments when specific post ids are requested
  • Loading branch information
gitlost authored Dec 29, 2017
2 parents 5934924 + f01d860 commit 4f7df86
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
102 changes: 102 additions & 0 deletions features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ Feature: Export content.
11
"""

When I run `wp post create --post_title='Post with attachment' --porcelain`
Then STDOUT should be a number
And save STDOUT as {ATTACHMENT_POST_ID}

When I run `wp post create --post_type=attachment --porcelain`
Then STDOUT should be a number
And save STDOUT as {ATTACHMENT_ID}

When I run `wp post update {ATTACHMENT_ID} --post_parent={ATTACHMENT_POST_ID} --porcelain`
Then STDOUT should contain:
"""
Success: Updated post {ATTACHMENT_ID}
"""

When I run `wp post create --post_title='Test post' --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}
Expand All @@ -160,6 +174,15 @@ Feature: Export content.
When I run `wp export --post__in={POST_ID}`
And save STDOUT 'Writing to file %s' as {EXPORT_FILE}

And the {EXPORT_FILE} file should not contain:
"""
<wp:post_id>{ATTACHMENT_ID}</wp:post_id>
"""
And the {EXPORT_FILE} file should not contain:
"""
<wp:post_type>attachment</wp:post_type>
"""

When I run `wp site empty --yes`
Then STDOUT should not be empty

Expand Down Expand Up @@ -616,3 +639,82 @@ Feature: Export content.
Error: --stdout and --dir cannot be used together.
"""
And the return code should be 1

Scenario: Export individual post with attachments
Given a WP install

When I run `wp plugin install wordpress-importer --activate`
Then STDOUT should contain:
"""
Success:
"""

When I run `wp post generate --count=10`
And I run `wp post list --format=count`
Then STDOUT should be:
"""
11
"""

When I run `wp post create --post_title='Post with attachment to export' --porcelain`
Then STDOUT should be a number
And save STDOUT as {EXPORT_ATTACHMENT_POST_ID}

When I run `wp post create --post_type=attachment --porcelain`
Then STDOUT should be a number
And save STDOUT as {EXPORT_ATTACHMENT_ID}

When I run `wp post update {EXPORT_ATTACHMENT_ID} --post_parent={EXPORT_ATTACHMENT_POST_ID} --porcelain`
Then STDOUT should contain:
"""
Success: Updated post {EXPORT_ATTACHMENT_ID}
"""

When I run `wp post create --post_title='Post with attachment to ignore' --porcelain`
Then STDOUT should be a number
And save STDOUT as {IGNORE_ATTACHMENT_POST_ID}

When I run `wp post create --post_type=attachment --porcelain`
Then STDOUT should be a number
And save STDOUT as {IGNORE_ATTACHMENT_ID}

When I run `wp post update {IGNORE_ATTACHMENT_ID} --post_parent={IGNORE_ATTACHMENT_POST_ID} --porcelain`
Then STDOUT should contain:
"""
Success: Updated post {IGNORE_ATTACHMENT_ID}
"""

When I run `wp post list --post_type=post --format=count`
Then STDOUT should be:
"""
13
"""

When I run `wp post list --post_type=attachment --format=count`
Then STDOUT should be:
"""
2
"""

When I run `wp export --post__in={EXPORT_ATTACHMENT_POST_ID} --with_attachments`
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
And the {EXPORT_FILE} file should contain:
"""
<wp:post_id>{EXPORT_ATTACHMENT_POST_ID}</wp:post_id>
"""
And the {EXPORT_FILE} file should contain:
"""
<wp:post_type>attachment</wp:post_type>
"""
And the {EXPORT_FILE} file should contain:
"""
<wp:post_id>{EXPORT_ATTACHMENT_ID}</wp:post_id>
"""
And the {EXPORT_FILE} file should not contain:
"""
<wp:post_id>{IGNORE_ATTACHMENT_POST_ID}</wp:post_id>
"""
And the {EXPORT_FILE} file should not contain:
"""
<wp:post_id>{IGNORE_ATTACHMENT_ID}</wp:post_id>
"""
17 changes: 16 additions & 1 deletion src/Export_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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 @@ -126,6 +130,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 @@ -137,10 +142,20 @@ 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 );

$this->export_args['with_attachments'] = WP_CLI\Utils\get_flag_value(
$assoc_args,
'with_attachments',
$defaults['with_attachments']
);

if ( !function_exists( 'wp_export' ) ) {
self::load_export_api();
}
Expand Down Expand Up @@ -302,7 +317,7 @@ private function check_post__in( $post__in ) {
return true;

$separator = false !== stripos( $post__in, ' ' ) ? ' ' : ',';
$post__in = array_unique( array_map( 'intval', explode( $separator, $post__in ) ) );
$post__in = array_filter( array_unique( array_map( 'intval', explode( $separator, $post__in ) ) ) );
if ( empty( $post__in ) ) {
WP_CLI::warning( "post__in should be comma-separated post IDs." );
return false;
Expand Down
17 changes: 14 additions & 3 deletions src/WP_Export_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public function posts() {
private function calculate_post_ids() {
global $wpdb;
if ( is_array( $this->filters['post_ids'] ) ) {
if ( $this->filters['with_attachments'] ) {
$attachment_post_ids = $this->include_attachment_ids( $this->filters['post_ids'] );
$this->filters['post_ids'] = array_merge( $this->filters['post_ids'], $attachment_post_ids );
}
return $this->filters['post_ids'];
}
$this->post_type_where();
Expand All @@ -156,7 +160,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 {$this->max_num_posts()}" );
$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_attachment_ids( $post_ids ) );
}
return $post_ids;
}

Expand Down Expand Up @@ -189,6 +195,11 @@ private function post_type_where() {
$this->wheres[] = 'p.post_type IS NULL';
return;
}

if ( $this->filters['with_attachments'] == FALSE && ( !$this->filters['post_type'] || !in_array( 'attachment', $this->filters['post_type'], TRUE ) ) ) {
unset( $post_types['attachment'] );
}

$this->wheres[] = _wp_export_build_IN_condition( 'p.post_type', $post_types );
}

Expand Down Expand Up @@ -270,9 +281,9 @@ private function max_num_posts() {
}
}

private function attachments_for_specific_post_types( $post_ids ) {
private function include_attachment_ids( $post_ids ) {
global $wpdb;
if ( !$this->filters['post_type'] ) {
if ( !$post_ids ) {
return array();
}
$attachment_ids = array();
Expand Down

0 comments on commit 4f7df86

Please sign in to comment.