| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* 插件启用、删除、禁用等 |
| 5 |
*/ |
| 6 |
class Ggpush_Plugin |
| 7 |
{ |
| 8 |
// 启用插件 |
| 9 |
public static function plugin_activation() |
| 10 |
{ |
| 11 |
global $wpdb; |
| 12 |
$table_name = $wpdb->prefix . 'ggpush_records'; |
| 13 |
$charset_collate = $wpdb->get_charset_collate(); |
| 14 |
$sql = <<<SQL |
| 15 |
CREATE TABLE {$table_name} ( |
| 16 |
`record_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 17 |
`record_platform` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送平台:1 百度,2 360,3 搜狗,4 头条,5 神马,6 bing,7 谷歌,8 indexnow,9 yandex,10 Seznam.cz', |
| 18 |
`record_mode` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送方式:1 普通收录,2 快速抓取,3 js提交,4 api提交,5 indexnow', |
| 19 |
`record_urls` LONGTEXT NULL DEFAULT NULL COMMENT '推送链接' , |
| 20 |
`record_num` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '推送链接数量', |
| 21 |
`record_result` TEXT NULL DEFAULT NULL COMMENT '推送结果' , |
| 22 |
`record_result_code` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '200' COMMENT '推送结果状态码', |
| 23 |
`record_result_status` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送状态:1 成功,2 失败,3 未知', |
| 24 |
`record_result_error` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '失败原因' , |
| 25 |
`record_date` DATETIME NULL DEFAULT NULL COMMENT '推送时间', |
| 26 |
PRIMARY KEY (`record_id`) USING BTREE |
| 27 |
) {$charset_collate}; |
| 28 |
SQL; |
| 29 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 30 |
// 如果表不存在才会执行创建 |
| 31 |
maybe_create_table($table_name, $sql); |
| 32 |
// 创建默认� |
| 33 |
�置 |
| 34 |
global $ggpush_options; |
| 35 |
if (empty($ggpush_options)) { |
| 36 |
add_option('ggpush_options', array( |
| 37 |
'menu_position' => 100, |
| 38 |
'push_timeout' => 30, |
| 39 |
)); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// 删除插件执行的代码 |
| 44 |
public static function plugin_uninstall() |
| 45 |
{ |
| 46 |
// 删除表 |
| 47 |
global $wpdb; |
| 48 |
global $ggpush_options; |
| 49 |
$ggpush_options = get_option('ggpush_options', array()); |
| 50 |
$table_name = $wpdb->prefix . 'ggpush_records'; |
| 51 |
$wpdb->query('DROP TABLE IF EXISTS `' . $table_name . '`'); |
| 52 |
// 删除IndexNow密钥文件 |
| 53 |
Ggpush_Api::delete_indexnow_keyfile(); |
| 54 |
// 删除� |
| 55 |
�置 |
| 56 |
delete_option('ggpush_options'); |
| 57 |
} |
| 58 |
|
| 59 |
// 禁用插件执行的代码 |
| 60 |
public static function plugin_deactivation() |
| 61 |
{ |
| 62 |
// 插件已禁用,删除定时任务 |
| 63 |
Ggpush_Cron::update_cron(false); |
| 64 |
} |
| 65 |
|
| 66 |
// 初始化 |
| 67 |
public static function admin_init() |
| 68 |
{ |
| 69 |
// 注册设置页面 |
| 70 |
if (!empty($_REQUEST['page'])) { |
| 71 |
if ('ggpush-timing-push' === $_REQUEST['page']) { |
| 72 |
// 定时推送 |
| 73 |
Ggpush_Task::init_page(); |
| 74 |
} else if ('ggpush-settings' === $_REQUEST['page']) { |
| 75 |
// 推送设置 |
| 76 |
Ggpush_Settings::init_page(); |
| 77 |
} else if ('ggpush-assist-push' === $_REQUEST['page']) { |
| 78 |
// � |
| 79 |
助推送 |
| 80 |
Ggpush_Assist::init_page(); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// 添加菜单 |
| 86 |
public static function admin_menu() |
| 87 |
{ |
| 88 |
global $ggpush_options; |
| 89 |
$position = null; |
| 90 |
if (!empty($ggpush_options['menu_position'])) { |
| 91 |
$position = intval($ggpush_options['menu_position']); |
| 92 |
} |
| 93 |
$push_record = 0; |
| 94 |
if (!empty($ggpush_options['push_record'])) { |
| 95 |
$push_record = intval($ggpush_options['push_record']); |
| 96 |
} |
| 97 |
// 父菜单 |
| 98 |
add_menu_page( |
| 99 |
'果果推送', |
| 100 |
'果果推送', |
| 101 |
'manage_options', |
| 102 |
'#ggpush', |
| 103 |
null, |
| 104 |
'dashicons-admin-links', |
| 105 |
$position |
| 106 |
); |
| 107 |
|
| 108 |
// 推送记录页面 |
| 109 |
if (0 === $push_record) { |
| 110 |
add_submenu_page( |
| 111 |
'#ggpush', |
| 112 |
'推送记录', |
| 113 |
'推送记录', |
| 114 |
'manage_options', |
| 115 |
'ggpush-record', |
| 116 |
array('Ggpush_Record', 'home') |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
// 推送设置 |
| 121 |
add_submenu_page( |
| 122 |
'#ggpush', |
| 123 |
'推送设置', |
| 124 |
'推送设置', |
| 125 |
'manage_options', |
| 126 |
'ggpush-settings', |
| 127 |
array('Ggpush_Settings', 'show_page') |
| 128 |
); |
| 129 |
|
| 130 |
// 定时推送 |
| 131 |
add_submenu_page( |
| 132 |
'#ggpush', |
| 133 |
'定时推送', |
| 134 |
'定时推送', |
| 135 |
'manage_options', |
| 136 |
'ggpush-timing-push', |
| 137 |
array('Ggpush_Task', 'show_page') |
| 138 |
); |
| 139 |
|
| 140 |
// � |
| 141 |
助推送 |
| 142 |
add_submenu_page( |
| 143 |
'#ggpush', |
| 144 |
'� |
| 145 |
助推送', |
| 146 |
'� |
| 147 |
助推送', |
| 148 |
'manage_options', |
| 149 |
'ggpush-assist-push', |
| 150 |
array('Ggpush_Assist', 'show_page') |
| 151 |
); |
| 152 |
|
| 153 |
remove_submenu_page('#ggpush', '#ggpush'); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* 在插件页面添加设置链接 |
| 158 |
* @param $links |
| 159 |
* @return mixed |
| 160 |
*/ |
| 161 |
public static function setups($links) |
| 162 |
{ |
| 163 |
$business_link = '<a href="https://www.ggdoc.cn/plugin/1.html" target="_blank">商业版</a>'; |
| 164 |
array_unshift($links, $business_link); |
| 165 |
|
| 166 |
array_unshift($links, '<a href="https://www.ggdoc.cn/archives/product_type/theme" target="_blank">主题</a>'); |
| 167 |
array_unshift($links, '<a href="https://www.ggdoc.cn/archives/product_type/plugin" target="_blank">插件</a>'); |
| 168 |
|
| 169 |
$setups = '<a href="admin.php?page=ggpush-settings">设置</a>'; |
| 170 |
array_unshift($links, $setups); |
| 171 |
|
| 172 |
return $links; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* 表单输� |
| 177 |
�框回调 |
| 178 |
* @param array $args 这数据就是add_settings_field方法中第6个参数($args)的数据 |
| 179 |
*/ |
| 180 |
public static function field_callback($args) |
| 181 |
{ |
| 182 |
// 表单的id或name字段 |
| 183 |
$id = $args['label_for']; |
| 184 |
// 表单的名称 |
| 185 |
$input_name = 'ggpush_options[' . $id . ']'; |
| 186 |
// 获取表单选项中的值 |
| 187 |
global $ggpush_options; |
| 188 |
// 表单的值 |
| 189 |
$input_value = isset($ggpush_options[$id]) ? $ggpush_options[$id] : ''; |
| 190 |
// 表单的类型 |
| 191 |
$form_type = isset($args['form_type']) ? $args['form_type'] : 'input'; |
| 192 |
// 输� |
| 193 |
�表单说明 |
| 194 |
$form_desc = isset($args['form_desc']) ? $args['form_desc'] : ''; |
| 195 |
// 输� |
| 196 |
�表单type |
| 197 |
$type = isset($args['type']) ? $args['type'] : 'text'; |
| 198 |
// 输� |
| 199 |
�表单placeholder |
| 200 |
$form_placeholder = isset($args['form_placeholder']) ? $args['form_placeholder'] : ''; |
| 201 |
// 下拉框等选项值 |
| 202 |
$form_data = isset($args['form_data']) ? $args['form_data'] : array(); |
| 203 |
// 扩展form表单属性 |
| 204 |
$form_extend = isset($args['form_extend']) ? $args['form_extend'] : array(); |
| 205 |
switch ($form_type) { |
| 206 |
case 'input': |
| 207 |
self::generate_input( |
| 208 |
array_merge( |
| 209 |
array( |
| 210 |
'id' => $id, |
| 211 |
'type' => $type, |
| 212 |
'placeholder' => $form_placeholder, |
| 213 |
'name' => $input_name, |
| 214 |
'value' => $input_value, |
| 215 |
'class' => 'regular-text', |
| 216 |
), |
| 217 |
$form_extend |
| 218 |
)); |
| 219 |
break; |
| 220 |
case 'select': |
| 221 |
self::generate_select( |
| 222 |
array_merge( |
| 223 |
array( |
| 224 |
'id' => $id, |
| 225 |
'placeholder' => $form_placeholder, |
| 226 |
'name' => $input_name |
| 227 |
), |
| 228 |
$form_extend |
| 229 |
), |
| 230 |
$form_data, |
| 231 |
$input_value |
| 232 |
); |
| 233 |
break; |
| 234 |
case 'checkbox': |
| 235 |
self::generate_checkbox( |
| 236 |
array_merge( |
| 237 |
array( |
| 238 |
'name' => $input_name . '[]', |
| 239 |
'input_name' => $input_name, |
| 240 |
), |
| 241 |
$form_extend |
| 242 |
), |
| 243 |
$form_data, |
| 244 |
$input_value |
| 245 |
); |
| 246 |
break; |
| 247 |
case 'textarea': |
| 248 |
self::generate_textarea( |
| 249 |
array_merge( |
| 250 |
array( |
| 251 |
'id' => $id, |
| 252 |
'placeholder' => $form_placeholder, |
| 253 |
'name' => $input_name, |
| 254 |
'class' => 'large-text code', |
| 255 |
'rows' => 5, |
| 256 |
), |
| 257 |
$form_extend |
| 258 |
), |
| 259 |
$input_value |
| 260 |
); |
| 261 |
break; |
| 262 |
} |
| 263 |
if (!empty($form_desc)) { |
| 264 |
?> |
| 265 |
<p class="description"><?php echo esc_html($form_desc); ?></p> |
| 266 |
<?php |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* 生成textarea表单 |
| 272 |
* @param array $form_data 标签上的属性数组 |
| 273 |
* @param string $value 默认值 |
| 274 |
* @return void |
| 275 |
*/ |
| 276 |
public static function generate_textarea($form_data, $value = '') |
| 277 |
{ |
| 278 |
?><textarea <?php |
| 279 |
foreach ($form_data as $k => $v) { |
| 280 |
echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php |
| 281 |
} ?>><?php echo esc_textarea($value); ?></textarea> |
| 282 |
<?php |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* 生成checkbox表单 |
| 287 |
* @param array $form_data 标签上的属性数组 |
| 288 |
* @param array $checkboxs 下拉列表数据 |
| 289 |
* @param string|array $value 选中值,单个选中字符串,多个选中数组 |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
public static function generate_checkbox($form_data, $checkboxs, $value = '') |
| 293 |
{ |
| 294 |
?> |
| 295 |
<fieldset> |
| 296 |
<p> |
| 297 |
<input type="hidden" name="<?php echo esc_attr($form_data['input_name']); ?>" value=""> |
| 298 |
<?php |
| 299 |
$len = count($checkboxs); |
| 300 |
foreach ($checkboxs as $k => $checkbox) { |
| 301 |
$checked = ''; |
| 302 |
if (!empty($value)) { |
| 303 |
if (is_array($value)) { |
| 304 |
if (in_array($checkbox['value'], $value)) { |
| 305 |
$checked = 'checked'; |
| 306 |
} |
| 307 |
} else { |
| 308 |
if ($checkbox['value'] == $value) { |
| 309 |
$checked = 'checked'; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
?> |
| 314 |
<label> |
| 315 |
<input type="checkbox" <?php checked($checked, 'checked'); ?><?php |
| 316 |
foreach ($form_data as $k2 => $v2) { |
| 317 |
echo esc_attr($k2); ?>="<?php echo esc_attr($v2); ?>" <?php |
| 318 |
} ?> value="<?php echo esc_attr($checkbox['value']); ?>" |
| 319 |
><?php echo esc_html($checkbox['title']); ?> |
| 320 |
</label> |
| 321 |
<?php |
| 322 |
if ($k < ($len - 1)) { |
| 323 |
?> |
| 324 |
<br> |
| 325 |
<?php |
| 326 |
} |
| 327 |
} |
| 328 |
?> |
| 329 |
</p> |
| 330 |
</fieldset> |
| 331 |
<?php |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* 生成input表单 |
| 336 |
* @param array $form_data 标签上的属性数组 |
| 337 |
* @return void |
| 338 |
*/ |
| 339 |
public static function generate_input($form_data) |
| 340 |
{ |
| 341 |
?><input <?php |
| 342 |
foreach ($form_data as $k => $v) { |
| 343 |
echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php |
| 344 |
} ?>><?php |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* 生成select表单 |
| 349 |
* @param array $form_data 标签上的属性数组 |
| 350 |
* @param array $selects 下拉列表数据 |
| 351 |
* @param string|array $value 选中值,单个选中字符串,多个选中数组 |
| 352 |
* @return void |
| 353 |
*/ |
| 354 |
public static function generate_select($form_data, $selects, $value = '') |
| 355 |
{ |
| 356 |
?><select <?php |
| 357 |
foreach ($form_data as $k => $v) { |
| 358 |
echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php |
| 359 |
} ?>><?php |
| 360 |
foreach ($selects as $select) { |
| 361 |
$selected = ''; |
| 362 |
if (!empty($value)) { |
| 363 |
if (is_array($value)) { |
| 364 |
if (in_array($select['value'], $value)) { |
| 365 |
$selected = 'selected'; |
| 366 |
} |
| 367 |
} else { |
| 368 |
if ($select['value'] == $value) { |
| 369 |
$selected = 'selected'; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
?> |
| 374 |
<option <?php selected($selected, 'selected'); ?> |
| 375 |
value="<?php echo esc_attr($select['value']); ?>"><?php echo esc_html($select['title']); ?></option> |
| 376 |
<?php |
| 377 |
} |
| 378 |
?> |
| 379 |
</select> |
| 380 |
<?php |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* 统一设置保存变量 |
| 385 |
* @param $input |
| 386 |
* @return array |
| 387 |
*/ |
| 388 |
public static function sanitize($input) |
| 389 |
{ |
| 390 |
global $ggpush_options; |
| 391 |
if (empty($input)) { |
| 392 |
return $ggpush_options; |
| 393 |
} |
| 394 |
// 更新indexnow密钥文件 |
| 395 |
if (isset($input['indexnow_token'])) { |
| 396 |
Ggpush_Api::update_indexnow_keyfile($input['indexnow_token'], isset($ggpush_options['indexnow_token']) ? $ggpush_options['indexnow_token'] : ''); |
| 397 |
} |
| 398 |
if (empty($ggpush_options)) { |
| 399 |
return $input; |
| 400 |
} else { |
| 401 |
return array_merge($ggpush_options, $input); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* 更新� |
| 407 |
�置 |
| 408 |
* @param array $options |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
public static function update_options($options) |
| 412 |
{ |
| 413 |
global $ggpush_options; |
| 414 |
$ggpush_options = self::sanitize($options); |
| 415 |
update_option('ggpush_options', $ggpush_options); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
/** |
| 420 |
* 获取存储的设置值 |
| 421 |
* @param string $option 设置的键 |
| 422 |
* @param mixed $def_value 默认值 |
| 423 |
* @return mixed|string |
| 424 |
*/ |
| 425 |
public static function get_option($option, $def_value = '') |
| 426 |
{ |
| 427 |
global $ggpush_options; |
| 428 |
if (!empty($ggpush_options[$option])) { |
| 429 |
return $ggpush_options[$option]; |
| 430 |
} |
| 431 |
return $def_value; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* 获取GET中的参数值 |
| 436 |
* @param string $name |
| 437 |
* @param mixed $defValue |
| 438 |
* @return mixed |
| 439 |
*/ |
| 440 |
public static function get($name, $defValue = '') |
| 441 |
{ |
| 442 |
if (isset($_GET[$name])) { |
| 443 |
return $_GET[$name]; |
| 444 |
} |
| 445 |
return $defValue; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* 设置上一次错误信息 |
| 450 |
* @param string $error |
| 451 |
* @return void |
| 452 |
*/ |
| 453 |
public static function set_error($error) |
| 454 |
{ |
| 455 |
global $ggpush_error; |
| 456 |
$ggpush_error = $error; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* 获取上一次的错误信息 |
| 461 |
* @return string |
| 462 |
*/ |
| 463 |
public static function get_error() |
| 464 |
{ |
| 465 |
global $ggpush_error; |
| 466 |
return $ggpush_error; |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* 添加JavaScript文件 |
| 471 |
* @return void |
| 472 |
*/ |
| 473 |
public static function admin_enqueue_scripts() |
| 474 |
{ |
| 475 |
global $ggpush_options; |
| 476 |
// 发布文章后推送 |
| 477 |
if (!empty($ggpush_options['publish_article_platform'])) { |
| 478 |
self::push_after_post(); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* 发布文章后推送处理的js |
| 484 |
* @return void |
| 485 |
*/ |
| 486 |
public static function push_after_post() |
| 487 |
{ |
| 488 |
global $pagenow; |
| 489 |
if (!empty($pagenow)) { |
| 490 |
// 发布文章后推送 |
| 491 |
if ('post.php' === $pagenow) { |
| 492 |
// 添加静态文件 |
| 493 |
wp_enqueue_script( |
| 494 |
'ggpush-sync', |
| 495 |
plugins_url('/js/ggpush_publish.min.js', GGPUSH_PLUGIN_FILE), |
| 496 |
array('jquery'), |
| 497 |
'0.0.4', |
| 498 |
true |
| 499 |
); |
| 500 |
wp_localize_script( |
| 501 |
'ggpush-sync', |
| 502 |
'ggpush_obj', |
| 503 |
array( |
| 504 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 505 |
'nonce' => wp_create_nonce('ggpush'), |
| 506 |
) |
| 507 |
); |
| 508 |
} else if ('post-new.php' === $pagenow) { |
| 509 |
global $post; |
| 510 |
$post_id = 0; |
| 511 |
if (!empty($post->ID)) { |
| 512 |
$post_id = $post->ID; |
| 513 |
} else if (!empty($_GET['post'])) { |
| 514 |
$post_id = intval($_GET['post']); |
| 515 |
} |
| 516 |
set_transient('ggpush_publish_post_id', $post_id); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* 发布文章后推送 |
| 523 |
* @return void |
| 524 |
*/ |
| 525 |
public static function wp_ajax_ggpush_publish() |
| 526 |
{ |
| 527 |
check_ajax_referer('ggpush'); |
| 528 |
global $ggpush_options; |
| 529 |
if (empty($ggpush_options['publish_article_platform'])) { |
| 530 |
wp_send_json(array( |
| 531 |
'status' => 0, |
| 532 |
'msg' => '未开启发布文章后推送', |
| 533 |
)); |
| 534 |
} |
| 535 |
$post_id = get_transient('ggpush_publish_post_id'); |
| 536 |
if (empty($post_id)) { |
| 537 |
wp_send_json(array( |
| 538 |
'status' => 0, |
| 539 |
'msg' => '非发布的新文章', |
| 540 |
)); |
| 541 |
} |
| 542 |
if (!empty($post_id) && !wp_is_post_revision($post_id) && 'publish' === get_post_status($post_id)) { |
| 543 |
// 推送链接 |
| 544 |
$post_url = get_permalink($post_id); |
| 545 |
Ggpush_Api::platform_push($post_url, $ggpush_options['publish_article_platform']); |
| 546 |
delete_transient('ggpush_publish_post_id'); |
| 547 |
wp_send_json(array( |
| 548 |
'url' => $post_url, |
| 549 |
'status' => 1, |
| 550 |
'msg' => '链接已推送', |
| 551 |
)); |
| 552 |
} |
| 553 |
wp_send_json(array( |
| 554 |
'status' => 0, |
| 555 |
'msg' => '链接推送失败', |
| 556 |
)); |
| 557 |
} |
| 558 |
} |