$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_html .= '' . $alt . ''; $figure_html .= '
'; $image_block = '' . $figure_html . ''; 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); }