Add --from-post=<post_id> flag to create duplicate posts#154
Add --from-post=<post_id> flag to create duplicate posts#154schlessera merged 9 commits intowp-cli:masterfrom
--from-post=<post_id> flag to create duplicate posts#154Conversation
danielbachhuber
left a comment
There was a problem hiding this comment.
👍 Great start! Can you include functional tests too?
src/Post_Command.php
Outdated
| * | ||
| * [--from-post=<post_id>] | ||
| * : The post id of a post to be duplicated. | ||
| * |
There was a problem hiding this comment.
Looks like this is spaces instead of tabs. Can you correct to tabs?
There was a problem hiding this comment.
yes, I will correct it.
src/Post_Command.php
Outdated
| * # Create a duplicate post with same post data. | ||
| * $ wp post create --from-post=123 --post_title='Different Title' | ||
| * Success: Created post 2350. | ||
| */ |
There was a problem hiding this comment.
My bad. I will change it too.
src/Post_Command.php
Outdated
| $post_id = $post_arr['ID']; | ||
| unset( $post_arr['post_date'] ); | ||
| unset( $post_arr['post_date_gmt'] ); | ||
| unset( $post_arr['guid'] ); |
There was a problem hiding this comment.
WHy did you decide to skip these values?
There was a problem hiding this comment.
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.
src/Post_Command.php
Outdated
| 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']; |
There was a problem hiding this comment.
$post_arr['ID'] should be unset, correct?
There was a problem hiding this comment.
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.
src/Post_Command.php
Outdated
| * @param $post_id id of the post. | ||
| * @return array | ||
| */ | ||
| private function get_metadata( $post_id = null ) { |
There was a problem hiding this comment.
Instead of making $post_id optional, we should make it required:
get_metadata( $post_id = null )
src/Post_Command.php
Outdated
|
|
||
| if ( empty( $assoc_args['meta_input'] ) ) { | ||
| $assoc_args['meta_input'] = $this->get_metadata( $post_id ); | ||
| } |
There was a problem hiding this comment.
Do we need to persist taxonomy data too?
There was a problem hiding this comment.
This part is for overwriting existing metadata if a user wants. Categories and tags can be duplicated too. what are your thoughts about it?
There was a problem hiding this comment.
Yes, I think categories and tags should be duplicated too.
|
@danielbachhuber I made changes you asked and added taxonomy duplication as well. I don't have any idea about functional testing so I haven't done testing yet. |
|
@sagarnasit I can help you go through setting up the testing. You should first check whether you can even run the existing tests. As the Functional Tests help states, you'll need to prepare your database server first:
|
|
Looking into the Travis failure: #161 |
|
@sagarnasit I should have addressed the test failure on PHP 5.3 now. Can you merge latest master into your PR to make the tests pass again? |
|
Thanks @schlessera . I also added functional testing as @danielbachhuber said. |
| """ | ||
| {TERM_ID} | ||
| """ | ||
| @require-wp-4.4 |
There was a problem hiding this comment.
Blank line needed between scenarios (above the @require-wp-4.4 tag).
src/Post_Command.php
Outdated
|
|
||
| $array_arguments = array( 'meta_input' ); | ||
| $assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments ); | ||
| $assoc_args = \WP_CLI\Utils\parse_shell_arrays($assoc_args, $array_arguments); |
There was a problem hiding this comment.
This entire block (lines 181-202) seems to have been inadvertently reformatted to PSR2 with the last commit.
Please adapt the formatting to adhere to WPCS instead.
| * | ||
| * @return array | ||
| */ | ||
| private function get_tags( $post_id ) { |
There was a problem hiding this comment.
The code path for tags is not covered by the tests.
|
@sagarnasit This looks good already. There's some whitespace that got messed up during the last commit that you need to redress, and I would like to also have a test that covers the "tags" code. |
|
@schlessera Added Test for Tags. Looks Good? |
src/Post_Command.php
Outdated
| /** | ||
| * Get Tags of a post. | ||
| * | ||
| * @param $post_id postid of the post. |
There was a problem hiding this comment.
Should be:
@param $post_id ID of the post.
src/Post_Command.php
Outdated
| /** | ||
| * Get Categories of a post. | ||
| * | ||
| * @param $post_id postid of the post. |
There was a problem hiding this comment.
Should be:
@param $post_id ID of the post.
src/Post_Command.php
Outdated
| /** | ||
| * Get post metadata. | ||
| * | ||
| * @param $post_id id of the post. |
There was a problem hiding this comment.
Should be:
@param $post_id ID of the post.
|
|
||
| When I run `wp post term add {POST_ID} post_tag {TAG_ID} --by=id` | ||
| Then STDOUT should contain: | ||
| """* |
There was a problem hiding this comment.
There's a random asterisk added here.
|
@schlessera Made changes you asked for. |
--from-post<post_id> flag to create duplicate posts
--from-post<post_id> flag to create duplicate posts--from-post=<post_id> flag to create duplicate posts
|
Thank you for the pull request, @sagarnasit ! |
I here modified the create command to generate a duplicate post.