Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/malformed utf8 esape #16

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion source/php/Index.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,19 @@ public function testThatABlockPostIsGeneratingAExcerpt() {
$this->assertNotEmpty($truncatedExcerpt);
}

}
public function testThatMalformedUTF8ContentIsFixed() {

// Given
$post = [
"post_title" => "Test Post",
"post_content" => "R\xc3\xb8d P\xc3\xb8lse 🌭"
];

// When
$post = Index::utf8ize($post);

// Then
$this->assertEquals("Rød Pølse 🌭", $post['post_content']);
}

}
23 changes: 23 additions & 0 deletions source/php/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function index($post)
}
});

$post = self::utf8ize($post); // UTF-8 Escape

//Index post
if (self::recordToLarge($post)) {
$splitRecord = self::splitRecord($post);
Expand Down Expand Up @@ -462,4 +464,25 @@ private static function getPostAndPostId($post)

return [$post, $postId];
}

/**
* Convert data to utf-8
*
* @param mixed $data
* @return mixed
*/
public static function utf8ize($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = self::utf8ize($value);
}
} else if (is_object($data)) {
foreach ($data as $key => $value) {
$data->$key = self::utf8ize($value);
}
} else if (is_string($data)) {
return mb_convert_encoding($data, 'UTF-8');
}
return $data;
}
}
Loading