init(); $this->admin_init(); } function init() { add_action('wp_ajax_aiea_prompt_ajax', [ $this, 'aiea_prompt' ] ); add_action('wp_ajax_aiea_get_city', [ $this, 'get_city' ] ); add_action('wp_ajax_aiea_weather_reset', [ $this, 'weather_reset' ] ); add_action('wp_ajax_aiea_widgets_save', [ $this, 'save_widgets' ] ); add_action('wp_ajax_aiea_settings_save', [ $this, 'save_settings' ] ); add_action( 'admin_menu', [ $this, 'admin_menu' ] ); add_action( 'admin_menu', [ $this, 'menu_label_change' ], 99 ); add_action( 'admin_enqueue_scripts', [ $this, 'framework_scripts' ] ); //$aiea_shortcodes = get_option('aiea_shortcodes'); //if( empty( $aiea_shortcodes ) ) $this->default_shortcodes(); } public function admin_init() { add_action( 'admin_enqueue_scripts', [ $this, 'admin_style' ] ); } public function admin_style() { wp_enqueue_style( 'aiea_wp_admin_css', AIEA_URL . 'core/admin/assets/css/admin-style.css', false, '1.0.0' ); } public static function sanitize_data( $value ) { if( is_array( $value ) ) { $value = array_map( [ $this, 'sanitize_data' ], $value ); }elseif( !empty( $value ) ) { return sanitize_text_field( $value ); } return $value; } public function save_widgets() { if( !isset( $_POST['aiea_save_widgets'] ) || ( isset( $_POST['aiea_save_widgets'] ) && !wp_verify_nonce( $_POST['aiea_save_widgets'], 'inc-save-widgets)(*^&*&^#' ) ) ) return wp_die( "f***" ); $aiea_shortcodes = $aiea_modules = $aiea_options = ''; // reset existing values update_option( 'aiea_shortcodes', '' ); update_option( 'aiea_modules', '' ); // save new values if( isset( $_POST['aiea_shortcodes'] ) ) { $aiea_shortcodes = array_map( [ $this, 'sanitize_data' ], (array)$_POST['aiea_shortcodes'] ); update_option( 'aiea_shortcodes', $aiea_shortcodes ); } if( isset( $_POST['aiea_modules'] ) ) { $aiea_modules = array_map( [ $this, 'sanitize_data' ], (array)$_POST['aiea_modules'] ); update_option( 'aiea_modules', $aiea_modules ); } wp_send_json( array( 'status' => 'success' ) ); } public function save_settings() { if( !isset( $_POST['aiea_save_settings'] ) || ( isset( $_POST['aiea_save_settings'] ) && !wp_verify_nonce( $_POST['aiea_save_settings'], 'inc-save-settings)(*^&*&^#' ) ) ) return wp_die( "f***" ); $aiea_options = ''; update_option( 'aiea_options', '' ); $aiea_options = array_map( [ $this, 'sanitize_data' ], (array)$_POST['aiea_options'] ); update_option( 'aiea_options', $aiea_options ); wp_send_json( array( 'status' => 'success' ) ); } function admin_menu() { add_menu_page( esc_html__( 'AI Addons', 'ai-addons' ), esc_html__( 'AI Addons', 'ai-addons' ), 'manage_options', 'ai-addons', [ $this, 'aiea_dashboard' ], //'aiea_elementor_addon_admin_page', AIEA_URL . '/assets/images/logo-icon.svg', 57.99 ); } public function menu_label_change() { global $submenu; if(isset($submenu['ai-addons'])){ $submenu['ai-addons'][0][0] = esc_html__( 'Dashboard', 'ai-addons' ); } } public function aiea_dashboard() { require_once ( AIEA_DIR . 'core/admin/parts/dashboard.php' ); } function framework_scripts() { if( isset( $_GET['page'] ) && ( $_GET['page'] == 'ai-addons' ) ){ wp_enqueue_style( 'ai-admin', AIEA_URL . '/core/admin/assets/css/ai-admin-page.css', array(), '1.0', 'all' ); wp_enqueue_style( 'bootstrap-icons', AIEA_URL . 'assets/css/front-end/bootstrap-icons.css', false, '1.0' ); wp_enqueue_style( 'aiea-dashboard-font', AIEA_URL . '/core/admin/assets/css/dashboard-font.css', array(), '1.0', 'all' ); wp_enqueue_style( 'aiea-dashboard', AIEA_URL . '/core/admin/assets/css/dashboard.css', array(), '1.0', 'all' ); wp_enqueue_script( 'aiea-dashboard', AIEA_URL . 'core/admin/assets/js/dahsboard.js', array( 'jquery' ), '1.0', true ); } //wp_enqueue_script( 'ai-framework-admin', AIEA_URL . 'core/admin/assets/js/ai-admin-script.js', array( 'jquery' ), '1.0', true ); } function default_shortcodes(){ $aiea_shortcodes = empty( $aiea_shortcodes ) ? get_option('aiea_shortcodes') : $aiea_shortcodes; if( empty( $aiea_shortcodes ) ){ $shortcode_stat = aiea_addon_base()->aiea_shortcodes(); $aiea_shortcodes = array(); foreach( $shortcode_stat as $key => $value ){ $shortcode_name = str_replace( "-", "_", $key ); $aiea_shortcodes[$shortcode_name] = 'on'; } update_option( 'aiea_shortcodes', $aiea_shortcodes ); } } public function aiea_prompt() { if( isset( $_POST['text'] ) && !empty( $_POST['text'] ) ) { $question = sanitize_textarea_field( $_POST['text'] ); $result = []; $api_key = aiea_addon_base()->aiea_options('openai-api'); $aiea_model = aiea_addon_base()->aiea_options('openai-model'); $custom_openai_domain = get_option( 'aiea_custom_openai_domain', 'api.openai.com'); $aiea_args = array( "max_tokens" => 2000, "temperature" => 0.7, "top_p" => 1, "frequency_penalty" => 0, "presence_penalty" => 0 ); $api_url = 'https://'. $custom_openai_domain .'/v1/completions'; $aiea_args['model'] = $aiea_model; $aiea_args['messages'] = array( array( "role" => "user", "content" => addslashes( $question ) ) ); $api_url = 'https://'. $custom_openai_domain .'/v1/chat/completions'; $openaiea_json_args = json_encode( $aiea_args ); $args = array( 'method' => 'POST', 'timeout' => 600, 'sslverify' => false, 'headers' => array( 'Authorization' => 'Bearer '. esc_attr( $api_key ), 'Content-Type' => 'application/json' ), 'body' => $openaiea_json_args, ); $response = wp_remote_post( $api_url, $args ); $server_output = json_decode( wp_remote_retrieve_body( $response ), true ); if( isset( $server_output['error'] ) ) { $result['error'] = 'OpenAI error: '. $server_output['error']['message']; } else { if( isset( $server_output['choices'] ) ) { $choices = $server_output['choices']; if( !empty( $choices ) && isset( $choices[0] ) ) { $choices_arr = $choices[0]; if( !empty( $choices_arr ) && isset( $choices_arr['message'] ) ) { $content_arr = $choices_arr['message']; if( !empty( $content_arr ) && is_array( $content_arr ) && isset( $content_arr['content'] ) ) { $result['response'] = $content_arr['content']; } } } } else { $result['error'] = '!Try after sometime..'; } } wp_send_json( $result ); } wp_send_json( array( 'error' => '!text should be filled out' ) ); } public function get_city() { $result = [ 'status' => 'failed', 'city' => '' ]; if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { $ip_address = $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip_address = $_SERVER['REMOTE_ADDR']; } if( $ip_address == '::1' ) { $ip_address = $this->get_my_ip(); } $response = wp_remote_get( 'http://www.geoplugin.net/json.gp?ip='. $ip_address, array( 'timeout' => 120, 'httpversion' => '1.1', 'headers' => array( 'Accept' => 'application/json', ) ) ); if ( ( !is_wp_error($response)) && (200 === wp_remote_retrieve_response_code( $response ) ) ) { $responseBody = json_decode($response['body']); $result = [ 'status' => 'success', 'city' => $responseBody ]; } wp_send_json( $result ); } public function get_my_ip() { $response = wp_remote_get( 'http://ipecho.net/plain', array( 'timeout' => 120, 'httpversion' => '1.1' ) ); if ( ( !is_wp_error($response)) && (200 === wp_remote_retrieve_response_code( $response ) ) ) { return $response['body']; } return false; } public function weather_reset() { $api_version = isset( $_POST['version'] ) ? $_POST['version'] : ''; $city = isset( $_POST['city'] ) ? $_POST['city'] : ''; $api_key = aiea_addon_base()->aiea_options('open-weather-api'); $api_version_key = $api_version ? str_replace( ".", "", $api_version ) : ''; $city_slug = sanitize_title( $city ); $transient_name = sprintf( 'aiea_weather_%s_%s_%s', $api_key, $api_version_key, $city_slug ); delete_transient( $transient_name ); wp_send_json( [ 'status' => 'success' ] ); } public static function get_version() { $plugin_data = get_plugin_data( AIEA_BASE ); return $plugin_data['Version']; } public static function instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new self(); } return self::$_instance; } } AIEA_Admin_Settings::instance(); function aiea_elementor_addon_admin_page(){ ?>