<?php
$man=json_decode(file_get_contents('/tmp/manifest.json'),true);
$map=[]; $created=0; $skipped=0;
foreach($man as $m){
  // idempotencia
  $ex=get_posts(["meta_key"=>"_mardones_src","meta_value"=>$m['file'],"post_type"=>"post","post_status"=>"any","posts_per_page"=>1,"suppress_filters"=>true]);
  if($ex){ $map[$m['file']]=$ex[0]->ID; $skipped++; continue; }
  $id=wp_insert_post([
    "post_type"=>"post","post_status"=>"publish","post_author"=>(int)$m['author'],
    "post_title"=>$m['title'],"post_content"=>$m['content'],
    "to_ping"=>"","pinged"=>"","post_content_filtered"=>"",
  ],true);
  if(is_wp_error($id)){ echo "ERROR ".$m['file'].": ".$id->get_error_message()."\n"; continue; }
  update_post_meta($id,"_mardones_src",$m['file']);
  wp_set_post_categories($id,[38]);
  if(function_exists('pll_set_post_language')) pll_set_post_language($id,'es');
  $map[$m['file']]=$id; $created++;
}
file_put_contents('/tmp/mardones_map.json',json_encode($map));
echo "creados=$created skipped=$skipped total=".count($map)."\n";
foreach($map as $f=>$id) echo "  $f -> $id\n";
