maybe_sync( $post->ID ); } /** * @param int $post_id 文章 ID。 * @param \WP_Post $post 文章对象。 * @param bool $update 是否更新。 */ public function after_classic_save( $post_id, $post, $update ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return; } if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { return; } $this->maybe_sync( $post_id ); } /** * @param int $post_id 文章 ID。 */ private function maybe_sync( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( ! Lesending_Plugin::is_remote_enabled() ) { return; } if ( ! Lesending_Plugin::current_user_can_sync( $post_id ) ) { return; } $mode = get_post_meta( $post_id, '_lesending_target_mode', true ); if ( ! is_string( $mode ) || $mode === '' ) { $mode = 'local_only'; } if ( 'local_only' === $mode ) { return; } $site_ids = get_post_meta( $post_id, '_lesending_remote_site_ids', true ); if ( ! is_array( $site_ids ) || empty( $site_ids ) ) { if ( 'local_and_remote' === $mode || 'remote_only' === $mode ) { $this->store_log( $post_id, array( 'ok' => false, 'message' => __( '未选择任何远程站点。', 'lesending' ), ) ); } return; } $post = get_post( $post_id ); if ( ! $post instanceof WP_Post ) { return; } if ( 'auto-draft' === $post->post_status ) { return; } $site_ids = array_values( array_map( 'strval', $site_ids ) ); sort( $site_ids ); $sync_hash = md5( wp_json_encode( array( $mode, $site_ids, $post->post_title, $post->post_content, $post->post_excerpt, $post->post_status, (int) get_post_thumbnail_id( $post_id ), Lesending_Plugin::is_media_sync_enabled(), ) ) ); $prev_hash = get_post_meta( $post_id, '_lesending_sync_hash', true ); $last_log = get_post_meta( $post_id, '_lesending_last_sync_log', true ); $last_ok = true; if ( is_string( $last_log ) && $last_log !== '' ) { $decoded = json_decode( $last_log, true ); if ( is_array( $decoded ) && array_key_exists( 'ok', $decoded ) ) { $last_ok = (bool) $decoded['ok']; } } if ( $sync_hash === $prev_hash && $last_ok ) { return; } $map = get_post_meta( $post_id, '_lesending_remote_post_ids', true ); if ( ! is_array( $map ) ) { $map = array(); } $results = array(); $all_ok = true; foreach ( $site_ids as $sid ) { $site = Lesending_Plugin::get_site_by_id( (string) $sid ); if ( ! $site || empty( $site['enabled'] ) ) { $results[ $sid ] = array( 'ok' => false, 'message' => __( '站点不存在或未启用。', 'lesending' ), ); $all_ok = false; continue; } $existing = isset( $map[ $sid ] ) ? (int) $map[ $sid ] : null; $res = Lesending_Remote_Api::upsert_post( $site, $post, $existing ); if ( is_wp_error( $res ) ) { $results[ $sid ] = array( 'ok' => false, 'message' => $res->get_error_message(), ); $all_ok = false; } else { $map[ $sid ] = $res['id']; $results[ $sid ] = array( 'ok' => true, 'message' => $res['link'], ); } } update_post_meta( $post_id, '_lesending_remote_post_ids', $map ); $this->store_log( $post_id, array( 'sites' => $results, 'ok' => $all_ok ) ); update_post_meta( $post_id, '_lesending_sync_hash', $sync_hash ); if ( 'remote_only' === $mode && $all_ok ) { wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft', ) ); update_post_meta( $post_id, '_lesending_target_mode', 'local_only' ); update_post_meta( $post_id, '_lesending_remote_site_ids', array() ); delete_post_meta( $post_id, '_lesending_sync_hash' ); set_transient( 'lesending_notice_' . get_current_user_id(), __( '已按「仅远程站点」将文章同步到远程;本地已保存为草稿,并已切回「仅本网站」以免后续保存把远程改为草稿。', 'lesending' ), 60 ); } elseif ( ! $all_ok ) { set_transient( 'lesending_notice_' . get_current_user_id(), __( '远程同步部分失败,请查看文章内的同步记录。', 'lesending' ), 120 ); } } /** * @param int $post_id 文章 ID。 * @param array $log 日志结构。 */ private function store_log( $post_id, $log ) { update_post_meta( $post_id, '_lesending_last_sync_log', wp_json_encode( $log ) ); update_post_meta( $post_id, '_lesending_last_sync_at', time() ); } }