boot_available_addons(); } add_action( 'plugins_loaded', 'wpthemoon_init', 20 ); /** * 注册自定义字段(用于存储AI生成的元数据) */ function wpthemoon_register_post_meta() { // 完整的AI元数据(JSON格式) register_post_meta( 'post', '_wpthemoon_ai_metadata', array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); // 文章类型 register_post_meta( 'post', '_wpthemoon_article_type', array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); // 内容语言 register_post_meta( 'post', '_wpthemoon_language', array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); // 字数统计 register_post_meta( 'post', '_wpthemoon_word_count', array( 'type' => 'integer', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); // 关键词密度 register_post_meta( 'post', '_wpthemoon_keyword_density', array( 'type' => 'number', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); // 可读性评分 register_post_meta( 'post', '_wpthemoon_readability_score', array( 'type' => 'integer', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ) ); } add_action( 'init', 'wpthemoon_register_post_meta' ); /** * 注册 Gutenberg 侧边栏脚本和样式 */ function wpthemoon_enqueue_block_editor_assets() { $asset_file = include( WPTHEMOON_PATH . 'build/index.asset.php' ); wp_enqueue_script( 'wpthemoon-sidebar', WPTHEMOON_URL . 'build/index.js', $asset_file['dependencies'], $asset_file['version'] ); // 注入 REST API 基础 URL 和 Nonce 到前端全局变量中 wp_localize_script( 'wpthemoon-sidebar', 'wpthemoonApi', array( 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ) ) ); wp_enqueue_style( 'wpthemoon-sidebar-style', WPTHEMOON_URL . 'build/index.css', array( 'wp-edit-post' ), $asset_file['version'] ); // Load translations for the script wp_set_script_translations( 'wpthemoon-sidebar', 'wpthemoon' ); } add_action( 'enqueue_block_editor_assets', 'wpthemoon_enqueue_block_editor_assets' ); /** * 在后台仪表盘添加状态小工具 */ add_action( 'wp_dashboard_setup', 'wpthemoon_dashboard_widgets' ); function wpthemoon_dashboard_widgets() { wp_add_dashboard_widget( 'wpthemoon_status_widget', __( 'AI Generator Status', 'wpthemoon' ), 'wpthemoon_status_widget_content' ); } function wpthemoon_status_widget_content() { $api_key = get_option( 'wpthemoon_api_key' ); if ( empty( $api_key ) ) { echo '
' . sprintf( __( 'API Key is not configured. Please go to the Settings page to configure it.', 'wpthemoon' ), esc_url( admin_url( 'admin.php?page=wpthemoon-settings' ) ) ) . '
'; return; } // 尝试从缓存获取积分信息(缓存 5 分钟) $credits_info = get_transient( 'wpthemoon_credits_cache' ); if ( false === $credits_info ) { $client = new WPTheMoon_API_Client(); $credits_info = $client->get_credits(); // 只缓存成功的响应 if ( ! is_wp_error( $credits_info ) ) { set_transient( 'wpthemoon_credits_cache', $credits_info, 5 * MINUTE_IN_SECONDS ); } } if ( is_wp_error( $credits_info ) ) { echo '' . esc_html__( 'Connection Failed:', 'wpthemoon' ) . ' ' . esc_html( $credits_info->get_error_message() ) . '
'; echo '' . esc_html__( 'Recheck Settings', 'wpthemoon' ) . '
'; } else { echo '' . esc_html__( 'Current account remaining credits:', 'wpthemoon' ) . ' ' . esc_html( $points ) . '
'; // 低余额警告 if ( $points < 100 ) { echo '⚠️ ' . esc_html__( 'Your credits are running low. Consider purchasing more to continue using AI features.', 'wpthemoon' ) . '
'; echo '' . esc_html__( 'Get more credits on main site', 'wpthemoon' ) . '
'; // 刷新缓存按钮 echo ''; echo '🔄 ' . esc_html__( 'Refresh credits', 'wpthemoon' ) . ''; echo '
'; } } /** * 处理手动刷新积分缓存 */ add_action( 'admin_init', 'wpthemoon_handle_refresh_credits' ); function wpthemoon_handle_refresh_credits() { if ( isset( $_GET['wpthemoon_refresh_credits'] ) && $_GET['wpthemoon_refresh_credits'] === '1' ) { delete_transient( 'wpthemoon_credits_cache' ); wp_safe_redirect( admin_url( 'index.php' ) ); exit; } }