From 5864fa7ee8c93b0f7097ee91bbbc01a63d0e9a36 Mon Sep 17 00:00:00 2001 From: codepuncher Date: Fri, 15 Nov 2024 12:14:59 +0000 Subject: [PATCH] Allow setting or deleting empty meta --- src/PostMetaImport.php | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/PostMetaImport.php b/src/PostMetaImport.php index ce701b9..1b263cc 100644 --- a/src/PostMetaImport.php +++ b/src/PostMetaImport.php @@ -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. @@ -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. * @@ -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); @@ -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; } @@ -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++; @@ -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}."); } } }