| Server IP : 38.190.208.107 / Your IP : 216.73.216.219 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : www ( 1000) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.3/wp-content/plugins/wpforms-lite/src/Integrations/AI/Admin/Ajax/ |
Upload File : |
<?php
namespace WPForms\Integrations\AI\Admin\Ajax;
use WPForms\Integrations\AI\API\API;
/**
* Base class.
*
* @since 1.9.1
*/
abstract class Base {
/**
* API instance.
*
* @since 1.9.1
*
* @var API
*/
protected $api;
/**
* Initialize.
*
* @since 1.9.1
*/
public function init() {
$this->api = new API();
$this->api->init();
$this->hooks();
}
/**
* Register hooks.
*
* @since 1.9.1
*/
private function hooks(): void {
add_action( 'wp_ajax_wpforms_rate_ai_response', [ $this, 'rate_response' ] );
}
/**
* Rate choices response.
*
* @since 1.9.1
*/
public function rate_response(): void {
if ( ! $this->validate_nonce() ) {
wp_send_json_error();
}
$helpful = $this->get_post_data( 'helpful', 'bool' );
$response_id = $this->get_post_data( 'response_id' );
$response = $this->api->rate( $helpful, $response_id );
wp_send_json_success( $response );
}
/**
* Validate nonce.
*
* @since 1.9.1
*
* @return bool|int
*/
protected function validate_nonce() {
return check_ajax_referer( 'wpforms-ai-nonce', 'nonce', false );
}
/**
* Get the post's data by key.
*
* @since 1.9.1
*
* @param string $key Key to get data for.
* @param string $type Type of data to get.
*
* @return mixed
*/
protected function get_post_data( string $key, string $type = 'text' ) {
switch ( $type ) {
case 'int':
$value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_NUMBER_INT ) ?? 0;
break;
case 'array':
$value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY ) ?? [];
break;
case 'bool':
$value = filter_input( INPUT_POST, $key, FILTER_VALIDATE_BOOLEAN ) ?? false;
break;
case 'json':
$value = json_decode( filter_input( INPUT_POST, $key ), true );
break;
default:
// We should use this alternative to FILTER_SANITIZE_FULL_SPECIAL_CHARS filter,
// because htmlspecialchars() function does double encoding of special characters,
// which is necessary to properly handle the encoded HTML in chat questions.
$value = htmlspecialchars( filter_input( INPUT_POST, $key ) ?? '' );
break;
}
return $value;
}
/**
* Determine whether a given prompt is empty.
*
* It must contain a minimum of one character.
*
* @since 1.9.1
*
* @param string $prompt The prompt to check.
*
* @return bool True if the prompt is empty.
*/
protected function is_empty_prompt( string $prompt ): bool {
$special_chars = [ '@', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '{', '}', '[', ']', '|', '\\', ':', ';', '"', "'", '<', '>', ',', '.', '?', '/' ];
$prompt = str_replace( $special_chars, '', $prompt );
return empty( $prompt );
}
}