| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Onboarding_Step_AI' ) ) : |
| 7 |
|
| 8 |
/** |
| 9 |
* Onboarding step 2: AI Assistant & Chatbot. |
| 10 |
* |
| 11 |
* Renders its own markup (not the existing Settings screen's form), but posts |
| 12 |
* to the existing 'wpsc_set_ai_settings' AJAX action so the API key is still |
| 13 |
* stored, validated and connection-tested by the existing, single code path - |
| 14 |
* no duplicate key storage or validation is introduced here. |
| 15 |
*/ |
| 16 |
final class WPSC_Onboarding_Step_AI { |
| 17 |
|
| 18 |
const STEP_ID = 'ai-assistant'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Render step body |
| 22 |
* |
| 23 |
* @param string $step_id - step id. |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public static function render( $step_id ) { |
| 27 |
|
| 28 |
$available = class_exists( 'WPSC_PS_AI_Setting_General' ); |
| 29 |
$ai_settings = $available ? get_option( 'wpsc-ps-ai-assistant-settings', array() ) : array(); |
| 30 |
$is_active = ! empty( $ai_settings['is-active'] ); |
| 31 |
$provider = isset( $ai_settings['provider'] ) ? $ai_settings['provider'] : 'openai'; |
| 32 |
$api_key = isset( $ai_settings['api_key'] ) ? $ai_settings['api_key'] : ''; |
| 33 |
$max_tokens = ! empty( $ai_settings['max-tokens'] ) ? intval( $ai_settings['max-tokens'] ) : 4096; |
| 34 |
?> |
| 35 |
<div class="wpsc-onboarding-step"> |
| 36 |
<h2><?php esc_attr_e( 'AI Assistant & Chatbot', 'supportcandy' ); ?></h2> |
| 37 |
<p class="wpsc-onboarding-step-desc"> |
| 38 |
<?php esc_attr_e( 'AI Assistant and Chatbot can help your support team summarize tickets, draft replies, answer customer questions, and provide AI-powered support using your configured knowledge. This is entirely optional and can be set up anytime later from Settings.', 'supportcandy' ); ?> |
| 39 |
</p> |
| 40 |
|
| 41 |
<?php if ( ! $available ) : ?> |
| 42 |
<div class="wpsc-onboarding-status pending"><?php esc_attr_e( 'AI Assistant is not available on this site.', 'supportcandy' ); ?></div> |
| 43 |
<?php elseif ( $is_active ) : ?> |
| 44 |
<div class="wpsc-onboarding-status success"> |
| 45 |
<?php WPSC_Icons::get( 'check' ); ?> |
| 46 |
<?php |
| 47 |
printf( |
| 48 |
/* translators: %s: AI provider name */ |
| 49 |
esc_html__( 'AI Assistant is connected using %s.', 'supportcandy' ), |
| 50 |
esc_html( 'google-gemini' === $provider ? __( 'Google Gemini', 'supportcandy' ) : __( 'OpenAI', 'supportcandy' ) ) |
| 51 |
); |
| 52 |
?> |
| 53 |
</div> |
| 54 |
<?php else : ?> |
| 55 |
<form action="#" onsubmit="return false;" class="wpsc-frm-onboarding-ai"> |
| 56 |
<div class="wpsc-input-group"> |
| 57 |
<div class="label-container"> |
| 58 |
<label><?php esc_attr_e( 'AI provider', 'supportcandy' ); ?></label> |
| 59 |
</div> |
| 60 |
<select name="wpsc-ai-service-provider"> |
| 61 |
<option value="openai" <?php selected( $provider, 'openai' ); ?>><?php esc_attr_e( 'OpenAI', 'supportcandy' ); ?></option> |
| 62 |
<option value="google-gemini" <?php selected( $provider, 'google-gemini' ); ?>><?php esc_attr_e( 'Google Gemini', 'supportcandy' ); ?></option> |
| 63 |
</select> |
| 64 |
</div> |
| 65 |
<div class="wpsc-input-group"> |
| 66 |
<div class="label-container"> |
| 67 |
<label><?php esc_attr_e( 'API key', 'supportcandy' ); ?></label> |
| 68 |
</div> |
| 69 |
<input type="text" name="wpsc-ai-api-key" value="<?php echo esc_attr( self::mask_api_key( $api_key ) ); ?>" placeholder="<?php esc_attr_e( 'Enter your API key', 'supportcandy' ); ?>" autocomplete="off"> |
| 70 |
</div> |
| 71 |
<input type="hidden" name="wpsc-ai-max-tokens" value="<?php echo esc_attr( $max_tokens ); ?>"> |
| 72 |
<input type="hidden" name="action" value="wpsc_set_ai_settings"> |
| 73 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_ai_settings' ) ); ?>"> |
| 74 |
</form> |
| 75 |
<div class="wpsc-onboarding-inline-message"></div> |
| 76 |
<div class="wpsc-onboarding-inline-actions"> |
| 77 |
<button type="button" class="wpsc-button normal primary" onclick="wpsc_onboarding_save_ai(this);"> |
| 78 |
<?php esc_attr_e( 'Connect', 'supportcandy' ); ?> |
| 79 |
</button> |
| 80 |
</div> |
| 81 |
<?php endif; ?> |
| 82 |
|
| 83 |
<?php WPSC_Onboarding::render_step_footer( $step_id ); ?> |
| 84 |
</div> |
| 85 |
<?php |
| 86 |
wp_die(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Mask an API key for display, e.g. "sk-1********2345" - mirrors the format |
| 91 |
* of the existing (private) masking helper so an unchanged resubmission is |
| 92 |
* still recognized as "unchanged" by the existing save handler. |
| 93 |
* |
| 94 |
* @param string $key - raw API key. |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
private static function mask_api_key( $key ) { |
| 98 |
|
| 99 |
$key = trim( (string) $key ); |
| 100 |
$length = strlen( $key ); |
| 101 |
|
| 102 |
if ( 0 === $length ) { |
| 103 |
return ''; |
| 104 |
} |
| 105 |
if ( $length <= 8 ) { |
| 106 |
return str_repeat( '*', $length ); |
| 107 |
} |
| 108 |
return substr( $key, 0, 4 ) . str_repeat( '*', 8 ) . substr( $key, -4 ); |
| 109 |
} |
| 110 |
} |
| 111 |
endif; |
| 112 |
|