Files
wordpress-theme-aksrt/src/Extensions/SearchPushMetaBox.php
T
kate522 cf39e72126 feat(extensions): align Baidu push with reference theme
与参考主题(zibll)1:1 对齐百度推送,并清掉存量双路与死代码:

- 新增普通/快速收录通道开关,快速收录走 &type=daily;自动推送 JS 与参考实现一致

- 分类法 saved_term 推送(可推送类型推导公开分类法,排除 post_format,aksrt_baidu_push_taxonomies 可过滤)

- 推送结果写入 meta 去重,成功后不重复提交;编辑页 Metabox 展示结果并提供「重新提交」(nonce 校验后强制推送)

- 配置面收敛到扩展页单一 canonical 键:删除 SEO 页 seo_baidu_* 重复面与两处合并逻辑,不写迁移兜底

- 清理死代码:push_batch(零调用)及 batch_size、从不写入的 baidu_push_verified、零消费的熊掌号 xzh_* 七键;baidu_push_log_enabled 接入 log()

新增 PHP harness 驱动真实逻辑(通道/去重/重试/重新提交/术语/面板渲染)+ 参考主题契约测试;tests/admin 与 tests/widget-system 全量通过。
2026-10-13 21:01:03 +08:00

191 lines
5.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* @package AKSRT
*/
namespace AKSRT\Extensions;
defined('ABSPATH') || exit;
/**
* 百度推送编辑页面板(对齐参考主题「百度资源提交」Metabox)。
*
* 文章:侧栏 Metabox;分类法:编辑 / 新增表单行。
* 展示推送结果与更新时间,并提供「重新提交」勾选——
* 勾选后保存会清空去重记录并强制再次推送。
*/
class SearchPushMetaBox {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
add_action('add_meta_boxes', [$this, 'register_post_meta_box']);
// 分类法可能由插件延后注册,面板在 admin_init 统一挂载。
add_action('admin_init', [$this, 'register_term_panels']);
}
/**
* 为可推送文章类型注册侧栏 Metabox(推送功能就绪后才展示)。
*/
public function register_post_meta_box(): void {
if (!$this->is_ready()) {
return;
}
foreach (SearchPush::pushable_post_types() as $post_type) {
add_meta_box(
'aksrt_baidu_push',
__('百度资源提交', 'aksrt'),
[$this, 'render_post_panel'],
$post_type,
'side',
'high'
);
}
}
/**
* 为可推送分类法注册编辑 / 新增表单面板。
*/
public function register_term_panels(): void {
if (!$this->is_ready()) {
return;
}
foreach (SearchPush::pushable_taxonomies() as $taxonomy) {
add_action("{$taxonomy}_edit_form_fields", [$this, 'render_term_panel'], 20);
add_action("{$taxonomy}_add_form_fields", [$this, 'render_term_panel'], 20);
}
}
/**
* 渲染文章 Metabox。
*
* @param mixed $post WP_Post 或 null。
*/
public function render_post_panel($post): void {
if (!$post instanceof \WP_Post) {
return;
}
$this->render_panel((int) $post->ID, false);
}
/**
* 渲染分类法表单行(新增表单无术语记录,仅展示提示)。
*
* @param mixed $term WP_Term、分类名字符串或 null。
*/
public function render_term_panel($term = null): void {
$term_id = 0;
if ($term instanceof \WP_Term) {
$term_id = (int) $term->term_id;
} elseif (isset($_GET['tag_ID'])) {
$term_id = (int) $_GET['tag_ID'];
}
// 新增表单无表格结构,仅编辑表单以表单行包裹。
$this->render_panel($term_id, true, $term_id > 0);
}
private function is_ready(): bool {
return class_exists(SearchPush::class) && SearchPush::get_instance()->is_ready();
}
/**
* 渲染推送结果面板。
*
* @param int $object_id 文章 / 术语 ID(0 表示新增表单)。
* @param bool $is_term 是否为术语。
* @param bool $as_row 是否以分类法表单行包裹。
*/
private function render_panel(int $object_id, bool $is_term, bool $as_row = false): void {
ob_start();
wp_nonce_field(SearchPush::NONCE_ACTION, SearchPush::NONCE_FIELD);
$record = $object_id > 0 ? SearchPush::get_push_record($object_id, $is_term) : [];
$lines = $this->result_lines($record);
?>
<div class="aksrt-push-meta-box">
<?php if ($lines) : ?>
<p class="aksrt-push-meta-box__label"><strong><?php esc_html_e('提交结果:', 'aksrt'); ?></strong></p>
<ul class="aksrt-push-meta-box__results">
<?php foreach ($lines as $line) : ?>
<li><strong><?php echo esc_html($line[0]); ?></strong> <span><?php echo esc_html($line[1]); ?></span></li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p class="description"><?php esc_html_e('发布、更新后刷新页面后可查看提交结果', 'aksrt'); ?></p>
<?php endif; ?>
<?php if (!empty($record['update_time'])) : ?>
<p class="aksrt-push-meta-box__resubmit">
<label>
<input type="checkbox" name="<?php echo esc_attr(SearchPush::RESUBMIT_FIELD); ?>" value="1" />
<?php esc_html_e('重新提交', 'aksrt'); ?>
</label>
<span class="description"><?php esc_html_e('勾选后保存将清空提交记录并再次推送', 'aksrt'); ?></span>
</p>
<?php endif; ?>
</div>
<?php
$body = (string) ob_get_clean();
if ($as_row) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 面板 HTML 已在输出前逐项转义。
echo '<tr class="form-field"><th scope="row">' . esc_html(__('百度资源提交', 'aksrt')) . '</th><td>' . $body . '</td></tr>';
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 面板 HTML 已在输出前逐项转义。
echo $body;
}
/**
* 汇总推送结果行(展示项与参考主题对齐)。
*
* @param array $record 推送记录。
* @return array<int,array{0:string,1:string}>
*/
private function result_lines(array $record): array {
$lines = [];
if (!empty($record['normal_push'])) {
$lines[] = [__('普通收录:成功', 'aksrt'), $this->format_result($record['normal_result'] ?? null)];
} elseif (array_key_exists('normal_push', $record)) {
$lines[] = [__('普通收录:失败', 'aksrt'), $this->format_result($record['normal_result'] ?? null)];
}
if (!empty($record['daily_push'])) {
$lines[] = [__('快速收录:成功', 'aksrt'), $this->format_result($record['daily_result'] ?? null)];
} elseif (array_key_exists('daily_push', $record)) {
$lines[] = [__('快速收录:失败', 'aksrt'), $this->format_result($record['daily_result'] ?? null)];
}
if (!empty($record['update_time'])) {
$lines[] = [__('更新时间:', 'aksrt'), (string) $record['update_time']];
}
return $lines;
}
/**
* 格式化接口返回结果。
*
* @param mixed $result 百度接口返回的解码数据。
*/
private function format_result($result): string {
if (is_array($result) || is_object($result)) {
return (string) wp_json_encode($result);
}
return (string) $result;
}
}