flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } /* * Allow third-party plugins to modify the settings before saving. * * @param array $settings The settings array. * * @since 1.7.0 */ do_action( 'aicw_before_save_general_settings' ); $allowed_hosts = isset( $_POST['aicw_allowed_hosts'] ) ? sanitize_textarea_field( wp_unslash( $_POST['aicw_allowed_hosts'] ) ) : 'https://www.bing.com'; $default_host = isset( $_POST['aicw_default_host'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_default_host'] ) ) : 'https://www.bing.com/'; $api_model = isset( $_POST['aicw_api_model'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_api_model'] ) ) : ''; $redirect_is_enabled = isset( $_POST['aicw_enable_redirection'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_enable_redirection'] ) ) : ''; $enabled_img_gen = isset( $_POST['aicw_enable_img_generation'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_enable_img_generation'] ) ) : ''; $log_retention_period = isset( $_POST['aicw_log_retention_period'] ) ? absint( wp_unslash( $_POST['aicw_log_retention_period'] ) ) : ''; if ( 'https://www.bing.com' === $allowed_hosts ) { update_option( 'aicw_allowed_hosts', $allowed_hosts ); } update_option( 'aicw_default_host', $default_host ); update_option( 'aicw_api_model', $api_model ); update_option( 'aicw_enable_redirection', $redirect_is_enabled ); update_option( 'aicw_enable_img_generation', $enabled_img_gen ); update_option( 'aicw_log_retention_period', $log_retention_period ); ai_content_writer()->flash_notice( esc_html__( 'General settings saved successfully.', 'ai-content-writer' ), 'success' ); wp_safe_redirect( $referer ); exit(); } /** * Save API settings. * * @since 1.0.0 * @return void */ public static function save_api_settings() { check_admin_referer( 'aicw_save_api_settings' ); $referer = wp_get_referer(); if ( ! current_user_can( 'manage_options' ) ) { ai_content_writer()->flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } $gemini_api_key = isset( $_POST['aicw_gemini_api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_gemini_api_key'] ) ) : ''; $gemini_ai_model = isset( $_POST['aicw_gemini_ai_model'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_gemini_ai_model'] ) ) : ''; $chatgpt_api_secret_key = isset( $_POST['aicw_chatgpt_api_secret_key'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_chatgpt_api_secret_key'] ) ) : ''; $chatgpt_ai_model = isset( $_POST['aicw_chatgpt_ai_model'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_chatgpt_ai_model'] ) ) : ''; $pexels_api_key = isset( $_POST['aicw_pexels_api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_pexels_api_key'] ) ) : ''; // Update common options. update_option( 'aicw_gemini_ai_model', $gemini_ai_model ); update_option( 'aicw_chatgpt_ai_model', $chatgpt_ai_model ); update_option( 'aicw_pexels_api_key', $pexels_api_key ); // Check if the Gemini API key is valid or not. if ( ! empty( $gemini_api_key ) ) { // Create Gemini service instance. $service = new \AIContentWriter\Services\Gemini( $gemini_api_key ); // Call the service to get models. $models = $service->get_models(); if ( is_wp_error( $models ) ) { ai_content_writer()->flash_notice( esc_html( 'Gemini: ' . $models->get_error_message() ), 'error' ); update_option( 'aicw_gemini_api_key', '' ); update_option( 'aicw_gemini_models', array() ); } else { update_option( 'aicw_gemini_api_key', $gemini_api_key ); update_option( 'aicw_gemini_models', $models ); } } // Check if the ChatGPT API secret key is valid or not. if ( ! empty( $chatgpt_api_secret_key ) ) { // Create OpenAI service instance. $service = new \AIContentWriter\Services\OpenAI( $chatgpt_api_secret_key ); // Call the service to get models. $models = $service->get_models(); if ( is_wp_error( $models ) ) { ai_content_writer()->flash_notice( esc_html( 'OpenAI: ' . $models->get_error_message() ), 'error' ); update_option( 'aicw_chatgpt_api_secret_key', '' ); update_option( 'aicw_chatgpt_models', array() ); } else { update_option( 'aicw_chatgpt_api_secret_key', $chatgpt_api_secret_key ); update_option( 'aicw_chatgpt_models', $models ); } } ai_content_writer()->flash_notice( esc_html__( 'API settings saved successfully.', 'ai-content-writer' ), 'success' ); wp_safe_redirect( $referer ); exit(); } /** * Save Image Settings. * * @since 2.2.2 * @return void */ public static function save_image_settings() { check_admin_referer( 'aicw_save_image_settings' ); $referer = wp_get_referer(); if ( ! current_user_can( 'manage_options' ) ) { ai_content_writer()->flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } $enabled_img_gen = isset( $_POST['aicw_enable_img_generation'] ) ? sanitize_text_field( wp_unslash( $_POST['aicw_enable_img_generation'] ) ) : ''; update_option( 'aicw_enable_img_generation', $enabled_img_gen ); ai_content_writer()->flash_notice( esc_html__( 'Image generation settings saved successfully.', 'ai-content-writer' ), 'success' ); wp_safe_redirect( $referer ); exit(); } /** * Add campaign. * * @since 1.0.0 * @return void */ public static function handle_add_campaign() { check_admin_referer( 'aicw_add_campaign' ); $referer = wp_get_referer(); if ( ! current_user_can( 'manage_options' ) ) { ai_content_writer()->flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } $type = isset( $_POST['campaign_type'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_type'] ) ) : 'articles'; $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; $keywords = isset( $_POST['keywords'] ) ? wp_kses_post( wp_unslash( $_POST['keywords'] ) ) : ''; $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : 'publish'; $rss_feed = isset( $_POST['rss_feed_link'] ) ? sanitize_text_field( wp_unslash( $_POST['rss_feed_link'] ) ) : ''; // Validate the input fields. if ( empty( $title ) ) { ai_content_writer()->flash_notice( esc_html__( 'Title is required. Please enter the campaign title.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $type ) ) { ai_content_writer()->flash_notice( esc_html__( 'Campaign type is required. Please select the campaign type.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $keywords ) && 'rss-feed' !== $type ) { ai_content_writer()->flash_notice( esc_html__( 'Keywords are required. Please enter the keywords.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( 'rss-feed' === $type && empty( $rss_feed ) ) { ai_content_writer()->flash_notice( esc_html__( 'RSS feed link is required. Please enter the RSS feed link.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } // Create a new campaign. $campaign_id = wp_insert_post( array( 'post_title' => wp_strip_all_tags( $title ), 'post_content' => wp_kses_post( $keywords ), 'post_status' => $status, 'post_type' => 'aicw_campaign', ) ); if ( is_wp_error( $campaign_id ) ) { ai_content_writer()->flash_notice( $campaign_id->get_error_message(), 'error' ); wp_safe_redirect( $referer ); exit(); } // Update the campaign meta. $host = isset( $_POST['campaign_host'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_host'] ) ) : 'https://bing.com'; $source = isset( $_POST['campaign_source'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_source'] ) ) : ''; $block_keywords = isset( $_POST['block_keywords'] ) ? sanitize_text_field( wp_unslash( $_POST['block_keywords'] ) ) : ''; $search_replaces = isset( $_POST['search_replaces'] ) ? map_deep( wp_unslash( $_POST['search_replaces'] ), 'sanitize_text_field' ) : array(); $html_cleaners = isset( $_POST['html_cleaners'] ) ? sanitize_text_field( wp_unslash( $_POST['html_cleaners'] ) ) : ''; $insert_content_position = isset( $_POST['insert_content_position'] ) ? sanitize_text_field( wp_unslash( $_POST['insert_content_position'] ) ) : 'none'; $insert_content = isset( $_POST['insert_content'] ) ? wp_kses_post( wp_unslash( $_POST['insert_content'] ) ) : ''; $target = isset( $_POST['target'] ) ? sanitize_text_field( wp_unslash( $_POST['target'] ) ) : ''; $frequency = isset( $_POST['frequency'] ) ? sanitize_text_field( wp_unslash( $_POST['frequency'] ) ) : 'hourly'; $generate_thumb = isset( $_POST['generate_thumbnail'] ) ? 'yes' : ''; $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'post'; $completed_post_status = isset( $_POST['completed_post_status'] ) ? sanitize_text_field( wp_unslash( $_POST['completed_post_status'] ) ) : 'publish'; update_post_meta( $campaign_id, '_aicw_campaign_type', $type ); update_post_meta( $campaign_id, '_aicw_campaign_host', $host ); update_post_meta( $campaign_id, '_aicw_campaign_source', $source ); update_post_meta( $campaign_id, '_aicw_rss_feed_link', $rss_feed ); update_post_meta( $campaign_id, '_aicw_block_keywords', $block_keywords ); update_post_meta( $campaign_id, '_aicw_search_replaces', $search_replaces ); update_post_meta( $campaign_id, '_aicw_html_cleaners', $html_cleaners ); update_post_meta( $campaign_id, '_aicw_insert_content_position', $insert_content_position ); update_post_meta( $campaign_id, '_aicw_insert_content', $insert_content ); update_post_meta( $campaign_id, '_aicw_campaign_target', $target ); update_post_meta( $campaign_id, '_aicw_campaign_frequency', $frequency ); update_post_meta( $campaign_id, '_aicw_generate_thumbnail', $generate_thumb ); update_post_meta( $campaign_id, '_aicw_post_type', $post_type ); update_post_meta( $campaign_id, '_aicw_completed_post_status', $completed_post_status ); // Schedule the cron event to run the campaign. if ( 'publish' === $status ) { Cron::schedule_campaign( $campaign_id, $frequency ); } ai_content_writer()->flash_notice( esc_html__( 'Campaign added successfully.', 'ai-content-writer' ), 'success' ); $referer = remove_query_arg( 'add', $referer ); wp_safe_redirect( add_query_arg( 'edit', $campaign_id, $referer ) ); exit(); } /** * Edit campaign. * * @since 1.0.0 * @return void */ public static function handle_edit_campaign() { check_admin_referer( 'aicw_edit_campaign' ); $referer = wp_get_referer(); if ( ! current_user_can( 'manage_options' ) ) { ai_content_writer()->flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } $id = isset( $_POST['id'] ) ? absint( wp_unslash( $_POST['id'] ) ) : 0; $type = isset( $_POST['campaign_type'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_type'] ) ) : 'articles'; $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; $keywords = isset( $_POST['keywords'] ) ? wp_kses_post( wp_unslash( $_POST['keywords'] ) ) : ''; $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : 'publish'; $rss_feed = isset( $_POST['rss_feed_link'] ) ? sanitize_text_field( wp_unslash( $_POST['rss_feed_link'] ) ) : ''; // Validate the input fields. if ( empty( $id ) ) { ai_content_writer()->flash_notice( esc_html__( 'Invalid campaign. Please try again.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $title ) ) { ai_content_writer()->flash_notice( esc_html__( 'Title is required. Please enter the campaign title.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $type ) ) { ai_content_writer()->flash_notice( esc_html__( 'Campaign type is required. Please select the campaign type.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $keywords ) && 'rss-feed' !== $type ) { ai_content_writer()->flash_notice( esc_html__( 'Keywords are required. Please enter the keywords.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( 'rss-feed' === $type && empty( $rss_feed ) ) { ai_content_writer()->flash_notice( esc_html__( 'RSS feed link is required. Please enter the RSS feed link.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } // Update the campaign. $campaign_id = wp_update_post( array( 'ID' => $id, 'post_title' => wp_strip_all_tags( $title ), 'post_content' => wp_kses_post( $keywords ), 'post_status' => $status, ) ); if ( is_wp_error( $campaign_id ) ) { ai_content_writer()->flash_notice( $campaign_id->get_error_message(), 'error' ); wp_safe_redirect( $referer ); exit(); } // Update the campaign meta. $host = isset( $_POST['campaign_host'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_host'] ) ) : 'https://bing.com'; $source = isset( $_POST['campaign_source'] ) ? sanitize_text_field( wp_unslash( $_POST['campaign_source'] ) ) : ''; $block_keywords = isset( $_POST['block_keywords'] ) ? sanitize_text_field( wp_unslash( $_POST['block_keywords'] ) ) : ''; $search_replaces = isset( $_POST['search_replaces'] ) ? map_deep( wp_unslash( $_POST['search_replaces'] ), 'sanitize_text_field' ) : array(); $html_cleaners = isset( $_POST['html_cleaners'] ) ? sanitize_text_field( wp_unslash( $_POST['html_cleaners'] ) ) : ''; $insert_content_position = isset( $_POST['insert_content_position'] ) ? sanitize_text_field( wp_unslash( $_POST['insert_content_position'] ) ) : 'none'; $insert_content = isset( $_POST['insert_content'] ) ? wp_kses_post( wp_unslash( $_POST['insert_content'] ) ) : ''; $target = isset( $_POST['target'] ) ? sanitize_text_field( wp_unslash( $_POST['target'] ) ) : ''; $frequency = isset( $_POST['frequency'] ) ? sanitize_text_field( wp_unslash( $_POST['frequency'] ) ) : 'hourly'; $generate_thumb = isset( $_POST['generate_thumbnail'] ) ? 'yes' : ''; $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'post'; $completed_post_status = isset( $_POST['completed_post_status'] ) ? sanitize_text_field( wp_unslash( $_POST['completed_post_status'] ) ) : 'publish'; update_post_meta( $campaign_id, '_aicw_campaign_type', $type ); update_post_meta( $campaign_id, '_aicw_campaign_host', $host ); update_post_meta( $campaign_id, '_aicw_campaign_source', $source ); update_post_meta( $campaign_id, '_aicw_rss_feed_link', $rss_feed ); update_post_meta( $campaign_id, '_aicw_block_keywords', $block_keywords ); update_post_meta( $campaign_id, '_aicw_search_replaces', $search_replaces ); update_post_meta( $campaign_id, '_aicw_html_cleaners', $html_cleaners ); update_post_meta( $campaign_id, '_aicw_insert_content_position', $insert_content_position ); update_post_meta( $campaign_id, '_aicw_insert_content', $insert_content ); update_post_meta( $campaign_id, '_aicw_campaign_target', $target ); update_post_meta( $campaign_id, '_aicw_campaign_frequency', $frequency ); update_post_meta( $campaign_id, '_aicw_generate_thumbnail', $generate_thumb ); update_post_meta( $campaign_id, '_aicw_post_type', $post_type ); update_post_meta( $campaign_id, '_aicw_completed_post_status', $completed_post_status ); // Schedule the cron event to run the campaign. if ( 'publish' === $status ) { Cron::schedule_campaign( $campaign_id, $frequency ); } ai_content_writer()->flash_notice( esc_html__( 'Campaign updated successfully.', 'ai-content-writer' ), 'success' ); wp_safe_redirect( $referer ); exit(); } /** * Generate content. * * @since 1.0.0 * @return void */ public static function handle_generate_content() { check_admin_referer( 'aicw_generate_content' ); $referer = wp_get_referer(); if ( ! current_user_can( 'manage_options' ) ) { ai_content_writer()->flash_notice( esc_html__( 'You do not have permission to perform this action.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } $prompt_content = isset( $_POST['prompt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['prompt'] ) ) : ''; $keywords = isset( $_POST['keywords'] ) ? sanitize_text_field( wp_unslash( $_POST['keywords'] ) ) : ''; $prompt_type = isset( $_POST['prompt_type'] ) ? sanitize_text_field( wp_unslash( $_POST['prompt_type'] ) ) : 'Prompt'; $language = isset( $_POST['language'] ) ? sanitize_text_field( wp_unslash( $_POST['language'] ) ) : esc_html__( 'English', 'ai-content-writer' ); $min_words = isset( $_POST['min_words'] ) ? absint( wp_unslash( $_POST['min_words'] ) ) : ''; $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : ''; $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : 'draft'; // For ChatGPT OpenAI API model. $system_tone = isset( $_POST['system_tone'] ) ? sanitize_textarea_field( wp_unslash( $_POST['system_tone'] ) ) : ''; if ( empty( $system_tone ) ) { $system_tone = esc_html__( 'You are an expert SEO content writer. Generate factually accurate, engaging, and well-structured articles optimized for readability and search engines.', 'ai-content-writer' ); } $max_tokens = isset( $_POST['max_tokens'] ) ? absint( wp_unslash( $_POST['max_tokens'] ) ) : ''; $temperature = isset( $_POST['temperature'] ) ? floatval( wp_unslash( $_POST['temperature'] ) ) : ''; // Validate the input fields. if ( empty( $prompt_content ) ) { ai_content_writer()->flash_notice( esc_html__( 'Title is required. Please, start with "Write an article on..."', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $prompt_type ) ) { ai_content_writer()->flash_notice( esc_html__( 'Prompt type is required. Please, select the prompt type.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $language ) ) { ai_content_writer()->flash_notice( esc_html__( 'Language is required. Please, select the language.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } if ( empty( $post_type ) ) { ai_content_writer()->flash_notice( esc_html__( 'Post type is required. Please, select the post type.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } // Check the API model. $title = $prompt_content; $api_model = get_option( 'aicw_api_model' ); if ( 'gemini' === $api_model ) { $api_key = get_option( 'aicw_gemini_api_key' ); if ( empty( $api_key ) ) { ai_content_writer()->flash_notice( esc_html__( 'Please configure the API settings. A valid Gemini API key is required to generate the content.', 'ai-content-writer' ), 'error' ); wp_safe_redirect( $referer ); exit(); } // Generating prompt for the Gemini API model. $prompt = $prompt_type . ': ' . $prompt_content . ' in ' . $language; if ( ! empty( $keywords ) ) { $prompt = $prompt . ' with keywords: ' . $keywords . ','; } if ( ! empty( $min_words ) ) { $prompt = $prompt . ' with minimum words: ' . $min_words; } // Generate content using the Google Gemini OpenAI API. $prompt_data = array( 'text' => $prompt, ); $content = ai_content_writer()->generate_content( $prompt_data ); if ( is_wp_error( $content ) ) { ai_content_writer()->flash_notice( $content->get_error_message(), 'error' ); wp_safe_redirect( $referer ); exit(); } // Get all the matches from the generated content string starting form "**" and end with "**" as array. $pattern = '/\*\*(.*?)\*\*/'; preg_match_all( $pattern, $content, $matches ); // Get the title. $title = $matches[1][0]; // Remove the title from the content. $content = str_replace( $matches[0][0], '', $content ); if ( is_array( $matches[0] ) && is_array( $matches[1] ) ) { foreach ( $matches[0] as $key => $match ) { // replaces the strings. $content = str_replace( $match, '