Skip to content

Commit

Permalink
Add wp post meta clean-duplicates <id> <key> command (#366)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Bachhuber <[email protected]>
  • Loading branch information
BhargavBhandari90 and danielbachhuber authored Aug 31, 2022
1 parent 5addbb4 commit 35fe11a
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 92 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,30 @@ wp post meta add <id> <key> [<value>] [--format=<format>]



### wp post meta clean-duplicates

Cleans up duplicate post meta values on a post.

~~~
wp post meta clean-duplicates <id> <key>
~~~

**OPTIONS**

<id>
ID of the post to clean.

<key>
Meta key to clean up.

**EXAMPLES**

# Delete duplicate post meta.
wp post meta clean-duplicates 1234 enclosure
Success: Cleaned up duplicate 'enclosure' meta values.



### wp post meta delete

Delete a meta field.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"post list",
"post meta",
"post meta add",
"post meta clean-duplicates",
"post meta delete",
"post meta get",
"post meta list",
Expand Down
63 changes: 63 additions & 0 deletions features/post-meta-clean-duplicates.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Feature: Clean up duplicate post meta values

Scenario: Clean up duplicate post meta values.
Given a WP install
And a session_no file:
"""
n
"""
And a session_yes file:
"""
y
"""

When I run `wp post meta add 1 foo bar`
Then STDOUT should be:
"""
Success: Added custom field.
"""

When I run the previous command again
Then the return code should be 0

When I run the previous command again
Then the return code should be 0

When I run `wp post meta list 1 --keys=foo`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | foo | bar |
| 1 | foo | bar |
| 1 | foo | bar |

When I run `wp post meta clean-duplicates 1 foo < session_no`
# Check for contains only, as the string contains a trailing space.
Then STDOUT should contain:
"""
Are you sure you want to delete 2 duplicate meta values and keep 1 valid meta value? [y/n]
"""
When I run `wp post meta list 1 --keys=foo --format=count`
Then STDOUT should be:
"""
3
"""
When I run `wp post meta clean-duplicates 1 foo < session_yes`
Then STDOUT should contain:
"""
Cleaned up duplicate 'foo' meta values.
"""
When I try the previous command again
Then STDOUT should contain:
"""
Success: Nothing to clean up: found 1 valid meta value and 0 duplicates.
"""
When I try `wp post meta clean-duplicates 1 food`
Then STDERR should be:
"""
Error: No meta values found for 'food'.
"""
And the return code should be 1
168 changes: 84 additions & 84 deletions features/post-meta.feature
Original file line number Diff line number Diff line change
Expand Up @@ -59,87 +59,87 @@ Feature: Manage post custom fields

When I run `wp post meta list 1`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |

When I run `wp post meta list 1 --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |

When I run `wp post meta list 1 --orderby=id --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |

When I run `wp post meta list 1 --orderby=id --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |

When I run `wp post meta list 1 --orderby=meta_key --order=asc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |

When I run `wp post meta list 1 --orderby=meta_key --order=asc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |

When I run `wp post meta list 1 --orderby=meta_key --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |

When I run `wp post meta list 1 --orderby=meta_key --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |

When I run `wp post meta list 1 --orderby=meta_value --order=asc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |

When I run `wp post meta list 1 --orderby=meta_value --order=asc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |

When I run `wp post meta list 1 --orderby=meta_value --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |

When I run `wp post meta list 1 --orderby=meta_value --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |

Scenario: Delete all post meta
Given a WP install
Expand Down Expand Up @@ -186,8 +186,8 @@ Feature: Manage post custom fields

When I run `wp post meta list 1`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | foo | |
| post_id | meta_key | meta_value |
| 1 | foo | |

Scenario: Make sure WordPress receives the slashed data it expects in meta fields
Given a WP install
Expand Down Expand Up @@ -225,7 +225,7 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": "bar"
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand All @@ -242,14 +242,14 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": {
"bar": {
"baz": "some value"
}
},
"foo.com": {
"visitors": 999
}
"foo": {
"bar": {
"baz": "some value"
}
},
"foo.com": {
"visitors": 999
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand Down Expand Up @@ -307,7 +307,7 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": "bar"
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand All @@ -322,7 +322,7 @@ Feature: Manage post custom fields
Then STDOUT should be JSON containing:
"""
{
"foo": "baz"
"foo": "baz"
}
"""
Expand All @@ -332,10 +332,10 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And a patch file:
Expand All @@ -354,10 +354,10 @@ Feature: Manage post custom fields
Then STDOUT should be JSON containing:
"""
{
"foo": {
"bar": "new value"
},
"bar": "bad"
"foo": {
"bar": "new value"
},
"bar": "bad"
}
"""
Expand All @@ -367,10 +367,10 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand All @@ -389,10 +389,10 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": {
"bar": "baz",
"abe": "lincoln"
}
"foo": {
"bar": "baz",
"abe": "lincoln"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand All @@ -407,9 +407,9 @@ Feature: Manage post custom fields
Then STDOUT should be JSON containing:
"""
{
"foo": {
"abe": "lincoln"
}
"foo": {
"abe": "lincoln"
}
}
"""
Expand All @@ -419,9 +419,9 @@ Feature: Manage post custom fields
And an input.json file:
"""
{
"foo": {
"bar": "baz"
}
"foo": {
"bar": "baz"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
Expand Down Expand Up @@ -449,7 +449,7 @@ Feature: Manage post custom fields
Then STDOUT should be JSON containing:
"""
{
"foo": "bar"
"foo": "bar"
}
"""
Expand Down
Loading

0 comments on commit 35fe11a

Please sign in to comment.