wp_create_nonce('aiktp_connect'), 'isConnected' => $is_connected )); } /** * Render AIKTP Settings Page */ public static function render_settings_page() { // Get current API key and connection status $aiktp_api_key = get_option('aiktp_api_key', ''); $is_connected = !empty($aiktp_api_key); // Post Sync Settings $aiktp_author = get_option('aiktp_author', 0); $aiktp_category = get_option('aiktp_category', ''); // Get users $list_users = get_users(); $user_options = ''; foreach ($list_users as $user) { $selected = ($aiktp_author == $user->ID) ? 'selected' : ''; $user_options .= ''; } // Get categories $args = array('hide_empty' => false); $list_cat = get_categories($args); $arr_categories = !empty($aiktp_category) ? explode(',', $aiktp_category) : array(); $cat_options_li = ''; foreach ($list_cat as $cat) { $checked = in_array($cat->cat_ID, $arr_categories) ? 'checked' : ''; $cat_options_li .= '
  • '; } // Get WordPress site URL $wp_url = home_url(); ?>

    Settings

    Connect to aiktp.com, configure post synchronization, and enable WooCommerce AI SEO features powered by aiktp.com

    Connect Your WordPress Site to aiktp.com

    Visit aiktp.com/user-api to get your API key. Paste it into the field above and click “Connect” to link this website with AIKTP.

    Site URL:

    Account Information

    Loading credit information...

    Post Sync Configuration

    Choose an author for posts synced from aiktp.com

      array(), 'label' => array(), 'input' => array('type' => array(), 'name' => array(), 'checked' => array(), 'value' => array()))); ?>

    Select default categories for synced posts

    Post Sync - How to use

    Publish AI-generated posts from aiktp.com directly to this website.

    Features:

    • AI-generated posts are automatically published to your website
    • Automatic image upload and optimization
    • SEO optimization for better search rankings
    • Publish individual posts or bulk posts quickly

    How to use: Go to aiktp.com to create and publish your AI-generated content.

    WooCommerce AI Content Generator

    Select the language for AI-generated content. Default uses your site's configured language.

    Customize your own prompt, for example by adding company information, promotions, or warranty policies.

    WooCommerce AI - How to use

    1. Connect your API key in the Connect tab
    2. Configure content settings in WooCommerce AI tab
    3. Go to WooCommerce product edit page
    4. Open Bulk Actions dropdown and find "AI Content Generator" meta box in sidebar
    5. Click "Generate Description" or "Generate Short Description" and wait for completion
    'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '0' )); register_setting('aiktp_all_settings', 'aiktp_category', array( 'type' => 'string', 'sanitize_callback' => array('AIKTPZ_Settings', 'sanitize_categories'), 'default' => '' )); // WooCommerce AI Content Settings // Note: aiktp_api_key is NOT registered here because it should only be saved via AJAX Connect button // to prevent it from being saved as empty when other settings forms are submitted register_setting('aiktp_all_settings', 'aiktpz_content_length', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'medium' )); register_setting('aiktp_all_settings', 'aiktpz_content_tone', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'friendly' )); register_setting('aiktp_all_settings', 'aiktpz_content_language', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '' )); register_setting('aiktp_all_settings', 'aiktpz_custom_prompt', array( 'type' => 'string', 'sanitize_callback' => 'wp_kses_post', 'default' => '' )); } /** * Generate random token (10 characters: letters and numbers) */ private static function generate_random_token() { $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $token = ''; $length = 10; for ($i = 0; $i < $length; $i++) { $token .= $characters[wp_rand(0, strlen($characters) - 1)]; } return $token; } /** * AJAX handler for Connect button */ public function ajax_connect() { // Verify nonce $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (empty($nonce) || !wp_verify_nonce($nonce, 'aiktp_connect')) { wp_send_json_error('Invalid nonce'); return; } // Check permissions if (!current_user_can('manage_options')) { wp_send_json_error('Unauthorized'); return; } // Get API key $api_key = isset($_POST['api_key']) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ): ''; if (empty($api_key)) { wp_send_json_error('API key is required'); return; } // Get or generate wpToken using backward-compatible method $wp_token = self::get_token(); if (empty($wp_token)) { $wp_token = self::generate_random_token(); self::save_token($wp_token); } // Get WordPress URL $wp_url = home_url(); // Call API $api_url = 'https://aiktp.com/api/ai.php'; $response = wp_remote_post($api_url, array( 'body' => json_encode(array( 'task' => 'addWPSiteToAIKTP', 'wpURL' => $wp_url, 'wpToken' => $wp_token )), 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key ), 'timeout' => 30 )); if (is_wp_error($response)) { wp_send_json_error($response->get_error_message()); return; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); // Check if successful: data.data.status = 'success' and has data.data.siteId if ($response['response']['code'] == 200 && isset($data['data']['status']) && $data['data']['status'] === 'success' && isset($data['data']['siteId'])) { // Save API key update_option('aiktp_api_key', $api_key); wp_send_json_success(array( 'message' => 'Successfully connected to AIKTP', 'api_key' => $api_key, 'siteId' => $data['data']['siteId'] )); } else { $error_message = 'Connection failed'; if (isset($data['data']['message'])) { $error_message = $data['data']['message']; } elseif (isset($data['message'])) { $error_message = $data['message']; } elseif (isset($data['error'])) { $error_message = is_array($data['error']) ? ($data['error']['message'] ?? 'Unknown error') : $data['error']; } wp_send_json_error($error_message); } } /** * Sanitize categories (convert array to comma-separated string) */ public static function sanitize_categories( $value ) { // Verify nonce if ( ! isset( $_POST['aiktp_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aiktp_nonce'] ) ), 'aiktp_action' ) ) { return ''; } // Check if categories exist if ( ! isset( $_POST['aiktp_categories'] ) ) { return ''; } // Sanitize array input $post_data = map_deep( wp_unslash( $_POST['aiktp_categories'] ), 'sanitize_text_field' ); if ( is_array( $post_data ) ) { $cats = array_map( 'absint', $post_data ); return implode( ',', $cats ); } return ''; } /** * Migrate old option names to new prefixes * Ensures backward compatibility for existing installations */ public static function migrate_old_options() { // Migrate token (one-time migration from old name) $new_token = get_option('aiktpz_token'); $old_token = get_option('chatgpt_aiktp_key'); if (empty($new_token) && !empty($old_token)) { update_option('aiktpz_token', $old_token); // Delete old option after migration delete_option('chatgpt_aiktp_key'); } // Migrate WooCommerce AI settings $old_to_new = array( 'wcai_content_length' => 'aiktpz_content_length', 'wcai_content_tone' => 'aiktpz_content_tone', 'wcai_content_language' => 'aiktpz_content_language', 'wcai_custom_prompt' => 'aiktpz_custom_prompt' ); foreach ($old_to_new as $old => $new) { $new_value = get_option($new); $old_value = get_option($old); if (empty($new_value) && !empty($old_value)) { update_option($new, $old_value); } } } /** * Get token */ public static function get_token() { $token = get_option('aiktpz_token'); // One-time migration from old option name if (empty($token)) { $old_token = get_option('chatgpt_aiktp_key'); if (!empty($old_token)) { update_option('aiktpz_token', $old_token); delete_option('chatgpt_aiktp_key'); $token = $old_token; } } return $token; } /** * Save token */ public static function save_token($token) { update_option('aiktpz_token', $token); } /** * Handle Settings Form Submission */ public function handle_settings_form() { // Register all settings for options.php to handle self::register_all_settings(); } } // Initialize AIKTPZ_Settings::get_instance();