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( 'options-general.php?page=wpthemoon' ) ) ) . '

'; 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 '
'; echo ''; echo '' . esc_html__( 'Service connected successfully', 'wpthemoon' ) . ''; echo '
'; $points = isset( $credits_info['points'] ) ? intval( $credits_info['points'] ) : 0; echo '

' . esc_html__( 'Current account remaining credits:', 'wpthemoon' ) . ' ' . esc_html( $points ) . '

'; // 低余额警告 if ( $points < 100 ) { echo '
'; echo '

⚠️ ' . esc_html__( 'Your credits are running low. Consider purchasing more to continue using AI features.', 'wpthemoon' ) . '

'; echo '
'; } 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; } }