Add force pull functionality and improve post content handling

This commit is contained in:
Matt Batchelder
2026-02-23 19:35:33 -05:00
parent 3b51382797
commit cdf176e224
5 changed files with 165 additions and 23 deletions

View File

@@ -733,14 +733,14 @@ function oribi_sync_run_posts(
}
$post_arr['ID'] = $existing->ID;
$post_id = wp_update_post( $post_arr, true );
$post_id = oribi_sync_save_post( $post_arr );
if ( is_wp_error( $post_id ) ) {
$result['errors'][] = $slug . ': ' . $post_id->get_error_message();
continue;
}
$result['updated'][] = $slug;
} else {
$post_id = wp_insert_post( $post_arr, true );
$post_id = oribi_sync_save_post( $post_arr );
if ( is_wp_error( $post_id ) ) {
$result['errors'][] = $slug . ': ' . $post_id->get_error_message();
continue;
@@ -788,7 +788,7 @@ function oribi_sync_run_posts(
$entry['path']
);
if ( $rewritten !== $html_content ) {
wp_update_post( [ 'ID' => $post_id, 'post_content' => $rewritten ] );
oribi_sync_save_post( [ 'ID' => $post_id, 'post_content' => $rewritten ] );
}
// ── Featured image ──────────────────────────────────────────────────
@@ -852,10 +852,13 @@ function oribi_sync_trash_removed_posts( array $current_slugs ): array {
* The post_content (HTML) is stored as the body — raw HTML is valid in
* most Markdown flavours and renders correctly when re-imported.
*
* @param WP_Post $post
* @param WP_Post $post
* @param int|null $post_id When provided, post_content is read raw from the DB.
* @return string Markdown source.
*/
function oribi_sync_generate_post_markdown( WP_Post $post ): string {
function oribi_sync_generate_post_markdown( WP_Post $post, ?int $post_id = null ): string {
global $wpdb;
$fm = "---\n";
// Title — decode HTML entities (WP stores & etc. in DB) so the YAML
@@ -909,7 +912,14 @@ function oribi_sync_generate_post_markdown( WP_Post $post ): string {
$fm .= "---\n\n";
return $fm . $post->post_content;
// Read post_content directly from the DB when a post_id is supplied so
// we get exactly what oribi_sync_save_post() wrote, with no filter applied.
$id = $post_id ?? $post->ID;
$body = (string) $wpdb->get_var(
$wpdb->prepare( 'SELECT post_content FROM ' . $wpdb->posts . ' WHERE ID = %d', $id )
);
return $fm . $body;
}
// ─── Push post to repo ────────────────────────────────────────────────────────
@@ -976,7 +986,7 @@ function oribi_sync_push_post( int $post_id, array $opts = [] ): array {
$repo_path = rtrim( $posts_folder, '/' ) . '/' . $post->post_name . '.md';
}
$markdown_content = oribi_sync_generate_post_markdown( $post );
$markdown_content = oribi_sync_generate_post_markdown( $post, $post_id );
$commit_msg = $opts['message'] ?? 'Sync: update post ' . $post->post_name . ' from WordPress';
$new_checksum = hash( 'sha256', $markdown_content );