#mu-plugins/functions.php #注册专属业务文档 CPT (sc_doc) 并开启修订支持 add_action( 'init', function() { register_post_type( 'sc_doc', array( 'labels' => array( 'name' => '业务文书', 'singular_name' => '文书' ), 'public' => true, 'has_archive' => false, 'supports' => array( 'title', 'editor', 'author', 'revisions' ), // 核心:开启 revisions 'show_in_rest' => true, ) ); } ); #从母版起草文书与版本历史 API(免进后台) 通过 REST API 让前端页面直接完成“起草、版本列表获取、版本对比 add_action( 'rest_api_init', function () { // 1. 从母版克隆新草案 register_rest_route( 'shipchain/v1', '/doc/fork', array( 'methods' => 'POST', 'callback' => 'sc_fork_template_to_doc', 'permission_callback' => function() { return is_user_logged_in(); } ) ); // 2. 获取文档的所有历史修订版本 register_rest_route( 'shipchain/v1', '/doc/(?P\d+)/revisions', array( 'methods' => 'GET', 'callback' => 'sc_get_doc_revisions', 'permission_callback' => function() { return is_user_logged_in(); } ) ); // 3. 对比任意两个版本的文本差异 (Diff) register_rest_route( 'shipchain/v1', '/doc/diff', array( 'methods' => 'GET', 'callback' => 'sc_compare_doc_revisions', 'permission_callback' => function() { return is_user_logged_in(); } ) ); } ); // 1. 克隆实现 function sc_fork_template_to_doc( WP_REST_Request $request ) { $template_id = intval( $request->get_param('template_id') ); $template = get_post( $template_id ); if ( ! $template ) { return new WP_Error( 'not_found', '模板不存在', array( 'status' => 404 ) ); } $current_user_id = get_current_user_id(); $new_doc_id = wp_insert_post( array( 'post_title' => $template->post_title . ' - 草案 (' . date('m-d H:i') . ')', 'post_content' => $template->post_content, 'post_status' => 'draft', 'post_type' => 'sc_doc', 'post_author' => $current_user_id, ) ); // 记录来源母版 update_post_meta( $new_doc_id, '_source_template_id', $template_id ); return rest_ensure_response( array( 'status' => 'success', 'doc_id' => $new_doc_id, 'title' => get_the_title( $new_doc_id ), 'content' => $template->post_content ) ); } // 2. 版本清单实现 function sc_get_doc_revisions( WP_REST_Request $request ) { $doc_id = intval( $request['id'] ); $doc = get_post( $doc_id ); if ( ! $doc || ( $doc->post_author != get_current_user_id() && ! current_user_can('edit_others_posts') ) ) { return new WP_Error( 'forbidden', '无权调阅此文书版本', array( 'status' => 403 ) ); } $revisions = wp_get_post_revisions( $doc_id ); $history = array(); foreach ( $revisions as $rev ) { $history[] = array( 'revision_id' => $rev->ID, 'time' => $rev->post_modified, 'author_name' => get_the_author_meta( 'display_name', $rev->post_author ), 'char_count' => mb_strlen( strip_tags( $rev->post_content ) ) ); } return rest_ensure_response( array( 'doc_id' => $doc_id, 'history' => $history ) ); } // 3. 版本 Diff 差异对比实现 (利用 WP 原生差异比对器) function sc_compare_doc_revisions( WP_REST_Request $request ) { require_once ABSPATH . 'wp-admin/includes/diff.php'; $rev_from_id = intval( $request->get_param('from') ); $rev_to_id = intval( $request->get_param('to') ); $rev_from = get_post( $rev_from_id ); $rev_to = get_post( $rev_to_id ); if ( ! $rev_from || ! $rev_to ) { return new WP_Error( 'not_found', '对比版本不存在', array( 'status' => 404 ) ); } // 原生渲染红绿增删对比 HTML 表格 $diff_table = wp_text_diff( $rev_from->post_content, $rev_to->post_content, array( 'title' => '文书版本差异分析', 'title_left' => '早期版本 (' . $rev_from->post_modified . ')', 'title_right' => '目标版本 (' . $rev_to->post_modified . ')' ) ); return rest_ensure_response( array( 'diff_html' => $diff_table ?: '
两个版本内容一致,无变动差异。
' ) ); }