wp-md-rest-import/test-fixed.php

197 lines
7.6 KiB
PHP

<?php
/**
* Test script for Markdown Parser WP enhanced functionality with fixes
*
* This script simulates the core functionality of the WordPress plugin
* in a non-WordPress environment for testing purposes.
*/
// Load Composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
// Test URL
$url = 'https://git.rpi-virtuell.de/Comenius-Institut/FOERBICO/raw/branch/main/Website/content/posts/2025-03-20-Workshop-KlimaOER/index.md';
echo "Testing Fixed Markdown Parser WP functionality...\n\n";
echo "URL: $url\n\n";
try {
// Fetch markdown content
echo "Fetching markdown content...\n";
$content = file_get_contents($url);
if ($content === false) {
throw new Exception("Could not load file from URL.");
}
echo "Content fetched successfully.\n\n";
// Split YAML and Markdown
echo "Splitting YAML and Markdown content...\n";
$parts = explode('---', $content);
if (count($parts) < 3) {
throw new Exception("Invalid file format - YAML header not found.");
}
$yamlContent = trim($parts[1]);
$markdownContent = trim($parts[2]);
echo "Content split successfully.\n\n";
// Parse YAML
echo "Parsing YAML content...\n";
$parsedYaml = Yaml::parse($yamlContent);
$jsonOutput = json_encode(
$parsedYaml,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
echo "YAML parsed successfully.\n\n";
// Remove title heading
echo "Processing markdown content...\n";
$markdownLines = explode("\n", $markdownContent);
foreach ($markdownLines as $key => $line) {
if (strpos(trim($line), '# ') === 0) {
unset($markdownLines[$key]);
break;
}
}
$cleanedMarkdown = implode("\n", $markdownLines);
echo "Markdown processed successfully.\n\n";
// Save results
echo "Saving results to files...\n";
file_put_contents('test-output-metadata.json', $jsonOutput);
file_put_contents('test-output-content.md', $cleanedMarkdown);
echo "Results saved successfully.\n\n";
// Simulate mapping JSON to post fields
echo "Simulating JSON to post fields mapping...\n";
$postFields = [
'post_title' => $parsedYaml['title'] ?? $parsedYaml['name'] ?? 'Imported Post',
'post_excerpt' => $parsedYaml['summary'] ?? $parsedYaml['description'] ?? '',
'post_date' => $parsedYaml['datePublished'] ?? date('Y-m-d'),
'post_name' => $parsedYaml['url'] ?? sanitize_title($parsedYaml['title'] ?? ''),
];
echo "Post fields mapped:\n";
print_r($postFields);
echo "\n";
// Simulate mapping JSON to post meta
echo "Simulating JSON to post meta mapping...\n";
$postMeta = [
'_markdown_parser_original_metadata' => $parsedYaml,
'_markdown_parser_license' => $parsedYaml['license'] ?? '',
'_markdown_parser_original_id' => $parsedYaml['id'] ?? '',
'_markdown_parser_status' => $parsedYaml['creativeWorkStatus'] ?? '',
'_markdown_parser_type' => $parsedYaml['type'] ?? '',
'_markdown_parser_language' => $parsedYaml['inLanguage'] ?? '',
'_markdown_parser_date_published' => $parsedYaml['datePublished'] ?? '',
'_markdown_parser_context' => $parsedYaml['@context'] ?? '',
];
echo "Post meta mapped (sample):\n";
foreach (array_slice($postMeta, 0, 5, true) as $key => $value) {
if (is_array($value)) {
echo "$key => [Array]\n";
} else {
echo "$key => $value\n";
}
}
echo "...\n\n";
// Simulate mapping JSON to taxonomies with tag creation
echo "Simulating JSON to taxonomies mapping with tag creation...\n";
$taxonomies = [];
if (isset($parsedYaml['keywords']) && is_array($parsedYaml['keywords'])) {
$tag_ids = [];
foreach ($parsedYaml['keywords'] as $keyword) {
$tag_name = $keyword;
echo "Processing tag: $tag_name\n";
// In a real WordPress environment, this would check if the tag exists
// and create it if it doesn't
$tag_ids[] = mt_rand(1, 100); // Simulate tag IDs
}
$taxonomies['post_tag'] = $tag_ids;
}
echo "Taxonomies mapped (with IDs):\n";
print_r($taxonomies);
echo "\n";
// Simulate featured image handling
echo "Simulating featured image handling...\n";
$featuredImage = null;
if (isset($parsedYaml['image'])) {
$featuredImage = $parsedYaml['image'];
} elseif (isset($parsedYaml['cover']) && isset($parsedYaml['cover']['image'])) {
$featuredImage = $parsedYaml['cover']['image'];
if (isset($parsedYaml['cover']['relative']) && $parsedYaml['cover']['relative'] === true) {
$baseUrl = dirname($url) . '/';
$featuredImage = $baseUrl . $featuredImage;
}
}
if ($featuredImage) {
echo "Featured image found: $featuredImage\n";
echo "Would import to media library and set as featured image.\n";
echo "Simulating attachment ID: " . mt_rand(1000, 9999) . "\n\n";
} else {
echo "No featured image found.\n\n";
}
// Simulate converting markdown to Gutenberg blocks with image handling
echo "Simulating markdown to Gutenberg blocks conversion with image handling...\n";
// Find image markdown syntax in the content
preg_match_all('/!\[(.*?)\]\((.*?)\)/', $cleanedMarkdown, $matches, PREG_SET_ORDER);
echo "Found " . count($matches) . " images in markdown content.\n";
if (count($matches) > 0) {
echo "Sample image blocks that would be created:\n";
foreach (array_slice($matches, 0, 2) as $index => $match) {
$alt = $match[1];
$src = $match[2];
$attachment_id = mt_rand(1000, 9999); // Simulate attachment ID
echo "Image " . ($index + 1) . ":\n";
echo "- Alt: $alt\n";
echo "- Src: $src\n";
echo "- Attachment ID: $attachment_id\n";
$block_attrs = [
'url' => $src,
'alt' => $alt,
'id' => $attachment_id,
'width' => 800, // Simulated width
'height' => 600, // Simulated height
'sizeSlug' => 'full'
];
$figure_html = '<figure class="wp-block-image size-full">';
$figure_html .= '<img src="' . $src . '" alt="' . $alt . '"';
$figure_html .= ' width="800" height="600"';
$figure_html .= ' class="wp-image-' . $attachment_id . '"/>';
$figure_html .= '</figure>';
$image_block = '<!-- wp:image ' . json_encode($block_attrs) . ' -->' .
$figure_html .
'<!-- /wp:image -->';
echo "- Block HTML:\n$image_block\n\n";
}
}
echo "Test completed successfully!\n";
echo "The fixed plugin would create a WordPress post with:\n";
echo "- Title: " . $postFields['post_title'] . "\n";
echo "- Excerpt: " . substr($postFields['post_excerpt'], 0, 50) . "...\n";
echo "- Featured Image: " . ($featuredImage ? "Yes (properly imported)" : "No") . "\n";
echo "- Tags: " . (isset($parsedYaml['keywords']) ? implode(", ", array_slice($parsedYaml['keywords'], 0, 3)) . "... (properly created with IDs)" : "None") . "\n";
echo "- Images in content: " . count($matches) . " (properly converted to blocks with attachment IDs)\n";
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
exit(1);
}