Hace tiempo hice un script para PHP que permitía postear en WordPress usando Jabber (por ejemplo GTalk). Ahora he mejorado un poco el script para que admita poner categorías (si no las hay, las crea) y subir imágenes mediante una URL (ajustándolo a un ancho máximo).
Existen distintas palabras claves para separar los distintos elementos del post dentro del texto que se envía: titulo, contenido, tags, categorias e image. Las distintas secciones se separan con ‘##’ para diferenciarlas unas de otras y se separarán por dos puntos ‘:’ formando pares clave:valor. Un ejemplo para escribir en el IM sería el siguiente:
titulo:Titulo del post##contenido:Lorem Ipsum... con todo el HTML que querramos##tags:etiqueta1, etiqueta2, etiqueta3##categorías:Categoria1,Categoria2#image:http://servidor.com/ruta/imagen.png
El código principal es el siguiente:
$e";'), explode("\n", $contenido)));
}
if (isset($image)) {
$img_content = file_get_contents($image);
$im = imagecreatefromstring($img_content);
$w = imagesx($im);
$h = imagesy($im);
$imgtype = exif_imagetype($image);
if ($w > $max_width) {
$image_p = imagecreatetruecolor($max_width, intval($h*$max_width/$w));
imagecopyresampled($image_p, $im, 0, 0, 0, 0, $max_width, intval($h*$max_width/$w), $w, $h);
ob_start();
imagepng($image_p);
$img_content = ob_get_contents();
ob_end_clean();
$imgtype = IMAGETYPE_PNG;
$w = $max_width;
$h = intval($h*$max_width/$w);
}
$imgname = "img".time().image_type_to_extension($imgtype);
$f = new xmlrpcmsg('metaWeblog.newMediaObject',
array(php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña), php_xmlrpc_encode(array('name'=>$imgname, 'bits'=>new xmlrpcval($img_content, 'base64'))))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if(!$r->faultCode()) {
$v=$r->value();
$datos = simplexml_load_string($v->serialize());
$url = $datos->xpath('//member/name[. ="url"]/following-sibling::*/string');
$contenido = '
'.$contenido;
} else {
return "Error a la hora de subir la imagen: $image [".$r->faultString()."]";
}
}
//$contenido .= mb_convert_encoding($contenido, 'UTF-8');
$f = new xmlrpcmsg('wp.getUsersBlogs',
array(php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if(!$r->faultCode()) {
$v=$r->value();
$datos = simplexml_load_string($v->serialize());
$blogid = $datos->xpath('//member/name[. ="blogid"]/following-sibling::*/string');
$blogid = is_array($blogid) ? (string) $blogid[0] : (string) $blogid;
$f = new xmlrpcmsg('metaWeblog.newPost',
array(php_xmlrpc_encode($blogid), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña), php_xmlrpc_encode(array('title'=>$titulo, 'description'=>$contenido, 'mt_keywords'=>$tags)), php_xmlrpc_encode(1))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->request_charset_encoding = 'UTF-8';
$c->setDebug(0);
$r=&$c->send($f);
if($r->faultCode()) {
return "Ha habido un error al intentar crear un nuevo post [".$r->faultString()."]";
} else {
$datos = simplexml_load_string($r->serialize());
$postid = $datos->xpath('//value/string');
$postid = is_array($postid) ? (string) $postid[0] : (string) $postid;
$f = new xmlrpcmsg('metaWeblog.getPost',
array(php_xmlrpc_encode($postid), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if($r->faultCode()) {
return "Ha habido un error al intentar recuperar información sobre el post insertado [".$r->faultString()."]";
} else {
$datos = simplexml_load_string($r->serialize());
$permalink = $datos->xpath('//member/name[. ="permaLink"]/following-sibling::*/string');
$permalink = is_array($permalink) ? (string) $permalink[0] : (string) $permalink;
$title = $datos->xpath('//member/name[. ="title"]/following-sibling::*/string');
$title = is_array($title) ? (string) $title[0] : (string) $title;
if (isset($categorias)) {
$f = new xmlrpcmsg('mt.getCategoryList',
array(php_xmlrpc_encode($postid), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if(!$r->faultCode()) {
$datos = simplexml_load_string($r->serialize());
$catsblog = $datos->xpath('//struct');
foreach(explode(',', $categorias) as $cat) {$cats[$cat] = null;}
foreach($catsblog as $cat) {
foreach($cats as $_cat=>$val) {
if (strtolower((string) $cat->member[1]->value->string) == strtolower($_cat)) {
$cats[$_cat] = (string) $cat->member[0]->value->string;
}
}
}
foreach($cats as $cat=>$val) {
if (!$val) {
$f = new xmlrpcmsg('wp.newCategory',
array(php_xmlrpc_encode($postid), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña), php_xmlrpc_encode(array('name'=>$cat, 'slug'=>str_replace(' ', '_', strtolower($cat)), 'parent_id'=>1, 'description'=>'')))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if(!$r->faultCode()) {
$datos = simplexml_load_string($r->serialize());
$catid = (string) $datos->xpath('//int');
$cats[$cat] = $catid;
} else {
return "Ha habido un error a la hora de crear la categoría $cat";
}
}
}
foreach($cats as $val) {
$catlist[] = array('categoryId'=>$val);
}
$f = new xmlrpcmsg('mt.setPostCategories',
array(php_xmlrpc_encode($postid), php_xmlrpc_encode($blog_usuario), php_xmlrpc_encode($blog_contraseña), php_xmlrpc_encode($catlist))
);
$c=new xmlrpc_client($blog_xmlrpc_path, $blog_url, $blog_puerto);
$c->setDebug(0);
$r=&$c->send($f);
if($r->faultCode()) {
return "Hubo un error a la hora de modificar las categorías del post";
}
} else {
return "Ha habido un error al intentar recuperar las categorias del blog";
}
}
return "Se ha publicado '$title': $permalink";
}
}
} else {
return "Ha habido un error al intentar recuperar información sobre el blog [".$r->faultString()."]";
}
}
//Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
//If this doesn't work, are you running 64-bit PHP with < 5.2.6?
$conn = new XMPPHP_XMPPOld($jabber_host, $jabber_puerto, $jabber_usuario, $jabber_contraseña, 'xmpphp', $jabber_host, false,
XMPPHP_Log::LEVEL_ERROR);
$conn->autoSubscribe();
$conn->useEncryption(false);
try {
$conn->connect();
while(!$conn->isDisconnected()) {
$payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
foreach($payloads as $event) {
$pl = $event[1];
switch($event[0]) {
case 'message':
if (trim($pl['body']) == '') break;
$conn->message($pl['from'], $body=post($pl['body']), $type=$pl['type']);
if($pl['body'] == 'quit') $conn->disconnect();
if($pl['body'] == 'break') $conn->send("");
break;
case 'presence':
// print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
break;
case 'session_start':
//print "Empezamos\n";
$conn->getRoster();
$conn->presence($status="Asonesss jefeeeeeee!");
break;
}
}
}
} catch(XMPPHP_Exception $e) {
echo 'ERROR:';
die($e->getMessage());
}
Podéis bajaros el código aquí.
Hola,
Parece que el filtro
posts_resultsya esta obsoleto, uno equivalente podrÃa serposts_request.Saludos!
Hola Francisco
Pues sinceramente, ni idea de si está obsoleto, yo me metà en el código de WP y vi que hacÃa uso de ese filtro.
Muchas gracias por el aviso, ¿dónde puedo ver que el filtro está obsoleto para la próxima?
Saludos
Qué tal,
Me dà cuenta que estaba depreciado porque no lo encontré en includes/query.php donde pensé que corresponde, tampoco esta en la documentación oficial de filtros y por último lo busqué en google y en otro repositorio de hooks encontré http://adambrown.info/p/wp_hooks/hook/posts_results
Me imagino que si no esta en la lista de filtros oficial, seguramente este depreciado y pronto a eliminarse, aunque yo buscarÃa directamente por el hook en cuestión para asegurarme.
Un saludo!
Me encanta la página que has pasado, nunca me acuerdo de meterla en “Marcadores” y me viene de perlas que me la hayas pasado 🙂
Gracias