diff --git a/readme.txt b/readme.txt index c618ef59..aad7f315 100755 --- a/readme.txt +++ b/readme.txt @@ -78,10 +78,11 @@ You can even leverage your listenership through audio advertising. Use our self- = 6.0.1 = -Release date: 25th November 2025 +Release date: 26th November 2025 **Fixes** +* [#464](https://github.com/beyondwords-io/wordpress-plugin/pull/464) Use `CoreUtils::getPostMetaKeys` to get all keys for removal. * [#461](https://github.com/beyondwords-io/wordpress-plugin/pull/461) Accept `null` params from WP Core for `isProtectedMeta`. * [#460](https://github.com/beyondwords-io/wordpress-plugin/pull/460) Accept a `null` parameter in `getLangCodeFromJsonIfEmpty`. diff --git a/src/Component/Post/PostMetaUtils.php b/src/Component/Post/PostMetaUtils.php index c52185a2..e208904e 100755 --- a/src/Component/Post/PostMetaUtils.php +++ b/src/Component/Post/PostMetaUtils.php @@ -96,42 +96,17 @@ public static function getAllBeyondwordsMetadata(int $postId): array /** * Remove the BeyondWords metadata for a Post. + * + * @param int $postId Post ID. + * + * @since 4.x Introduced. + * @since 6.0.1 Use CoreUtils::getPostMetaKeys() to get all keys. */ public static function removeAllBeyondwordsMetadata(int $postId): void { - $keysToCheck = [ - 'beyondwords_generate_audio', - 'beyondwords_project_id', - 'beyondwords_content_id', - 'beyondwords_podcast_id', - 'beyondwords_preview_token', - 'beyondwords_player_content', - 'beyondwords_player_style', - 'beyondwords_language_code', - 'beyondwords_language_id', - 'beyondwords_body_voice_id', - 'beyondwords_title_voice_id', - 'beyondwords_summary_voice_id', - 'beyondwords_error_message', - 'beyondwords_disabled', - 'beyondwords_delete_content', - 'publish_post_to_speechkit', - 'speechkit_generate_audio', - 'speechkit_project_id', - 'speechkit_podcast_id', - 'speechkit_error_message', - 'speechkit_disabled', - 'speechkit_access_key', - 'speechkit_error', - 'speechkit_info', - 'speechkit_response', - 'speechkit_retries', - 'speechkit_status', - '_speechkit_link', - '_speechkit_text', - ]; - - foreach ($keysToCheck as $key) { + $keys = CoreUtils::getPostMetaKeys('all'); + + foreach ($keys as $key) { delete_post_meta($postId, $key, null); } } diff --git a/tests/phpunit/Core/PostMetaUtilsTest.php b/tests/phpunit/Core/PostMetaUtilsTest.php index 53e59cef..85c64e88 100644 --- a/tests/phpunit/Core/PostMetaUtilsTest.php +++ b/tests/phpunit/Core/PostMetaUtilsTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Beyondwords\Wordpress\Component\Post\PostMetaUtils; +use Beyondwords\Wordpress\Core\CoreUtils; class PostMetaUtilsTest extends TestCase { @@ -278,4 +279,59 @@ public function hasGenerateAudioProvider() 'speechkit_generate_audio is "1"' => [true, ['post_title' => 'UtilsTest:hasGenerateAudio', 'meta_input' => ['speechkit_generate_audio' => '1']]], ]; } + + /** + * Test removeAllBeyondwordsMetadata removes all BeyondWords keys + * without affecting other post meta. + * + * @since 6.0.1 + * + * @test + */ + public function removeAllBeyondwordsMetadata() + { + $postId = self::factory()->post->create([ + 'post_title' => 'PostMetaUtilsTest:removeAllBeyondwordsMetadata', + ]); + + // Set all BeyondWords meta keys + $beyondwordsKeys = CoreUtils::getPostMetaKeys('all'); + + foreach ($beyondwordsKeys as $key) { + update_post_meta($postId, $key, 'test_value'); + } + + // Set a non-BeyondWords meta key that should NOT be removed + $customKey = 'my_custom_meta_key'; + update_post_meta($postId, $customKey, 'custom_value'); + + // Verify all keys are set + foreach ($beyondwordsKeys as $key) { + $this->assertNotEmpty( + get_post_meta($postId, $key, true), + "Expected $key to be set before removal" + ); + } + $this->assertEquals('custom_value', get_post_meta($postId, $customKey, true)); + + // Remove all BeyondWords metadata + PostMetaUtils::removeAllBeyondwordsMetadata($postId); + + // Verify all BeyondWords keys are removed + foreach ($beyondwordsKeys as $key) { + $this->assertEmpty( + get_post_meta($postId, $key, true), + "Expected $key to be removed" + ); + } + + // Verify non-BeyondWords key is still present + $this->assertEquals( + 'custom_value', + get_post_meta($postId, $customKey, true), + 'Expected custom meta key to remain after removal' + ); + + wp_delete_post($postId, true); + } }