166 lines
6.3 KiB
PHP
166 lines
6.3 KiB
PHP
<?php
|
|
/**
|
|
* Test script for Markdown Parser WP enhanced functionality
|
|
*
|
|
* 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 Enhanced 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
|
|
echo "Simulating JSON to taxonomies mapping...\n";
|
|
$taxonomies = [];
|
|
if (isset($parsedYaml['keywords']) && is_array($parsedYaml['keywords'])) {
|
|
$taxonomies['post_tag'] = $parsedYaml['keywords'];
|
|
}
|
|
echo "Taxonomies mapped:\n";
|
|
print_r($taxonomies);
|
|
echo "\n";
|
|
|
|
// Simulate converting markdown to Gutenberg blocks
|
|
echo "Simulating markdown to Gutenberg blocks conversion...\n";
|
|
// For testing purposes, we'll just convert a small portion
|
|
$sampleMarkdown = substr($cleanedMarkdown, 0, 500);
|
|
$blocks = [];
|
|
|
|
// Simple heading detection
|
|
preg_match_all('/^(#{2,6})\s+(.+)$/m', $sampleMarkdown, $headings, PREG_SET_ORDER);
|
|
foreach ($headings as $heading) {
|
|
$level = strlen($heading[1]);
|
|
$blocks[] = "<!-- wp:heading {\"level\":$level} -->\n<h$level>{$heading[2]}</h$level>\n<!-- /wp:heading -->";
|
|
}
|
|
|
|
// Simple paragraph detection (very simplified)
|
|
preg_match_all('/^(?!#{1,6}\s)(.+)$/m', $sampleMarkdown, $paragraphs, PREG_SET_ORDER);
|
|
foreach ($paragraphs as $paragraph) {
|
|
if (trim($paragraph[1]) !== '') {
|
|
$blocks[] = "<!-- wp:paragraph -->\n<p>{$paragraph[1]}</p>\n<!-- /wp:paragraph -->";
|
|
}
|
|
}
|
|
|
|
echo "Generated " . count($blocks) . " blocks (sample):\n";
|
|
echo implode("\n\n", array_slice($blocks, 0, 2)) . "\n...\n\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\n";
|
|
} else {
|
|
echo "No featured image found.\n\n";
|
|
}
|
|
|
|
echo "Test completed successfully!\n";
|
|
echo "The enhanced plugin would create a WordPress post with:\n";
|
|
echo "- Title: " . $postFields['post_title'] . "\n";
|
|
echo "- Excerpt: " . substr($postFields['post_excerpt'], 0, 50) . "...\n";
|
|
echo "- Content: " . count($blocks) . " Gutenberg blocks\n";
|
|
echo "- Featured Image: " . ($featuredImage ? "Yes" : "No") . "\n";
|
|
echo "- Tags: " . (isset($taxonomies['post_tag']) ? implode(", ", array_slice($taxonomies['post_tag'], 0, 3)) . "..." : "None") . "\n";
|
|
|
|
} catch (Exception $e) {
|
|
echo "ERROR: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|