ID, $custom_name, true); if ($meta == 'pub') { return; } } } aimogen_do_post($post, false, false, false); $editors = get_option('aimogen_Editor_Rules'); if (!empty($editors)) { $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); foreach($editors as $current_editor) { if(is_array($current_editor) && $current_editor[5] == '1') { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Running content editing rule: ' . $current_editor[8]); } aimogen_do_post($post, false, false, $current_editor); } } } } function aimogen_register_my_custom_cron_event() { add_action('aimogen_handle_delayed_post', 'aimogen_process_delayed_post', 10, 1); } function aimogen_schedule_post($post_id, $delay) { $execution_time = time() + $delay; wp_schedule_single_event($execution_time, 'aimogen_handle_delayed_post', [$post_id]); } function aimogen_process_delayed_post($post_id) { $post = get_post($post_id); if ($post === null) { aimogen_log_to_file('Post ID no longer found! ID is: ' . $post_id); return; } if ($post->post_status === 'draft') { $is_draft_added = get_post_meta($post_id, 'aimogen_draft_processed', true); if (!$is_draft_added) { update_post_meta($post_id, 'aimogen_draft_processed', '1'); aimogen_do_post_wrapper($post, true); } } else { delete_post_meta($post_id, 'aimogen_draft_processed'); } } function aimogen_draft_process($post_id, $post, $update) { if (wp_is_post_autosave($post_id)) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { return; } if (wp_is_post_revision($post_id)) { return; } if ($post->post_status === 'draft') { $aimogen_Spinner_Settings = get_option('aimogen_Spinner_Settings', false); if (isset($aimogen_Spinner_Settings['delay_post']) && $aimogen_Spinner_Settings['delay_post'] != '' && is_numeric($aimogen_Spinner_Settings['delay_post'])) { aimogen_schedule_post($post_id, intval($aimogen_Spinner_Settings['delay_post'])); return; } $is_draft_added = get_post_meta($post_id, 'aimogen_draft_processed', true); if (!$is_draft_added) { update_post_meta($post_id, 'aimogen_draft_processed', '1'); aimogen_do_post_wrapper($post, true); } } else { delete_post_meta($post_id, 'aimogen_draft_processed'); } } function aimogen_pending_process($post_id, $post, $update) { if (wp_is_post_autosave($post_id)) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { return; } if (wp_is_post_revision($post_id)) { return; } if ($post->post_status === 'pending') { $aimogen_Spinner_Settings = get_option('aimogen_Spinner_Settings', false); if (isset($aimogen_Spinner_Settings['delay_post']) && $aimogen_Spinner_Settings['delay_post'] != '' && is_numeric($aimogen_Spinner_Settings['delay_post'])) { sleep(intval($aimogen_Spinner_Settings['delay_post'])); $post = get_post($post_id); if($post === null) { aimogen_log_to_file('Post ID no longer found! ID is: ' . $post_id); return; } } $is_draft_added = get_post_meta($post_id, 'aimogen_pending_processed', true); if (!$is_draft_added) { update_post_meta($post_id, 'aimogen_pending_processed', '1'); aimogen_do_post_wrapper($post, true); } } else { delete_post_meta($post_id, 'aimogen_pending_processed'); } } function aimogen_new_post($new_status, $old_status, $post) { if ('publish' !== $new_status or 'publish' === $old_status) { return; } else { if($old_status == 'auto-draft' && $new_status == 'publish' && !has_post_thumbnail($post->ID) && ((function_exists('has_blocks') && has_blocks($post)) || ($post->post_content == '' && function_exists('has_blocks') && !class_exists('Classic_Editor')))) { $delay_it_is_gutenberg = true; } else { $delay_it_is_gutenberg = false; } } $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); { $aimogen_Spinner_Settings = get_option('aimogen_Spinner_Settings', false); if (isset($aimogen_Spinner_Settings['aimogen_spinning']) && $aimogen_Spinner_Settings['aimogen_spinning'] == 'on') { if (isset($aimogen_Spinner_Settings['delay_post']) && $aimogen_Spinner_Settings['delay_post'] != '' && is_numeric($aimogen_Spinner_Settings['delay_post'])) { if(wp_next_scheduled('aimogen_new_post_cron', array($post, true)) === false) { if($delay_it_is_gutenberg && $aimogen_Spinner_Settings['delay_post'] < 2) { $aimogen_Spinner_Settings['delay_post'] = 2; } wp_schedule_single_event(time() + $aimogen_Spinner_Settings['delay_post'], 'aimogen_new_post_cron', array($post, true)); } } else { if (isset($aimogen_Spinner_Settings['run_background']) && $aimogen_Spinner_Settings['run_background'] == 'on') { if($delay_it_is_gutenberg) { if(wp_next_scheduled('aimogen_new_post_cron', array($post, true)) === false) { wp_schedule_single_event(time() + 2, 'aimogen_new_post_cron', array($post, true)); } } else { $unique_id = uniqid(); aimogen_update_option('aimogen_do_post_uniqid', $unique_id); $xcron_url = site_url( '?aimogen_do_post_cronjob=1&post_id=' . $post->ID . '&aimogen_do_post_key=' . $unique_id); wp_remote_post( $xcron_url, array( 'timeout' => 1, 'blocking' => false, 'sslverify' => false ) ); } } else { if($delay_it_is_gutenberg) { if(wp_next_scheduled('aimogen_new_post_cron', array($post, true)) === false) { wp_schedule_single_event( time() + 2, 'aimogen_new_post_cron', array($post, true) ); } } else { aimogen_do_post_wrapper($post, true); } } } } } } function aimogen_process_elementor_elements(&$elements, $elementor_widgets, &$edits_made, $max_tokens, $instructions_token_count, $model, $completionmodels, $exclude_count_before, $ai_instruction, $token, $assistant_id, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, &$finish_reason, &$error, $vision_file, $thread_id, $store_data, $post, $allowed_classes = []) { foreach ($elements as &$element) { if ($element['elType'] === 'container' || $element['elType'] === 'widget') { if ($element['elType'] === 'widget' && in_array($element['widgetType'], $elementor_widgets)) { if (!empty($allowed_classes)) { $css_classes = ''; if (!empty($element['settings']['_css_classes'])) { $css_classes = $element['settings']['_css_classes']; } elseif (!empty($element['settings']['css_classes'])) { $css_classes = $element['settings']['css_classes']; } if (empty($css_classes)) { continue; } $class_array = preg_split('/\s+/', $css_classes); if (count(array_intersect($allowed_classes, $class_array)) === 0) { continue; } } aimogen_process_elementor_widget($element, $elementor_widgets, $edits_made, $max_tokens, $instructions_token_count, $model, $completionmodels, $exclude_count_before, $ai_instruction, $token, $assistant_id, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, $finish_reason, $error, $vision_file, $thread_id, $store_data, $post); } if (!empty($element['elements'])) { aimogen_process_elementor_elements($element['elements'], $elementor_widgets, $edits_made, $max_tokens, $instructions_token_count, $model, $completionmodels, $exclude_count_before, $ai_instruction, $token, $assistant_id, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, $finish_reason, $error, $vision_file, $thread_id, $store_data, $post, $allowed_classes); } } } } function aimogen_process_elementor_widget(&$element, $allowed_widgets, &$edits_made, $max_tokens, $instructions_token_count, $model, $completionmodels, $exclude_count_before, $ai_instruction, $token, $assistant_id, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, $finish_reason, $error, $vision_file, $thread_id, $store_data, $post) { if (!isset($element['elType']) || $element['elType'] !== 'widget') { if (isset($element['elements']) && is_array($element['elements'])) { foreach ($element['elements'] as &$sub_element) { aimogen_process_elementor_widget($sub_element, $allowed_widgets, $edits_made, $max_tokens, $instructions_token_count, $model, $completionmodels, $exclude_count_before, $ai_instruction, $token, $assistant_id, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, $finish_reason, $error, $vision_file, $thread_id, $store_data, $post); } } return; } if (!isset($element['widgetType']) || !in_array($element['widgetType'], $allowed_widgets)) { return; } if (isset($element['settings']['editor']) && !empty($element['settings']['editor'])) { $content_to_edit = $element['settings']['editor']; if ($content_to_edit != '') { $final_gut_content_ai = ''; $tokens = aimogen_encode($content_to_edit); $content_token_count = count($tokens); $available_tokens = $max_tokens - ($instructions_token_count + $content_token_count + 2); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { if (isset($aimogen_Spinner_Settings['max_char_chunks']) && $aimogen_Spinner_Settings['max_char_chunks'] != '' && intval($aimogen_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Splitting text into chunks of ' . $aimogen_Spinner_Settings['max_char_chunks'] . ' characters.'); } $chunk_split = str_split($content_to_edit, intval($aimogen_Spinner_Settings['max_char_chunks'])); } else { $chunk_split = aimogen_split_to_token_len($tokens, intval($max_tokens / 2)); } } else { if (isset($aimogen_Spinner_Settings['max_char_chunks']) && $aimogen_Spinner_Settings['max_char_chunks'] != '' && intval($aimogen_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens && (!isset($aimogen_Spinner_Settings['no_add_content']) || $aimogen_Spinner_Settings['no_add_content'] != 'on')) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Splitting text into chunks of ' . $aimogen_Spinner_Settings['max_char_chunks'] . ' characters.'); } $chunk_split = str_split($content_to_edit, intval($aimogen_Spinner_Settings['max_char_chunks'])); } else { $chunk_split = array($content_to_edit); } } if (isset($aimogen_Spinner_Settings['no_add_content']) && $aimogen_Spinner_Settings['no_add_content'] == 'on') { $chunk_split = array($content_to_edit); } foreach($chunk_split as $my_little_chunk) { if(!in_array($model, $completionmodels)) { $exclude_count_before += aimogen_countExcludes($my_little_chunk); } if(in_array($model, $completionmodels)) { if (!isset($aimogen_Spinner_Settings['no_add_content']) || $aimogen_Spinner_Settings['no_add_content'] != 'on') { $prompt = $ai_instruction . ':\r\n\r\n ' . $my_little_chunk . ' \r\n\r\n'; } else { $prompt = $ai_instruction; } $error = ''; $finish_reason = ''; $max_tokens = aimogen_get_max_tokens($model); $query_token_count = count(aimogen_encode($prompt)); $available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($prompt); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $prompt = aimogen_substr($prompt, 0, $string_len); $prompt = trim($prompt); if(empty($prompt)) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Empty API seed expression provided (after processing)'); } } else { $query_token_count = count(aimogen_encode($prompt)); $available_tokens = $max_tokens - $query_token_count; } } if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { $api_service = aimogen_get_api_service($token, $model); aimogen_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $model . ')(' . $available_tokens . ') Content Editor with seed command: ' . $prompt); } $response_text = aimogen_generate_text($token, $model, $prompt, $available_tokens, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, false, 'contentCEditor', 0, $finish_reason, $error, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), ''); if($response_text === false) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit the post chunk using AI: ' . $error . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk); } $final_gut_content_ai .= $my_little_chunk; } else { $response_text = trim($response_text); $final_gut_content_ai .= $response_text; } } else { $aierror = ''; $edited_content = aimogen_edit_text($token, $model, $ai_instruction, $my_little_chunk, $edit_temperature, $edit_top_p, 'contentEditor', 0, $aierror); if($edited_content !== false) { $final_gut_content_ai .= $edited_content; } else { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit post chunk using AI: ' . $aierror . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk); } $final_gut_content_ai .= $my_little_chunk; } } } if(!empty($final_gut_content_ai)) { $processed_content = $final_gut_content_ai; if ($processed_content !== $content_to_edit) { if(function_exists('mb_convert_encoding')) { $processed_content = mb_convert_encoding($processed_content, 'UTF-8', 'auto'); } $element['settings']['editor'] = $processed_content; $edits_made = true; } } } } } add_action('init', 'aimogen_do_post_callback', 0); function aimogen_do_post_callback() { // Cron callback cannot use nonces — they expire. This is used in cron jobs, so they need to be long lived. // Request is authenticated via a long-lived secret key stored in the DB. aimogen_do_post_uniqid // Isolate raw GET access (required by PHPCS) $raw_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $secretp_key = get_option('aimogen_do_post_uniqid', false); if (isset($raw_get['aimogen_do_post_cronjob']) && $raw_get['aimogen_do_post_cronjob'] == '1' && isset($raw_get['post_id']) && is_numeric($raw_get['post_id']) && $raw_get['aimogen_do_post_key'] === $secretp_key) { $post = get_post(sanitize_text_field( wp_unslash($raw_get['post_id']))); if($post !== null) { aimogen_do_post_wrapper($post, false); exit(); } } } function aimogen_do_post($post, $manual = false, $template = false, $editor_rule = false) { if(!function_exists('is_plugin_active')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if (is_plugin_active('aimogen-pro/aimogen-pro.php')) { return; } $plugin = plugin_basename(__FILE__); $plugin_slug = explode('/', $plugin); $plugin_slug = $plugin_slug[0]; $post_link = ''; $post_title = ''; $blog_title = ''; $final_content = ''; $user_name = ''; $featured_image = ''; $post_cats = ''; $post_tagz = ''; $thread_id = ''; $aimogen_Main_Settings = get_option('aimogen_Main_Settings', false); { require_once(dirname(__FILE__) . "/res/aimogen-chars.php"); wp_cache_delete('aimogen_Spinner_Settings', 'options'); $aimogen_Spinner_Settings = get_option('aimogen_Spinner_Settings', false); if(isset($aimogen_Spinner_Settings['disabled_categories']) && is_array($aimogen_Spinner_Settings['disabled_categories']) && in_array('aimogen_no_category_12345678', $aimogen_Spinner_Settings['disabled_categories'])) { if (($key = array_search('aimogen_no_category_12345678', $aimogen_Spinner_Settings['disabled_categories'])) !== false) { unset($aimogen_Spinner_Settings['disabled_categories'][$key]); } } if(isset($aimogen_Spinner_Settings['store_data']) && $aimogen_Spinner_Settings['store_data'] == 'on') { $store_data = 'on'; } else { $store_data = 'off'; } $pid = $post->ID; $post = get_post($post->ID); if($post === null) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Post ID no longer found: ' . $pid); } return; } if(in_array($post->post_type, AIMOGEN_EXCEPTED_POST_TYPES_FROM_EDITING)) { return; } if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_exec_time('Edit Posts'); } if ($manual || isset($aimogen_Spinner_Settings['aimogen_spinning']) && $aimogen_Spinner_Settings['aimogen_spinning'] == 'on') { if (!isset($aimogen_Main_Settings['app_id']) || trim($aimogen_Main_Settings['app_id']) == '') { aimogen_log_to_file('You need to insert a valid OpenAI/AimogenAPI API Key for this to work!'); return; } $vision_file = ''; $appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id'])); $appids = array_filter($appids); $token = $appids[array_rand($appids)]; if (!$manual && isset($aimogen_Spinner_Settings['post_posts'])) { if ($aimogen_Spinner_Settings['post_posts'] == 'on' && 'post' === $post->post_type) { return; } } if (!$manual && isset($aimogen_Spinner_Settings['post_pages'])) { if ($aimogen_Spinner_Settings['post_pages'] == 'on' && 'page' === $post->post_type) { return; } } if (!$manual && isset($aimogen_Spinner_Settings['post_custom'])) { if ($aimogen_Spinner_Settings['post_custom'] == 'on' && 'page' !== $post->post_type && 'post' !== $post->post_type) { if (isset($aimogen_Spinner_Settings['except_type']) && $aimogen_Spinner_Settings['except_type'] != '') { $excepted_types = explode(',', $aimogen_Spinner_Settings['except_type']); $excepted_types = array_map('trim', $excepted_types); if(!in_array($post->post_type, $excepted_types)) { return; } } else { return; } } } if (!$manual && (!isset($aimogen_Spinner_Settings['post_custom']) || $aimogen_Spinner_Settings['post_custom'] != 'on')) { if (isset($aimogen_Spinner_Settings['only_type']) && $aimogen_Spinner_Settings['only_type'] != '') { $only_types = explode(',', $aimogen_Spinner_Settings['only_type']); $only_types = array_map('trim', $only_types); if(!in_array($post->post_type, $only_types)) { return; } } } if (isset($aimogen_Spinner_Settings['custom_name']) && trim($aimogen_Spinner_Settings['custom_name']) != '') { $custom_name = trim($aimogen_Spinner_Settings['custom_name']); $custom_name = str_replace('%%current_date%%', gmdate("Y-m-d"), $custom_name); } else { $custom_name = 'aimogen_published'; } $meta = get_post_meta($post->ID, $custom_name, true); if (!$manual && $meta == 'pub' && $editor_rule === false) { return; } $meta = get_post_meta($post->ID, "aimogen_auto_post_spinned", true); if ($meta === '1') { return; } if (isset($aimogen_Spinner_Settings['model']) && $aimogen_Spinner_Settings['model'] != '') { $completion_model = $aimogen_Spinner_Settings['model']; } else { $completion_model = aimogen_get_default_model_name($aimogen_Main_Settings); } if (isset($aimogen_Spinner_Settings['append_assistant_id']) && $aimogen_Spinner_Settings['append_assistant_id'] != '') { $assistant_id = $aimogen_Spinner_Settings['append_assistant_id']; } else { $assistant_id = ''; } if (isset($aimogen_Spinner_Settings['max_tokens']) && $aimogen_Spinner_Settings['max_tokens'] != '') { $max_tokens_completion = intval($aimogen_Spinner_Settings['max_tokens']); } else { $max_tokens_completion = aimogen_get_max_tokens($completion_model); } if($max_tokens_completion <= 0) { $max_tokens_completion = AIMOGEN_DEFAULT_MAX_TOKENS; } if($max_tokens_completion > aimogen_get_max_tokens($completion_model) && aimogen_get_max_tokens($completion_model) > 0) { $max_tokens_completion = aimogen_get_max_tokens($completion_model); } $max_tokens = $max_tokens_completion; $updated = false; if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Starting editing post ID: ' . $pid); } $post_title = $post->post_title; $final_content = $post->post_content; $changes_made = false; if (isset($aimogen_Spinner_Settings['ai_rewriter']) && $aimogen_Spinner_Settings['ai_rewriter'] != '' && $aimogen_Spinner_Settings['ai_rewriter'] != 'disabled') { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Starting post rewriter...'); } $vision_file = ''; $vision_file_title = ''; if (isset($aimogen_Spinner_Settings['edit_temperature']) && $aimogen_Spinner_Settings['edit_temperature'] != '') { $edit_temperature = floatval($aimogen_Spinner_Settings['edit_temperature']); } else { $edit_temperature = 0; } if (isset($aimogen_Spinner_Settings['edit_model']) && $aimogen_Spinner_Settings['edit_model'] != '') { $model = trim($aimogen_Spinner_Settings['edit_model']); } else { $model = aimogen_get_default_model_name($aimogen_Main_Settings); } if (isset($aimogen_Spinner_Settings['title_model']) && $aimogen_Spinner_Settings['title_model'] != '') { $title_model = trim($aimogen_Spinner_Settings['title_model']); } else { $title_model = $model; } if (isset($aimogen_Spinner_Settings['ai_vision']) && $aimogen_Spinner_Settings['ai_vision'] == 'on') { $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail'); if($avatar !== false) { $vision_file = $avatar; } } if (isset($aimogen_Spinner_Settings['ai_title_vision']) && $aimogen_Spinner_Settings['ai_title_vision'] == 'on') { $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail'); if($avatar !== false) { $vision_file_title = $avatar; } } if (isset($aimogen_Spinner_Settings['ai_excerpt_vision']) && $aimogen_Spinner_Settings['ai_excerpt_vision'] == 'on') { $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail'); } if (isset($aimogen_Spinner_Settings['ai_slug_vision']) && $aimogen_Spinner_Settings['ai_slug_vision'] == 'on') { $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail'); } if (isset($aimogen_Spinner_Settings['edit_assistant_id']) && $aimogen_Spinner_Settings['edit_assistant_id'] != '') { $assistant_id = trim($aimogen_Spinner_Settings['edit_assistant_id']); } else { $assistant_id = ''; } if (isset($aimogen_Spinner_Settings['edit_top_p']) && $aimogen_Spinner_Settings['edit_top_p'] != '') { $edit_top_p = floatval($aimogen_Spinner_Settings['edit_top_p']); } else { $edit_top_p = 1; } if (isset($aimogen_Spinner_Settings['edit_presence_penalty']) && $aimogen_Spinner_Settings['edit_presence_penalty'] != '') { $edit_presence_penalty = floatval($aimogen_Spinner_Settings['edit_presence_penalty']); } else { $edit_presence_penalty = 0; } if (isset($aimogen_Spinner_Settings['edit_frequency_penalty']) && $aimogen_Spinner_Settings['edit_frequency_penalty'] != '') { $edit_frequency_penalty = floatval($aimogen_Spinner_Settings['edit_frequency_penalty']); } else { $edit_frequency_penalty = 1; } $completionmodels = aimogen_get_all_models(true); if ((isset($aimogen_Spinner_Settings['ai_instruction']) && $aimogen_Spinner_Settings['ai_instruction'] != '') || (isset($aimogen_Spinner_Settings['ai_instruction_title']) && $aimogen_Spinner_Settings['ai_instruction_title'] != '')) { $ai_instruction = trim($aimogen_Spinner_Settings['ai_instruction']); $ai_instruction = aimogen_replaceSynergyShortcodes($ai_instruction); $ai_instruction_title = trim($aimogen_Spinner_Settings['ai_instruction_title']); $ai_instruction_title = aimogen_replaceSynergyShortcodes($ai_instruction_title); $post_link = get_permalink($post->ID); $blog_title = html_entity_decode(get_bloginfo('title')); $author_obj = get_user_by('id', $post->post_author); if($author_obj !== false && isset($author_obj->user_nicename)) { $user_name = $author_obj->user_nicename; } else { $user_name = ''; } $featured_image = ''; wp_suspend_cache_addition(true); $metas = get_post_custom($post->ID); wp_suspend_cache_addition(false); if(is_array($metas)) { $rez_meta = aimogen_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas); } else { $rez_meta = array(); } if(count($rez_meta) > 0) { foreach($rez_meta as $rm) { if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL)) { $featured_image = $rm[0]; break; } } } if($featured_image == '') { $featured_image = aimogen_generate_thumbmail($post->ID); } if($featured_image == '' && $final_content != '') { $dom = new DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $dom->loadHTML($final_content); libxml_use_internal_errors($internalErrors); $tags = $dom->getElementsByTagName('img'); foreach ($tags as $tag) { $temp_get_img = $tag->getAttribute('src'); if ($temp_get_img != '') { $temp_get_img = strtok($temp_get_img, '?'); $featured_image = rtrim($temp_get_img, '/'); } } } $post_cats = ''; $post_categories = wp_get_post_categories( $post->ID ); foreach($post_categories as $c){ $cat = get_category( $c ); $post_cats .= $cat->name . ','; } $post_cats = trim($post_cats, ','); if($post_cats != '') { $post_categories = explode(',', $post_cats); } else { $post_categories = array(); } if(count($post_categories) == 0) { $terms = get_the_terms( $post->ID, 'product_cat' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { $post_categories[] = $term->slug; } $post_cats = implode(',', $post_categories); } } foreach($post_categories as $pc) { if (!$manual && isset($aimogen_Spinner_Settings['disabled_categories']) && !empty($aimogen_Spinner_Settings['disabled_categories'])) { foreach($aimogen_Spinner_Settings['disabled_categories'] as $disabled_cat) { if($manual != true && trim($pc) == get_cat_name($disabled_cat)) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title); } return; } } } } $post_tagz = ''; $post_tags = wp_get_post_tags( $post->ID ); foreach($post_tags as $t){ $post_tagz .= $t->name . ','; } $post_tagz = trim($post_tagz, ','); if($post_tagz != '') { $post_tags = explode(',', $post_tagz); } else { $post_tags = array(); } if(count($post_tags) == 0) { $terms = get_the_terms( $post->ID, 'product_tag' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { $post_tags[] = $term->slug; } $post_tagz = implode(',', $post_tags); } } if (!$manual && isset($aimogen_Spinner_Settings['disable_users']) && $aimogen_Spinner_Settings['disable_users'] != '') { $disable_users = explode(",", $aimogen_Spinner_Settings['disable_users']); foreach($disable_users as $disable_user) { if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user))) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author); } return; } } } if (!$manual && isset($aimogen_Spinner_Settings['enable_users']) && $aimogen_Spinner_Settings['enable_users'] != '') { $enable_users = explode(",", $aimogen_Spinner_Settings['enable_users']); $found = false; foreach($enable_users as $disable_user) { if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user))) { $found = true; } } if($found === false) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author); } return; } } if (!$manual && isset($aimogen_Spinner_Settings['featured_status']) && $aimogen_Spinner_Settings['featured_status'] != '' && $aimogen_Spinner_Settings['featured_status'] != 'any') { if ($aimogen_Spinner_Settings['featured_status'] == 'yes') { if ( !has_post_thumbnail($post->ID) ) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, does not have a featured image: ' . $post->ID); } return; } } elseif ($aimogen_Spinner_Settings['featured_status'] == 'no') { if ( has_post_thumbnail($post->ID) ) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, already has a featured image: ' . $post->ID); } return; } } } if (!$manual && isset($aimogen_Spinner_Settings['seo_status']) && $aimogen_Spinner_Settings['seo_status'] != '' && $aimogen_Spinner_Settings['seo_status'] != 'any') { if ($aimogen_Spinner_Settings['seo_status'] == 'yes') { if ( !aimogen_check_seo_description($post->ID) ) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID); } return; } } elseif ($aimogen_Spinner_Settings['seo_status'] == 'no') { if ( aimogen_check_seo_description($post->ID) ) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID); } return; } } } if (!$manual && isset($aimogen_Spinner_Settings['enable_field']) && trim($aimogen_Spinner_Settings['enable_field']) != '') { $skip_me = true; $enable_field = preg_split('/\r\n|\r|\n/', trim($aimogen_Spinner_Settings['enable_field'])); foreach($enable_field as $fieldx) { if(strstr($fieldx, '=>') !== false) { $fna = explode('=>', $fieldx); if(isset($fna[1])) { $custom_val = get_post_meta($post->ID, trim($fna[0]), true); if ($custom_val) { if(trim($fna[1]) == '*') { $skip_me = false; } if($custom_val == trim($fna[1])) { $skip_me = false; } } } } } if($skip_me === true) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID); } return; } } if (!$manual && isset($aimogen_Spinner_Settings['disable_field']) && trim($aimogen_Spinner_Settings['disable_field']) != '') { $skip_me = false; $disable_field = preg_split('/\r\n|\r|\n/', trim($aimogen_Spinner_Settings['disable_field'])); foreach($disable_field as $fieldx) { if(strstr($fieldx, '=>') !== false) { $fna = explode('=>', $fieldx); if(isset($fna[1])) { $custom_val = get_post_meta($post->ID, trim($fna[0]), true); if ($custom_val) { if(trim($fna[1]) == '*') { $skip_me = true; } if($custom_val == trim($fna[1])) { $skip_me = true; } } } } } if($skip_me === true) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID); } return; } } foreach($post_tags as $pt) { if (!$manual && isset($aimogen_Spinner_Settings['disable_tags']) && $aimogen_Spinner_Settings['disable_tags'] != '') { $disable_tags = explode(",", $aimogen_Spinner_Settings['disable_tags']); foreach($disable_tags as $disabled_tag) { if($manual != true && trim($pt) == trim($disabled_tag)) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title); } return; } } } } if (!$manual && isset($aimogen_Spinner_Settings['enable_tags']) && $aimogen_Spinner_Settings['enable_tags'] != '') { $skip = true; $enable_tags = explode(",", $aimogen_Spinner_Settings['enable_tags']); foreach($post_tags as $pt) { foreach($enable_tags as $enable_tag) { if(trim($pt) == trim($enable_tag)) { $skip = false; break; } } } if($skip == true) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Skipping post, does not have required tag: ' . $post->post_title); } return; } } if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Now editing post ID: ' . $post->ID); } $ai_instruction = aimogen_replaceAIPostShortcodes($ai_instruction, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', ''); $ai_instruction = trim($ai_instruction); $ai_instruction_title = aimogen_replaceAIPostShortcodes($ai_instruction_title, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', ''); $ai_instruction_title = trim($ai_instruction_title); if(isset($aimogen_Spinner_Settings['protect_html']) && $aimogen_Spinner_Settings['protect_html'] == 'on') { if(!in_array($model, $completionmodels)) { if(!empty($ai_instruction)) { $ai_instruction .= ", numbers in brackets are protected terms, keep them unchanged in the returned text."; } } else { $ai_instruction .= ", don't edit HTML tags, only text."; } } if (!$manual && isset($aimogen_Spinner_Settings['max_char']) && $aimogen_Spinner_Settings['max_char'] != '') { if(strlen($ai_instruction_title) > $aimogen_Spinner_Settings['max_char']) { aimogen_log_to_file('Skipping post, title too long, max is: ' . $aimogen_Spinner_Settings['max_char']); } if(strlen($ai_instruction) > $aimogen_Spinner_Settings['max_char']) { aimogen_log_to_file('Skipping post, content too long, max is: ' . $aimogen_Spinner_Settings['max_char']); } } $instructions_token_count_title = count(aimogen_encode($ai_instruction_title)); $instructions_token_count = count(aimogen_encode($ai_instruction)); $title_token_count = count(aimogen_encode($post_title)); $max_tokens = aimogen_get_max_tokens($model); $max_title_tokens = aimogen_get_max_tokens($title_model); $available_title_tokens = $max_title_tokens - ($instructions_token_count_title + $title_token_count); $title_ai_edited = ''; $excerpt_ai_edited = ''; if ((!isset($aimogen_Spinner_Settings['no_title']) || $aimogen_Spinner_Settings['no_title'] != 'on') && !empty($ai_instruction_title)) { if($available_title_tokens < 0) { aimogen_log_to_file('Skipping editing title, it is too long: ' . $post->post_title); } else { if(in_array($title_model, $completionmodels)) { if (!isset($aimogen_Spinner_Settings['no_add_title']) || $aimogen_Spinner_Settings['no_add_title'] != 'on') { $prompt = $ai_instruction_title . ': ' . $post_title; } else { $prompt = $ai_instruction_title; } $error = ''; $finish_reason = ''; $max_tokens = aimogen_get_max_tokens($title_model); $query_token_count = count(aimogen_encode($prompt)); $available_tokens = aimogen_compute_available_tokens($title_model, $max_tokens, $prompt, $query_token_count); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($prompt); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $prompt = aimogen_substr($prompt, 0, $string_len); $prompt = trim($prompt); if(empty($prompt)) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Empty API seed expression provided (after processing)'); } } else { $query_token_count = count(aimogen_encode($prompt)); $available_tokens = $max_tokens - $query_token_count; } } if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { $api_service = aimogen_get_api_service($token, $title_model); aimogen_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $title_model . ')(' . $available_tokens . ') Title Editor with seed command: ' . $prompt); } $response_text = aimogen_generate_text($token, $title_model, $prompt, $available_tokens, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, false, 'titleCEditor', 0, $finish_reason, $error, true, false, false, $vision_file_title, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), ''); if($response_text === false) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit post title using AI: ' . $error); } } else { $response_text = ucfirst(trim(trim(trim(trim($response_text), '.'), ' "\''))); $title_ai_edited = $response_text; $post_title = $response_text; } } else { $aierror = ''; $edited_content = aimogen_edit_text($token, $title_model, $ai_instruction_title, $post_title, $edit_temperature, $edit_top_p, 'titleEditor', 0, $aierror); if($edited_content !== false) { $edited_content = ucfirst(trim(trim(trim(trim($edited_content), '.'), ' "\''))); $title_ai_edited = $edited_content; $post_title = $edited_content; } else { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit post title using AI: ' . $aierror); } } } } } $edited_slug = ''; $one_success = false; $final_content_ai = ''; if ((!isset($aimogen_Spinner_Settings['no_content']) || $aimogen_Spinner_Settings['no_content'] != 'on') && !empty($ai_instruction)) { $final_content_pre = $final_content; $tokens = aimogen_encode($final_content_pre); $content_token_count = count($tokens); $available_tokens = $max_tokens - ($instructions_token_count + $content_token_count + 2); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { if (isset($aimogen_Spinner_Settings['max_char_chunks']) && $aimogen_Spinner_Settings['max_char_chunks'] != '' && intval($aimogen_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Splitting text into chunks of ' . $aimogen_Spinner_Settings['max_char_chunks'] . ' characters.'); } $chunk_split = str_split($final_content_pre, intval($aimogen_Spinner_Settings['max_char_chunks'])); } else { $chunk_split = aimogen_split_to_token_len($tokens, intval($max_tokens / 2)); } } else { if (isset($aimogen_Spinner_Settings['max_char_chunks']) && $aimogen_Spinner_Settings['max_char_chunks'] != '' && intval($aimogen_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Splitting text into chunks of ' . $aimogen_Spinner_Settings['max_char_chunks'] . ' characters.'); } $chunk_split = str_split($final_content_pre, intval($aimogen_Spinner_Settings['max_char_chunks'])); } else { $chunk_split = array($final_content_pre); } } if (isset($aimogen_Spinner_Settings['no_add_content']) && $aimogen_Spinner_Settings['no_add_content'] == 'on') { $chunk_split = array($final_content_pre); } foreach($chunk_split as $my_little_chunk) { if(!in_array($model, $completionmodels)) { $exclude_count_before += aimogen_countExcludes($my_little_chunk); } if(in_array($model, $completionmodels)) { if (!isset($aimogen_Spinner_Settings['no_add_content']) || $aimogen_Spinner_Settings['no_add_content'] != 'on') { $prompt = $ai_instruction . ':\r\n\r\n ' . $my_little_chunk . ' \r\n\r\n'; } else { $prompt = $ai_instruction; } $error = ''; $finish_reason = ''; $max_tokens = aimogen_get_max_tokens($model); $query_token_count = count(aimogen_encode($prompt)); $available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count); if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) { $string_len = strlen($prompt); $string_len = $string_len / 2; $string_len = intval(0 - $string_len); $prompt = aimogen_substr($prompt, 0, $string_len); $prompt = trim($prompt); if(empty($prompt)) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Empty API seed expression provided (after processing)'); } } else { $query_token_count = count(aimogen_encode($prompt)); $available_tokens = $max_tokens - $query_token_count; } } if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { $api_service = aimogen_get_api_service($token, $model); aimogen_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $model . ')(' . $available_tokens . ') Content Editor with seed command prompt: ' . $prompt); } $response_text = aimogen_generate_text($token, $model, $prompt, $available_tokens, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, false, 'contentCEditor', 0, $finish_reason, $error, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), ''); if($response_text === false) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit the post chunk using AI: ' . $error . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk); } $final_content_ai .= $my_little_chunk; } else { $response_text = trim($response_text); $final_content_ai .= $response_text; $one_success = true; } } else { $aierror = ''; $edited_content = aimogen_edit_text($token, $model, $ai_instruction, $my_little_chunk, $edit_temperature, $edit_top_p, 'contentEditor', 0, $aierror); if($edited_content !== false) { $final_content_ai .= $edited_content; $one_success = true; } else { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to edit post chunk using AI: ' . $aierror . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk); } $final_content_ai .= $my_little_chunk; } } } } $exclude_count_before = 0; $elementor_updated = false; if($one_success === false) { $final_content_ai = ''; } if($final_content_ai != '') { if(!in_array($model, $completionmodels)) { $exclude_count_after = aimogen_countExcludes($final_content_ai); } else { $exclude_count_after = 0; } if((!isset($aimogen_Spinner_Settings['no_html_check']) || $aimogen_Spinner_Settings['no_html_check'] != 'on') && $exclude_count_before != $exclude_count_after) { aimogen_log_to_file('Post edit failed, as HTML tags were removed by the AI editor. Because of this, edits are not saved. Count of HTML tags missing: ' . ($exclude_count_before - $exclude_count_after)); } else { $final_content = $final_content_ai; $args = array(); $args['ID'] = $post->ID; if(!isset($aimogen_xchars)) { $aimogen_xchars = array(); } if (!isset($aimogen_Main_Settings['no_undetectibility']) || $aimogen_Main_Settings['no_undetectibility'] != 'on' && !aimogen_stringContainsArrayChars($final_content, $aimogen_xchars)) { if (!isset($aimogen_Main_Settings['no_undetectibility_editor']) || $aimogen_Main_Settings['no_undetectibility_editor'] != 'on') { $rand_percentage = wp_rand(10, 20); $final_content = aimogen_make_unique($final_content, $aimogen_xchars, $rand_percentage); } } $args['post_content'] = $final_content; if($title_ai_edited != '') { if ((isset($aimogen_Spinner_Settings['rewrite_url']) && $aimogen_Spinner_Settings['rewrite_url'] == 'on')) { if(!empty(sanitize_title($title_ai_edited))) { $args['post_name'] = sanitize_title($title_ai_edited); } } $args['post_title'] = aimogen_truncate_title($title_ai_edited); } if(!empty($edited_slug)) { $args['post_name'] = trim($edited_slug); } if($excerpt_ai_edited != '') { $args['post_excerpt'] = trim($excerpt_ai_edited); } if (isset($aimogen_Spinner_Settings['change_status']) && $aimogen_Spinner_Settings['change_status'] != '' && $aimogen_Spinner_Settings['change_status'] != 'no') { $args['post_status'] = $aimogen_Spinner_Settings['change_status']; } if (isset($aimogen_Spinner_Settings['change_date']) && trim($aimogen_Spinner_Settings['change_date']) != '') { $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aimogen_Spinner_Settings['change_date']))); if($date_stamp) { $postdate = gmdate("Y-m-d H:i:s", $date_stamp); $args['post_date'] = $postdate; } } update_post_meta($post->ID, $custom_name, "pub"); remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); remove_filter('title_save_pre', 'wp_filter_kses'); $post_updated = wp_update_post($args); $changes_made = true; add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); add_filter('title_save_pre', 'wp_filter_kses'); if (is_wp_error($post_updated)) { $errors = $post_updated->get_error_messages(); foreach ($errors as $error) { aimogen_log_to_file('Error occured while updating post for content rewriting "' . $post->post_title . '": ' . $error); } } else { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI rewritten content.'); } } } } else { if($title_ai_edited != '') { $args = array(); $args['ID'] = $post->ID; if ((isset($aimogen_Spinner_Settings['rewrite_url']) && $aimogen_Spinner_Settings['rewrite_url'] == 'on')) { if(!empty(sanitize_title($title_ai_edited))) { $args['post_name'] = sanitize_title($title_ai_edited); } } $args['post_title'] = aimogen_truncate_title($title_ai_edited); if (isset($aimogen_Spinner_Settings['change_status']) && $aimogen_Spinner_Settings['change_status'] != '' && $aimogen_Spinner_Settings['change_status'] != 'no') { $args['post_status'] = $aimogen_Spinner_Settings['change_status']; } if (isset($aimogen_Spinner_Settings['change_date']) && trim($aimogen_Spinner_Settings['change_date']) != '') { $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aimogen_Spinner_Settings['change_date']))); if($date_stamp) { $postdate = gmdate("Y-m-d H:i:s", $date_stamp); $args['post_date'] = $postdate; } } if($excerpt_ai_edited != '') { $args['post_excerpt'] = trim($excerpt_ai_edited); } if(!empty($edited_slug)) { $args['post_name'] = trim($edited_slug); } remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); remove_filter('title_save_pre', 'wp_filter_kses'); update_post_meta($post->ID, $custom_name, "pub"); $post_updated = wp_update_post($args); $changes_made = true; add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); add_filter('title_save_pre', 'wp_filter_kses'); if (is_wp_error($post_updated)) { $errors = $post_updated->get_error_messages(); foreach ($errors as $error) { aimogen_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error); } } else { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated title.'); } } } else { if(!$elementor_updated) { if (isset($aimogen_Main_Settings['enable_detailed_logging'])) { aimogen_log_to_file('Post ID ' . $post->ID . ' failed to be editted, nothing returned from AI editor'); } } } } } } if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Finished editing post ID: ' . $post->ID); } if (isset($aimogen_Spinner_Settings['publish_new']) && $aimogen_Spinner_Settings['publish_new'] === 'yes') { if($changes_made === false) { if (isset($aimogen_Main_Settings['enable_detailed_logging']) && $aimogen_Main_Settings['enable_detailed_logging'] == 'on') { aimogen_log_to_file('Deleting duplicated post, as no changes were made: ' . $post->ID); } wp_delete_post($post->ID); } } } } } ?>