admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ucgaip_generate_nonce') )); } add_action('wp_enqueue_scripts', 'ucgaip_generator_localize_scripts'); // Shortcode to render content generation tool function ucgaip_generator_shortcode() { ob_start(); include(plugin_dir_path(__FILE__) . 'content-generator-template.php'); return ob_get_clean(); } add_shortcode('ucgaip-generator', 'ucgaip_generator_shortcode'); function ucgaip_create_database_table() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $table_name = $wpdb->prefix . 'ai_content_data'; // SQL to create the table $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_input text NOT NULL, ucgaip_response text NOT NULL, timestamp datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; // Load the upgrade class if it's not already loaded if (!class_exists('WP_Upgrader')) { require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } // Ensure that dbDelta() is available if (!function_exists('dbDelta')) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; } dbDelta($sql); } register_activation_hook(__FILE__, 'ucgaip_content_activate'); function ucgaip_content_activate() { // Check if the other plugin is active if (is_plugin_active('ucgaip-content-generator-pro/ucgaip-content-generator.php')) { // Deactivate the other plugin deactivate_plugins('ucgaip-content-generator-pro/ucgaip-content-generator.php'); } } function ucgaip_generate_text() { // Nonce Verification check_ajax_referer('ucgaip_generate_nonce', 'nonce'); if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ucgaip_generate_nonce')) { wp_send_json_error("Nonce verification failed."); wp_die(); } // Check if the current user has the capability to generate text // if ( ! current_user_can( 'generate_text_capability' ) ) { // wp_send_json_error( 'You do not have permission to generate text.' ); // wp_die(); // } if (!current_user_can('manage_options')) { throw new Exception("You don't have permission to perform this action."); } $prompt = sanitize_text_field($_POST['prompt']); $api_key = sanitize_text_field(get_option('ucgaip_api_key')); $api_url = 'https://api.openai.com/v1/chat/completions'; $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key ); $body = array( 'model' => 'gpt-3.5-turbo', 'messages' => array(array('role' => 'user', 'content' => $prompt)), 'temperature' => 0.7 ); $args = array( 'method' => 'POST', 'headers' => $headers, 'body' => wp_json_encode($body), 'timeout' => 120 ); $response = wp_remote_request($api_url, $args); if (is_wp_error($response)) { $error_message = $response->get_error_message(); wp_send_json_error("Something went wrong: $error_message"); } else { $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (json_last_error() !== JSON_ERROR_NONE) { wp_send_json_error('Invalid JSON in API response'); } elseif (!isset($data['choices'])) { wp_send_json_error('API request failed. Response: ' . $body); } else { $ucgaip_response = $data['choices'][0]['message']['content']; // Only store the user input and AI response if ucgaip_store_inputs is enabled $ucgaip_store_inputs = sanitize_text_field(get_option('ucgaip_store_inputs')); if ($ucgaip_store_inputs == "1") { $base_prompt = sanitize_textarea_field(get_option('ucgaip_prompt')); $topic = str_replace($base_prompt, '', $prompt); // Extracting the topic $post_data = array( 'post_title' => trim($topic), 'post_content' => $ucgaip_response, 'post_status' => 'publish', 'post_type' => 'ucgaip_input', ); $post_id = wp_insert_post($post_data); if ($post_id === 0 || is_wp_error($post_id)) { wp_send_json_error('Failed to save user input and AI response.'); } else { update_post_meta($post_id, 'user_input', $prompt); update_post_meta($post_id, 'ucgaip_response', $ucgaip_response); update_post_meta($post_id, 'timestamp', current_time('mysql')); } } wp_send_json_success($data); } } wp_die(); } add_action('wp_ajax_ucgaip_generate_text', 'ucgaip_generate_text'); add_action('wp_ajax_nopriv_ucgaip_generate_text', 'ucgaip_generate_text'); function ucgaip_generate_content() { check_ajax_referer('ucgaip_generate_nonce', 'nonce'); $prompt_text = sanitize_text_field(wp_unslash($_POST['prompt'])); $response = "Your response from Ai API"; // Modify this to get the actual response // Save user input and response as a custom post $post_data = array( 'post_title' => sanitize_text_field($prompt_text), 'post_content' => wp_kses_post($response), 'post_status' => 'publish', 'post_type' => 'ucgaip_input', ); wp_insert_post($post_data); wp_send_json_success(array('response' => esc_html($response))); // echo $response; // wp_die(); } function ucgaip_register_ai_settings() { register_setting('ucgaip-settings-group', 'ucgaip_api_key'); register_setting('ucgaip-settings-group', 'ucgaip_prompt'); register_setting('ucgaip-settings-group', 'ucgaip_button_text'); register_setting('ucgaip-settings-group', 'ucgaip_button_color'); register_setting('ucgaip-settings-group', 'ucgaip_border_radius'); register_setting('ucgaip-settings-group', 'ucgaip_prompt_text'); register_setting('ucgaip-settings-group', 'ucgaip_placeholder_text'); register_setting('ucgaip-settings-group', 'ucgaip_store_inputs'); } add_action('admin_init', 'ucgaip_register_ai_settings'); function ucgaip_settings_display() { ?>