Skip to content

Commit

Permalink
Merge pull request #14 from trendwerk/1-delete-data
Browse files Browse the repository at this point in the history
Delete data
  • Loading branch information
haroldangenent authored May 1, 2017
2 parents 794eb5e + e823ebb commit ec5643a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 18 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ Requires [wp-cli](https://github.com/wp-cli/wp-cli) >= 0.23.
wp faker fake <files>...
```

### Options
#### Options
| Parameter | Default | Required | Description |
| :--- | :--- | :--- | :--- |
| `<file>` | `null` | Yes | Location to an [Alice](https://github.com/nelmio/alice) YAML file

### Delete data
```sh
wp faker delete
```

Deletes all fake data.

## Support
The YAML file supports:

Expand Down
33 changes: 33 additions & 0 deletions features/delete.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Feature: Delete fake data

Scenario: Generate and delete posts
Given a WP install

And a image.yml file:
"""
Trendwerk\Faker\Entity\Image:
image{1..1}:
data: '<image()>'
"""

And a post.yml file:
"""
Trendwerk\Faker\Entity\Post:
post{1..10}:
post_content: <paragraphs(4, true)>
post_title: '<sentence()>'
"""

When I run `wp faker fake image.yml post.yml`
When I run `wp faker delete --yes`
Then STDOUT should contain:
"""
Removed 1 attachment.
Removed 10 posts.
"""

When I run `wp post list --meta_key=_fake --format=count`
Then STDOUT should be:
"""
0
"""
30 changes: 30 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ public function fake(array $files = [])

WP_CLI::success(sprintf('Generated %d new posts.', $objectCount));
}

/**
* Delete fake data
*
* ## OPTIONS
*
* [--yes]
* : Delete the fake data without a confirmation prompt.
*/
public function delete(array $args, array $assocArgs)
{
WP_CLI::confirm('Are you sure you want remove all fake data?', $assocArgs);

// Sorted by removal specifity
$dataTypes = [
'Attachment',
'Post',
];

foreach ($dataTypes as $dataType) {
$className = __NAMESPACE__ . '\\Entity\\' . $dataType;
$count = $className::delete();

if ($count > 0) {
WP_CLI::log(sprintf('Removed %d %s%s.', $count, strtolower($dataType), $count > 1 ? 's' : ''));
}
}

WP_CLI::success('Successfully removed fake data.');
}
}
24 changes: 24 additions & 0 deletions src/Entity/Attachment.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
<?php
namespace Trendwerk\Faker\Entity;

use WP_Query;

abstract class Attachment extends Post
{
public $data;
public $post_type = 'attachment';
public $post_status = 'inherit';

public static function delete()
{
$query = new WP_Query([
'fields' => 'ids',
'meta_query' => [
[
'key' => '_fake',
'value' => true,
],
],
'post_status' => 'any',
'post_type' => 'attachment',
'posts_per_page' => -1,
]);

foreach ($query->posts as $id) {
wp_delete_attachment($id, true);
}

return count($query->posts);
}
}
3 changes: 2 additions & 1 deletion src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ abstract class Entity
{
public $id;

abstract protected function create();
abstract public function persist();
abstract public static function delete();
abstract protected function create();
}
56 changes: 40 additions & 16 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Trendwerk\Faker\Entity;

use WP_Query;

class Post extends Entity
{
public $acf;
Expand All @@ -27,22 +29,6 @@ class Post extends Entity
public $terms;
public $to_ping;

protected function getPostData()
{
$data = get_object_vars($this);

unset($data['acf']);
unset($data['meta']);
unset($data['terms']);

return array_filter($data);
}

protected function create()
{
return wp_insert_post($this->getPostData());
}

public function persist()
{
$this->id = $this->create();
Expand All @@ -68,4 +54,42 @@ public function persist()
}
}
}

public static function delete()
{
$query = new WP_Query([
'fields' => 'ids',
'meta_query' => [
[
'key' => '_fake',
'value' => true,
],
],
'post_status' => 'any',
'post_type' => 'any',
'posts_per_page' => -1,
]);

foreach ($query->posts as $id) {
wp_delete_post($id, true);
}

return count($query->posts);
}

protected function create()
{
return wp_insert_post($this->getPostData());
}

protected function getPostData()
{
$data = get_object_vars($this);

unset($data['acf']);
unset($data['meta']);
unset($data['terms']);

return array_filter($data);
}
}

0 comments on commit ec5643a

Please sign in to comment.