如何实现WordPress插件中同步文章功能的custom_fields和上传功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1199个文字,预计阅读时间需要5分钟。
WordPress 同步插件的同步功能,无法实现 custom_fields 的同步,也不能将非媒体库文件上传到非媒体库中。代码示例:`get_row($sql, ARRAY_A); $seo_tab=$table_prefix . postmeta; $seo=SELECT meta_key, meta_value FROM;`
WordPress同步插件的同步实现功能,未能够实现custom_fields的同步,未能实现非媒体库文件上传到非媒体库中。
get_row($sql,ARRAY_A);
$seo_tab = $table_prefix."postmeta";
$seo = "SELECT meta_key,meta_value FROM $seo_tab WHERE post_id = $postId AND (meta_key LIKE '_aioseop_%')";
$custom_fields = $wpdb->get_results($seo,ARRAY_A);
$host = addslashes(site_url().'/');
//设置远程站点信息
$target = $server['host'];
if ( '/' != substr( $target, -1 ) ) {
$target .= '/';
}
$username = $server['user'];
$password = $server['pass'];
$result=sync_post($post,$username,$password,$target,$host,$custom_fields);
}else{
$result=array('status'=>500,'msg'=>$server['api']);
}
echo json_encode($result);
}else{
echo json_encode(array('status'=>404,'msg'=>'No operation.'));
}
// 替换源站点附件
function replace_host($string,$target,$host){
if('' == $target || '' == $host) return false;
// 判断连接是否为图片,PDF附件等
$allowedExts=array('jpeg','jpg','png','gif','txt','pdf');
$pattern = "([a-zA-Z0-9\./]+)";
preg_match_all($pattern, $string, $urls);
//如果源站点的附件等才替换,其他的不替换
if(!empty($urls)&&!empty($urls[0])){
foreach ($urls[0] as $v) {
$ext = strtolower(pathinfo($v,PATHINFO_EXTENSION));
if(in_array($ext, $allowedExts)){
$tmp=preg_replace('('.$host.')',$target,$v);
// echo $tmp."\r\n";
$string=str_replace($v,$tmp,$string);//替换掉附件的url域名,非附件不替换
// echo $v."\r\n";
// $string = $tmp;
}
}
}
// echo $string;
return $string;
//测试
// return preg_replace('('.$host.')',$target,$string);
}
/**
* 远程同步文章(A->B)
* @param array $post 需要同步的文章信息
* @param string $user 同步站点的用户(登录用)
* @param string $pass 同步站点的用户的登陆密码
* @param string $target 目标站点域名(同步站点)
* @param string $host 来源站点域名(本站点)
* @param array $custom_fields 需要同步的自定义字段
* @return array 返回消息记录(成功200失败400)
*/
function sync_post($post,$user,$pass,$target,$host,$custom_fields){
$xmlrpcurl=$target.'xmlrpc.php';
$blogid=get_current_blog_id();
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
$client = new IXR_Client($xmlrpcurl);
//先获取文章主体内容,用于上传内容中的附件
$file=$post['post_content'];
// $post['post_content']=replace_host($post['post_content'],$target,$host);//替换成目标站点url
$post['guid']='';
if(!empty($custom_fields)){//如果用户自定义非空,则添加到$post中
$cs_fields=array();
foreach ($custom_fields as $k => $v) {
$tmp_fields['key']=$v['meta_key'];
$tmp_fields['value']=$v['meta_value'];
// $tmp_fields=array($v['meta_key'],$v['meta_value']);
$cs_fields[]=$tmp_fields;
}
$post['custom_fields']=$cs_fields;
// $post['custom_fields']=array(array('key' => '_aioseop_title', 'value' => '还是测试'));
}
// 新的文章则进行新增,否则修改
$target_id = get_option("target_postid_".$post['ID']);
if(!$target_id){
$client->query("wp.newPost",$blogid,$user,$pass,$post);
$response=$client->getResponse();
// 成功后设置
if ($response['faultCode']==0){
if( is_a( $client->message, '\IXR_Message' ) ){
// 需要设置 target_postid_{the_ID} => 返回的post_id
if(!$client->message->message->params[0]){//如果无法获取post_id,则根据返回的xml获取
if(preg_match('/
本文共计1199个文字,预计阅读时间需要5分钟。
WordPress 同步插件的同步功能,无法实现 custom_fields 的同步,也不能将非媒体库文件上传到非媒体库中。代码示例:`get_row($sql, ARRAY_A); $seo_tab=$table_prefix . postmeta; $seo=SELECT meta_key, meta_value FROM;`
WordPress同步插件的同步实现功能,未能够实现custom_fields的同步,未能实现非媒体库文件上传到非媒体库中。
get_row($sql,ARRAY_A);
$seo_tab = $table_prefix."postmeta";
$seo = "SELECT meta_key,meta_value FROM $seo_tab WHERE post_id = $postId AND (meta_key LIKE '_aioseop_%')";
$custom_fields = $wpdb->get_results($seo,ARRAY_A);
$host = addslashes(site_url().'/');
//设置远程站点信息
$target = $server['host'];
if ( '/' != substr( $target, -1 ) ) {
$target .= '/';
}
$username = $server['user'];
$password = $server['pass'];
$result=sync_post($post,$username,$password,$target,$host,$custom_fields);
}else{
$result=array('status'=>500,'msg'=>$server['api']);
}
echo json_encode($result);
}else{
echo json_encode(array('status'=>404,'msg'=>'No operation.'));
}
// 替换源站点附件
function replace_host($string,$target,$host){
if('' == $target || '' == $host) return false;
// 判断连接是否为图片,PDF附件等
$allowedExts=array('jpeg','jpg','png','gif','txt','pdf');
$pattern = "([a-zA-Z0-9\./]+)";
preg_match_all($pattern, $string, $urls);
//如果源站点的附件等才替换,其他的不替换
if(!empty($urls)&&!empty($urls[0])){
foreach ($urls[0] as $v) {
$ext = strtolower(pathinfo($v,PATHINFO_EXTENSION));
if(in_array($ext, $allowedExts)){
$tmp=preg_replace('('.$host.')',$target,$v);
// echo $tmp."\r\n";
$string=str_replace($v,$tmp,$string);//替换掉附件的url域名,非附件不替换
// echo $v."\r\n";
// $string = $tmp;
}
}
}
// echo $string;
return $string;
//测试
// return preg_replace('('.$host.')',$target,$string);
}
/**
* 远程同步文章(A->B)
* @param array $post 需要同步的文章信息
* @param string $user 同步站点的用户(登录用)
* @param string $pass 同步站点的用户的登陆密码
* @param string $target 目标站点域名(同步站点)
* @param string $host 来源站点域名(本站点)
* @param array $custom_fields 需要同步的自定义字段
* @return array 返回消息记录(成功200失败400)
*/
function sync_post($post,$user,$pass,$target,$host,$custom_fields){
$xmlrpcurl=$target.'xmlrpc.php';
$blogid=get_current_blog_id();
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
$client = new IXR_Client($xmlrpcurl);
//先获取文章主体内容,用于上传内容中的附件
$file=$post['post_content'];
// $post['post_content']=replace_host($post['post_content'],$target,$host);//替换成目标站点url
$post['guid']='';
if(!empty($custom_fields)){//如果用户自定义非空,则添加到$post中
$cs_fields=array();
foreach ($custom_fields as $k => $v) {
$tmp_fields['key']=$v['meta_key'];
$tmp_fields['value']=$v['meta_value'];
// $tmp_fields=array($v['meta_key'],$v['meta_value']);
$cs_fields[]=$tmp_fields;
}
$post['custom_fields']=$cs_fields;
// $post['custom_fields']=array(array('key' => '_aioseop_title', 'value' => '还是测试'));
}
// 新的文章则进行新增,否则修改
$target_id = get_option("target_postid_".$post['ID']);
if(!$target_id){
$client->query("wp.newPost",$blogid,$user,$pass,$post);
$response=$client->getResponse();
// 成功后设置
if ($response['faultCode']==0){
if( is_a( $client->message, '\IXR_Message' ) ){
// 需要设置 target_postid_{the_ID} => 返回的post_id
if(!$client->message->message->params[0]){//如果无法获取post_id,则根据返回的xml获取
if(preg_match('/

