Commit a2deb30e authored by Peter J. Herrel's avatar Peter J. Herrel Committed by GitHub
Browse files

Merge pull request #101 from diggy/wp-pll-post-duplicate

add `wp pll post duplicate` subcommand
parents 5b64accc afd9e772
......@@ -941,6 +941,37 @@ wp pll post get <post_id> [<language-code>] [--api]
### 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 delete
Delete a post and its translations.
......@@ -1437,30 +1468,21 @@ We appreciate you taking the initiative to contribute to this project.
Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.
For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines.
### Reporting a bug
Think you’ve found a bug? We’d love for you to help us get it fixed.
Before you create a new issue, you should [search existing issues](https://github.com/diggy/polylang-cli/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.
Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/diggy/polylang-cli/issues/new) with the following:
1. What you were doing (e.g. "When I run `wp post list`").
2. What you saw (e.g. "I see a fatal about a class being undefined.").
3. What you expected to see (e.g. "I expected to see the list of posts.")
Include as much detail as you can, and clear steps to reproduce if possible.
Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/diggy/polylang-cli/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, [review our bug report documentation](https://make.wordpress.org/cli/handbook/bug-reports/).
### Creating a pull request
Want to contribute a new feature? Please first [open a new issue](https://github.com/diggy/polylang-cli/issues/new) to discuss whether the feature is a good fit for the project.
Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience:
1. Create a feature branch for each contribution.
2. Submit your pull request early for feedback.
3. Include functional tests with your changes. [Read the WP-CLI documentation](https://wp-cli.org/docs/pull-requests/#functional-tests) for an introduction.
4. Follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/).
Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally.
## Development
......
......@@ -61,6 +61,7 @@
"pll post generate",
"pll post create",
"pll post get",
"pll post duplicate",
"pll post delete",
"pll post list",
"pll post-type",
......
This diff is collapsed.
......@@ -74,3 +74,45 @@ Feature: Manage posts and their translations
Success: Trashed post 10 11.
"""
And the return code should be 0
@pll-post-duplicate
Scenario: Duplicate a post to one or more languages
When I run `wp pll lang create de de de_DE`
And I run `wp post create --post_type=page --post_title='Just another page' --porcelain`
And save STDOUT as {POST_ID}
And I run `wp pll doctor translate`
And I run `wp pll post duplicate {POST_ID}`
Then STDOUT should contain:
"""
Success: Created post 6 (de) < post 5 (nl)
"""
And the return code should be 0
When I run `wp pll lang create es es es_ES`
And I run `wp pll post duplicate {POST_ID}`
Then STDOUT should contain:
"""
Success: Updated post 6 (de) < post 5 (nl)
Success: Created post 9 (es) < post 5 (nl)
"""
And the return code should be 0
When I run `wp pll post duplicate {POST_ID} de,es,r2d2`
Then STDOUT should contain:
"""
Success: Updated post 6 (de) < post 5 (nl)
Success: Updated post 9 (es) < post 5 (nl)
"""
And STDERR should be:
"""
Warning: r2d2 is not a valid language.
"""
And the return code should be 0
When I run `wp pll post duplicate {POST_ID} nl`
Then STDERR should be:
"""
Warning: Post 5 (nl) cannot be duplicated to itself.
"""
And the return code should be 0
......@@ -196,6 +196,114 @@ class PostCommand extends BaseCommand {
$this->cli->command( array( 'post', 'delete', implode( ' ', $post_ids ) ), $assoc_args );
}
/**
* Duplicate a post to one or more languages.
*
* 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)
*/
public function duplicate( $args, $assoc_args ) {
list( $post_id ) = $args;
if ( ! $post = get_post( $post_id, ARRAY_A ) ) {
$this->cli->error( sprintf( '%d is not a valid post object', $post_id ) );
}
if ( ! $this->api->is_translated_post_type( $post['post_type'] ) ) {
$this->cli->error( 'Polylang does not manage languages and translations for this post type.' );
}
$slugs = isset( $args[1] ) && $args[1]
? array_map( 'sanitize_title_with_dashes', explode( ',', $args[1] ) )
: array_diff( $this->api->languages_list(), array( pll_get_post_language( $post_id ) ) );
foreach ( $slugs as $slug ) {
if ( ! in_array( $slug, $this->api->languages_list() ) ) {
$this->cli->warning( sprintf( '%s is not a valid language.', $slug ) );
continue;
}
$this->duplicate_post( $post, $slug );
}
}
private function duplicate_post( $post, $slug )
{
$post_id = absint( $post['ID'] );
$post_language = pll_get_post_language( $post_id );
$post_translations = $this->api->get_post_translations( $post_id );
$post_data = $post;
if ( $slug === $post_language ) {
$this->cli->warning( sprintf( 'Post %d (%s) cannot be duplicated to itself.', $post_id, $slug ) );
} else {
# check for translated post parent
if ( ( $post_parent_id = wp_get_post_parent_id( $post_id ) ) && ( $parent = $this->pll->model->post->get_translation( $post_parent_id, $slug ) ) ) {
$post_data['post_parent'] = absint( $parent );
}
# check if translation already exists
$exists = $this->api->get_post( $post_id, $slug );
# insert or update translation
if ( ! empty( $exists ) ) {
$post_data['ID'] = absint( $exists );
$duplicate = wp_update_post( wp_slash( $post_data ), true );
} else {
unset( $post_data['ID'] );
$duplicate = wp_insert_post( wp_slash( $post_data ), true );
}
if ( empty( $duplicate ) ) {
$this->cli->warning( sprintf( 'Could not duplicate post %d to %s.', $post_id, $slug ) );
} elseif ( is_wp_error( $duplicate ) ) {
$this->cli->warning( $duplicate->get_error_message() );
} else {
# set post language
$this->api->set_post_language( $duplicate, $slug );
# save post translations
$this->api->save_post_translations( array_unique( array_merge( array( $post_language => $post_id, $slug => $duplicate ), $post_translations ) ) );
# sync taxonomies and post meta, if applicable
$sync = new \PLL_Admin_Sync( $this->pll );
$sync->pll_save_post( $post_id, get_post( $post_id, 'OBJECT' ), $this->api->get_post_translations( $post_id ) );
# success message
$msg = $exists
? 'Updated post %3$d (%4$s) < post %1$d (%2$s)'
: 'Created post %3$d (%4$s) < post %1$d (%2$s)';
$this->cli->success( sprintf( $msg, $post_id, $post_language, $duplicate, $slug ) );
}
}
}
/**
* Count posts for a language.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment