<?php
/*
Plugin Name: Fanly Submit
Plugin URI: https://www.6gym.com
Description: WordPress百度快速收录、普通收录提交插件,实现自动自主将文章推送百度,有利于快速抓取收录,提升网站优化效果。
Author: 6g源码6gym.com
Version: 4.1
Author URI: https://www.6gym.com
*/
// Fanly Submit
add_action('init', 'FanlySubmit', 100);
function FanlySubmit() { // 自定义文章类型
$Fanly = get_option('FanlySubmit');//获取选项
if ( is_array($Fanly['Types']) ) {
foreach($Fanly['Types'] as $type) {
//add_action('save_'.$type, 'fanly_submit', 10, 2);
add_filter('manage_'.$type.'_posts_columns', 'fanly_submit_add_post_columns');
add_action('manage_'.$type.'s_custom_column', 'fanly_submit_render_post_columns', 10, 2);
}
}
}
// Fanly Submit
add_action('post_updated', 'fanly_submit', 10, 3);
function fanly_submit($post_ID, $post_after, $post_before) {
if((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || !current_user_can('edit_post', $post_id)) return;
if (wp_is_post_revision($post_ID) || wp_is_post_autosave($post_ID)) return;
if( isset($_POST['Fanly_Submit_CHECK']) || ($post_after->post_status=='publish' && in_array(get_post_meta($post_ID,'Fanly_Submit',true), array('dailysubmit','linksubmit'))) ){
//获取选项
$Fanly = get_option('FanlySubmit');
$P_Fanly_Submit = isset($_POST['Fanly_Submit']) ? $_POST['Fanly_Submit'] : '';
$Fanly_Submit = get_post_meta($post_ID,'Fanly_Submit',true);
//$future = $post->post_status=='future' ? true :false;//定时文章
//判断是否设置新增
if( $Fanly_Submit!='-1' && $Fanly_Submit!='OK' ){
if($P_Fanly_Submit=='dailysubmit'){update_post_meta($post_ID, 'Fanly_Submit', 'dailysubmit');}
//else{update_post_meta($post_ID, 'Fanly_Submit', 0);}
//if($future){update_post_meta($post_ID, 'Fanly_Submit_Future', 1);}
}
//判断文章状态与推送状态 返回/继续
if( $post_after->post_status != 'publish' || $Fanly_Submit=='OK' )return;
//执行
if( ($P_Fanly_Submit && $post_after->post_status=='publish') || (in_array($Fanly_Submit,array('dailysubmit','linksubmit')) && !isset($_POST['Fanly_Submit_CHECK'])) ){
$type = $P_Fanly_Submit=='dailysubmit' || ($Fanly_Submit=='dailysubmit'&&$P_Fanly_Submit=='') ? '&type=daily' : '';
$api_url = 'http://data.zz.baidu.com/urls?site='.$Fanly['Site'].'&token='.$Fanly['Token'].$type;
$link = $Fanly['Site']==home_url() ? get_permalink($post_ID) : str_replace(home_url(),$Fanly['Site'],get_permalink($post_ID));
$cambrian_re = wp_remote_post($api_url, array(
'headers' => array('Accept-Encoding'=>'','Content-Type'=>'text/plain'),
'timeout' => 10,
'sslverify' => false,
'blocking' => true,
'body' => $link
));
if ( is_wp_error( $cambrian_re ) ) {
update_post_meta($post_ID, 'Fanly_Submit', '-1');
$Fanly['msg'] = $cambrian_re->get_error_message();//错误信息
update_option('FanlySubmit', $Fanly);//更新选项
} else {
//若同步成功,则给自定义栏目Fanly_Submit
$res = json_decode($cambrian_re['body'], true);
if($res['success']==1){
if($type){//快速收录
$Fanly['daily'] = $res['remain_daily'].'|'.date('Ymd');
}else{
$Fanly['link'] = $res['remain'].'|'.date('Ymd');
}
update_post_meta($post_ID, 'Fanly_Submit', 'OK');// OK 成功
}elseif($res['remain']==0||$res['success']==0){//当天剩余的可推送url条数
if($res['success_daily']==0){//当快速收录为0时,自动用普通收录重试
$linksubmit_re = wp_remote_post('http://data.zz.baidu.com/urls?site='.$Fanly['Site'].'&token='.$Fanly['Token'], array('timeout'=>10,'sslverify'=>false,'blocking'=>true,'body'=>$body));
$linksubmit_json = json_decode($linksubmit_re['body'], true);
if($linksubmit_json['success']==1){
update_post_meta($post_ID, 'Fanly_Submit', 'OK');// OK 成功
}
}
$Fanly['link'] = $res['remain'].'|'.date('Ymd');
}else{//未知错误 提交失败
update_post_meta($post_ID, 'Fanly_Submit', '-1');
$Fanly['err']=$res;//调试
}
update_option('FanlySubmit', $Fanly);//更新选项
}
}
}
}
//获取当前文章类型
function fanly_submit_get_post_type() {
global $post, $typenow, $current_screen;
if ( $post && $post->post_type ) {return $post->post_type;
} elseif ( $typenow ) {return $typenow;
} elseif ( $current_screen && $current_screen->post_type ) {return $current_screen->post_type;
} elseif ( isset( $_REQUEST['post_type'] ) ) {return sanitize_key( $_REQUEST['post_type'] );
} elseif ( isset( $_REQUEST['post'] ) ) {return get_post_type( $_REQUEST['post'] );
} return 'post';
}
//同步
add_action( 'admin_menu', 'fanly_submit_create' );
function fanly_submit_create(){
$Fanly = get_option('FanlySubmit');//获取选项
if(is_array($Fanly['Types']) && in_array(fanly_submit_get_post_type(),$Fanly['Types'])){
add_action( 'post_submitbox_misc_actions', 'fanly_submit_to_publish_metabox' );//同步选项
}
}
add_action( 'add_meta_boxes', 'fanly_submit_register_meta_boxes' );
function fanly_submit_register_meta_boxes() {
global $post_id;
if( class_exists( 'Classic_Editor' ) && (
(get_option('classic-editor-replace')=='classic' && get_option('classic-editor-allow-users')=='disallow') ||
(get_option('classic-editor-allow-users')=='allow' && get_post_meta($post_id ,'classic-editor-remember',true)=='classic-editor')
))return;
$Fanly = get_option('FanlySubmit');//获取选项
if(!is_array($Fanly['Types']))return;
add_meta_box(
'fanly-submit-meta-box',
'Fanly Submit',
'fanly_submit_to_publish_metabox',
$Fanly['Types'],
'normal',
'high',
array(
'__block_editor_compatible_meta_box' => true,
)
);
}
//同步选项
function fanly_submit_to_publish_metabox() {
$Fanly = get_option('FanlySubmit');//获取选项
if($Fanly['Token']=='')return;
global $post,$post_ID;
$fanly_submit = get_post_meta($post_ID,'Fanly_Submit',true);
$remain_d = explode('|',@$Fanly['daily']);//快速收录
$remain_l = explode('|',$Fanly['link']);//普通收录
$checked = ($Fanly['Default']=='true' && $fanly_submit=='') || $fanly_submit=='dailysubmit' || $fanly_submit=='linksubmit' ? 'checked="checked"' : '';
if($fanly_submit=='OK'){//成功
$z = @$remain_d[1]==date('Ymd') ? '['.$remain_d[0].']' : '';
$input = '
<input id="Fanly_Submit" type="checkbox" checked="checked" disabled>
<label for="Fanly_Submit" class="selectit">成功'.$z.'</label>
';
}elseif( strtotime(date($post->post_date))+24*60*60 <= time() && $post->post_status=='publish'){//周级收录/文章发布已经超过二十四小时
if($remain_l[0]==0 && $remain_l[1]==date('Ymd')){
$input = '<label for="Fanly_Submit" class="selectit">上限<a style="font-weight:bold;color:#0066FF;text-decoration:none;" href="javascript:;" title="超过提交配额数量">?</a></label>';
}else{
$repost_text = $fanly_submit=='-1' ? '失败重试' : '普通';
$input = '
<label class="selectit"><input name="Fanly_Submit" type="checkbox" value="linksubmit" '.$checked.'>'.$repost_text.'</label>';
}
}else{
if(@$remain_d[0]==0 && @$remain_d[1]==date('Ymd')){//快速收录
$input = '
<label class="selectit"><input name="Fanly_Submit" type="checkbox" value="dailysubmit" '.$checked.'>上限</label>';
}else{
$z = @$remain_d[1]==date('Ymd') ? '['.$remain_d[0].']' : '';
$realtime_text = $fanly_submit=='-1' ? '重试' : '快速';
$input = '
<label class="selectit"><input name="Fanly_Submit" type="checkbox" value="dailysubmit" '.$checked.'>'.$realtime_text.$z.'</label>
';
}
if(@$remain_l[0]==0 && @$remain_l[1]==date('Ymd')){//普通收录
$input .= '
<label class="selectit"><input name="Fanly_Submit" type="checkbox" value="linksubmit" '.$checked.'>上限</label>';