Skip to content

Commit

Permalink
Merge pull request #3 from ItinerisLtd/empty-changes
Browse files Browse the repository at this point in the history
Allow setting or deleting empty meta
  • Loading branch information
codepuncher authored Nov 15, 2024
2 parents 750f97d + 5864fa7 commit da7255e
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/PostMetaImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PostMetaImport extends WP_CLI_Command
protected int $meta_failed = 0;
protected int $meta_skipped = 0;
protected int $meta_unchanged = 0;
protected bool $set_empty = false;
protected bool $delete_empty = false;

/**
* Bulk import meta data for posts.
Expand All @@ -46,6 +48,18 @@ class PostMetaImport extends WP_CLI_Command
* default: --dry-run
* --
*
* [--[no-]delete-empty]
* : Whether to delete metas where their new values are empty.
* --
* default: --no-delete-empty
* --
*
* [--[no-]set-empty]
* : Whether to set meta values to empty where specified.
* --
* default: --no-set-empty
* --
*
* [--yes]
* : Confirm running without prompt.
*
Expand Down Expand Up @@ -120,6 +134,12 @@ public function __invoke(array $args, array $assoc_args): void
WP_CLI::warning('Executing WITHOUT dry run.');
}

$this->set_empty = $assoc_args['set-empty'] ?? false;
$this->delete_empty = $assoc_args['delete-empty'] ?? false;
if ($this->set_empty && $this->delete_empty) {
WP_CLI::error('You cannot delete and set empty meta values at the same time. Use only one of these flags.');
}

WP_CLI::log("Processing {$this->row_count} records...");
$this->run($dry_run);

Expand Down Expand Up @@ -191,7 +211,7 @@ protected function updatePost(int $post_id, array $fields, bool $dry_run = true)
$new_value = trim($value ?? '');
$current_value = get_post_meta($post_id, $key, true);
if ($dry_run) {
if (empty($new_value)) {
if ((! $this->delete_empty && ! $this->set_empty) && empty($new_value)) {
continue;
}

Expand All @@ -202,13 +222,19 @@ protected function updatePost(int $post_id, array $fields, bool $dry_run = true)
continue;
}

if (empty($new_value)) {
if ((! $this->delete_empty && ! $this->set_empty) && empty($new_value)) {
WP_CLI::warning("The value for field '{$key}' on '{$url}' is empty");
$this->meta_skipped++;
continue;
}

$update_meta = update_post_meta($post_id, $key, $new_value);
if ($this->delete_empty) {
$update_meta = delete_post_meta($post_id, $key);
$action = 'Deleted';
} else {
$update_meta = update_post_meta($post_id, $key, $new_value);
$action = 'Updated';
}
if (false === $update_meta) {
if ($current_value === $new_value) {
$this->meta_unchanged++;
Expand All @@ -219,7 +245,7 @@ protected function updatePost(int $post_id, array $fields, bool $dry_run = true)
}
} else {
$this->meta_updated++;
WP_CLI::success("Updated '{$key}' field on {$url}.");
WP_CLI::success("{$action} '{$key}' field on {$url}.");
}
}
}
Expand Down

0 comments on commit da7255e

Please sign in to comment.