text($markdownText); return $html; } class URL_Scanner_Plugin extends rendersetting{ public $active = true; public $active_option_name = 'URL_Scanner_Plugin_active'; public $usage_option_name = 'Schedule_AI_usage'; public $icon = ''; protected $postfields = array(); protected $shortcodes = array(); protected $htmltags = array(); public $limit = AIAUTOTOOL_FREE; private $plan_limit_aiautotool ; public $name_plan ; public $config = array(); public $notice ; public function __construct() { $this->name_plan = esc_html__('Autoblogging AI post','ai-auto-tool'); $this->plan_limit_aiautotool = 'plan_limit_aiautotool_'.$this->active_option_name; $this->notice = new aiautotool_Warning_Notice(); $this->active = get_option($this->active_option_name, true); if ($this->active=='true') { $this->init(); } add_action('wp_ajax_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); add_action('wp_ajax_nopriv_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); } private function aiautotool_has_plugin_data() { return get_option($this->plan_limit_aiautotool) !== false; } private function aiautotool_initialize_plugin_data() { $current_date = date('Y-m-d'); $expiration = strtotime('+1 month', strtotime($current_date)); $expiration = date('Y-m-d', $expiration); update_option($this->plan_limit_aiautotool, array( 'start_date' => $current_date,'expiration'=>$expiration)); } public function aiautotool_check_post_limit() { $stored_data = get_option($this->plan_limit_aiautotool, array()); $current_date = date('Y-m-d'); if ($this->is_new_month($current_date, $stored_data['start_date'])) { $expiration = strtotime('+1 month', strtotime($current_date)); $expiration = date('Y-m-d', $expiration); update_option($this->plan_limit_aiautotool, array( 'start_date' => $current_date,'expiration'=>$expiration)); update_option($this->usage_option_name, 0,null, 'no'); } } private function is_new_month($current_date, $start_date) { $current_month = date('m', strtotime($current_date)); $start_month = date('m', strtotime($start_date)); return ($current_month != $start_month); } public function aiautotool_checklimit($config) { return $this->check_quota(); } public function aiautotool_update_usage() { $this->aiautotool_check_post_limit(); $current_value = get_option($this->usage_option_name, 0); $new_value = $current_value + 1; if($this->config['number_post']!=-1){ if($this->config['number_post'] > $new_value){ update_option($this->usage_option_name, $new_value, 'no'); } }else{ update_option($this->usage_option_name, $new_value, 'no'); } return $new_value; } public function render_plan(){ if ($this->active=='true') { $quota = $this->config['number_post'] == -1 ? 'Unlimited' : esc_html($this->config['number_post']); echo wp_kses( '

' . $this->icon . ' ' . esc_html($this->name_plan) . ': Usage: ' . esc_html($this->config['usage']) . '

', [ 'p' => [], 'i' => ['class' => []], 'strong' => [], ] ); } } public function update_active_option_callback() { check_ajax_referer('aiautotool_nonce', 'security'); if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active,null, 'no'); print_r($active); } wp_die(); } public function init(){ $configs = get_option($this->plan_limit_aiautotool, array()); if (!$this->aiautotool_has_plugin_data()) { $this->aiautotool_initialize_plugin_data(); $configs = (array) $this->aiautotool_has_plugin_data(); } if(!isset($configs['expiration'])) { $current_date = date('Y-m-d'); $expiration = strtotime('+1 month', strtotime($current_date)); $configs['expiration'] = date('Y-m-d', $expiration); } $this->config = array( 'number_post'=>$this->limit, 'usage'=>get_option($this->usage_option_name, 0, 'no'), 'time_exprice'=>$configs['expiration'] ); if($this->is_premium()->get_plan_name()=='aiautotoolpro'||$this->is_premium()->get_plan_name()=='premium'){ $this->config = array( 'number_post'=>-1, 'usage'=>get_option($this->usage_option_name, 0, 'no'), 'time_exprice'=>$this->is_premium()->_get_license()->expiration ); } add_action('admin_menu', array($this, 'add_menu')); add_action('admin_init', array($this, 'aiautotool_check_post_limit')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); if ($this->aiautotool_checklimit($this->config)) { add_filter('cron_schedules', array($this, 'aiautotool_schedule_cron_intervals')); add_filter('cron_schedules', array($this, 'schedule_create_content')); if (!wp_next_scheduled('cron_task_event_new')) { wp_schedule_event(time(), 'aiautotool_schedule_cron_intervals', 'cron_task_event_new'); } if (!wp_next_scheduled('schedule_create_content_callback')) { wp_schedule_event(time(), 'schedule_create_content', 'schedule_create_content_callback'); } add_action('cron_task_event_new', array($this, 'cron_task')); add_action('schedule_create_content_callback', array($this, 'schedule_create_content_callback')); }else{ $this->notice->add_notice( AIAUTOTOOL_TITLE_UPGRADE, 'notice-error', null, true, 'aiautotool' ); } add_action('wp_ajax_add_post', array($this, 'add_post_callback')); add_action('wp_ajax_nopriv_add_post', array($this, 'add_post_callback')); add_action('rest_api_init', array($this, 'register_aiautotool_scan_route')); add_action('wp_ajax_clear_log_action', array($this, 'clear_log_action_callback')); add_action('url_scanner_clear_log_cron', array($this, 'clear_log_action_callback')); if (!wp_next_scheduled('url_scanner_clear_log_cron')) { wp_schedule_event(time(), 'twicedaily', 'url_scanner_clear_log_cron'); } add_action('wp_ajax_aiautotool_idea_title_post', array($this, 'aiautotool_idea_title_post')); } public function add_button_auto_Content($actions, $post) { $post_id = $post->ID; $custom_button_html = 'Bard
'; $actions['custom'] = $custom_button_html; return $actions; } public function render_setting() { ?> '; } $current_interval = get_option('aiautotool_draft_public_time', 5); $number_post = get_option('aiautotool_draft_public_number_post', 1); $aiautotool_draft_public_turnoff_auto_images = get_option('aiautotool_draft_public_turnoff_auto_images', false); $default_language = get_option('aiautotool_default_language', 'vi'); $languages = $this->languageCodes; ?>

:

:

:


active=='true') { echo ''; } } public function render_feature(){ $autoToolBox = new AutoToolBox(wp_kses_post($this->icon).' '.esc_html__('Autoblogging AI post','ai-auto-tool'), "Autoblogging uses AI Gemini. Just put in the list of article titles and set the time to post. The system works 100% automatically to take care of your website every day", "https://doc.aiautotool.com/integrations/schedule-ai-post-user-guide", $this->active_option_name, $this->active,esc_url(plugins_url('../images/logo.svg', __FILE__))); echo ($autoToolBox->generateHTML()); } function add_post_callback() { check_ajax_referer('aiautotool_nonce', 'security'); $languageCodes = $this->languageCodes; $post_titles = sanitize_textarea_field($_POST['post_titles']); $post_category = intval($_POST['post_category']); $post_tags = sanitize_text_field($_POST['post_tags']); $post_language = sanitize_text_field($_POST['post_language']); $anchor_text = wp_kses_post($_POST['anchor_text']); $auto_change_title = 0;//isset($_POST['auto_change_title']) ? true : false; $aiautotool_prompt = isset($_POST['aiautotool_prompt']) ? sanitize_text_field($_POST['aiautotool_prompt']):''; $titles = explode(PHP_EOL, $post_titles); $user_id = ''; $user_ids = get_users( array( 'fields' => 'ID', ) ); $kq = array(); foreach ($titles as $title) { $words = explode(",", $post_tags); if (count($words) > 3) { $randomWords = array_rand($words, 3); $result = []; foreach ($randomWords as $index) { $result[] = $words[$index]; } } else { $result = $words; } $post_tags = implode(",", $result); $title = ucwords(mb_strtolower(html_entity_decode($title))); $random_user_id = $user_ids[array_rand($user_ids)]; $user_id = $random_user_id; $new_post = array( 'post_title' => $title, 'post_content' => '', 'post_name' => sanitize_title($title), 'post_status' => 'draft', 'post_author'=>$user_id, 'post_category' => array($post_category), 'tags_input' => $post_tags, 'post_type' => 'post', 'lang' => $post_language ); $post_id = wp_insert_post($new_post); $selectedLanguageName = ''; if(array_key_exists($post_language, $languageCodes)) { $selectedLanguageName = $languageCodes[$post_language]; if(in_array('polylang/polylang.php', apply_filters('active_plugins', get_option('active_plugins')))){ pll_set_post_language($post_id, $post_language); } } update_post_meta($post_id, 'lang', $selectedLanguageName); update_post_meta($post_id, 'aiautotool_prompt', $aiautotool_prompt); update_post_meta($post_id, 'auto_change_title', $auto_change_title); update_post_meta($post_id, 'aiautotool_linkin', $anchor_text); $kq[] = array('title'=>$title,'url_edit'=>get_edit_post_link($post_id)); } wp_send_json_success($kq); } private function fix_content_spin($content){ $spintax = new Spintaxkct(); $content = $spintax->process($content); return trim($content); } public function aiautotool_schedule_cron_intervals($schedules) { $current_interval = get_option('aiautotool_draft_public_time', 5); $schedules['aiautotool_schedule_cron_intervals'] = array( 'interval' => intval($current_interval) * 60, 'display' => __('aiautotool bard content public', 'ai-auto-tool') ); return $schedules; } public function schedule_create_content($schedules) { $schedules['schedule_create_content'] = array( 'interval' => 60, 'display' => 'Schedule create content' ); return $schedules; } public function schedule_cron() { if (!wp_next_scheduled('url_scanner_cron')) { wp_schedule_event(time(), 'minute', 'url_scanner_cron'); } } public function schedule_create_content_callback(){ global $wpdb; $log = new AIautoTool_log(); $cron_widget = AIAutoTool_Cron_Status_Widget::get_instance(); // Update cron status - starting $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Searching for draft posts...', 'aiautotool-cron-widget'), 'current_post_title' => '', 'progress' => 10, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); if (false === ($draft_posts = get_transient('aiautotool_draft_posts'))) { $query = $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_content = '' AND post_status = 'draft' AND post_type = 'post' LIMIT 50" ); $draft_posts = $wpdb->get_col($query); set_transient('aiautotool_draft_posts', $draft_posts, 24 * HOUR_IN_SECONDS); } $post_id = array_shift($draft_posts); if (!$post_id) { // No draft posts found $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('No draft posts found to publish', 'aiautotool-cron-widget'), 'current_post_title' => '', 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); return; } $post = get_post($post_id); if (!$post) { $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => 'Lỗi: Không tìm thấy bài viết', 'current_post_title' => '', 'progress' => 100, 'cron_type' => 'Auto Content' )); return; } $attachThumbnail = ''; if($this->aiautotool_checklimit($this->config)){ $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>get_the_title($post_id),'msg'=>'star run create content')); // Update status - found post $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Creating content for post...', 'aiautotool-cron-widget'), 'current_post_title' => get_the_title($post_id), 'progress' => 20, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); if ($post) { $post_id = $post->ID; $auto_generate_title = 0; $aiautotool_prompt = get_post_meta($post_id, 'aiautotool_prompt', true); $lang = get_post_meta($post_id, 'lang', 'Vietnamese'); if (empty($lang) || $lang === 'Vietnamese') { $default_language = get_option('aiautotool_default_language', 'vi'); $lang = $this->languageCodes[$default_language] ?? 'Vietnamese'; } $aiautotool_linkin = get_post_meta($post_id, 'aiautotool_linkin', true); $bardGenContent = new BardGenContent(); $Imagescontent = new Imagescontent(); $title = self::fix_years(get_the_title($post_id)); $allprompt = get_option('aiautotool_prompt_options',array()); $auto = true; if(empty($allprompt)){ $auto = true; }else{ if($aiautotool_prompt==''){ if(isset($allprompt['aiautotool_prompt_artcile'])) { $artcle_prompt = $allprompt['aiautotool_prompt_artcile']; } }else{ $artcle_prompt = $aiautotool_prompt; } $artcle_prompt = str_replace('%%title%%',$title,$artcle_prompt); $content=''; $cate = ''; $blogtitle = ''; $prompt = $this->aiautotool_fix_question($artcle_prompt,$title,$lang,$blogtitle,$cate,$content); $auto = false; } // Update status - calling AI $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Calling AI to generate content...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 40, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); if($auto){ $newcontent = $bardGenContent->bardcontentfull($title,$lang); }else{ $newcontent = $bardGenContent->bardcontentmore($prompt,$lang); } $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'run bard api create contet post'.$newcontent)); if (!empty($newcontent)) { $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'create content success, next step find img')); // Update status - content created, finding images $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Searching for images...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 60, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $aiautotool_draft_public_turnoff_auto_images = get_option('aiautotool_draft_public_turnoff_auto_images', false); if($aiautotool_draft_public_turnoff_auto_images===false){ $listimg = $bardGenContent->searchimg($title); }else{ $listimg = false; } $current_content = $post->post_content; $current_content = apply_filters('the_content', $current_content); $current_content = str_replace(']]>', ']]>', $current_content); $current_content = preg_replace('##i', '', $current_content); $updated_content = $newcontent . $current_content; $updated_content = self::fix_years($updated_content); $random_count = mt_rand(1, 3); if (!empty($listimg) && is_int($random_count) && $random_count > 0) { $random_count = min($random_count, count($listimg)); $random_keys = array_rand($listimg, $random_count); } if ($listimg) { $listimg10 = array_slice($listimg, 0, 10); shuffle($listimg10); $updated_content = $Imagescontent->insertImages($updated_content,$listimg10); $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'Found image success, next step update content to post.')); }else{ $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'No found image.')); } // Update status - processing images $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Processing images...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 80, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $postname = $this->kct_aiautotool_sanitize($title); $html = stripslashes($updated_content); preg_match_all('/]+src\s*=\s*["\']([^"\']+)["\']/i', $html, $matches); $listimg1 = $matches[1]; $imgUploaded = array(); $attachThumnail = 0; if (!empty($listimg1)){ foreach ($listimg1 as $post_image_url){ try { $image_url_new = $this->kct_aiautotool_save_image($post_image_url,$postname); if (!empty($image_url_new)){ $imgUploaded[] = $image_url_new; if ($attachThumbnail == 0) { $attachThumbnail = $image_url_new['attach_id']; } } }catch (Exception $e) { } } } foreach ($imgUploaded as $img){ $updated_content = str_replace($img['url'],$img['baseurl'],$updated_content); } if($auto_generate_title != 0){ $title = $bardGenContent->gentitle($title,$lang); } if ($attachThumbnail != 0) { set_post_thumbnail($post_id, $attachThumbnail); } if($aiautotool_linkin!=''){ $textadd = self::fix_content_spin($aiautotool_linkin); $updated_content = self::insert_text_after_third_paragraph($updated_content, $textadd); } // Update status - saving post $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Saving post...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 90, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $post_data = array( 'ID' => $post_id, 'post_title'=>$title, 'post_content' => $updated_content, 'post_status'=>'draft' ); $post_id = wp_update_post($post_data); $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'create content success and Update content to post draft : '.$title)); $this->aiautotool_update_usage(); $this->notice->add_notice( 'create content success and Update content to post draft : '.$title , 'notice-info', null, true,$this->name_plan ); // Update status - completed $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Content creation completed', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); }else{ $log->set_log('schedule_gen_content',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'create content Error '.$title)); // Update status - error $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Error: Unable to create content', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } wp_reset_postdata(); } if (!empty($draft_posts)) { set_transient('aiautotool_draft_posts', $draft_posts, 24 * HOUR_IN_SECONDS); } else { delete_transient('aiautotool_draft_posts'); } }else{ $this->notice->add_notice( AIAUTOTOOL_TITLE_UPGRADE, 'notice-error', null, true, 'aiautotool' ); // Update status - quota exceeded $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Usage limit exceeded', 'aiautotool-cron-widget'), 'current_post_title' => '', 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } } public static function insert_text_after_third_paragraph($content, $text) { preg_match_all('//', $content, $matches, PREG_OFFSET_CAPTURE); $total_paragraphs = count($matches[0]); $random_position = mt_rand(2, $total_paragraphs - 1); if (isset($matches[0][$random_position])) { $paragraph_pos = $matches[0][$random_position][1] + strlen($matches[0][$random_position][0]); $content = substr($content, 0, $paragraph_pos) . $text . substr($content, $paragraph_pos); } return $content; } private function process_post_content($post_id, $title, $lang, $auto_generate_title, $log){ } public function cron_task2() { $log = new AIautoTool_log(); set_time_limit(300); $draft_posts = get_transient('aiautotool_draft_posts_public'); if (false === $draft_posts) { $number_post = get_option('aiautotool_draft_public_number_post', 500); $args = array( 'post_type' => 'post', 'post_status' => 'draft', 'posts_per_page' => $number_post, ); $draft_posts_query = new WP_Query($args); if ($draft_posts_query->have_posts()) { $draft_posts = array(); while ($draft_posts_query->have_posts()) { $draft_posts_query->the_post(); $draft_posts[] = get_the_ID(); } set_transient('aiautotool_draft_posts_public', $draft_posts, 24 * HOUR_IN_SECONDS); } else { return; } } $post_id = array_shift($draft_posts); if (!$post_id) { delete_transient('aiautotool_draft_posts_public'); return; } $content_post = get_post($post_id); $current_content = $content_post->post_content; if (strlen($current_content) < 255) { if($this->aiautotool_checklimit($this->config)){ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'star run bard api')); $auto_generate_title = 0;//get_post_meta($post_id, 'auto_generate_title', false); $aiautotool_prompt = get_post_meta($post_id, 'aiautotool_prompt', true); $lang = get_post_meta($post_id, 'lang', 'Vietnamese'); // Use default language if post doesn't have language set if (empty($lang) || $lang === 'Vietnamese') { $default_language = get_option('aiautotool_default_language', 'vi'); $lang = $this->languageCodes[$default_language] ?? 'Vietnamese'; } $aiautotool_linkin = get_post_meta($post_id, 'aiautotool_linkin', true); $bardGenContent = new BardGenContent(); $Imagescontent = new Imagescontent(); $title = self::fix_years(get_the_title($post_id)); $allprompt = get_option('aiautotool_prompt_options',array()); $auto = true; if(empty($allprompt)){ $auto = true; }else{ if($aiautotool_prompt==''){ if(isset($allprompt['aiautotool_prompt_artcile'])) { $artcle_prompt = $allprompt['aiautotool_prompt_artcile']; } }else{ $artcle_prompt = $aiautotool_prompt; } $content=''; $cate = ''; $blogtitle = ''; $prompt = $this->aiautotool_fix_question($artcle_prompt,$title,$lang,$blogtitle,$cate,$content); $auto = false; } if($auto){ $newcontent = $bardGenContent->bardcontentfull($title,$lang); }else{ $newcontent = $bardGenContent->bardcontentmore($prompt,$lang); } $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content by bard api')); if (!empty($newcontent)) { $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content by bard api success next step search img')); $listimg = $bardGenContent->searchimg($title); $current_content = apply_filters('the_content', $current_content); $current_content = str_replace(']]>', ']]>', $current_content); $current_content = preg_replace('##i', '', $current_content); $updated_content = $newcontent . $current_content; $updated_content = self::fix_years($updated_content); // $updated_content = self::preserveHtmlTagsShortcodes($updated_content); $random_count = mt_rand(1, 3); $random_keys = array_rand($listimg, $random_count); if ($listimg) { $listimg10 = array_slice($listimg, 0, 10); shuffle($listimg10); $updated_content = $Imagescontent->insertImages($updated_content,$listimg10); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Had found images , add to content')); }else{ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'No found images.')); } $postname = $this->kct_aiautotool_sanitize($title); $html = stripslashes($updated_content); preg_match_all('/]+src\s*=\s*["\']([^"\']+)["\']/i', $html, $matches); $listimg1 = $matches[1]; $imgUploaded = array(); $attachThumnail = 0; if (!empty($listimg1)){ foreach ($listimg1 as $post_image_url){ try { $image_url_new = $this->kct_aiautotool_save_image($post_image_url,$postname); if (!empty($image_url_new)){ $imgUploaded[] = $image_url_new; if ($attachThumbnail == 0) { $attachThumbnail = $image_url_new['attach_id']; } } }catch (Exception $e) { } } } foreach ($imgUploaded as $img){ $updated_content = str_replace($img['url'],$img['baseurl'],$updated_content); } if($auto_generate_title!=0){ $title = $bardGenContent->gentitle($title,$lang); } if ($attachThumbnail ) { set_post_thumbnail($post_id, $attachThumbnail); } $post_data = array( 'ID' => $post_id, 'post_title'=>$title, 'post_content' => $updated_content, 'post_status'=>'draft' ); $post_id = wp_update_post($post_data); $this->update_and_publish_post( $post_id ); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Publish post '.get_the_title())); $this->aiautotool_update_usage(); }else{ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content Error 1 '.$title)); } }else{ $this->notice->add_notice( AIAUTOTOOL_TITLE_UPGRADE, 'notice-error', null, true, $this->name_plan ); } set_transient('aiautotool_draft_posts_public', $draft_posts, 24 * HOUR_IN_SECONDS); } else { $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'star run bard api and next step: publish post has content post.')); $result = $this->update_and_publish_post( $post_id ); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Publish post '.get_the_title())); } wp_reset_postdata(); } public function cron_task() { set_time_limit(300); $log = new AIautoTool_log(); // Update cron status - starting publish task $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => 'Đang tìm bài viết để publish...', 'current_post_title' => '', 'progress' => 10, 'next_schedule' => 'Lần chạy tiếp theo: ' . date('H:i:s', time() + get_option('aiautotool_draft_public_time', 5) * 60) )); $number_post = get_option('aiautotool_draft_public_number_post', 1); $args = array( 'post_type' => 'post', 'post_status' => 'draft', 'posts_per_page' => $number_post, ); $draft_posts = new WP_Query($args); if ($draft_posts->have_posts()) { while ($draft_posts->have_posts()) { $draft_posts->the_post(); $post_id = get_the_ID(); $content_post = get_post($post_id); $current_content = $content_post->post_content; if(strlen($current_content) < 255){ if($this->aiautotool_checklimit($this->config)){ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'star run bard api')); // Update status - found post to process $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Creating content for post to publish...', 'aiautotool-cron-widget'), 'current_post_title' => get_the_title($post_id), 'progress' => 20, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); // Update status - calling AI $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Calling AI to generate content...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 40, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); if($auto){ $newcontent = $bardGenContent->bardcontentfull($title,$lang); }else{ $newcontent = $bardGenContent->bardcontentmore($prompt,$lang); } // $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>$title,'msg'=>'Prompt with create contet post'.$prompt)); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content by bard api')); if (!empty($newcontent)) { $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content by bard api success next step search img')); // Update status - content created, finding images $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Searching for images...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 60, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $listimg = $bardGenContent->searchimg($title); // $content_post = get_post($post_id); // $current_content = $content_post->post_content; $current_content = apply_filters('the_content', $current_content); $current_content = str_replace(']]>', ']]>', $current_content); $current_content = preg_replace('##i', '', $current_content); $updated_content = $newcontent . $current_content; $updated_content = self::fix_years($updated_content); // $updated_content = self::preserveHtmlTagsShortcodes($updated_content); $random_count = mt_rand(1, 3); $random_keys = array_rand($listimg, $random_count); if ($listimg) { $listimg10 = array_slice($listimg, 0, 10); shuffle($listimg10); $updated_content = $Imagescontent->insertImages($updated_content,$listimg10); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Had found images , add to content')); }else{ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'No found images.')); } // Update status - processing images $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Processing images...', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 80, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $postname = $this->kct_aiautotool_sanitize($title); $html = stripslashes($updated_content); preg_match_all('/]+src\s*=\s*["\']([^"\']+)["\']/i', $html, $matches); $listimg1 = $matches[1]; $imgUploaded = array(); $attachThumnail = 0; if (!empty($listimg1)){ foreach ($listimg1 as $post_image_url){ try { $image_url_new = $this->kct_aiautotool_save_image($post_image_url,$postname); if (!empty($image_url_new)){ $imgUploaded[] = $image_url_new; if ($attachThumbnail == 0) { $attachThumbnail = $image_url_new['attach_id']; } } }catch (Exception $e) { } } } foreach ($imgUploaded as $img){ $updated_content = str_replace($img['url'],$img['baseurl'],$updated_content); } if($auto_generate_title!=0){ $title = $bardGenContent->gentitle($title,$lang); } if ($attachThumbnail != 0) { set_post_thumbnail($post_id, $attachThumbnail); } // Update status - publishing post $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => 'Đang publish bài viết...', 'current_post_title' => $title, 'progress' => 90, 'next_schedule' => 'Lần chạy tiếp theo: ' . date('H:i:s', time() + get_option('aiautotool_draft_public_time', 5) * 60) )); $post_data = array( 'ID' => $post_id, 'post_title'=>$title, 'post_content' => $updated_content, 'post_status'=>'draft' ); $post_id = wp_update_post($post_data); $this->update_and_publish_post( $post_id ); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Publish post '.get_the_title())); $this->aiautotool_update_usage(); // Update status - completed $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Content creation completed', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); }else{ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'create content Error 1 '.$title)); // Update status - error $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Error: Unable to create content', 'aiautotool-cron-widget'), 'current_post_title' => $title, 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } }else{ $this->notice->add_notice( AIAUTOTOOL_TITLE_UPGRADE, 'notice-error', null, true, $this->name_plan ); // Update status - quota exceeded $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Usage limit exceeded', 'aiautotool-cron-widget'), 'current_post_title' => '', 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } }else{ $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'star run bard api and next step: publish post has content post.')); // Update status - publishing existing content $cron_widget->update_cron_status(array( 'is_running' => true, 'current_task' => __('Publishing post with existing content...', 'aiautotool-cron-widget'), 'current_post_title' => get_the_title($post_id), 'progress' => 90, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); $result = $this->update_and_publish_post( $post_id ); $log->set_log('schedule_publish',array('post_id'=>$post_id,'post_title'=>get_the_title(),'msg'=>'Publish post '.get_the_title())); // Update status - completed $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Post published successfully', 'aiautotool-cron-widget'), 'current_post_title' => get_the_title($post_id), 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } } } else { // No draft posts found $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('No draft posts found to publish', 'aiautotool-cron-widget'), 'current_post_title' => '', 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } wp_reset_postdata(); } public function update_and_publish_post($post_id) { $current_time = current_time('mysql'); $post_data = array( 'ID' => $post_id, 'post_date' => $current_time, 'post_date_gmt' => get_gmt_from_date($current_time), 'post_status' =>'publish' ); wp_update_post($post_data); $pingback = new aiautotool_Telegram_Notifications('6845486754:AAH5KyPTuluu_OCRnPklp6UEbzVfe0CtHkU','730694172'); $message = "Publish article:".get_permalink($post_id)."\n"; $message .= "timeactive: " . current_time('Y-m-d H:i:s') . "\n"; $pingback->send_bot_message($message); // Update cron status with success notification $cron_widget->update_cron_status(array( 'is_running' => false, 'current_task' => __('Post published successfully', 'aiautotool-cron-widget'), 'current_post_title' => get_the_title($post_id), 'progress' => 100, 'cron_type' => __('Auto Content', 'aiautotool-cron-widget') )); } public function bard_content($post_id){ // Hàm xử lý công bố bài viết dạng draft $auto_generate_title = get_post_meta($post_id, 'auto_generate_title', true); // Lấy giá trị meta $lang = get_post_meta($post_id, 'lang', true); // Use default language if post doesn't have language set if (empty($lang)) { $default_language = get_option('aiautotool_default_language', 'vi'); $lang = $this->languageCodes[$default_language] ?? 'Vietnamese'; } $bardGenContent = new BardGenContent(); $Imagescontent = new Imagescontent(); $title = self::fix_years(get_the_title($post_id)); // Lấy tiêu đề của bài viết dựa trên ID $newcontent = $bardGenContent->bardcontent($title, $lang); if (!empty($newcontent)) { $listimg = $bardGenContent->searchimg($title); $content_post = get_post($post_id); $current_content = $content_post->post_content; $current_content = apply_filters('the_content', $current_content); $current_content = str_replace(']]>', ']]>', $current_content); $current_content = preg_replace('##i', '', $current_content); $updated_content = $newcontent . $current_content; $updated_content = self::fix_years($updated_content); $random_count = mt_rand(1, 3); $random_keys = array_rand($listimg, $random_count); if ($listimg) { $listimg10 = array_slice($listimg, 0, 10); shuffle($listimg10); $updated_content = $Imagescontent->insertImages($updated_content, $listimg10); } $postname = $this->kct_aiautotool_sanitize($title); $html = stripslashes($updated_content); preg_match_all('/]+src\s*=\s*["\']([^"\']+)["\']/i', $html, $matches); $listimg1 = $matches[1]; $imgUploaded = array(); $attachThumbnail = 0; if (!empty($listimg1)){ foreach ($listimg1 as $post_image_url){ try { $image_url_new = $this->kct_aiautotool_save_image($post_image_url, $postname); if (!empty($image_url_new)){ $imgUploaded[] = $image_url_new; if ($attachThumbnail == 0) { $attachThumbnail = $image_url_new['attach_id']; } } } catch (Exception $e) { } } } foreach ($imgUploaded as $img){ $updated_content = str_replace($img['url'], $img['baseurl'], $updated_content); } if ($auto_generate_title){ $title = $bardGenContent->gentitle($title, $lang); } if ($attachThumbnail != 0) { set_post_thumbnail($post_id, $attachThumbnail); } $post_data = array( 'ID' => $post_id, 'post_title' => $title, 'post_content' => $updated_content, 'post_status' => 'publish' ); $post_id = wp_update_post($post_data); return get_permalink($post_id); }else{ return false; } } public function kct_aiautotool_save_image($imgURL,$post_title){ $imageUploader = new KCT_AIAutoTool_ImageUploader(); $kq = $imageUploader->saveImage($imgURL, $post_title); return $kq; } public function kct_aiautotool_sanitize($title) { $replacement = '-'; $map = array(); $quotedReplacement = preg_quote($replacement, '/'); $default = array( '/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|å/' => 'a', '/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|ë/' => 'e', '/ì|í|ị|ỉ|ĩ|Ì|Í|Ị|Ỉ|Ĩ|î/' => 'i', '/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|ø/' => 'o', '/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|ů|û/' => 'u', '/ỳ|ý|ỵ|ỷ|ỹ|Ỳ|Ý|Ỵ|Ỷ|Ỹ/' => 'y', '/đ|Đ/' => 'd', '/ç/' => 'c', '/ñ/' => 'n', '/ä|æ/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue', '/Ä/' => 'Ae', '/Ü/' => 'Ue', '/Ö/' => 'Oe', '/ß/' => 'ss', '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ', '/\\s+/' => $replacement, sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '', ); //Some URL was encode, decode first $title = urldecode($title); $map = array_merge($map, $default); return strtolower(preg_replace(array_keys($map), array_values($map), $title)); } public function preserveHtmlTagsShortcodes($string) { //preserve shortcodes if (preg_match_all("/\[.*\]/", $string , $shortcodes)) { $this->shortcodes = $shortcodes[0]; foreach($this->shortcodes as $num => $tag) { $string = str_replace($tag, "[3x" . (10000+ $num) . "]", $string); } } // and html tags if(preg_match_all('@<[\/\!]*?[^<>]*?>@si', $string, $htmltags)) { $this->htmltags = $htmltags[0]; foreach($this->htmltags as $num => $tag) { $string = str_replace($tag, "[3x" . (1000+ $num) . "]", $string); } } return $string; } public function replaceHTMLTagsShortcodes($string) { // replace the preserved html tags foreach($this->htmltags as $num => $tag) { $string = str_replace("[3x" . (1000+ $num) . "]", $tag, $string); } // and shortcodes foreach($this->shortcodes as $num => $tag) { $string = str_replace("[3x" . (10000+ $num) . "]", $tag, $string); } return $string; } public function fix_years($title) { return $title; } public function register_aiautotool_scan_route(){ register_rest_route('aiautotool/v1', '/bardcontent', array( 'methods' => 'POST', // Phương thức POST để gửi dữ liệu JSON 'callback' => array($this, 'bard_gencontent'), )); register_rest_route('aiautotool/v1', '/createcontentbard', array( 'methods' => 'POST', // Phương thức POST để gửi dữ liệu JSON 'callback' => array($this, 'bard_content_request'), )); } public function bard_content_request($request){ $idpost = $request['post_id']; $kq = $this->bard_content($idpost); return wp_send_json_success($kq ); } public function bard_gencontent($request){ $title = $request['title']; $idpost = $request['id']; $bardGenContent = new BardGenContent(); $newcontent = $bardGenContent->sendRequest($title); if (!empty($newcontent)) { $content_post = get_post($idpost); $current_content = $content_post->post_content; $current_content = apply_filters('the_content', $current_content); $current_content = str_replace(']]>', ']]>', $current_content); $updated_content = $newcontent . $current_content; // Cập nhật nội dung của bài viết $post_data = array( 'ID' => $idpost, 'post_content' => $updated_content, ); $post_id = wp_update_post($post_data); } return wp_send_json_success($newcontent); } public function aiautotool_get_all_draft() { $drafts = get_posts(array( 'post_type' => 'post', 'post_status' => 'draft', 'numberposts' => -1, )); $draftsWithContent = 0; $draftsWithoutContent = 0; foreach ($drafts as $draft) { $content = get_post_field('post_content', $draft->ID); if (empty($content)) { // Nếu nội dung rỗng, tăng counter cho draft chưa có nội dung $draftsWithoutContent++; } else { // Ngược lại, tăng counter cho draft đã có nội dung $draftsWithContent++; } } if(count($drafts)==0){ return array('total'=>0,'hascontent'=>0,'nocontent'=>0); }else{ return array('total'=>count($drafts),'hascontent'=>$draftsWithContent,'nocontent'=>$draftsWithoutContent); } } public function add_menu() { add_submenu_page( MENUSUBPARRENT, wp_kses_post($this->icon).' '.esc_html__('Autoblogging AI post','ai-auto-tool'), wp_kses_post($this->icon).' '.esc_html__('Autoblogging AI post','ai-auto-tool'), 'manage_options', 'add-post-draft', array($this, 'add_key_to_post') ); } public function clear_log_action_callback() { update_option('aiautotool_logall', array(),null, 'no'); wp_send_json_success('Log cleared successfully.'); wp_die(); } public function add_key_to_post() { $default_language = get_option('aiautotool_default_language', 'vi'); $languages = $this->languageCodes; echo '
'; ?>


render_select_prompt(); } ?>

'post_category', 'show_option_all' => 'Select a category', 'hide_empty' => 0, 'hierarchical' => 1 )); ?>

'; foreach ($languages as $code => $name) { $is_selected = selected($default_language, $code, false); echo ''; } echo ''; ?>

Link & Text:

'anchor_text', 'textarea_rows' => 5, 'teeny' => false, 'quicktags' => true, 'tinymce' => true, ); wp_editor($anchor_text_value, 'anchor_text_editor', $editor_settings); ?>

icon); ?>

icon); ?>

define('DISABLE_WP_CRON', true);

crontab -e */5 * * * * wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

*/5 * * * *:

roles; if (!in_array('administrator', $user_roles) && !is_super_admin()) { return; } wp_localize_script('jquery', 'aiautotool_js_setting', array( 'ajax_url' => admin_url('admin-ajax.php'), 'security' => wp_create_nonce('aiautotool_nonce'), 'is_admin' => true )); } }