Removes unnecessary commented out code

This commit is contained in:
Jonathan Rosenbaum 2025-07-20 21:16:57 +00:00
parent 2038f9c42f
commit 96331dea98

View File

@ -306,30 +306,30 @@
libxml_use_internal_errors(true);
$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
$body = $doc->getElementsByTagName('body')->item(0);
$output = '';
foreach ($body->childNodes as $node) {
$output .= $this->convert_node_to_block($node);
}
return $output;
}
private function convert_node_to_block($node)
{
if ($node instanceof DOMText) {
$text = trim($node->wholeText);
return $text !== '' ? "<!-- wp:paragraph -->\n<p>{$text}</p>\n<!-- /wp:paragraph -->\n\n" : '';
}
if (!($node instanceof DOMElement)) {
return '';
}
$tag = strtolower($node->tagName);
// 🖼 Image handler
if ($tag === 'img') {
$src = $node->getAttribute('src');
@ -337,7 +337,7 @@
$html = '<img src="' . esc_url($src) . '" alt="' . esc_attr($alt) . '" />';
return "<!-- wp:image -->\n<figure class='wp-block-image'>{$html}</figure>\n<!-- /wp:image -->\n\n";
}
// 🔗 Image wrapped in <a>
if ($tag === 'a' && $node->getElementsByTagName('img')->length > 0) {
$img = $node->getElementsByTagName('img')->item(0);
@ -347,7 +347,7 @@
$html = '<a href="' . esc_url($href) . '"><img src="' . esc_url($src) . '" alt="' . esc_attr($alt) . '" /></a>';
return "<!-- wp:image -->\n<figure class='wp-block-image'>{$html}</figure>\n<!-- /wp:image -->\n\n";
}
// 🧱 Recognize div and process its children recursively
if ($tag === 'div') {
$content = '';
@ -359,13 +359,13 @@
if ($tag === 'table') {
$innerHTML = $node->ownerDocument->saveHTML($node);
// Optional: sanitize or restructure table HTML here
return "<!-- wp:table -->\n{$innerHTML}\n<!-- /wp:table -->\n\n";
}
// 🧾 Generic block mapping
$innerHTML = $node->ownerDocument->saveHTML($node);
switch ($tag) {
@ -384,144 +384,6 @@
return "<!-- wp:paragraph -->\n{$innerHTML}\n<!-- /wp:paragraph -->\n\n";
}
}
// private function convert_to_blocks($html)
// {
// $doc = new DOMDocument();
// libxml_use_internal_errors(true);
// $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
// libxml_clear_errors();
// $body = $doc->getElementsByTagName('body')->item(0);
// $output = '';
// // foreach ($body->childNodes as $node) {
// // if (!($node instanceof DOMElement)) {
// // continue;
// // }
// // // Check if this node (or its children) contain an <img>
// // $img_tag = $node->getElementsByTagName('img')->item(0);
// // if ($img_tag) {
// // // Extract src and alt
// // $src = $img_tag->getAttribute('src');
// // $alt = $img_tag->getAttribute('alt') ?: '';
// // // Check for a wrapping <a>
// // $link = $node->getElementsByTagName('a')->item(0);
// // $image_html = '<img src="' . esc_url($src) . '" alt="' . esc_attr($alt) . '" />';
// // if ($link) {
// // $href = $link->getAttribute('href');
// // $image_html = '<a href="' . esc_url($href) . '">' . $image_html . '</a>';
// // }
// // // Wrap in figure block
// // $figure = '<figure class="wp-block-image">' . $image_html . '</figure>';
// // $output .= "<!-- wp:image -->\n" . $figure . "\n<!-- /wp:image -->\n\n";
// // continue;
// // }
// // // Render other HTML blocks
// // $innerHTML = $doc->saveHTML($node);
// // // Fix insecure YouTube embeds
// // $innerHTML = preg_replace(
// // '#src=["\']http://(www\.)?youtube\.com#i',
// // 'src="https://www.youtube.com',
// // $innerHTML
// // );
// // switch ($node->nodeName) {
// // case 'p':
// // $output .= "<!-- wp:paragraph -->\n{$innerHTML}\n<!-- /wp:paragraph -->\n\n";
// // break;
// // case 'ol':
// // $output .= "<!-- wp:list {\"ordered\":true} -->\n{$innerHTML}\n<!-- /wp:list -->\n\n";
// // break;
// // case 'ul':
// // $output .= "<!-- wp:list -->\n{$innerHTML}\n<!-- /wp:list -->\n\n";
// // break;
// // case 'h2':
// // case 'h3':
// // $output .= "<!-- wp:heading -->\n{$innerHTML}\n<!-- /wp:heading -->\n\n";
// // break;
// // case 'blockquote':
// // $output .= "<!-- wp:quote -->\n{$innerHTML}\n<!-- /wp:quote -->\n\n";
// // break;
// // default:
// // $output .= "<!-- wp:paragraph -->\n{$innerHTML}\n<!-- /wp:paragraph -->\n\n";
// // break;
// // }
// // }
// foreach ($body->childNodes as $node) {
// if (!($node instanceof DOMElement)) {
// continue;
// }
// // 🖼 Render all images first
// $img_tags = $node->getElementsByTagName('img');
// if ($img_tags->length > 0) {
// foreach ($img_tags as $img_tag) {
// $src = $img_tag->getAttribute('src');
// $alt = $img_tag->getAttribute('alt') ?: '';
// // Check for wrapping <a>
// $link = $img_tag->parentNode instanceof DOMElement && $img_tag->parentNode->nodeName === 'a'
// ? $img_tag->parentNode
// : null;
// $image_html = '<img src="' . esc_url($src) . '" alt="' . esc_attr($alt) . '" />';
// if ($link) {
// $href = $link->getAttribute('href');
// $image_html = '<a href="' . esc_url($href) . '">' . $image_html . '</a>';
// }
// $figure = '<figure class="wp-block-image">' . $image_html . '</figure>';
// $output .= "<!-- wp:image -->\n" . $figure . "\n<!-- /wp:image -->\n\n";
// }
// }
// // 💡 Continue with other content
// $innerHTML = $doc->saveHTML($node);
// // Fix insecure embeds
// $innerHTML = preg_replace(
// '#src=["\']http://(www\.)?youtube\.com#i',
// 'src="https://www.youtube.com',
// $innerHTML
// );
// switch ($node->nodeName) {
// case 'p':
// $output .= "<!-- wp:paragraph -->\n{$innerHTML}\n<!-- /wp:paragraph -->\n\n";
// break;
// case 'ol':
// $output .= "<!-- wp:list {\"ordered\":true} -->\n{$innerHTML}\n<!-- /wp:list -->\n\n";
// break;
// case 'ul':
// $output .= "<!-- wp:list -->\n{$innerHTML}\n<!-- /wp:list -->\n\n";
// break;
// case 'h2':
// case 'h3':
// $output .= "<!-- wp:heading -->\n{$innerHTML}\n<!-- /wp:heading -->\n\n";
// break;
// case 'blockquote':
// $output .= "<!-- wp:quote -->\n{$innerHTML}\n<!-- /wp:quote -->\n\n";
// break;
// default:
// $output .= "<!-- wp:paragraph -->\n{$innerHTML}\n<!-- /wp:paragraph -->\n\n";
// break;
// }
// }
// return $output;
// }
}
WP_CLI::add_command('drupal-import', 'Drupal_Import_Command');