This commit is contained in:
Javier Casares 2026-08-08 08:53:08 +00:00
commit 7d3fc1969d

View file

@ -292,6 +292,11 @@ function robotstxt_docmd_sync_mapping( int $mapping_id ): bool|WP_Error {
return $content;
}
// Derive the post title from the first H1 and remove that H1 from the body.
$extracted = robotstxt_docmd_extract_h1_title( $content, $mapping['title'] );
$content = $extracted['content'];
$post_title = $extracted['title'];
// Convert Markdown to HTML using CommonMark.
// Strip raw HTML from Markdown to prevent stored XSS via compromised upstream repos.
$converter = new \League\CommonMark\CommonMarkConverter(
@ -307,7 +312,7 @@ function robotstxt_docmd_sync_mapping( int $mapping_id ): bool|WP_Error {
// Create new post.
$post_id = wp_insert_post(
array(
'post_title' => $mapping['title'],
'post_title' => $post_title,
'post_content' => $html_content,
'post_type' => $mapping['target_post_type'],
'post_status' => 'publish',
@ -325,20 +330,35 @@ function robotstxt_docmd_sync_mapping( int $mapping_id ): bool|WP_Error {
// Save the post ID for future syncs.
update_post_meta( $mapping_id, '_robotstxt_docmd_target_post_id', $post_id );
$target_post_id = (int) $post_id;
} else {
// Update existing post.
$update_result = wp_update_post(
array(
'ID' => $mapping['target_post_id'],
'post_content' => $html_content,
),
true
);
$target_post_id = (int) $mapping['target_post_id'];
}
if ( is_wp_error( $update_result ) ) {
update_post_meta( $mapping_id, '_robotstxt_docmd_sync_status', 'error' );
return $update_result;
}
// Post-process: translate internal links and sideload repository images.
$asset_map = robotstxt_docmd_get_asset_map( $target_post_id );
$processed = robotstxt_docmd_process_synced_html( $html_content, $mapping, $target_post_id, $token, $asset_map );
if ( '' !== $processed['html'] ) {
$html_content = $processed['html'];
}
robotstxt_docmd_save_asset_map( $target_post_id, $processed['assets'] );
// Persist the rewritten content and the H1-derived title.
// NOTE: post_name (slug) is intentionally NEVER passed here, so the slug
// remains stable across syncs even though the title follows the H1.
$update_result = wp_update_post(
array(
'ID' => $target_post_id,
'post_title' => $post_title,
'post_content' => $html_content,
),
true
);
if ( is_wp_error( $update_result ) ) {
update_post_meta( $mapping_id, '_robotstxt_docmd_sync_status', 'error' );
return $update_result;
}
// Update sync status.