admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aanya_admin' ),
'chatbotId' => $creds['chatbot_id'],
'embedToken' => $creds['embed_token'],
'share' => array(
'products' => get_option( 'aanya_share_products', '0' ),
'orders' => get_option( 'aanya_share_orders', '0' ),
'customers' => get_option( 'aanya_share_customers', '0' ),
'wooActive' => class_exists( 'WooCommerce' ),
'optionsUrl' => admin_url( 'options.php' ),
),
'i18n' => array(
'loading' => __( 'Loading AANYA studio…', 'aanya-ai-chatbot' ),
'saved' => __( 'Saved.', 'aanya-ai-chatbot' ),
'error' => __( 'Something went wrong. Please try again.', 'aanya-ai-chatbot' ),
'confirmDel' => __( 'Remove this training item?', 'aanya-ai-chatbot' ),
'empty' => __( 'Nothing here yet.', 'aanya-ai-chatbot' ),
'view' => __( 'View', 'aanya-ai-chatbot' ),
'remove' => __( 'Remove', 'aanya-ai-chatbot' ),
'close' => __( 'Close', 'aanya-ai-chatbot' ),
),
)
);
}
/**
* Render native studio shell (data filled by JS).
*/
public function render() {
?>
__( 'Forbidden.', 'aanya-ai-chatbot' ) ), 403 );
}
check_ajax_referer( 'aanya_admin', 'nonce' );
// Prefer credentials localized on the settings page (avoids stale/empty option cache on admin-ajax).
$chatbot_id = $this->post_text( 'chatbot_id' );
$embed_token = $this->post_text( 'embed_token' );
if ( '' !== $chatbot_id && '' !== $embed_token ) {
Aanya_Api_Client::set_request_credentials( $chatbot_id, $embed_token );
}
}
/**
* Read sanitized POST text after nonce verification in assert_ajax().
*
* @param string $key Field key.
* @param string $default Default.
* @return string
*/
private function post_text( $key, $default = '' ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- callers run assert_ajax() / check_ajax_referer first.
if ( ! isset( $_POST[ $key ] ) ) {
return $default;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- see above.
return sanitize_text_field( wp_unslash( (string) $_POST[ $key ] ) );
}
/**
* Bootstrap studio payload.
*/
public function ajax_bootstrap() {
$this->assert_ajax();
$creds = Aanya_Connection::get_credentials();
if ( '' === $creds['chatbot_id'] || '' === $creds['embed_token'] ) {
$post_id = $this->post_text( 'chatbot_id' );
$post_token = $this->post_text( 'embed_token' );
if ( '' !== $post_id && '' !== $post_token ) {
Aanya_Connection::save( $post_id, $post_token );
Aanya_Api_Client::set_request_credentials( $post_id, $post_token );
}
}
$result = Aanya_Api_Client::request( 'GET', '' );
if ( is_wp_error( $result ) ) {
wp_send_json_error(
array(
'message' => $result->get_error_message(),
'code' => $result->get_error_code(),
)
);
}
wp_send_json_success( $result );
}
/**
* Save widget settings (supports logo upload).
*/
public function ajax_save_settings() {
$this->assert_ajax();
$fields = array();
$map = array( 'name', 'welcome_message', 'primary_color', 'theme', 'widget_template_id' );
foreach ( $map as $key ) {
if ( isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$fields[ $key ] = sanitize_text_field( wp_unslash( $_POST[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
}
if ( ! empty( $_POST['languages'] ) && is_array( $_POST['languages'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$fields['languages'] = array_map( 'sanitize_text_field', wp_unslash( $_POST['languages'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
if ( ! empty( $_POST['remove_logo'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$fields['remove_logo'] = true;
}
$has_logo = ! empty( $_FILES['logo']['tmp_name'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( $has_logo ) {
$file = $this->sanitize_uploaded_file(
'logo',
array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' ),
'logo.png'
);
if ( is_wp_error( $file ) ) {
wp_send_json_error( array( 'message' => $file->get_error_message() ) );
}
$result = Aanya_Api_Client::upload(
'settings',
$fields,
'logo',
$file['tmp_name'],
$file['name'],
$file['type']
);
} else {
$result = Aanya_Api_Client::request( 'POST', 'settings', $fields );
}
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Save WooCommerce feature toggles.
*/
public function ajax_save_woocommerce() {
$this->assert_ajax();
$features = array();
$keys = array( 'product_qa', 'product_pricing', 'inventory_aware', 'order_lookup', 'assisted_checkout' );
foreach ( $keys as $key ) {
$features[ $key ] = ! empty( $_POST['shopify_features'][ $key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
$result = Aanya_Api_Client::request(
'PUT',
'woocommerce',
array( 'shopify_features' => $features )
);
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Save lead notification email.
*/
public function ajax_save_lead_settings() {
$this->assert_ajax();
$email = isset( $_POST['lead_notification_email'] ) ? sanitize_email( wp_unslash( $_POST['lead_notification_email'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request(
'PUT',
'lead-settings',
array( 'lead_notification_email' => $email )
);
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Knowledge actions: document|url|qa|manual|languages|crawl|content|delete.
*/
public function ajax_knowledge() {
$this->assert_ajax();
$action = isset( $_POST['knowledge_action'] ) ? sanitize_key( wp_unslash( $_POST['knowledge_action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
switch ( $action ) {
case 'document':
if ( empty( $_FILES['document']['tmp_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
wp_send_json_error( array( 'message' => __( 'Choose a document to upload.', 'aanya-ai-chatbot' ) ) );
}
$file = $this->sanitize_uploaded_file(
'document',
array(
'text/plain',
'text/csv',
'text/markdown',
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/rtf',
),
'document.txt'
);
if ( is_wp_error( $file ) ) {
wp_send_json_error( array( 'message' => $file->get_error_message() ) );
}
$result = Aanya_Api_Client::upload(
'knowledge/documents',
array(),
'document',
$file['tmp_name'],
$file['name'],
$file['type']
);
break;
case 'url':
$result = Aanya_Api_Client::request(
'POST',
'knowledge/urls',
array(
'url' => isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'mode' => isset( $_POST['mode'] ) ? sanitize_key( wp_unslash( $_POST['mode'] ) ) : 'full', // phpcs:ignore WordPress.Security.NonceVerification.Missing
)
);
break;
case 'qa':
$result = Aanya_Api_Client::request(
'POST',
'knowledge/qa',
array(
'question' => isset( $_POST['question'] ) ? sanitize_textarea_field( wp_unslash( $_POST['question'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'answer' => isset( $_POST['answer'] ) ? sanitize_textarea_field( wp_unslash( $_POST['answer'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
)
);
break;
case 'manual':
$result = Aanya_Api_Client::request(
'POST',
'knowledge/manual',
array(
'title' => isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'content' => isset( $_POST['content'] ) ? sanitize_textarea_field( wp_unslash( $_POST['content'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
)
);
break;
case 'languages':
$langs = array();
if ( ! empty( $_POST['languages'] ) && is_array( $_POST['languages'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$langs = array_map( 'sanitize_text_field', wp_unslash( $_POST['languages'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
$result = Aanya_Api_Client::request( 'POST', 'knowledge/languages', array( 'languages' => $langs ) );
break;
case 'crawl':
$job_id = isset( $_POST['job_id'] ) ? absint( $_POST['job_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request( 'GET', 'knowledge/crawl/' . $job_id );
break;
case 'content':
$item_id = isset( $_POST['item_id'] ) ? absint( $_POST['item_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request( 'GET', 'knowledge/' . $item_id . '/content' );
break;
case 'delete':
$item_id = isset( $_POST['item_id'] ) ? absint( $_POST['item_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request( 'DELETE', 'knowledge/' . $item_id );
break;
default:
wp_send_json_error( array( 'message' => __( 'Unknown knowledge action.', 'aanya-ai-chatbot' ) ) );
}
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Conversation detail.
*/
public function ajax_conversation() {
$this->assert_ajax();
$id = isset( $_POST['conversation_id'] ) ? sanitize_text_field( wp_unslash( $_POST['conversation_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request( 'GET', 'conversations/' . rawurlencode( $id ) );
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Lead conversation.
*/
public function ajax_lead_conversation() {
$this->assert_ajax();
$id = isset( $_POST['lead_id'] ) ? absint( $_POST['lead_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$result = Aanya_Api_Client::request( 'GET', 'leads/' . $id . '/conversation' );
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => $result->get_error_message() ) );
}
wp_send_json_success( $result );
}
/**
* Sanitize and validate an uploaded file from $_FILES.
*
* @param string $field File input name.
* @param array $allowed_mimes Allowed MIME types.
* @param string $default_name Fallback filename.
* @return array{tmp_name:string,name:string,type:string}|WP_Error
*/
private function sanitize_uploaded_file( $field, $allowed_mimes, $default_name ) {
if ( empty( $_FILES[ $field ] ) || ! is_array( $_FILES[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return new WP_Error( 'aanya_upload_missing', __( 'No file was uploaded.', 'aanya-ai-chatbot' ) );
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized below field-by-field.
$raw = $_FILES[ $field ];
$error = isset( $raw['error'] ) ? (int) $raw['error'] : UPLOAD_ERR_NO_FILE;
if ( UPLOAD_ERR_OK !== $error ) {
return new WP_Error( 'aanya_upload_error', __( 'File upload failed. Please try again.', 'aanya-ai-chatbot' ) );
}
$tmp_name = isset( $raw['tmp_name'] ) ? (string) $raw['tmp_name'] : '';
if ( '' === $tmp_name || ! is_uploaded_file( $tmp_name ) ) {
return new WP_Error( 'aanya_upload_invalid', __( 'Invalid upload.', 'aanya-ai-chatbot' ) );
}
$name = isset( $raw['name'] ) ? sanitize_file_name( wp_unslash( (string) $raw['name'] ) ) : '';
if ( '' === $name ) {
$name = $default_name;
}
$check = wp_check_filetype_and_ext( $tmp_name, $name );
$type = ! empty( $check['type'] ) ? (string) $check['type'] : '';
if ( '' === $type && isset( $raw['type'] ) ) {
$type = sanitize_mime_type( wp_unslash( (string) $raw['type'] ) );
}
if ( '' === $type ) {
$type = 'application/octet-stream';
}
if ( ! empty( $allowed_mimes ) && ! in_array( $type, $allowed_mimes, true ) ) {
return new WP_Error( 'aanya_upload_type', __( 'This file type is not allowed.', 'aanya-ai-chatbot' ) );
}
if ( ! empty( $check['proper_filename'] ) ) {
$name = sanitize_file_name( (string) $check['proper_filename'] );
}
return array(
'tmp_name' => $tmp_name,
'name' => $name,
'type' => $type,
);
}
}