@@ -94,7 +94,7 @@ public static function register(): void {
9494 'description ' => 'Import MDI markdown files into the current WordPress database. ' ,
9595 'category ' => 'markdown-db ' ,
9696 'input_schema ' => self ::import_input_schema (),
97- 'output_schema ' => array ( ' type ' => ' object ' ),
97+ 'output_schema ' => self :: import_output_schema ( ),
9898 'execute_callback ' => array ( self ::class, 'import ' ),
9999 'permission_callback ' => array ( self ::class, 'can_manage_markdown_db ' ),
100100 )
@@ -428,6 +428,7 @@ static function ( object $a, object $b ): int {
428428 $ id_map = array ();
429429 $ created = array ();
430430 $ updated = array ();
431+ $ unchanged = array ();
431432 $ skipped = array ();
432433 foreach ( $ storage ->get_skipped_files () as $ path => $ reason ) {
433434 $ skipped [] = array ( 'path ' => self ::relative_path ( $ path , $ content_dir ), 'reason ' => $ reason );
@@ -476,6 +477,15 @@ static function ( object $a, object $b ): int {
476477 $ postarr ['post_content ' ] = $ converted_content ;
477478 $ postarr = self ::apply_post_data_filter ( $ postarr , $ context , $ post );
478479
480+ if ( 'update ' === $ operation && self ::import_is_unchanged ( $ existing_id , $ postarr , $ source_path , $ source_hash ) ) {
481+ $ operation = 'unchanged ' ;
482+ --$ update_count ;
483+ $ unchanged [] = array ( 'id ' => $ existing_id , 'path ' => $ source_path );
484+ if ( $ old_id > 0 ) {
485+ $ id_map [ $ old_id ] = $ existing_id ;
486+ }
487+ }
488+
479489 if ( count ( $ sample ) < 10 ) {
480490 $ sample [] = array (
481491 'operation ' => $ operation ,
@@ -485,7 +495,7 @@ static function ( object $a, object $b ): int {
485495 );
486496 }
487497
488- if ( $ dry_run ) {
498+ if ( $ dry_run || ' unchanged ' === $ operation ) {
489499 continue ;
490500 }
491501
@@ -536,9 +546,11 @@ static function ( object $a, object $b ): int {
536546 'candidate_count ' => count ( $ posts ),
537547 'create_count ' => $ dry_run ? $ create_count : count ( $ created ),
538548 'update_count ' => $ dry_run ? $ update_count : count ( $ updated ),
549+ 'unchanged_count ' => count ( $ unchanged ),
539550 'skipped_count ' => count ( $ skipped ),
540551 'created ' => $ created ,
541552 'updated ' => $ updated ,
553+ 'unchanged ' => $ unchanged ,
542554 'skipped ' => $ skipped ,
543555 'sample ' => $ sample ,
544556 );
@@ -661,7 +673,7 @@ private static function emit_cli_result( array $result, string $format, string $
661673 WP_CLI ::line ( sprintf ( '%s mode: %s ' , $ label , $ result ['mode ' ] ?? '' ) );
662674 WP_CLI ::line ( sprintf ( 'Path: %s ' , $ result ['path ' ] ?? '' ) );
663675 WP_CLI ::line ( sprintf ( 'Candidates: %d ' , (int ) ( $ result ['candidate_count ' ] ?? 0 ) ) );
664- foreach ( array ( 'create_count ' , 'update_count ' , 'written_count ' , 'skipped_count ' ) as $ key ) {
676+ foreach ( array ( 'create_count ' , 'update_count ' , 'unchanged_count ' , ' written_count ' , 'skipped_count ' ) as $ key ) {
665677 if ( isset ( $ result [ $ key ] ) ) {
666678 WP_CLI ::line ( sprintf ( '%s: %d ' , ucfirst ( str_replace ( '_ ' , ' ' , $ key ) ), (int ) $ result [ $ key ] ) );
667679 }
@@ -671,6 +683,28 @@ private static function emit_cli_result( array $result, string $format, string $
671683 }
672684 }
673685
686+ private static function import_output_schema (): array {
687+ $ row = array (
688+ 'type ' => 'object ' ,
689+ 'properties ' => array ( 'id ' => array ( 'type ' => 'integer ' ), 'path ' => array ( 'type ' => 'string ' ) ),
690+ 'required ' => array ( 'id ' , 'path ' ),
691+ );
692+ $ properties = array (
693+ 'success ' => array ( 'type ' => 'boolean ' ),
694+ 'mode ' => array ( 'type ' => 'string ' ),
695+ 'path ' => array ( 'type ' => 'string ' ),
696+ 'sample ' => array ( 'type ' => 'array ' , 'items ' => array ( 'type ' => 'object ' ) ),
697+ 'skipped ' => array ( 'type ' => 'array ' , 'items ' => array ( 'type ' => 'object ' ) ),
698+ );
699+ foreach ( array ( 'candidate_count ' , 'create_count ' , 'update_count ' , 'unchanged_count ' , 'skipped_count ' ) as $ key ) {
700+ $ properties [ $ key ] = array ( 'type ' => 'integer ' , 'minimum ' => 0 );
701+ }
702+ foreach ( array ( 'created ' , 'updated ' , 'unchanged ' ) as $ key ) {
703+ $ properties [ $ key ] = array ( 'type ' => 'array ' , 'items ' => $ row );
704+ }
705+ return array ( 'type ' => 'object ' , 'properties ' => $ properties );
706+ }
707+
674708 private static function import_input_schema (): array {
675709 return array (
676710 'type ' => 'object ' ,
@@ -891,6 +925,38 @@ private static function find_existing_post_id( object $post ): int {
891925 return 0 ;
892926 }
893927
928+ /** Compare only supplied fields; omitted dates must remain owned by WordPress. */
929+ private static function import_is_unchanged ( int $ post_id , array $ postarr , string $ source_path , string $ source_hash ): bool {
930+ $ stored = function_exists ( 'get_post ' ) ? get_post ( $ post_id ) : null ;
931+ if ( ! $ stored || ! function_exists ( 'get_post_meta ' ) ) {
932+ return false ;
933+ }
934+
935+ // wp_insert_post consumes slashed input and stores unslashed values.
936+ $ postarr = wp_unslash ( wp_slash ( $ postarr ) );
937+ $ integer_fields = array ( 'ID ' , 'post_author ' , 'post_parent ' , 'menu_order ' , 'comment_count ' );
938+ foreach ( $ postarr as $ field => $ value ) {
939+ // Unknown/filter-added write directives cannot safely be treated as a no-op.
940+ if ( ! property_exists ( $ stored , $ field ) || ! is_scalar ( $ value ) ) {
941+ return false ;
942+ }
943+ if ( in_array ( $ field , $ integer_fields , true ) ) {
944+ if ( (int ) $ value !== (int ) $ stored ->$ field ) {
945+ return false ;
946+ }
947+ } elseif ( (string ) $ value !== (string ) $ stored ->$ field ) {
948+ return false ;
949+ }
950+ }
951+
952+ foreach ( array ( self ::SOURCE_PATH_META => $ source_path , self ::SOURCE_IDENTITY_META => $ source_path , self ::SOURCE_HASH_META => $ source_hash ) as $ key => $ value ) {
953+ if ( (string ) get_post_meta ( $ post_id , $ key , true ) !== $ value ) {
954+ return false ;
955+ }
956+ }
957+ return true ;
958+ }
959+
894960 private static function post_array ( object $ post , int $ existing_id , array $ id_map ): array {
895961 $ old_parent = (int ) ( $ post ->post_parent ?? 0 );
896962 $ parent_id = $ old_parent > 0 ? (int ) ( $ id_map [ $ old_parent ] ?? $ old_parent ) : 0 ;
0 commit comments