ID, '_lesending_target_mode', true ); $sel = get_post_meta( $post->ID, '_lesending_remote_site_ids', true ); $sel = is_array( $sel ) ? $sel : array(); $sites = Lesending_Plugin::get_public_site_list(); $modes = array( 'local_only' => __( '仅本网站', 'lesending' ), 'local_and_remote' => __( '本网站 + 远程站点', 'lesending' ), 'remote_only' => __( '仅远程站点(成功后本地改草稿)', 'lesending' ), ); if ( empty( $sites ) ) { echo '

' . esc_html__( '请先在「设置 → Lesending」中添加远程站点。', 'lesending' ) . '

'; return; } echo '

'; echo ''; echo '

' . esc_html__( '远程目标', 'lesending' ) . '

'; foreach ( $sites as $s ) { if ( empty( $s['enabled'] ) ) { continue; } printf( '', esc_attr( $s['id'] ), checked( in_array( (string) $s['id'], array_map( 'strval', $sel ), true ), true, false ), esc_html( $s['name'] ? $s['name'] : $s['url'] ) ); } } /** * @param \WP_Post $post 文章。 */ public function render_log_metabox( $post ) { $raw = get_post_meta( $post->ID, '_lesending_last_sync_log', true ); $at = (int) get_post_meta( $post->ID, '_lesending_last_sync_at', true ); if ( ! is_string( $raw ) || $raw === '' ) { echo '

' . esc_html__( '尚无同步记录。', 'lesending' ) . '

'; return; } $data = json_decode( $raw, true ); if ( $at > 0 ) { echo '

' . esc_html__( '最近同步时间', 'lesending' ) . ': ' . esc_html( wp_date( get_option( 'date_format' ) . ' H:i', $at ) ) . '

'; } if ( is_array( $data ) && isset( $data['sites'] ) && is_array( $data['sites'] ) ) { echo ''; return; } echo '
' . esc_html( $raw ) . '
'; } /** * @param int $post_id 文章 ID。 * @param \WP_Post $post 文章对象。 */ public function save_classic_meta( $post_id, $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable if ( ! Lesending_Plugin::is_remote_enabled() ) { return; } if ( ! isset( $_POST['lesending_classic_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['lesending_classic_nonce'] ) ), 'lesending_classic_save' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // 无发布权限者不可选择远程目标(避免触发使用管理员存储凭据的同步)。 if ( ! current_user_can( 'publish_posts' ) ) { update_post_meta( $post_id, '_lesending_target_mode', 'local_only' ); update_post_meta( $post_id, '_lesending_remote_site_ids', array() ); return; } $mode = isset( $_POST['lesending_target_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['lesending_target_mode'] ) ) : 'local_only'; $allowed = array( 'local_only', 'local_and_remote', 'remote_only' ); if ( ! in_array( $mode, $allowed, true ) ) { $mode = 'local_only'; } $ids = array(); if ( ! empty( $_POST['lesending_remote_site_ids'] ) && is_array( $_POST['lesending_remote_site_ids'] ) ) { $ids = array_map( 'sanitize_text_field', wp_unslash( $_POST['lesending_remote_site_ids'] ) ); } update_post_meta( $post_id, '_lesending_target_mode', $mode ); update_post_meta( $post_id, '_lesending_remote_site_ids', array_values( $ids ) ); } public function enqueue() { if ( ! Lesending_Plugin::is_remote_enabled() || ! current_user_can( 'publish_posts' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'post' !== $screen->base ) { return; } global $post; if ( ! $post || 'post' !== $post->post_type ) { return; } if ( ! current_user_can( 'edit_post', $post->ID ) ) { return; } $handle = 'lesending-editor'; wp_enqueue_script( $handle, LESENDING_PLUGIN_URL . 'assets/js/editor-panel.js', array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-i18n', ), LESENDING_VERSION, true ); wp_localize_script( $handle, 'lesendingEditor', array( 'sites' => Lesending_Plugin::get_public_site_list(), ) ); wp_set_script_translations( $handle, 'lesending', LESENDING_PLUGIN_DIR . 'languages' ); } }