<?php
$man=json_decode(file_get_contents('/entrada/manifest_prod.json'),true);
$map=[]; $created=0;$skip=0;
foreach($man as $m){
  $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; $skip++; continue; }
  $author=(int)$m['author']; if($author && !get_userdata($author)) $author=1;
  $id=wp_insert_post(["post_type"=>"post","post_status"=>"publish","post_author"=>$author,
    "post_title"=>$m['title'],"post_content"=>$m['content'],"to_ping"=>"","pinged"=>"","post_content_filtered"=>""],true);
  if(is_wp_error($id)){ echo "ERR ".$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++;
}
echo "creados=$created skip=$skip total=".count($map)."\n";
// repuntar 17990
$c=get_post(17990)->post_content; $n=0;
foreach($map as $file=>$id){
  $url=get_permalink($id);
  $c2=preg_replace('#href="anterior/'.preg_quote($file,'#').'\.htm"#i','href="'.$url.'"',$c,1,$cnt);
  if($cnt>0){ $c=$c2; $n++; }
}
wp_update_post(["ID"=>17990,"post_content"=>$c]);
$rest=preg_match_all('#anterior/[\w-]+\.htm#i',get_post(17990)->post_content,$mm);
echo "17990 repuntados=$n/23 | anterior/*.htm restantes=$rest\n";
