Skip to content

Commit

Permalink
Merge pull request #106 from diggy/add-wp-pll-post-update-subcommand
Browse files Browse the repository at this point in the history
add 'wp pll post update' subcommand
  • Loading branch information
diggy committed Aug 23, 2017
2 parents f75be8d + 3cb50ca commit 88e7174
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 25 deletions.
67 changes: 50 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -951,34 +951,36 @@ wp pll post get <post_id> [<language-code>] [--api]



### wp pll post duplicate
### wp pll post update

Duplicate a post to one or more languages.
Update one or more existing posts and their translations.

~~~
wp pll post duplicate <post_id> [<language-code>]
wp pll post update <id>... [<file>] --<field>=<value> [--defer-term-counting]
~~~

Syncs metadata and taxonomy terms, based on Polylang settings. Run `wp pll option list` to inspect current settings.

**OPTIONS**

<post_id>
Post ID of the post to duplicate. Required.
<id>...
One or more IDs of posts to update.

[<language-code>]
Language code (slug), or comma-separated list of language codes, to duplicate the post to. Omit to duplicate to all languages. Optional.
[<file>]
Read post content from <file>. If this value is present, the
`--post_content` argument will be ignored.

**EXAMPLES**
Passing `-` as the filename will cause post content to
be read from STDIN.

# Duplicate post 23 (Dutch) to German
$ wp pll post duplicate 23 de
Success: Created post 68 (de) < post 23 (nl)
--<field>=<value>
One or more fields to update. See wp_update_post().

# Duplicate post 23 (Dutch) to all languages (Dutch and Spanish)
$ wp pll post duplicate 23
Success: Updated post 68 (de) < post 23 (nl)
Success: Created post 69 (es) < post 23 (nl)
[--defer-term-counting]
Recalculate term count in batch, for a performance boost.

**EXAMPLES**

$ wp pll post update 13 --comment_status=closed
Success: Updated post 13.



Expand Down Expand Up @@ -1007,6 +1009,37 @@ wp pll post delete <post_id> [--force] [--defer-term-counting]



### wp pll post duplicate

Duplicate a post to one or more languages.

~~~
wp pll post duplicate <post_id> [<language-code>]
~~~

Syncs metadata and taxonomy terms, based on Polylang settings. Run `wp pll option list` to inspect current settings.

**OPTIONS**

<post_id>
Post ID of the post to duplicate. Required.

[<language-code>]
Language code (slug), or comma-separated list of language codes, to duplicate the post to. Omit to duplicate to all languages. Optional.

**EXAMPLES**

# Duplicate post 23 (Dutch) to German
$ wp pll post duplicate 23 de
Success: Created post 68 (de) < post 23 (nl)

# Duplicate post 23 (Dutch) to all languages (Dutch and Spanish)
$ wp pll post duplicate 23
Success: Updated post 68 (de) < post 23 (nl)
Success: Created post 69 (es) < post 23 (nl)



### wp pll post list

Get a list of posts in a language.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
"pll post generate",
"pll post create",
"pll post get",
"pll post duplicate",
"pll post update",
"pll post delete",
"pll post duplicate",
"pll post list",
"pll post-type",
"pll post-type disable",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ Feature: Manage posts and their translations
Warning: tlh is not a valid language.
"""

@pll-post-update
Scenario: Update posts and their translations

When I run `wp pll option sync comment_status`
And I run `wp pll lang create de de de_DE`
And I run `wp pll post duplicate 1`
And I run `wp pll post update 1 --comment_status="closed"`
And I run `wp post list --fields=ID,comment_status`
Then STDOUT should be a table containing rows:
| ID | comment_status |
| 5 | closed |
| 1 | closed |
And STDERR should be empty

When I run `wp pll post update 5 --comment_status="open"`
And I run `wp post list --fields=ID,comment_status`
Then STDOUT should be a table containing rows:
| ID | comment_status |
| 5 | open |
| 1 | open |
And STDERR should be empty

Scenario: Delete posts and their translations

When I run `wp pll post delete 1`
Expand Down
48 changes: 47 additions & 1 deletion src/Commands/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,52 @@ public function create( $args, $assoc_args ) {
echo implode( ' ', array_map( 'absint', $post_ids ) );
}

/**
* Update one or more existing posts and their translations.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of posts to update.
*
* [<file>]
* : Read post content from <file>. If this value is present, the
* `--post_content` argument will be ignored.
*
* Passing `-` as the filename will cause post content to
* be read from STDIN.
*
* --<field>=<value>
* : One or more fields to update. See wp_update_post().
*
* [--defer-term-counting]
* : Recalculate term count in batch, for a performance boost.
*
* ## EXAMPLES
*
* $ wp pll post update 13 --comment_status=closed
* Success: Updated post 13.
*/
public function update( $args, $assoc_args ) {

$this->pll->filters_post = new \PLL_Admin_Filters_Post( $this->pll );
$this->pll->sync = new \PLL_Admin_Sync( $this->pll );

$GLOBALS['pagenow'] = 'post.php';

# get around Polylang's capability check
$current_user = wp_get_current_user();
$current_user->allcaps = get_role( 'administrator' )->capabilities;

$_args = implode( ' ', array_merge( array( 'post', 'update' ), $args ) );
$_assoc_args = empty( $assoc_args ) ? '' : implode( ' ', array_map( function ( $v, $k ) { return "--{$k}='{$v}'"; }, $assoc_args, array_keys( $assoc_args ) ) );

$this->cli->runcommand(
sprintf( '%s %s', $_args, $_assoc_args ),
array( 'return' => false, 'launch' => false, 'exit_error' => false )
);
}

/**
* Delete a post and its translations.
*
Expand Down Expand Up @@ -437,7 +483,7 @@ public function list_( $args, $assoc_args ) {
$this->cli->command( array( 'post', 'list' ), $assoc_args );
}

/**
/**
* Generate some posts and their translations.
*
* Creates a specified number of sets of new posts with dummy data.
Expand Down

0 comments on commit 88e7174

Please sign in to comment.