prefix . self::$prompt_table; $settings_table_name = $wpdb->prefix . self::$settings_table_name; $created_post_table = $wpdb->prefix . self::$prompt_created_post; $charset_collate = $wpdb->get_charset_collate(); $sqls = array( "CREATE TABLE $prompt_table ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id BIGINT(20) UNSIGNED NOT NULL, prompt TEXT NOT NULL, post_type VARCHAR(255) NOT NULL, taxonomy_id BIGINT(20) UNSIGNED DEFAULT NULL, post_language VARCHAR(255) NOT NULL, status VARCHAR(10) NOT NULL, active TINYINT(1) DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) $charset_collate;", "CREATE TABLE $settings_table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, api_key VARCHAR(255) NOT NULL, api_name VARCHAR(255) NOT NULL, user_id VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) $charset_collate;", "CREATE TABLE $created_post_table ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, prompt_id VARCHAR(255) NOT NULL, post_id VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) $charset_collate;", ); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( $sqls as $sql ) { dbDelta( $sql ); } } /** * Insert a prompt content row. * * @param string $prompt Prompt text. * @param string $post_type Post type. * @param int $taxonomy_id Taxonomy ID. * @param string $post_status Post status. * @param string $post_language Post language. */ public static function insert_content( $prompt, $post_type = null, $taxonomy_id = null, $post_status = null, $post_language = null ) { global $wpdb; $prompt_table = $wpdb->prefix . self::$prompt_table; $wpdb->insert( $prompt_table, array( 'user_id' => get_current_user_id(), 'prompt' => sanitize_textarea_field( $prompt ), 'post_type' => sanitize_text_field( $post_type ), 'taxonomy_id' => absint( $taxonomy_id ), 'post_language' => sanitize_text_field( $post_language ), 'status' => sanitize_text_field( $post_status ), 'active' => 0, ) ); } /** * Retrieve all saved API settings. * * @return array */ public static function get_settings() { global $wpdb; $settings_table_name = $wpdb->prefix . self::$settings_table_name; return $wpdb->get_results( "SELECT * FROM $settings_table_name ORDER BY id DESC" ); } /** * Get API key by model name. * * @param string $api_model API model name. * @return array */ public static function get_api_key( $api_model ) { global $wpdb; $settings_table_name = $wpdb->prefix . self::$settings_table_name; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $settings_table_name WHERE api_name = %s ORDER BY id DESC", $api_model ) ); } /** * Update or remove existing API key for a model. * * @param string $api_key API key. * @param string $api_name API name. * @return array Status message. */ public static function update_settings( $api_key, $api_name ) { global $wpdb; $settings_table_name = $wpdb->prefix . self::$settings_table_name; $existing_settings = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM $settings_table_name WHERE api_name = %s ORDER BY id DESC LIMIT 1", $api_name ) ); if ( ! empty( $existing_settings ) ) { $wpdb->delete( $settings_table_name, array( 'id' => $existing_settings->id ) ); return array( 'status' => 'success', 'message' => 'API Key Removed Successfully', ); } $wpdb->insert( $settings_table_name, array( 'user_id' => get_current_user_id(), 'api_key' => sanitize_text_field( $api_key ), 'api_name' => sanitize_text_field( $api_name ), 'created_at' => current_time( 'mysql' ), ) ); return array( 'status' => 'success', 'message' => 'API Key Saved Successfully', ); } /** * Get all prompt records. * * @return array */ public static function get_aimsaisc_contents() { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_table; return $wpdb->get_results( "SELECT * FROM $table_name" ); } /** * Get all prompt records where active = 0. * * @return array */ public static function get_active_promp_contents() { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_table; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE active = %d", 0 ) ); } /** * Insert post creation log. * * @param int $id Prompt ID. * @param int $post_id Post ID. */ public static function insert_promp_details( $id, $post_id ) { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_created_post; $wpdb->insert( $table_name, array( 'prompt_id' => absint( $id ), 'post_id' => absint( $post_id ), 'created_at' => current_time( 'mysql' ), ) ); } /** * Get prompt detail by ID. * * @param int $id Prompt ID. * @return array */ public static function get_prompt_detail_from_id( $id ) { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_table; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id ) ); } /** * Get all created post records. * * @return array */ public static function get_create_post_from_prompt() { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_created_post; return $wpdb->get_results( "SELECT * FROM $table_name" ); } /** * Get created post by prompt ID. * * @param int $id Prompt ID. * @return array */ public static function get_created_post( $id ) { global $wpdb; $table_name = $wpdb->prefix . self::$prompt_created_post; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE prompt_id = %d", $id ) ); } }