| 1 |
<?php |
| 2 |
/** |
| 3 |
* grok AI |
| 4 |
* |
| 5 |
* @package Botmaster |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'qcld_wpgrok_addons' ) ) { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Main Class. |
| 18 |
*/ |
| 19 |
final class qcld_wpgrok_addons { |
| 20 |
|
| 21 |
private $id = 'Open AI'; |
| 22 |
|
| 23 |
/** |
| 24 |
* WPBot Pro version. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $version = '1.0.6'; |
| 29 |
|
| 30 |
/** |
| 31 |
* WPBot Pro helper. |
| 32 |
* |
| 33 |
* @var object |
| 34 |
*/ |
| 35 |
public $helper; |
| 36 |
|
| 37 |
/** |
| 38 |
* The single instance of the class. |
| 39 |
* |
| 40 |
* @var qcld_wb_Chatbot |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
protected static $_instance = null; |
| 44 |
|
| 45 |
/** |
| 46 |
* Main wpbot Instance. |
| 47 |
* |
| 48 |
* Ensures only one instance of wpbot is loaded or can be loaded. |
| 49 |
* |
| 50 |
* @return qcld_wb_Chatbot - Main instance. |
| 51 |
* @since 1.0.0 |
| 52 |
* @static |
| 53 |
*/ |
| 54 |
public static function instance() { |
| 55 |
|
| 56 |
if ( is_null( self::$_instance ) ) { |
| 57 |
self::$_instance = new self(); |
| 58 |
} |
| 59 |
|
| 60 |
return self::$_instance; |
| 61 |
} |
| 62 |
|
| 63 |
public $response_list; |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructor |
| 67 |
*/ |
| 68 |
public function __construct() { |
| 69 |
$this->includes(); |
| 70 |
add_action( 'wp_ajax_qcld_grok_response', array( $this, 'grok_response_callback' ) ); |
| 71 |
add_action( 'wp_ajax_nopriv_qcld_grok_response', array( $this, 'grok_response_callback' ) ); |
| 72 |
add_action('wp_ajax_qcld_stream_grokai', array($this, 'qcld_stream_grok_callback')); |
| 73 |
add_action( 'wp_ajax_qcld_grok_add_collection', array( $this, 'qcld_grok_add_collection_callback' ) ); |
| 74 |
add_action('wp_ajax_qcld_grok_collectionlist', array($this, 'qcld_grok_collectionlist_callback')); |
| 75 |
add_action('wp_ajax_nopriv_qcld_stream_grokai', array($this, 'qcld_stream_grok_callback')); |
| 76 |
add_action('wp_ajax_qcld_grok_remove_collection', array($this, 'qcld_grok_remove_collection_callback')); |
| 77 |
add_action( 'wp_ajax_qcld_grok_settings_option', array( $this, 'qcld_grok_settings_option_callback' ) ); |
| 78 |
add_action( 'wp_ajax_qcld_grok_file_upload', array( $this, 'qcld_grok_file_upload_callback' ) ); |
| 79 |
if ( is_admin() && ! empty( $_GET['page'] ) && ( ( $_GET['page'] == 'openai-panel_dashboard' ) || ( $_GET['page'] == 'openai-panel_file' ) || ( $_GET['page'] == 'wpbot_openAi' ) || ( $_GET['page'] == 'openai-panel_help' ) ) ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 80 |
add_action( 'admin_enqueue_scripts', array( $this, 'qcld_wb_chatbot_grok_admin_scripts' ) ); |
| 81 |
} |
| 82 |
// add_action('wp_enqueue_scripts', array($this, 'qcld_wb_chatbot_grok_scripts')); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
public function qcld_wb_chatbot_grok_admin_scripts() { |
| 87 |
wp_register_script( |
| 88 |
'qcld-wp-chatbot-grok-admin', |
| 89 |
QCLD_wpCHATBOT_PLUGIN_URL . 'includes/integration/grok/js/qcld-wp-grok-admin.js', |
| 90 |
array( 'jquery' ), |
| 91 |
QCLD_wpCHATBOT_VERSION, |
| 92 |
true |
| 93 |
); |
| 94 |
|
| 95 |
// Localize the script with necessary data. |
| 96 |
wp_localize_script( |
| 97 |
'qcld-wp-chatbot-grok-admin', |
| 98 |
'ajax_object', |
| 99 |
array( |
| 100 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 101 |
'ajax_nonce' => wp_create_nonce( 'wp_chatbot' ), |
| 102 |
'grok_api_key' => get_option( 'qcld_grok_api_key' ), |
| 103 |
'grok_model' => get_option( 'qcld_grok_model' ), |
| 104 |
'grok_enabled' => get_option( 'qcld_grok_enabled' ), |
| 105 |
'qcld_grok_rag_enabled' => get_option( 'qcld_grok_rag_enabled' ), |
| 106 |
'qcld_grok_append_content' => get_option( 'qcld_grok_append_content' ), |
| 107 |
'qcld_grok_prepend_content' => get_option( 'qcld_grok_prepend_content' ), |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
wp_enqueue_script( 'qcld-wp-chatbot-grok-admin' ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Define wpbot Constants. |
| 116 |
* |
| 117 |
* @return void |
| 118 |
* @since 1.0.0 |
| 119 |
*/ |
| 120 |
public function includes() { |
| 121 |
require_once QCLD_wpCHATBOT_PLUGIN_DIR_PATH . 'includes/Parsedown.php'; |
| 122 |
require_once QCLD_wpCHATBOT_PLUGIN_DIR_PATH . 'includes/class-common-function.php'; |
| 123 |
} |
| 124 |
|
| 125 |
public function qcld_grok_settings_option_callback() { |
| 126 |
$nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); |
| 127 |
if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) { |
| 128 |
wp_send_json( |
| 129 |
array( |
| 130 |
'success' => false, |
| 131 |
'msg' => esc_html__( 'Failed in Security check', 'chatbot'), |
| 132 |
) |
| 133 |
); |
| 134 |
wp_die(); |
| 135 |
} else { |
| 136 |
$grok_api_key = sanitize_text_field( wp_unslash($_POST['grok_api_key']) ?? '' ); |
| 137 |
$grok_model = sanitize_text_field( wp_unslash($_POST['grok_model']) ?? '' ); |
| 138 |
$grok_enabled = sanitize_text_field(wp_unslash($_POST['grok_enabled'])); |
| 139 |
$qcld_grok_page_suggestion_enabled = sanitize_text_field(wp_unslash($_POST['qcld_grok_page_suggestion_enabled'])); |
| 140 |
$qcld_grok_append_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_append_content'])) ?? ''; |
| 141 |
$qcld_grok_prepend_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_prepend_content'])) ?? ''; |
| 142 |
$grok_rag_enabled = sanitize_text_field(wp_unslash($_POST['grok_rag_enabled'])) ?? ''; |
| 143 |
$qcld_grok_system_content = sanitize_text_field(wp_unslash($_POST['qcld_grok_system_content'])) ?? ''; |
| 144 |
$grok_stream_enabled = sanitize_text_field(wp_unslash($_POST['grok_stream_enabled'])) ?? ''; |
| 145 |
$grok_management_api_key = sanitize_text_field( wp_unslash($_POST['grok_management_api_key']) ?? '' ); |
| 146 |
$grok_collection_id = sanitize_text_field( wp_unslash($_POST['grok_collection_id']) ?? '' ); |
| 147 |
|
| 148 |
if ( $grok_management_api_key != '' ) { |
| 149 |
update_option( 'qcld_grok_management_api_key', $grok_management_api_key ); |
| 150 |
} |
| 151 |
if ( $grok_rag_enabled != '' ) { |
| 152 |
update_option( 'qcld_grok_rag_enabled', $grok_rag_enabled ); |
| 153 |
} |
| 154 |
if ( $grok_stream_enabled != '' ) { |
| 155 |
update_option( 'qcld_grok_stream_enabled', $grok_stream_enabled ); |
| 156 |
} |
| 157 |
if ( $grok_api_key != '' ) { |
| 158 |
update_option( 'qcld_grok_api_key', $grok_api_key ); |
| 159 |
} |
| 160 |
if ( $grok_model != '' ) { |
| 161 |
update_option( 'qcld_grok_model', $grok_model ); |
| 162 |
} |
| 163 |
if ( $grok_enabled != '' ) { |
| 164 |
update_option( 'qcld_grok_enabled', $grok_enabled ); |
| 165 |
} |
| 166 |
if ( $grok_collection_id != '' ) { |
| 167 |
update_option( 'qcld_grok_collection_id', $grok_collection_id ); |
| 168 |
} |
| 169 |
if ( $grok_enabled == '1' ) { |
| 170 |
update_option( 'ai_enabled', 0 ); |
| 171 |
update_option( 'qcld_ollama_enabled', 0 ); |
| 172 |
|
| 173 |
update_option( 'qcld_openrouter_enabled', 0 ); |
| 174 |
} else { |
| 175 |
update_option( 'ai_enabled', 1 ); |
| 176 |
} |
| 177 |
if ( $qcld_grok_system_content != '' ) { |
| 178 |
update_option( 'qcld_grok_system_content', $qcld_grok_system_content ); |
| 179 |
} |
| 180 |
update_option( 'qcld_grok_page_suggestion_enabled', $qcld_grok_page_suggestion_enabled ); |
| 181 |
if ( isset( $_POST['openai_post_type'] ) ) { |
| 182 |
$openai_post_types = is_array( $_POST['openai_post_type'] ) |
| 183 |
? array_map( 'sanitize_text_field', wp_unslash( $_POST['openai_post_type'] ) ) |
| 184 |
: sanitize_text_field( wp_unslash( $_POST['openai_post_type'] ) ); |
| 185 |
update_option( 'qcld_openai_relevant_post', $openai_post_types ); |
| 186 |
} |
| 187 |
|
| 188 |
update_option( 'qcld_grok_append_content', $qcld_grok_append_content ); |
| 189 |
update_option( 'qcld_grok_prepend_content', $qcld_grok_prepend_content ); |
| 190 |
} |
| 191 |
wp_send_json( $grok_enabled ); |
| 192 |
} |
| 193 |
|
| 194 |
public function grok_response_callback() { |
| 195 |
|
| 196 |
|
| 197 |
if (get_option('is_rate_limiting_enabled') == '1') { |
| 198 |
do_action('rate_limit_checker'); |
| 199 |
} |
| 200 |
$grok_api_key = get_option( 'qcld_grok_api_key' ); |
| 201 |
$keyword = isset($_POST['keyword']) ? sanitize_text_field( wp_unslash($_POST['keyword']) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 202 |
$grok_system_content = get_option( 'qcld_grok_system_content' ); |
| 203 |
$rag_context = ""; |
| 204 |
if (get_option('qcld_grok_rag_enabled') == '1') { |
| 205 |
$rag_context = Qcld_Bot_Rag()->run_rag_search($keyword); |
| 206 |
} |
| 207 |
$relevant_pagelink = Qcld_WPBot_Common_Functions::qcpd_relevant_pagelink( $keyword ); |
| 208 |
$relevant_pagelink = array_slice( $relevant_pagelink, 0, 5, true ); |
| 209 |
if ( ( get_option( 'page_suggestion_enabled' ) == '1' ) && count( $relevant_pagelink ) > 0 ) { |
| 210 |
$relevant_post_link = maybe_unserialize( get_option( 'qlcd_wp_chatbot_relevant_post_link_openai' ) ); |
| 211 |
if ( is_array( $relevant_post_link[ get_wpbot_locale() ] ) ) { |
| 212 |
$relevant_pagelinks = '<br><br><p><em>' . implode( '', $relevant_post_link[ get_wpbot_locale() ] ) . '</em><p>' . implode( '</br>', $relevant_pagelink ); |
| 213 |
} else { |
| 214 |
$relevant_pagelinks = '<br><br><p><em>' . $relevant_post_link[ get_wpbot_locale() ] . '</em><p>' . implode( '</br>', $relevant_pagelink ); |
| 215 |
} |
| 216 |
} else { |
| 217 |
$relevant_pagelinks = ''; |
| 218 |
} |
| 219 |
$collection_id = get_option( 'qcld_grok_collection_id' ) ?? ''; |
| 220 |
if( $collection_id != '' ) { |
| 221 |
$result = $this->direct_search_collection( $keyword ); |
| 222 |
$response['status'] = 'success'; |
| 223 |
$response['message'] = $result ; |
| 224 |
wp_send_json( $response ); |
| 225 |
} else { |
| 226 |
$response_mess = wp_remote_post('https://api.x.ai/v1/chat/completions', [ |
| 227 |
'headers' => [ |
| 228 |
'Authorization' => 'Bearer ' . $grok_api_key, |
| 229 |
'Content-Type' => 'application/json', |
| 230 |
], |
| 231 |
'body' => wp_json_encode([ |
| 232 |
'messages' => [ |
| 233 |
['role' => 'system', 'content' => $grok_system_content ?? 'You are a helpful and intelligent assistant for the website "' . site_url() . '". Use live website data and the provided context to respond accurately and briefly. Stay relevant and do not introduce additional topics.' ], |
| 234 |
['role' => 'user', 'content' => $keyword ] |
| 235 |
], |
| 236 |
'model' => 'grok-3-latest', |
| 237 |
'temperature' => 0, |
| 238 |
'stream' => false |
| 239 |
]), |
| 240 |
'timeout' => 60, |
| 241 |
]); |
| 242 |
$response['message'] = ''; |
| 243 |
$status_code = wp_remote_retrieve_response_code($response_mess); |
| 244 |
// Step 2: Get the raw body (this is almost always what you want) |
| 245 |
$data = wp_remote_retrieve_body($response_mess); |
| 246 |
$data = json_decode($data, true); // Decode as associative array |
| 247 |
$response['status'] = 'success'; |
| 248 |
$response['message'] = $data['choices'][0]['message']['content'] ?? 'No content received'; // true = assoc array |
| 249 |
do_action('qcld_openai_user_rate_cal', 1); |
| 250 |
wp_send_json( $response ); |
| 251 |
} |
| 252 |
} |
| 253 |
public function qcld_stream_grok_callback() |
| 254 |
{ |
| 255 |
|
| 256 |
if (!defined('ABSPATH')) { |
| 257 |
// Optional: protect direct access or require wp-load.php |
| 258 |
// For security → better to use admin-ajax or custom rewrite rule in production |
| 259 |
http_response_code(403); |
| 260 |
exit; |
| 261 |
} |
| 262 |
if (get_option('is_rate_limiting_enabled') == '1') { |
| 263 |
do_action('rate_limit_checker'); |
| 264 |
} |
| 265 |
$api_key = get_option('qcld_grok_api_key'); // ← Use get_option() or env in real code! |
| 266 |
|
| 267 |
// Disable compression (critical for streaming) |
| 268 |
if (function_exists('apache_setenv')) { |
| 269 |
@apache_setenv('no-gzip', 1); |
| 270 |
} |
| 271 |
@ini_set('zlib.output_compression', 0); |
| 272 |
while (ob_get_level() > 0) { ob_end_clean(); } |
| 273 |
|
| 274 |
set_time_limit(180); // Allow long-running requests |
| 275 |
|
| 276 |
$headers = [ |
| 277 |
'Content-Type: application/json', |
| 278 |
'Authorization: Bearer ' . $api_key, |
| 279 |
]; |
| 280 |
$system_content = 'You are a helpful and intelligent assistant for the website "' . site_url() . '". Use live website data and the provided context to respond accurately and briefly. Stay relevant and do not introduce additional topics.'; |
| 281 |
$messages = []; |
| 282 |
// Load previous conversation from cookie if continuity is enabled |
| 283 |
if ($conversation_continuity == 1 && !empty($_COOKIE['last_five_prompt'])) { |
| 284 |
$decoded_cookie = json_decode(stripslashes($_COOKIE['last_five_prompt']), true); |
| 285 |
if (is_array($decoded_cookie)) { |
| 286 |
$messages = $decoded_cookie; |
| 287 |
} |
| 288 |
} |
| 289 |
// Ensure system message is the first message. |
| 290 |
if (empty($messages) || (isset($messages[0]) && $messages[0]['role'] !== 'system')) { |
| 291 |
array_unshift($messages, ['role' => 'system', 'content' => $system_content]); |
| 292 |
} elseif (isset($messages[0]) && $messages[0]['role'] === 'system' && $messages[0]['content'] !== $system_content) { |
| 293 |
$messages[0]['content'] = $system_content; |
| 294 |
} |
| 295 |
|
| 296 |
array_push($messages, ['role' => 'user', 'content' => sanitize_text_field( wp_unslash( $_POST['message'] ) )]); |
| 297 |
$postData = json_encode([ |
| 298 |
'model' => 'grok-3-latest', |
| 299 |
'messages' => $messages, |
| 300 |
'stream' => true |
| 301 |
]); |
| 302 |
|
| 303 |
// phpcs:ignore WordPress.WP.AlternativeFunctions -- streaming requires native cURL |
| 304 |
$ch = curl_init('https://api.x.ai/v1/chat/completions'); |
| 305 |
curl_setopt($ch, CURLOPT_POST, true); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 306 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 307 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 308 |
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$full_assistant_response) { // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 309 |
echo $chunk; |
| 310 |
echo str_repeat(' ', 1024); |
| 311 |
flush(); |
| 312 |
ob_flush(); |
| 313 |
|
| 314 |
// Parse the chunk to extract assistant's content for continuity. |
| 315 |
$lines = explode("\n", $chunk); |
| 316 |
foreach ($lines as $line) { |
| 317 |
if (strpos($line, 'data: ') === 0) { |
| 318 |
$json_data = substr($line, 6); |
| 319 |
if ($json_data === '[DONE]') { |
| 320 |
continue; |
| 321 |
} |
| 322 |
$data = json_decode($json_data, true); |
| 323 |
if (isset($data['choices'][0]['delta']['content'])) { |
| 324 |
$full_assistant_response .= $data['choices'][0]['delta']['content']; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
return strlen($chunk); |
| 329 |
}); |
| 330 |
curl_exec($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 331 |
curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 332 |
// phpcs:ignore WordPress.WP.AlternativeFunctions -- streaming requires native cURL |
| 333 |
$ch = curl_init('https://api.x.ai/v1/chat/completions'); |
| 334 |
|
| 335 |
// phpcs:ignore WordPress.WP.AlternativeFunctions |
| 336 |
curl_setopt_array($ch, [ |
| 337 |
CURLOPT_POST => true, |
| 338 |
CURLOPT_HTTPHEADER => $headers, |
| 339 |
CURLOPT_POSTFIELDS => $postData, |
| 340 |
CURLOPT_RETURNTRANSFER => false, // We echo ourselves |
| 341 |
CURLOPT_WRITEFUNCTION => function ($curl, $chunk) { |
| 342 |
echo $chunk; |
| 343 |
flush(); // Push to browser immediately |
| 344 |
return strlen($chunk); |
| 345 |
}, |
| 346 |
CURLOPT_TIMEOUT => 150, |
| 347 |
CURLOPT_CONNECTTIMEOUT => 30, |
| 348 |
CURLOPT_SSL_VERIFYPEER => true, |
| 349 |
CURLOPT_FOLLOWLOCATION => true, |
| 350 |
CURLOPT_ENCODING => '', // Let server decide |
| 351 |
]); |
| 352 |
|
| 353 |
$success = curl_exec($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 354 |
do_action('qcld_openai_user_rate_cal', 1); |
| 355 |
if (curl_errno($ch)) { // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 356 |
$err = curl_error($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 357 |
echo "data: " . wp_json_encode( array( 'error' => 'cURL error: ' . $err ) ) . "\n\n"; |
| 358 |
flush(); |
| 359 |
} |
| 360 |
|
| 361 |
curl_close($ch); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 362 |
|
| 363 |
// Final done signal (optional but nice) |
| 364 |
echo "data: [DONE]\n\n"; |
| 365 |
flush(); |
| 366 |
} |
| 367 |
public function qcld_grok_collectionlist_callback() { |
| 368 |
$nonce = sanitize_text_field( wp_unslash($_POST['nonce']) ?? '' ); |
| 369 |
if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) { |
| 370 |
wp_send_json( |
| 371 |
array( |
| 372 |
'success' => false, |
| 373 |
'msg' => esc_html__( 'Failed in Security check', 'chatbot'), |
| 374 |
) |
| 375 |
); |
| 376 |
wp_die(); |
| 377 |
} |
| 378 |
$grok_api_key = get_option( 'qcld_grok_api_key' ); |
| 379 |
$response_raw = wp_remote_get( |
| 380 |
'https://api.x.ai/v1/collections', |
| 381 |
array( |
| 382 |
'headers' => array( |
| 383 |
'Authorization' => 'Bearer ' . $grok_api_key, |
| 384 |
'Content-Type' => 'application/json', |
| 385 |
), |
| 386 |
'timeout' => 60, |
| 387 |
) |
| 388 |
); |
| 389 |
$response = json_decode( wp_remote_retrieve_body( $response_raw ), true ); |
| 390 |
wp_send_json( $response ); |
| 391 |
} |
| 392 |
public function qcld_grok_file_upload_callback() |
| 393 |
{ |
| 394 |
|
| 395 |
$nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); |
| 396 |
if (! wp_verify_nonce($nonce, 'wp_chatbot') || ! current_user_can( 'manage_options' ) ) { |
| 397 |
wp_send_json( |
| 398 |
array( |
| 399 |
'success' => false, |
| 400 |
'msg' => esc_html__('Failed in Security check', 'chatbot'), |
| 401 |
) |
| 402 |
); |
| 403 |
wp_die(); |
| 404 |
} else { |
| 405 |
// $uploadedfile = $_FILES['file']; |
| 406 |
// $collection_id = sanitize_text_field(wp_unslash($_POST['collection_id'])); |
| 407 |
// $api_key = get_option( 'qcld_grok_api_key' ); |
| 408 |
|
| 409 |
// $ch = curl_init(); |
| 410 |
|
| 411 |
// $file = new CURLFile( |
| 412 |
// $uploadedfile['tmp_name'], |
| 413 |
// $uploadedfile['type'] ?: 'application/octet-stream', |
| 414 |
// $uploadedfile['name'] |
| 415 |
// ); |
| 416 |
|
| 417 |
// $post_data = ['file' => $file]; |
| 418 |
|
| 419 |
// curl_setopt_array($ch, [ |
| 420 |
// CURLOPT_URL => 'https://api.x.ai/v1/files', |
| 421 |
// CURLOPT_POST => true, |
| 422 |
// CURLOPT_POSTFIELDS => $post_data, |
| 423 |
// CURLOPT_RETURNTRANSFER => true, |
| 424 |
// CURLOPT_HTTPHEADER => [ |
| 425 |
// 'Authorization: Bearer ' . $api_key, |
| 426 |
// // Do NOT add Content-Type here!! |
| 427 |
// ], |
| 428 |
// CURLOPT_TIMEOUT => 180, |
| 429 |
// CURLOPT_CONNECTTIMEOUT => 30, |
| 430 |
// ]); |
| 431 |
|
| 432 |
// $result = json_decode(curl_exec($ch)); |
| 433 |
// $error = curl_error($ch); |
| 434 |
// curl_close($ch); |
| 435 |
// $file_id = $result->id |
| 436 |
$collection_id = "collection_22aa88f1-80e8-4e1a-8e13-c119afb0481c"; |
| 437 |
$file_id = "file_06208083-0e27-44ff-af29-5faa211f4892"; |
| 438 |
$this->add_file_to_collection($collection_id, $file_id, []); |
| 439 |
if (! empty($res->error)) { |
| 440 |
$response['status'] = 'error'; |
| 441 |
$response['message'] = $res->error->message; |
| 442 |
} |
| 443 |
|
| 444 |
if (! empty($res->status)) { |
| 445 |
$response['status'] = 'success'; |
| 446 |
$response['message'] = 'Successfully Created file' . $res->id; |
| 447 |
} |
| 448 |
// echo wp_send_json(array($response)); |
| 449 |
// wp_die(); |
| 450 |
} |
| 451 |
} |
| 452 |
public function add_file_to_collection($collection_id, $file_id, $metadata = []) { |
| 453 |
$url = "https://management-api.x.ai/v1/collections/{$collection_id}/documents/{$file_id}"; |
| 454 |
$grok_management_api_key = get_option( 'qcld_grok_management_api_key' ); |
| 455 |
|
| 456 |
$args = [ |
| 457 |
'method' => 'POST', |
| 458 |
'timeout' => 30, |
| 459 |
'redirection' => 5, |
| 460 |
'httpversion' => '1.1', |
| 461 |
'blocking' => true, |
| 462 |
'headers' => [ |
| 463 |
'Authorization' => 'Bearer ' . $grok_management_api_key, // Management API key required |
| 464 |
'Content-Type' => 'application/json', |
| 465 |
], |
| 466 |
// 'body' => json_encode([ |
| 467 |
// 'fields' => $metadata // Optional: matches your field_definitions |
| 468 |
// ]), |
| 469 |
]; |
| 470 |
// return ['success' => false, 'error' => $response->get_error_message()]; |
| 471 |
$response = wp_remote_post( $url, $args ); |
| 472 |
$body = wp_remote_retrieve_body( $response ); |
| 473 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 474 |
if ($status_code >= 200 && $status_code < 300) { |
| 475 |
return ['success' => true, 'message' => 'File added to collection', 'body' => $body]; |
| 476 |
} else { |
| 477 |
return ['success' => false, 'status' => $status_code, 'error' => $body]; |
| 478 |
} |
| 479 |
} |
| 480 |
public function qcld_grok_remove_collection_callback() { |
| 481 |
$nonce = sanitize_text_field( wp_unslash($_POST['nonce']) ?? '' ); |
| 482 |
if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) { |
| 483 |
wp_send_json( |
| 484 |
array( |
| 485 |
'success' => false, |
| 486 |
'msg' => esc_html__( 'Failed in Security check', 'chatbot'), |
| 487 |
) |
| 488 |
); |
| 489 |
wp_die(); |
| 490 |
} |
| 491 |
$grok_api_key = get_option( 'qcld_grok_api_key' ); |
| 492 |
$collection_id = sanitize_text_field( wp_unslash($_POST['collection_id']) ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 493 |
$url = 'https://api.x.ai/v1/collections/' . $collection_id; // Check exact endpoint in docs.x.ai |
| 494 |
$args = [ |
| 495 |
'method' => 'DELETE', |
| 496 |
'headers' => [ |
| 497 |
'Authorization' => 'Bearer ' . $grok_api_key, |
| 498 |
'Content-Type' => 'application/json' |
| 499 |
], |
| 500 |
'timeout' => 60, |
| 501 |
]; |
| 502 |
$response = wp_remote_request( $url, $args ); |
| 503 |
$response_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 504 |
wp_send_json( $response_data ); |
| 505 |
} |
| 506 |
public function qcld_grok_add_collection_callback() { |
| 507 |
$nonce = sanitize_text_field( wp_unslash($_POST['nonce']) ?? '' ); |
| 508 |
if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) { |
| 509 |
wp_send_json( |
| 510 |
array( |
| 511 |
'success' => false, |
| 512 |
'msg' => esc_html__( 'Failed in Security check', 'chatbot'), |
| 513 |
) |
| 514 |
); |
| 515 |
wp_die(); |
| 516 |
} |
| 517 |
$grok_api_key = get_option( 'qcld_grok_api_key' ); |
| 518 |
$collection_name = isset( $_POST['collection_name'] ) ? sanitize_text_field( wp_unslash( $_POST['collection_name'] ) ) : ( site_url() . wp_rand( 1, 999 ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 519 |
$response_raw = wp_remote_post( |
| 520 |
'https://api.x.ai/v1/collections', |
| 521 |
array( |
| 522 |
'headers' => array( |
| 523 |
'Authorization' => 'Bearer ' . $grok_api_key, |
| 524 |
'Content-Type' => 'application/json', |
| 525 |
), |
| 526 |
'body' => wp_json_encode( array( 'collection_name' => $collection_name, 'field_definitions' => array() ) ), |
| 527 |
'timeout' => 60, |
| 528 |
) |
| 529 |
); |
| 530 |
$response = json_decode( wp_remote_retrieve_body( $response_raw ), true ); |
| 531 |
wp_send_json( $response ); |
| 532 |
} |
| 533 |
public function direct_search_collection( $query ) { |
| 534 |
|
| 535 |
$collection_id = get_option( 'qcld_grok_collection_id' ); |
| 536 |
$grok_api_key = get_option( 'qcld_grok_api_key' ); |
| 537 |
$url = 'https://api.x.ai/v1/documents/search'; |
| 538 |
$payload = [ |
| 539 |
'query' => $query, |
| 540 |
'source' => ['collection_ids' => [$collection_id]], |
| 541 |
'retrieval_mode' => ['type' => 'hybrid'] // or 'semantic', 'keyword' |
| 542 |
]; |
| 543 |
$args = [ |
| 544 |
'method' => 'POST', |
| 545 |
'headers' => [ |
| 546 |
'Authorization' => 'Bearer ' . $grok_api_key, |
| 547 |
'Content-Type' => 'application/json', |
| 548 |
], |
| 549 |
'body' => wp_json_encode($payload), |
| 550 |
'timeout' => 30, |
| 551 |
]; |
| 552 |
$response = wp_remote_post($url, $args); |
| 553 |
$result = json_decode(wp_remote_retrieve_body($response), true); |
| 554 |
if (is_wp_error($response)) { |
| 555 |
return ['error' => $response->get_error_message()]; |
| 556 |
} |
| 557 |
foreach ($result['matches'] as $key => $match) { |
| 558 |
if ($key >= 1) break; |
| 559 |
$responses = $match ["chunk_content"]; |
| 560 |
} |
| 561 |
return $responses; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* @return qcld_wpopenai_addon |
| 567 |
*/ |
| 568 |
if ( ! function_exists( 'qcld_wpgrok_addons' ) ) { |
| 569 |
|
| 570 |
function qcld_wpgrok_addons() { |
| 571 |
$qcld_wpgrok_addon = new qcld_wpgrok_addons(); |
| 572 |
return $qcld_wpgrok_addon->instance(); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// fire off the plugin. |
| 577 |
qcld_wpgrok_addons(); |
| 578 |
|
| 579 |
} |