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