| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: Bing 站长工� |
| 4 |
� |
| 5 |
URI: https://blog.wpjam.com/m/bing-webmaster/ |
| 6 |
Description: Bing 站长工� |
| 7 |
�扩展实现提交链接到 Microsoft Bing,让博客的文章能够更快被 Bing 收录。 |
| 8 |
Version: 1.0 |
| 9 |
*/ |
| 10 |
class WPJAM_Bing_Webmaster{ |
| 11 |
public static function __callStatic($method, $args){ |
| 12 |
return wpjam_call_option(static::class, $method, ...$args); |
| 13 |
} |
| 14 |
|
| 15 |
public static function submittable(){ |
| 16 |
return self::get_setting('bing_site_url') && self::get_setting('bing_api_key'); |
| 17 |
} |
| 18 |
|
| 19 |
public static function get_fields(){ |
| 20 |
return [ |
| 21 |
'bing_site_url' => ['title'=>'站点', 'type'=>'url', 'required', 'class'=>'all-options', 'value'=>untrailingslashit(site_url())], |
| 22 |
'bing_api_key' => ['title'=>'密钥', 'type'=>'password', 'required'], |
| 23 |
]; |
| 24 |
} |
| 25 |
|
| 26 |
public static function get_menu_page(){ |
| 27 |
return [ |
| 28 |
'tab_slug' => 'bing-webmaster', |
| 29 |
'summary' => __FILE__, |
| 30 |
]+(self::submittable() ? [ |
| 31 |
'function' => 'form', |
| 32 |
'submit_text' => '批量提交', |
| 33 |
'callback' => [self::class, 'batch_submit'], |
| 34 |
'fields' => fn()=> ['view'=>[ |
| 35 |
'type' => 'view', |
| 36 |
'value' => '已设置 Bing Webmaster 的站点和密钥('.wpjam_get_page_button('set_bing_webmaster', ['button_text' => '修改', 'class'=>'']).'),可以使用 Bing 站长工� |
| 37 |
�更新� |
| 38 |
容接口批量将博客中的所有� |
| 39 |
容都提交给 Bing 站长平台。' |
| 40 |
]], |
| 41 |
'actions' => ['set_bing_webmaster'=>[ |
| 42 |
'title' => '设置', |
| 43 |
'submit_text' => '设置', |
| 44 |
'validate' => true, |
| 45 |
'dismiss' => true, |
| 46 |
'response' => 'redirect', |
| 47 |
'fields' => [self::class, 'get_fields'], |
| 48 |
'value_callback' => [self::class, 'get_setting'], |
| 49 |
'callback' => [self::class, 'update_setting'] |
| 50 |
]] |
| 51 |
] : []); |
| 52 |
} |
| 53 |
|
| 54 |
public static function submit($urls){ |
| 55 |
if(!self::submittable()){ |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$site_url = untrailingslashit(self::get_setting('bing_site_url')); |
| 60 |
$api_key = self::get_setting('bing_api_key'); |
| 61 |
$current = untrailingslashit(site_url()); |
| 62 |
|
| 63 |
if($current != $site_url){ |
| 64 |
$urls = wpjam_map($urls, fn($url)=> str_replace($current, $site_url, $url)); |
| 65 |
} |
| 66 |
|
| 67 |
$api_url = 'https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey='.$api_key; |
| 68 |
// $api_url = 'https://www.bing.com/webmaster/api.svc/json/WPSubmitUrl?apikey='.$api_key; |
| 69 |
|
| 70 |
$err_args = ['errcode'=>'ErrorCode', 'errmsg'=>'Message']; |
| 71 |
|
| 72 |
return wpjam_remote_request($api_url, [ |
| 73 |
'json_encode' => true, |
| 74 |
'sslverify' => false, |
| 75 |
'blocking' => false, |
| 76 |
'body' => ['siteUrl'=>$site_url, 'urlList'=>$urls] |
| 77 |
], $err_args); |
| 78 |
} |
| 79 |
|
| 80 |
public static function submit_post($post_id){ |
| 81 |
if(!self::submittable()){ |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if(is_array($post_id)){ |
| 86 |
$wp_error = false; |
| 87 |
$post_ids = $post_id; |
| 88 |
}else{ |
| 89 |
$wp_error = wp_doing_ajax(); |
| 90 |
$post_ids = [$post_id]; |
| 91 |
} |
| 92 |
|
| 93 |
$urls = []; |
| 94 |
|
| 95 |
foreach($post_ids as $post_id){ |
| 96 |
if(get_post_status($post_id) == 'publish'){ |
| 97 |
if(wp_cache_get($post_id, 'bing_webmaster_notified') === false){ |
| 98 |
wp_cache_set($post_id, true, 'bing_webmaster_notified', HOUR_IN_SECONDS); |
| 99 |
|
| 100 |
$urls[] = get_permalink($post_id); |
| 101 |
}else{ |
| 102 |
$wp_error && wp_die('一小时� |
| 103 |
已经提交过了'); |
| 104 |
} |
| 105 |
}else{ |
| 106 |
$wp_error && wp_die('未发布的文章不能同步到 Bing'); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if(!$urls){ |
| 111 |
$wp_error && wp_die('没有需要提交到 Bing 的链接'); |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return self::submit($urls); |
| 117 |
} |
| 118 |
|
| 119 |
public static function batch_submit(){ |
| 120 |
$submited = (int)self::get_setting('submited'); |
| 121 |
|
| 122 |
if(time() - (int)self::get_setting('last') < DAY_IN_SECONDS){ |
| 123 |
if($submited == -1){ |
| 124 |
return ['notice'=>'所有页面都已提交']; |
| 125 |
}else{ |
| 126 |
wp_die('批量提交的� |
| 127 |
�额已用完,请稍后重试'); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if($submited == -1){ |
| 132 |
$submited = 0; |
| 133 |
} |
| 134 |
|
| 135 |
$per_page = 500; |
| 136 |
$per_day = 9000; |
| 137 |
$offset = (int)wpjam_param('offset', 'data'); |
| 138 |
|
| 139 |
if($offset >= $per_day){ |
| 140 |
self::update_setting('last', time()); |
| 141 |
self::update_setting('submited', $submited+$offset-$per_page); |
| 142 |
|
| 143 |
wp_die('今日批量提交的� |
| 144 |
�额已用完,请明日接着提交'); |
| 145 |
} |
| 146 |
|
| 147 |
$query = new WP_Query([ |
| 148 |
'post_type' => 'any', |
| 149 |
'post_status' => 'publish', |
| 150 |
'order' => 'ASC', |
| 151 |
'posts_per_page' => 500, |
| 152 |
'fields' => 'ids', |
| 153 |
'offset' => $offset+$submited |
| 154 |
]); |
| 155 |
|
| 156 |
if($query->have_posts()){ |
| 157 |
self::submit_post($query->posts); |
| 158 |
|
| 159 |
$count = count($query->posts); |
| 160 |
}else{ |
| 161 |
$count = 0; |
| 162 |
} |
| 163 |
|
| 164 |
$number = $offset+$count; |
| 165 |
|
| 166 |
if($count < $per_page){ |
| 167 |
self::update_setting('last', time()); |
| 168 |
self::update_setting('submited', -1); |
| 169 |
|
| 170 |
return ['notice' => '提交成功,本次提交了'.$number.'个页面。']; |
| 171 |
}else{ |
| 172 |
return [ |
| 173 |
'notice' => ['type'=>'info', 'message'=>'批量提交中,请勿� |
| 174 |
�闭浏览器,本次提交了'.$number.'个页面。'], |
| 175 |
'args' => ['offset'=>$number] |
| 176 |
]; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public static function on_after_insert_post($post_id, $post, $update, $post_before){ |
| 181 |
if((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) |
| 182 |
|| $post->post_status != 'publish' |
| 183 |
|| !current_user_can('edit_post', $post_id) |
| 184 |
){ |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
self::submit_post($post_id); |
| 189 |
} |
| 190 |
|
| 191 |
public static function on_publish_future_post($post_id){ |
| 192 |
self::submit_post($post_id); |
| 193 |
} |
| 194 |
|
| 195 |
public static function builtin_page_load($screen){ |
| 196 |
if(!self::submittable()){ |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
if($screen->base == 'edit'){ |
| 201 |
if(is_post_type_viewable($screen->post_type)){ |
| 202 |
wpjam_register_list_table_action('submit_bing', [ |
| 203 |
'title' => '提交到 Bing', |
| 204 |
'post_status' => ['publish'], |
| 205 |
'callback' => [self::class, 'submit_post'], |
| 206 |
'bulk_callback' => [self::class, 'submit_post'], |
| 207 |
'row_action' => false, |
| 208 |
'bulk' => true, |
| 209 |
'direct' => true |
| 210 |
]); |
| 211 |
} |
| 212 |
}elseif($screen->base == 'post'){ |
| 213 |
is_post_type_viewable($screen->post_type) &&add_action('wp_after_insert_post', [self::class, 'on_after_insert_post'], 10, 4); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public static function add_hooks(){ |
| 218 |
add_action('publish_future_post', [self::class, 'on_publish_future_post'], 11); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
wpjam_register_option('wpjam-seo', [ |
| 223 |
'plugin_page' => 'wpjam-seo', |
| 224 |
'current_tab' => 'bing-webmaster', |
| 225 |
'model' => 'WPJAM_Bing_Webmaster', |
| 226 |
'title' => 'Bing Webmaster', |
| 227 |
'ajax' => false, |
| 228 |
'admin_load' => ['base'=>['post','edit']] |
| 229 |
]); |