Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions features/post-create-duplicate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ Feature: Create Duplicate WordPress post from existing posts.
Then STDOUT should be a table containing rows:
| Field | Value |
| post_type | page |

Scenario: Duplicating a post does not inherit its modification date
Given a WP install

When I run `wp post create --post_title='Source' --post_status=publish --porcelain`
Then STDOUT should be a number
And save STDOUT as {SOURCE_ID}

When I run `wp post update {SOURCE_ID} --post_modified='2015-03-03 09:00:00'`
Then STDOUT should not be empty

When I run `wp post create --from-post={SOURCE_ID} --post_title='Duplicate' --porcelain`
Then STDOUT should be a number
And save STDOUT as {DUPLICATE_ID}

When I run `wp post get {DUPLICATE_ID} --field=post_modified`
Then STDOUT should not contain:
"""
2015-03-03
"""
48 changes: 48 additions & 0 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,51 @@ Feature: Manage WordPress posts
"""
{"block_version":1}
"""

Scenario: Set a post's modification date on update
Given a WP install

When I run `wp post create --post_title='A post' --post_status=publish --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post update {POST_ID} --post_modified='2020-01-01 12:00:00'`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""

When I run `wp post get {POST_ID} --field=post_modified`
Then STDOUT should be:
"""
2020-01-01 12:00:00
"""

Scenario: Set a post's modification date on create
Given a WP install

When I run `wp post create --post_title='Another post' --post_date='2019-05-05 10:00:00' --post_modified='2020-01-01 12:00:00' --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post get {POST_ID} --field=post_modified`
Then STDOUT should be:
"""
2020-01-01 12:00:00
"""

Scenario: A post's modification date defaults to the current time
Given a WP install

When I run `wp post create --post_title='Undated post' --post_status=publish --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post update {POST_ID} --post_title='Retitled'`
Then STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""

When I run `wp post get {POST_ID} --field=post_modified`
Then STDOUT should not be empty
38 changes: 38 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,41 @@ Feature: Manage WordPress users
"""
newtestuser
"""

Scenario: Create a user with a nicename and rich editing preference
Given a WP install

When I run `wp user create bob bob@example.com --user_nicename=bobby --rich_editing=false --porcelain`
Then STDOUT should be a number
And save STDOUT as {USER_ID}

When I run `wp user get {USER_ID} --field=user_nicename`
Then STDOUT should be:
"""
bobby
"""

When I run `wp user meta get {USER_ID} rich_editing`
Then STDOUT should be:
"""
false
"""

Scenario: Creating a user without a nicename falls back to the login
Given a WP install

When I run `wp user create carol carol@example.com --porcelain`
Then STDOUT should be a number
And save STDOUT as {USER_ID}

When I run `wp user get {USER_ID} --field=user_nicename`
Then STDOUT should be:
"""
carol
"""

When I run `wp user meta get {USER_ID} rich_editing`
Then STDOUT should be:
"""
true
"""
55 changes: 55 additions & 0 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@
$post_id = $post_arr['ID'];
unset( $post_arr['post_date'] );
unset( $post_arr['post_date_gmt'] );
unset( $post_arr['post_modified'] );
unset( $post_arr['post_modified_gmt'] );
unset( $post_arr['guid'] );
unset( $post_arr['ID'] );

Expand Down Expand Up @@ -242,8 +244,14 @@
}
}

$modified_callback = self::add_post_modified_filter( $params );

$result = wp_insert_post( $params, true );

if ( $modified_callback ) {
remove_filter( 'wp_insert_post_data', $modified_callback );
}

if ( $filter_callback ) {
remove_filter( 'user_has_cap', $filter_callback );
}
Expand All @@ -253,6 +261,47 @@
);
}

/**
* Applies an explicitly requested modification date.
*
* wp_insert_post() derives post_modified and post_modified_gmt itself and
* never reads them back from $postarr — on update they are unconditionally
* the current time — so the documented parameters have to be applied to the
* post data on its way to the database.
*
* @param array<string, mixed> $params Parameters passed to wp_insert_post() or wp_update_post().
* @return callable|null The registered callback, for the caller to remove, or null when
* no modification date was requested.
*/
private static function add_post_modified_filter( $params ) {
$modified = [];

foreach ( [ 'post_modified', 'post_modified_gmt' ] as $key ) {
if ( ! empty( $params[ $key ] ) ) {
$modified[ $key ] = $params[ $key ];
}
}

if ( empty( $modified ) ) {
return null;
}

// Keep the pair consistent when only one of the two was given.
if ( ! isset( $modified['post_modified_gmt'] ) ) {
$modified['post_modified_gmt'] = get_gmt_from_date( $modified['post_modified'] );

Check failure on line 291 in src/Post_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $date_string of function get_gmt_from_date expects string, mixed given.

Check failure on line 291 in src/Post_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Offset 'post_modified' might not exist on non-empty-array{post_modified?: mixed, post_modified_gmt?: mixed}.
} elseif ( ! isset( $modified['post_modified'] ) ) {
$modified['post_modified'] = get_date_from_gmt( $modified['post_modified_gmt'] );

Check failure on line 293 in src/Post_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $date_string of function get_date_from_gmt expects string, mixed given.
}

$callback = static function ( $data ) use ( $modified ) {
return array_merge( $data, $modified );
};

add_filter( 'wp_insert_post_data', $callback );

return $callback;
}

/**
* Updates one or more existing posts.
*
Expand Down Expand Up @@ -422,8 +471,14 @@
}
}

$modified_callback = self::add_post_modified_filter( $params );

$result = wp_update_post( $params, true );

if ( $modified_callback ) {
remove_filter( 'wp_insert_post_data', $modified_callback );
}

if ( $filter_callback ) {
remove_filter( 'user_has_cap', $filter_callback );
}
Expand Down
4 changes: 4 additions & 0 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ public function create( $args, $assoc_args ) {

$user->user_url = Utils\get_flag_value( $assoc_args, 'user_url', false );

$user->user_nicename = Utils\get_flag_value( $assoc_args, 'user_nicename', false );

$user->rich_editing = Utils\get_flag_value( $assoc_args, 'rich_editing', false );

if ( isset( $assoc_args['user_pass'] ) ) {
$user->user_pass = $assoc_args['user_pass'];
} else {
Expand Down
Loading