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); ?>

' . esc_html(__('百度资源提交', 'aksrt')) . '' . $body . ''; return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 面板 HTML 已在输出前逐项转义。 echo $body; } /** * 汇总推送结果行(展示项与参考主题对齐)。 * * @param array $record 推送记录。 * @return array */ 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; } }