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 = '/', $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('#]+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('#
]+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('#
]+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 '