现在建立一个博客的门槛已经很低了,尤其是Wordpress号称15秒建站,建好站点之后下一步就是考虑数据的推送,酒香也怕巷子深,如果只是孤芳自赏的话,相信博主们也会在坚持若干时间后放弃维护站点,今天分享一段百度熊掌号的实时推送代码,基于http://data.zz.baidu.com/urls?appid=接口开发,以下就是这段代码,乐分享的用户可以在functions.php里引入这个文件或者直接添加到functions.php文件内。
百度熊掌号推送工具演示
<?php
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
function fanly_submit($post_ID, $post) {
if( isset($_POST[‘Fanly_Submit_CHECK’]) || ($post->post_status==’publish’ && in_array(get_post_meta($post_ID,’Fanly_Submit’,true), array(‘realtime’,’batch’))) ){
//获取选项
$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==’realtime’){update_post_meta($post_ID, ‘Fanly_Submit’, ‘realtime’);}
//else{update_post_meta($post_ID, ‘Fanly_Submit’, 0);}
//if($future){update_post_meta($post_ID, ‘Fanly_Submit_Future’, 1);}
}//判断文章状态与推送状态 返回/继续
if( $post->post_status != ‘publish’ )return;
//执行
if( ($P_Fanly_Submit && $post->post_status==’publish’) || (in_array($Fanly_Submit,array(‘realtime’,’batch’)) && !isset($_POST[‘Fanly_Submit_CHECK’])) ){
$type = $P_Fanly_Submit==’realtime’ || ($Fanly_Submit==’realtime’&&$P_Fanly_Submit==”) ? ‘realtime’ : ‘batch’;
if(strtotime(date($post->post_date))+24*60*60 <= time() && $P_Fanly_Submit==’realtime’){$type=’batch’;}//超过24小时自动提交为周级提交数据
$api_url = ‘http://data.zz.baidu.com/urls?appid=’.$Fanly[‘APPID’].’&token=’.$Fanly[‘APPToken’].’&type=’.$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_’.$type]==1){
$Fanly[$type] = $res[‘remain_’.$type].’|’.date(‘Ymd’);
update_post_meta($post_ID, ‘Fanly_Submit’, ‘OK’);// OK 成功
}elseif($res[‘remain_’.$type]==0){//当天剩余的可推送url条数
$Fanly[$type] = $res[‘remain_’.$type].’|’.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[‘APPID’]==” || $Fanly[‘APPToken’]==”)return;
global $post,$post_ID;
$fanly_submit = get_post_meta($post_ID,’Fanly_Submit’,true);
$remain_z = explode(‘|’,@$Fanly[‘realtime’]);//天级
$remain_c = explode(‘|’,$Fanly[‘batch’]);//周级
$checked = ($Fanly[‘Default’]==’true’ && $fanly_submit==”) || $fanly_submit==’realtime’ || $fanly_submit==’batch’ ? ‘checked=”checked”‘ : ”;if($fanly_submit==’OK’){//成功
$input = ‘
<input id=”Fanly_Submit” type=”checkbox” checked=”checked” disabled>
<label for=”Fanly_Submit” class=”selectit”>成功</label>
‘;
}elseif( strtotime(date($post->post_date))+24*60*60 <= time() && $post->post_status==’publish’){//周级收录/文章发布已经超过二十四小时
if($remain_c[0]==0 && $remain_c[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=”batch” ‘.$checked.’>’.$repost_text.'</label>’;
}
}else{
if(@$remain_z[0]==0 && @$remain_z[1]==date(‘Ymd’)){//天级收录
$input = ‘
<label class=”selectit”><input name=”Fanly_Submit” type=”checkbox” value=”realtime” ‘.$checked.’>上限</label>’;
}else{
$z = @$remain_z[1]==date(‘Ymd’) ? ‘[‘.$remain_z[0].’]’ : ”;
$realtime_text = $fanly_submit==’-1′ ? ‘重试’ : ‘天级’;
$input = ‘
<label class=”selectit”><input name=”Fanly_Submit” type=”checkbox” value=”realtime” ‘.$checked.’>’.$realtime_text.$z.'</label>
‘;
}
if(@$remain_c[0]==0 && @$remain_c[1]==date(‘Ymd’)){//周级收录
$input .= ‘<label class=”selectit”><input name=”Fanly_Submit” type=”checkbox” value=”batch” ‘.$checked.’>上限</label>’;
}else{
$batch_text = $fanly_submit==’-1′ ? ‘重试’ : ‘周级’;
$input .= ‘
<label class=”selectit”><input name=”Fanly_Submit” type=”checkbox” value=”batch”>’.$batch_text.'</label>’;
}
}
echo ‘<div class=”misc-pub-section misc-pub-post-status”><input name=”Fanly_Submit_CHECK” type=”hidden” value=”true”>熊掌:<span id=”submit-span”>’.$input.'</span></div>’;
}//add plugin link
add_filter( ‘plugin_action_links’, ‘fanly_submit_Add_Link’, 10, 2 );
function fanly_submit_Add_Link( $actions, $plugin_file ) {
static $plugin;
if (!isset($plugin))
$plugin = plugin_basename(__FILE__);
if ($plugin == $plugin_file) {
$settings = array(‘settings’ => ‘<a href=”options-general.php?page=Fanly_Submit”>’ . __(‘Settings’) . ‘</a>’);
$site_link = array(‘support’ => ‘<a href=”‘);
$actions = array_merge($settings, $actions);
$actions = array_merge($site_link, $actions);
}
return $actions;
}// 文章列表字段
function fanly_submit_add_post_columns($columns) {
$columns[‘Fanly_Submit’] = ‘熊掌’;
return $columns;
}
function fanly_submit_render_post_columns($column_name, $id) {
switch ($column_name) {
case ‘Fanly_Submit’:
echo get_post_meta( $id, ‘Fanly_Submit’, TRUE)==’OK’ || get_post_meta( $id, ‘Fanly_Submit’, TRUE)==’Original’ ? ‘提交成功’ : (get_post_meta( $id, ‘Fanly_Submit’, TRUE)==’-1′ ? ‘提交失败’ : ‘未提交’); //数据提交
break;
}
}/* Token测试
$Appid
$Token
*/
function fanly_submit_test($Site, $Appid, $Token){
$baidu_api_url = ‘http://data.zz.baidu.com/urls?appid=’.$Appid.’&token=’.$Token.’&type=batch’;
$response = wp_remote_post($baidu_api_url, array(
‘headers’ => array(‘Accept-Encoding’=>”,’Content-Type’=>’text/plain’),
‘timeout’ => 10,
‘sslverify’ => false,
‘blocking’ => true,
‘body’ => $Site
));
if(is_array($response) && array_key_exists(‘body’, $response)){
$data = json_decode( $response[‘body’], true );
return $data;
}else{return FALSE;}
}//默认数据
add_action(‘admin_init’, ‘fanly_submit_default_options’);
function fanly_submit_default_options(){
$Fanly = get_option(‘FanlySubmit’);//获取选项
if( $Fanly == ” ){
$Fanly = array(//设置默认数据
‘Types’ => ”,
‘APPID’ => ”,
‘APPToken’ => ”,
‘Default’ => ”,
);
update_option(‘FanlySubmit’, $Fanly);//更新选项
}
}// 更新检测
function FANLY_SUBMIT_CHECK($refresh=”){
$FANLY_SUBMIT_CHECK = get_transient(‘FANLY_SUBMIT_CHECK’);
if( $FANLY_SUBMIT_CHECK === false || $refresh) {
$response = wp_remote_get( ‘https://zhan.leiue.com/?plugin=Fanly-Submit’, array(‘timeout’ => 100) );
if (!is_wp_error($response) && $response[‘response’][‘code’] == ‘200’ ){
$FANLY_SUBMIT_CHECK = json_decode( $response[‘body’], true );
}else{
$FANLY_SUBMIT_CHECK = ”;
}
set_transient(‘FANLY_SUBMIT_CHECK’, $FANLY_SUBMIT_CHECK, 86400);
}
return $FANLY_SUBMIT_CHECK;
}//插件设置菜单
add_action(‘admin_menu’, ‘fanly_submit_menu’);
function fanly_submit_menu() {
add_submenu_page(‘options-general.php’,’Fanly Submit’, ‘Fanly Submit’, ‘manage_options’, ‘Fanly_Submit’,’fanly_submit_options’, ”);
}
//设置页面
function fanly_submit_options() {
//Refresh FanlyMIPUA_Check
if(isset($_POST[‘check_updated’])){
@FANLY_SUBMIT_CHECK(true);
}
//检测更新
if( $data = @FANLY_SUBMIT_CHECK() ){
$ver = @$data[‘ver’];
$url = @$data[‘url’];
$plugin_data = get_plugin_data( __FILE__ );
if($ver > $plugin_data[‘Version’]){
echo ”;
}
}
//保存数据
if(isset($_POST[‘FanlySubmit’])){
$APPID = trim($_POST[‘APPID’]);
$APPToken = trim($_POST[‘APPToken’]);
$Site = preg_replace(‘#/$#’,”, trim(@$_POST[‘Site’]));
if(substr($Site,0,7)==’http://’ || substr($Site,0,8)==’https://’){}else{$Site=”;}
$xzh = fanly_submit_test($Site, $APPID, $APPToken);
if( $xzh && @$xzh[‘success_batch’]!=1 ){
$APPID = ”;
$APPToken = ”;
}
$Fanly = array(
‘Site’ => $Site,
‘APPID’ => $APPID,
‘APPToken’ => $APPToken,
‘Types’ => @$_POST[‘Types’],
‘Default’ => trim(@$_POST[‘Default’]),
);
@update_option(‘FanlySubmit’, $Fanly);//更新选项if($Site){
$updated = $APPID ? ‘设置成功!’ : ‘熊掌 ID/Token 错误!’;
}else{
$updated = ‘站点域名必须以‘http://’或‘https://’开始’;
}
echo ‘<div class=”updated” id=”message”><p>’.$updated.'</p></div>’;
}$Fanly = get_option(‘FanlySubmit’);//获取选项
$Default = $Fanly[‘Default’] !== ” ? ‘checked=”checked”‘ : ”;
echo ‘<div class=”wrap”>’;
echo ‘<h2>百度数据推送</h2>’;
echo ‘<form method=”post”>’;
echo ‘<table class=”form-table”>’;
echo ‘<tr valign=”top”>’;
echo ‘<th scope=”row”>站点域名</th>’;
$Site = @$Fanly[‘Site’] ? $Fanly[‘Site’] : home_url();
echo ‘<td><input class=”all-options” type=”text” name=”Site” value=”‘.$Site.'” /></td>’;
echo ‘</tr>’;echo ‘<tr valign=”top”>’;
echo ‘<th scope=”row”>熊掌 APPID</th>’;
echo ‘<td><input class=”all-options” type=”text” name=”APPID” value=”‘.$Fanly[‘APPID’].'” /></td>’;
echo ‘</tr>’;echo ‘<tr valign=”top”>’;
echo ‘<th scope=”row”>熊掌 Token</th>’;
echo ‘<td><input class=”all-options” type=”text” name=”APPToken” value=”‘.$Fanly[‘APPToken’].'” /></td>’;
echo ‘</tr>’;echo ‘<tr valign=”top”>’;
echo ‘<th scope=”row”>文章类型支持</th>’;
echo ‘<td>’;
$args = array(‘public’ => true,);
$post_types = get_post_types($args);
foreach ( $post_types as $post_type ) {
if($post_type != ‘attachment’){
$postType = get_post_type_object($post_type);
echo ‘<label><input type=”checkbox” name=”Types[]” value=”‘.$post_type.'” ‘;
if(is_array($Fanly[‘Types’])) {if(in_array($post_type,$Fanly[‘Types’])) echo ‘checked’;}
echo ‘>’.$postType->labels->singular_name.’ </label>’;
}
}
echo ‘</td></tr>’;echo ‘<tr valign=”top”>’;
echo ‘<th scope=”row”>是否默认提交数据</th>’;
echo ‘<td><label><input value=”true” type=”checkbox” name=”Default” ‘.$Default.’> 勾选后默认都提交数据到百度,文章发布时可修改!</label></td>’;
echo ‘</tr>’;echo ‘</table>’;
echo ‘<p class=”submit”>’;
echo ‘<input type=”submit” name=”FanlySubmit” id=”submit” class=”button button-primary” value=”保存更改” />’;
echo ‘ <input type=”submit” name=”check_updated” class=”button” value=”检测更新” />’;
echo ‘</p>’;
echo ‘</form>’;
echo ‘<p><strong>使用提示</strong>:<br>
熊掌 APPID/Token 通过 百度搜索资源平台·移动专区->站点资源管理->天级收录[API接口]/周级收录 -> 接口调用地址<br>
</p>’;
echo ‘</div>’;
}
?>
1.《wordpress免插件实现百度熊掌号推送工具》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《wordpress免插件实现百度熊掌号推送工具》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/study/44.html