From: Alexander Ebert Date: Tue, 28 May 2024 15:09:39 +0000 (+0200) Subject: Add support for embedded images in newer WP versions X-Git-Tag: 6.0.14~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=94a80c289f134c830df88a0222eebc2c56c4a624;p=GitHub%2FWoltLab%2Fcom.woltlab.wcf.exporter.git Add support for embedded images in newer WP versions --- diff --git a/files/lib/system/exporter/WordPress3xExporter.class.php b/files/lib/system/exporter/WordPress3xExporter.class.php index e842b16..fbd945e 100644 --- a/files/lib/system/exporter/WordPress3xExporter.class.php +++ b/files/lib/system/exporter/WordPress3xExporter.class.php @@ -670,14 +670,25 @@ final class WordPress3xExporter extends AbstractExporter private static function fixMessage($string) { // replace media + $string = \preg_replace_callback('~.+?~is', static function ($matches) { + try { + $json = \json_decode($matches['json'], true, flags: \JSON_THROW_ON_ERROR); + + return self::replaceEmbeddedMedia( + $matches[0], + $json['id'] ?? 0, + $json['align'] ?? '', + $json['sizeSlug'] ?? '', + $json['width'] ?? 'auto', + ); + } catch (\JsonException) { + return $matches[0]; + } + }, $string); + $string = \preg_replace_callback( '~~is', static function ($matches) { - $mediaID = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.media', $matches[2]); - if (!$mediaID) { - return $matches[0]; - } - $alignment = 'none'; if (\strpos($matches[1], 'alignleft') !== false) { $alignment = 'left'; @@ -694,11 +705,12 @@ final class WordPress3xExporter extends AbstractExporter $size = 'large'; } - $data = [$mediaID, $size, $alignment]; - - return \sprintf( - '', - \base64_encode(\json_encode($data)) + return self::replaceEmbeddedMedia( + $matches[0], + $matches[2], + $alignment, + $size, + 'auto', ); }, $string @@ -717,4 +729,47 @@ final class WordPress3xExporter extends AbstractExporter return $string; } + + private static function replaceEmbeddedMedia( + string $html, + int $mediaID, + string $alignment, + string $size, + int|string $width, + ): ?string { + $mediaID = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.media', $mediaID); + if (!$mediaID) { + return $html; + } + + $alignment = match ($alignment) { + 'alignleft', 'left' => 'left', + 'alignright', 'right' => 'right', + 'aligncenter', 'center' => 'center', + default => 'none', + }; + + $size = match ($size) { + 'thumbnail' => 'small', + 'medium' => 'medium', + 'large' => 'large', + default => 'original', + }; + + if (\is_numeric($width)) { + $width = (int)$width; + if ($width <= 0) { + $width = 'auto'; + } + } else { + $width = 'auto'; + } + + $data = [$mediaID, $size, $alignment, $width]; + + return \sprintf( + '', + \base64_encode(\json_encode($data)) + ); + } }