Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add `--from-post=<post_id>` flag to create duplicate posts #154

Merged
merged 9 commits into from May 8, 2018
Copy path View file
@@ -77,7 +77,10 @@ public function __construct() {
*
* [--post_name=<post_name>]
* : The post name. Default is the sanitized post title when creating a new post.
*
*
* [--from-post=<post_id>]
* : The post id of a post to be duplicated.
*

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

Looks like this is spaces instead of tabs. Can you correct to tabs?

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

yes, I will correct it.

* [--to_ping=<to_ping>]
* : Space or carriage return-separated list of URLs to ping. Default empty.
*
@@ -147,7 +150,11 @@ public function __construct() {
* # Create a post with multiple meta values.
* $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
* Success: Created post 1923.
*/
*
* # Create a duplicate post with same post data.
* $ wp post create --from-post=123 --post_title='Different Title'
* Success: Created post 2350.
*/

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

Ditto spaces -> tabs.

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

My bad. I will change it too.

public function create( $args, $assoc_args ) {
if ( ! empty( $args[0] ) ) {
$assoc_args['post_content'] = $this->read_from_file_or_stdin( $args[0] );
@@ -173,6 +180,21 @@ public function create( $args, $assoc_args ) {
$array_arguments = array( 'meta_input' );
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
if( isset( $assoc_args['from-post'] ) ) {
$post = $this->fetcher->get_check( $assoc_args['from-post'] );
$post_arr = get_object_vars( $post );
$post_id = $post_arr['ID'];

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

$post_arr['ID'] should be unset, correct?

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

yes, you are right. Actually, if we don't unset id it will still generate new id. But, for a safer side, I will make correct it.

unset( $post_arr['post_date'] );
unset( $post_arr['post_date_gmt'] );
unset( $post_arr['guid'] );

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

WHy did you decide to skip these values?

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

This values will be duplicated too. I skip post_date and post_date_gmt so that values can be reset according to the current date at the time of duplicating. Reason to unset guid is that duplicated post guid will same as the original.

If you have any suggestions please let me know.

if ( empty( $assoc_args['meta_input'] ) ) {
$assoc_args['meta_input'] = $this->get_metadata( $post_id );
}

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

Do we need to persist taxonomy data too?

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

This part is for overwriting existing metadata if a user wants. Categories and tags can be duplicated too. what are your thoughts about it?

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 21, 2018

Member

Yes, I think categories and tags should be duplicated too.

$assoc_args = array_merge( $post_arr, $assoc_args );
}
$assoc_args = wp_slash( $assoc_args );
parent::_create( $args, $assoc_args, function ( $params ) {
return wp_insert_post( $params, true );
@@ -797,4 +819,22 @@ private function get_category_ids( $arg ) {
// If no category ids found, return exploded array for compat with previous WP-CLI versions.
return $category_ids ? $category_ids : $categories;
}
/**
* Get post metadata.
*
* @param $post_id id of the post.
* @return array
*/
private function get_metadata( $post_id = null ) {

This comment has been minimized.

Copy link
@danielbachhuber

danielbachhuber Mar 20, 2018

Member

Instead of making $post_id optional, we should make it required:

get_metadata( $post_id = null )

This comment has been minimized.

Copy link
@sagarnasit

sagarnasit Mar 21, 2018

Author Contributor

sure.

$metadata = get_metadata( 'post', $post_id );
$items = array();
foreach ( $metadata as $key => $values ) {
foreach ( $values as $item_value ) {
$item_value = maybe_unserialize( $item_value );
$items[ $key ] = $item_value;
}
}
return $items;
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.