Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/aiomatic-do-post.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + defined('ABSPATH') or die();
3 + use AiomaticOpenAI\OpenAi\OpenAi;
4 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
5 + if (isset($aiomatic_Spinner_Settings['process_event']) && $aiomatic_Spinner_Settings['process_event'] === 'draft')
6 + {
7 + add_action('save_post', 'aiomatic_draft_process', 10, 3);
8 + }
9 + elseif (isset($aiomatic_Spinner_Settings['process_event']) && $aiomatic_Spinner_Settings['process_event'] === 'pending')
10 + {
11 + add_action('save_post', 'aiomatic_pending_process', 10, 3);
12 + }
13 + else
14 + {
15 + add_action('aiomatic_new_post_cron', 'aiomatic_do_post_wrapper', 10, 2);
16 + add_action('transition_post_status', 'aiomatic_new_post', 10, 3);
17 + }
18 + add_action('init', 'aiomatic_register_my_custom_cron_event');
19 +
20 + function aiomatic_do_post_wrapper($post, $skip_if_edited = false)
21 + {
22 + if($skip_if_edited == true)
23 + {
24 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
25 + if (isset($aiomatic_Spinner_Settings['no_twice']) && $aiomatic_Spinner_Settings['no_twice'] == 'on')
26 + {
27 + if (isset($aiomatic_Spinner_Settings['custom_name']) && trim($aiomatic_Spinner_Settings['custom_name']) != '')
28 + {
29 + $custom_name = trim($aiomatic_Spinner_Settings['custom_name']);
30 + $custom_name = str_replace('%%current_date%%', date("Y-m-d"), $custom_name);
31 + }
32 + else
33 + {
34 + $custom_name = 'aiomatic_published';
35 + }
36 + $meta = get_post_meta($post->ID, $custom_name, true);
37 + if ($meta == 'pub')
38 + {
39 + return;
40 + }
41 + }
42 + }
43 +
44 + aiomatic_do_post($post, false, false, false);
45 + $editors = get_option('aiomatic_Editor_Rules');
46 + if (!empty($editors))
47 + {
48 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
49 + foreach($editors as $current_editor)
50 + {
51 + if(is_array($current_editor) && $current_editor[5] == '1')
52 + {
53 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
54 + {
55 + aiomatic_log_to_file('Running content editing rule: ' . $current_editor[8]);
56 + }
57 + aiomatic_do_post($post, false, false, $current_editor);
58 + }
59 + }
60 + }
61 + }
62 + function aiomatic_register_my_custom_cron_event()
63 + {
64 + add_action('aiomatic_handle_delayed_post', 'aiomatic_process_delayed_post', 10, 1);
65 + }
66 + function aiomatic_schedule_post($post_id, $delay)
67 + {
68 + $execution_time = time() + $delay;
69 + wp_schedule_single_event($execution_time, 'aiomatic_handle_delayed_post', [$post_id]);
70 + }
71 + function aiomatic_process_delayed_post($post_id)
72 + {
73 + $post = get_post($post_id);
74 + if ($post === null)
75 + {
76 + aiomatic_log_to_file('Post ID no longer found! ID is: ' . $post_id);
77 + return;
78 + }
79 + if ($post->post_status === 'draft')
80 + {
81 + $is_draft_added = get_post_meta($post_id, 'aiomatic_draft_processed', true);
82 + if (!$is_draft_added)
83 + {
84 + update_post_meta($post_id, 'aiomatic_draft_processed', '1');
85 + aiomatic_do_post_wrapper($post, true);
86 + }
87 + }
88 + else
89 + {
90 + delete_post_meta($post_id, 'aiomatic_draft_processed');
91 + }
92 + }
93 + function aiomatic_draft_process($post_id, $post, $update)
94 + {
95 + if (wp_is_post_autosave($post_id))
96 + {
97 + return;
98 + }
99 + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
100 + return;
101 + }
102 + if (wp_is_post_revision($post_id)) {
103 + return;
104 + }
105 + if ($post->post_status === 'draft')
106 + {
107 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
108 + if (isset($aiomatic_Spinner_Settings['delay_post']) && $aiomatic_Spinner_Settings['delay_post'] != '' && is_numeric($aiomatic_Spinner_Settings['delay_post']))
109 + {
110 + aiomatic_schedule_post($post_id, intval($aiomatic_Spinner_Settings['delay_post']));
111 + return;
112 + }
113 + $is_draft_added = get_post_meta($post_id, 'aiomatic_draft_processed', true);
114 + if (!$is_draft_added)
115 + {
116 + update_post_meta($post_id, 'aiomatic_draft_processed', '1');
117 + aiomatic_do_post_wrapper($post, true);
118 + }
119 + }
120 + else
121 + {
122 + delete_post_meta($post_id, 'aiomatic_draft_processed');
123 + }
124 + }
125 + function aiomatic_pending_process($post_id, $post, $update)
126 + {
127 + if (wp_is_post_autosave($post_id))
128 + {
129 + return;
130 + }
131 + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
132 + return;
133 + }
134 + if (wp_is_post_revision($post_id)) {
135 + return;
136 + }
137 + if ($post->post_status === 'pending')
138 + {
139 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
140 + if (isset($aiomatic_Spinner_Settings['delay_post']) && $aiomatic_Spinner_Settings['delay_post'] != '' && is_numeric($aiomatic_Spinner_Settings['delay_post']))
141 + {
142 + sleep(intval($aiomatic_Spinner_Settings['delay_post']));
143 + $post = get_post($post_id);
144 + if($post === null)
145 + {
146 + aiomatic_log_to_file('Post ID no longer found! ID is: ' . $post_id);
147 + return;
148 + }
149 + }
150 + $is_draft_added = get_post_meta($post_id, 'aiomatic_pending_processed', true);
151 + if (!$is_draft_added)
152 + {
153 + update_post_meta($post_id, 'aiomatic_pending_processed', '1');
154 + aiomatic_do_post_wrapper($post, true);
155 + }
156 + }
157 + else
158 + {
159 + delete_post_meta($post_id, 'aiomatic_pending_processed');
160 + }
161 + }
162 + function aiomatic_new_post($new_status, $old_status, $post)
163 + {
164 + if ('publish' !== $new_status or 'publish' === $old_status)
165 + {
166 + return;
167 + }
168 + else
169 + {
170 + 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'))))
171 + {
172 + $delay_it_is_gutenberg = true;
173 + }
174 + else
175 + {
176 + $delay_it_is_gutenberg = false;
177 + }
178 + }
179 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
180 + if (isset($aiomatic_Main_Settings['aiomatic_enabled']) && $aiomatic_Main_Settings['aiomatic_enabled'] == 'on')
181 + {
182 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
183 + if (isset($aiomatic_Spinner_Settings['aiomatic_spinning']) && $aiomatic_Spinner_Settings['aiomatic_spinning'] == 'on') {
184 + if (isset($aiomatic_Spinner_Settings['delay_post']) && $aiomatic_Spinner_Settings['delay_post'] != '' && is_numeric($aiomatic_Spinner_Settings['delay_post'])) {
185 + if(wp_next_scheduled('aiomatic_new_post_cron', array($post, true)) === false)
186 + {
187 + if($delay_it_is_gutenberg && $aiomatic_Spinner_Settings['delay_post'] < 2)
188 + {
189 + $aiomatic_Spinner_Settings['delay_post'] = 2;
190 + }
191 + wp_schedule_single_event(time() + $aiomatic_Spinner_Settings['delay_post'], 'aiomatic_new_post_cron', array($post, true));
192 + }
193 + }
194 + else
195 + {
196 + if (isset($aiomatic_Spinner_Settings['run_background']) && $aiomatic_Spinner_Settings['run_background'] == 'on') {
197 + if($delay_it_is_gutenberg)
198 + {
199 + if(wp_next_scheduled('aiomatic_new_post_cron', array($post, true)) === false)
200 + {
201 + wp_schedule_single_event(time() + 2, 'aiomatic_new_post_cron', array($post, true));
202 + }
203 + }
204 + else
205 + {
206 + $unique_id = uniqid();
207 + aiomatic_update_option('aiomatic_do_post_uniqid', $unique_id);
208 + $xcron_url = site_url( '?aiomatic_do_post_cronjob=1&post_id=' . $post->ID . '&aiomatic_do_post_key=' . $unique_id);
209 + wp_remote_post( $xcron_url, array( 'timeout' => 1, 'blocking' => false, 'sslverify' => false ) );
210 + }
211 + }
212 + else
213 + {
214 + if($delay_it_is_gutenberg)
215 + {
216 + if(wp_next_scheduled('aiomatic_new_post_cron', array($post, true)) === false)
217 + {
218 + wp_schedule_single_event( time() + 2, 'aiomatic_new_post_cron', array($post, true) );
219 + }
220 + }
221 + else
222 + {
223 + aiomatic_do_post_wrapper($post, true);
224 + }
225 + }
226 + }
227 + }
228 + }
229 + }
230 +
231 + function aiomatic_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 = [])
232 + {
233 + foreach ($elements as &$element)
234 + {
235 + if ($element['elType'] === 'container' || $element['elType'] === 'widget')
236 + {
237 + if ($element['elType'] === 'widget' && in_array($element['widgetType'], $elementor_widgets))
238 + {
239 + if (!empty($allowed_classes))
240 + {
241 + $css_classes = '';
242 + if (!empty($element['settings']['_css_classes'])) {
243 + $css_classes = $element['settings']['_css_classes'];
244 + } elseif (!empty($element['settings']['css_classes'])) {
245 + $css_classes = $element['settings']['css_classes'];
246 + }
247 + if (empty($css_classes)) {
248 + continue;
249 + }
250 + $class_array = preg_split('/\s+/', $css_classes);
251 + if (count(array_intersect($allowed_classes, $class_array)) === 0) {
252 + continue;
253 + }
254 + }
255 + aiomatic_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);
256 + }
257 + if (!empty($element['elements'])) {
258 + aiomatic_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);
259 + }
260 + }
261 + }
262 + }
263 +
264 + function aiomatic_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) {
265 + if (!isset($element['elType']) || $element['elType'] !== 'widget') {
266 + if (isset($element['elements']) && is_array($element['elements'])) {
267 + foreach ($element['elements'] as &$sub_element) {
268 + aiomatic_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);
269 + }
270 + }
271 + return;
272 + }
273 + if (!isset($element['widgetType']) || !in_array($element['widgetType'], $allowed_widgets)) {
274 + return;
275 + }
276 + if (isset($element['settings']['editor']) && !empty($element['settings']['editor'])) {
277 + $content_to_edit = $element['settings']['editor'];
278 + if ($content_to_edit != '')
279 + {
280 + $final_gut_content_ai = '';
281 + $tokens = aiomatic_encode($content_to_edit);
282 + $content_token_count = count($tokens);
283 + $available_tokens = $max_tokens - ($instructions_token_count + $content_token_count + 2);
284 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
285 + {
286 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens)
287 + {
288 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
289 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
290 + }
291 + $chunk_split = str_split($content_to_edit, intval($aiomatic_Spinner_Settings['max_char_chunks']));
292 + }
293 + else
294 + {
295 + $chunk_split = aiomatic_split_to_token_len($tokens, intval($max_tokens / 2));
296 + }
297 + }
298 + else
299 + {
300 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens && (!isset($aiomatic_Spinner_Settings['no_add_content']) || $aiomatic_Spinner_Settings['no_add_content'] != 'on'))
301 + {
302 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
303 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
304 + }
305 + $chunk_split = str_split($content_to_edit, intval($aiomatic_Spinner_Settings['max_char_chunks']));
306 + }
307 + else
308 + {
309 + $chunk_split = array($content_to_edit);
310 + }
311 + }
312 + if (isset($aiomatic_Spinner_Settings['no_add_content']) && $aiomatic_Spinner_Settings['no_add_content'] == 'on')
313 + {
314 + $chunk_split = array($content_to_edit);
315 + }
316 + foreach($chunk_split as $my_little_chunk)
317 + {
318 + if(!in_array($model, $completionmodels))
319 + {
320 + $exclude_count_before += aiomatic_countExcludes($my_little_chunk);
321 + }
322 + if(in_array($model, $completionmodels))
323 + {
324 + if (!isset($aiomatic_Spinner_Settings['no_add_content']) || $aiomatic_Spinner_Settings['no_add_content'] != 'on')
325 + {
326 + $prompt = $ai_instruction . ':\r\n\r\n ' . $my_little_chunk . ' \r\n\r\n';
327 + }
328 + else
329 + {
330 + $prompt = $ai_instruction;
331 + }
332 + $error = '';
333 + $finish_reason = '';
334 + $max_tokens = aimogenpro_get_max_tokens($model);
335 + $query_token_count = count(aiomatic_encode($prompt));
336 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
337 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
338 + {
339 + $string_len = aiomatic_strlen($prompt);
340 + $string_len = $string_len / 2;
341 + $string_len = intval(0 - $string_len);
342 + $prompt = aiomatic_substr($prompt, 0, $string_len);
343 + $prompt = trim($prompt);
344 + if(empty($prompt))
345 + {
346 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
347 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
348 + }
349 + }
350 + else
351 + {
352 + $query_token_count = count(aiomatic_encode($prompt));
353 + $available_tokens = $max_tokens - $query_token_count;
354 + }
355 + }
356 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
357 + {
358 + $api_service = aiomatic_get_api_service($token, $model);
359 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $model . ')(' . $available_tokens . ') Content Editor with seed command: ' . $prompt);
360 + }
361 + $response_text = aiomatic_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(), '');
362 + if($response_text === false)
363 + {
364 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
365 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit the post chunk using AI: ' . $error . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
366 + }
367 + $final_gut_content_ai .= $my_little_chunk;
368 + }
369 + else
370 + {
371 + $response_text = trim($response_text);
372 + $final_gut_content_ai .= $response_text;
373 + }
374 + }
375 + else
376 + {
377 + $aierror = '';
378 + $edited_content = aiomatic_edit_text($token, $model, $ai_instruction, $my_little_chunk, $edit_temperature, $edit_top_p, 'contentEditor', 0, $aierror);
379 + if($edited_content !== false)
380 + {
381 + $final_gut_content_ai .= $edited_content;
382 + }
383 + else
384 + {
385 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
386 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post chunk using AI: ' . $aierror . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
387 + }
388 + $final_gut_content_ai .= $my_little_chunk;
389 + }
390 + }
391 + }
392 + if(!empty($final_gut_content_ai))
393 + {
394 + $processed_content = $final_gut_content_ai;
395 + if ($processed_content !== $content_to_edit)
396 + {
397 + if(function_exists('mb_convert_encoding'))
398 + {
399 + $processed_content = mb_convert_encoding($processed_content, 'UTF-8', 'auto');
400 + }
401 + $element['settings']['editor'] = $processed_content;
402 + $edits_made = true;
403 + }
404 + }
405 + }
406 + }
407 + }
408 +
409 + add_action('init', 'aiomatic_do_post_callback', 0);
410 + function aiomatic_do_post_callback()
411 + {
412 + $secretp_key = get_option('aiomatic_do_post_uniqid', false);
413 + if (isset($_GET['aiomatic_do_post_cronjob']) && $_GET['aiomatic_do_post_cronjob'] == '1' && isset($_GET['post_id']) && is_numeric($_GET['post_id']) && $_GET['aiomatic_do_post_key'] === $secretp_key)
414 + {
415 + $post = get_post($_GET['post_id']);
416 + if($post !== null)
417 + {
418 + aiomatic_do_post_wrapper($post, false);
419 + exit();
420 + }
421 + }
422 + }
423 + function aiomatic_do_post($post, $manual = false, $template = false, $editor_rule = false)
424 + {
425 + $plugin = plugin_basename(__FILE__);
426 + $plugin_slug = explode('/', $plugin);
427 + $plugin_slug = $plugin_slug[0];
428 + $uoptions = array();
429 + $is_activated = aiomatic_is_activated($plugin_slug, $uoptions);
430 + if($is_activated !== true && $is_activated !== 2 && $is_activated !== -2)
431 + {
432 + aiomatic_log_to_file('The plugin is not activated using a valid purchase code. You need to activate the plugin for this feature to work.');
433 + return;
434 + }
435 + $raw_img_list = array();
436 + $full_result_list = array();
437 + $post_link = '';
438 + $post_title = '';
439 + $blog_title = '';
440 + $post_excerpt = '';
441 + $final_content = '';
442 + $user_name = '';
443 + $featured_image = '';
444 + $post_cats = '';
445 + $post_tagz = '';
446 + $postID = '';
447 + $img_attr = '';
448 + $thread_id = '';
449 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
450 + if (isset($aiomatic_Main_Settings['rule_timeout']) && $aiomatic_Main_Settings['rule_timeout'] != '')
451 + {
452 + $timeout = intval($aiomatic_Main_Settings['rule_timeout']);
453 + }
454 + else
455 + {
456 + $timeout = 36000;
457 + }
458 + ini_set('safe_mode', 'Off');
459 + ini_set('max_execution_time', $timeout);
460 + ini_set('ignore_user_abort', 1);
461 + ini_set('user_agent', aiomatic_get_random_user_agent());
462 + if(function_exists('ignore_user_abort'))
463 + {
464 + ignore_user_abort(true);
465 + }
466 + if(function_exists('set_time_limit'))
467 + {
468 + set_time_limit($timeout);
469 + }
470 + if (isset($aiomatic_Main_Settings['aiomatic_enabled']) && $aiomatic_Main_Settings['aiomatic_enabled'] == 'on')
471 + {
472 + require_once(dirname(__FILE__) . "/res/aiomatic-chars.php");
473 + wp_cache_delete('aiomatic_Spinner_Settings', 'options');
474 + $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
475 + $editor_rule_applied = false;
476 + if($editor_rule !== false && is_array($editor_rule) && $editor_rule[5] == '1')
477 + {
478 + if($editor_rule[0] == 'default')
479 + {
480 + $editor_rule[0] = '';
481 + }
482 + $aiomatic_Spinner_Settings['use_template_auto'] = $editor_rule[0];
483 + $aiomatic_Spinner_Settings['post_posts'] = $editor_rule[1];
484 + $aiomatic_Spinner_Settings['post_pages'] = $editor_rule[2];
485 + $aiomatic_Spinner_Settings['post_custom'] = $editor_rule[3];
486 + $aiomatic_Spinner_Settings['except_type'] = $editor_rule[4];
487 + $aiomatic_Spinner_Settings['only_type'] = $editor_rule[6];
488 + $aiomatic_Spinner_Settings['disabled_categories'] = $editor_rule[7];
489 + $aiomatic_Spinner_Settings['disable_tags'] = $editor_rule[9];
490 + $aiomatic_Spinner_Settings['disable_users'] = $editor_rule[10];
491 + $aiomatic_Spinner_Settings['enable_users'] = $editor_rule[11];
492 + $aiomatic_Spinner_Settings['featured_status'] = $editor_rule[12];
493 + $aiomatic_Spinner_Settings['enable_field'] = $editor_rule[13];
494 + $aiomatic_Spinner_Settings['disable_field'] = $editor_rule[14];
495 + $aiomatic_Spinner_Settings['enable_tags'] = $editor_rule[15];
496 + $aiomatic_Spinner_Settings['seo_status'] = $editor_rule[16];
497 + $aiomatic_Spinner_Settings['enable_default'] = 'yes';
498 + $editor_rule_applied = true;
499 + }
500 + if($editor_rule_applied == false && $manual == false && isset($aiomatic_Spinner_Settings['enable_default']) && $aiomatic_Spinner_Settings['enable_default'] == 'on')
501 + {
502 + return;
503 + }
504 + if(isset($aiomatic_Spinner_Settings['disabled_categories']) && is_array($aiomatic_Spinner_Settings['disabled_categories']) && in_array('aiomatic_no_category_12345678', $aiomatic_Spinner_Settings['disabled_categories']))
505 + {
506 + if (($key = array_search('aiomatic_no_category_12345678', $aiomatic_Spinner_Settings['disabled_categories'])) !== false)
507 + {
508 + unset($aiomatic_Spinner_Settings['disabled_categories'][$key]);
509 + }
510 + }
511 + if(isset($aiomatic_Spinner_Settings['store_data']) && $aiomatic_Spinner_Settings['store_data'] == 'on')
512 + {
513 + $store_data = 'on';
514 + }
515 + else
516 + {
517 + $store_data = 'off';
518 + }
519 + $pid = $post->ID;
520 + $post = get_post($post->ID);
521 + if($post === null)
522 + {
523 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
524 + {
525 + aiomatic_log_to_file('Post ID no longer found: ' . $pid);
526 + }
527 + return;
528 + }
529 + if(in_array($post->post_type, AIMOGEN_EXCEPTED_POST_TYPES_FROM_EDITING))
530 + {
531 + return;
532 + }
533 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
534 + {
535 + aiomatic_log_exec_time('Edit Posts');
536 + }
537 + $added_img_list = array();
538 + $added_images = 0;
539 + $heading_results = array();
540 + if ($manual)
541 + {
542 + if($template !== 'skip')
543 + {
544 + if(!empty($template) && is_numeric($template))
545 + {
546 + $template_name = '';
547 + $post_data = false;
548 + $args = array(
549 + 'post_type' => 'aiomatic_editor_temp',
550 + 'p' => intval($template),
551 + );
552 + $the_query = new WP_Query( $args );
553 + if ( $the_query->have_posts() )
554 + {
555 + while ( $the_query->have_posts() )
556 + {
557 + $the_query->the_post();
558 + $post_id = get_the_ID();
559 + $template_name = get_the_title();
560 + $post_data = get_post_meta($post_id, 'aiomatic_json', true);
561 + }
562 + }
563 + else
564 + {
565 + wp_reset_postdata();
566 + aiomatic_log_to_file('Failed to parse AI Content Editor template with ID: ' . $template);
567 + return;
568 + }
569 + wp_reset_postdata();
570 + if(!empty($post_data))
571 + {
572 + if(is_string($post_data))
573 + {
574 + $post_data = str_replace("\\'", "'", $post_data);
575 + $post_data = str_replace("'", "\\\\'", $post_data);
576 + $post_data_decode = json_decode($post_data);
577 + }
578 + else
579 + {
580 + $post_data_decode = $post_data;
581 + }
582 + if($post_data_decode === null)
583 + {
584 + $json_last_error = json_last_error();
585 + $json_last_error_msg = json_last_error_msg();
586 + $error_message = 'Failed to parse Post Editor template with ID: ' . $post_data . "\n";
587 + $error_message .= 'JSON Error: ' . $json_last_error . ' - ' . $json_last_error_msg;
588 + aiomatic_log_to_file($error_message);
589 + return;
590 + }
591 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
592 + {
593 + aiomatic_log_to_file('Loading Manual AI Content Editor Template: ' . $template_name);
594 + }
595 + foreach($post_data_decode as $theindex => $thevalue)
596 + {
597 + $aiomatic_Spinner_Settings[$theindex] = $thevalue;
598 + }
599 + }
600 + else
601 + {
602 + aiomatic_log_to_file('Cannot find the AI Content Editor template with ID: ' . $template);
603 + return;
604 + }
605 + }
606 + else
607 + {
608 + if(isset($aiomatic_Spinner_Settings['use_template_manual']) && $aiomatic_Spinner_Settings['use_template_manual'] != '')
609 + {
610 + $template_name = '';
611 + $post_data = false;
612 + $args = array(
613 + 'post_type' => 'aiomatic_editor_temp',
614 + 'p' => intval($aiomatic_Spinner_Settings['use_template_manual']),
615 + );
616 + $the_query = new WP_Query( $args );
617 + if ( $the_query->have_posts() )
618 + {
619 + while ( $the_query->have_posts() )
620 + {
621 + $the_query->the_post();
622 + $post_id = get_the_ID();
623 + $template_name = get_the_title();
624 + $post_data = get_post_meta($post_id, 'aiomatic_json', true);
625 + }
626 + }
627 + else
628 + {
629 + wp_reset_postdata();
630 + aiomatic_log_to_file('Failed to parse AI Content Editor template with ID: ' . $aiomatic_Spinner_Settings['use_template_manual']);
631 + return;
632 + }
633 + wp_reset_postdata();
634 + if(!empty($post_data))
635 + {
636 + if(is_string($post_data))
637 + {
638 + $post_data = str_replace("\\'", "'", $post_data);
639 + $post_data = str_replace("'", "\\\\'", $post_data);
640 + $post_data_decode = json_decode($post_data);
641 + }
642 + else
643 + {
644 + $post_data_decode = $post_data;
645 + }
646 + if($post_data_decode === null)
647 + {
648 + $json_last_error = json_last_error();
649 + $json_last_error_msg = json_last_error_msg();
650 + $error_message = 'Failed to parse Post Editor template with ID: ' . $post_data . "\n";
651 + $error_message .= 'JSON Error: ' . $json_last_error . ' - ' . $json_last_error_msg;
652 + aiomatic_log_to_file($error_message);
653 + return;
654 + }
655 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
656 + {
657 + aiomatic_log_to_file('Loading Manual AI Content Editor Template: ' . $template_name);
658 + }
659 + foreach($post_data_decode as $theindex => $thevalue)
660 + {
661 + $aiomatic_Spinner_Settings[$theindex] = $thevalue;
662 + }
663 + }
664 + else
665 + {
666 + aiomatic_log_to_file('Cannot find the AI Content Editor template with ID: ' . $aiomatic_Spinner_Settings['use_template_manual']);
667 + return;
668 + }
669 + }
670 + }
671 + }
672 + }
673 + else
674 + {
675 + if(isset($aiomatic_Spinner_Settings['use_template_auto']) && $aiomatic_Spinner_Settings['use_template_auto'] != '')
676 + {
677 + $template_name = '';
678 + $post_data = false;
679 + $args = array(
680 + 'post_type' => 'aiomatic_editor_temp',
681 + 'p' => intval($aiomatic_Spinner_Settings['use_template_auto']),
682 + );
683 + $the_query = new WP_Query( $args );
684 + if ( $the_query->have_posts() )
685 + {
686 + while ( $the_query->have_posts() )
687 + {
688 + $the_query->the_post();
689 + $post_id = get_the_ID();
690 + $template_name = get_the_title();
691 + $post_data = get_post_meta($post_id, 'aiomatic_json', true);
692 + }
693 + }
694 + else
695 + {
696 + wp_reset_postdata();
697 + aiomatic_log_to_file('Failed to process AI Content Editor template with ID: ' . $aiomatic_Spinner_Settings['use_template_auto']);
698 + return;
699 + }
700 + wp_reset_postdata();
701 + if(!empty($post_data))
702 + {
703 + if(is_string($post_data))
704 + {
705 + $post_data = str_replace("\\'", "'", $post_data);
706 + $post_data = str_replace("'", "\\\\'", $post_data);
707 + $post_data_decode = json_decode($post_data);
708 + }
709 + else
710 + {
711 + $post_data_decode = $post_data;
712 + }
713 + if($post_data_decode === null)
714 + {
715 + $json_last_error = json_last_error();
716 + $json_last_error_msg = json_last_error_msg();
717 + $error_message = 'Failed to parse Post Editor template with ID: ' . $post_data . "\n";
718 + $error_message .= 'JSON Error: ' . $json_last_error . ' - ' . $json_last_error_msg;
719 + aiomatic_log_to_file($error_message);
720 + return;
721 + }
722 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
723 + {
724 + aiomatic_log_to_file('Loading Automatic AI Content Editor Template: ' . $template_name);
725 + }
726 + foreach($post_data_decode as $theindex => $thevalue)
727 + {
728 + $aiomatic_Spinner_Settings[$theindex] = $thevalue;
729 + }
730 + }
731 + else
732 + {
733 + aiomatic_log_to_file('Cannot find the AI Content Editor template with ID: ' . $aiomatic_Spinner_Settings['use_template_auto']);
734 + return;
735 + }
736 + }
737 + }
738 + if ($manual || isset($aiomatic_Spinner_Settings['aiomatic_spinning']) && $aiomatic_Spinner_Settings['aiomatic_spinning'] == 'on')
739 + {
740 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
741 + {
742 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for this to work!');
743 + return;
744 + }
745 + $vision_file = '';
746 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
747 + $appids = array_filter($appids);
748 + $token = $appids[array_rand($appids)];
749 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
750 + if (!$manual && isset($aiomatic_Spinner_Settings['post_posts'])) {
751 + if ($aiomatic_Spinner_Settings['post_posts'] == 'on' && 'post' === $post->post_type) {
752 + return;
753 + }
754 + }
755 + if (!$manual && isset($aiomatic_Spinner_Settings['post_pages'])) {
756 + if ($aiomatic_Spinner_Settings['post_pages'] == 'on' && 'page' === $post->post_type) {
757 + return;
758 + }
759 + }
760 + if (!$manual && isset($aiomatic_Spinner_Settings['post_custom'])) {
761 + if ($aiomatic_Spinner_Settings['post_custom'] == 'on' && 'page' !== $post->post_type && 'post' !== $post->post_type)
762 + {
763 + if (isset($aiomatic_Spinner_Settings['except_type']) && $aiomatic_Spinner_Settings['except_type'] != '')
764 + {
765 + $excepted_types = explode(',', $aiomatic_Spinner_Settings['except_type']);
766 + $excepted_types = array_map('trim', $excepted_types);
767 + if(!in_array($post->post_type, $excepted_types))
768 + {
769 + return;
770 + }
771 + }
772 + else
773 + {
774 + return;
775 + }
776 + }
777 + }
778 + if (!$manual && (!isset($aiomatic_Spinner_Settings['post_custom']) || $aiomatic_Spinner_Settings['post_custom'] != 'on'))
779 + {
780 + if (isset($aiomatic_Spinner_Settings['only_type']) && $aiomatic_Spinner_Settings['only_type'] != '')
781 + {
782 + $only_types = explode(',', $aiomatic_Spinner_Settings['only_type']);
783 + $only_types = array_map('trim', $only_types);
784 + if(!in_array($post->post_type, $only_types))
785 + {
786 + return;
787 + }
788 + }
789 + }
790 + if (isset($aiomatic_Spinner_Settings['custom_name']) && trim($aiomatic_Spinner_Settings['custom_name']) != '')
791 + {
792 + $custom_name = trim($aiomatic_Spinner_Settings['custom_name']);
793 + $custom_name = str_replace('%%current_date%%', date("Y-m-d"), $custom_name);
794 + }
795 + else
796 + {
797 + $custom_name = 'aiomatic_published';
798 + }
799 + $meta = get_post_meta($post->ID, $custom_name, true);
800 + if (!$manual && $meta == 'pub' && $editor_rule === false)
801 + {
802 + return;
803 + }
804 + $meta = get_post_meta($post->ID, "aiomatic_auto_post_spinned", true);
805 + if ($meta === '1')
806 + {
807 + return;
808 + }
809 + if (isset($aiomatic_Spinner_Settings['max_seed_tokens']) && $aiomatic_Spinner_Settings['max_seed_tokens'] != '')
810 + {
811 + $max_seed_tokens = intval($aiomatic_Spinner_Settings['max_seed_tokens']);
812 + }
813 + else
814 + {
815 + $max_seed_tokens = '';
816 + }
817 + if (isset($aiomatic_Spinner_Settings['temperature']) && $aiomatic_Spinner_Settings['temperature'] != '')
818 + {
819 + $temperature = floatval($aiomatic_Spinner_Settings['temperature']);
820 + }
821 + else
822 + {
823 + $temperature = 1;
824 + }
825 + if (isset($aiomatic_Spinner_Settings['top_p']) && $aiomatic_Spinner_Settings['top_p'] != '')
826 + {
827 + $top_p = floatval($aiomatic_Spinner_Settings['top_p']);
828 + }
829 + else
830 + {
831 + $top_p = 1;
832 + }
833 + if (isset($aiomatic_Spinner_Settings['presence_penalty']) && $aiomatic_Spinner_Settings['presence_penalty'] != '')
834 + {
835 + $presence_penalty = floatval($aiomatic_Spinner_Settings['presence_penalty']);
836 + }
837 + else
838 + {
839 + $presence_penalty = 0;
840 + }
841 + if (isset($aiomatic_Spinner_Settings['frequency_penalty']) && $aiomatic_Spinner_Settings['frequency_penalty'] != '')
842 + {
843 + $frequency_penalty = floatval($aiomatic_Spinner_Settings['frequency_penalty']);
844 + }
845 + else
846 + {
847 + $frequency_penalty = 0;
848 + }
849 + if (isset($aiomatic_Spinner_Settings['model']) && $aiomatic_Spinner_Settings['model'] != '')
850 + {
851 + $completion_model = $aiomatic_Spinner_Settings['model'];
852 + }
853 + else
854 + {
855 + $completion_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
856 + }
857 + if (isset($aiomatic_Spinner_Settings['append_assistant_id']) && $aiomatic_Spinner_Settings['append_assistant_id'] != '')
858 + {
859 + $assistant_id = $aiomatic_Spinner_Settings['append_assistant_id'];
860 + }
861 + else
862 + {
863 + $assistant_id = '';
864 + }
865 + if (isset($aiomatic_Spinner_Settings['headings_model']) && $aiomatic_Spinner_Settings['headings_model'] != '')
866 + {
867 + $headings_model = $aiomatic_Spinner_Settings['headings_model'];
868 + }
869 + else
870 + {
871 + $headings_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
872 + }
873 + if (isset($aiomatic_Spinner_Settings['headings_assistant_id']) && $aiomatic_Spinner_Settings['headings_assistant_id'] != '')
874 + {
875 + $headings_assistant_id = $aiomatic_Spinner_Settings['headings_assistant_id'];
876 + }
877 + else
878 + {
879 + $headings_assistant_id = '';
880 + }
881 + if (isset($aiomatic_Spinner_Settings['headings_ai_command']) && $aiomatic_Spinner_Settings['headings_ai_command'] != '')
882 + {
883 + $headings_ai_command = $aiomatic_Spinner_Settings['headings_ai_command'];
884 + }
885 + else
886 + {
887 + $headings_ai_command = 'Write %%needed_heading_count%% PAA related questions, each on a new line, for the title: %%post_title%%';
888 + }
889 + if (isset($aiomatic_Spinner_Settings['max_tokens']) && $aiomatic_Spinner_Settings['max_tokens'] != '')
890 + {
891 + $max_tokens_completion = intval($aiomatic_Spinner_Settings['max_tokens']);
892 + }
893 + else
894 + {
895 + $max_tokens_completion = aimogenpro_get_max_tokens($completion_model);
896 + }
897 + if($max_tokens_completion <= 0)
898 + {
899 + $max_tokens_completion = AIMOGEN_DEFAULT_MAX_TOKENS;
900 + }
901 + if($max_tokens_completion > aimogenpro_get_max_tokens($completion_model) && aimogenpro_get_max_tokens($completion_model) > 0)
902 + {
903 + $max_tokens_completion = aimogenpro_get_max_tokens($completion_model);
904 + }
905 + $max_tokens = $max_tokens_completion;
906 + $updated = false;
907 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
908 + {
909 + aiomatic_log_to_file('Starting editing post ID: ' . $pid);
910 + }
911 + $post_title = $post->post_title;
912 + $post_excerpt = $post->post_excerpt;
913 + $final_content = $post->post_content;
914 + if (isset($aiomatic_Spinner_Settings['ai_duplication']) && $aiomatic_Spinner_Settings['ai_duplication'] != '' && $aiomatic_Spinner_Settings['ai_duplication'] != 'disabled')
915 + {
916 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
917 + {
918 + aiomatic_log_to_file('Starting AI duplication checking...');
919 + }
920 + $post_type_to_check = $post->post_type;
921 + $duplicate_post_id = false;
922 + $duplicate_post_title = false;
923 + if (isset($aiomatic_Spinner_Settings['similarity_threshold']) && $aiomatic_Spinner_Settings['similarity_threshold'] != '' && is_numeric($aiomatic_Spinner_Settings['similarity_threshold']))
924 + {
925 + $similarity_threshold = floatval($aiomatic_Spinner_Settings['similarity_threshold']);
926 + }
927 + else
928 + {
929 + $similarity_threshold = 0.9;
930 + }
931 + if (isset($aiomatic_Spinner_Settings['max_to_process_in_one_go']) && $aiomatic_Spinner_Settings['max_to_process_in_one_go'] != '' && is_numeric($aiomatic_Spinner_Settings['max_to_process_in_one_go']))
932 + {
933 + $max_to_process_in_one_go = intval($aiomatic_Spinner_Settings['max_to_process_in_one_go']);
934 + }
935 + else
936 + {
937 + $max_to_process_in_one_go = PHP_INT_MAX;
938 + }
939 + if (isset($aiomatic_Spinner_Settings['dup_titles']) && $aiomatic_Spinner_Settings['dup_titles'] == 'on')
940 + {
941 + $dup_titles = 1;
942 + }
943 + else
944 + {
945 + $dup_titles = 0;
946 + }
947 + if (isset($aiomatic_Spinner_Settings['enhance_articles']) && $aiomatic_Spinner_Settings['enhance_articles'] == 'on')
948 + {
949 + $enhance_articles = 1;
950 + }
951 + else
952 + {
953 + $enhance_articles = 0;
954 + }
955 + if (isset($aiomatic_Spinner_Settings['enhance_model']) && $aiomatic_Spinner_Settings['enhance_model'] != '')
956 + {
957 + $enhance_model = $aiomatic_Spinner_Settings['enhance_model'];
958 + }
959 + else
960 + {
961 + $enhance_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
962 + }
963 + if (isset($aiomatic_Spinner_Settings['enhance_assistant_id']) && $aiomatic_Spinner_Settings['enhance_assistant_id'] != '')
964 + {
965 + $enhance_assistant_id = $aiomatic_Spinner_Settings['enhance_assistant_id'];
966 + }
967 + else
968 + {
969 + $enhance_assistant_id = '';
970 + }
971 + if (isset($aiomatic_Spinner_Settings['ai_instruction_enhance']) && $aiomatic_Spinner_Settings['ai_instruction_enhance'] != '')
972 + {
973 + $ai_instruction_enhance = $aiomatic_Spinner_Settings['ai_instruction_enhance'];
974 + }
975 + else
976 + {
977 + $ai_instruction_enhance = '';
978 + }
979 + if (isset($aiomatic_Spinner_Settings['duplication_template']) && $aiomatic_Spinner_Settings['duplication_template'] != '')
980 + {
981 + $duplication_template = $aiomatic_Spinner_Settings['duplication_template'];
982 + }
983 + else
984 + {
985 + $duplication_template = '%%post_content_plain_text%%';
986 + }
987 + if (isset($aiomatic_Spinner_Settings['duplication_index']) && $aiomatic_Spinner_Settings['duplication_index'] != '')
988 + {
989 + $duplication_index = $aiomatic_Spinner_Settings['duplication_index'];
990 + }
991 + else
992 + {
993 + $duplication_index = '_aiomatic_index';
994 + }
995 + if (isset($aiomatic_Spinner_Settings['duplicate_action']) && $aiomatic_Spinner_Settings['duplicate_action'] != '')
996 + {
997 + $duplicate_action = $aiomatic_Spinner_Settings['duplicate_action'];
998 + }
999 + else
1000 + {
1001 + $duplicate_action = 'delete';
1002 + }
1003 + if (isset($aiomatic_Spinner_Settings['duplicate_handle']) && $aiomatic_Spinner_Settings['duplicate_handle'] != '')
1004 + {
1005 + $duplicate_handle = $aiomatic_Spinner_Settings['duplicate_handle'];
1006 + }
1007 + else
1008 + {
1009 + $duplicate_handle = 'keep_relevant';
1010 + }
1011 + $exclude_args = array(
1012 + 'date_after' => $aiomatic_Spinner_Settings['duplication_date_after'] ?? '',
1013 + 'date_before' => $aiomatic_Spinner_Settings['duplication_date_before'] ?? '',
1014 +
1015 + 'author__not_in' => !empty($aiomatic_Spinner_Settings['duplication_author_exclude'])
1016 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_author_exclude']))
1017 + : array(),
1018 +
1019 + 'author__in' => !empty($aiomatic_Spinner_Settings['duplication_author_include'])
1020 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_author_include']))
1021 + : array(),
1022 +
1023 + 'category__in' => !empty($aiomatic_Spinner_Settings['duplication_category_include'])
1024 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_category_include']))
1025 + : array(),
1026 +
1027 + 'category__not_in'=> !empty($aiomatic_Spinner_Settings['duplication_category_exclude'])
1028 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_category_exclude']))
1029 + : array(),
1030 +
1031 + 'tag__in' => !empty($aiomatic_Spinner_Settings['duplication_tag_include'])
1032 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_tag_include']))
1033 + : array(),
1034 +
1035 + 'tag__not_in' => !empty($aiomatic_Spinner_Settings['duplication_tag_exclude'])
1036 + ? array_map('intval', explode(',', $aiomatic_Spinner_Settings['duplication_tag_exclude']))
1037 + : array(),
1038 + );
1039 + $best_similarity = 0;
1040 + $new_embedding = aiomatic_check_duplicate_or_replace($post, $aiomatic_Main_Settings, $post_type_to_check, $duplicate_post_id, $duplicate_post_title, $similarity_threshold, $max_to_process_in_one_go, $dup_titles, $duplication_template, $duplication_index, $best_similarity, $exclude_args);
1041 + if (!$new_embedding)
1042 + {
1043 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1044 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to create embedding in AI duplication checking');
1045 + }
1046 + }
1047 + else
1048 + {
1049 + if($duplicate_handle == 'keep_old' && $new_embedding === 'REPLACE_OLD')
1050 + {
1051 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1052 + {
1053 + aiomatic_log_to_file('Enforcing to keep old post...');
1054 + }
1055 + $new_embedding = 'DUPLICATE_KEEP_OLD';
1056 + }
1057 + elseif($duplicate_handle == 'keep_new' && $new_embedding === 'DUPLICATE_KEEP_OLD')
1058 + {
1059 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1060 + {
1061 + aiomatic_log_to_file('Enforcing to keep new post...');
1062 + }
1063 + $new_embedding = 'REPLACE_OLD';
1064 + }
1065 + $kept_source = '';
1066 + $rem_source = '';
1067 + if($new_embedding === 'REPLACE_OLD' || $new_embedding === 'DUPLICATE_KEEP_OLD')
1068 + {
1069 + if($enhance_articles == 1 && $ai_instruction_enhance != '')
1070 + {
1071 + $go_process = false;
1072 + $dupli_post = null;
1073 + if($new_embedding === 'REPLACE_OLD')
1074 + {
1075 + $go_process = true;
1076 + $ai_instruction_enhance = str_replace('%%post_title_to_be_kept%%', $post->post_title, $ai_instruction_enhance);
1077 + $ai_instruction_enhance = str_replace('%%post_content_to_be_kept%%', $post->post_content, $ai_instruction_enhance);
1078 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_kept%%', wp_strip_all_tags(strip_shortcodes($post->post_content)), $ai_instruction_enhance);
1079 + $all_meta = get_post_meta($post->ID);
1080 + if ($all_meta && !empty($all_meta))
1081 + {
1082 + foreach ($all_meta as $key => $value)
1083 + {
1084 + if (stripos($key, 'post_url') !== false)
1085 + {
1086 + $kept_source = is_array($value) ? $value[0] : $value;
1087 + break;
1088 + }
1089 + }
1090 + }
1091 + $ai_instruction_enhance = str_replace('%%post_source_to_be_kept%%', $kept_source, $ai_instruction_enhance);
1092 +
1093 + $dupli_post = get_post($duplicate_post_id);
1094 + if ($dupli_post === null)
1095 + {
1096 + $ai_instruction_enhance = str_replace('%%post_title_to_be_removed%%', $duplicate_post_title, $ai_instruction_enhance);
1097 + $ai_instruction_enhance = str_replace('%%post_content_to_be_removed%%', '', $ai_instruction_enhance);
1098 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_removed%%', '', $ai_instruction_enhance);
1099 + $ai_instruction_enhance = str_replace('%%post_source_to_be_removed%%', '', $ai_instruction_enhance);
1100 + }
1101 + else
1102 + {
1103 + $ai_instruction_enhance = str_replace('%%post_title_to_be_removed%%', $duplicate_post_title, $ai_instruction_enhance);
1104 + $ai_instruction_enhance = str_replace('%%post_content_to_be_removed%%', $dupli_post->post_content, $ai_instruction_enhance);
1105 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_removed%%', wp_strip_all_tags(strip_shortcodes($dupli_post->post_content)), $ai_instruction_enhance);
1106 + $all_meta = get_post_meta($duplicate_post_id);
1107 + if ($all_meta && !empty($all_meta))
1108 + {
1109 + foreach ($all_meta as $key => $value)
1110 + {
1111 + if (stripos($key, 'post_url') !== false)
1112 + {
1113 + $rem_source = is_array($value) ? $value[0] : $value;
1114 + break;
1115 + }
1116 + }
1117 + }
1118 + $ai_instruction_enhance = str_replace('%%post_source_to_be_removed%%', $rem_source, $ai_instruction_enhance);
1119 + }
1120 + $ai_instruction_enhance = aiomatic_replaceSynergyShortcodes($ai_instruction_enhance);
1121 + $ai_instruction_enhance = trim($ai_instruction_enhance);
1122 + if(!empty($rem_source))
1123 + {
1124 + $existing_sources = get_post_meta($post->ID, 'aiomatic_duplicate_sources', true);
1125 + if(!is_array($existing_sources))
1126 + {
1127 + $existing_sources = array();
1128 + }
1129 + $existing_sources[] = $rem_source;
1130 + update_post_meta($post->ID, 'aiomatic_duplicate_sources', $existing_sources);
1131 + }
1132 + }
1133 + elseif($new_embedding === 'DUPLICATE_KEEP_OLD')
1134 + {
1135 + $go_process = true;
1136 + $ai_instruction_enhance = str_replace('%%post_title_to_be_removed%%', $post->post_title, $ai_instruction_enhance);
1137 + $ai_instruction_enhance = str_replace('%%post_content_to_be_removed%%', $post->post_content, $ai_instruction_enhance);
1138 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_removed%%', wp_strip_all_tags(strip_shortcodes($post->post_content)), $ai_instruction_enhance);
1139 + $all_meta = get_post_meta($post->ID);
1140 + if ($all_meta && !empty($all_meta))
1141 + {
1142 + foreach ($all_meta as $key => $value)
1143 + {
1144 + if (stripos($key, 'post_url') !== false)
1145 + {
1146 + $kept_source = is_array($value) ? $value[0] : $value;
1147 + break;
1148 + }
1149 + }
1150 + }
1151 + $ai_instruction_enhance = str_replace('%%post_source_to_be_removed%%', $kept_source, $ai_instruction_enhance);
1152 + if(!empty($kept_source))
1153 + {
1154 + $existing_sources = get_post_meta($duplicate_post_id, 'aiomatic_duplicate_sources', true);
1155 + if(!is_array($existing_sources))
1156 + {
1157 + $existing_sources = array();
1158 + }
1159 + $existing_sources[] = $kept_source;
1160 + update_post_meta($duplicate_post_id, 'aiomatic_duplicate_sources', $existing_sources);
1161 + }
1162 + $dupli_post = get_post($duplicate_post_id);
1163 + if ($dupli_post === null)
1164 + {
1165 + $ai_instruction_enhance = str_replace('%%post_title_to_be_kept%%', $duplicate_post_title, $ai_instruction_enhance);
1166 + $ai_instruction_enhance = str_replace('%%post_content_to_be_kept%%', '', $ai_instruction_enhance);
1167 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_kept%%', '', $ai_instruction_enhance);
1168 + $ai_instruction_enhance = str_replace('%%post_source_to_be_kept%%', '', $ai_instruction_enhance);
1169 + }
1170 + else
1171 + {
1172 + $ai_instruction_enhance = str_replace('%%post_title_to_be_kept%%', $duplicate_post_title, $ai_instruction_enhance);
1173 + $ai_instruction_enhance = str_replace('%%post_content_to_be_kept%%', $dupli_post->post_content, $ai_instruction_enhance);
1174 + $ai_instruction_enhance = str_replace('%%post_plain_text_content_to_be_kept%%', wp_strip_all_tags(strip_shortcodes($dupli_post->post_content)), $ai_instruction_enhance);
1175 + $all_meta = get_post_meta($duplicate_post_id);
1176 + if ($all_meta && !empty($all_meta))
1177 + {
1178 + foreach ($all_meta as $key => $value)
1179 + {
1180 + if (stripos($key, 'post_url') !== false)
1181 + {
1182 + $rem_source = is_array($value) ? $value[0] : $value;
1183 + break;
1184 + }
1185 + }
1186 + }
1187 + $ai_instruction_enhance = str_replace('%%post_source_to_be_kept%%', $rem_source, $ai_instruction_enhance);
1188 + }
1189 + $ai_instruction_enhance = aiomatic_replaceSynergyShortcodes($ai_instruction_enhance);
1190 + $ai_instruction_enhance = trim($ai_instruction_enhance);
1191 + }
1192 + if($go_process === true && $ai_instruction_enhance != '')
1193 + {
1194 + $query_token_count = count(aiomatic_encode($ai_instruction_enhance));
1195 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
1196 + {
1197 + $ai_instruction_enhance = aiomatic_substr($ai_instruction_enhance, 0, (0-($max_seed_tokens * 4)));
1198 + $query_token_count = count(aiomatic_encode($ai_instruction_enhance));
1199 + }
1200 + $max_tokens_enhance = aimogenpro_get_max_tokens($enhance_model);
1201 + $available_tokens = aiomatic_compute_available_tokens($enhance_model, $max_tokens_enhance, $ai_instruction_enhance, $query_token_count);
1202 + $max_result_tokens = aimogenpro_get_max_tokens($enhance_model);
1203 + if($available_tokens > $max_result_tokens)
1204 + {
1205 + $available_tokens = $max_result_tokens;
1206 + }
1207 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
1208 + {
1209 + $string_len = aiomatic_strlen($ai_instruction_enhance);
1210 + $string_len = $string_len / 2;
1211 + $string_len = intval(0 - $string_len);
1212 + $ai_instruction_enhance = aiomatic_substr($ai_instruction_enhance, 0, $string_len);
1213 + $ai_instruction_enhance = trim($ai_instruction_enhance);
1214 + if(empty($ai_instruction_enhance))
1215 + {
1216 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_instruction_enhance, true));
1217 + return;
1218 + }
1219 + $query_token_count = count(aiomatic_encode($ai_instruction_enhance));
1220 + $available_tokens = $max_tokens_enhance - $query_token_count;
1221 + }
1222 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1223 + {
1224 + $api_service = aiomatic_get_api_service($token, $enhance_model);
1225 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $enhance_assistant_id . '\\' . $enhance_model . ')(' . $available_tokens . ') Duplicate Post Editor with seed command: ' . $ai_instruction_enhance);
1226 + }
1227 + $aierror = '';
1228 + $aiwriter = '';
1229 + $finish_reason = '';
1230 + $generated_text = aiomatic_generate_text($token, $enhance_model, $ai_instruction_enhance, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'duplicateEditing', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $enhance_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
1231 + if($generated_text === false)
1232 + {
1233 + aiomatic_log_to_file($aierror);
1234 + return;
1235 + }
1236 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1237 + {
1238 + $api_service = aiomatic_get_api_service($token, $enhance_model);
1239 + aiomatic_log_to_file($api_service . ' responded successfully, ID: ' . $post->ID);
1240 + }
1241 + $args = array();
1242 + if($new_embedding === 'REPLACE_OLD')
1243 + {
1244 + $args['ID'] = $post->ID;
1245 + }
1246 + elseif($new_embedding === 'DUPLICATE_KEEP_OLD' && isset($dupli_post->ID))
1247 + {
1248 + $args['ID'] = $dupli_post->ID;
1249 + }
1250 + if(isset($args['ID']))
1251 + {
1252 + $args['post_content'] = $generated_text;
1253 + remove_filter('content_save_pre', 'wp_filter_post_kses');
1254 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1255 + remove_filter('title_save_pre', 'wp_filter_kses');
1256 + update_post_meta($args['ID'], $custom_name, "pub");
1257 + $post_updated = wp_update_post($args);
1258 + add_filter('content_save_pre', 'wp_filter_post_kses');
1259 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1260 + add_filter('title_save_pre', 'wp_filter_kses');
1261 + if (is_wp_error($post_updated)) {
1262 + $errors = $post_updated->get_error_messages();
1263 + foreach ($errors as $error) {
1264 + aiomatic_log_to_file('Error occured while updating post for AI enhanced content "' . $post->post_title . '": ' . $error);
1265 + }
1266 + }
1267 + else
1268 + {
1269 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1270 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated enhanced content.');
1271 + }
1272 + }
1273 + }
1274 + }
1275 + }
1276 + }
1277 + if($new_embedding === 'REPLACE_OLD')
1278 + {
1279 + if($duplicate_action === 'delete')
1280 + {
1281 + wp_delete_post($duplicate_post_id, true);
1282 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1283 + {
1284 + aiomatic_log_to_file('Duplicate found for post ID: ' . $pid . ' ("' . $post->post_title . '"), deleting old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1285 + }
1286 + }
1287 + elseif($duplicate_action === 'thrash')
1288 + {
1289 + wp_delete_post($duplicate_post_id, false);
1290 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1291 + {
1292 + aiomatic_log_to_file('Duplicate found for post ID: ' . $pid . ' ("' . $post->post_title . '"), thrashing old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1293 + }
1294 + }
1295 + elseif($duplicate_action === 'nothing')
1296 + {
1297 + aiomatic_log_to_file('Duplicate found for post ID: ' . $pid . ' ("' . $post->post_title . '"), old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1298 + }
1299 + else
1300 + {
1301 + aiomatic_log_to_file('Invalid duplicate action: ' . $duplicate_action);
1302 + }
1303 + }
1304 + elseif($new_embedding === 'DUPLICATE_KEEP_OLD')
1305 + {
1306 + if($duplicate_action === 'delete')
1307 + {
1308 + wp_delete_post($pid, true);
1309 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1310 + {
1311 + aiomatic_log_to_file('Duplicate found for post ID, deleting newly published post: ' . $pid . ' ("' . $post->post_title . '"), keeping old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1312 + }
1313 + return;
1314 + }
1315 + elseif($duplicate_action === 'thrash')
1316 + {
1317 + wp_delete_post($pid, false);
1318 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1319 + {
1320 + aiomatic_log_to_file('Duplicate found for post ID, thrashing newly published post: ' . $pid . ' ("' . $post->post_title . '"), keeping old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1321 + }
1322 + return;
1323 + }
1324 + elseif($duplicate_action === 'nothing')
1325 + {
1326 + aiomatic_log_to_file('Duplicate found for post ID, newly published post: ' . $pid . ' ("' . $post->post_title . '"), keeping old post: ' . $duplicate_post_id . ' ("' . $duplicate_post_title . '"), similarity score: ' . $best_similarity);
1327 + }
1328 + else
1329 + {
1330 + aiomatic_log_to_file('Invalid duplicate action: ' . $duplicate_action);
1331 + }
1332 + }
1333 + elseif($new_embedding === 'NO_MATCH')
1334 + {
1335 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1336 + {
1337 + aiomatic_log_to_file('No duplication matches found for: ' . $pid . ' ("' . $post->post_title . '"), max similarity score: ' . $best_similarity);
1338 + }
1339 + }
1340 + else
1341 + {
1342 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1343 + {
1344 + aiomatic_log_to_file('Unknown return value from duplicate checking for post ID: ' . $pid . ', return value: ' . $new_embedding);
1345 + }
1346 +
1347 + }
1348 + if($new_embedding !== 'DUPLICATE_KEEP_OLD')
1349 + {
1350 + update_post_meta($post->ID, $custom_name, "pub");
1351 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
1352 + {
1353 + $args = array();
1354 + $args['ID'] = $post->ID;
1355 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
1356 + remove_filter('content_save_pre', 'wp_filter_post_kses');
1357 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1358 + remove_filter('title_save_pre', 'wp_filter_kses');
1359 + $post_updated = wp_update_post($args);
1360 + add_filter('content_save_pre', 'wp_filter_post_kses');
1361 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1362 + add_filter('title_save_pre', 'wp_filter_kses');
1363 + if (is_wp_error($post_updated)) {
1364 + $errors = $post_updated->get_error_messages();
1365 + foreach ($errors as $error) {
1366 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
1367 + }
1368 + }
1369 + }
1370 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
1371 + {
1372 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
1373 + if($date_stamp)
1374 + {
1375 + $args = array();
1376 + $args['ID'] = $post->ID;
1377 + $postdate = date("Y-m-d H:i:s", $date_stamp);
1378 + $args['post_date'] = $postdate;
1379 + remove_filter('content_save_pre', 'wp_filter_post_kses');
1380 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1381 + remove_filter('title_save_pre', 'wp_filter_kses');
1382 + $post_updated = wp_update_post($args);
1383 + add_filter('content_save_pre', 'wp_filter_post_kses');
1384 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1385 + add_filter('title_save_pre', 'wp_filter_kses');
1386 + if (is_wp_error($post_updated)) {
1387 + $errors = $post_updated->get_error_messages();
1388 + foreach ($errors as $error) {
1389 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
1390 + }
1391 + }
1392 + }
1393 + }
1394 + }
1395 + }
1396 + }
1397 + $changes_made = false;
1398 + if (isset($aiomatic_Spinner_Settings['publish_new']) && $aiomatic_Spinner_Settings['publish_new'] === 'yes')
1399 + {
1400 + $new_id = aiomatic_duplicate_post_everything($post->ID);
1401 + $new_post = get_post($new_id);
1402 + if($new_post === null)
1403 + {
1404 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1405 + {
1406 + aiomatic_log_to_file('Failed to publish new post while editing it editing, post ID: ' . $post->ID);
1407 + }
1408 + return;
1409 + }
1410 + else
1411 + {
1412 + $post = $new_post;
1413 + $pid = $post->ID;
1414 + $post_title = $post->post_title;
1415 + $post_excerpt = $post->post_excerpt;
1416 + $final_content = $post->post_content;
1417 + }
1418 + }
1419 + if (isset($aiomatic_Spinner_Settings['ai_rewriter']) && $aiomatic_Spinner_Settings['ai_rewriter'] != '' && $aiomatic_Spinner_Settings['ai_rewriter'] != 'disabled')
1420 + {
1421 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1422 + {
1423 + aiomatic_log_to_file('Starting post rewriter...');
1424 + }
1425 + $vision_file = '';
1426 + $vision_file_title = '';
1427 + $vision_file_excerpt = '';
1428 + $vision_file_slug = '';
1429 + if (isset($aiomatic_Spinner_Settings['edit_temperature']) && $aiomatic_Spinner_Settings['edit_temperature'] != '')
1430 + {
1431 + $edit_temperature = floatval($aiomatic_Spinner_Settings['edit_temperature']);
1432 + }
1433 + else
1434 + {
1435 + $edit_temperature = 0;
1436 + }
1437 + if (isset($aiomatic_Spinner_Settings['edit_model']) && $aiomatic_Spinner_Settings['edit_model'] != '')
1438 + {
1439 + $model = trim($aiomatic_Spinner_Settings['edit_model']);
1440 + }
1441 + else
1442 + {
1443 + $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1444 + }
1445 + if (isset($aiomatic_Spinner_Settings['title_model']) && $aiomatic_Spinner_Settings['title_model'] != '')
1446 + {
1447 + $title_model = trim($aiomatic_Spinner_Settings['title_model']);
1448 + }
1449 + else
1450 + {
1451 + $title_model = $model;
1452 + }
1453 + if (isset($aiomatic_Spinner_Settings['excerpt_model']) && $aiomatic_Spinner_Settings['excerpt_model'] != '')
1454 + {
1455 + $excerpt_model = trim($aiomatic_Spinner_Settings['excerpt_model']);
1456 + }
1457 + else
1458 + {
1459 + $excerpt_model = $model;
1460 + }
1461 + if (isset($aiomatic_Spinner_Settings['slug_model']) && $aiomatic_Spinner_Settings['slug_model'] != '')
1462 + {
1463 + $slug_model = trim($aiomatic_Spinner_Settings['slug_model']);
1464 + }
1465 + else
1466 + {
1467 + $slug_model = $model;
1468 + }
1469 + if (isset($aiomatic_Spinner_Settings['ai_vision']) && $aiomatic_Spinner_Settings['ai_vision'] == 'on')
1470 + {
1471 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
1472 + if($avatar !== false)
1473 + {
1474 + $vision_file = $avatar;
1475 + }
1476 + }
1477 + if (isset($aiomatic_Spinner_Settings['ai_title_vision']) && $aiomatic_Spinner_Settings['ai_title_vision'] == 'on')
1478 + {
1479 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
1480 + if($avatar !== false)
1481 + {
1482 + $vision_file_title = $avatar;
1483 + }
1484 + }
1485 + if (isset($aiomatic_Spinner_Settings['ai_excerpt_vision']) && $aiomatic_Spinner_Settings['ai_excerpt_vision'] == 'on')
1486 + {
1487 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
1488 + if($avatar !== false)
1489 + {
1490 + $vision_file_excerpt = $avatar;
1491 + }
1492 + }
1493 + if (isset($aiomatic_Spinner_Settings['ai_slug_vision']) && $aiomatic_Spinner_Settings['ai_slug_vision'] == 'on')
1494 + {
1495 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
1496 + if($avatar !== false)
1497 + {
1498 + $vision_file_slug = $avatar;
1499 + }
1500 + }
1501 + if (isset($aiomatic_Spinner_Settings['edit_assistant_id']) && $aiomatic_Spinner_Settings['edit_assistant_id'] != '')
1502 + {
1503 + $assistant_id = trim($aiomatic_Spinner_Settings['edit_assistant_id']);
1504 + }
1505 + else
1506 + {
1507 + $assistant_id = '';
1508 + }
1509 + if (isset($aiomatic_Spinner_Settings['edit_top_p']) && $aiomatic_Spinner_Settings['edit_top_p'] != '')
1510 + {
1511 + $edit_top_p = floatval($aiomatic_Spinner_Settings['edit_top_p']);
1512 + }
1513 + else
1514 + {
1515 + $edit_top_p = 1;
1516 + }
1517 + if (isset($aiomatic_Spinner_Settings['edit_presence_penalty']) && $aiomatic_Spinner_Settings['edit_presence_penalty'] != '')
1518 + {
1519 + $edit_presence_penalty = floatval($aiomatic_Spinner_Settings['edit_presence_penalty']);
1520 + }
1521 + else
1522 + {
1523 + $edit_presence_penalty = 0;
1524 + }
1525 + if (isset($aiomatic_Spinner_Settings['edit_frequency_penalty']) && $aiomatic_Spinner_Settings['edit_frequency_penalty'] != '')
1526 + {
1527 + $edit_frequency_penalty = floatval($aiomatic_Spinner_Settings['edit_frequency_penalty']);
1528 + }
1529 + else
1530 + {
1531 + $edit_frequency_penalty = 1;
1532 + }
1533 + $completionmodels = aiomatic_get_all_models(true);
1534 + if ((isset($aiomatic_Spinner_Settings['ai_instruction']) && $aiomatic_Spinner_Settings['ai_instruction'] != '') || (isset($aiomatic_Spinner_Settings['ai_instruction_title']) && $aiomatic_Spinner_Settings['ai_instruction_title'] != '') || (isset($aiomatic_Spinner_Settings['ai_instruction_excerpt']) && $aiomatic_Spinner_Settings['ai_instruction_excerpt'] != '') || (isset($aiomatic_Spinner_Settings['ai_instruction_slug']) && $aiomatic_Spinner_Settings['ai_instruction_slug'] != ''))
1535 + {
1536 + $ai_instruction = trim($aiomatic_Spinner_Settings['ai_instruction']);
1537 + $ai_instruction = aiomatic_replaceSynergyShortcodes($ai_instruction);
1538 + $ai_instruction_title = trim($aiomatic_Spinner_Settings['ai_instruction_title']);
1539 + $ai_instruction_title = aiomatic_replaceSynergyShortcodes($ai_instruction_title);
1540 + $ai_instruction_excerpt = trim($aiomatic_Spinner_Settings['ai_instruction_excerpt']);
1541 + $ai_instruction_excerpt = aiomatic_replaceSynergyShortcodes($ai_instruction_excerpt);
1542 + $ai_instruction_slug = trim($aiomatic_Spinner_Settings['ai_instruction_slug']);
1543 + $ai_instruction_slug = aiomatic_replaceSynergyShortcodes($ai_instruction_slug);
1544 + $post_link = get_permalink($post->ID);
1545 + $blog_title = html_entity_decode(get_bloginfo('title'));
1546 + $author_obj = get_user_by('id', $post->post_author);
1547 + if($author_obj !== false && isset($author_obj->user_nicename))
1548 + {
1549 + $user_name = $author_obj->user_nicename;
1550 + }
1551 + else
1552 + {
1553 + $user_name = '';
1554 + }
1555 + $featured_image = '';
1556 + wp_suspend_cache_addition(true);
1557 + $metas = get_post_custom($post->ID);
1558 + wp_suspend_cache_addition(false);
1559 + if(is_array($metas))
1560 + {
1561 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
1562 + }
1563 + else
1564 + {
1565 + $rez_meta = array();
1566 + }
1567 + if(count($rez_meta) > 0)
1568 + {
1569 + foreach($rez_meta as $rm)
1570 + {
1571 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
1572 + {
1573 + $featured_image = $rm[0];
1574 + break;
1575 + }
1576 + }
1577 + }
1578 + if($featured_image == '')
1579 + {
1580 + $featured_image = aiomatic_generate_thumbmail($post->ID);
1581 + }
1582 + if($featured_image == '' && $final_content != '')
1583 + {
1584 + $dom = new DOMDocument();
1585 + $internalErrors = libxml_use_internal_errors(true);
1586 + $dom->loadHTML($final_content);
1587 + libxml_use_internal_errors($internalErrors);
1588 + $tags = $dom->getElementsByTagName('img');
1589 + foreach ($tags as $tag) {
1590 + $temp_get_img = $tag->getAttribute('src');
1591 + if ($temp_get_img != '') {
1592 + $temp_get_img = strtok($temp_get_img, '?');
1593 + $featured_image = rtrim($temp_get_img, '/');
1594 + }
1595 + }
1596 + }
1597 + $post_cats = '';
1598 + $post_categories = wp_get_post_categories( $post->ID );
1599 + foreach($post_categories as $c){
1600 + $cat = get_category( $c );
1601 + $post_cats .= $cat->name . ',';
1602 + }
1603 + $post_cats = trim($post_cats, ',');
1604 + if($post_cats != '')
1605 + {
1606 + $post_categories = explode(',', $post_cats);
1607 + }
1608 + else
1609 + {
1610 + $post_categories = array();
1611 + }
1612 + if(count($post_categories) == 0)
1613 + {
1614 + $terms = get_the_terms( $post->ID, 'product_cat' );
1615 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
1616 + foreach ( $terms as $term ) {
1617 + $post_categories[] = $term->slug;
1618 + }
1619 + $post_cats = implode(',', $post_categories);
1620 + }
1621 +
1622 + }
1623 + foreach($post_categories as $pc)
1624 + {
1625 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
1626 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
1627 + {
1628 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
1629 + {
1630 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1631 + {
1632 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
1633 + }
1634 + update_post_meta($post->ID, $custom_name, "skip");
1635 + return;
1636 + }
1637 + }
1638 + }
1639 + }
1640 + $post_tagz = '';
1641 + $post_tags = wp_get_post_tags( $post->ID );
1642 + foreach($post_tags as $t){
1643 + $post_tagz .= $t->name . ',';
1644 + }
1645 + $post_tagz = trim($post_tagz, ',');
1646 + if($post_tagz != '')
1647 + {
1648 + $post_tags = explode(',', $post_tagz);
1649 + }
1650 + else
1651 + {
1652 + $post_tags = array();
1653 + }
1654 + if(count($post_tags) == 0)
1655 + {
1656 + $terms = get_the_terms( $post->ID, 'product_tag' );
1657 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
1658 + foreach ( $terms as $term ) {
1659 + $post_tags[] = $term->slug;
1660 + }
1661 + $post_tagz = implode(',', $post_tags);
1662 + }
1663 +
1664 + }
1665 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
1666 +
1667 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
1668 + foreach($disable_users as $disable_user)
1669 + {
1670 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
1671 + {
1672 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1673 + {
1674 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
1675 + }
1676 + update_post_meta($post->ID, $custom_name, "skip");
1677 + return;
1678 + }
1679 + }
1680 + }
1681 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
1682 +
1683 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
1684 + $found = false;
1685 + foreach($enable_users as $disable_user)
1686 + {
1687 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
1688 + {
1689 + $found = true;
1690 + }
1691 + }
1692 + if($found === false)
1693 + {
1694 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1695 + {
1696 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
1697 + }
1698 + update_post_meta($post->ID, $custom_name, "skip");
1699 + return;
1700 + }
1701 + }
1702 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
1703 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
1704 + {
1705 + if ( !has_post_thumbnail($post->ID) )
1706 + {
1707 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1708 + {
1709 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
1710 + }
1711 + update_post_meta($post->ID, $custom_name, "skip");
1712 + return;
1713 + }
1714 + }
1715 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
1716 + {
1717 + if ( has_post_thumbnail($post->ID) )
1718 + {
1719 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1720 + {
1721 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
1722 + }
1723 + update_post_meta($post->ID, $custom_name, "skip");
1724 + return;
1725 + }
1726 + }
1727 + }
1728 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
1729 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
1730 + {
1731 +
1732 + if ( !aiomatic_check_seo_description($post->ID) )
1733 + {
1734 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1735 + {
1736 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
1737 + }
1738 + update_post_meta($post->ID, $custom_name, "skip");
1739 + return;
1740 + }
1741 + }
1742 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
1743 + {
1744 + if ( aiomatic_check_seo_description($post->ID) )
1745 + {
1746 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1747 + {
1748 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
1749 + }
1750 + update_post_meta($post->ID, $custom_name, "skip");
1751 + return;
1752 + }
1753 + }
1754 + }
1755 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
1756 + $skip_me = true;
1757 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
1758 + foreach($enable_field as $fieldx)
1759 + {
1760 + if(strstr($fieldx, '=>') !== false)
1761 + {
1762 + $fna = explode('=>', $fieldx);
1763 + if(isset($fna[1]))
1764 + {
1765 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
1766 + if ($custom_val)
1767 + {
1768 + if(trim($fna[1]) == '*')
1769 + {
1770 + $skip_me = false;
1771 + }
1772 + if($custom_val == trim($fna[1]))
1773 + {
1774 + $skip_me = false;
1775 + }
1776 + }
1777 + }
1778 + }
1779 + }
1780 + if($skip_me === true)
1781 + {
1782 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1783 + {
1784 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
1785 + }
1786 + update_post_meta($post->ID, $custom_name, "skip");
1787 + return;
1788 + }
1789 + }
1790 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
1791 + $skip_me = false;
1792 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
1793 + foreach($disable_field as $fieldx)
1794 + {
1795 + if(strstr($fieldx, '=>') !== false)
1796 + {
1797 + $fna = explode('=>', $fieldx);
1798 + if(isset($fna[1]))
1799 + {
1800 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
1801 + if ($custom_val)
1802 + {
1803 + if(trim($fna[1]) == '*')
1804 + {
1805 + $skip_me = true;
1806 + }
1807 + if($custom_val == trim($fna[1]))
1808 + {
1809 + $skip_me = true;
1810 + }
1811 + }
1812 + }
1813 + }
1814 + }
1815 + if($skip_me === true)
1816 + {
1817 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1818 + {
1819 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
1820 + }
1821 + update_post_meta($post->ID, $custom_name, "skip");
1822 + return;
1823 + }
1824 + }
1825 + foreach($post_tags as $pt)
1826 + {
1827 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
1828 +
1829 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
1830 + foreach($disable_tags as $disabled_tag)
1831 + {
1832 + if($manual != true && trim($pt) == trim($disabled_tag))
1833 + {
1834 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1835 + {
1836 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
1837 + }
1838 + update_post_meta($post->ID, $custom_name, "skip");
1839 + return;
1840 + }
1841 + }
1842 + }
1843 + }
1844 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
1845 + $skip = true;
1846 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
1847 + foreach($post_tags as $pt)
1848 + {
1849 + foreach($enable_tags as $enable_tag)
1850 + {
1851 + if(trim($pt) == trim($enable_tag))
1852 + {
1853 + $skip = false;
1854 + break;
1855 + }
1856 + }
1857 + }
1858 + if($skip == true)
1859 + {
1860 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1861 + {
1862 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
1863 + }
1864 + update_post_meta($post->ID, $custom_name, "skip");
1865 + return;
1866 + }
1867 + }
1868 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1869 + {
1870 + aiomatic_log_to_file('Now editing post ID: ' . $post->ID);
1871 + }
1872 + $ai_instruction = aiomatic_replaceAIPostShortcodes($ai_instruction, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
1873 + $ai_instruction = trim($ai_instruction);
1874 + $ai_instruction_title = aiomatic_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, '', '', '', '', '', '');
1875 + $ai_instruction_title = trim($ai_instruction_title);
1876 + $ai_instruction_excerpt = aiomatic_replaceAIPostShortcodes($ai_instruction_excerpt, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
1877 + $ai_instruction_excerpt = trim($ai_instruction_excerpt);
1878 + $ai_instruction_slug = aiomatic_replaceAIPostShortcodes($ai_instruction_slug, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
1879 + $ai_instruction_slug = str_replace('%%post_slug%%', $post->post_name, $ai_instruction_slug);
1880 + $ai_instruction_slug = trim($ai_instruction_slug);
1881 + if(isset($aiomatic_Spinner_Settings['protect_html']) && $aiomatic_Spinner_Settings['protect_html'] == 'on')
1882 + {
1883 + if(!in_array($model, $completionmodels))
1884 + {
1885 + if(!empty($ai_instruction))
1886 + {
1887 + $ai_instruction .= ", numbers in brackets are protected terms, keep them unchanged in the returned text.";
1888 + }
1889 + }
1890 + else
1891 + {
1892 + $ai_instruction .= ", don't edit HTML tags, only text.";
1893 + }
1894 + }
1895 + $pre_tags_matches = array();
1896 + $pre_tags_matches_s = array();
1897 + $conseqMatchs = array();
1898 + $htmlfounds = array();
1899 + if(!in_array($model, $completionmodels))
1900 + {
1901 + $final_content_pre = aiomatic_replaceExcludes($final_content, $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
1902 + }
1903 + else
1904 + {
1905 + $final_content_pre = $final_content;
1906 + }
1907 +
1908 + if (!$manual && isset($aiomatic_Spinner_Settings['max_char']) && $aiomatic_Spinner_Settings['max_char'] != '')
1909 + {
1910 + if(aiomatic_strlen($ai_instruction_title) > $aiomatic_Spinner_Settings['max_char'])
1911 + {
1912 + aiomatic_log_to_file('Skipping post, title too long, max is: ' . $aiomatic_Spinner_Settings['max_char']);
1913 + }
1914 + if(aiomatic_strlen($ai_instruction) > $aiomatic_Spinner_Settings['max_char'])
1915 + {
1916 + aiomatic_log_to_file('Skipping post, content too long, max is: ' . $aiomatic_Spinner_Settings['max_char']);
1917 + }
1918 + if(aiomatic_strlen($ai_instruction_excerpt) > $aiomatic_Spinner_Settings['max_char'])
1919 + {
1920 + aiomatic_log_to_file('Skipping post, excerpt too long, max is: ' . $aiomatic_Spinner_Settings['max_char']);
1921 + }
1922 + }
1923 + $instructions_token_count = count(aiomatic_encode($ai_instruction));
1924 + $instructions_token_count_title = count(aiomatic_encode($ai_instruction_title));
1925 + $instructions_token_count_excerpt = count(aiomatic_encode($ai_instruction_excerpt));
1926 + $instructions_token_count_slug = count(aiomatic_encode($ai_instruction_slug));
1927 + $title_token_count = count(aiomatic_encode($post_title));
1928 + $excerpt_token_count = count(aiomatic_encode($post->post_excerpt));
1929 + $slug_token_count = count(aiomatic_encode($post->post_name));
1930 + $max_tokens = aimogenpro_get_max_tokens($model);
1931 + $max_title_tokens = aimogenpro_get_max_tokens($title_model);
1932 + $max_excerpt_tokens = aimogenpro_get_max_tokens($excerpt_model);
1933 + $max_slug_tokens = aimogenpro_get_max_tokens($slug_model);
1934 + $available_title_tokens = $max_title_tokens - ($instructions_token_count_title + $title_token_count);
1935 + $available_excerpt_tokens = $max_excerpt_tokens - ($instructions_token_count_excerpt + $excerpt_token_count);
1936 + $available_slug_tokens = $max_slug_tokens - ($instructions_token_count_slug + $slug_token_count);
1937 + $title_ai_edited = '';
1938 + $excerpt_ai_edited = '';
1939 + if ((!isset($aiomatic_Spinner_Settings['no_title']) || $aiomatic_Spinner_Settings['no_title'] != 'on') && !empty($ai_instruction_title))
1940 + {
1941 + if($available_title_tokens < 0)
1942 + {
1943 + aiomatic_log_to_file('Skipping editing title, it is too long: ' . $post->post_title);
1944 + }
1945 + else
1946 + {
1947 + if(in_array($title_model, $completionmodels))
1948 + {
1949 + if (!isset($aiomatic_Spinner_Settings['no_add_title']) || $aiomatic_Spinner_Settings['no_add_title'] != 'on')
1950 + {
1951 + $prompt = $ai_instruction_title . ': ' . $post_title;
1952 + }
1953 + else
1954 + {
1955 + $prompt = $ai_instruction_title;
1956 + }
1957 + $error = '';
1958 + $finish_reason = '';
1959 + $max_tokens = aimogenpro_get_max_tokens($title_model);
1960 + $query_token_count = count(aiomatic_encode($prompt));
1961 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $prompt, $query_token_count);
1962 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
1963 + {
1964 + $string_len = aiomatic_strlen($prompt);
1965 + $string_len = $string_len / 2;
1966 + $string_len = intval(0 - $string_len);
1967 + $prompt = aiomatic_substr($prompt, 0, $string_len);
1968 + $prompt = trim($prompt);
1969 + if(empty($prompt))
1970 + {
1971 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1972 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
1973 + }
1974 + }
1975 + else
1976 + {
1977 + $query_token_count = count(aiomatic_encode($prompt));
1978 + $available_tokens = $max_tokens - $query_token_count;
1979 + }
1980 + }
1981 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1982 + {
1983 + $api_service = aiomatic_get_api_service($token, $title_model);
1984 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $title_model . ')(' . $available_tokens . ') Title Editor with seed command: ' . $prompt);
1985 + }
1986 + $response_text = aiomatic_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(), '');
1987 + if($response_text === false)
1988 + {
1989 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1990 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post title using AI: ' . $error);
1991 + }
1992 + }
1993 + else
1994 + {
1995 + $response_text = ucfirst(trim(trim(trim(trim($response_text), '.'), ' "\'')));
1996 + $title_ai_edited = $response_text;
1997 + $post_title = $response_text;
1998 + }
1999 + }
2000 + else
2001 + {
2002 + $aierror = '';
2003 + $edited_content = aiomatic_edit_text($token, $title_model, $ai_instruction_title, $post_title, $edit_temperature, $edit_top_p, 'titleEditor', 0, $aierror);
2004 + if($edited_content !== false)
2005 + {
2006 + $edited_content = ucfirst(trim(trim(trim(trim($edited_content), '.'), ' "\'')));
2007 + $title_ai_edited = $edited_content;
2008 + $post_title = $edited_content;
2009 + }
2010 + else
2011 + {
2012 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2013 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post title using AI: ' . $aierror);
2014 + }
2015 + }
2016 + }
2017 + }
2018 + }
2019 + if ((!isset($aiomatic_Spinner_Settings['no_excerpt']) || $aiomatic_Spinner_Settings['no_excerpt'] != 'on') && !empty($ai_instruction_excerpt))
2020 + {
2021 + if($available_excerpt_tokens < 0)
2022 + {
2023 + aiomatic_log_to_file('Skipping editing excerpt, it is too long: ' . $post->post_excerpt);
2024 + }
2025 + else
2026 + {
2027 + if(in_array($excerpt_model, $completionmodels))
2028 + {
2029 + if (!isset($aiomatic_Spinner_Settings['no_add_excerpt']) || $aiomatic_Spinner_Settings['no_add_excerpt'] != 'on')
2030 + {
2031 + $prompt = $ai_instruction_excerpt . ': ' . $post_excerpt;
2032 + }
2033 + else
2034 + {
2035 + $prompt = $ai_instruction_excerpt;
2036 + }
2037 + $error = '';
2038 + $finish_reason = '';
2039 + $max_tokens = aimogenpro_get_max_tokens($excerpt_model);
2040 + $query_token_count = count(aiomatic_encode($prompt));
2041 + $available_tokens = aiomatic_compute_available_tokens($excerpt_model, $max_tokens, $prompt, $query_token_count);
2042 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2043 + {
2044 + $string_len = aiomatic_strlen($prompt);
2045 + $string_len = $string_len / 2;
2046 + $string_len = intval(0 - $string_len);
2047 + $prompt = aiomatic_substr($prompt, 0, $string_len);
2048 + $prompt = trim($prompt);
2049 + if(empty($prompt))
2050 + {
2051 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2052 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
2053 + }
2054 + }
2055 + else
2056 + {
2057 + $query_token_count = count(aiomatic_encode($prompt));
2058 + $available_tokens = $max_tokens - $query_token_count;
2059 + }
2060 + }
2061 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2062 + {
2063 + $api_service = aiomatic_get_api_service($token, $excerpt_model);
2064 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $excerpt_model . ')(' . $available_tokens . ') Excerpt Editor with seed command: ' . $prompt);
2065 + }
2066 + $response_text = aiomatic_generate_text($token, $excerpt_model, $prompt, $available_tokens, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, false, 'excerptCEditor', 0, $finish_reason, $error, true, false, false, $vision_file_excerpt, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
2067 + if($response_text === false)
2068 + {
2069 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2070 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post excerpt using AI: ' . $error);
2071 + }
2072 + }
2073 + else
2074 + {
2075 + $response_text = trim($response_text);
2076 + $response_text = ucfirst(trim(trim(trim(trim($response_text), '.'), ' "\'')));
2077 + $excerpt_ai_edited = $response_text;
2078 + $post_excerpt = $response_text;
2079 + }
2080 + }
2081 + else
2082 + {
2083 + $aierror = '';
2084 + $edited_content = aiomatic_edit_text($token, $excerpt_model, $ai_instruction_excerpt, $post_excerpt, $edit_temperature, $edit_top_p, 'excerptEditor', 0, $aierror);
2085 + if($edited_content !== false)
2086 + {
2087 + $edited_content = ucfirst(trim(trim(trim(trim($edited_content), '.'), ' "\'')));
2088 + $excerpt_ai_edited = $edited_content;
2089 + $post_excerpt = $edited_content;
2090 + }
2091 + else
2092 + {
2093 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2094 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post excerpt using AI: ' . $aierror);
2095 + }
2096 + }
2097 + }
2098 + }
2099 + }
2100 + $edited_slug = '';
2101 + if ((!isset($aiomatic_Spinner_Settings['no_slug']) || $aiomatic_Spinner_Settings['no_slug'] != 'on') && !empty($ai_instruction_slug))
2102 + {
2103 + if($available_slug_tokens < 0)
2104 + {
2105 + aiomatic_log_to_file('Skipping editing slug, it is too long: ' . $post->post_excerpt);
2106 + }
2107 + else
2108 + {
2109 + if(in_array($slug_model, $completionmodels))
2110 + {
2111 + if (!isset($aiomatic_Spinner_Settings['no_add_slug']) || $aiomatic_Spinner_Settings['no_add_slug'] != 'on')
2112 + {
2113 + $prompt = $ai_instruction_slug . ': ' . $post->post_name;
2114 + }
2115 + else
2116 + {
2117 + $prompt = $ai_instruction_slug;
2118 + }
2119 + $error = '';
2120 + $finish_reason = '';
2121 + $max_tokens = aimogenpro_get_max_tokens($slug_model);
2122 + $query_token_count = count(aiomatic_encode($prompt));
2123 + $available_tokens = aiomatic_compute_available_tokens($slug_model, $max_tokens, $prompt, $query_token_count);
2124 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2125 + {
2126 + $string_len = aiomatic_strlen($prompt);
2127 + $string_len = $string_len / 2;
2128 + $string_len = intval(0 - $string_len);
2129 + $prompt = aiomatic_substr($prompt, 0, $string_len);
2130 + $prompt = trim($prompt);
2131 + if(empty($prompt))
2132 + {
2133 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2134 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
2135 + }
2136 + }
2137 + else
2138 + {
2139 + $query_token_count = count(aiomatic_encode($prompt));
2140 + $available_tokens = $max_tokens - $query_token_count;
2141 + }
2142 + }
2143 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2144 + {
2145 + $api_service = aiomatic_get_api_service($token, $slug_model);
2146 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $slug_model . ')(' . $available_tokens . ') Slug Editor with seed command: ' . $prompt);
2147 + }
2148 + $response_text = aiomatic_generate_text($token, $slug_model, $prompt, $available_tokens, $edit_temperature, $edit_top_p, $edit_presence_penalty, $edit_frequency_penalty, false, 'slugCEditor', 0, $finish_reason, $error, true, false, false, $vision_file_slug, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
2149 + if($response_text === false)
2150 + {
2151 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2152 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post slug using AI: ' . $error);
2153 + }
2154 + }
2155 + else
2156 + {
2157 + $response_text = trim($response_text);
2158 + $response_text = ucfirst(trim(trim(trim(trim($response_text), '.'), ' "\'')));
2159 + $edited_slug = sanitize_title($response_text);
2160 + if(!empty($max_slug_len))
2161 + {
2162 + $edited_slug = aiomatic_substr($edited_slug, 0, intval($max_slug_len));
2163 + $edited_slug = trim($edited_slug, '-');
2164 + }
2165 + }
2166 + }
2167 + else
2168 + {
2169 + $aierror = '';
2170 + $edited_content = aiomatic_edit_text($token, $slug_model, $ai_instruction_slug, $post->post_name, $edit_temperature, $edit_top_p, 'slugEditor', 0, $aierror);
2171 + if($edited_content !== false)
2172 + {
2173 + $edited_content = ucfirst(trim(trim(trim(trim($edited_content), '.'), ' "\'')));
2174 + $edited_slug = sanitize_title($response_text);
2175 + if(!empty($max_slug_len))
2176 + {
2177 + $edited_slug = aiomatic_substr($edited_slug, 0, intval($max_slug_len));
2178 + $edited_slug = trim($edited_slug, '-');
2179 + }
2180 + }
2181 + else
2182 + {
2183 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2184 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post slug using AI: ' . $aierror);
2185 + }
2186 + }
2187 + }
2188 + }
2189 + }
2190 + $one_success = false;
2191 + $final_content_ai = '';
2192 + $exclude_count_before = 0;
2193 + $elementor_updated = false;
2194 + if ((!isset($aiomatic_Spinner_Settings['no_content']) || $aiomatic_Spinner_Settings['no_content'] != 'on') && !empty($ai_instruction))
2195 + {
2196 + $elementor_found = false;
2197 + if ((!isset($aiomatic_Spinner_Settings['enable_elementor']) || $aiomatic_Spinner_Settings['enable_elementor'] != '0'))
2198 + {
2199 + $allowed_classes = array();
2200 + if (!empty($aiomatic_Spinner_Settings['elementor_css_class_filter'])) {
2201 + $allowed_classes = array_map('trim', explode(',', $aiomatic_Spinner_Settings['elementor_css_class_filter']));
2202 + }
2203 + if(!function_exists('is_plugin_active'))
2204 + {
2205 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
2206 + }
2207 + if ( is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active( 'elementor-pro/elementor-pro.php' ) )
2208 + {
2209 + $elementor_data = get_post_meta($post->ID, '_elementor_data', true);
2210 + if ($elementor_data) {
2211 + $elementor_json = json_decode($elementor_data, true);
2212 + if ($elementor_json && is_array($elementor_json))
2213 + {
2214 + $elementor_found = true;
2215 + $elementor_widgets = ['text-editor', 'heading', 'accordion', 'toggle', 'tabs', 'alert', 'call-to-action', 'testimonial', 'html'];
2216 + $edits_made = false;
2217 + $max_tokens = aimogenpro_get_max_tokens($model);
2218 +
2219 + aiomatic_process_elementor_elements($elementor_json, $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);
2220 +
2221 + //set it as processed, even if no edits were made to avoid re-checking next time
2222 + update_post_meta($post->ID, $custom_name, "pub");
2223 +
2224 + if ($edits_made)
2225 + {
2226 + $json_data = wp_json_encode($elementor_json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
2227 + if ($json_data === false)
2228 + {
2229 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2230 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to re-encode Elementor JSON data: ' . json_last_error_msg());
2231 + }
2232 + return;
2233 + }
2234 +
2235 + $old_elementor_data = get_post_meta($post->ID, '_elementor_data', true);
2236 + $old_elementor_data = is_string($old_elementor_data) ? json_decode($old_elementor_data, true) : [];
2237 +
2238 + $meta_updated = update_post_meta($post->ID, '_elementor_data', wp_slash($json_data));
2239 + if ($meta_updated === false)
2240 + {
2241 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2242 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to update _elementor_data meta.');
2243 + }
2244 + return;
2245 + }
2246 +
2247 + $elementor_updated = true;
2248 +
2249 + if (class_exists('\Elementor\Plugin'))
2250 + {
2251 + if (defined('ELEMENTOR_VERSION'))
2252 + {
2253 + update_post_meta($post->ID, '_elementor_version', ELEMENTOR_VERSION);
2254 + }
2255 + update_post_meta($post->ID, '_elementor_edit_mode', 'builder');
2256 +
2257 + /** @disregard P1014 it's ok */
2258 + $eplugin = \Elementor\Plugin::$instance;
2259 + if (isset($eplugin->posts_css_manager) && method_exists($eplugin->posts_css_manager, 'clear_cache'))
2260 + {
2261 + $eplugin->posts_css_manager->clear_cache($post->ID);
2262 + }
2263 + if (isset($eplugin->files_manager) && method_exists($eplugin->files_manager, 'clear_cache'))
2264 + {
2265 + $eplugin->files_manager->clear_cache();
2266 + }
2267 +
2268 + if (class_exists('\Elementor\Core\Files\CSS\Post') && method_exists('\Elementor\Core\Files\CSS\Post', 'clear_cache'))
2269 + {
2270 + $css_post = new \Elementor\Core\Files\CSS\Post($post->ID);
2271 + $css_post->clear_cache();
2272 + $css_post->update();
2273 + }
2274 +
2275 + if (class_exists('\ElementorPro\Plugin'))
2276 + {
2277 + $theme_builder = \ElementorPro\Plugin::instance()->modules_manager->get_modules('theme-builder');
2278 + if ($theme_builder && method_exists($theme_builder, 'update_location_data'))
2279 + {
2280 + $theme_builder->update_location_data($post->ID);
2281 + }
2282 + }
2283 +
2284 + wp_update_post([
2285 + 'ID' => $post->ID,
2286 + 'post_modified' => current_time('mysql'),
2287 + 'post_modified_gmt' => current_time('mysql', 1),
2288 + ]);
2289 +
2290 + delete_transient('elementor_css_cache');
2291 + /** @disregard P1014 it's ok */
2292 + \Elementor\Plugin::$instance->files_manager->clear_cache();
2293 + /** @disregard P1014 it's ok */
2294 + \Elementor\Plugin::$instance->posts_css_manager->clear_cache();
2295 + $editor_data = $elementor_json ?? [];
2296 + do_action('save_post', $post->ID, get_post($post->ID), true);
2297 + do_action('elementor/editor/after_save', $post->ID, $editor_data);
2298 + do_action('elementor/core/files/clear_cache');
2299 +
2300 + $document = $eplugin->documents->get($post->ID);
2301 + if ($document) {
2302 + do_action('elementor/document/after_save', $document, [
2303 + 'new_settings' => $elementor_json,
2304 + 'old_settings' => $old_elementor_data,
2305 + ]);
2306 + } else {
2307 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2308 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to retrieve Elementor document.');
2309 + }
2310 + }
2311 + if (method_exists($eplugin->frontend, 'clear_cache'))
2312 + {
2313 + $eplugin->frontend->clear_cache();
2314 + }
2315 +
2316 + $changes_made = true;
2317 +
2318 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2319 + {
2320 + aiomatic_log_to_file('Post ID ' . $post->ID . ' had its Elementor blocks edited successfully.');
2321 + }
2322 + }
2323 + else
2324 + {
2325 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2326 + {
2327 + aiomatic_log_to_file('Post ID ' . $post->ID . ' could not update Elementor data: Elementor Plugin class not found.');
2328 + }
2329 + }
2330 + }
2331 + else
2332 + {
2333 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2334 + {
2335 + aiomatic_log_to_file('Post ID ' . $post->ID . ' no Elementor edits were made, but we set this post as processed, to avoid reprocessing.');
2336 + }
2337 + }
2338 + }
2339 + }
2340 + }
2341 + }
2342 + if($elementor_found === false)
2343 + {
2344 + if ((isset($aiomatic_Spinner_Settings['enable_gutenberg']) && $aiomatic_Spinner_Settings['enable_gutenberg'] == '1') && has_blocks($final_content_pre))
2345 + {
2346 + $blocks = parse_blocks($final_content_pre);
2347 + if (isset($aiomatic_Spinner_Settings['gutenberg_list']) && is_array($aiomatic_Spinner_Settings['gutenberg_list']) && !empty($aiomatic_Spinner_Settings['gutenberg_list']))
2348 + {
2349 + $gutenberg_block_types = $aiomatic_Spinner_Settings['gutenberg_list'];
2350 + }
2351 + else
2352 + {
2353 + $gutenberg_block_types = ['core/paragraph', 'core/heading', 'core/list', 'core/quote', 'core/code', 'core/preformatted', 'core/pullquote', 'core/table', 'core/verse'];
2354 + }
2355 + $edits_made = false;
2356 + foreach ($blocks as &$block)
2357 + {
2358 + if (isset($block['blockName']) && in_array($block['blockName'], $gutenberg_block_types))
2359 + {
2360 + $content_to_edit = $block['innerHTML'] ?? '';
2361 + if ($content_to_edit != '')
2362 + {
2363 + $final_gut_content_ai = '';
2364 + $tokens = aiomatic_encode($content_to_edit);
2365 + $content_token_count = count($tokens);
2366 + $available_tokens = $max_tokens - ($instructions_token_count + $content_token_count + 2);
2367 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2368 + {
2369 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens)
2370 + {
2371 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2372 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
2373 + }
2374 + $chunk_split = str_split($content_to_edit, intval($aiomatic_Spinner_Settings['max_char_chunks']));
2375 + }
2376 + else
2377 + {
2378 + $chunk_split = aiomatic_split_to_token_len($tokens, intval($max_tokens / 2));
2379 + }
2380 + }
2381 + else
2382 + {
2383 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens && (!isset($aiomatic_Spinner_Settings['no_add_content']) || $aiomatic_Spinner_Settings['no_add_content'] != 'on'))
2384 + {
2385 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2386 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
2387 + }
2388 + $chunk_split = str_split($content_to_edit, intval($aiomatic_Spinner_Settings['max_char_chunks']));
2389 + }
2390 + else
2391 + {
2392 + $chunk_split = array($content_to_edit);
2393 + }
2394 + }
2395 + if (isset($aiomatic_Spinner_Settings['no_add_content']) && $aiomatic_Spinner_Settings['no_add_content'] == 'on')
2396 + {
2397 + $chunk_split = array($content_to_edit);
2398 + }
2399 + foreach($chunk_split as $my_little_chunk)
2400 + {
2401 + if(!in_array($model, $completionmodels))
2402 + {
2403 + $exclude_count_before += aiomatic_countExcludes($my_little_chunk);
2404 + }
2405 + if(in_array($model, $completionmodels))
2406 + {
2407 + if (!isset($aiomatic_Spinner_Settings['no_add_content']) || $aiomatic_Spinner_Settings['no_add_content'] != 'on')
2408 + {
2409 + $prompt = $ai_instruction . ':\r\n\r\n ' . $my_little_chunk . ' \r\n\r\n';
2410 + }
2411 + else
2412 + {
2413 + $prompt = $ai_instruction;
2414 + }
2415 + $error = '';
2416 + $finish_reason = '';
2417 + $max_tokens = aimogenpro_get_max_tokens($model);
2418 + $query_token_count = count(aiomatic_encode($prompt));
2419 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
2420 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2421 + {
2422 + $string_len = aiomatic_strlen($prompt);
2423 + $string_len = $string_len / 2;
2424 + $string_len = intval(0 - $string_len);
2425 + $prompt = aiomatic_substr($prompt, 0, $string_len);
2426 + $prompt = trim($prompt);
2427 + if(empty($prompt))
2428 + {
2429 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2430 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
2431 + }
2432 + }
2433 + else
2434 + {
2435 + $query_token_count = count(aiomatic_encode($prompt));
2436 + $available_tokens = $max_tokens - $query_token_count;
2437 + }
2438 + }
2439 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2440 + {
2441 + $api_service = aiomatic_get_api_service($token, $model);
2442 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $model . ')(' . $available_tokens . ') Content Editor with seed command set: ' . $prompt);
2443 + }
2444 + $response_text = aiomatic_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(), '');
2445 + if($response_text === false)
2446 + {
2447 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2448 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit the post chunk using AI: ' . $error . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
2449 + }
2450 + $final_gut_content_ai .= $my_little_chunk;
2451 + }
2452 + else
2453 + {
2454 + $response_text = trim($response_text);
2455 + $final_gut_content_ai .= $response_text;
2456 + }
2457 + }
2458 + else
2459 + {
2460 + $aierror = '';
2461 + $edited_content = aiomatic_edit_text($token, $model, $ai_instruction, $my_little_chunk, $edit_temperature, $edit_top_p, 'contentEditor', 0, $aierror);
2462 + if($edited_content !== false)
2463 + {
2464 + $final_gut_content_ai .= $edited_content;
2465 + }
2466 + else
2467 + {
2468 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2469 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post chunk using AI: ' . $aierror . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
2470 + }
2471 + $final_gut_content_ai .= $my_little_chunk;
2472 + }
2473 + }
2474 + }
2475 + if(!empty($final_gut_content_ai))
2476 + {
2477 + $block['innerHTML'] = $final_gut_content_ai;
2478 + $edits_made = true;
2479 + }
2480 + }
2481 + }
2482 + }
2483 + if($edits_made === true)
2484 + {
2485 + $gutenberg_post_content = aiomatic_reconstruct_post_content($blocks);
2486 + if(!empty($gutenberg_post_content))
2487 + {
2488 + $final_content_ai = $gutenberg_post_content;
2489 + $one_success = true;
2490 + }
2491 + }
2492 + }
2493 + else
2494 + {
2495 + $tokens = aiomatic_encode($final_content_pre);
2496 + $content_token_count = count($tokens);
2497 + $available_tokens = $max_tokens - ($instructions_token_count + $content_token_count + 2);
2498 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2499 + {
2500 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens)
2501 + {
2502 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2503 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
2504 + }
2505 + $chunk_split = str_split($final_content_pre, intval($aiomatic_Spinner_Settings['max_char_chunks']));
2506 + }
2507 + else
2508 + {
2509 + $chunk_split = aiomatic_split_to_token_len($tokens, intval($max_tokens / 2));
2510 + }
2511 + }
2512 + else
2513 + {
2514 + if (isset($aiomatic_Spinner_Settings['max_char_chunks']) && $aiomatic_Spinner_Settings['max_char_chunks'] != '' && intval($aiomatic_Spinner_Settings['max_char_chunks']) / 4 < $max_tokens)
2515 + {
2516 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2517 + aiomatic_log_to_file('Splitting text into chunks of ' . $aiomatic_Spinner_Settings['max_char_chunks'] . ' characters.');
2518 + }
2519 + $chunk_split = str_split($final_content_pre, intval($aiomatic_Spinner_Settings['max_char_chunks']));
2520 + }
2521 + else
2522 + {
2523 + $chunk_split = array($final_content_pre);
2524 + }
2525 + }
2526 + if (isset($aiomatic_Spinner_Settings['no_add_content']) && $aiomatic_Spinner_Settings['no_add_content'] == 'on')
2527 + {
2528 + $chunk_split = array($final_content_pre);
2529 + }
2530 + foreach($chunk_split as $my_little_chunk)
2531 + {
2532 + if(!in_array($model, $completionmodels))
2533 + {
2534 + $exclude_count_before += aiomatic_countExcludes($my_little_chunk);
2535 + }
2536 + if(in_array($model, $completionmodels))
2537 + {
2538 + if (!isset($aiomatic_Spinner_Settings['no_add_content']) || $aiomatic_Spinner_Settings['no_add_content'] != 'on')
2539 + {
2540 + $prompt = $ai_instruction . ':\r\n\r\n ' . $my_little_chunk . ' \r\n\r\n';
2541 + }
2542 + else
2543 + {
2544 + $prompt = $ai_instruction;
2545 + }
2546 + $error = '';
2547 + $finish_reason = '';
2548 + $max_tokens = aimogenpro_get_max_tokens($model);
2549 + $query_token_count = count(aiomatic_encode($prompt));
2550 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
2551 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
2552 + {
2553 + $string_len = aiomatic_strlen($prompt);
2554 + $string_len = $string_len / 2;
2555 + $string_len = intval(0 - $string_len);
2556 + $prompt = aiomatic_substr($prompt, 0, $string_len);
2557 + $prompt = trim($prompt);
2558 + if(empty($prompt))
2559 + {
2560 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2561 + aiomatic_log_to_file('Empty API seed expression provided (after processing)');
2562 + }
2563 + }
2564 + else
2565 + {
2566 + $query_token_count = count(aiomatic_encode($prompt));
2567 + $available_tokens = $max_tokens - $query_token_count;
2568 + }
2569 + }
2570 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
2571 + {
2572 + $api_service = aiomatic_get_api_service($token, $model);
2573 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $model . ')(' . $available_tokens . ') Content Editor with seed command prompt: ' . $prompt);
2574 + }
2575 + $response_text = aiomatic_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(), '');
2576 + if($response_text === false)
2577 + {
2578 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2579 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit the post chunk using AI: ' . $error . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
2580 + }
2581 + $final_content_ai .= $my_little_chunk;
2582 + }
2583 + else
2584 + {
2585 + $response_text = trim($response_text);
2586 + $final_content_ai .= $response_text;
2587 + $one_success = true;
2588 + }
2589 + }
2590 + else
2591 + {
2592 + $aierror = '';
2593 + $edited_content = aiomatic_edit_text($token, $model, $ai_instruction, $my_little_chunk, $edit_temperature, $edit_top_p, 'contentEditor', 0, $aierror);
2594 + if($edited_content !== false)
2595 + {
2596 + $final_content_ai .= $edited_content;
2597 + $one_success = true;
2598 + }
2599 + else
2600 + {
2601 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2602 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit post chunk using AI: ' . $aierror . ' !-! ' . $ai_instruction . ' !-! ' . $my_little_chunk);
2603 + }
2604 + $final_content_ai .= $my_little_chunk;
2605 + }
2606 + }
2607 + }
2608 + }
2609 + }
2610 + }
2611 + if($one_success === false)
2612 + {
2613 + $final_content_ai = '';
2614 + }
2615 + if($final_content_ai != '')
2616 + {
2617 + if(!in_array($model, $completionmodels))
2618 + {
2619 + $exclude_count_after = aiomatic_countExcludes($final_content_ai);
2620 + }
2621 + else
2622 + {
2623 + $exclude_count_after = 0;
2624 + }
2625 + if((!isset($aiomatic_Spinner_Settings['no_html_check']) || $aiomatic_Spinner_Settings['no_html_check'] != 'on') && $exclude_count_before != $exclude_count_after)
2626 + {
2627 + aiomatic_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));
2628 + }
2629 + else
2630 + {
2631 + if(!in_array($model, $completionmodels))
2632 + {
2633 + $final_content_ai = aiomatic_restoreExcludes($final_content_ai, $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
2634 + }
2635 + $final_content = $final_content_ai;
2636 + $args = array();
2637 + $args['ID'] = $post->ID;
2638 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on' && !aiomatic_stringContainsArrayChars($final_content, $xchars))
2639 + {
2640 + if (!isset($aiomatic_Main_Settings['no_undetectibility_editor']) || $aiomatic_Main_Settings['no_undetectibility_editor'] != 'on')
2641 + {
2642 + if(!isset($xchars))
2643 + {
2644 + $xchars = array();
2645 + }
2646 + $rand_percentage = rand(10, 20);
2647 + $final_content = aiomatic_make_unique($final_content, $xchars, $rand_percentage);
2648 + }
2649 + }
2650 + $args['post_content'] = $final_content;
2651 + if($title_ai_edited != '')
2652 + {
2653 + if ((isset($aiomatic_Spinner_Settings['rewrite_url']) && $aiomatic_Spinner_Settings['rewrite_url'] == 'on'))
2654 + {
2655 + if(!empty(sanitize_title($title_ai_edited)))
2656 + {
2657 + $args['post_name'] = sanitize_title($title_ai_edited);
2658 + }
2659 + }
2660 + $args['post_title'] = aiomatic_truncate_title($title_ai_edited);
2661 + }
2662 + if(!empty($edited_slug))
2663 + {
2664 + $args['post_name'] = trim($edited_slug);
2665 + }
2666 + if($excerpt_ai_edited != '')
2667 + {
2668 + $args['post_excerpt'] = trim($excerpt_ai_edited);
2669 + }
2670 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
2671 + {
2672 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
2673 + }
2674 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
2675 + {
2676 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
2677 + if($date_stamp)
2678 + {
2679 + $postdate = date("Y-m-d H:i:s", $date_stamp);
2680 + $args['post_date'] = $postdate;
2681 + }
2682 + }
2683 + update_post_meta($post->ID, $custom_name, "pub");
2684 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2685 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2686 + remove_filter('title_save_pre', 'wp_filter_kses');
2687 + $post_updated = wp_update_post($args);
2688 + $changes_made = true;
2689 + add_filter('content_save_pre', 'wp_filter_post_kses');
2690 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2691 + add_filter('title_save_pre', 'wp_filter_kses');
2692 + if (is_wp_error($post_updated)) {
2693 + $errors = $post_updated->get_error_messages();
2694 + foreach ($errors as $error) {
2695 + aiomatic_log_to_file('Error occured while updating post for content rewriting "' . $post->post_title . '": ' . $error);
2696 + }
2697 + }
2698 + else
2699 + {
2700 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2701 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI rewritten content.');
2702 + }
2703 + }
2704 + }
2705 + }
2706 + else
2707 + {
2708 + if($title_ai_edited != '')
2709 + {
2710 + $args = array();
2711 + $args['ID'] = $post->ID;
2712 + if ((isset($aiomatic_Spinner_Settings['rewrite_url']) && $aiomatic_Spinner_Settings['rewrite_url'] == 'on'))
2713 + {
2714 + if(!empty(sanitize_title($title_ai_edited)))
2715 + {
2716 + $args['post_name'] = sanitize_title($title_ai_edited);
2717 + }
2718 + }
2719 + $args['post_title'] = aiomatic_truncate_title($title_ai_edited);
2720 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
2721 + {
2722 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
2723 + }
2724 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
2725 + {
2726 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
2727 + if($date_stamp)
2728 + {
2729 + $postdate = date("Y-m-d H:i:s", $date_stamp);
2730 + $args['post_date'] = $postdate;
2731 + }
2732 + }
2733 + if($excerpt_ai_edited != '')
2734 + {
2735 + $args['post_excerpt'] = trim($excerpt_ai_edited);
2736 + }
2737 + if(!empty($edited_slug))
2738 + {
2739 + $args['post_name'] = trim($edited_slug);
2740 + }
2741 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2742 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2743 + remove_filter('title_save_pre', 'wp_filter_kses');
2744 + update_post_meta($post->ID, $custom_name, "pub");
2745 + $post_updated = wp_update_post($args);
2746 + $changes_made = true;
2747 + add_filter('content_save_pre', 'wp_filter_post_kses');
2748 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2749 + add_filter('title_save_pre', 'wp_filter_kses');
2750 + if (is_wp_error($post_updated)) {
2751 + $errors = $post_updated->get_error_messages();
2752 + foreach ($errors as $error) {
2753 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
2754 + }
2755 + }
2756 + else
2757 + {
2758 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2759 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated title.');
2760 + }
2761 + }
2762 + }
2763 + else
2764 + {
2765 + if($excerpt_ai_edited != '')
2766 + {
2767 + $args = array();
2768 + $args['ID'] = $post->ID;
2769 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
2770 + {
2771 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
2772 + }
2773 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
2774 + {
2775 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
2776 + if($date_stamp)
2777 + {
2778 + $postdate = date("Y-m-d H:i:s", $date_stamp);
2779 + $args['post_date'] = $postdate;
2780 + }
2781 + }
2782 + if(!empty($edited_slug))
2783 + {
2784 + $args['post_name'] = trim($edited_slug);
2785 + }
2786 + $args['post_excerpt'] = trim($excerpt_ai_edited);
2787 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2788 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2789 + remove_filter('title_save_pre', 'wp_filter_kses');
2790 + update_post_meta($post->ID, $custom_name, "pub");
2791 + $post_updated = wp_update_post($args);
2792 + $changes_made = true;
2793 + add_filter('content_save_pre', 'wp_filter_post_kses');
2794 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2795 + add_filter('title_save_pre', 'wp_filter_kses');
2796 + if (is_wp_error($post_updated)) {
2797 + $errors = $post_updated->get_error_messages();
2798 + foreach ($errors as $error) {
2799 + aiomatic_log_to_file('Error occured while updating excerpt post for title "' . $post->post_title . '": ' . $error);
2800 + }
2801 + }
2802 + else
2803 + {
2804 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2805 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated excerpt.');
2806 + }
2807 + }
2808 + }
2809 + else
2810 + {
2811 + if($edited_slug != '')
2812 + {
2813 + $args = array();
2814 + $args['ID'] = $post->ID;
2815 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
2816 + {
2817 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
2818 + }
2819 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
2820 + {
2821 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
2822 + if($date_stamp)
2823 + {
2824 + $postdate = date("Y-m-d H:i:s", $date_stamp);
2825 + $args['post_date'] = $postdate;
2826 + }
2827 + }
2828 + $args['post_name'] = trim($edited_slug);
2829 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2830 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2831 + remove_filter('title_save_pre', 'wp_filter_kses');
2832 + update_post_meta($post->ID, $custom_name, "pub");
2833 + $post_updated = wp_update_post($args);
2834 + $changes_made = true;
2835 + add_filter('content_save_pre', 'wp_filter_post_kses');
2836 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2837 + add_filter('title_save_pre', 'wp_filter_kses');
2838 + if (is_wp_error($post_updated)) {
2839 + $errors = $post_updated->get_error_messages();
2840 + foreach ($errors as $error) {
2841 + aiomatic_log_to_file('Error occured while updating post slug for title "' . $post->post_title . '": ' . $error);
2842 + }
2843 + }
2844 + else
2845 + {
2846 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2847 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated slug.');
2848 + }
2849 + }
2850 + }
2851 + else
2852 + {
2853 + if(!$elementor_updated)
2854 + {
2855 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
2856 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to be editted, nothing returned from AI editor');
2857 + }
2858 + }
2859 + else
2860 + {
2861 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
2862 + {
2863 + $args = array();
2864 + $args['ID'] = $post->ID;
2865 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
2866 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2867 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2868 + remove_filter('title_save_pre', 'wp_filter_kses');
2869 + update_post_meta($post->ID, $custom_name, "pub");
2870 + $post_updated = wp_update_post($args);
2871 + $changes_made = true;
2872 + add_filter('content_save_pre', 'wp_filter_post_kses');
2873 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2874 + add_filter('title_save_pre', 'wp_filter_kses');
2875 + if (is_wp_error($post_updated)) {
2876 + $errors = $post_updated->get_error_messages();
2877 + foreach ($errors as $error) {
2878 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
2879 + }
2880 + }
2881 + }
2882 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
2883 + {
2884 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
2885 + if($date_stamp)
2886 + {
2887 + $args = array();
2888 + $args['ID'] = $post->ID;
2889 + $postdate = date("Y-m-d H:i:s", $date_stamp);
2890 + $args['post_date'] = $postdate;
2891 + remove_filter('content_save_pre', 'wp_filter_post_kses');
2892 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2893 + remove_filter('title_save_pre', 'wp_filter_kses');
2894 + update_post_meta($post->ID, $custom_name, "pub");
2895 + $post_updated = wp_update_post($args);
2896 + $changes_made = true;
2897 + add_filter('content_save_pre', 'wp_filter_post_kses');
2898 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
2899 + add_filter('title_save_pre', 'wp_filter_kses');
2900 + if (is_wp_error($post_updated)) {
2901 + $errors = $post_updated->get_error_messages();
2902 + foreach ($errors as $error) {
2903 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
2904 + }
2905 + }
2906 + }
2907 + }
2908 + }
2909 + }
2910 + }
2911 + }
2912 + }
2913 + }
2914 + }
2915 + if (isset($aiomatic_Spinner_Settings['append_spintax']) && $aiomatic_Spinner_Settings['append_spintax'] != '' && $aiomatic_Spinner_Settings['append_spintax'] != 'disabled')
2916 + {
2917 +
2918 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
2919 + {
2920 + aiomatic_log_to_file('Starting post content completion module...');
2921 + }
2922 + if(isset($aiomatic_Spinner_Settings['social_list']) && !empty($aiomatic_Spinner_Settings['social_list']) && is_array($aiomatic_Spinner_Settings['social_list']))
2923 + {
2924 + $social_list = $aiomatic_Spinner_Settings['social_list'];
2925 + }
2926 + else
2927 + {
2928 + $social_list = array();
2929 + }
2930 + if(isset($aiomatic_Spinner_Settings['rich_embed_location']) && !empty($aiomatic_Spinner_Settings['rich_embed_location']))
2931 + {
2932 + $rich_embed_location = $aiomatic_Spinner_Settings['rich_embed_location'];
2933 + }
2934 + else
2935 + {
2936 + $rich_embed_location = 'bottom';
2937 + }
2938 + $vision_file = '';
2939 + if (isset($aiomatic_Spinner_Settings['headings']) && $aiomatic_Spinner_Settings['headings'] != '')
2940 + {
2941 + $headings = intval($aiomatic_Spinner_Settings['headings']);
2942 + }
2943 + else
2944 + {
2945 + $headings = '';
2946 + }
2947 + if (isset($aiomatic_Spinner_Settings['ai_vision_add']) && $aiomatic_Spinner_Settings['ai_vision_add'] == 'on')
2948 + {
2949 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
2950 + if($avatar !== false)
2951 + {
2952 + $vision_file = $avatar;
2953 + }
2954 + }
2955 + if (isset($aiomatic_Spinner_Settings['images']) && $aiomatic_Spinner_Settings['images'] != '')
2956 + {
2957 + $images = intval($aiomatic_Spinner_Settings['images']);
2958 + }
2959 + else
2960 + {
2961 + $images = '';
2962 + }
2963 + if (isset($aiomatic_Spinner_Settings['videos']) && $aiomatic_Spinner_Settings['videos'] != '')
2964 + {
2965 + $videos = $aiomatic_Spinner_Settings['videos'];
2966 + }
2967 + else
2968 + {
2969 + $videos = '';
2970 + }
2971 + if (isset($aiomatic_Spinner_Settings['max_result_tokens']) && $aiomatic_Spinner_Settings['max_result_tokens'] != '')
2972 + {
2973 + $max_result_tokens = intval($aiomatic_Spinner_Settings['max_result_tokens']);
2974 + }
2975 + else
2976 + {
2977 + $max_result_tokens = aimogenpro_get_max_tokens($completion_model);
2978 + }
2979 + $max_tokens = aimogenpro_get_max_tokens($completion_model);
2980 + if (isset($aiomatic_Spinner_Settings['ai_command']) && $aiomatic_Spinner_Settings['ai_command'] != '')
2981 + {
2982 + $aicontent = trim(strip_tags($aiomatic_Spinner_Settings['ai_command']));
2983 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
2984 + $post_link = get_permalink($post->ID);
2985 + $blog_title = html_entity_decode(get_bloginfo('title'));
2986 + $author_obj = get_user_by('id', $post->post_author);
2987 + if($author_obj !== false && isset($author_obj->user_nicename))
2988 + {
2989 + $user_name = $author_obj->user_nicename;
2990 + }
2991 + else
2992 + {
2993 + $user_name = '';
2994 + }
2995 + $featured_image = '';
2996 + wp_suspend_cache_addition(true);
2997 + $metas = get_post_custom($post->ID);
2998 + wp_suspend_cache_addition(false);
2999 + if(is_array($metas))
3000 + {
3001 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
3002 + }
3003 + else
3004 + {
3005 + $rez_meta = array();
3006 + }
3007 + if(count($rez_meta) > 0)
3008 + {
3009 + foreach($rez_meta as $rm)
3010 + {
3011 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
3012 + {
3013 + $featured_image = $rm[0];
3014 + break;
3015 + }
3016 + }
3017 + }
3018 + if($featured_image == '')
3019 + {
3020 + $featured_image = aiomatic_generate_thumbmail($post->ID);
3021 + }
3022 + if($featured_image == '' && $final_content != '')
3023 + {
3024 + $dom = new DOMDocument();
3025 + $internalErrors = libxml_use_internal_errors(true);
3026 + $dom->loadHTML($final_content);
3027 + libxml_use_internal_errors($internalErrors);
3028 + $tags = $dom->getElementsByTagName('img');
3029 + foreach ($tags as $tag) {
3030 + $temp_get_img = $tag->getAttribute('src');
3031 + if ($temp_get_img != '') {
3032 + $temp_get_img = strtok($temp_get_img, '?');
3033 + $featured_image = rtrim($temp_get_img, '/');
3034 + }
3035 + }
3036 + }
3037 + $post_cats = '';
3038 + $post_categories = wp_get_post_categories( $post->ID );
3039 + foreach($post_categories as $c){
3040 + $cat = get_category( $c );
3041 + $post_cats .= $cat->name . ',';
3042 + }
3043 + $post_cats = trim($post_cats, ',');
3044 + if($post_cats != '')
3045 + {
3046 + $post_categories = explode(',', $post_cats);
3047 + }
3048 + else
3049 + {
3050 + $post_categories = array();
3051 + }
3052 + if(count($post_categories) == 0)
3053 + {
3054 + $terms = get_the_terms( $post->ID, 'product_cat' );
3055 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
3056 + foreach ( $terms as $term ) {
3057 + $post_categories[] = $term->slug;
3058 + }
3059 + $post_cats = implode(',', $post_categories);
3060 + }
3061 +
3062 + }
3063 + foreach($post_categories as $pc)
3064 + {
3065 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
3066 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
3067 + {
3068 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
3069 + {
3070 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3071 + {
3072 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
3073 + }
3074 + update_post_meta($post->ID, $custom_name, "skip");
3075 + return;
3076 + }
3077 + }
3078 + }
3079 + }
3080 + $post_tagz = '';
3081 + $post_tags = wp_get_post_tags( $post->ID );
3082 + foreach($post_tags as $t){
3083 + $post_tagz .= $t->name . ',';
3084 + }
3085 + $post_tagz = trim($post_tagz, ',');
3086 + if($post_tagz != '')
3087 + {
3088 + $post_tags = explode(',', $post_tagz);
3089 + }
3090 + else
3091 + {
3092 + $post_tags = array();
3093 + }
3094 + if(count($post_tags) == 0)
3095 + {
3096 + $terms = get_the_terms( $post->ID, 'product_tag' );
3097 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
3098 + foreach ( $terms as $term ) {
3099 + $post_tags[] = $term->slug;
3100 + }
3101 + $post_tagz = implode(',', $post_tags);
3102 + }
3103 +
3104 + }
3105 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
3106 +
3107 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
3108 + foreach($disable_users as $disable_user)
3109 + {
3110 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
3111 + {
3112 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3113 + {
3114 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
3115 + }
3116 + update_post_meta($post->ID, $custom_name, "skip");
3117 + return;
3118 + }
3119 + }
3120 + }
3121 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
3122 +
3123 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
3124 + $found = false;
3125 + foreach($enable_users as $disable_user)
3126 + {
3127 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
3128 + {
3129 + $found = true;
3130 + }
3131 + }
3132 + if($found === false)
3133 + {
3134 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3135 + {
3136 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
3137 + }
3138 + update_post_meta($post->ID, $custom_name, "skip");
3139 + return;
3140 + }
3141 + }
3142 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
3143 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
3144 + {
3145 + if ( !has_post_thumbnail($post->ID) )
3146 + {
3147 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3148 + {
3149 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
3150 + }
3151 + update_post_meta($post->ID, $custom_name, "skip");
3152 + return;
3153 + }
3154 + }
3155 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
3156 + {
3157 + if ( has_post_thumbnail($post->ID) )
3158 + {
3159 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3160 + {
3161 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
3162 + }
3163 + update_post_meta($post->ID, $custom_name, "skip");
3164 + return;
3165 + }
3166 + }
3167 + }
3168 +
3169 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
3170 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
3171 + {
3172 + if ( !aiomatic_check_seo_description($post->ID) )
3173 + {
3174 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3175 + {
3176 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
3177 + }
3178 + update_post_meta($post->ID, $custom_name, "skip");
3179 + return;
3180 + }
3181 + }
3182 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
3183 + {
3184 + if ( aiomatic_check_seo_description($post->ID) )
3185 + {
3186 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3187 + {
3188 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
3189 + }
3190 + update_post_meta($post->ID, $custom_name, "skip");
3191 + return;
3192 + }
3193 + }
3194 + }
3195 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
3196 + $skip_me = true;
3197 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
3198 + foreach($enable_field as $fieldx)
3199 + {
3200 + if(strstr($fieldx, '=>') !== false)
3201 + {
3202 + $fna = explode('=>', $fieldx);
3203 + if(isset($fna[1]))
3204 + {
3205 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
3206 + if ($custom_val)
3207 + {
3208 + if(trim($fna[1]) == '*')
3209 + {
3210 + $skip_me = false;
3211 + }
3212 + if($custom_val == trim($fna[1]))
3213 + {
3214 + $skip_me = false;
3215 + }
3216 + }
3217 + }
3218 + }
3219 + }
3220 + if($skip_me === true)
3221 + {
3222 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3223 + {
3224 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
3225 + }
3226 + update_post_meta($post->ID, $custom_name, "skip");
3227 + return;
3228 + }
3229 + }
3230 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
3231 + $skip_me = false;
3232 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
3233 + foreach($disable_field as $fieldx)
3234 + {
3235 + if(strstr($fieldx, '=>') !== false)
3236 + {
3237 + $fna = explode('=>', $fieldx);
3238 + if(isset($fna[1]))
3239 + {
3240 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
3241 + if ($custom_val)
3242 + {
3243 + if(trim($fna[1]) == '*')
3244 + {
3245 + $skip_me = true;
3246 + }
3247 + if($custom_val == trim($fna[1]))
3248 + {
3249 + $skip_me = true;
3250 + }
3251 + }
3252 + }
3253 + }
3254 + }
3255 + if($skip_me === true)
3256 + {
3257 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3258 + {
3259 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
3260 + }
3261 + update_post_meta($post->ID, $custom_name, "skip");
3262 + return;
3263 + }
3264 + }
3265 + foreach($post_tags as $pt)
3266 + {
3267 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
3268 +
3269 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
3270 + foreach($disable_tags as $disabled_tag)
3271 + {
3272 + if($manual != true && trim($pt) == trim($disabled_tag))
3273 + {
3274 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3275 + {
3276 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
3277 + }
3278 + update_post_meta($post->ID, $custom_name, "skip");
3279 + return;
3280 + }
3281 + }
3282 + }
3283 + }
3284 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
3285 + $skip = true;
3286 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
3287 + foreach($post_tags as $pt)
3288 + {
3289 + foreach($enable_tags as $enable_tag)
3290 + {
3291 + if(trim($pt) == trim($enable_tag))
3292 + {
3293 + $skip = false;
3294 + break;
3295 + }
3296 + }
3297 + }
3298 + if($skip == true)
3299 + {
3300 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
3301 + {
3302 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
3303 + }
3304 + update_post_meta($post->ID, $custom_name, "skip");
3305 + return;
3306 + }
3307 + }
3308 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
3309 + }
3310 + else
3311 + {
3312 + $aicontent = trim(strip_tags($final_content));
3313 + if(empty($aicontent))
3314 + {
3315 + $aicontent = trim(strip_tags($post->post_excerpt));
3316 + }
3317 + if(empty($aicontent))
3318 + {
3319 + $aicontent = trim(strip_tags($post_title));
3320 + $last_char = aiomatic_substr($aicontent, -1, null);
3321 + if(!ctype_punct($last_char))
3322 + {
3323 + $aicontent .= '.';
3324 + }
3325 + }
3326 + }
3327 + $aicontent = str_replace('%%first_content_paragraph_plain_text%%', aiomatic_extract_paragraph($post->post_content, false, 500), $aicontent);
3328 + $aicontent = str_replace('%%last_content_paragraph_plain_text%%', aiomatic_extract_paragraph($post->post_content, true, 500), $aicontent);
3329 + $aicontent = str_replace('%%first_content_paragraph%%', aiomatic_extract_text_chars($post->post_content, false, 500), $aicontent);
3330 + $aicontent = str_replace('%%last_content_paragraph%%', aiomatic_extract_text_chars($post->post_content, true, 500), $aicontent);
3331 + $aicontent = trim($aicontent);
3332 + $query_token_count = count(aiomatic_encode($aicontent));
3333 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
3334 + {
3335 + $aicontent = aiomatic_substr($aicontent, 0, (0-($max_seed_tokens * 4)));
3336 + $query_token_count = count(aiomatic_encode($aicontent));
3337 + }
3338 + $available_tokens = aiomatic_compute_available_tokens($completion_model, $max_tokens, $aicontent, $query_token_count);
3339 + if($available_tokens > $max_result_tokens)
3340 + {
3341 + $available_tokens = $max_result_tokens;
3342 + }
3343 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
3344 + {
3345 + $string_len = aiomatic_strlen($aicontent);
3346 + $string_len = $string_len / 2;
3347 + $string_len = intval(0 - $string_len);
3348 + $aicontent = aiomatic_substr($aicontent, 0, $string_len);
3349 + $aicontent = trim($aicontent);
3350 + if(empty($aicontent))
3351 + {
3352 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($aicontent, true));
3353 + return;
3354 + }
3355 + $query_token_count = count(aiomatic_encode($aicontent));
3356 + $available_tokens = $max_tokens - $query_token_count;
3357 + }
3358 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3359 + {
3360 + $api_service = aiomatic_get_api_service($token, $completion_model);
3361 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $completion_model . ')(' . $available_tokens . ') Post Editor with seed command: ' . $aicontent);
3362 + }
3363 + $aierror = '';
3364 + $aiwriter = '';
3365 + $finish_reason = '';
3366 + $generated_text = aiomatic_generate_text($token, $completion_model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentCompletion', 0, $finish_reason, $aierror, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
3367 + if($generated_text === false)
3368 + {
3369 + aiomatic_log_to_file($aierror);
3370 + return;
3371 + }
3372 + else
3373 + {
3374 + $aiwriter = ucfirst(trim(nl2br(trim($generated_text))));
3375 + }
3376 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3377 + {
3378 + $api_service = aiomatic_get_api_service($token, $completion_model);
3379 + aiomatic_log_to_file($api_service . ' responded successfully, ID: ' . $post->ID);
3380 + }
3381 + $ai_created_data = '';
3382 + $prepp = ucfirst(trim(nl2br($aiwriter)));
3383 + if($prepp != false && $prepp != '')
3384 + {
3385 + $ai_created_data = $prepp;
3386 + }
3387 + $image_query = '';
3388 + $heading_val = '';
3389 + if(!empty($ai_created_data))
3390 + {
3391 + if($headings != '' && is_numeric($headings))
3392 + {
3393 + $heading_results = aiomatic_scrape_related_questions($ai_created_data, $headings, $headings_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $headings_ai_command, $headings_assistant_id);
3394 + }
3395 + $need_more = true;
3396 + if (isset($aiomatic_Spinner_Settings['min_char']) && $aiomatic_Spinner_Settings['min_char'] != '')
3397 + {
3398 + $min_char = intval($aiomatic_Spinner_Settings['min_char']);
3399 + $cnt = 1;
3400 + $max_fails = 10;
3401 + $failed_calls = 0;
3402 + if (isset($aiomatic_Spinner_Settings['max_continue_tokens']) && $aiomatic_Spinner_Settings['max_continue_tokens'] != '')
3403 + {
3404 + $max_continue_tokens = intval($aiomatic_Spinner_Settings['max_continue_tokens']);
3405 + }
3406 + else
3407 + {
3408 + $max_continue_tokens = 1000;
3409 + }
3410 + $ai_retry = false;
3411 + $ai_continue_title = $post_title;
3412 + while(aiomatic_strlen(strip_tags($ai_created_data)) < $min_char)
3413 + {
3414 + if (isset($aiomatic_Main_Settings['max_retry']) && $aiomatic_Main_Settings['max_retry'] != '' && is_numeric($aiomatic_Main_Settings['max_retry']) && intval($aiomatic_Main_Settings['max_retry']) < $cnt)
3415 + {
3416 + break;
3417 + }
3418 + $need_more = false;
3419 + $just_set_fallback = false;
3420 + $image_query = '';
3421 + $heading_val = '';
3422 + if(count($heading_results) > 0)
3423 + {
3424 + $rand_heading = '';
3425 + $saverand = array_rand($heading_results);
3426 + $rand_heading = $heading_results[$saverand];
3427 + unset($heading_results[$saverand]);
3428 + if(isset($rand_heading['q']))
3429 + {
3430 + $rand_heading['q'] = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $rand_heading['q']);
3431 + $heading_val = '<h2>' . $rand_heading['q'] . '</h2>' . '<span>' . $rand_heading['a'];
3432 + $image_query = $rand_heading['q'];
3433 + }
3434 + }
3435 + if($heading_val == '')
3436 + {
3437 + $temp_post = trim($ai_created_data);
3438 + }
3439 + else
3440 + {
3441 + $temp_post = trim($heading_val);
3442 + }
3443 + if(aiomatic_strlen($temp_post) > $max_continue_tokens * 4)
3444 + {
3445 + $negative_contiue_tokens = 0 - ($max_continue_tokens * 4);
3446 + $newaicontent = aiomatic_substr($temp_post, $negative_contiue_tokens, null);
3447 + }
3448 + else
3449 + {
3450 + $newaicontent = $temp_post;
3451 + }
3452 + $add_me_to_text = '';
3453 + if($ai_retry == true)
3454 + {
3455 + $just_set_fallback = true;
3456 + if (isset($aiomatic_Main_Settings['alternate_continue']) && $aiomatic_Main_Settings['alternate_continue'] == 'on')
3457 + {
3458 + $newaicontent = $newaicontent . ' ' . $ai_continue_title;
3459 + }
3460 + else
3461 + {
3462 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3463 + {
3464 + $api_service = aiomatic_get_api_service($token, $completion_model);
3465 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '\\' . $completion_model . ')(' . aimogenpro_get_max_tokens($completion_model) . ') PAA writer with seed command: ' . 'Write a People Also Asked question related to "' . $ai_continue_title . '"');
3466 + }
3467 + $aierror = '';
3468 + $finish_reason = '';
3469 + $generated_text = aiomatic_generate_text($token, $completion_model, 'Write a People Also Asked question related to "' . $ai_continue_title . '"', aimogenpro_get_max_tokens($completion_model), $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'headingCompletion', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
3470 + if($generated_text === false)
3471 + {
3472 + aiomatic_log_to_file('Similarity finding failed: ' . $aierror);
3473 + $newaicontent = $aicontent;
3474 + }
3475 + else
3476 + {
3477 + $newaicontent = ucfirst(trim(nl2br(trim($generated_text))));
3478 + if(empty($newaicontent))
3479 + {
3480 + $newaicontent = $aicontent;
3481 + }
3482 + else
3483 + {
3484 + $newaicontent = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $newaicontent);
3485 + $add_me_to_text = '<h3>' . $newaicontent . '</h3> ';
3486 + $ai_continue_title = $newaicontent;
3487 + }
3488 + }
3489 + }
3490 + }
3491 + $ai_retry = false;
3492 + $newaicontent = trim($newaicontent);
3493 + $query_token_count = count(aiomatic_encode($newaicontent));
3494 + $available_tokens = aiomatic_compute_available_tokens($completion_model, $max_tokens, $newaicontent, $query_token_count);
3495 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
3496 + {
3497 + $string_len = aiomatic_strlen($newaicontent);
3498 + $string_len = $string_len / 2;
3499 + $string_len = intval(0 - $string_len);
3500 + $newaicontent = aiomatic_substr($newaicontent, 0, $string_len);
3501 + $newaicontent = trim($newaicontent);
3502 + if(empty($newaicontent))
3503 + {
3504 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($temp_post, true));
3505 + break;
3506 + }
3507 + $query_token_count = count(aiomatic_encode($newaicontent));
3508 + $available_tokens = $max_tokens - $query_token_count;
3509 + }
3510 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
3511 + $api_service = aiomatic_get_api_service($token, $completion_model);
3512 + aiomatic_log_to_file('Calling ' . $api_service . ' again (' . $cnt . ')(' . $available_tokens . ') from text editor, to meet minimum character limit: ' . $min_char . ' - current char count: ' . aiomatic_strlen(strip_tags($ai_created_data)));
3513 + }
3514 + $aierror = '';
3515 + $aiwriter = '';
3516 + $finish_reason = '';
3517 + $generated_text = aiomatic_generate_text($token, $completion_model, $newaicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentCompletion', 0, $finish_reason, $aierror, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
3518 + if($generated_text === false)
3519 + {
3520 + aiomatic_log_to_file($aierror);
3521 + break;
3522 + }
3523 + else
3524 + {
3525 + $aiwriter = $add_me_to_text . ucfirst(trim(nl2br(trim($generated_text))));
3526 + }
3527 +
3528 + if($aiwriter == '')
3529 + {
3530 + $ai_retry = true;
3531 + if($just_set_fallback == true)
3532 + {
3533 + aiomatic_log_to_file('Ending execution, already retried once');
3534 + break;
3535 + }
3536 + continue;
3537 + }
3538 + $add_my_image = '';
3539 +
3540 + $temp_get_img = '';
3541 + $temp_get_video = '';
3542 + if($images != '' && is_numeric($images) && $images > $added_images)
3543 + {
3544 + if ($videos == 'on' && $rich_embed_location == 'inside')
3545 + {
3546 + if(rand(1, 100) <= 50)
3547 + {
3548 + $temp_get_video = aiomatic_get_video(trim(stripslashes(str_replace('&quot;', '"', $post_title))), '', $social_list);
3549 + }
3550 + }
3551 + if(empty($temp_get_video))
3552 + {
3553 + $query_words = '';
3554 + if($image_query == '')
3555 + {
3556 + $image_query = $temp_post;
3557 + }
3558 + if (isset($aiomatic_Spinner_Settings['enable_ai_images']) && ($aiomatic_Spinner_Settings['enable_ai_images'] == '1' || $aiomatic_Spinner_Settings['enable_ai_images'] == 'on'))
3559 + {
3560 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
3561 + {
3562 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
3563 + }
3564 + else
3565 + {
3566 + $image_size = '1024x1024';
3567 + }
3568 + if (isset($aiomatic_Spinner_Settings['image_model']) && trim($aiomatic_Spinner_Settings['image_model']) != '')
3569 + {
3570 + $image_model = trim($aiomatic_Spinner_Settings['image_model']);
3571 + }
3572 + else
3573 + {
3574 + $image_model = 'dalle2';
3575 + }
3576 + $get_img = '';
3577 + $query_words = $post_title;
3578 + if($image_query == '')
3579 + {
3580 + $image_query = $temp_post;
3581 + }
3582 + $orig_ai_command_image = '';
3583 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
3584 + {
3585 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
3586 + }
3587 + if($orig_ai_command_image == '')
3588 + {
3589 + $orig_ai_command_image = $image_query;
3590 + }
3591 + if($orig_ai_command_image != '')
3592 + {
3593 + $ai_command_image = $orig_ai_command_image;
3594 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
3595 + $ai_command_image = array_filter($ai_command_image);
3596 + if(count($ai_command_image) > 0)
3597 + {
3598 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
3599 + }
3600 + else
3601 + {
3602 + $ai_command_image = '';
3603 + }
3604 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3605 + if(!empty($ai_command_image))
3606 + {
3607 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3608 + }
3609 + else
3610 + {
3611 + $ai_command_image = trim(strip_tags($post_title));
3612 + }
3613 + $ai_command_image = trim($ai_command_image);
3614 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
3615 + {
3616 + $txt_content = aiomatic_get_web_page($ai_command_image);
3617 + if ($txt_content !== FALSE)
3618 + {
3619 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
3620 + $txt_content = array_filter($txt_content);
3621 + if(count($txt_content) > 0)
3622 + {
3623 + $txt_content = $txt_content[array_rand($txt_content)];
3624 + if(trim($txt_content) != '')
3625 + {
3626 + $ai_command_image = $txt_content;
3627 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3628 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3629 + }
3630 + }
3631 + }
3632 + }
3633 + if(empty($ai_command_image))
3634 + {
3635 + aiomatic_log_to_file('Empty API image seed expression provided!');
3636 + }
3637 + else
3638 + {
3639 + if(aiomatic_strlen($ai_command_image) > 400)
3640 + {
3641 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 400);
3642 + }
3643 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3644 + {
3645 + $api_service = aiomatic_get_api_service($token, $image_model);
3646 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
3647 + }
3648 + $aierror = '';
3649 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'editContentImage', false, 0, $aierror, $image_model, $post_title);
3650 + if($get_img !== false)
3651 + {
3652 + foreach($get_img as $tmpimg)
3653 + {
3654 + $added_images++;
3655 + $added_img_list[] = $tmpimg;
3656 + $temp_get_img = $tmpimg;
3657 + }
3658 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3659 + {
3660 + aiomatic_log_to_file('AI generated image returned: ' . $tmpimg);
3661 + }
3662 + }
3663 + else
3664 + {
3665 + aiomatic_log_to_file('Failed to generate AI image: ' . $aierror);
3666 + $get_img = '';
3667 + }
3668 + }
3669 + }
3670 + else
3671 + {
3672 + aiomatic_log_to_file('Empty AI image query entered.');
3673 + }
3674 + }
3675 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '2')
3676 + {
3677 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
3678 + {
3679 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
3680 + }
3681 + else
3682 + {
3683 + $image_size = '1024x1024';
3684 + }
3685 + $get_img = '';
3686 + $query_words = $post_title;
3687 + if($image_query == '')
3688 + {
3689 + $image_query = $temp_post;
3690 + }
3691 + $orig_ai_command_image = '';
3692 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
3693 + {
3694 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
3695 + }
3696 + if($orig_ai_command_image == '')
3697 + {
3698 + $orig_ai_command_image = $image_query;
3699 + }
3700 + if($orig_ai_command_image != '')
3701 + {
3702 + $ai_command_image = $orig_ai_command_image;
3703 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
3704 + $ai_command_image = array_filter($ai_command_image);
3705 + if(count($ai_command_image) > 0)
3706 + {
3707 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
3708 + }
3709 + else
3710 + {
3711 + $ai_command_image = '';
3712 + }
3713 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3714 + if(!empty($ai_command_image))
3715 + {
3716 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3717 + }
3718 + else
3719 + {
3720 + $ai_command_image = trim(strip_tags($post_title));
3721 + }
3722 + $ai_command_image = trim($ai_command_image);
3723 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
3724 + {
3725 + $txt_content = aiomatic_get_web_page($ai_command_image);
3726 + if ($txt_content !== FALSE)
3727 + {
3728 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
3729 + $txt_content = array_filter($txt_content);
3730 + if(count($txt_content) > 0)
3731 + {
3732 + $txt_content = $txt_content[array_rand($txt_content)];
3733 + if(trim($txt_content) != '')
3734 + {
3735 + $ai_command_image = $txt_content;
3736 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3737 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3738 + }
3739 + }
3740 + }
3741 + }
3742 + if(empty($ai_command_image))
3743 + {
3744 + aiomatic_log_to_file('Empty API image seed expression provided!');
3745 + }
3746 + else
3747 + {
3748 + if(aiomatic_strlen($ai_command_image) > 2000)
3749 + {
3750 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
3751 + }
3752 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3753 + {
3754 + $api_service = 'Stability.AI';
3755 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
3756 + }
3757 + if($image_size == '256x256')
3758 + {
3759 + $width = '512';
3760 + $height = '512';
3761 + }
3762 + elseif($image_size == '512x512')
3763 + {
3764 + $width = '512';
3765 + $height = '512';
3766 + }
3767 + elseif($image_size == '1024x1024')
3768 + {
3769 + $width = '1024';
3770 + $height = '1024';
3771 + }
3772 + else
3773 + {
3774 + $width = '512';
3775 + $height = '512';
3776 + }
3777 + $ierror = '';
3778 + $temp_get_imgs = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'editorContentStableImage', 0, false, $ierror, false, false, '', 0);
3779 + if($temp_get_imgs !== false)
3780 + {
3781 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3782 + {
3783 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
3784 + }
3785 + $added_images++;
3786 + $added_img_list[] = $temp_get_imgs[1];
3787 + $temp_get_img = $temp_get_imgs[1];
3788 + }
3789 + else
3790 + {
3791 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
3792 + $temp_get_img = '';
3793 + }
3794 + }
3795 + }
3796 + else
3797 + {
3798 + aiomatic_log_to_file('Empty AI image query entered.');
3799 + }
3800 + }
3801 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '6')
3802 + {
3803 + $get_img = '';
3804 + $query_words = $post_title;
3805 + if($image_query == '')
3806 + {
3807 + $image_query = $temp_post;
3808 + }
3809 + $orig_ai_command_image = '';
3810 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
3811 + {
3812 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
3813 + }
3814 + if($orig_ai_command_image == '')
3815 + {
3816 + $orig_ai_command_image = $image_query;
3817 + }
3818 + if($orig_ai_command_image != '')
3819 + {
3820 + $ai_command_image = $orig_ai_command_image;
3821 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
3822 + $ai_command_image = array_filter($ai_command_image);
3823 + if(count($ai_command_image) > 0)
3824 + {
3825 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
3826 + }
3827 + else
3828 + {
3829 + $ai_command_image = '';
3830 + }
3831 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3832 + if(!empty($ai_command_image))
3833 + {
3834 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3835 + }
3836 + else
3837 + {
3838 + $ai_command_image = trim(strip_tags($post_title));
3839 + }
3840 + $ai_command_image = trim($ai_command_image);
3841 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
3842 + {
3843 + $txt_content = aiomatic_get_web_page($ai_command_image);
3844 + if ($txt_content !== FALSE)
3845 + {
3846 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
3847 + $txt_content = array_filter($txt_content);
3848 + if(count($txt_content) > 0)
3849 + {
3850 + $txt_content = $txt_content[array_rand($txt_content)];
3851 + if(trim($txt_content) != '')
3852 + {
3853 + $ai_command_image = $txt_content;
3854 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3855 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3856 + }
3857 + }
3858 + }
3859 + }
3860 + if(empty($ai_command_image))
3861 + {
3862 + aiomatic_log_to_file('Empty API image seed expression provided!');
3863 + }
3864 + else
3865 + {
3866 + if(aiomatic_strlen($ai_command_image) > 2000)
3867 + {
3868 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
3869 + }
3870 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3871 + {
3872 + $api_service = 'Google';
3873 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
3874 + }
3875 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
3876 + {
3877 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
3878 + }
3879 + else
3880 + {
3881 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
3882 + }
3883 + $ierror = '';
3884 +
3885 + $temp_get_imgs = aiomatic_generate_image_google($ai_command_image, $model, 'editorContentGoogleImage', false, $ierror, false);
3886 + if($temp_get_imgs !== false)
3887 + {
3888 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3889 + {
3890 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
3891 + }
3892 + $added_images++;
3893 + $added_img_list[] = $temp_get_imgs[1];
3894 + $temp_get_img = $temp_get_imgs[1];
3895 + }
3896 + else
3897 + {
3898 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
3899 + $temp_get_img = '';
3900 + }
3901 + }
3902 + }
3903 + else
3904 + {
3905 + aiomatic_log_to_file('Empty AI image query entered.');
3906 + }
3907 + }
3908 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '3')
3909 + {
3910 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
3911 + {
3912 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
3913 + }
3914 + else
3915 + {
3916 + $image_size = '1024x1024';
3917 + }
3918 + $get_img = '';
3919 + $query_words = $post_title;
3920 + if($image_query == '')
3921 + {
3922 + $image_query = $temp_post;
3923 + }
3924 + $orig_ai_command_image = '';
3925 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
3926 + {
3927 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
3928 + }
3929 + if($orig_ai_command_image == '')
3930 + {
3931 + $orig_ai_command_image = $image_query;
3932 + }
3933 + if($orig_ai_command_image != '')
3934 + {
3935 + $ai_command_image = $orig_ai_command_image;
3936 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
3937 + $ai_command_image = array_filter($ai_command_image);
3938 + if(count($ai_command_image) > 0)
3939 + {
3940 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
3941 + }
3942 + else
3943 + {
3944 + $ai_command_image = '';
3945 + }
3946 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3947 + if(!empty($ai_command_image))
3948 + {
3949 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3950 + }
3951 + else
3952 + {
3953 + $ai_command_image = trim(strip_tags($post_title));
3954 + }
3955 + $ai_command_image = trim($ai_command_image);
3956 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
3957 + {
3958 + $txt_content = aiomatic_get_web_page($ai_command_image);
3959 + if ($txt_content !== FALSE)
3960 + {
3961 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
3962 + $txt_content = array_filter($txt_content);
3963 + if(count($txt_content) > 0)
3964 + {
3965 + $txt_content = $txt_content[array_rand($txt_content)];
3966 + if(trim($txt_content) != '')
3967 + {
3968 + $ai_command_image = $txt_content;
3969 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
3970 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
3971 + }
3972 + }
3973 + }
3974 + }
3975 + if(empty($ai_command_image))
3976 + {
3977 + aiomatic_log_to_file('Empty API image seed expression provided!');
3978 + }
3979 + else
3980 + {
3981 + if(aiomatic_strlen($ai_command_image) > 2000)
3982 + {
3983 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
3984 + }
3985 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3986 + {
3987 + $api_service = 'GoAPI (Midjourney)';
3988 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
3989 + }
3990 + if($image_size == '256x256')
3991 + {
3992 + $width = '512';
3993 + $height = '512';
3994 + }
3995 + elseif($image_size == '512x512')
3996 + {
3997 + $width = '512';
3998 + $height = '512';
3999 + }
4000 + elseif($image_size == '1024x1024')
4001 + {
4002 + $width = '1024';
4003 + $height = '1024';
4004 + }
4005 + elseif($image_size == '1792x1024')
4006 + {
4007 + $width = '1792';
4008 + $height = '1024';
4009 + }
4010 + elseif($image_size == '1024x1792')
4011 + {
4012 + $width = '1024';
4013 + $height = '1792';
4014 + }
4015 + else
4016 + {
4017 + $width = '512';
4018 + $height = '512';
4019 + }
4020 + $ierror = '';
4021 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'editorContentMidjourneyImage', false, $ierror);
4022 + if($temp_get_imgs !== false)
4023 + {
4024 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4025 + {
4026 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
4027 + }
4028 + $added_images++;
4029 + $added_img_list[] = $temp_get_imgs;
4030 + $temp_get_img = $temp_get_imgs;
4031 + }
4032 + else
4033 + {
4034 + aiomatic_log_to_file('Failed to generate Midjourney image: ' . $ierror);
4035 + $temp_get_img = '';
4036 + }
4037 + }
4038 + }
4039 + else
4040 + {
4041 + aiomatic_log_to_file('Empty AI image query entered.');
4042 + }
4043 + }
4044 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '4')
4045 + {
4046 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
4047 + {
4048 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
4049 + }
4050 + else
4051 + {
4052 + $image_size = '1024x1024';
4053 + }
4054 + $get_img = '';
4055 + $query_words = $post_title;
4056 + if($image_query == '')
4057 + {
4058 + $image_query = $temp_post;
4059 + }
4060 + $orig_ai_command_image = '';
4061 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4062 + {
4063 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
4064 + }
4065 + if($orig_ai_command_image == '')
4066 + {
4067 + $orig_ai_command_image = $image_query;
4068 + }
4069 + if($orig_ai_command_image != '')
4070 + {
4071 + $ai_command_image = $orig_ai_command_image;
4072 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
4073 + $ai_command_image = array_filter($ai_command_image);
4074 + if(count($ai_command_image) > 0)
4075 + {
4076 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
4077 + }
4078 + else
4079 + {
4080 + $ai_command_image = '';
4081 + }
4082 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4083 + if(!empty($ai_command_image))
4084 + {
4085 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4086 + }
4087 + else
4088 + {
4089 + $ai_command_image = trim(strip_tags($post_title));
4090 + }
4091 + $ai_command_image = trim($ai_command_image);
4092 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
4093 + {
4094 + $txt_content = aiomatic_get_web_page($ai_command_image);
4095 + if ($txt_content !== FALSE)
4096 + {
4097 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4098 + $txt_content = array_filter($txt_content);
4099 + if(count($txt_content) > 0)
4100 + {
4101 + $txt_content = $txt_content[array_rand($txt_content)];
4102 + if(trim($txt_content) != '')
4103 + {
4104 + $ai_command_image = $txt_content;
4105 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4106 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4107 + }
4108 + }
4109 + }
4110 + }
4111 + if(empty($ai_command_image))
4112 + {
4113 + aiomatic_log_to_file('Empty API image seed expression provided!');
4114 + }
4115 + else
4116 + {
4117 + if(aiomatic_strlen($ai_command_image) > 2000)
4118 + {
4119 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
4120 + }
4121 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4122 + {
4123 + $api_service = 'Replicate';
4124 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
4125 + }
4126 + if($image_size == '256x256')
4127 + {
4128 + $width = '512';
4129 + $height = '512';
4130 + }
4131 + elseif($image_size == '512x512')
4132 + {
4133 + $width = '512';
4134 + $height = '512';
4135 + }
4136 + elseif($image_size == '1024x1024')
4137 + {
4138 + $width = '1024';
4139 + $height = '1024';
4140 + }
4141 + elseif($image_size == '1792x1024')
4142 + {
4143 + $width = '1792';
4144 + $height = '1024';
4145 + }
4146 + elseif($image_size == '1024x1792')
4147 + {
4148 + $width = '1024';
4149 + $height = '1792';
4150 + }
4151 + else
4152 + {
4153 + $width = '512';
4154 + $height = '512';
4155 + }
4156 + $ierror = '';
4157 + $temp_get_imgs = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'editorContentReplicateImage', false, $ierror);
4158 + if($temp_get_imgs !== false)
4159 + {
4160 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4161 + {
4162 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
4163 + }
4164 + $added_images++;
4165 + $added_img_list[] = $temp_get_imgs;
4166 + $temp_get_img = $temp_get_imgs;
4167 + }
4168 + else
4169 + {
4170 + aiomatic_log_to_file('Failed to generate Replicate image: ' . $ierror);
4171 + $temp_get_img = '';
4172 + }
4173 + }
4174 + }
4175 + else
4176 + {
4177 + aiomatic_log_to_file('Empty AI image query entered.');
4178 + }
4179 + }
4180 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '5')
4181 + {
4182 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
4183 + {
4184 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
4185 + }
4186 + else
4187 + {
4188 + $image_size = '1024x1024';
4189 + }
4190 + $get_img = '';
4191 + $query_words = $post_title;
4192 + if($image_query == '')
4193 + {
4194 + $image_query = $temp_post;
4195 + }
4196 + $orig_ai_command_image = '';
4197 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4198 + {
4199 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
4200 + }
4201 + if($orig_ai_command_image == '')
4202 + {
4203 + $orig_ai_command_image = $image_query;
4204 + }
4205 + if($orig_ai_command_image != '')
4206 + {
4207 + $ai_command_image = $orig_ai_command_image;
4208 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
4209 + $ai_command_image = array_filter($ai_command_image);
4210 + if(count($ai_command_image) > 0)
4211 + {
4212 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
4213 + }
4214 + else
4215 + {
4216 + $ai_command_image = '';
4217 + }
4218 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4219 + if(!empty($ai_command_image))
4220 + {
4221 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4222 + }
4223 + else
4224 + {
4225 + $ai_command_image = trim(strip_tags($post_title));
4226 + }
4227 + $ai_command_image = trim($ai_command_image);
4228 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
4229 + {
4230 + $txt_content = aiomatic_get_web_page($ai_command_image);
4231 + if ($txt_content !== FALSE)
4232 + {
4233 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4234 + $txt_content = array_filter($txt_content);
4235 + if(count($txt_content) > 0)
4236 + {
4237 + $txt_content = $txt_content[array_rand($txt_content)];
4238 + if(trim($txt_content) != '')
4239 + {
4240 + $ai_command_image = $txt_content;
4241 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4242 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4243 + }
4244 + }
4245 + }
4246 + }
4247 + if(empty($ai_command_image))
4248 + {
4249 + aiomatic_log_to_file('Empty API image seed expression provided!');
4250 + }
4251 + else
4252 + {
4253 + if(aiomatic_strlen($ai_command_image) > 2000)
4254 + {
4255 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
4256 + }
4257 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4258 + {
4259 + $api_service = 'Ideogram';
4260 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
4261 + }
4262 + if($image_size == '256x256')
4263 + {
4264 + $width = '512';
4265 + $height = '512';
4266 + }
4267 + elseif($image_size == '512x512')
4268 + {
4269 + $width = '512';
4270 + $height = '512';
4271 + }
4272 + elseif($image_size == '1024x1024')
4273 + {
4274 + $width = '1024';
4275 + $height = '1024';
4276 + }
4277 + elseif($image_size == '1792x1024')
4278 + {
4279 + $width = '1792';
4280 + $height = '1024';
4281 + }
4282 + elseif($image_size == '1024x1792')
4283 + {
4284 + $width = '1024';
4285 + $height = '1792';
4286 + }
4287 + else
4288 + {
4289 + $width = '512';
4290 + $height = '512';
4291 + }
4292 + $ierror = '';
4293 + $temp_get_imgs = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'editorContentIdeogramImage', false, $ierror);
4294 + if($temp_get_imgs !== false)
4295 + {
4296 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4297 + {
4298 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
4299 + }
4300 + $added_images++;
4301 + $added_img_list[] = $temp_get_imgs;
4302 + $temp_get_img = $temp_get_imgs;
4303 + }
4304 + else
4305 + {
4306 + aiomatic_log_to_file('Failed to generate Ideogram image: ' . $ierror);
4307 + $temp_get_img = '';
4308 + }
4309 + }
4310 + }
4311 + else
4312 + {
4313 + aiomatic_log_to_file('Empty AI image query entered.');
4314 + }
4315 + }
4316 + elseif (!isset($aiomatic_Spinner_Settings['enable_ai_images']) || $aiomatic_Spinner_Settings['enable_ai_images'] == '0')
4317 + {
4318 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4319 + {
4320 + $image_query = $aiomatic_Spinner_Settings['ai_image_command'];
4321 + }
4322 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
4323 + {
4324 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
4325 + {
4326 + try
4327 + {
4328 + if(!class_exists('TextRazor'))
4329 + {
4330 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
4331 + }
4332 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
4333 + $textrazor = new TextRazor();
4334 + $textrazor->addExtractor('entities');
4335 + $response = $textrazor->analyze($image_query);
4336 + if (isset($response['response']['entities']))
4337 + {
4338 + foreach ($response['response']['entities'] as $entity)
4339 + {
4340 + $query_words = '';
4341 + if(isset($entity['entityEnglishId']))
4342 + {
4343 + $query_words = $entity['entityEnglishId'];
4344 + }
4345 + else
4346 + {
4347 + $query_words = $entity['entityId'];
4348 + }
4349 + if($query_words != '')
4350 + {
4351 + $attached_id = '';
4352 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
4353 + if(!empty($z_img))
4354 + {
4355 + $added_images++;
4356 + $added_img_list[] = $z_img;
4357 + $temp_get_img = $z_img;
4358 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
4359 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
4360 + }
4361 + break;
4362 + }
4363 + }
4364 + }
4365 + }
4366 + }
4367 + catch(Exception $e)
4368 + {
4369 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
4370 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
4371 + }
4372 + }
4373 + }
4374 + }
4375 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
4376 + {
4377 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
4378 + {
4379 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
4380 + {
4381 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
4382 + }
4383 + else
4384 + {
4385 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
4386 + }
4387 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
4388 + {
4389 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
4390 + }
4391 + else
4392 + {
4393 + $keyword_assistant_id = '';
4394 + }
4395 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
4396 + $title_ai_command = str_replace('%%default_post_cats%%', '', $title_ai_command);
4397 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
4398 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
4399 + $title_ai_command = array_filter($title_ai_command);
4400 + if(count($title_ai_command) > 0)
4401 + {
4402 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
4403 + }
4404 + else
4405 + {
4406 + $title_ai_command = '';
4407 + }
4408 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
4409 + if(!empty($title_ai_command))
4410 + {
4411 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4412 + }
4413 + $title_ai_command = trim($title_ai_command);
4414 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
4415 + {
4416 + $txt_content = aiomatic_get_web_page($title_ai_command);
4417 + if ($txt_content !== FALSE)
4418 + {
4419 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4420 + $txt_content = array_filter($txt_content);
4421 + if(count($txt_content) > 0)
4422 + {
4423 + $txt_content = $txt_content[array_rand($txt_content)];
4424 + if(trim($txt_content) != '')
4425 + {
4426 + $title_ai_command = $txt_content;
4427 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
4428 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4429 + }
4430 + }
4431 + }
4432 + }
4433 + if(empty($title_ai_command))
4434 + {
4435 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
4436 + $title_ai_command = 'Extract a comma separated list of relevant keywords from the text: ' . trim(strip_tags($post_title));
4437 + }
4438 + if(!empty($max_seed_tokens) && aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
4439 + {
4440 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
4441 + }
4442 + $title_ai_command = trim($title_ai_command);
4443 + if(empty($title_ai_command))
4444 + {
4445 + aiomatic_log_to_file('Empty API title seed expression provided(6)! ' . print_r($title_ai_command, true));
4446 + }
4447 + else
4448 + {
4449 + $query_token_count = count(aiomatic_encode($title_ai_command));
4450 + $max_tokens = aimogenpro_get_max_tokens($kw_model);
4451 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
4452 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
4453 + {
4454 + $string_len = aiomatic_strlen($title_ai_command);
4455 + $string_len = $string_len / 2;
4456 + $string_len = intval(0 - $string_len);
4457 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
4458 + $title_ai_command = trim($title_ai_command);
4459 + $query_token_count = count(aiomatic_encode($title_ai_command));
4460 + $available_tokens = $max_tokens - $query_token_count;
4461 + }
4462 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4463 + {
4464 + $api_service = aiomatic_get_api_service($token, $kw_model);
4465 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '\\' . $kw_model . ')(' . $available_tokens . ') for title text1: ' . $title_ai_command);
4466 + }
4467 + $aierror = '';
4468 + $finish_reason = '';
4469 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordCompletion', 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
4470 + if($generated_text === false)
4471 + {
4472 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
4473 + $ai_title = '';
4474 + }
4475 + else
4476 + {
4477 + $ai_title = trim(trim(trim(trim($generated_text), '.'), ' "\''));
4478 + $ai_titles = explode(',', $ai_title);
4479 + foreach($ai_titles as $query_words)
4480 + {
4481 + $attached_id = '';
4482 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, trim($query_words), $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
4483 + if(!empty($z_img))
4484 + {
4485 + $added_images++;
4486 + $added_img_list[] = $z_img;
4487 + $temp_get_img = $z_img;
4488 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
4489 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
4490 + }
4491 + break;
4492 + }
4493 + }
4494 + }
4495 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4496 + {
4497 + $api_service = aiomatic_get_api_service($token, $kw_model);
4498 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
4499 + }
4500 + }
4501 + }
4502 + }
4503 + if(empty($temp_get_img))
4504 + {
4505 + $keyword_class = new Aiomatic_keywords();
4506 + $query_words = $keyword_class->keywords($image_query, 2);
4507 + $temp_img_attr = '';
4508 + $attached_id = '';
4509 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $temp_img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
4510 + if($temp_get_img == '' || $temp_get_img === false)
4511 + {
4512 + $query_words = $keyword_class->keywords($image_query, 1);
4513 + $attached_id = '';
4514 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $temp_img_attr, $attached_id, 20, false, $raw_img_list, array(), $full_result_list);
4515 + if($temp_get_img == '' || $temp_get_img === false)
4516 + {
4517 + $temp_get_img = '';
4518 + }
4519 + else
4520 + {
4521 + if(!in_array($temp_get_img, $added_img_list))
4522 + {
4523 + $added_images++;
4524 + $added_img_list[] = $temp_get_img;
4525 + }
4526 + else
4527 + {
4528 + $temp_get_img = '';
4529 + }
4530 + }
4531 + }
4532 + else
4533 + {
4534 + if(!in_array($temp_get_img, $added_img_list))
4535 + {
4536 + $added_images++;
4537 + $added_img_list[] = $temp_get_img;
4538 + }
4539 + else
4540 + {
4541 + $temp_get_img = '';
4542 + }
4543 + }
4544 + }
4545 + }
4546 + }
4547 + }
4548 + if($temp_get_video != '')
4549 + {
4550 + $add_my_image = $temp_get_video . '<br/>';
4551 + }
4552 + elseif($temp_get_img != '')
4553 + {
4554 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
4555 + {
4556 + $attached_id = '';
4557 + $localpath = aiomatic_copy_image_locally($temp_get_img, $attached_id, '', false, '');
4558 + if($localpath !== false)
4559 + {
4560 + $temp_get_img = $localpath[0];
4561 + }
4562 + }
4563 + $add_my_image = '<img class="ximage_class" src="' . $temp_get_img . '" alt="' . $query_words . '"><br/>';
4564 + }
4565 + if($heading_val == '')
4566 + {
4567 + if($add_my_image == '')
4568 + {
4569 + $add_my_image = ' ';
4570 + }
4571 + $ai_created_data .= $add_my_image . trim(nl2br($aiwriter));
4572 + }
4573 + else
4574 + {
4575 + $ai_created_data .= $add_my_image . $heading_val . ' ' . trim(nl2br($aiwriter)) . '</span>';
4576 + }
4577 + sleep(1);
4578 + $cnt++;
4579 + }
4580 + }
4581 + if($need_more === true)
4582 + {
4583 + $add_my_image = '';
4584 + $temp_get_img = '';
4585 + $temp_get_video = '';
4586 + if(count($heading_results) > 0)
4587 + {
4588 + $rand_heading = '';
4589 + $saverand = array_rand($heading_results);
4590 + $rand_heading = $heading_results[$saverand];
4591 + unset($heading_results[$saverand]);
4592 + if(isset($rand_heading['q']))
4593 + {
4594 + $rand_heading['q'] = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $rand_heading['q']);
4595 + $heading_val = '<h2>' . $rand_heading['q'] . '</h2>' . '<span>' . $rand_heading['a'];
4596 + $image_query = $rand_heading['q'];
4597 + }
4598 + }
4599 + if($images != '' && is_numeric($images) && $images > $added_images)
4600 + {
4601 + if ($videos == 'on' && $rich_embed_location == 'inside')
4602 + {
4603 + if(rand(1, 100) <= 50)
4604 + {
4605 + $temp_get_video = aiomatic_get_video(trim(stripslashes(str_replace('&quot;', '"', $post_title))), '', $social_list);
4606 + }
4607 + }
4608 + if(empty($temp_get_video))
4609 + {
4610 + if($heading_val == '')
4611 + {
4612 + $temp_post = trim($ai_created_data);
4613 + }
4614 + else
4615 + {
4616 + $temp_post = trim($heading_val);
4617 + }
4618 + $query_words = '';
4619 + if($image_query == '')
4620 + {
4621 + $image_query = $temp_post;
4622 + }
4623 + if (isset($aiomatic_Spinner_Settings['enable_ai_images']) && ($aiomatic_Spinner_Settings['enable_ai_images'] == '1' || $aiomatic_Spinner_Settings['enable_ai_images'] == 'on'))
4624 + {
4625 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
4626 + {
4627 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
4628 + }
4629 + else
4630 + {
4631 + $image_size = '1024x1024';
4632 + }
4633 + if (isset($aiomatic_Spinner_Settings['image_model']) && trim($aiomatic_Spinner_Settings['image_model']) != '')
4634 + {
4635 + $image_model = trim($aiomatic_Spinner_Settings['image_model']);
4636 + }
4637 + else
4638 + {
4639 + $image_model = 'dalle2';
4640 + }
4641 + $get_img = '';
4642 + $query_words = $post_title;
4643 + $orig_ai_command_image = '';
4644 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4645 + {
4646 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
4647 + }
4648 + if($orig_ai_command_image == '')
4649 + {
4650 + $orig_ai_command_image = $image_query;
4651 + }
4652 + if($orig_ai_command_image != '')
4653 + {
4654 + $ai_command_image = $orig_ai_command_image;
4655 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
4656 + $ai_command_image = array_filter($ai_command_image);
4657 + if(count($ai_command_image) > 0)
4658 + {
4659 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
4660 + }
4661 + else
4662 + {
4663 + $ai_command_image = '';
4664 + }
4665 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4666 + if(!empty($ai_command_image))
4667 + {
4668 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4669 + }
4670 + else
4671 + {
4672 + $ai_command_image = trim(strip_tags($post_title));
4673 + }
4674 + $ai_command_image = trim($ai_command_image);
4675 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
4676 + {
4677 + $txt_content = aiomatic_get_web_page($ai_command_image);
4678 + if ($txt_content !== FALSE)
4679 + {
4680 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4681 + $txt_content = array_filter($txt_content);
4682 + if(count($txt_content) > 0)
4683 + {
4684 + $txt_content = $txt_content[array_rand($txt_content)];
4685 + if(trim($txt_content) != '')
4686 + {
4687 + $ai_command_image = $txt_content;
4688 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4689 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4690 + }
4691 + }
4692 + }
4693 + }
4694 + if(empty($ai_command_image))
4695 + {
4696 + aiomatic_log_to_file('Empty API image seed expression provided!');
4697 + }
4698 + else
4699 + {
4700 + if(aiomatic_strlen($ai_command_image) > 400)
4701 + {
4702 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 400);
4703 + }
4704 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4705 + {
4706 + $api_service = aiomatic_get_api_service($token, $image_model);
4707 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
4708 + }
4709 + $aierror = '';
4710 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'editContentImage', false, 0, $aierror, $image_model, $post_title);
4711 + if($get_img !== false)
4712 + {
4713 + foreach($get_img as $tmpimg)
4714 + {
4715 + $added_images++;
4716 + $added_img_list[] = $tmpimg;
4717 + $temp_get_img = $tmpimg;
4718 + }
4719 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4720 + {
4721 + aiomatic_log_to_file('AI generated image returned: ' . $tmpimg);
4722 + }
4723 + }
4724 + else
4725 + {
4726 + aiomatic_log_to_file('Failed to generate AI image: ' . $aierror);
4727 + $get_img = '';
4728 + }
4729 + }
4730 + }
4731 + else
4732 + {
4733 + aiomatic_log_to_file('Empty AI image query entered.');
4734 + }
4735 + }
4736 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '2')
4737 + {
4738 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
4739 + {
4740 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
4741 + }
4742 + else
4743 + {
4744 + $image_size = '1024x1024';
4745 + }
4746 + $get_img = '';
4747 + $query_words = $post_title;
4748 + if($image_query == '')
4749 + {
4750 + $image_query = $temp_post;
4751 + }
4752 + $orig_ai_command_image = '';
4753 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4754 + {
4755 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
4756 + }
4757 + if($orig_ai_command_image == '')
4758 + {
4759 + $orig_ai_command_image = $image_query;
4760 + }
4761 + if($orig_ai_command_image != '')
4762 + {
4763 + $ai_command_image = $orig_ai_command_image;
4764 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
4765 + $ai_command_image = array_filter($ai_command_image);
4766 + if(count($ai_command_image) > 0)
4767 + {
4768 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
4769 + }
4770 + else
4771 + {
4772 + $ai_command_image = '';
4773 + }
4774 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4775 + if(!empty($ai_command_image))
4776 + {
4777 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4778 + }
4779 + else
4780 + {
4781 + $ai_command_image = trim(strip_tags($post_title));
4782 + }
4783 + $ai_command_image = trim($ai_command_image);
4784 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
4785 + {
4786 + $txt_content = aiomatic_get_web_page($ai_command_image);
4787 + if ($txt_content !== FALSE)
4788 + {
4789 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4790 + $txt_content = array_filter($txt_content);
4791 + if(count($txt_content) > 0)
4792 + {
4793 + $txt_content = $txt_content[array_rand($txt_content)];
4794 + if(trim($txt_content) != '')
4795 + {
4796 + $ai_command_image = $txt_content;
4797 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4798 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4799 + }
4800 + }
4801 + }
4802 + }
4803 + if(empty($ai_command_image))
4804 + {
4805 + aiomatic_log_to_file('Empty API image seed expression provided!');
4806 + }
4807 + else
4808 + {
4809 + if(aiomatic_strlen($ai_command_image) > 2000)
4810 + {
4811 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
4812 + }
4813 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4814 + {
4815 + $api_service = 'Stability.AI';
4816 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
4817 + }
4818 + if($image_size == '256x256')
4819 + {
4820 + $width = '512';
4821 + $height = '512';
4822 + }
4823 + elseif($image_size == '512x512')
4824 + {
4825 + $width = '512';
4826 + $height = '512';
4827 + }
4828 + elseif($image_size == '1024x1024')
4829 + {
4830 + $width = '1024';
4831 + $height = '1024';
4832 + }
4833 + else
4834 + {
4835 + $width = '512';
4836 + $height = '512';
4837 + }
4838 + $ierror = '';
4839 + $temp_get_imgs = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'editorContentStableImage', 0, false, $ierror, false, false, '', 0);
4840 + if($temp_get_imgs !== false)
4841 + {
4842 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4843 + {
4844 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
4845 + }
4846 + $added_images++;
4847 + $added_img_list[] = $temp_get_imgs[1];
4848 + $temp_get_img = $temp_get_imgs[1];
4849 + }
4850 + else
4851 + {
4852 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
4853 + $temp_get_img = '';
4854 + }
4855 + }
4856 + }
4857 + else
4858 + {
4859 + aiomatic_log_to_file('Empty AI image query entered.');
4860 + }
4861 + }
4862 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '3')
4863 + {
4864 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
4865 + {
4866 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
4867 + }
4868 + else
4869 + {
4870 + $image_size = '1024x1024';
4871 + }
4872 + $get_img = '';
4873 + $query_words = $post_title;
4874 + if($image_query == '')
4875 + {
4876 + $image_query = $temp_post;
4877 + }
4878 + $orig_ai_command_image = '';
4879 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
4880 + {
4881 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
4882 + }
4883 + if($orig_ai_command_image == '')
4884 + {
4885 + $orig_ai_command_image = $image_query;
4886 + }
4887 + if($orig_ai_command_image != '')
4888 + {
4889 + $ai_command_image = $orig_ai_command_image;
4890 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
4891 + $ai_command_image = array_filter($ai_command_image);
4892 + if(count($ai_command_image) > 0)
4893 + {
4894 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
4895 + }
4896 + else
4897 + {
4898 + $ai_command_image = '';
4899 + }
4900 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4901 + if(!empty($ai_command_image))
4902 + {
4903 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4904 + }
4905 + else
4906 + {
4907 + $ai_command_image = trim(strip_tags($post_title));
4908 + }
4909 + $ai_command_image = trim($ai_command_image);
4910 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
4911 + {
4912 + $txt_content = aiomatic_get_web_page($ai_command_image);
4913 + if ($txt_content !== FALSE)
4914 + {
4915 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4916 + $txt_content = array_filter($txt_content);
4917 + if(count($txt_content) > 0)
4918 + {
4919 + $txt_content = $txt_content[array_rand($txt_content)];
4920 + if(trim($txt_content) != '')
4921 + {
4922 + $ai_command_image = $txt_content;
4923 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
4924 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
4925 + }
4926 + }
4927 + }
4928 + }
4929 + if(empty($ai_command_image))
4930 + {
4931 + aiomatic_log_to_file('Empty API image seed expression provided!');
4932 + }
4933 + else
4934 + {
4935 + if(aiomatic_strlen($ai_command_image) > 2000)
4936 + {
4937 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
4938 + }
4939 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4940 + {
4941 + $api_service = 'GoAPI (Midjourney)';
4942 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
4943 + }
4944 + if($image_size == '256x256')
4945 + {
4946 + $width = '512';
4947 + $height = '512';
4948 + }
4949 + elseif($image_size == '512x512')
4950 + {
4951 + $width = '512';
4952 + $height = '512';
4953 + }
4954 + elseif($image_size == '1024x1024')
4955 + {
4956 + $width = '1024';
4957 + $height = '1024';
4958 + }
4959 + elseif($image_size == '1792x1024')
4960 + {
4961 + $width = '1792';
4962 + $height = '1024';
4963 + }
4964 + elseif($image_size == '1024x1792')
4965 + {
4966 + $width = '1024';
4967 + $height = '1792';
4968 + }
4969 + else
4970 + {
4971 + $width = '512';
4972 + $height = '512';
4973 + }
4974 + $ierror = '';
4975 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'editorContentMidjourneyImage', false, $ierror);
4976 + if($temp_get_imgs !== false)
4977 + {
4978 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
4979 + {
4980 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
4981 + }
4982 + $added_images++;
4983 + $added_img_list[] = $temp_get_imgs;
4984 + $temp_get_img = $temp_get_imgs;
4985 + }
4986 + else
4987 + {
4988 + aiomatic_log_to_file('Failed to generate Midjourney image: ' . $ierror);
4989 + $temp_get_img = '';
4990 + }
4991 + }
4992 + }
4993 + else
4994 + {
4995 + aiomatic_log_to_file('Empty AI image query entered.');
4996 + }
4997 + }
4998 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '6')
4999 + {
5000 + $get_img = '';
5001 + $query_words = $post_title;
5002 + if($image_query == '')
5003 + {
5004 + $image_query = $temp_post;
5005 + }
5006 + $orig_ai_command_image = '';
5007 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
5008 + {
5009 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
5010 + }
5011 + if($orig_ai_command_image == '')
5012 + {
5013 + $orig_ai_command_image = $image_query;
5014 + }
5015 + if($orig_ai_command_image != '')
5016 + {
5017 + $ai_command_image = $orig_ai_command_image;
5018 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5019 + $ai_command_image = array_filter($ai_command_image);
5020 + if(count($ai_command_image) > 0)
5021 + {
5022 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5023 + }
5024 + else
5025 + {
5026 + $ai_command_image = '';
5027 + }
5028 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5029 + if(!empty($ai_command_image))
5030 + {
5031 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5032 + }
5033 + else
5034 + {
5035 + $ai_command_image = trim(strip_tags($post_title));
5036 + }
5037 + $ai_command_image = trim($ai_command_image);
5038 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5039 + {
5040 + $txt_content = aiomatic_get_web_page($ai_command_image);
5041 + if ($txt_content !== FALSE)
5042 + {
5043 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5044 + $txt_content = array_filter($txt_content);
5045 + if(count($txt_content) > 0)
5046 + {
5047 + $txt_content = $txt_content[array_rand($txt_content)];
5048 + if(trim($txt_content) != '')
5049 + {
5050 + $ai_command_image = $txt_content;
5051 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5052 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5053 + }
5054 + }
5055 + }
5056 + }
5057 + if(empty($ai_command_image))
5058 + {
5059 + aiomatic_log_to_file('Empty API image seed expression provided!');
5060 + }
5061 + else
5062 + {
5063 + if(aiomatic_strlen($ai_command_image) > 2000)
5064 + {
5065 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
5066 + }
5067 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5068 + {
5069 + $api_service = 'Google';
5070 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
5071 + }
5072 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
5073 + {
5074 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
5075 + }
5076 + else
5077 + {
5078 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
5079 + }
5080 + $ierror = '';
5081 +
5082 + $temp_get_imgs = aiomatic_generate_image_google($ai_command_image, $model, 'editorContentGoogleImage', false, $ierror, false);
5083 + if($temp_get_imgs !== false)
5084 + {
5085 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5086 + {
5087 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
5088 + }
5089 + $added_images++;
5090 + $added_img_list[] = $temp_get_imgs[1];
5091 + $temp_get_img = $temp_get_imgs[1];
5092 + }
5093 + else
5094 + {
5095 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
5096 + $temp_get_img = '';
5097 + }
5098 + }
5099 + }
5100 + else
5101 + {
5102 + aiomatic_log_to_file('Empty AI image query entered.');
5103 + }
5104 + }
5105 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '4')
5106 + {
5107 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
5108 + {
5109 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
5110 + }
5111 + else
5112 + {
5113 + $image_size = '1024x1024';
5114 + }
5115 + $get_img = '';
5116 + $query_words = $post_title;
5117 + if($image_query == '')
5118 + {
5119 + $image_query = $temp_post;
5120 + }
5121 + $orig_ai_command_image = '';
5122 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
5123 + {
5124 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
5125 + }
5126 + if($orig_ai_command_image == '')
5127 + {
5128 + $orig_ai_command_image = $image_query;
5129 + }
5130 + if($orig_ai_command_image != '')
5131 + {
5132 + $ai_command_image = $orig_ai_command_image;
5133 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5134 + $ai_command_image = array_filter($ai_command_image);
5135 + if(count($ai_command_image) > 0)
5136 + {
5137 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5138 + }
5139 + else
5140 + {
5141 + $ai_command_image = '';
5142 + }
5143 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5144 + if(!empty($ai_command_image))
5145 + {
5146 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5147 + }
5148 + else
5149 + {
5150 + $ai_command_image = trim(strip_tags($post_title));
5151 + }
5152 + $ai_command_image = trim($ai_command_image);
5153 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5154 + {
5155 + $txt_content = aiomatic_get_web_page($ai_command_image);
5156 + if ($txt_content !== FALSE)
5157 + {
5158 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5159 + $txt_content = array_filter($txt_content);
5160 + if(count($txt_content) > 0)
5161 + {
5162 + $txt_content = $txt_content[array_rand($txt_content)];
5163 + if(trim($txt_content) != '')
5164 + {
5165 + $ai_command_image = $txt_content;
5166 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5167 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5168 + }
5169 + }
5170 + }
5171 + }
5172 + if(empty($ai_command_image))
5173 + {
5174 + aiomatic_log_to_file('Empty API image seed expression provided!');
5175 + }
5176 + else
5177 + {
5178 + if(aiomatic_strlen($ai_command_image) > 2000)
5179 + {
5180 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
5181 + }
5182 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5183 + {
5184 + $api_service = 'Replicate';
5185 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
5186 + }
5187 + if($image_size == '256x256')
5188 + {
5189 + $width = '512';
5190 + $height = '512';
5191 + }
5192 + elseif($image_size == '512x512')
5193 + {
5194 + $width = '512';
5195 + $height = '512';
5196 + }
5197 + elseif($image_size == '1024x1024')
5198 + {
5199 + $width = '1024';
5200 + $height = '1024';
5201 + }
5202 + elseif($image_size == '1792x1024')
5203 + {
5204 + $width = '1792';
5205 + $height = '1024';
5206 + }
5207 + elseif($image_size == '1024x1792')
5208 + {
5209 + $width = '1024';
5210 + $height = '1792';
5211 + }
5212 + else
5213 + {
5214 + $width = '512';
5215 + $height = '512';
5216 + }
5217 + $ierror = '';
5218 + $temp_get_imgs = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'editorContentReplicateImage', false, $ierror);
5219 + if($temp_get_imgs !== false)
5220 + {
5221 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5222 + {
5223 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
5224 + }
5225 + $added_images++;
5226 + $added_img_list[] = $temp_get_imgs;
5227 + $temp_get_img = $temp_get_imgs;
5228 + }
5229 + else
5230 + {
5231 + aiomatic_log_to_file('Failed to generate Replicate image: ' . $ierror);
5232 + $temp_get_img = '';
5233 + }
5234 + }
5235 + }
5236 + else
5237 + {
5238 + aiomatic_log_to_file('Empty AI image query entered.');
5239 + }
5240 + }
5241 + elseif (isset($aiomatic_Spinner_Settings['enable_ai_images']) && $aiomatic_Spinner_Settings['enable_ai_images'] == '5')
5242 + {
5243 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
5244 + {
5245 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
5246 + }
5247 + else
5248 + {
5249 + $image_size = '1024x1024';
5250 + }
5251 + $get_img = '';
5252 + $query_words = $post_title;
5253 + if($image_query == '')
5254 + {
5255 + $image_query = $temp_post;
5256 + }
5257 + $orig_ai_command_image = '';
5258 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
5259 + {
5260 + $orig_ai_command_image = $aiomatic_Spinner_Settings['ai_image_command'];
5261 + }
5262 + if($orig_ai_command_image == '')
5263 + {
5264 + $orig_ai_command_image = $image_query;
5265 + }
5266 + if($orig_ai_command_image != '')
5267 + {
5268 + $ai_command_image = $orig_ai_command_image;
5269 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5270 + $ai_command_image = array_filter($ai_command_image);
5271 + if(count($ai_command_image) > 0)
5272 + {
5273 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5274 + }
5275 + else
5276 + {
5277 + $ai_command_image = '';
5278 + }
5279 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5280 + if(!empty($ai_command_image))
5281 + {
5282 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5283 + }
5284 + else
5285 + {
5286 + $ai_command_image = trim(strip_tags($post_title));
5287 + }
5288 + $ai_command_image = trim($ai_command_image);
5289 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5290 + {
5291 + $txt_content = aiomatic_get_web_page($ai_command_image);
5292 + if ($txt_content !== FALSE)
5293 + {
5294 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5295 + $txt_content = array_filter($txt_content);
5296 + if(count($txt_content) > 0)
5297 + {
5298 + $txt_content = $txt_content[array_rand($txt_content)];
5299 + if(trim($txt_content) != '')
5300 + {
5301 + $ai_command_image = $txt_content;
5302 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5303 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5304 + }
5305 + }
5306 + }
5307 + }
5308 + if(empty($ai_command_image))
5309 + {
5310 + aiomatic_log_to_file('Empty API image seed expression provided!');
5311 + }
5312 + else
5313 + {
5314 + if(aiomatic_strlen($ai_command_image) > 2000)
5315 + {
5316 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
5317 + }
5318 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5319 + {
5320 + $api_service = 'Ideogram';
5321 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
5322 + }
5323 + if($image_size == '256x256')
5324 + {
5325 + $width = '512';
5326 + $height = '512';
5327 + }
5328 + elseif($image_size == '512x512')
5329 + {
5330 + $width = '512';
5331 + $height = '512';
5332 + }
5333 + elseif($image_size == '1024x1024')
5334 + {
5335 + $width = '1024';
5336 + $height = '1024';
5337 + }
5338 + elseif($image_size == '1792x1024')
5339 + {
5340 + $width = '1792';
5341 + $height = '1024';
5342 + }
5343 + elseif($image_size == '1024x1792')
5344 + {
5345 + $width = '1024';
5346 + $height = '1792';
5347 + }
5348 + else
5349 + {
5350 + $width = '512';
5351 + $height = '512';
5352 + }
5353 + $ierror = '';
5354 + $temp_get_imgs = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'editorContentIdeogramImage', false, $ierror);
5355 + if($temp_get_imgs !== false)
5356 + {
5357 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5358 + {
5359 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
5360 + }
5361 + $added_images++;
5362 + $added_img_list[] = $temp_get_imgs;
5363 + $temp_get_img = $temp_get_imgs;
5364 + }
5365 + else
5366 + {
5367 + aiomatic_log_to_file('Failed to generate Ideogram image: ' . $ierror);
5368 + $temp_get_img = '';
5369 + }
5370 + }
5371 + }
5372 + else
5373 + {
5374 + aiomatic_log_to_file('Empty AI image query entered.');
5375 + }
5376 + }
5377 + elseif (!isset($aiomatic_Spinner_Settings['enable_ai_images']) || $aiomatic_Spinner_Settings['enable_ai_images'] == '0')
5378 + {
5379 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
5380 + {
5381 + $image_query = $aiomatic_Spinner_Settings['ai_image_command'];
5382 + }
5383 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
5384 + {
5385 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
5386 + {
5387 + try
5388 + {
5389 + if(!class_exists('TextRazor'))
5390 + {
5391 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
5392 + }
5393 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
5394 + $textrazor = new TextRazor();
5395 + $textrazor->addExtractor('entities');
5396 + $response = $textrazor->analyze($image_query);
5397 + if (isset($response['response']['entities']))
5398 + {
5399 + foreach ($response['response']['entities'] as $entity)
5400 + {
5401 + $query_words = '';
5402 + if(isset($entity['entityEnglishId']))
5403 + {
5404 + $query_words = $entity['entityEnglishId'];
5405 + }
5406 + else
5407 + {
5408 + $query_words = $entity['entityId'];
5409 + }
5410 + if($query_words != '')
5411 + {
5412 + $attached_id = '';
5413 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
5414 + if(!empty($z_img))
5415 + {
5416 + $added_images++;
5417 + $added_img_list[] = $z_img;
5418 + $temp_get_img = $z_img;
5419 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
5420 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
5421 + }
5422 + break;
5423 + }
5424 + }
5425 + }
5426 + }
5427 + }
5428 + catch(Exception $e)
5429 + {
5430 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
5431 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
5432 + }
5433 + }
5434 + }
5435 + }
5436 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
5437 + {
5438 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
5439 + {
5440 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
5441 + {
5442 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
5443 + }
5444 + else
5445 + {
5446 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
5447 + }
5448 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
5449 + {
5450 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
5451 + }
5452 + else
5453 + {
5454 + $keyword_assistant_id = '';
5455 + }
5456 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
5457 + $title_ai_command = str_replace('%%default_post_cats%%', '', $title_ai_command);
5458 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
5459 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
5460 + $title_ai_command = array_filter($title_ai_command);
5461 + if(count($title_ai_command) > 0)
5462 + {
5463 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
5464 + }
5465 + else
5466 + {
5467 + $title_ai_command = '';
5468 + }
5469 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
5470 + if(!empty($title_ai_command))
5471 + {
5472 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5473 + }
5474 + $title_ai_command = trim($title_ai_command);
5475 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
5476 + {
5477 + $txt_content = aiomatic_get_web_page($title_ai_command);
5478 + if ($txt_content !== FALSE)
5479 + {
5480 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5481 + $txt_content = array_filter($txt_content);
5482 + if(count($txt_content) > 0)
5483 + {
5484 + $txt_content = $txt_content[array_rand($txt_content)];
5485 + if(trim($txt_content) != '')
5486 + {
5487 + $title_ai_command = $txt_content;
5488 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
5489 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
5490 + }
5491 + }
5492 + }
5493 + }
5494 + if(empty($title_ai_command))
5495 + {
5496 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
5497 + $title_ai_command = 'Extract a comma separated list of relevant keywords from the text: ' . trim(strip_tags($post_title));
5498 + }
5499 + if(!empty($max_seed_tokens) && aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
5500 + {
5501 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
5502 + }
5503 + $title_ai_command = trim($title_ai_command);
5504 + if(empty($title_ai_command))
5505 + {
5506 + aiomatic_log_to_file('Empty API title seed expression provided(7)! ' . print_r($title_ai_command, true));
5507 + }
5508 + else
5509 + {
5510 + $query_token_count = count(aiomatic_encode($title_ai_command));
5511 + $max_tokens = aimogenpro_get_max_tokens($kw_model);
5512 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
5513 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
5514 + {
5515 + $string_len = aiomatic_strlen($title_ai_command);
5516 + $string_len = $string_len / 2;
5517 + $string_len = intval(0 - $string_len);
5518 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
5519 + $title_ai_command = trim($title_ai_command);
5520 + $query_token_count = count(aiomatic_encode($title_ai_command));
5521 + $available_tokens = $max_tokens - $query_token_count;
5522 + }
5523 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5524 + {
5525 + $api_service = aiomatic_get_api_service($token, $kw_model);
5526 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '\\' . $kw_model . ')(' . $available_tokens . ') for title text2: ' . $title_ai_command);
5527 + }
5528 + $aierror = '';
5529 + $finish_reason = '';
5530 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordCompletion', 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
5531 + if($generated_text === false)
5532 + {
5533 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
5534 + $ai_title = '';
5535 + }
5536 + else
5537 + {
5538 + $ai_title = trim(trim(trim(trim($generated_text), '.'), ' "\''));
5539 + $ai_titles = explode(',', $ai_title);
5540 + foreach($ai_titles as $query_words)
5541 + {
5542 + $attached_id = '';
5543 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, trim($query_words), $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
5544 + if(!empty($z_img))
5545 + {
5546 + $added_images++;
5547 + $added_img_list[] = $z_img;
5548 + $temp_get_img = $z_img;
5549 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
5550 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
5551 + }
5552 + break;
5553 + }
5554 + }
5555 + }
5556 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5557 + {
5558 + $api_service = aiomatic_get_api_service($token, $kw_model);
5559 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
5560 + }
5561 + }
5562 + }
5563 + }
5564 + if(empty($temp_get_img))
5565 + {
5566 + $keyword_class = new Aiomatic_keywords();
5567 + $query_words = $keyword_class->keywords($image_query, 2);
5568 + $temp_img_attr = '';
5569 + $attached_id = '';
5570 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $temp_img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
5571 + if($temp_get_img == '' || $temp_get_img === false)
5572 + {
5573 + $query_words = $keyword_class->keywords($image_query, 1);
5574 + $attached_id = '';
5575 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $temp_img_attr, $attached_id, 20, false, $raw_img_list, array(), $full_result_list);
5576 + if($temp_get_img == '' || $temp_get_img === false)
5577 + {
5578 + $temp_get_img = '';
5579 + }
5580 + else
5581 + {
5582 + if(!in_array($temp_get_img, $added_img_list))
5583 + {
5584 + $added_images++;
5585 + $added_img_list[] = $temp_get_img;
5586 + }
5587 + else
5588 + {
5589 + $temp_get_img = '';
5590 + }
5591 + }
5592 + }
5593 + else
5594 + {
5595 + if(!in_array($temp_get_img, $added_img_list))
5596 + {
5597 + $added_images++;
5598 + $added_img_list[] = $temp_get_img;
5599 + }
5600 + else
5601 + {
5602 + $temp_get_img = '';
5603 + }
5604 + }
5605 + }
5606 + }
5607 + }
5608 + }
5609 + if($heading_val != '')
5610 + {
5611 + $ai_created_data = $heading_val . ' ' . $ai_created_data;
5612 + }
5613 + if($temp_get_video != '')
5614 + {
5615 + $ai_created_data = $temp_get_video . ' ' . $ai_created_data;
5616 + }
5617 + elseif($temp_get_img != '')
5618 + {
5619 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
5620 + {
5621 + $attached_id = '';
5622 + $localpath = aiomatic_copy_image_locally($temp_get_img, $attached_id, '', false, '');
5623 + if($localpath !== false)
5624 + {
5625 + $temp_get_img = $localpath[0];
5626 + }
5627 + }
5628 + $ai_created_data = '<img class="ximage_class" src="' . $temp_get_img . '" alt="' . $query_words . '">' . ' ' . $ai_created_data;
5629 + }
5630 + }
5631 + }
5632 + if ($videos == 'on' && $rich_embed_location != 'inside' && !empty($rich_embed_location) && $ai_created_data != false)
5633 + {
5634 + $new_vid = aiomatic_get_video(trim(stripslashes(str_replace('&quot;', '"', $post_title))), '', $social_list);
5635 + if($new_vid !== false)
5636 + {
5637 + if($rich_embed_location == 'bottom' || empty($rich_embed_location))
5638 + {
5639 + $ai_created_data .= $new_vid;
5640 + }
5641 + elseif($rich_embed_location == 'top')
5642 + {
5643 + $ai_created_data = $new_vid . ' ' . $ai_created_data;
5644 + }
5645 + }
5646 + }
5647 + $final_excerpt = $post->post_excerpt;
5648 + $final_title = $post->post_title;
5649 + if($ai_created_data != false && $ai_created_data != '')
5650 + {
5651 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
5652 + {
5653 + if (!isset($aiomatic_Main_Settings['no_undetectibility_editor']) || $aiomatic_Main_Settings['no_undetectibility_editor'] != 'on')
5654 + {
5655 + $rand_percentage = rand(10, 20);
5656 + if(!isset($xchars))
5657 + {
5658 + $xchars = array();
5659 + }
5660 + $ai_created_data = aiomatic_make_unique($ai_created_data, $xchars, $rand_percentage);
5661 + }
5662 + }
5663 + if (isset($aiomatic_Spinner_Settings['preppend_add']) && $aiomatic_Spinner_Settings['preppend_add'] != '')
5664 + {
5665 + $preppend_add = aiomatic_replaceAIPostShortcodes($aiomatic_Spinner_Settings['preppend_add'], $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
5666 + $ai_created_data = $preppend_add . $ai_created_data;
5667 + }
5668 + if (isset($aiomatic_Spinner_Settings['append_add']) && $aiomatic_Spinner_Settings['append_add'] != '')
5669 + {
5670 + $append_add = aiomatic_replaceAIPostShortcodes($aiomatic_Spinner_Settings['append_add'], $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
5671 + $ai_created_data = $ai_created_data . $append_add;
5672 + }
5673 + if (isset($aiomatic_Spinner_Settings['append_spintax']) && $aiomatic_Spinner_Settings['append_spintax'] == 'append')
5674 + {
5675 + if (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'title')
5676 + {
5677 + $final_title = $final_title . ' ' . $ai_created_data;
5678 + }
5679 + elseif (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'excerpt')
5680 + {
5681 + $final_excerpt = $final_excerpt . ' ' . $ai_created_data;
5682 + }
5683 + else
5684 + {
5685 + $final_content = $final_content . ' <br/> ' . $ai_created_data;
5686 + }
5687 + $updated = true;
5688 + }
5689 + elseif (isset($aiomatic_Spinner_Settings['append_spintax']) && $aiomatic_Spinner_Settings['append_spintax'] == 'preppend')
5690 + {
5691 + if (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'title')
5692 + {
5693 + $final_title = $ai_created_data . ' ' . $final_title;
5694 + }
5695 + elseif (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'excerpt')
5696 + {
5697 + $final_excerpt = $ai_created_data . ' ' . $final_excerpt;
5698 + }
5699 + else
5700 + {
5701 + $final_content = $ai_created_data . ' <br/> ' . $final_content;
5702 + }
5703 + $updated = true;
5704 + }
5705 + elseif (isset($aiomatic_Spinner_Settings['append_spintax']) && $aiomatic_Spinner_Settings['append_spintax'] == 'inside')
5706 + {
5707 + if (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'title')
5708 + {
5709 + $final_title = aiomatic_insert_ai_content($final_title, $ai_created_data);
5710 + }
5711 + elseif (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'excerpt')
5712 + {
5713 + $final_excerpt = aiomatic_insert_ai_content($final_excerpt, $ai_created_data);
5714 + }
5715 + else
5716 + {
5717 + $final_content = aiomatic_insert_ai_content($final_content, $ai_created_data);
5718 + }
5719 + $updated = true;
5720 + }
5721 + }
5722 + if($updated == true)
5723 + {
5724 + $args = array();
5725 + $args['ID'] = $post->ID;
5726 + if (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'title')
5727 + {
5728 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
5729 + {
5730 + require_once(dirname(__FILE__) . "/res/swear.php");
5731 + $final_title = aimogenpro_filterwords($final_title);
5732 + }
5733 + $args['post_title'] = aiomatic_truncate_title($final_title);
5734 + }
5735 + elseif (isset($aiomatic_Spinner_Settings['append_location']) && $aiomatic_Spinner_Settings['append_location'] == 'excerpt')
5736 + {
5737 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
5738 + {
5739 + require_once(dirname(__FILE__) . "/res/swear.php");
5740 + $final_excerpt = aimogenpro_filterwords($final_excerpt);
5741 + }
5742 + $args['post_excerpt'] = $final_excerpt;
5743 + }
5744 + else
5745 + {
5746 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
5747 + {
5748 + require_once(dirname(__FILE__) . "/res/swear.php");
5749 + $final_content = aimogenpro_filterwords($final_content);
5750 + }
5751 + $args['post_content'] = $final_content;
5752 + }
5753 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
5754 + {
5755 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
5756 + }
5757 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
5758 + {
5759 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
5760 + if($date_stamp)
5761 + {
5762 + $postdate = date("Y-m-d H:i:s", $date_stamp);
5763 + $args['post_date'] = $postdate;
5764 + }
5765 + }
5766 + remove_filter('content_save_pre', 'wp_filter_post_kses');
5767 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
5768 + remove_filter('title_save_pre', 'wp_filter_kses');
5769 + update_post_meta($post->ID, $custom_name, "pub");
5770 + $post_updated = wp_update_post($args);
5771 + $changes_made = true;
5772 + add_filter('content_save_pre', 'wp_filter_post_kses');
5773 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
5774 + add_filter('title_save_pre', 'wp_filter_kses');
5775 + if (is_wp_error($post_updated)) {
5776 + $errors = $post_updated->get_error_messages();
5777 + foreach ($errors as $error) {
5778 + aiomatic_log_to_file('Error occured while updating post for AI content "' . $post->post_title . '": ' . $error);
5779 + }
5780 + }
5781 + else
5782 + {
5783 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
5784 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated content.');
5785 + }
5786 + }
5787 + }
5788 + }
5789 + if (isset($aiomatic_Spinner_Settings['add_links']) && $aiomatic_Spinner_Settings['add_links'] != '' && $aiomatic_Spinner_Settings['add_links'] != 'disabled')
5790 + {
5791 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5792 + {
5793 + aiomatic_log_to_file('Starting post auto linker...');
5794 + }
5795 + if(!function_exists('is_plugin_active'))
5796 + {
5797 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
5798 + }
5799 + if (isset($aiomatic_Spinner_Settings['link_method']) && $aiomatic_Spinner_Settings['link_method'] == 'linkjuicer' && (is_plugin_active('internal-links/wp-internal-linkjuicer.php') || is_plugin_active('internal-links-premium/wp-internal-linkjuicer.php')))
5800 + {
5801 + $vision_file = '';
5802 + if (isset($aiomatic_Spinner_Settings['ai_vision_link_juicer']) && $aiomatic_Spinner_Settings['ai_vision_link_juicer'] == 'on')
5803 + {
5804 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
5805 + if($avatar !== false)
5806 + {
5807 + $vision_file = $avatar;
5808 + }
5809 + }
5810 + if (isset($aiomatic_Spinner_Settings['link_juicer_prompt']) && $aiomatic_Spinner_Settings['link_juicer_prompt'] != '')
5811 + {
5812 + $link_juicer_prompt = $aiomatic_Spinner_Settings['link_juicer_prompt'];
5813 + }
5814 + else
5815 + {
5816 + $link_juicer_prompt = 'Generate a comma-separated list of relevant keywords for the post title (for use in the Link Juicer plugin): "%%post_title%%".';
5817 + }
5818 + $link_juicer_prompt = aiomatic_replaceSynergyShortcodes($link_juicer_prompt);
5819 +
5820 + $post_link = get_permalink($post->ID);
5821 + $blog_title = html_entity_decode(get_bloginfo('title'));
5822 + $author_obj = get_user_by('id', $post->post_author);
5823 + if($author_obj !== false && isset($author_obj->user_nicename))
5824 + {
5825 + $user_name = $author_obj->user_nicename;
5826 + }
5827 + else
5828 + {
5829 + $user_name = '';
5830 + }
5831 + $featured_image = '';
5832 + wp_suspend_cache_addition(true);
5833 + $metas = get_post_custom($post->ID);
5834 + wp_suspend_cache_addition(false);
5835 + if(is_array($metas))
5836 + {
5837 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
5838 + }
5839 + else
5840 + {
5841 + $rez_meta = array();
5842 + }
5843 + if(count($rez_meta) > 0)
5844 + {
5845 + foreach($rez_meta as $rm)
5846 + {
5847 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
5848 + {
5849 + $featured_image = $rm[0];
5850 + break;
5851 + }
5852 + }
5853 + }
5854 + if($featured_image == '')
5855 + {
5856 + $featured_image = aiomatic_generate_thumbmail($post->ID);
5857 + }
5858 + if($featured_image == '' && $final_content != '')
5859 + {
5860 + $dom = new DOMDocument();
5861 + $internalErrors = libxml_use_internal_errors(true);
5862 + $dom->loadHTML($final_content);
5863 + libxml_use_internal_errors($internalErrors);
5864 + $tags = $dom->getElementsByTagName('img');
5865 + foreach ($tags as $tag) {
5866 + $temp_get_img = $tag->getAttribute('src');
5867 + if ($temp_get_img != '') {
5868 + $temp_get_img = strtok($temp_get_img, '?');
5869 + $featured_image = rtrim($temp_get_img, '/');
5870 + }
5871 + }
5872 + }
5873 + $post_cats = '';
5874 + $post_categories = wp_get_post_categories( $post->ID );
5875 + foreach($post_categories as $c){
5876 + $cat = get_category( $c );
5877 + $post_cats .= $cat->name . ',';
5878 + }
5879 + $post_cats = trim($post_cats, ',');
5880 + if($post_cats != '')
5881 + {
5882 + $post_categories = explode(',', $post_cats);
5883 + }
5884 + else
5885 + {
5886 + $post_categories = array();
5887 + }
5888 + if(count($post_categories) == 0)
5889 + {
5890 + $terms = get_the_terms( $post->ID, 'product_cat' );
5891 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
5892 + foreach ( $terms as $term ) {
5893 + $post_categories[] = $term->slug;
5894 + }
5895 + $post_cats = implode(',', $post_categories);
5896 + }
5897 +
5898 + }
5899 + foreach($post_categories as $pc)
5900 + {
5901 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
5902 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
5903 + {
5904 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
5905 + {
5906 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5907 + {
5908 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
5909 + }
5910 + update_post_meta($post->ID, $custom_name, "skip");
5911 + return;
5912 + }
5913 + }
5914 + }
5915 + }
5916 + $post_tagz = '';
5917 + $post_tags = wp_get_post_tags( $post->ID );
5918 + foreach($post_tags as $t){
5919 + $post_tagz .= $t->name . ',';
5920 + }
5921 + $post_tagz = trim($post_tagz, ',');
5922 + if($post_tagz != '')
5923 + {
5924 + $post_tags = explode(',', $post_tagz);
5925 + }
5926 + else
5927 + {
5928 + $post_tags = array();
5929 + }
5930 + if(count($post_tags) == 0)
5931 + {
5932 + $terms = get_the_terms( $post->ID, 'product_tag' );
5933 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
5934 + foreach ( $terms as $term ) {
5935 + $post_tags[] = $term->slug;
5936 + }
5937 + $post_tagz = implode(',', $post_tags);
5938 + }
5939 +
5940 + }
5941 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
5942 +
5943 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
5944 + foreach($disable_users as $disable_user)
5945 + {
5946 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
5947 + {
5948 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5949 + {
5950 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
5951 + }
5952 + update_post_meta($post->ID, $custom_name, "skip");
5953 + return;
5954 + }
5955 + }
5956 + }
5957 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
5958 +
5959 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
5960 + $found = false;
5961 + foreach($enable_users as $disable_user)
5962 + {
5963 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
5964 + {
5965 + $found = true;
5966 + }
5967 + }
5968 + if($found === false)
5969 + {
5970 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5971 + {
5972 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
5973 + }
5974 + update_post_meta($post->ID, $custom_name, "skip");
5975 + return;
5976 + }
5977 + }
5978 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
5979 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
5980 + {
5981 + if ( !has_post_thumbnail($post->ID) )
5982 + {
5983 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5984 + {
5985 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
5986 + }
5987 + update_post_meta($post->ID, $custom_name, "skip");
5988 + return;
5989 + }
5990 + }
5991 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
5992 + {
5993 + if ( has_post_thumbnail($post->ID) )
5994 + {
5995 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
5996 + {
5997 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
5998 + }
5999 + update_post_meta($post->ID, $custom_name, "skip");
6000 + return;
6001 + }
6002 + }
6003 + }
6004 +
6005 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
6006 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
6007 + {
6008 + if ( !aiomatic_check_seo_description($post->ID) )
6009 + {
6010 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6011 + {
6012 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
6013 + }
6014 + update_post_meta($post->ID, $custom_name, "skip");
6015 + return;
6016 + }
6017 + }
6018 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
6019 + {
6020 + if ( aiomatic_check_seo_description($post->ID) )
6021 + {
6022 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6023 + {
6024 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
6025 + }
6026 + update_post_meta($post->ID, $custom_name, "skip");
6027 + return;
6028 + }
6029 + }
6030 + }
6031 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
6032 + $skip_me = true;
6033 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
6034 + foreach($enable_field as $fieldx)
6035 + {
6036 + if(strstr($fieldx, '=>') !== false)
6037 + {
6038 + $fna = explode('=>', $fieldx);
6039 + if(isset($fna[1]))
6040 + {
6041 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
6042 + if ($custom_val)
6043 + {
6044 + if(trim($fna[1]) == '*')
6045 + {
6046 + $skip_me = false;
6047 + }
6048 + if($custom_val == trim($fna[1]))
6049 + {
6050 + $skip_me = false;
6051 + }
6052 + }
6053 + }
6054 + }
6055 + }
6056 + if($skip_me === true)
6057 + {
6058 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6059 + {
6060 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
6061 + }
6062 + update_post_meta($post->ID, $custom_name, "skip");
6063 + return;
6064 + }
6065 + }
6066 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
6067 + $skip_me = false;
6068 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
6069 + foreach($disable_field as $fieldx)
6070 + {
6071 + if(strstr($fieldx, '=>') !== false)
6072 + {
6073 + $fna = explode('=>', $fieldx);
6074 + if(isset($fna[1]))
6075 + {
6076 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
6077 + if ($custom_val)
6078 + {
6079 + if(trim($fna[1]) == '*')
6080 + {
6081 + $skip_me = true;
6082 + }
6083 + if($custom_val == trim($fna[1]))
6084 + {
6085 + $skip_me = true;
6086 + }
6087 + }
6088 + }
6089 + }
6090 + }
6091 + if($skip_me === true)
6092 + {
6093 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6094 + {
6095 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
6096 + }
6097 + update_post_meta($post->ID, $custom_name, "skip");
6098 + return;
6099 + }
6100 + }
6101 + foreach($post_tags as $pt)
6102 + {
6103 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
6104 +
6105 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
6106 + foreach($disable_tags as $disabled_tag)
6107 + {
6108 + if($manual != true && trim($pt) == trim($disabled_tag))
6109 + {
6110 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6111 + {
6112 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
6113 + }
6114 + update_post_meta($post->ID, $custom_name, "skip");
6115 + return;
6116 + }
6117 + }
6118 + }
6119 + }
6120 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
6121 + $skip = true;
6122 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
6123 + foreach($post_tags as $pt)
6124 + {
6125 + foreach($enable_tags as $enable_tag)
6126 + {
6127 + if(trim($pt) == trim($enable_tag))
6128 + {
6129 + $skip = false;
6130 + break;
6131 + }
6132 + }
6133 + }
6134 + if($skip == true)
6135 + {
6136 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6137 + {
6138 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
6139 + }
6140 + update_post_meta($post->ID, $custom_name, "skip");
6141 + return;
6142 + }
6143 + }
6144 + $link_juicer_prompt = aiomatic_replaceAIPostShortcodes($link_juicer_prompt, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
6145 + $link_juicer_prompt = trim($link_juicer_prompt);
6146 + if (isset($aiomatic_Spinner_Settings['link_juicer_model']) && $aiomatic_Spinner_Settings['link_juicer_model'] != '') {
6147 + $cmodel = $aiomatic_Spinner_Settings['link_juicer_model'];
6148 + }
6149 + else
6150 + {
6151 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
6152 + }
6153 + if (isset($aiomatic_Spinner_Settings['link_juicer_assistant_id']) && $aiomatic_Spinner_Settings['link_juicer_assistant_id'] != '') {
6154 + $link_juicer_assistant_id = $aiomatic_Spinner_Settings['link_juicer_assistant_id'];
6155 + }
6156 + else
6157 + {
6158 + $link_juicer_assistant_id = '';
6159 + }
6160 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_cats, $mxatches);
6161 + if(isset($mxatches[2][0]))
6162 + {
6163 + $minx = $mxatches[1][0];
6164 + $maxx = $mxatches[2][0];
6165 + $max_cats = rand(intval($minx), intval($maxx));
6166 + }
6167 + else
6168 + {
6169 + $max_cats = intval($max_cats);
6170 + }
6171 + $author_obj = get_user_by('id', $post->post_author);
6172 + if(isset($author_obj->user_nicename))
6173 + {
6174 + $xuser_name = $author_obj->user_nicename;
6175 + }
6176 + else
6177 + {
6178 + $xuser_name = get_bloginfo( 'name' );
6179 + }
6180 + $xpost_link = get_permalink($post->ID);
6181 + $link_juicer_prompt_new = $link_juicer_prompt;
6182 + $userid = false;
6183 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
6184 + $query_token_count = count(aiomatic_encode($link_juicer_prompt_new));
6185 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
6186 + {
6187 + $link_juicer_prompt_new = aiomatic_substr($link_juicer_prompt_new, 0, (0-($max_seed_tokens * 4)));
6188 + $query_token_count = count(aiomatic_encode($link_juicer_prompt_new));
6189 + }
6190 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
6191 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $link_juicer_prompt_new, $query_token_count);
6192 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
6193 + if($available_tokens > $max_result_tokens)
6194 + {
6195 + $available_tokens = $max_result_tokens;
6196 + }
6197 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
6198 + {
6199 + $string_len = aiomatic_strlen($link_juicer_prompt_new);
6200 + $string_len = $string_len / 2;
6201 + $string_len = intval(0 - $string_len);
6202 + $link_juicer_prompt_new = aiomatic_substr($link_juicer_prompt_new, 0, $string_len);
6203 + $link_juicer_prompt_new = trim(link_juicer_prompt_new);
6204 + if(empty(link_juicer_prompt_new))
6205 + {
6206 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($link_juicer_prompt_new, true));
6207 + }
6208 + $query_token_count = count(aiomatic_encode($link_juicer_prompt_new));
6209 + $available_tokens = $max_tokens - $query_token_count;
6210 + }
6211 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6212 + {
6213 + $api_service = aiomatic_get_api_service($token, $cmodel);
6214 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $link_juicer_assistant_id . '\\' . $cmodel . ')(' . $available_tokens . ') post Link Juicer keyword generator, with seed command: ' . $link_juicer_prompt_new);
6215 + }
6216 + $aierror = '';
6217 + $extra_kws = '';
6218 + $finish_reason = '';
6219 + $generated_text = aiomatic_generate_text($token, $cmodel, $link_juicer_prompt_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'linkJuicerKeywordGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $link_juicer_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
6220 + if($generated_text === false)
6221 + {
6222 + aiomatic_log_to_file('Link Juicer keyword generator error: ' . $aierror);
6223 + }
6224 + else
6225 + {
6226 + $extra_kws = ucfirst(trim(nl2br(trim($generated_text))));
6227 + $extra_kws = str_replace('//', '', $extra_kws);
6228 + }
6229 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($extra_kws) !== '')
6230 + {
6231 + $api_service = aiomatic_get_api_service($token, $cmodel);
6232 + aiomatic_log_to_file($api_service . ' responded successfully, Link Juicer keyword generated for post ID ' . $post->ID . ': ' . $extra_kws, true);
6233 + }
6234 + if($extra_kws != '')
6235 + {
6236 + if (isset($aiomatic_Spinner_Settings['force_keywords']) && $aiomatic_Spinner_Settings['force_keywords'] != '')
6237 + {
6238 + $force_keywords = $aiomatic_Spinner_Settings['force_keywords'];
6239 + $force_keywords = explode(',', $force_keywords);
6240 + $force_keywords = array_map('trim', $force_keywords);
6241 + $force_keywords = array_filter($force_keywords);
6242 + }
6243 + else
6244 + {
6245 + $force_keywords = array();
6246 + }
6247 + $link_juicer_arr = explode(',', $extra_kws);
6248 + $link_juicer_arr = array_merge($link_juicer_arr, $force_keywords);
6249 + update_post_meta( $post->ID, 'ilj_linkdefinition', $link_juicer_arr);
6250 + do_action(
6251 + 'ilj_after_keywords_update',
6252 + $post->ID,
6253 + 'post',
6254 + $post->post_status
6255 + );
6256 + }
6257 + }
6258 + else
6259 + {
6260 + if (isset($aiomatic_Spinner_Settings['max_links']) && $aiomatic_Spinner_Settings['max_links'] != '')
6261 + {
6262 + $max_links = trim($aiomatic_Spinner_Settings['max_links']);
6263 + }
6264 + else
6265 + {
6266 + $max_links = '3-5';
6267 + }
6268 + if (isset($aiomatic_Spinner_Settings['link_post_types']) && $aiomatic_Spinner_Settings['link_post_types'] != '')
6269 + {
6270 + $link_post_types = trim($aiomatic_Spinner_Settings['link_post_types']);
6271 + }
6272 + else
6273 + {
6274 + $link_post_types = 'post';
6275 + }
6276 + if (isset($aiomatic_Spinner_Settings['link_post_not_ids']) && $aiomatic_Spinner_Settings['link_post_not_ids'] != '')
6277 + {
6278 + $link_post_not_ids = trim($aiomatic_Spinner_Settings['link_post_not_ids']);
6279 + }
6280 + else
6281 + {
6282 + $link_post_not_ids = '';
6283 + }
6284 + if (isset($aiomatic_Spinner_Settings['link_post_ids']) && $aiomatic_Spinner_Settings['link_post_ids'] != '')
6285 + {
6286 + $link_post_ids = trim($aiomatic_Spinner_Settings['link_post_ids']);
6287 + }
6288 + else
6289 + {
6290 + $link_post_ids = '';
6291 + }
6292 + if (isset($aiomatic_Spinner_Settings['link_nofollow']) && $aiomatic_Spinner_Settings['link_nofollow'] != '')
6293 + {
6294 + $link_nofollow = trim($aiomatic_Spinner_Settings['link_nofollow']);
6295 + }
6296 + else
6297 + {
6298 + $link_nofollow = 'post';
6299 + }
6300 + if (isset($aiomatic_Spinner_Settings['link_type']) && $aiomatic_Spinner_Settings['link_type'] != '')
6301 + {
6302 + $link_type = $aiomatic_Spinner_Settings['link_type'];
6303 + }
6304 + else
6305 + {
6306 + $link_type = 'internal';
6307 + }
6308 + if (isset($aiomatic_Spinner_Settings['link_list']) && !empty($aiomatic_Spinner_Settings['link_list']))
6309 + {
6310 + $link_list = $aiomatic_Spinner_Settings['link_list'];
6311 + }
6312 + else
6313 + {
6314 + $link_list = '';
6315 + }
6316 + $zlang = 'en_US';
6317 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang']))
6318 + {
6319 + $zlang = $aiomatic_Main_Settings['kw_lang'];
6320 + }
6321 + $rel_search = array('post_title', 'post_content');
6322 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search']))
6323 + {
6324 + $rel_search = $aiomatic_Main_Settings['rel_search'];
6325 + }
6326 + if($max_links !== '')
6327 + {
6328 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_links, $mxatches);
6329 + if(isset($mxatches[2][0]))
6330 + {
6331 + $min = $mxatches[1][0];
6332 + $max = $mxatches[2][0];
6333 + $max_links = rand(intval($min), intval($max));
6334 + }
6335 + else
6336 + {
6337 + $max_links = intval($max_links);
6338 + }
6339 + if (isset($aiomatic_Spinner_Settings['force_keywords']) && $aiomatic_Spinner_Settings['force_keywords'] != '')
6340 + {
6341 + $force_keywords = $aiomatic_Spinner_Settings['force_keywords'];
6342 + $force_keywords = explode(',', $force_keywords);
6343 + $force_keywords = array_map('trim', $force_keywords);
6344 + $force_keywords = array_filter($force_keywords);
6345 + }
6346 + else
6347 + {
6348 + $force_keywords = array();
6349 + }
6350 + require_once(dirname(__FILE__) . "/res/InboundLinks.php");
6351 + $inboundlinker = new AiomaticAutoInboundLinks();
6352 + try
6353 + {
6354 + $final_content_links = $inboundlinker->add_inbound_links($final_content, $max_links, $link_post_types, $zlang, $rel_search, $post->ID, $link_type, $link_list, $link_nofollow, $link_post_ids, $link_post_not_ids, $force_keywords);
6355 + if(!empty($final_content_links) && $final_content_links != $final_content)
6356 + {
6357 + $final_content = $final_content_links;
6358 + $args = array();
6359 + $args['ID'] = $post->ID;
6360 + $args['post_content'] = $final_content_links;
6361 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
6362 + {
6363 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
6364 + }
6365 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
6366 + {
6367 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
6368 + if($date_stamp)
6369 + {
6370 + $postdate = date("Y-m-d H:i:s", $date_stamp);
6371 + $args['post_date'] = $postdate;
6372 + }
6373 + }
6374 + remove_filter('content_save_pre', 'wp_filter_post_kses');
6375 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
6376 + remove_filter('title_save_pre', 'wp_filter_kses');
6377 + update_post_meta($post->ID, $custom_name, "pub");
6378 + $post_updated = wp_update_post($args);
6379 + $changes_made = true;
6380 + add_filter('content_save_pre', 'wp_filter_post_kses');
6381 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
6382 + add_filter('title_save_pre', 'wp_filter_kses');
6383 + if (is_wp_error($post_updated)) {
6384 + $errors = $post_updated->get_error_messages();
6385 + foreach ($errors as $error) {
6386 + aiomatic_log_to_file('Error occured while updating post for internal links "' . $post->post_title . '": ' . $error);
6387 + }
6388 + }
6389 + else
6390 + {
6391 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6392 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with internal links.');
6393 + }
6394 + }
6395 + }
6396 + }
6397 + catch(Exception $ex)
6398 + {
6399 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6400 + aiomatic_log_to_file('Failed to automatically add new inbound links to content: ' . $ex->getMessage());
6401 + }
6402 + }
6403 + }
6404 + }
6405 + }
6406 + if (isset($aiomatic_Spinner_Settings['append_toc']) && $aiomatic_Spinner_Settings['append_toc'] != '' && $aiomatic_Spinner_Settings['append_toc'] != 'disabled')
6407 + {
6408 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6409 + {
6410 + aiomatic_log_to_file('Starting post ToC generator...');
6411 + }
6412 + $updated = false;
6413 + $items = '';
6414 + $find = [];
6415 + $replace = [];
6416 + $css_classes = '';
6417 + $items = aiomatic_extract_headings( $find, $replace, $final_content );
6418 + if ( $items )
6419 + {
6420 + if (isset($aiomatic_Spinner_Settings['float_toc']) && $aiomatic_Spinner_Settings['float_toc'] != '' && $aiomatic_Spinner_Settings['float_toc'] != 'none')
6421 + {
6422 + switch ( $aiomatic_Spinner_Settings['float_toc'] )
6423 + {
6424 + case 'left':
6425 + $css_classes .= ' aiomatic_toc_wrap_left';
6426 + break;
6427 +
6428 + case 'right':
6429 + $css_classes .= ' aiomatic_toc_wrap_right';
6430 + break;
6431 +
6432 + case 'none':
6433 + default:
6434 + }
6435 + }
6436 +
6437 + if (isset($aiomatic_Spinner_Settings['color_toc']) && $aiomatic_Spinner_Settings['color_toc'] != '' && $aiomatic_Spinner_Settings['color_toc'] != 'gray')
6438 + {
6439 + switch ( $aiomatic_Spinner_Settings['color_toc'] )
6440 + {
6441 + case 'blue':
6442 + $css_classes .= ' aiomatic_toc_light_blue';
6443 + break;
6444 +
6445 + case 'white':
6446 + $css_classes .= ' aiomatic_toc_white';
6447 + break;
6448 +
6449 + case 'black':
6450 + $css_classes .= ' aiomatic_toc_black';
6451 + break;
6452 +
6453 + case 'transparent':
6454 + $css_classes .= ' aiomatic_toc_transparent';
6455 + break;
6456 +
6457 + case 'gray':
6458 + default:
6459 + }
6460 + }
6461 + $css_classes = trim( $css_classes );
6462 + if ( ! $css_classes )
6463 + {
6464 + $css_classes = ' ';
6465 + }
6466 + $html = '<div id="aiomatic_toc_container" class="' . htmlentities( $css_classes, ENT_COMPAT, 'UTF-8' ) . '">';
6467 + if (isset($aiomatic_Spinner_Settings['title_toc']) && trim($aiomatic_Spinner_Settings['title_toc']) != '')
6468 + {
6469 + $toc_title = trim($aiomatic_Spinner_Settings['title_toc']);
6470 + }
6471 + else
6472 + {
6473 + $toc_title = 'Table of Contents';
6474 + }
6475 + $toc_title = htmlentities( $toc_title, ENT_COMPAT, 'UTF-8' );
6476 + $html .= '<p class="aiomatic_toc_title">' . $toc_title . '</p>';
6477 + $html .= '<ul class="aiomatic_toc_list">' . $items . '</ul></div>' . "\n";
6478 + $toc_location = $aiomatic_Spinner_Settings['append_toc'];
6479 + if ( count( $find ) > 0 )
6480 + {
6481 + switch ( $toc_location )
6482 + {
6483 + case 'preppend':
6484 + $final_content = $html . aiomatic_mb_find_replace( $find, $replace, $final_content );
6485 + $updated = true;
6486 + break;
6487 +
6488 + case 'append':
6489 + $final_content = aiomatic_mb_find_replace( $find, $replace, $final_content ) . $html;
6490 + $updated = true;
6491 + break;
6492 +
6493 + case 'heading2':
6494 + $replace[0] = $replace[0] . $html;
6495 + $final_content = aiomatic_mb_find_replace( $find, $replace, $final_content );
6496 + $updated = true;
6497 + break;
6498 +
6499 + case 'heading':
6500 + default:
6501 + $replace[0] = $html . $replace[0];
6502 + $final_content = aiomatic_mb_find_replace( $find, $replace, $final_content );
6503 + $updated = true;
6504 + }
6505 + }
6506 + }
6507 + if($updated == true)
6508 + {
6509 + $args = array();
6510 + $args['ID'] = $post->ID;
6511 + $args['post_content'] = $final_content;
6512 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
6513 + {
6514 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
6515 + }
6516 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
6517 + {
6518 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
6519 + if($date_stamp)
6520 + {
6521 + $postdate = date("Y-m-d H:i:s", $date_stamp);
6522 + $args['post_date'] = $postdate;
6523 + }
6524 + }
6525 + remove_filter('content_save_pre', 'wp_filter_post_kses');
6526 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
6527 + remove_filter('title_save_pre', 'wp_filter_kses');
6528 + update_post_meta($post->ID, $custom_name, "pub");
6529 + $post_updated = wp_update_post($args);
6530 + $changes_made = true;
6531 + add_filter('content_save_pre', 'wp_filter_post_kses');
6532 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
6533 + add_filter('title_save_pre', 'wp_filter_kses');
6534 + if (is_wp_error($post_updated)) {
6535 + $errors = $post_updated->get_error_messages();
6536 + foreach ($errors as $error) {
6537 + aiomatic_log_to_file('Error occured while updating post for AI content "' . $post->post_title . '": ' . $error);
6538 + }
6539 + }
6540 + else
6541 + {
6542 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6543 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated content.');
6544 + }
6545 + update_post_meta($post->ID, 'aiomatic_toc', '1');
6546 + }
6547 + }
6548 + }
6549 + if (isset($aiomatic_Spinner_Settings['add_cats']) && $aiomatic_Spinner_Settings['add_cats'] != '' && $aiomatic_Spinner_Settings['add_cats'] != 'disabled')
6550 + {
6551 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6552 + {
6553 + aiomatic_log_to_file('Starting post category generator...');
6554 + }
6555 + $vision_file = '';
6556 + if (isset($aiomatic_Spinner_Settings['max_cats']) && $aiomatic_Spinner_Settings['max_cats'] != '')
6557 + {
6558 + $max_cats = trim($aiomatic_Spinner_Settings['max_cats']);
6559 + }
6560 + else
6561 + {
6562 + $max_cats = '1';
6563 + }
6564 + if($max_cats !== '')
6565 + {
6566 + if (isset($aiomatic_Spinner_Settings['cats_tax']) && trim($aiomatic_Spinner_Settings['cats_tax']) != '')
6567 + {
6568 + $cat_slug = trim($aiomatic_Spinner_Settings['cats_tax']);
6569 + }
6570 + else
6571 + {
6572 + $cat_slug = 'category';
6573 + if($post->post_type == 'product')
6574 + {
6575 + $cat_slug = 'product_cat';
6576 + }
6577 + }
6578 + if (isset($aiomatic_Spinner_Settings['ai_vision_cat']) && $aiomatic_Spinner_Settings['ai_vision_cat'] == 'on')
6579 + {
6580 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
6581 + if($avatar !== false)
6582 + {
6583 + $vision_file = $avatar;
6584 + }
6585 + }
6586 + if (isset($aiomatic_Spinner_Settings['ai_cats']) && $aiomatic_Spinner_Settings['ai_cats'] != '')
6587 + {
6588 + $ai_cats = $aiomatic_Spinner_Settings['ai_cats'];
6589 + }
6590 + else
6591 + {
6592 + $ai_cats = 'Write a comma separated list of 5 categories for post title: %%post_title%%';
6593 + }
6594 + $ai_cats = aiomatic_replaceSynergyShortcodes($ai_cats);
6595 +
6596 + $post_link = get_permalink($post->ID);
6597 + $blog_title = html_entity_decode(get_bloginfo('title'));
6598 + $author_obj = get_user_by('id', $post->post_author);
6599 + if($author_obj !== false && isset($author_obj->user_nicename))
6600 + {
6601 + $user_name = $author_obj->user_nicename;
6602 + }
6603 + else
6604 + {
6605 + $user_name = '';
6606 + }
6607 + $featured_image = '';
6608 + wp_suspend_cache_addition(true);
6609 + $metas = get_post_custom($post->ID);
6610 + wp_suspend_cache_addition(false);
6611 + if(is_array($metas))
6612 + {
6613 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
6614 + }
6615 + else
6616 + {
6617 + $rez_meta = array();
6618 + }
6619 + if(count($rez_meta) > 0)
6620 + {
6621 + foreach($rez_meta as $rm)
6622 + {
6623 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
6624 + {
6625 + $featured_image = $rm[0];
6626 + break;
6627 + }
6628 + }
6629 + }
6630 + if($featured_image == '')
6631 + {
6632 + $featured_image = aiomatic_generate_thumbmail($post->ID);
6633 + }
6634 + if($featured_image == '' && $final_content != '')
6635 + {
6636 + $dom = new DOMDocument();
6637 + $internalErrors = libxml_use_internal_errors(true);
6638 + $dom->loadHTML($final_content);
6639 + libxml_use_internal_errors($internalErrors);
6640 + $tags = $dom->getElementsByTagName('img');
6641 + foreach ($tags as $tag) {
6642 + $temp_get_img = $tag->getAttribute('src');
6643 + if ($temp_get_img != '') {
6644 + $temp_get_img = strtok($temp_get_img, '?');
6645 + $featured_image = rtrim($temp_get_img, '/');
6646 + }
6647 + }
6648 + }
6649 + $post_cats = '';
6650 + $post_categories = wp_get_post_categories( $post->ID );
6651 + foreach($post_categories as $c){
6652 + $cat = get_category( $c );
6653 + $post_cats .= $cat->name . ',';
6654 + }
6655 + $post_cats = trim($post_cats, ',');
6656 + if($post_cats != '')
6657 + {
6658 + $post_categories = explode(',', $post_cats);
6659 + }
6660 + else
6661 + {
6662 + $post_categories = array();
6663 + }
6664 + if(count($post_categories) == 0)
6665 + {
6666 + $terms = get_the_terms( $post->ID, 'product_cat' );
6667 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
6668 + foreach ( $terms as $term ) {
6669 + $post_categories[] = $term->slug;
6670 + }
6671 + $post_cats = implode(',', $post_categories);
6672 + }
6673 +
6674 + }
6675 + foreach($post_categories as $pc)
6676 + {
6677 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
6678 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
6679 + {
6680 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
6681 + {
6682 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6683 + {
6684 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
6685 + }
6686 + update_post_meta($post->ID, $custom_name, "skip");
6687 + return;
6688 + }
6689 + }
6690 + }
6691 + }
6692 + $post_tagz = '';
6693 + $post_tags = wp_get_post_tags( $post->ID );
6694 + foreach($post_tags as $t){
6695 + $post_tagz .= $t->name . ',';
6696 + }
6697 + $post_tagz = trim($post_tagz, ',');
6698 + if($post_tagz != '')
6699 + {
6700 + $post_tags = explode(',', $post_tagz);
6701 + }
6702 + else
6703 + {
6704 + $post_tags = array();
6705 + }
6706 + if(count($post_tags) == 0)
6707 + {
6708 + $terms = get_the_terms( $post->ID, 'product_tag' );
6709 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
6710 + foreach ( $terms as $term ) {
6711 + $post_tags[] = $term->slug;
6712 + }
6713 + $post_tagz = implode(',', $post_tags);
6714 + }
6715 +
6716 + }
6717 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
6718 +
6719 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
6720 + foreach($disable_users as $disable_user)
6721 + {
6722 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
6723 + {
6724 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6725 + {
6726 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
6727 + }
6728 + update_post_meta($post->ID, $custom_name, "skip");
6729 + return;
6730 + }
6731 + }
6732 + }
6733 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
6734 +
6735 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
6736 + $found = false;
6737 + foreach($enable_users as $disable_user)
6738 + {
6739 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
6740 + {
6741 + $found = true;
6742 + }
6743 + }
6744 + if($found === false)
6745 + {
6746 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6747 + {
6748 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
6749 + }
6750 + update_post_meta($post->ID, $custom_name, "skip");
6751 + return;
6752 + }
6753 + }
6754 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
6755 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
6756 + {
6757 + if ( !has_post_thumbnail($post->ID) )
6758 + {
6759 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6760 + {
6761 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
6762 + }
6763 + update_post_meta($post->ID, $custom_name, "skip");
6764 + return;
6765 + }
6766 + }
6767 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
6768 + {
6769 + if ( has_post_thumbnail($post->ID) )
6770 + {
6771 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6772 + {
6773 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
6774 + }
6775 + update_post_meta($post->ID, $custom_name, "skip");
6776 + return;
6777 + }
6778 + }
6779 + }
6780 +
6781 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
6782 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
6783 + {
6784 + if ( !aiomatic_check_seo_description($post->ID) )
6785 + {
6786 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6787 + {
6788 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
6789 + }
6790 + update_post_meta($post->ID, $custom_name, "skip");
6791 + return;
6792 + }
6793 + }
6794 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
6795 + {
6796 + if ( aiomatic_check_seo_description($post->ID) )
6797 + {
6798 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6799 + {
6800 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
6801 + }
6802 + update_post_meta($post->ID, $custom_name, "skip");
6803 + return;
6804 + }
6805 + }
6806 + }
6807 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
6808 + $skip_me = true;
6809 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
6810 + foreach($enable_field as $fieldx)
6811 + {
6812 + if(strstr($fieldx, '=>') !== false)
6813 + {
6814 + $fna = explode('=>', $fieldx);
6815 + if(isset($fna[1]))
6816 + {
6817 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
6818 + if ($custom_val)
6819 + {
6820 + if(trim($fna[1]) == '*')
6821 + {
6822 + $skip_me = false;
6823 + }
6824 + if($custom_val == trim($fna[1]))
6825 + {
6826 + $skip_me = false;
6827 + }
6828 + }
6829 + }
6830 + }
6831 + }
6832 + if($skip_me === true)
6833 + {
6834 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6835 + {
6836 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
6837 + }
6838 + update_post_meta($post->ID, $custom_name, "skip");
6839 + return;
6840 + }
6841 + }
6842 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
6843 + $skip_me = false;
6844 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
6845 + foreach($disable_field as $fieldx)
6846 + {
6847 + if(strstr($fieldx, '=>') !== false)
6848 + {
6849 + $fna = explode('=>', $fieldx);
6850 + if(isset($fna[1]))
6851 + {
6852 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
6853 + if ($custom_val)
6854 + {
6855 + if(trim($fna[1]) == '*')
6856 + {
6857 + $skip_me = true;
6858 + }
6859 + if($custom_val == trim($fna[1]))
6860 + {
6861 + $skip_me = true;
6862 + }
6863 + }
6864 + }
6865 + }
6866 + }
6867 + if($skip_me === true)
6868 + {
6869 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6870 + {
6871 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
6872 + }
6873 + update_post_meta($post->ID, $custom_name, "skip");
6874 + return;
6875 + }
6876 + }
6877 + foreach($post_tags as $pt)
6878 + {
6879 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
6880 +
6881 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
6882 + foreach($disable_tags as $disabled_tag)
6883 + {
6884 + if($manual != true && trim($pt) == trim($disabled_tag))
6885 + {
6886 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6887 + {
6888 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
6889 + }
6890 + update_post_meta($post->ID, $custom_name, "skip");
6891 + return;
6892 + }
6893 + }
6894 + }
6895 + }
6896 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
6897 + $skip = true;
6898 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
6899 + foreach($post_tags as $pt)
6900 + {
6901 + foreach($enable_tags as $enable_tag)
6902 + {
6903 + if(trim($pt) == trim($enable_tag))
6904 + {
6905 + $skip = false;
6906 + break;
6907 + }
6908 + }
6909 + }
6910 + if($skip == true)
6911 + {
6912 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
6913 + {
6914 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
6915 + }
6916 + update_post_meta($post->ID, $custom_name, "skip");
6917 + return;
6918 + }
6919 + }
6920 + $caxcategories = get_terms(array(
6921 + 'taxonomy' => array($cat_slug)
6922 + ));
6923 + $all_categories = '';
6924 + if (!is_wp_error($caxcategories) && !empty($caxcategories))
6925 + {
6926 + foreach ($caxcategories as $category)
6927 + {
6928 + $all_categories .= $category->slug . ',';
6929 + }
6930 + $all_categories = rtrim($all_categories, ',');
6931 + }
6932 + $ai_cats = str_replace('%%all_categories%%', $all_categories, $ai_cats);
6933 + $ai_cats = aiomatic_replaceAIPostShortcodes($ai_cats, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
6934 + $ai_cats = trim($ai_cats);
6935 + if (isset($aiomatic_Spinner_Settings['cats_model']) && $aiomatic_Spinner_Settings['cats_model'] != '') {
6936 + $cmodel = $aiomatic_Spinner_Settings['cats_model'];
6937 + }
6938 + else
6939 + {
6940 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
6941 + }
6942 + if (isset($aiomatic_Spinner_Settings['categories_assistant_id']) && $aiomatic_Spinner_Settings['categories_assistant_id'] != '') {
6943 + $categories_assistant_id = $aiomatic_Spinner_Settings['categories_assistant_id'];
6944 + }
6945 + else
6946 + {
6947 + $categories_assistant_id = '';
6948 + }
6949 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_cats, $mxatches);
6950 + if(isset($mxatches[2][0]))
6951 + {
6952 + $minx = $mxatches[1][0];
6953 + $maxx = $mxatches[2][0];
6954 + $max_cats = rand(intval($minx), intval($maxx));
6955 + }
6956 + else
6957 + {
6958 + $max_cats = intval($max_cats);
6959 + }
6960 + $author_obj = get_user_by('id', $post->post_author);
6961 + if(isset($author_obj->user_nicename))
6962 + {
6963 + $xuser_name = $author_obj->user_nicename;
6964 + }
6965 + else
6966 + {
6967 + $xuser_name = get_bloginfo( 'name' );
6968 + }
6969 + $xpost_link = get_permalink($post->ID);
6970 + $ai_cats_new = $ai_cats;
6971 + $userid = false;
6972 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
6973 + $query_token_count = count(aiomatic_encode($ai_cats_new));
6974 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
6975 + {
6976 + $ai_cats_new = aiomatic_substr($ai_cats_new, 0, (0-($max_seed_tokens * 4)));
6977 + $query_token_count = count(aiomatic_encode($ai_cats_new));
6978 + }
6979 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
6980 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $ai_cats_new, $query_token_count);
6981 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
6982 + if($available_tokens > $max_result_tokens)
6983 + {
6984 + $available_tokens = $max_result_tokens;
6985 + }
6986 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
6987 + {
6988 + $string_len = aiomatic_strlen($ai_cats_new);
6989 + $string_len = $string_len / 2;
6990 + $string_len = intval(0 - $string_len);
6991 + $ai_cats_new = aiomatic_substr($ai_cats_new, 0, $string_len);
6992 + $ai_cats_new = trim($ai_cats_new);
6993 + if(empty($ai_cats_new))
6994 + {
6995 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_cats_new, true));
6996 + }
6997 + $query_token_count = count(aiomatic_encode($ai_cats_new));
6998 + $available_tokens = $max_tokens - $query_token_count;
6999 + }
7000 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7001 + {
7002 + $api_service = aiomatic_get_api_service($token, $cmodel);
7003 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $categories_assistant_id . '\\' . $cmodel . ')(' . $available_tokens . ') post category generator, with seed command: ' . $ai_cats_new);
7004 + }
7005 + $aierror = '';
7006 + $extra_categories = '';
7007 + $finish_reason = '';
7008 + $generated_text = aiomatic_generate_text($token, $cmodel, $ai_cats_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $categories_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
7009 + if($generated_text === false)
7010 + {
7011 + aiomatic_log_to_file('Category generator error: ' . $aierror);
7012 + }
7013 + else
7014 + {
7015 + $extra_categories = ucfirst(trim(nl2br(trim($generated_text))));
7016 + $extra_categories = str_replace('//', '', $extra_categories);
7017 + }
7018 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($extra_categories) !== '')
7019 + {
7020 + $api_service = aiomatic_get_api_service($token, $cmodel);
7021 + aiomatic_log_to_file($api_service . ' responded successfully, category generated for post ID ' . $post->ID . ': ' . $extra_categories, true);
7022 + }
7023 + if($extra_categories != '')
7024 + {
7025 + $extra_cats = explode(',', $extra_categories);
7026 + $added_cats = 0;
7027 + foreach($extra_cats as $extra_cat)
7028 + {
7029 + if($added_cats >= $max_cats)
7030 + {
7031 + break;
7032 + }
7033 + $extra_cat = trim($extra_cat);
7034 + $extra_cat = strip_tags($extra_cat);
7035 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
7036 + if(empty($extra_cat))
7037 + {
7038 + continue;
7039 + }
7040 + if (isset($aiomatic_Spinner_Settings['skip_inexist']) && $aiomatic_Spinner_Settings['skip_inexist'] == 'on')
7041 + {
7042 + if(!term_exists($extra_cat, $cat_slug))
7043 + {
7044 + aiomatic_log_to_file('Skipping category, as it is not found on this site: ' . $extra_cat);
7045 + continue;
7046 + }
7047 + }
7048 + $added_cats++;
7049 + $termid = aiomatic_create_terms($cat_slug, null, trim($extra_cat));
7050 + wp_set_post_terms($post->ID, $termid, $cat_slug, true);
7051 + }
7052 + if($added_cats > 0)
7053 + {
7054 + update_post_meta($post->ID, $custom_name, "pub");
7055 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
7056 + {
7057 + $args = array();
7058 + $args['ID'] = $post->ID;
7059 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
7060 + remove_filter('content_save_pre', 'wp_filter_post_kses');
7061 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7062 + remove_filter('title_save_pre', 'wp_filter_kses');
7063 + $post_updated = wp_update_post($args);
7064 + $changes_made = true;
7065 + add_filter('content_save_pre', 'wp_filter_post_kses');
7066 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7067 + add_filter('title_save_pre', 'wp_filter_kses');
7068 + if (is_wp_error($post_updated)) {
7069 + $errors = $post_updated->get_error_messages();
7070 + foreach ($errors as $error) {
7071 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
7072 + }
7073 + }
7074 + }
7075 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
7076 + {
7077 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
7078 + if($date_stamp)
7079 + {
7080 + $args = array();
7081 + $args['ID'] = $post->ID;
7082 + $postdate = date("Y-m-d H:i:s", $date_stamp);
7083 + $args['post_date'] = $postdate;
7084 + remove_filter('content_save_pre', 'wp_filter_post_kses');
7085 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7086 + remove_filter('title_save_pre', 'wp_filter_kses');
7087 + $post_updated = wp_update_post($args);
7088 + $changes_made = true;
7089 + add_filter('content_save_pre', 'wp_filter_post_kses');
7090 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7091 + add_filter('title_save_pre', 'wp_filter_kses');
7092 + if (is_wp_error($post_updated)) {
7093 + $errors = $post_updated->get_error_messages();
7094 + foreach ($errors as $error) {
7095 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
7096 + }
7097 + }
7098 + }
7099 + }
7100 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
7101 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated categories.');
7102 + }
7103 + }
7104 + }
7105 + }
7106 + }
7107 + if (isset($aiomatic_Spinner_Settings['add_tags']) && $aiomatic_Spinner_Settings['add_tags'] != '' && $aiomatic_Spinner_Settings['add_tags'] != 'disabled')
7108 + {
7109 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7110 + {
7111 + aiomatic_log_to_file('Starting post tag generator...');
7112 + }
7113 + if (isset($aiomatic_Spinner_Settings['max_tags']) && $aiomatic_Spinner_Settings['max_tags'] != '')
7114 + {
7115 + $max_tags = trim($aiomatic_Spinner_Settings['max_tags']);
7116 + }
7117 + else
7118 + {
7119 + $max_tags = '1';
7120 + }
7121 + if($max_tags !== '')
7122 + {
7123 + if (isset($aiomatic_Spinner_Settings['tags_tax']) && trim($aiomatic_Spinner_Settings['tags_tax']) != '')
7124 + {
7125 + $tag_slug = trim($aiomatic_Spinner_Settings['tags_tax']);
7126 + }
7127 + else
7128 + {
7129 + $tag_slug = 'post_tag';
7130 + if($post->post_type == 'product')
7131 + {
7132 + $tag_slug = 'product_tag';
7133 + }
7134 + }
7135 + if (isset($aiomatic_Spinner_Settings['ai_vision_tag']) && $aiomatic_Spinner_Settings['ai_vision_tag'] == 'on')
7136 + {
7137 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
7138 + if($avatar !== false)
7139 + {
7140 + $vision_file = $avatar;
7141 + }
7142 + }
7143 + if (isset($aiomatic_Spinner_Settings['ai_tags']) && $aiomatic_Spinner_Settings['ai_tags'] != '')
7144 + {
7145 + $ai_tags = $aiomatic_Spinner_Settings['ai_tags'];
7146 + }
7147 + else
7148 + {
7149 + $ai_tags = 'Write a comma separated list of 5 tags for post title: %%post_title%%';
7150 + }
7151 + $ai_tags = aiomatic_replaceSynergyShortcodes($ai_tags);
7152 +
7153 + $post_link = get_permalink($post->ID);
7154 + $blog_title = html_entity_decode(get_bloginfo('title'));
7155 + $author_obj = get_user_by('id', $post->post_author);
7156 + if($author_obj !== false && isset($author_obj->user_nicename))
7157 + {
7158 + $user_name = $author_obj->user_nicename;
7159 + }
7160 + else
7161 + {
7162 + $user_name = '';
7163 + }
7164 + $featured_image = '';
7165 + wp_suspend_cache_addition(true);
7166 + $metas = get_post_custom($post->ID);
7167 + wp_suspend_cache_addition(false);
7168 + if(is_array($metas))
7169 + {
7170 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
7171 + }
7172 + else
7173 + {
7174 + $rez_meta = array();
7175 + }
7176 + if(count($rez_meta) > 0)
7177 + {
7178 + foreach($rez_meta as $rm)
7179 + {
7180 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
7181 + {
7182 + $featured_image = $rm[0];
7183 + break;
7184 + }
7185 + }
7186 + }
7187 + if($featured_image == '')
7188 + {
7189 + $featured_image = aiomatic_generate_thumbmail($post->ID);
7190 + }
7191 + if($featured_image == '' && $final_content != '')
7192 + {
7193 + $dom = new DOMDocument();
7194 + $internalErrors = libxml_use_internal_errors(true);
7195 + $dom->loadHTML($final_content);
7196 + libxml_use_internal_errors($internalErrors);
7197 + $tags = $dom->getElementsByTagName('img');
7198 + foreach ($tags as $tag) {
7199 + $temp_get_img = $tag->getAttribute('src');
7200 + if ($temp_get_img != '') {
7201 + $temp_get_img = strtok($temp_get_img, '?');
7202 + $featured_image = rtrim($temp_get_img, '/');
7203 + }
7204 + }
7205 + }
7206 + $post_cats = '';
7207 + $post_categories = wp_get_post_categories( $post->ID );
7208 + foreach($post_categories as $c){
7209 + $cat = get_category( $c );
7210 + $post_cats .= $cat->name . ',';
7211 + }
7212 + $post_cats = trim($post_cats, ',');
7213 + if($post_cats != '')
7214 + {
7215 + $post_categories = explode(',', $post_cats);
7216 + }
7217 + else
7218 + {
7219 + $post_categories = array();
7220 + }
7221 + if(count($post_categories) == 0)
7222 + {
7223 + $terms = get_the_terms( $post->ID, 'product_cat' );
7224 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
7225 + foreach ( $terms as $term ) {
7226 + $post_categories[] = $term->slug;
7227 + }
7228 + $post_cats = implode(',', $post_categories);
7229 + }
7230 +
7231 + }
7232 + foreach($post_categories as $pc)
7233 + {
7234 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
7235 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
7236 + {
7237 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
7238 + {
7239 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7240 + {
7241 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
7242 + }
7243 + update_post_meta($post->ID, $custom_name, "skip");
7244 + return;
7245 + }
7246 + }
7247 + }
7248 + }
7249 + $post_tagz = '';
7250 + $post_tags = wp_get_post_tags( $post->ID );
7251 + foreach($post_tags as $t){
7252 + $post_tagz .= $t->name . ',';
7253 + }
7254 + $post_tagz = trim($post_tagz, ',');
7255 + if($post_tagz != '')
7256 + {
7257 + $post_tags = explode(',', $post_tagz);
7258 + }
7259 + else
7260 + {
7261 + $post_tags = array();
7262 + }
7263 + if(count($post_tags) == 0)
7264 + {
7265 + $terms = get_the_terms( $post->ID, $tag_slug );
7266 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
7267 + foreach ( $terms as $term ) {
7268 + $post_tags[] = $term->slug;
7269 + }
7270 + $post_tagz = implode(',', $post_tags);
7271 + }
7272 +
7273 + }
7274 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
7275 +
7276 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
7277 + foreach($disable_users as $disable_user)
7278 + {
7279 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
7280 + {
7281 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7282 + {
7283 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
7284 + }
7285 + update_post_meta($post->ID, $custom_name, "skip");
7286 + return;
7287 + }
7288 + }
7289 + }
7290 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
7291 +
7292 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
7293 + $found = false;
7294 + foreach($enable_users as $disable_user)
7295 + {
7296 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
7297 + {
7298 + $found = true;
7299 + }
7300 + }
7301 + if($found === false)
7302 + {
7303 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7304 + {
7305 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
7306 + }
7307 + update_post_meta($post->ID, $custom_name, "skip");
7308 + return;
7309 + }
7310 + }
7311 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
7312 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
7313 + {
7314 + if ( !has_post_thumbnail($post->ID) )
7315 + {
7316 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7317 + {
7318 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
7319 + }
7320 + update_post_meta($post->ID, $custom_name, "skip");
7321 + return;
7322 + }
7323 + }
7324 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
7325 + {
7326 + if ( has_post_thumbnail($post->ID) )
7327 + {
7328 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7329 + {
7330 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
7331 + }
7332 + update_post_meta($post->ID, $custom_name, "skip");
7333 + return;
7334 + }
7335 + }
7336 + }
7337 +
7338 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
7339 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
7340 + {
7341 + if ( !aiomatic_check_seo_description($post->ID) )
7342 + {
7343 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7344 + {
7345 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
7346 + }
7347 + update_post_meta($post->ID, $custom_name, "skip");
7348 + return;
7349 + }
7350 + }
7351 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
7352 + {
7353 + if ( aiomatic_check_seo_description($post->ID) )
7354 + {
7355 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7356 + {
7357 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
7358 + }
7359 + update_post_meta($post->ID, $custom_name, "skip");
7360 + return;
7361 + }
7362 + }
7363 + }
7364 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
7365 + $skip_me = true;
7366 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
7367 + foreach($enable_field as $fieldx)
7368 + {
7369 + if(strstr($fieldx, '=>') !== false)
7370 + {
7371 + $fna = explode('=>', $fieldx);
7372 + if(isset($fna[1]))
7373 + {
7374 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
7375 + if ($custom_val)
7376 + {
7377 + if(trim($fna[1]) == '*')
7378 + {
7379 + $skip_me = false;
7380 + }
7381 + if($custom_val == trim($fna[1]))
7382 + {
7383 + $skip_me = false;
7384 + }
7385 + }
7386 + }
7387 + }
7388 + }
7389 + if($skip_me === true)
7390 + {
7391 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7392 + {
7393 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
7394 + }
7395 + update_post_meta($post->ID, $custom_name, "skip");
7396 + return;
7397 + }
7398 + }
7399 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
7400 + $skip_me = false;
7401 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
7402 + foreach($disable_field as $fieldx)
7403 + {
7404 + if(strstr($fieldx, '=>') !== false)
7405 + {
7406 + $fna = explode('=>', $fieldx);
7407 + if(isset($fna[1]))
7408 + {
7409 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
7410 + if ($custom_val)
7411 + {
7412 + if(trim($fna[1]) == '*')
7413 + {
7414 + $skip_me = true;
7415 + }
7416 + if($custom_val == trim($fna[1]))
7417 + {
7418 + $skip_me = true;
7419 + }
7420 + }
7421 + }
7422 + }
7423 + }
7424 + if($skip_me === true)
7425 + {
7426 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7427 + {
7428 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
7429 + }
7430 + update_post_meta($post->ID, $custom_name, "skip");
7431 + return;
7432 + }
7433 + }
7434 + foreach($post_tags as $pt)
7435 + {
7436 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
7437 +
7438 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
7439 + foreach($disable_tags as $disabled_tag)
7440 + {
7441 + if($manual != true && trim($pt) == trim($disabled_tag))
7442 + {
7443 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7444 + {
7445 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
7446 + }
7447 + update_post_meta($post->ID, $custom_name, "skip");
7448 + return;
7449 + }
7450 + }
7451 + }
7452 + }
7453 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
7454 + $skip = true;
7455 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
7456 + foreach($post_tags as $pt)
7457 + {
7458 + foreach($enable_tags as $enable_tag)
7459 + {
7460 + if(trim($pt) == trim($enable_tag))
7461 + {
7462 + $skip = false;
7463 + break;
7464 + }
7465 + }
7466 + }
7467 + if($skip == true)
7468 + {
7469 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7470 + {
7471 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
7472 + }
7473 + update_post_meta($post->ID, $custom_name, "skip");
7474 + return;
7475 + }
7476 + }
7477 + $ai_tags = aiomatic_replaceAIPostShortcodes($ai_tags, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
7478 + $ai_tags = trim($ai_tags);
7479 + if (isset($aiomatic_Spinner_Settings['tags_model']) && $aiomatic_Spinner_Settings['tags_model'] != '') {
7480 + $cmodel = $aiomatic_Spinner_Settings['tags_model'];
7481 + }
7482 + else
7483 + {
7484 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
7485 + }
7486 + if (isset($aiomatic_Spinner_Settings['tags_assistant_id']) && $aiomatic_Spinner_Settings['tags_assistant_id'] != '') {
7487 + $tags_assistant_id = $aiomatic_Spinner_Settings['tags_assistant_id'];
7488 + }
7489 + else
7490 + {
7491 + $tags_assistant_id = '';
7492 + }
7493 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_tags, $mxatches);
7494 + if(isset($mxatches[2][0]))
7495 + {
7496 + $minx = $mxatches[1][0];
7497 + $maxx = $mxatches[2][0];
7498 + $max_tags = rand(intval($minx), intval($maxx));
7499 + }
7500 + else
7501 + {
7502 + $max_tags = intval($max_tags);
7503 + }
7504 + $author_obj = get_user_by('id', $post->post_author);
7505 + if(isset($author_obj->user_nicename))
7506 + {
7507 + $xuser_name = $author_obj->user_nicename;
7508 + }
7509 + else
7510 + {
7511 + $xuser_name = get_bloginfo( 'name' );
7512 + }
7513 + $xpost_link = get_permalink($post->ID);
7514 + $ai_tags_new = $ai_tags;
7515 + $userid = false;
7516 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
7517 + $query_token_count = count(aiomatic_encode($ai_tags_new));
7518 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
7519 + {
7520 + $ai_tags_new = aiomatic_substr($ai_tags_new, 0, (0-($max_seed_tokens * 4)));
7521 + $query_token_count = count(aiomatic_encode($ai_tags_new));
7522 + }
7523 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
7524 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $ai_tags_new, $query_token_count);
7525 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
7526 + if($available_tokens > $max_result_tokens)
7527 + {
7528 + $available_tokens = $max_result_tokens;
7529 + }
7530 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
7531 + {
7532 + $string_len = aiomatic_strlen($ai_tags_new);
7533 + $string_len = $string_len / 2;
7534 + $string_len = intval(0 - $string_len);
7535 + $ai_tags_new = aiomatic_substr($ai_tags_new, 0, $string_len);
7536 + $ai_tags_new = trim($ai_tags_new);
7537 + if(empty($ai_tags_new))
7538 + {
7539 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_tags_new, true));
7540 + }
7541 + $query_token_count = count(aiomatic_encode($ai_tags_new));
7542 + $available_tokens = $max_tokens - $query_token_count;
7543 + }
7544 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7545 + {
7546 + $api_service = aiomatic_get_api_service($token, $cmodel);
7547 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $tags_assistant_id . '\\' . $cmodel . ')(' . $available_tokens . ') post tags generator, with seed command: ' . $ai_tags_new);
7548 + }
7549 + $aierror = '';
7550 + $extra_tags = '';
7551 + $finish_reason = '';
7552 + $generated_text = aiomatic_generate_text($token, $cmodel, $ai_tags_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagsGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $tags_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
7553 + if($generated_text === false)
7554 + {
7555 + aiomatic_log_to_file('Tags generator error: ' . $aierror);
7556 + }
7557 + else
7558 + {
7559 + $extra_tags = ucfirst(trim(nl2br(trim($generated_text))));
7560 + $extra_tags = str_replace('//', '', $extra_tags);
7561 + }
7562 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($extra_tags) !== '')
7563 + {
7564 + $api_service = aiomatic_get_api_service($token, $cmodel);
7565 + aiomatic_log_to_file($api_service . ' responded successfully, tag generated for post ID ' . $post->ID . ': ' . $extra_tags);
7566 + }
7567 + if($extra_tags != '')
7568 + {
7569 + $extra_tagsz = explode(',', $extra_tags);
7570 + $added_tags = 0;
7571 + foreach($extra_tagsz as $extra_tag)
7572 + {
7573 + if($added_tags >= $max_tags)
7574 + {
7575 + break;
7576 + }
7577 + $extra_tag = trim($extra_tag);
7578 + $extra_tag = strip_tags($extra_tag);
7579 + $extra_tag = preg_replace('#^\d+\.\s*#', '', $extra_tag);
7580 + $extra_tag = str_replace('#', '', $extra_tag);
7581 + if(empty($extra_tag))
7582 + {
7583 + continue;
7584 + }
7585 + if (isset($aiomatic_Spinner_Settings['skip_inexist_tags']) && $aiomatic_Spinner_Settings['skip_inexist_tags'] == 'on')
7586 + {
7587 + if(!term_exists($extra_tag, $tag_slug))
7588 + {
7589 + aiomatic_log_to_file('Skipping tag, as it is not found on this site: ' . $extra_tag);
7590 + continue;
7591 + }
7592 + }
7593 + $added_tags++;
7594 + $term = get_term_by('name', trim($extra_tag), $tag_slug);
7595 + $term_id = '';
7596 + if (!$term) {
7597 + $term = wp_insert_term(trim($extra_tag), $tag_slug);
7598 + if (is_wp_error($term)) {
7599 + aiomatic_log_to_file('Failed to insert new term: ' . $extra_tag . ' - error: ' . $term->get_error_message());
7600 + continue;
7601 + }
7602 + $term_id = $term['term_id'];
7603 + }
7604 + else
7605 + {
7606 + $term_id = $term->term_id;
7607 + }
7608 + if ($term_id) {
7609 + wp_set_object_terms($post->ID, array($term_id), $tag_slug, true);
7610 + }
7611 + }
7612 + if($added_tags > 0)
7613 + {
7614 + update_post_meta($post->ID, $custom_name, "pub");
7615 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
7616 + {
7617 + $args = array();
7618 + $args['ID'] = $post->ID;
7619 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
7620 + remove_filter('content_save_pre', 'wp_filter_post_kses');
7621 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7622 + remove_filter('title_save_pre', 'wp_filter_kses');
7623 + $post_updated = wp_update_post($args);
7624 + $changes_made = true;
7625 + add_filter('content_save_pre', 'wp_filter_post_kses');
7626 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7627 + add_filter('title_save_pre', 'wp_filter_kses');
7628 + if (is_wp_error($post_updated)) {
7629 + $errors = $post_updated->get_error_messages();
7630 + foreach ($errors as $error) {
7631 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
7632 + }
7633 + }
7634 + }
7635 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
7636 + {
7637 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
7638 + if($date_stamp)
7639 + {
7640 + $args = array();
7641 + $args['ID'] = $post->ID;
7642 + $postdate = date("Y-m-d H:i:s", $date_stamp);
7643 + $args['post_date'] = $postdate;
7644 + remove_filter('content_save_pre', 'wp_filter_post_kses');
7645 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7646 + remove_filter('title_save_pre', 'wp_filter_kses');
7647 + $post_updated = wp_update_post($args);
7648 + $changes_made = true;
7649 + add_filter('content_save_pre', 'wp_filter_post_kses');
7650 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
7651 + add_filter('title_save_pre', 'wp_filter_kses');
7652 + if (is_wp_error($post_updated)) {
7653 + $errors = $post_updated->get_error_messages();
7654 + foreach ($errors as $error) {
7655 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
7656 + }
7657 + }
7658 + }
7659 + }
7660 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
7661 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated tags.');
7662 + }
7663 + }
7664 + }
7665 + }
7666 + }
7667 + if (isset($aiomatic_Spinner_Settings['add_custom']) && $aiomatic_Spinner_Settings['add_custom'] != '' && $aiomatic_Spinner_Settings['add_custom'] != 'disabled')
7668 + {
7669 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7670 + {
7671 + aiomatic_log_to_file('Starting post custom taxonomy creator...');
7672 + }
7673 + if (isset($aiomatic_Spinner_Settings['max_custom']) && $aiomatic_Spinner_Settings['max_custom'] != '')
7674 + {
7675 + $max_custom = trim($aiomatic_Spinner_Settings['max_custom']);
7676 + }
7677 + else
7678 + {
7679 + $max_custom = '1';
7680 + }
7681 + if($max_custom !== '')
7682 + {
7683 + if (isset($aiomatic_Spinner_Settings['ai_vision_custom']) && $aiomatic_Spinner_Settings['ai_vision_custom'] == 'on')
7684 + {
7685 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
7686 + if($avatar !== false)
7687 + {
7688 + $vision_file = $avatar;
7689 + }
7690 + }
7691 + if (isset($aiomatic_Spinner_Settings['ai_custom_tax']) && $aiomatic_Spinner_Settings['ai_custom_tax'] != '')
7692 + {
7693 + $ai_custom_tax_arr = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['ai_custom_tax']));
7694 + $added_tax = 0;
7695 + foreach($ai_custom_tax_arr as $ai_custom_tax_full)
7696 + {
7697 + $ai_custom_tax_full = trim($ai_custom_tax_full);
7698 + if($ai_custom_tax_full == '')
7699 + {
7700 + continue;
7701 + }
7702 + if(strstr($ai_custom_tax_full, '=>') === false)
7703 + {
7704 + aiomatic_log_to_file('Invalid custom taxonomy creator format sent. Correct format is: taxonomy_slug => AI prompt. Sent: ' . $ai_custom_tax_full);
7705 + continue;
7706 + }
7707 + $ai_custom_tax_parts = explode('=>', $ai_custom_tax_full);
7708 + $ai_custom_tax = trim($ai_custom_tax_parts[1]);
7709 + $ai_custom_tax_slug = trim($ai_custom_tax_parts[0]);
7710 + if(empty($ai_custom_tax))
7711 + {
7712 + aiomatic_log_to_file('Invalid custom taxonomy creator prompt sent. Correct format is: taxonomy_slug => AI prompt. Sent: ' . $ai_custom_tax_full);
7713 + continue;
7714 + }
7715 + if(empty($ai_custom_tax_slug))
7716 + {
7717 + aiomatic_log_to_file('Invalid custom taxonomy creator slug sent. Correct format is: taxonomy_slug => AI prompt. Sent: ' . $ai_custom_tax_full);
7718 + continue;
7719 + }
7720 + $ai_custom_tax = aiomatic_replaceSynergyShortcodes($ai_custom_tax);
7721 + $post_link = get_permalink($post->ID);
7722 + $blog_title = html_entity_decode(get_bloginfo('title'));
7723 + $author_obj = get_user_by('id', $post->post_author);
7724 + if($author_obj !== false && isset($author_obj->user_nicename))
7725 + {
7726 + $user_name = $author_obj->user_nicename;
7727 + }
7728 + else
7729 + {
7730 + $user_name = '';
7731 + }
7732 + $featured_image = '';
7733 + wp_suspend_cache_addition(true);
7734 + $metas = get_post_custom($post->ID);
7735 + wp_suspend_cache_addition(false);
7736 + if(is_array($metas))
7737 + {
7738 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
7739 + }
7740 + else
7741 + {
7742 + $rez_meta = array();
7743 + }
7744 + if(count($rez_meta) > 0)
7745 + {
7746 + foreach($rez_meta as $rm)
7747 + {
7748 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
7749 + {
7750 + $featured_image = $rm[0];
7751 + break;
7752 + }
7753 + }
7754 + }
7755 + if($featured_image == '')
7756 + {
7757 + $featured_image = aiomatic_generate_thumbmail($post->ID);
7758 + }
7759 + if($featured_image == '' && $final_content != '')
7760 + {
7761 + $dom = new DOMDocument();
7762 + $internalErrors = libxml_use_internal_errors(true);
7763 + $dom->loadHTML($final_content);
7764 + libxml_use_internal_errors($internalErrors);
7765 + $tags = $dom->getElementsByTagName('img');
7766 + foreach ($tags as $tag) {
7767 + $temp_get_img = $tag->getAttribute('src');
7768 + if ($temp_get_img != '') {
7769 + $temp_get_img = strtok($temp_get_img, '?');
7770 + $featured_image = rtrim($temp_get_img, '/');
7771 + }
7772 + }
7773 + }
7774 + $post_cats = '';
7775 + $post_categories = wp_get_post_categories( $post->ID );
7776 + foreach($post_categories as $c){
7777 + $cat = get_category( $c );
7778 + $post_cats .= $cat->name . ',';
7779 + }
7780 + $post_cats = trim($post_cats, ',');
7781 + if($post_cats != '')
7782 + {
7783 + $post_categories = explode(',', $post_cats);
7784 + }
7785 + else
7786 + {
7787 + $post_categories = array();
7788 + }
7789 + if(count($post_categories) == 0)
7790 + {
7791 + $terms = get_the_terms( $post->ID, 'product_cat' );
7792 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
7793 + foreach ( $terms as $term ) {
7794 + $post_categories[] = $term->slug;
7795 + }
7796 + $post_cats = implode(',', $post_categories);
7797 + }
7798 +
7799 + }
7800 + foreach($post_categories as $pc)
7801 + {
7802 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
7803 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
7804 + {
7805 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
7806 + {
7807 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7808 + {
7809 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
7810 + }
7811 + update_post_meta($post->ID, $custom_name, "skip");
7812 + return;
7813 + }
7814 + }
7815 + }
7816 + }
7817 + $post_tagz = '';
7818 + $post_tags = wp_get_post_tags( $post->ID );
7819 + foreach($post_tags as $t){
7820 + $post_tagz .= $t->name . ',';
7821 + }
7822 + $post_tagz = trim($post_tagz, ',');
7823 + if($post_tagz != '')
7824 + {
7825 + $post_tags = explode(',', $post_tagz);
7826 + }
7827 + else
7828 + {
7829 + $post_tags = array();
7830 + }
7831 + if(count($post_tags) == 0)
7832 + {
7833 + $terms = get_the_terms( $post->ID, 'product_tag' );
7834 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
7835 + foreach ( $terms as $term ) {
7836 + $post_tags[] = $term->slug;
7837 + }
7838 + $post_tagz = implode(',', $post_tags);
7839 + }
7840 +
7841 + }
7842 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
7843 +
7844 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
7845 + foreach($disable_users as $disable_user)
7846 + {
7847 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
7848 + {
7849 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7850 + {
7851 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
7852 + }
7853 + update_post_meta($post->ID, $custom_name, "skip");
7854 + return;
7855 + }
7856 + }
7857 + }
7858 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
7859 +
7860 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
7861 + $found = false;
7862 + foreach($enable_users as $disable_user)
7863 + {
7864 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
7865 + {
7866 + $found = true;
7867 + }
7868 + }
7869 + if($found === false)
7870 + {
7871 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7872 + {
7873 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
7874 + }
7875 + update_post_meta($post->ID, $custom_name, "skip");
7876 + return;
7877 + }
7878 + }
7879 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
7880 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
7881 + {
7882 + if ( !has_post_thumbnail($post->ID) )
7883 + {
7884 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7885 + {
7886 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
7887 + }
7888 + update_post_meta($post->ID, $custom_name, "skip");
7889 + return;
7890 + }
7891 + }
7892 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
7893 + {
7894 + if ( has_post_thumbnail($post->ID) )
7895 + {
7896 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7897 + {
7898 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
7899 + }
7900 + update_post_meta($post->ID, $custom_name, "skip");
7901 + return;
7902 + }
7903 + }
7904 + }
7905 +
7906 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
7907 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
7908 + {
7909 + if ( !aiomatic_check_seo_description($post->ID) )
7910 + {
7911 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7912 + {
7913 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
7914 + }
7915 + update_post_meta($post->ID, $custom_name, "skip");
7916 + return;
7917 + }
7918 + }
7919 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
7920 + {
7921 + if ( aiomatic_check_seo_description($post->ID) )
7922 + {
7923 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7924 + {
7925 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
7926 + }
7927 + update_post_meta($post->ID, $custom_name, "skip");
7928 + return;
7929 + }
7930 + }
7931 + }
7932 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
7933 + $skip_me = true;
7934 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
7935 + foreach($enable_field as $fieldx)
7936 + {
7937 + if(strstr($fieldx, '=>') !== false)
7938 + {
7939 + $fna = explode('=>', $fieldx);
7940 + if(isset($fna[1]))
7941 + {
7942 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
7943 + if ($custom_val)
7944 + {
7945 + if(trim($fna[1]) == '*')
7946 + {
7947 + $skip_me = false;
7948 + }
7949 + if($custom_val == trim($fna[1]))
7950 + {
7951 + $skip_me = false;
7952 + }
7953 + }
7954 + }
7955 + }
7956 + }
7957 + if($skip_me === true)
7958 + {
7959 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7960 + {
7961 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
7962 + }
7963 + update_post_meta($post->ID, $custom_name, "skip");
7964 + return;
7965 + }
7966 + }
7967 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
7968 + $skip_me = false;
7969 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
7970 + foreach($disable_field as $fieldx)
7971 + {
7972 + if(strstr($fieldx, '=>') !== false)
7973 + {
7974 + $fna = explode('=>', $fieldx);
7975 + if(isset($fna[1]))
7976 + {
7977 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
7978 + if ($custom_val)
7979 + {
7980 + if(trim($fna[1]) == '*')
7981 + {
7982 + $skip_me = true;
7983 + }
7984 + if($custom_val == trim($fna[1]))
7985 + {
7986 + $skip_me = true;
7987 + }
7988 + }
7989 + }
7990 + }
7991 + }
7992 + if($skip_me === true)
7993 + {
7994 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
7995 + {
7996 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
7997 + }
7998 + update_post_meta($post->ID, $custom_name, "skip");
7999 + return;
8000 + }
8001 + }
8002 + foreach($post_tags as $pt)
8003 + {
8004 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
8005 +
8006 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
8007 + foreach($disable_tags as $disabled_tag)
8008 + {
8009 + if($manual != true && trim($pt) == trim($disabled_tag))
8010 + {
8011 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8012 + {
8013 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
8014 + }
8015 + update_post_meta($post->ID, $custom_name, "skip");
8016 + return;
8017 + }
8018 + }
8019 + }
8020 + }
8021 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
8022 + $skip = true;
8023 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
8024 + foreach($post_tags as $pt)
8025 + {
8026 + foreach($enable_tags as $enable_tag)
8027 + {
8028 + if(trim($pt) == trim($enable_tag))
8029 + {
8030 + $skip = false;
8031 + break;
8032 + }
8033 + }
8034 + }
8035 + if($skip == true)
8036 + {
8037 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8038 + {
8039 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
8040 + }
8041 + update_post_meta($post->ID, $custom_name, "skip");
8042 + return;
8043 + }
8044 + }
8045 + $ai_custom_tax = aiomatic_replaceAIPostShortcodes($ai_custom_tax, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
8046 + $ai_custom_tax = trim($ai_custom_tax);
8047 + if (isset($aiomatic_Spinner_Settings['custom_model']) && $aiomatic_Spinner_Settings['custom_model'] != '') {
8048 + $cmodel = $aiomatic_Spinner_Settings['custom_model'];
8049 + }
8050 + else
8051 + {
8052 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
8053 + }
8054 + if (isset($aiomatic_Spinner_Settings['custom_assistant_id']) && $aiomatic_Spinner_Settings['custom_assistant_id'] != '') {
8055 + $custom_assistant_id = $aiomatic_Spinner_Settings['custom_assistant_id'];
8056 + }
8057 + else
8058 + {
8059 + $custom_assistant_id = '';
8060 + }
8061 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_custom, $mxatches);
8062 + if(isset($mxatches[2][0]))
8063 + {
8064 + $minx = $mxatches[1][0];
8065 + $maxx = $mxatches[2][0];
8066 + $max_custom = rand(intval($minx), intval($maxx));
8067 + }
8068 + else
8069 + {
8070 + $max_custom = intval($max_custom);
8071 + }
8072 + $author_obj = get_user_by('id', $post->post_author);
8073 + if(isset($author_obj->user_nicename))
8074 + {
8075 + $xuser_name = $author_obj->user_nicename;
8076 + }
8077 + else
8078 + {
8079 + $xuser_name = get_bloginfo( 'name' );
8080 + }
8081 + $xpost_link = get_permalink($post->ID);
8082 + $ai_custom_tax_new = $ai_custom_tax;
8083 + $userid = false;
8084 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
8085 + if (isset($aiomatic_Spinner_Settings['no_custom_tax_prompt']) && $aiomatic_Spinner_Settings['no_custom_tax_prompt'] == 'on')
8086 + {
8087 + $extra_tax = $ai_custom_field_new;
8088 + }
8089 + else
8090 + {
8091 + $query_token_count = count(aiomatic_encode($ai_custom_tax_new));
8092 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
8093 + {
8094 + $ai_custom_tax_new = aiomatic_substr($ai_custom_tax_new, 0, (0-($max_seed_tokens * 4)));
8095 + $query_token_count = count(aiomatic_encode($ai_custom_tax_new));
8096 + }
8097 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
8098 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $ai_custom_tax_new, $query_token_count);
8099 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
8100 + if($available_tokens > $max_result_tokens)
8101 + {
8102 + $available_tokens = $max_result_tokens;
8103 + }
8104 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
8105 + {
8106 + $string_len = aiomatic_strlen($ai_custom_tax_new);
8107 + $string_len = $string_len / 2;
8108 + $string_len = intval(0 - $string_len);
8109 + $ai_custom_tax_new = aiomatic_substr($ai_custom_tax_new, 0, $string_len);
8110 + $ai_custom_tax_new = trim($ai_custom_tax_new);
8111 + if(empty($ai_custom_tax_new))
8112 + {
8113 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_custom_tax_new, true));
8114 + }
8115 + $query_token_count = count(aiomatic_encode($ai_custom_tax_new));
8116 + $available_tokens = $max_tokens - $query_token_count;
8117 + }
8118 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8119 + {
8120 + $api_service = aiomatic_get_api_service($token, $cmodel);
8121 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $custom_assistant_id . '\\' . $cmodel . ')(' . $available_tokens . ') post custom taxonomy generator, with seed command: ' . $ai_custom_tax_new);
8122 + }
8123 + $aierror = '';
8124 + $extra_tax = '';
8125 + $finish_reason = '';
8126 + $generated_text = aiomatic_generate_text($token, $cmodel, $ai_custom_tax_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'customTaxonomyGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $custom_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
8127 + if($generated_text === false)
8128 + {
8129 + aiomatic_log_to_file('Custom Taxonomy generator error: ' . $aierror);
8130 + }
8131 + else
8132 + {
8133 + $extra_tax = ucfirst(trim(nl2br(trim($generated_text))));
8134 + $extra_tax = str_replace('//', '', $extra_tax);
8135 + }
8136 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($extra_tax) !== '')
8137 + {
8138 + $api_service = aiomatic_get_api_service($token, $cmodel);
8139 + aiomatic_log_to_file($api_service . ' responded successfully, custom taxonomy generated for post ID ' . $post->ID . ': ' . $extra_tax);
8140 + }
8141 + }
8142 + if($extra_tax != '')
8143 + {
8144 + $extra_taxes = explode(',', $extra_tax);
8145 + foreach($extra_taxes as $my_extra_tax)
8146 + {
8147 + if($added_tax >= $max_custom)
8148 + {
8149 + break;
8150 + }
8151 + $my_extra_tax = trim($my_extra_tax);
8152 + $my_extra_tax = strip_tags($my_extra_tax);
8153 + $my_extra_tax = preg_replace('#^\d+\.\s*#', '', $my_extra_tax);
8154 + if(empty($my_extra_tax))
8155 + {
8156 + continue;
8157 + }
8158 + if (isset($aiomatic_Spinner_Settings['skip_inexist_custom']) && $aiomatic_Spinner_Settings['skip_inexist_custom'] == 'on')
8159 + {
8160 + if(!term_exists($my_extra_tax, $ai_custom_tax_slug))
8161 + {
8162 + aiomatic_log_to_file('Skipping tag, as it is not found on this site: ' . $my_extra_tax);
8163 + continue;
8164 + }
8165 + }
8166 + $added_tax++;
8167 + $termid = aiomatic_create_terms($ai_custom_tax_slug, null, trim($my_extra_tax));
8168 + wp_set_post_terms($post->ID, $termid, $ai_custom_tax_slug, true);
8169 + }
8170 + }
8171 + }
8172 + if($added_tax > 0)
8173 + {
8174 + update_post_meta($post->ID, $custom_name, "pub");
8175 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
8176 + {
8177 + $args = array();
8178 + $args['ID'] = $post->ID;
8179 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
8180 + remove_filter('content_save_pre', 'wp_filter_post_kses');
8181 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8182 + remove_filter('title_save_pre', 'wp_filter_kses');
8183 + $post_updated = wp_update_post($args);
8184 + $changes_made = true;
8185 + add_filter('content_save_pre', 'wp_filter_post_kses');
8186 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8187 + add_filter('title_save_pre', 'wp_filter_kses');
8188 + if (is_wp_error($post_updated)) {
8189 + $errors = $post_updated->get_error_messages();
8190 + foreach ($errors as $error) {
8191 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
8192 + }
8193 + }
8194 + }
8195 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
8196 + {
8197 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
8198 + if($date_stamp)
8199 + {
8200 + $args = array();
8201 + $args['ID'] = $post->ID;
8202 + $postdate = date("Y-m-d H:i:s", $date_stamp);
8203 + $args['post_date'] = $postdate;
8204 + remove_filter('content_save_pre', 'wp_filter_post_kses');
8205 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8206 + remove_filter('title_save_pre', 'wp_filter_kses');
8207 + $post_updated = wp_update_post($args);
8208 + $changes_made = true;
8209 + add_filter('content_save_pre', 'wp_filter_post_kses');
8210 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8211 + add_filter('title_save_pre', 'wp_filter_kses');
8212 + if (is_wp_error($post_updated)) {
8213 + $errors = $post_updated->get_error_messages();
8214 + foreach ($errors as $error) {
8215 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
8216 + }
8217 + }
8218 + }
8219 + }
8220 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
8221 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated custom taxonomies.');
8222 + }
8223 + }
8224 + }
8225 + }
8226 + if (isset($aiomatic_Spinner_Settings['ai_custom_field']) && $aiomatic_Spinner_Settings['ai_custom_field'] != '')
8227 + {
8228 + $ai_custom_field_arr = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['ai_custom_field']));
8229 + $added_field = 0;
8230 + foreach($ai_custom_field_arr as $ai_custom_field_full)
8231 + {
8232 + $ai_custom_field_full = trim($ai_custom_field_full);
8233 + if($ai_custom_field_full == '')
8234 + {
8235 + continue;
8236 + }
8237 + if(strstr($ai_custom_field_full, '=>') === false)
8238 + {
8239 + aiomatic_log_to_file('Invalid custom field creator format sent. Correct format is: custom_field_slug => AI prompt. Sent: ' . $ai_custom_field_full);
8240 + continue;
8241 + }
8242 + $ai_custom_field_parts = explode('=>', $ai_custom_field_full);
8243 + $ai_custom_field = trim($ai_custom_field_parts[1]);
8244 + $ai_custom_field_slug = trim($ai_custom_field_parts[0]);
8245 + if(empty($ai_custom_field))
8246 + {
8247 + aiomatic_log_to_file('Invalid custom field creator prompt sent. Correct format is: custom_field_slug => AI prompt. Sent: ' . $ai_custom_field_full);
8248 + continue;
8249 + }
8250 + if(empty($ai_custom_field_slug))
8251 + {
8252 + aiomatic_log_to_file('Invalid custom field creator slug sent. Correct format is: custom_field_slug => AI prompt. Sent: ' . $ai_custom_field_full);
8253 + continue;
8254 + }
8255 + $ai_custom_field = aiomatic_replaceSynergyShortcodes($ai_custom_field);
8256 + $post_link = get_permalink($post->ID);
8257 + $blog_title = html_entity_decode(get_bloginfo('title'));
8258 + $author_obj = get_user_by('id', $post->post_author);
8259 + if($author_obj !== false && isset($author_obj->user_nicename))
8260 + {
8261 + $user_name = $author_obj->user_nicename;
8262 + }
8263 + else
8264 + {
8265 + $user_name = '';
8266 + }
8267 + $featured_image = '';
8268 + wp_suspend_cache_addition(true);
8269 + $metas = get_post_custom($post->ID);
8270 + wp_suspend_cache_addition(false);
8271 + if(is_array($metas))
8272 + {
8273 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
8274 + }
8275 + else
8276 + {
8277 + $rez_meta = array();
8278 + }
8279 + if(count($rez_meta) > 0)
8280 + {
8281 + foreach($rez_meta as $rm)
8282 + {
8283 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
8284 + {
8285 + $featured_image = $rm[0];
8286 + break;
8287 + }
8288 + }
8289 + }
8290 + if($featured_image == '')
8291 + {
8292 + $featured_image = aiomatic_generate_thumbmail($post->ID);
8293 + }
8294 + if($featured_image == '' && $final_content != '')
8295 + {
8296 + $dom = new DOMDocument();
8297 + $internalErrors = libxml_use_internal_errors(true);
8298 + $dom->loadHTML($final_content);
8299 + libxml_use_internal_errors($internalErrors);
8300 + $tags = $dom->getElementsByTagName('img');
8301 + foreach ($tags as $tag) {
8302 + $temp_get_img = $tag->getAttribute('src');
8303 + if ($temp_get_img != '') {
8304 + $temp_get_img = strtok($temp_get_img, '?');
8305 + $featured_image = rtrim($temp_get_img, '/');
8306 + }
8307 + }
8308 + }
8309 + $post_cats = '';
8310 + $post_categories = wp_get_post_categories( $post->ID );
8311 + foreach($post_categories as $c){
8312 + $cat = get_category( $c );
8313 + $post_cats .= $cat->name . ',';
8314 + }
8315 + $post_cats = trim($post_cats, ',');
8316 + if($post_cats != '')
8317 + {
8318 + $post_categories = explode(',', $post_cats);
8319 + }
8320 + else
8321 + {
8322 + $post_categories = array();
8323 + }
8324 + if(count($post_categories) == 0)
8325 + {
8326 + $terms = get_the_terms( $post->ID, 'product_cat' );
8327 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
8328 + foreach ( $terms as $term ) {
8329 + $post_categories[] = $term->slug;
8330 + }
8331 + $post_cats = implode(',', $post_categories);
8332 + }
8333 +
8334 + }
8335 + foreach($post_categories as $pc)
8336 + {
8337 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
8338 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
8339 + {
8340 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
8341 + {
8342 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8343 + {
8344 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
8345 + }
8346 + update_post_meta($post->ID, $custom_name, "skip");
8347 + return;
8348 + }
8349 + }
8350 + }
8351 + }
8352 + $post_tagz = '';
8353 + $post_tags = wp_get_post_tags( $post->ID );
8354 + foreach($post_tags as $t){
8355 + $post_tagz .= $t->name . ',';
8356 + }
8357 + $post_tagz = trim($post_tagz, ',');
8358 + if($post_tagz != '')
8359 + {
8360 + $post_tags = explode(',', $post_tagz);
8361 + }
8362 + else
8363 + {
8364 + $post_tags = array();
8365 + }
8366 + if(count($post_tags) == 0)
8367 + {
8368 + $terms = get_the_terms( $post->ID, 'product_tag' );
8369 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
8370 + foreach ( $terms as $term ) {
8371 + $post_tags[] = $term->slug;
8372 + }
8373 + $post_tagz = implode(',', $post_tags);
8374 + }
8375 +
8376 + }
8377 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
8378 +
8379 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
8380 + foreach($disable_users as $disable_user)
8381 + {
8382 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
8383 + {
8384 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8385 + {
8386 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
8387 + }
8388 + update_post_meta($post->ID, $custom_name, "skip");
8389 + return;
8390 + }
8391 + }
8392 + }
8393 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
8394 +
8395 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
8396 + $found = false;
8397 + foreach($enable_users as $disable_user)
8398 + {
8399 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
8400 + {
8401 + $found = true;
8402 + }
8403 + }
8404 + if($found === false)
8405 + {
8406 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8407 + {
8408 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
8409 + }
8410 + update_post_meta($post->ID, $custom_name, "skip");
8411 + return;
8412 + }
8413 + }
8414 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
8415 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
8416 + {
8417 + if ( !has_post_thumbnail($post->ID) )
8418 + {
8419 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8420 + {
8421 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
8422 + }
8423 + update_post_meta($post->ID, $custom_name, "skip");
8424 + return;
8425 + }
8426 + }
8427 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
8428 + {
8429 + if ( has_post_thumbnail($post->ID) )
8430 + {
8431 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8432 + {
8433 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
8434 + }
8435 + update_post_meta($post->ID, $custom_name, "skip");
8436 + return;
8437 + }
8438 + }
8439 + }
8440 +
8441 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
8442 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
8443 + {
8444 + if ( !aiomatic_check_seo_description($post->ID) )
8445 + {
8446 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8447 + {
8448 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
8449 + }
8450 + update_post_meta($post->ID, $custom_name, "skip");
8451 + return;
8452 + }
8453 + }
8454 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
8455 + {
8456 + if ( aiomatic_check_seo_description($post->ID) )
8457 + {
8458 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8459 + {
8460 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
8461 + }
8462 + update_post_meta($post->ID, $custom_name, "skip");
8463 + return;
8464 + }
8465 + }
8466 + }
8467 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
8468 + $skip_me = true;
8469 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
8470 + foreach($enable_field as $fieldx)
8471 + {
8472 + if(strstr($fieldx, '=>') !== false)
8473 + {
8474 + $fna = explode('=>', $fieldx);
8475 + if(isset($fna[1]))
8476 + {
8477 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
8478 + if ($custom_val)
8479 + {
8480 + if(trim($fna[1]) == '*')
8481 + {
8482 + $skip_me = false;
8483 + }
8484 + if($custom_val == trim($fna[1]))
8485 + {
8486 + $skip_me = false;
8487 + }
8488 + }
8489 + }
8490 + }
8491 + }
8492 + if($skip_me === true)
8493 + {
8494 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8495 + {
8496 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
8497 + }
8498 + update_post_meta($post->ID, $custom_name, "skip");
8499 + return;
8500 + }
8501 + }
8502 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
8503 + $skip_me = false;
8504 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
8505 + foreach($disable_field as $fieldx)
8506 + {
8507 + if(strstr($fieldx, '=>') !== false)
8508 + {
8509 + $fna = explode('=>', $fieldx);
8510 + if(isset($fna[1]))
8511 + {
8512 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
8513 + if ($custom_val)
8514 + {
8515 + if(trim($fna[1]) == '*')
8516 + {
8517 + $skip_me = true;
8518 + }
8519 + if($custom_val == trim($fna[1]))
8520 + {
8521 + $skip_me = true;
8522 + }
8523 + }
8524 + }
8525 + }
8526 + }
8527 + if($skip_me === true)
8528 + {
8529 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8530 + {
8531 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
8532 + }
8533 + update_post_meta($post->ID, $custom_name, "skip");
8534 + return;
8535 + }
8536 + }
8537 + foreach($post_tags as $pt)
8538 + {
8539 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
8540 +
8541 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
8542 + foreach($disable_tags as $disabled_tag)
8543 + {
8544 + if($manual != true && trim($pt) == trim($disabled_tag))
8545 + {
8546 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8547 + {
8548 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
8549 + }
8550 + update_post_meta($post->ID, $custom_name, "skip");
8551 + return;
8552 + }
8553 + }
8554 + }
8555 + }
8556 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
8557 + $skip = true;
8558 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
8559 + foreach($post_tags as $pt)
8560 + {
8561 + foreach($enable_tags as $enable_tag)
8562 + {
8563 + if(trim($pt) == trim($enable_tag))
8564 + {
8565 + $skip = false;
8566 + break;
8567 + }
8568 + }
8569 + }
8570 + if($skip == true)
8571 + {
8572 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8573 + {
8574 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
8575 + }
8576 + update_post_meta($post->ID, $custom_name, "skip");
8577 + return;
8578 + }
8579 + }
8580 + $ai_custom_field = aiomatic_replaceAIPostShortcodes($ai_custom_field, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
8581 + $ai_custom_field = trim($ai_custom_field);
8582 + if (isset($aiomatic_Spinner_Settings['custom_model']) && $aiomatic_Spinner_Settings['custom_model'] != '') {
8583 + $cmodel = $aiomatic_Spinner_Settings['custom_model'];
8584 + }
8585 + else
8586 + {
8587 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
8588 + }
8589 + if (isset($aiomatic_Spinner_Settings['custom_assistant_id']) && $aiomatic_Spinner_Settings['custom_assistant_id'] != '') {
8590 + $custom_assistant_id = $aiomatic_Spinner_Settings['custom_assistant_id'];
8591 + }
8592 + else
8593 + {
8594 + $custom_assistant_id = '';
8595 + }
8596 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_custom, $mxatches);
8597 + if(isset($mxatches[2][0]))
8598 + {
8599 + $minx = $mxatches[1][0];
8600 + $maxx = $mxatches[2][0];
8601 + $max_custom = rand(intval($minx), intval($maxx));
8602 + }
8603 + else
8604 + {
8605 + $max_custom = intval($max_custom);
8606 + }
8607 + $author_obj = get_user_by('id', $post->post_author);
8608 + if(isset($author_obj->user_nicename))
8609 + {
8610 + $xuser_name = $author_obj->user_nicename;
8611 + }
8612 + else
8613 + {
8614 + $xuser_name = get_bloginfo( 'name' );
8615 + }
8616 + $xpost_link = get_permalink($post->ID);
8617 + $ai_custom_field_new = $ai_custom_field;
8618 + $userid = false;
8619 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
8620 + if (isset($aiomatic_Spinner_Settings['no_custom_field_prompt']) && $aiomatic_Spinner_Settings['no_custom_field_prompt'] == 'on')
8621 + {
8622 + $extra_field = $ai_custom_field_new;
8623 + }
8624 + else
8625 + {
8626 + $query_token_count = count(aiomatic_encode($ai_custom_field_new));
8627 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
8628 + {
8629 + $ai_custom_field_new = aiomatic_substr($ai_custom_field_new, 0, (0-($max_seed_tokens * 4)));
8630 + $query_token_count = count(aiomatic_encode($ai_custom_field_new));
8631 + }
8632 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
8633 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $ai_custom_field_new, $query_token_count);
8634 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
8635 + if($available_tokens > $max_result_tokens)
8636 + {
8637 + $available_tokens = $max_result_tokens;
8638 + }
8639 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
8640 + {
8641 + $string_len = aiomatic_strlen($ai_custom_field_new);
8642 + $string_len = $string_len / 2;
8643 + $string_len = intval(0 - $string_len);
8644 + $ai_custom_field_new = aiomatic_substr($ai_custom_field_new, 0, $string_len);
8645 + $ai_custom_field_new = trim($ai_custom_field_new);
8646 + if(empty($ai_custom_field_new))
8647 + {
8648 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_custom_field_new, true));
8649 + }
8650 + $query_token_count = count(aiomatic_encode($ai_custom_field_new));
8651 + $available_tokens = $max_tokens - $query_token_count;
8652 + }
8653 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8654 + {
8655 + $api_service = aiomatic_get_api_service($token, $cmodel);
8656 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $custom_assistant_id . '\\' . $cmodel . ')(' . $available_tokens . ') post custom field generator, with seed command: ' . $ai_custom_field_new);
8657 + }
8658 + $aierror = '';
8659 + $extra_field = '';
8660 + $finish_reason = '';
8661 + $generated_text = aiomatic_generate_text($token, $cmodel, $ai_custom_field_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'customFieldGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $custom_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
8662 + if($generated_text === false)
8663 + {
8664 + aiomatic_log_to_file('Custom Field generator error: ' . $aierror);
8665 + }
8666 + else
8667 + {
8668 + $extra_field = ucfirst(trim(nl2br(trim($generated_text))));
8669 + $extra_field = str_replace('//', '', $extra_field);
8670 + }
8671 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($extra_field) !== '')
8672 + {
8673 + $api_service = aiomatic_get_api_service($token, $cmodel);
8674 + aiomatic_log_to_file($api_service . ' responded successfully, custom field generated for post ID ' . $post->ID . ': ' . $extra_field);
8675 + }
8676 + }
8677 + if($extra_field != '')
8678 + {
8679 + $extra_field = trim($extra_field);
8680 + if(empty($extra_field))
8681 + {
8682 + continue;
8683 + }
8684 + $added_field++;
8685 + update_post_meta($post->ID, $ai_custom_field_slug, $extra_field);
8686 + }
8687 + }
8688 + if($added_field > 0)
8689 + {
8690 + update_post_meta($post->ID, $custom_name, "pub");
8691 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
8692 + {
8693 + $args = array();
8694 + $args['ID'] = $post->ID;
8695 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
8696 + remove_filter('content_save_pre', 'wp_filter_post_kses');
8697 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8698 + remove_filter('title_save_pre', 'wp_filter_kses');
8699 + $post_updated = wp_update_post($args);
8700 + $changes_made = true;
8701 + add_filter('content_save_pre', 'wp_filter_post_kses');
8702 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8703 + add_filter('title_save_pre', 'wp_filter_kses');
8704 + if (is_wp_error($post_updated)) {
8705 + $errors = $post_updated->get_error_messages();
8706 + foreach ($errors as $error) {
8707 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
8708 + }
8709 + }
8710 + }
8711 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
8712 + {
8713 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
8714 + if($date_stamp)
8715 + {
8716 + $args = array();
8717 + $args['ID'] = $post->ID;
8718 + $postdate = date("Y-m-d H:i:s", $date_stamp);
8719 + $args['post_date'] = $postdate;
8720 + remove_filter('content_save_pre', 'wp_filter_post_kses');
8721 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8722 + remove_filter('title_save_pre', 'wp_filter_kses');
8723 + $post_updated = wp_update_post($args);
8724 + $changes_made = true;
8725 + add_filter('content_save_pre', 'wp_filter_post_kses');
8726 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
8727 + add_filter('title_save_pre', 'wp_filter_kses');
8728 + if (is_wp_error($post_updated)) {
8729 + $errors = $post_updated->get_error_messages();
8730 + foreach ($errors as $error) {
8731 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
8732 + }
8733 + }
8734 + }
8735 + }
8736 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
8737 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated custom fields.');
8738 + }
8739 + }
8740 + }
8741 + }
8742 + if (isset($aiomatic_Spinner_Settings['add_comments']) && $aiomatic_Spinner_Settings['add_comments'] != '' && $aiomatic_Spinner_Settings['add_comments'] != 'disabled')
8743 + {
8744 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8745 + {
8746 + aiomatic_log_to_file('Starting post auto commenter...');
8747 + }
8748 + if (isset($aiomatic_Spinner_Settings['max_comments']) && $aiomatic_Spinner_Settings['max_comments'] != '')
8749 + {
8750 + $max_comments = trim($aiomatic_Spinner_Settings['max_comments']);
8751 + }
8752 + else
8753 + {
8754 + $max_comments = '1-2';
8755 + }
8756 + if($max_comments !== '')
8757 + {
8758 + $vision_file = '';
8759 + if (isset($aiomatic_Spinner_Settings['ai_comments']) && $aiomatic_Spinner_Settings['ai_comments'] != '')
8760 + {
8761 + $ai_comments = $aiomatic_Spinner_Settings['ai_comments'];
8762 + }
8763 + else
8764 + {
8765 + $ai_comments = `Write a single comment (don't start a new line) for the post title: %%post_title%%
8766 + Previous comments are:
8767 + %%previous_comments%%
8768 + %%comment_author_name%%:`;
8769 + }
8770 + if (isset($aiomatic_Spinner_Settings['prev_comms']) && $aiomatic_Spinner_Settings['prev_comms'] != '')
8771 + {
8772 + $prev_comms = intval($aiomatic_Spinner_Settings['prev_comms']);
8773 + }
8774 + else
8775 + {
8776 + $prev_comms = 5;
8777 + }
8778 + $ai_comments = aiomatic_replaceSynergyShortcodes($ai_comments);
8779 +
8780 + if (isset($aiomatic_Spinner_Settings['ai_vision_com']) && $aiomatic_Spinner_Settings['ai_vision_com'] == 'on')
8781 + {
8782 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
8783 + if($avatar !== false)
8784 + {
8785 + $vision_file = $avatar;
8786 + }
8787 + }
8788 + $post_link = get_permalink($post->ID);
8789 + $blog_title = html_entity_decode(get_bloginfo('title'));
8790 + $author_obj = get_user_by('id', $post->post_author);
8791 + if($author_obj !== false && isset($author_obj->user_nicename))
8792 + {
8793 + $user_name = $author_obj->user_nicename;
8794 + }
8795 + else
8796 + {
8797 + $user_name = '';
8798 + }
8799 + $featured_image = '';
8800 + wp_suspend_cache_addition(true);
8801 + $metas = get_post_custom($post->ID);
8802 + wp_suspend_cache_addition(false);
8803 + if(is_array($metas))
8804 + {
8805 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
8806 + }
8807 + else
8808 + {
8809 + $rez_meta = array();
8810 + }
8811 + if(count($rez_meta) > 0)
8812 + {
8813 + foreach($rez_meta as $rm)
8814 + {
8815 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
8816 + {
8817 + $featured_image = $rm[0];
8818 + break;
8819 + }
8820 + }
8821 + }
8822 + if($featured_image == '')
8823 + {
8824 + $featured_image = aiomatic_generate_thumbmail($post->ID);
8825 + }
8826 + if($featured_image == '' && $final_content != '')
8827 + {
8828 + $dom = new DOMDocument();
8829 + $internalErrors = libxml_use_internal_errors(true);
8830 + $dom->loadHTML($final_content);
8831 + libxml_use_internal_errors($internalErrors);
8832 + $tags = $dom->getElementsByTagName('img');
8833 + foreach ($tags as $tag) {
8834 + $temp_get_img = $tag->getAttribute('src');
8835 + if ($temp_get_img != '') {
8836 + $temp_get_img = strtok($temp_get_img, '?');
8837 + $featured_image = rtrim($temp_get_img, '/');
8838 + }
8839 + }
8840 + }
8841 + $post_cats = '';
8842 + $post_categories = wp_get_post_categories( $post->ID );
8843 + foreach($post_categories as $c){
8844 + $cat = get_category( $c );
8845 + $post_cats .= $cat->name . ',';
8846 + }
8847 + $post_cats = trim($post_cats, ',');
8848 + if($post_cats != '')
8849 + {
8850 + $post_categories = explode(',', $post_cats);
8851 + }
8852 + else
8853 + {
8854 + $post_categories = array();
8855 + }
8856 + if(count($post_categories) == 0)
8857 + {
8858 + $terms = get_the_terms( $post->ID, 'product_cat' );
8859 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
8860 + foreach ( $terms as $term ) {
8861 + $post_categories[] = $term->slug;
8862 + }
8863 + $post_cats = implode(',', $post_categories);
8864 + }
8865 +
8866 + }
8867 + foreach($post_categories as $pc)
8868 + {
8869 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
8870 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
8871 + {
8872 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
8873 + {
8874 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8875 + {
8876 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
8877 + }
8878 + update_post_meta($post->ID, $custom_name, "skip");
8879 + return;
8880 + }
8881 + }
8882 + }
8883 + }
8884 + $post_tagz = '';
8885 + $post_tags = wp_get_post_tags( $post->ID );
8886 + foreach($post_tags as $t){
8887 + $post_tagz .= $t->name . ',';
8888 + }
8889 + $post_tagz = trim($post_tagz, ',');
8890 + if($post_tagz != '')
8891 + {
8892 + $post_tags = explode(',', $post_tagz);
8893 + }
8894 + else
8895 + {
8896 + $post_tags = array();
8897 + }
8898 + if(count($post_tags) == 0)
8899 + {
8900 + $terms = get_the_terms( $post->ID, 'product_tag' );
8901 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
8902 + foreach ( $terms as $term ) {
8903 + $post_tags[] = $term->slug;
8904 + }
8905 + $post_tagz = implode(',', $post_tags);
8906 + }
8907 +
8908 + }
8909 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
8910 +
8911 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
8912 + foreach($disable_users as $disable_user)
8913 + {
8914 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
8915 + {
8916 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8917 + {
8918 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
8919 + }
8920 + update_post_meta($post->ID, $custom_name, "skip");
8921 + return;
8922 + }
8923 + }
8924 + }
8925 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
8926 +
8927 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
8928 + $found = false;
8929 + foreach($enable_users as $disable_user)
8930 + {
8931 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
8932 + {
8933 + $found = true;
8934 + }
8935 + }
8936 + if($found === false)
8937 + {
8938 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8939 + {
8940 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
8941 + }
8942 + update_post_meta($post->ID, $custom_name, "skip");
8943 + return;
8944 + }
8945 + }
8946 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
8947 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
8948 + {
8949 + if ( !has_post_thumbnail($post->ID) )
8950 + {
8951 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8952 + {
8953 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
8954 + }
8955 + update_post_meta($post->ID, $custom_name, "skip");
8956 + return;
8957 + }
8958 + }
8959 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
8960 + {
8961 + if ( has_post_thumbnail($post->ID) )
8962 + {
8963 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8964 + {
8965 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
8966 + }
8967 + update_post_meta($post->ID, $custom_name, "skip");
8968 + return;
8969 + }
8970 + }
8971 + }
8972 +
8973 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
8974 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
8975 + {
8976 + if ( !aiomatic_check_seo_description($post->ID) )
8977 + {
8978 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8979 + {
8980 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
8981 + }
8982 + update_post_meta($post->ID, $custom_name, "skip");
8983 + return;
8984 + }
8985 + }
8986 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
8987 + {
8988 + if ( aiomatic_check_seo_description($post->ID) )
8989 + {
8990 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
8991 + {
8992 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
8993 + }
8994 + update_post_meta($post->ID, $custom_name, "skip");
8995 + return;
8996 + }
8997 + }
8998 + }
8999 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
9000 + $skip_me = true;
9001 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
9002 + foreach($enable_field as $fieldx)
9003 + {
9004 + if(strstr($fieldx, '=>') !== false)
9005 + {
9006 + $fna = explode('=>', $fieldx);
9007 + if(isset($fna[1]))
9008 + {
9009 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
9010 + if ($custom_val)
9011 + {
9012 + if(trim($fna[1]) == '*')
9013 + {
9014 + $skip_me = false;
9015 + }
9016 + if($custom_val == trim($fna[1]))
9017 + {
9018 + $skip_me = false;
9019 + }
9020 + }
9021 + }
9022 + }
9023 + }
9024 + if($skip_me === true)
9025 + {
9026 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9027 + {
9028 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
9029 + }
9030 + update_post_meta($post->ID, $custom_name, "skip");
9031 + return;
9032 + }
9033 + }
9034 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
9035 + $skip_me = false;
9036 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
9037 + foreach($disable_field as $fieldx)
9038 + {
9039 + if(strstr($fieldx, '=>') !== false)
9040 + {
9041 + $fna = explode('=>', $fieldx);
9042 + if(isset($fna[1]))
9043 + {
9044 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
9045 + if ($custom_val)
9046 + {
9047 + if(trim($fna[1]) == '*')
9048 + {
9049 + $skip_me = true;
9050 + }
9051 + if($custom_val == trim($fna[1]))
9052 + {
9053 + $skip_me = true;
9054 + }
9055 + }
9056 + }
9057 + }
9058 + }
9059 + if($skip_me === true)
9060 + {
9061 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9062 + {
9063 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
9064 + }
9065 + update_post_meta($post->ID, $custom_name, "skip");
9066 + return;
9067 + }
9068 + }
9069 + foreach($post_tags as $pt)
9070 + {
9071 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
9072 +
9073 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
9074 + foreach($disable_tags as $disabled_tag)
9075 + {
9076 + if($manual != true && trim($pt) == trim($disabled_tag))
9077 + {
9078 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9079 + {
9080 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
9081 + }
9082 + update_post_meta($post->ID, $custom_name, "skip");
9083 + return;
9084 + }
9085 + }
9086 + }
9087 + }
9088 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
9089 + $skip = true;
9090 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
9091 + foreach($post_tags as $pt)
9092 + {
9093 + foreach($enable_tags as $enable_tag)
9094 + {
9095 + if(trim($pt) == trim($enable_tag))
9096 + {
9097 + $skip = false;
9098 + break;
9099 + }
9100 + }
9101 + }
9102 + if($skip == true)
9103 + {
9104 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9105 + {
9106 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
9107 + }
9108 + update_post_meta($post->ID, $custom_name, "skip");
9109 + return;
9110 + }
9111 + }
9112 + $ai_comments = aiomatic_replaceAIPostShortcodes($ai_comments, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
9113 + $ai_comments = trim($ai_comments);
9114 + if (isset($aiomatic_Spinner_Settings['url_list']) && $aiomatic_Spinner_Settings['url_list'] != '') {
9115 + $blog_url_list = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['url_list']));
9116 + }
9117 + else
9118 + {
9119 + $blog_url_list = array('');
9120 + }
9121 + if (isset($aiomatic_Spinner_Settings['user_list']) && $aiomatic_Spinner_Settings['user_list'] != '') {
9122 + $blog_user_list = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['user_list']));
9123 + }
9124 + else
9125 + {
9126 + $blog_user_list = array('%%random_user%%');
9127 + }
9128 + if (isset($aiomatic_Spinner_Settings['email_list']) && $aiomatic_Spinner_Settings['email_list'] != '') {
9129 + $blog_email_list = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['email_list']));
9130 + }
9131 + else
9132 + {
9133 + $blog_email_list = array(aiomatic_get_random_word(4, 10) . '@' . aiomatic_get_random_word(4, 8) . '.com');
9134 + }
9135 + if (isset($aiomatic_Spinner_Settings['comments_model']) && $aiomatic_Spinner_Settings['comments_model'] != '') {
9136 + $cmodel = $aiomatic_Spinner_Settings['comments_model'];
9137 + }
9138 + else
9139 + {
9140 + $cmodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
9141 + }
9142 + if (isset($aiomatic_Spinner_Settings['comments_assistant_id']) && $aiomatic_Spinner_Settings['comments_assistant_id'] != '') {
9143 + $comments_assistant_id = $aiomatic_Spinner_Settings['comments_assistant_id'];
9144 + }
9145 + else
9146 + {
9147 + $comments_assistant_id = '';
9148 + }
9149 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_comments, $mxatches);
9150 + if(isset($mxatches[2][0]))
9151 + {
9152 + $minx = $mxatches[1][0];
9153 + $maxx = $mxatches[2][0];
9154 + $max_comments = rand(intval($minx), intval($maxx));
9155 + }
9156 + else
9157 + {
9158 + $max_comments = intval($max_comments);
9159 + }
9160 + $author_obj = get_user_by('id', $post->post_author);
9161 + if(isset($author_obj->user_login))
9162 + {
9163 + $xuser_name = $author_obj->user_login;
9164 + }
9165 + else
9166 + {
9167 + $xuser_name = get_bloginfo( 'name' );
9168 + }
9169 + $xpost_link = get_permalink($post->ID);
9170 + $comms_add = false;
9171 + for($i = 0; $i < $max_comments; $i++)
9172 + {
9173 + $star_count = '';
9174 + if (isset($aiomatic_Spinner_Settings['star_count']) && $aiomatic_Spinner_Settings['star_count'] != '')
9175 + {
9176 + $star_count = $aiomatic_Spinner_Settings['star_count'];
9177 + }
9178 + if(strstr($star_count, '-') !== false)
9179 + {
9180 + list($min, $max) = explode('-', $star_count);
9181 + if(is_numeric($min) && is_numeric($max) && intval($min) <= intval($max))
9182 + {
9183 + $min = (int)trim($min);
9184 + $max = (int)trim($max);
9185 + $star_count = rand($min, $max);
9186 + }
9187 + else
9188 + {
9189 + $star_count = '';
9190 + }
9191 + }
9192 + else
9193 + {
9194 + if(is_numeric($star_count))
9195 + {
9196 + $star_count = intval($star_count);
9197 + }
9198 + else
9199 + {
9200 + $star_count = '';
9201 + }
9202 + }
9203 + $userid = 0;
9204 + $comm_url = trim($blog_url_list[array_rand($blog_url_list)]);
9205 + $cauthor = trim($blog_user_list[array_rand($blog_user_list)]);
9206 + $cmail = trim($blog_email_list[array_rand($blog_email_list)]);
9207 + $cauthor = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $cauthor);
9208 + $cauthor = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $cauthor);
9209 + $cauthor = str_replace('%%author_name%%', $xuser_name, $cauthor);
9210 + if(strstr($cauthor, '%%random_new_name%%') !== false)
9211 + {
9212 + $cauthor = aiomatic_generateComplexRandomName();
9213 + }
9214 + elseif(strstr($cauthor, '%%random_user%%') !== false)
9215 + {
9216 + global $wpdb;
9217 + $random_user_id = $wpdb->get_var( "
9218 + SELECT ID
9219 + FROM {$wpdb->users}
9220 + ORDER BY RAND()
9221 + LIMIT 1
9222 + " );
9223 + if ( $random_user_id )
9224 + {
9225 + $userid = $random_user_id;
9226 + $xuser = get_user_by( 'id', $userid );
9227 + if($xuser !== false)
9228 + {
9229 + $cauthor = str_replace('%%random_user%%', $xuser->display_name, $cauthor);
9230 + }
9231 + else
9232 + {
9233 + $cauthor = str_replace('%%random_user%%', '', $cauthor);
9234 + }
9235 + }
9236 + }
9237 + else
9238 + {
9239 + $user = get_user_by('login', $cauthor);
9240 + if($user !== false)
9241 + {
9242 + $userid = $user->ID;
9243 + }
9244 + }
9245 + $cmail = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $cmail);
9246 + $cmail = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $cmail);
9247 + $comm_url = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $comm_url);
9248 + $comm_url = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $comm_url);
9249 + $comm_url = str_replace('%%post_link%%', $xpost_link, $comm_url);
9250 + if (isset($aiomatic_Spinner_Settings['max_time']) && $aiomatic_Spinner_Settings['max_time'] != '' && isset($aiomatic_Spinner_Settings['min_time']) && $aiomatic_Spinner_Settings['min_time'] != '')
9251 + {
9252 + $t1 = strtotime($aiomatic_Spinner_Settings['min_time']);
9253 + $t2 = strtotime($aiomatic_Spinner_Settings['max_time']);
9254 + if($t1 !== false && $t2 !== false)
9255 + {
9256 + $int = rand($t1, $t2);
9257 + $date = date('Y-m-d H:i:s', $int);
9258 + $gdate = gmdate('Y-m-d H:i:s', $int);
9259 + }
9260 + else
9261 + {
9262 + $date = date('Y-m-d H:i:s');
9263 + $gdate = gmdate('Y-m-d H:i:s');
9264 + }
9265 + }
9266 + else
9267 + {
9268 + $date = date('Y-m-d H:i:s');
9269 + $gdate = gmdate('Y-m-d H:i:s');
9270 + }
9271 + $ai_comments_new = str_replace('%%comment_author_name%%', $cauthor, $ai_comments);
9272 + $ai_comments_new = str_replace('%%comment_author_email%%', $cmail, $ai_comments_new);
9273 + $ai_comments_new = str_replace('%%comment_author_url%%', $comm_url, $ai_comments_new);
9274 + $ai_comments_new = str_replace('%%product_star_rating%%', $star_count, $ai_comments_new);
9275 + if(strstr($ai_comments_new, '%%previous_comments%%') !== false)
9276 + {
9277 + $comments = get_comments(array(
9278 + 'post_id' => $post->ID,
9279 + 'number' => $prev_comms,
9280 + 'orderby' => 'comment_date',
9281 + 'order' => 'DESC'
9282 + ));
9283 + $older_comms = '';
9284 + foreach($comments as $comment) {
9285 + $older_comms .= $comment->comment_author . ': ' . $comment->comment_content . '\r\n';
9286 + }
9287 + $ai_comments_new = str_replace('%%previous_comments%%', $older_comms, $ai_comments_new);
9288 + }
9289 + $query_token_count = count(aiomatic_encode($ai_comments_new));
9290 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
9291 + {
9292 + $ai_comments_new = aiomatic_substr($ai_comments_new, 0, (0-($max_seed_tokens * 4)));
9293 + $query_token_count = count(aiomatic_encode($ai_comments_new));
9294 + }
9295 + $max_tokens = aimogenpro_get_max_tokens($cmodel);
9296 + $available_tokens = aiomatic_compute_available_tokens($cmodel, $max_tokens, $ai_comments_new, $query_token_count);
9297 + $max_result_tokens = aimogenpro_get_max_tokens($cmodel);
9298 + if($available_tokens > $max_result_tokens)
9299 + {
9300 + $available_tokens = $max_result_tokens;
9301 + }
9302 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
9303 + {
9304 + $string_len = aiomatic_strlen($ai_comments_new);
9305 + $string_len = $string_len / 2;
9306 + $string_len = intval(0 - $string_len);
9307 + $ai_comments_new = aiomatic_substr($ai_comments_new, 0, $string_len);
9308 + $ai_comments_new = trim($ai_comments_new);
9309 + if(empty($ai_comments_new))
9310 + {
9311 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_comments_new, true));
9312 + }
9313 + $query_token_count = count(aiomatic_encode($ai_comments_new));
9314 + $available_tokens = $max_tokens - $query_token_count;
9315 + }
9316 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9317 + {
9318 + $api_service = aiomatic_get_api_service($token, $cmodel);
9319 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $comments_assistant_id , '/' . $cmodel . ')(' . $available_tokens . ') post comment generator, with seed command: ' . $ai_comments_new);
9320 + }
9321 + $aierror = '';
9322 + $comm = '';
9323 + $finish_reason = '';
9324 + $generated_text = aiomatic_generate_text($token, $cmodel, $ai_comments_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'commentGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $comments_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
9325 + if($generated_text === false)
9326 + {
9327 + aiomatic_log_to_file('Comment generator error: ' . $aierror);
9328 + }
9329 + else
9330 + {
9331 + $comm = ucfirst(trim(nl2br(trim($generated_text))));
9332 + $comm = str_replace('//', '', $comm);
9333 + }
9334 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($comm) !== '')
9335 + {
9336 + $api_service = aiomatic_get_api_service($token, $cmodel);
9337 + aiomatic_log_to_file($api_service . ' responded successfully, comment generated for post ID: ' . $post->ID);
9338 + }
9339 + $comment_type = '';
9340 + if(!empty($star_count) && $post->post_type == 'product')
9341 + {
9342 + $comment_type = 'review';
9343 + }
9344 + if (trim($comm) != '')
9345 + {
9346 + $data = array(
9347 + 'comment_post_ID' => $post->ID,
9348 + 'comment_author' => $cauthor,
9349 + 'comment_author_email' => $cmail,
9350 + 'comment_author_url' => $comm_url,
9351 + 'comment_content' => $comm,
9352 + 'comment_type' => $comment_type,
9353 + 'comment_parent' => 0,
9354 + 'user_id' => $userid,
9355 + 'comment_author_IP' => aiomatic_generateRandomIP(),
9356 + 'comment_agent' => aiomatic_get_random_user_agent(),
9357 + 'comment_date' => $date,
9358 + 'comment_date_gmt' => $gdate
9359 + );
9360 + if (isset($aiomatic_Spinner_Settings['no_approve']) && $aiomatic_Spinner_Settings['no_approve'] == 'on')
9361 + {
9362 + $data['comment_approved'] = 0;
9363 + }
9364 + else
9365 + {
9366 + $data['comment_approved'] = 1;
9367 + }
9368 + $comment_id = wp_insert_comment($data);
9369 + if($comment_id === false)
9370 + {
9371 + aiomatic_log_to_file('Failed to insert comment to post ID: ' . $post->ID);
9372 + }
9373 + else
9374 + {
9375 + $comms_add = true;
9376 + if(!empty($star_count) && $post->post_type == 'product')
9377 + {
9378 + update_comment_meta($comment_id, 'rating', $star_count);
9379 + }
9380 + }
9381 + }
9382 + }
9383 + if($comms_add == true)
9384 + {
9385 + update_post_meta($post->ID, $custom_name, "pub");
9386 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
9387 + {
9388 + $args = array();
9389 + $args['ID'] = $post->ID;
9390 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
9391 + remove_filter('content_save_pre', 'wp_filter_post_kses');
9392 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9393 + remove_filter('title_save_pre', 'wp_filter_kses');
9394 + $post_updated = wp_update_post($args);
9395 + $changes_made = true;
9396 + add_filter('content_save_pre', 'wp_filter_post_kses');
9397 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9398 + add_filter('title_save_pre', 'wp_filter_kses');
9399 + if (is_wp_error($post_updated)) {
9400 + $errors = $post_updated->get_error_messages();
9401 + foreach ($errors as $error) {
9402 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
9403 + }
9404 + }
9405 + }
9406 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
9407 + {
9408 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
9409 + if($date_stamp)
9410 + {
9411 + $args = array();
9412 + $args['ID'] = $post->ID;
9413 + $postdate = date("Y-m-d H:i:s", $date_stamp);
9414 + $args['post_date'] = $postdate;
9415 + remove_filter('content_save_pre', 'wp_filter_post_kses');
9416 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9417 + remove_filter('title_save_pre', 'wp_filter_kses');
9418 + $post_updated = wp_update_post($args);
9419 + $changes_made = true;
9420 + add_filter('content_save_pre', 'wp_filter_post_kses');
9421 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9422 + add_filter('title_save_pre', 'wp_filter_kses');
9423 + if (is_wp_error($post_updated)) {
9424 + $errors = $post_updated->get_error_messages();
9425 + foreach ($errors as $error) {
9426 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
9427 + }
9428 + }
9429 + }
9430 + }
9431 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
9432 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated comments.');
9433 + }
9434 + }
9435 + }
9436 + }
9437 + if (isset($aiomatic_Spinner_Settings['add_seo']) && $aiomatic_Spinner_Settings['add_seo'] != '' && $aiomatic_Spinner_Settings['add_seo'] != 'disabled')
9438 + {
9439 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9440 + {
9441 + aiomatic_log_to_file('Starting post SEO writer...');
9442 + }
9443 + if (isset($aiomatic_Spinner_Settings['seo_copy_excerpt']) && $aiomatic_Spinner_Settings['seo_copy_excerpt'] == 'on')
9444 + {
9445 + if(!empty($post->post_excerpt))
9446 + {
9447 + $comm = $post->post_excerpt;
9448 + if (trim($comm) != '') {
9449 + if (isset($aiomatic_Spinner_Settings['seo_max_char']) && $aiomatic_Spinner_Settings['seo_max_char'] != '')
9450 + {
9451 + $comm = aiomatic_substr($comm, 0, intval($aiomatic_Spinner_Settings['seo_max_char']));
9452 + }
9453 + aiomatic_save_seo_description($post->ID, trim($comm), '');
9454 + update_post_meta($post->ID, $custom_name, "pub");
9455 + }
9456 + }
9457 + }
9458 + else
9459 + {
9460 + $vision_file = '';
9461 + if (isset($aiomatic_Spinner_Settings['ai_seo']) && $aiomatic_Spinner_Settings['ai_seo'] != '')
9462 + {
9463 + $ai_seo = $aiomatic_Spinner_Settings['ai_seo'];
9464 + }
9465 + else
9466 + {
9467 + $ai_seo = 'Write a SEO meta description for the post title: %%post_title%%';
9468 + }
9469 + if (isset($aiomatic_Spinner_Settings['ai_vision_seo']) && $aiomatic_Spinner_Settings['ai_vision_seo'] == 'on')
9470 + {
9471 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
9472 + if($avatar !== false)
9473 + {
9474 + $vision_file = $avatar;
9475 + }
9476 + }
9477 + $ai_seo = aiomatic_replaceSynergyShortcodes($ai_seo);
9478 + $post_link = get_permalink($post->ID);
9479 + $blog_title = html_entity_decode(get_bloginfo('title'));
9480 + $author_obj = get_user_by('id', $post->post_author);
9481 + if($author_obj !== false && isset($author_obj->user_nicename))
9482 + {
9483 + $user_name = $author_obj->user_nicename;
9484 + }
9485 + else
9486 + {
9487 + $user_name = '';
9488 + }
9489 + $featured_image = '';
9490 + wp_suspend_cache_addition(true);
9491 + $metas = get_post_custom($post->ID);
9492 + wp_suspend_cache_addition(false);
9493 + if(is_array($metas))
9494 + {
9495 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
9496 + }
9497 + else
9498 + {
9499 + $rez_meta = array();
9500 + }
9501 + if(count($rez_meta) > 0)
9502 + {
9503 + foreach($rez_meta as $rm)
9504 + {
9505 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
9506 + {
9507 + $featured_image = $rm[0];
9508 + break;
9509 + }
9510 + }
9511 + }
9512 + if($featured_image == '')
9513 + {
9514 + $featured_image = aiomatic_generate_thumbmail($post->ID);
9515 + }
9516 + if($featured_image == '' && $final_content != '')
9517 + {
9518 + $dom = new DOMDocument();
9519 + $internalErrors = libxml_use_internal_errors(true);
9520 + $dom->loadHTML($final_content);
9521 + libxml_use_internal_errors($internalErrors);
9522 + $tags = $dom->getElementsByTagName('img');
9523 + foreach ($tags as $tag) {
9524 + $temp_get_img = $tag->getAttribute('src');
9525 + if ($temp_get_img != '') {
9526 + $temp_get_img = strtok($temp_get_img, '?');
9527 + $featured_image = rtrim($temp_get_img, '/');
9528 + }
9529 + }
9530 + }
9531 + $post_cats = '';
9532 + $post_categories = wp_get_post_categories( $post->ID );
9533 + foreach($post_categories as $c){
9534 + $cat = get_category( $c );
9535 + $post_cats .= $cat->name . ',';
9536 + }
9537 + $post_cats = trim($post_cats, ',');
9538 + if($post_cats != '')
9539 + {
9540 + $post_categories = explode(',', $post_cats);
9541 + }
9542 + else
9543 + {
9544 + $post_categories = array();
9545 + }
9546 + if(count($post_categories) == 0)
9547 + {
9548 + $terms = get_the_terms( $post->ID, 'product_cat' );
9549 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
9550 + foreach ( $terms as $term ) {
9551 + $post_categories[] = $term->slug;
9552 + }
9553 + $post_cats = implode(',', $post_categories);
9554 + }
9555 +
9556 + }
9557 + foreach($post_categories as $pc)
9558 + {
9559 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
9560 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
9561 + {
9562 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
9563 + {
9564 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9565 + {
9566 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
9567 + }
9568 + update_post_meta($post->ID, $custom_name, "skip");
9569 + return;
9570 + }
9571 + }
9572 + }
9573 + }
9574 + $post_tagz = '';
9575 + $post_tags = wp_get_post_tags( $post->ID );
9576 + foreach($post_tags as $t){
9577 + $post_tagz .= $t->name . ',';
9578 + }
9579 + $post_tagz = trim($post_tagz, ',');
9580 + if($post_tagz != '')
9581 + {
9582 + $post_tags = explode(',', $post_tagz);
9583 + }
9584 + else
9585 + {
9586 + $post_tags = array();
9587 + }
9588 + if(count($post_tags) == 0)
9589 + {
9590 + $terms = get_the_terms( $post->ID, 'product_tag' );
9591 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
9592 + foreach ( $terms as $term ) {
9593 + $post_tags[] = $term->slug;
9594 + }
9595 + $post_tagz = implode(',', $post_tags);
9596 + }
9597 +
9598 + }
9599 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
9600 +
9601 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
9602 + foreach($disable_users as $disable_user)
9603 + {
9604 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
9605 + {
9606 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9607 + {
9608 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
9609 + }
9610 + update_post_meta($post->ID, $custom_name, "skip");
9611 + return;
9612 + }
9613 + }
9614 + }
9615 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
9616 +
9617 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
9618 + $found = false;
9619 + foreach($enable_users as $disable_user)
9620 + {
9621 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
9622 + {
9623 + $found = true;
9624 + }
9625 + }
9626 + if($found === false)
9627 + {
9628 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9629 + {
9630 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
9631 + }
9632 + update_post_meta($post->ID, $custom_name, "skip");
9633 + return;
9634 + }
9635 + }
9636 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
9637 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
9638 + {
9639 + if ( !has_post_thumbnail($post->ID) )
9640 + {
9641 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9642 + {
9643 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
9644 + }
9645 + update_post_meta($post->ID, $custom_name, "skip");
9646 + return;
9647 + }
9648 + }
9649 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
9650 + {
9651 + if ( has_post_thumbnail($post->ID) )
9652 + {
9653 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9654 + {
9655 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
9656 + }
9657 + update_post_meta($post->ID, $custom_name, "skip");
9658 + return;
9659 + }
9660 + }
9661 + }
9662 +
9663 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
9664 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
9665 + {
9666 + if ( !aiomatic_check_seo_description($post->ID) )
9667 + {
9668 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9669 + {
9670 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
9671 + }
9672 + update_post_meta($post->ID, $custom_name, "skip");
9673 + return;
9674 + }
9675 + }
9676 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
9677 + {
9678 + if ( aiomatic_check_seo_description($post->ID) )
9679 + {
9680 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9681 + {
9682 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
9683 + }
9684 + update_post_meta($post->ID, $custom_name, "skip");
9685 + return;
9686 + }
9687 + }
9688 + }
9689 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
9690 + $skip_me = true;
9691 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
9692 + foreach($enable_field as $fieldx)
9693 + {
9694 + if(strstr($fieldx, '=>') !== false)
9695 + {
9696 + $fna = explode('=>', $fieldx);
9697 + if(isset($fna[1]))
9698 + {
9699 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
9700 + if ($custom_val)
9701 + {
9702 + if(trim($fna[1]) == '*')
9703 + {
9704 + $skip_me = false;
9705 + }
9706 + if($custom_val == trim($fna[1]))
9707 + {
9708 + $skip_me = false;
9709 + }
9710 + }
9711 + }
9712 + }
9713 + }
9714 + if($skip_me === true)
9715 + {
9716 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9717 + {
9718 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
9719 + }
9720 + update_post_meta($post->ID, $custom_name, "skip");
9721 + return;
9722 + }
9723 + }
9724 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
9725 + $skip_me = false;
9726 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
9727 + foreach($disable_field as $fieldx)
9728 + {
9729 + if(strstr($fieldx, '=>') !== false)
9730 + {
9731 + $fna = explode('=>', $fieldx);
9732 + if(isset($fna[1]))
9733 + {
9734 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
9735 + if ($custom_val)
9736 + {
9737 + if(trim($fna[1]) == '*')
9738 + {
9739 + $skip_me = true;
9740 + }
9741 + if($custom_val == trim($fna[1]))
9742 + {
9743 + $skip_me = true;
9744 + }
9745 + }
9746 + }
9747 + }
9748 + }
9749 + if($skip_me === true)
9750 + {
9751 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9752 + {
9753 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
9754 + }
9755 + update_post_meta($post->ID, $custom_name, "skip");
9756 + return;
9757 + }
9758 + }
9759 + foreach($post_tags as $pt)
9760 + {
9761 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
9762 +
9763 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
9764 + foreach($disable_tags as $disabled_tag)
9765 + {
9766 + if($manual != true && trim($pt) == trim($disabled_tag))
9767 + {
9768 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9769 + {
9770 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
9771 + }
9772 + update_post_meta($post->ID, $custom_name, "skip");
9773 + return;
9774 + }
9775 + }
9776 + }
9777 + }
9778 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
9779 + $skip = true;
9780 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
9781 + foreach($post_tags as $pt)
9782 + {
9783 + foreach($enable_tags as $enable_tag)
9784 + {
9785 + if(trim($pt) == trim($enable_tag))
9786 + {
9787 + $skip = false;
9788 + break;
9789 + }
9790 + }
9791 + }
9792 + if($skip == true)
9793 + {
9794 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9795 + {
9796 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
9797 + }
9798 + update_post_meta($post->ID, $custom_name, "skip");
9799 + return;
9800 + }
9801 + }
9802 + $ai_seo = aiomatic_replaceAIPostShortcodes($ai_seo, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
9803 + $ai_seo = trim($ai_seo);
9804 + if (isset($aiomatic_Spinner_Settings['seo_model']) && $aiomatic_Spinner_Settings['seo_model'] != '') {
9805 + $seo_model = $aiomatic_Spinner_Settings['seo_model'];
9806 + }
9807 + else
9808 + {
9809 + $seo_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
9810 + }
9811 + if (isset($aiomatic_Spinner_Settings['meta_assistant_id']) && $aiomatic_Spinner_Settings['meta_assistant_id'] != '') {
9812 + $meta_assistant_id = $aiomatic_Spinner_Settings['meta_assistant_id'];
9813 + }
9814 + else
9815 + {
9816 + $meta_assistant_id = '';
9817 + }
9818 + $xpost_link = get_permalink($post->ID);
9819 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
9820 + $ai_seo_new = $ai_seo;
9821 + $query_token_count = count(aiomatic_encode($ai_seo_new));
9822 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
9823 + {
9824 + $ai_seo_new = aiomatic_substr($ai_seo_new, 0, (0-($max_seed_tokens * 4)));
9825 + $query_token_count = count(aiomatic_encode($ai_seo_new));
9826 + }
9827 + $max_tokens = aimogenpro_get_max_tokens($seo_model);
9828 + $available_tokens = aiomatic_compute_available_tokens($seo_model, $max_tokens, $ai_seo_new, $query_token_count);
9829 + $max_result_tokens = aimogenpro_get_max_tokens($seo_model);
9830 + if($available_tokens > $max_result_tokens)
9831 + {
9832 + $available_tokens = $max_result_tokens;
9833 + }
9834 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
9835 + {
9836 + $string_len = aiomatic_strlen($ai_seo_new);
9837 + $string_len = $string_len / 2;
9838 + $string_len = intval(0 - $string_len);
9839 + $ai_seo_new = aiomatic_substr($ai_seo_new, 0, $string_len);
9840 + $ai_seo_new = trim($ai_seo_new);
9841 + if(empty($ai_seo_new))
9842 + {
9843 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_seo_new, true));
9844 + }
9845 + $query_token_count = count(aiomatic_encode($ai_seo_new));
9846 + $available_tokens = $max_tokens - $query_token_count;
9847 + }
9848 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9849 + {
9850 + $api_service = aiomatic_get_api_service($token, $seo_model);
9851 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $seo_model . ')(' . $available_tokens . ') post SEO meta generator, with seed command: ' . $ai_seo_new);
9852 + }
9853 + $aierror = '';
9854 + $comm = '';
9855 + $finish_reason = '';
9856 + $generated_text = aiomatic_generate_text($token, $seo_model, $ai_seo_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'seoMetaGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $meta_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
9857 + if($generated_text === false)
9858 + {
9859 + aiomatic_log_to_file('SEO meta generator error: ' . $aierror);
9860 + }
9861 + else
9862 + {
9863 + $comm = ucfirst(trim(nl2br(trim($generated_text))));
9864 + $comm = str_replace('//', '', $comm);
9865 + }
9866 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($comm) !== '')
9867 + {
9868 + $api_service = aiomatic_get_api_service($token, $seo_model);
9869 + aiomatic_log_to_file($api_service . ' responded successfully, SEO meta description generated for post ID: ' . $post->ID);
9870 + }
9871 + if (trim($comm) != '') {
9872 + if (isset($aiomatic_Spinner_Settings['seo_max_char']) && $aiomatic_Spinner_Settings['seo_max_char'] != '')
9873 + {
9874 + $comm = aiomatic_substr($comm, 0, intval($aiomatic_Spinner_Settings['seo_max_char']));
9875 + }
9876 + aiomatic_save_seo_description($post->ID, trim($comm), '');
9877 + update_post_meta($post->ID, $custom_name, "pub");
9878 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
9879 + {
9880 + $args = array();
9881 + $args['ID'] = $post->ID;
9882 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
9883 + remove_filter('content_save_pre', 'wp_filter_post_kses');
9884 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9885 + remove_filter('title_save_pre', 'wp_filter_kses');
9886 + $post_updated = wp_update_post($args);
9887 + $changes_made = true;
9888 + add_filter('content_save_pre', 'wp_filter_post_kses');
9889 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9890 + add_filter('title_save_pre', 'wp_filter_kses');
9891 + if (is_wp_error($post_updated)) {
9892 + $errors = $post_updated->get_error_messages();
9893 + foreach ($errors as $error) {
9894 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
9895 + }
9896 + }
9897 + }
9898 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
9899 + {
9900 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
9901 + if($date_stamp)
9902 + {
9903 + $args = array();
9904 + $args['ID'] = $post->ID;
9905 + $postdate = date("Y-m-d H:i:s", $date_stamp);
9906 + $args['post_date'] = $postdate;
9907 + remove_filter('content_save_pre', 'wp_filter_post_kses');
9908 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9909 + remove_filter('title_save_pre', 'wp_filter_kses');
9910 + $post_updated = wp_update_post($args);
9911 + $changes_made = true;
9912 + add_filter('content_save_pre', 'wp_filter_post_kses');
9913 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
9914 + add_filter('title_save_pre', 'wp_filter_kses');
9915 + if (is_wp_error($post_updated)) {
9916 + $errors = $post_updated->get_error_messages();
9917 + foreach ($errors as $error) {
9918 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
9919 + }
9920 + }
9921 + }
9922 + }
9923 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
9924 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated SEO meta description.');
9925 + }
9926 + }
9927 + }
9928 + }
9929 + if (isset($aiomatic_Spinner_Settings['add_seo_title']) && $aiomatic_Spinner_Settings['add_seo_title'] != '' && $aiomatic_Spinner_Settings['add_seo_title'] != 'disabled')
9930 + {
9931 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
9932 + {
9933 + aiomatic_log_to_file('Starting post SEO title writer...');
9934 + }
9935 + {
9936 + $vision_file = '';
9937 + if (isset($aiomatic_Spinner_Settings['ai_seo_title']) && $aiomatic_Spinner_Settings['ai_seo_title'] != '')
9938 + {
9939 + $ai_seo = $aiomatic_Spinner_Settings['ai_seo_title'];
9940 + }
9941 + else
9942 + {
9943 + $ai_seo = 'Write a SEO meta title for the post title: %%post_title%%';
9944 + }
9945 + if (isset($aiomatic_Spinner_Settings['ai_vision_seo_title']) && $aiomatic_Spinner_Settings['ai_vision_seo_title'] == 'on')
9946 + {
9947 + $avatar = get_the_post_thumbnail_url($post->ID, 'post-thumbnail');
9948 + if($avatar !== false)
9949 + {
9950 + $vision_file = $avatar;
9951 + }
9952 + }
9953 + $ai_seo = aiomatic_replaceSynergyShortcodes($ai_seo);
9954 + $post_link = get_permalink($post->ID);
9955 + $blog_title = html_entity_decode(get_bloginfo('title'));
9956 + $author_obj = get_user_by('id', $post->post_author);
9957 + if($author_obj !== false && isset($author_obj->user_nicename))
9958 + {
9959 + $user_name = $author_obj->user_nicename;
9960 + }
9961 + else
9962 + {
9963 + $user_name = '';
9964 + }
9965 + $featured_image = '';
9966 + wp_suspend_cache_addition(true);
9967 + $metas = get_post_custom($post->ID);
9968 + wp_suspend_cache_addition(false);
9969 + if(is_array($metas))
9970 + {
9971 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
9972 + }
9973 + else
9974 + {
9975 + $rez_meta = array();
9976 + }
9977 + if(count($rez_meta) > 0)
9978 + {
9979 + foreach($rez_meta as $rm)
9980 + {
9981 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
9982 + {
9983 + $featured_image = $rm[0];
9984 + break;
9985 + }
9986 + }
9987 + }
9988 + if($featured_image == '')
9989 + {
9990 + $featured_image = aiomatic_generate_thumbmail($post->ID);
9991 + }
9992 + if($featured_image == '' && $final_content != '')
9993 + {
9994 + $dom = new DOMDocument();
9995 + $internalErrors = libxml_use_internal_errors(true);
9996 + $dom->loadHTML($final_content);
9997 + libxml_use_internal_errors($internalErrors);
9998 + $tags = $dom->getElementsByTagName('img');
9999 + foreach ($tags as $tag) {
10000 + $temp_get_img = $tag->getAttribute('src');
10001 + if ($temp_get_img != '') {
10002 + $temp_get_img = strtok($temp_get_img, '?');
10003 + $featured_image = rtrim($temp_get_img, '/');
10004 + }
10005 + }
10006 + }
10007 + $post_cats = '';
10008 + $post_categories = wp_get_post_categories( $post->ID );
10009 + foreach($post_categories as $c){
10010 + $cat = get_category( $c );
10011 + $post_cats .= $cat->name . ',';
10012 + }
10013 + $post_cats = trim($post_cats, ',');
10014 + if($post_cats != '')
10015 + {
10016 + $post_categories = explode(',', $post_cats);
10017 + }
10018 + else
10019 + {
10020 + $post_categories = array();
10021 + }
10022 + if(count($post_categories) == 0)
10023 + {
10024 + $terms = get_the_terms( $post->ID, 'product_cat' );
10025 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
10026 + foreach ( $terms as $term ) {
10027 + $post_categories[] = $term->slug;
10028 + }
10029 + $post_cats = implode(',', $post_categories);
10030 + }
10031 +
10032 + }
10033 + foreach($post_categories as $pc)
10034 + {
10035 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
10036 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
10037 + {
10038 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
10039 + {
10040 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10041 + {
10042 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
10043 + }
10044 + update_post_meta($post->ID, $custom_name, "skip");
10045 + return;
10046 + }
10047 + }
10048 + }
10049 + }
10050 + $post_tagz = '';
10051 + $post_tags = wp_get_post_tags( $post->ID );
10052 + foreach($post_tags as $t){
10053 + $post_tagz .= $t->name . ',';
10054 + }
10055 + $post_tagz = trim($post_tagz, ',');
10056 + if($post_tagz != '')
10057 + {
10058 + $post_tags = explode(',', $post_tagz);
10059 + }
10060 + else
10061 + {
10062 + $post_tags = array();
10063 + }
10064 + if(count($post_tags) == 0)
10065 + {
10066 + $terms = get_the_terms( $post->ID, 'product_tag' );
10067 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
10068 + foreach ( $terms as $term ) {
10069 + $post_tags[] = $term->slug;
10070 + }
10071 + $post_tagz = implode(',', $post_tags);
10072 + }
10073 +
10074 + }
10075 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
10076 +
10077 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
10078 + foreach($disable_users as $disable_user)
10079 + {
10080 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
10081 + {
10082 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10083 + {
10084 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
10085 + }
10086 + update_post_meta($post->ID, $custom_name, "skip");
10087 + return;
10088 + }
10089 + }
10090 + }
10091 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
10092 +
10093 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
10094 + $found = false;
10095 + foreach($enable_users as $disable_user)
10096 + {
10097 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
10098 + {
10099 + $found = true;
10100 + }
10101 + }
10102 + if($found === false)
10103 + {
10104 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10105 + {
10106 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
10107 + }
10108 + update_post_meta($post->ID, $custom_name, "skip");
10109 + return;
10110 + }
10111 + }
10112 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
10113 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
10114 + {
10115 + if ( !has_post_thumbnail($post->ID) )
10116 + {
10117 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10118 + {
10119 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
10120 + }
10121 + update_post_meta($post->ID, $custom_name, "skip");
10122 + return;
10123 + }
10124 + }
10125 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
10126 + {
10127 + if ( has_post_thumbnail($post->ID) )
10128 + {
10129 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10130 + {
10131 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
10132 + }
10133 + update_post_meta($post->ID, $custom_name, "skip");
10134 + return;
10135 + }
10136 + }
10137 + }
10138 +
10139 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
10140 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
10141 + {
10142 + if ( !aiomatic_check_seo_description($post->ID) )
10143 + {
10144 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10145 + {
10146 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
10147 + }
10148 + update_post_meta($post->ID, $custom_name, "skip");
10149 + return;
10150 + }
10151 + }
10152 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
10153 + {
10154 + if ( aiomatic_check_seo_description($post->ID) )
10155 + {
10156 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10157 + {
10158 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
10159 + }
10160 + update_post_meta($post->ID, $custom_name, "skip");
10161 + return;
10162 + }
10163 + }
10164 + }
10165 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
10166 + $skip_me = true;
10167 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
10168 + foreach($enable_field as $fieldx)
10169 + {
10170 + if(strstr($fieldx, '=>') !== false)
10171 + {
10172 + $fna = explode('=>', $fieldx);
10173 + if(isset($fna[1]))
10174 + {
10175 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
10176 + if ($custom_val)
10177 + {
10178 + if(trim($fna[1]) == '*')
10179 + {
10180 + $skip_me = false;
10181 + }
10182 + if($custom_val == trim($fna[1]))
10183 + {
10184 + $skip_me = false;
10185 + }
10186 + }
10187 + }
10188 + }
10189 + }
10190 + if($skip_me === true)
10191 + {
10192 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10193 + {
10194 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
10195 + }
10196 + update_post_meta($post->ID, $custom_name, "skip");
10197 + return;
10198 + }
10199 + }
10200 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
10201 + $skip_me = false;
10202 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
10203 + foreach($disable_field as $fieldx)
10204 + {
10205 + if(strstr($fieldx, '=>') !== false)
10206 + {
10207 + $fna = explode('=>', $fieldx);
10208 + if(isset($fna[1]))
10209 + {
10210 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
10211 + if ($custom_val)
10212 + {
10213 + if(trim($fna[1]) == '*')
10214 + {
10215 + $skip_me = true;
10216 + }
10217 + if($custom_val == trim($fna[1]))
10218 + {
10219 + $skip_me = true;
10220 + }
10221 + }
10222 + }
10223 + }
10224 + }
10225 + if($skip_me === true)
10226 + {
10227 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10228 + {
10229 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
10230 + }
10231 + update_post_meta($post->ID, $custom_name, "skip");
10232 + return;
10233 + }
10234 + }
10235 + foreach($post_tags as $pt)
10236 + {
10237 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
10238 +
10239 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
10240 + foreach($disable_tags as $disabled_tag)
10241 + {
10242 + if($manual != true && trim($pt) == trim($disabled_tag))
10243 + {
10244 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10245 + {
10246 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
10247 + }
10248 + update_post_meta($post->ID, $custom_name, "skip");
10249 + return;
10250 + }
10251 + }
10252 + }
10253 + }
10254 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
10255 + $skip = true;
10256 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
10257 + foreach($post_tags as $pt)
10258 + {
10259 + foreach($enable_tags as $enable_tag)
10260 + {
10261 + if(trim($pt) == trim($enable_tag))
10262 + {
10263 + $skip = false;
10264 + break;
10265 + }
10266 + }
10267 + }
10268 + if($skip == true)
10269 + {
10270 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10271 + {
10272 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
10273 + }
10274 + update_post_meta($post->ID, $custom_name, "skip");
10275 + return;
10276 + }
10277 + }
10278 + $ai_seo = aiomatic_replaceAIPostShortcodes($ai_seo, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
10279 + $ai_seo = trim($ai_seo);
10280 + if (isset($aiomatic_Spinner_Settings['seo_model_title']) && $aiomatic_Spinner_Settings['seo_model_title'] != '') {
10281 + $seo_model = $aiomatic_Spinner_Settings['seo_model_title'];
10282 + }
10283 + else
10284 + {
10285 + $seo_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
10286 + }
10287 + if (isset($aiomatic_Spinner_Settings['meta_assistant_id_title']) && $aiomatic_Spinner_Settings['meta_assistant_id_title'] != '') {
10288 + $meta_assistant_id = $aiomatic_Spinner_Settings['meta_assistant_id_title'];
10289 + }
10290 + else
10291 + {
10292 + $meta_assistant_id = '';
10293 + }
10294 + $xpost_link = get_permalink($post->ID);
10295 + $date = date('Y-m-d H:i:s', strtotime(current_time('mysql')));
10296 + $ai_seo_new = $ai_seo;
10297 + $query_token_count = count(aiomatic_encode($ai_seo_new));
10298 + if(!empty($max_seed_tokens) && $query_token_count > $max_seed_tokens)
10299 + {
10300 + $ai_seo_new = aiomatic_substr($ai_seo_new, 0, (0-($max_seed_tokens * 4)));
10301 + $query_token_count = count(aiomatic_encode($ai_seo_new));
10302 + }
10303 + $max_tokens = aimogenpro_get_max_tokens($seo_model);
10304 + $available_tokens = aiomatic_compute_available_tokens($seo_model, $max_tokens, $ai_seo_new, $query_token_count);
10305 + $max_result_tokens = aimogenpro_get_max_tokens($seo_model);
10306 + if($available_tokens > $max_result_tokens)
10307 + {
10308 + $available_tokens = $max_result_tokens;
10309 + }
10310 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
10311 + {
10312 + $string_len = aiomatic_strlen($ai_seo_new);
10313 + $string_len = $string_len / 2;
10314 + $string_len = intval(0 - $string_len);
10315 + $ai_seo_new = aiomatic_substr($ai_seo_new, 0, $string_len);
10316 + $ai_seo_new = trim($ai_seo_new);
10317 + if(empty($ai_seo_new))
10318 + {
10319 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_seo_new, true));
10320 + }
10321 + $query_token_count = count(aiomatic_encode($ai_seo_new));
10322 + $available_tokens = $max_tokens - $query_token_count;
10323 + }
10324 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10325 + {
10326 + $api_service = aiomatic_get_api_service($token, $seo_model);
10327 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $seo_model . ')(' . $available_tokens . ') post SEO meta generator, with seed command: ' . $ai_seo_new);
10328 + }
10329 + $aierror = '';
10330 + $comm = '';
10331 + $finish_reason = '';
10332 + $generated_text = aiomatic_generate_text($token, $seo_model, $ai_seo_new, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'seoMetaGenerator', 0, $finish_reason, $aierror, true, false, false, $vision_file, '', 'user', $meta_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
10333 + if($generated_text === false)
10334 + {
10335 + aiomatic_log_to_file('SEO meta generator error: ' . $aierror);
10336 + }
10337 + else
10338 + {
10339 + $comm = ucfirst(trim(nl2br(trim($generated_text))));
10340 + $comm = str_replace('//', '', $comm);
10341 + }
10342 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && trim($comm) !== '')
10343 + {
10344 + $api_service = aiomatic_get_api_service($token, $seo_model);
10345 + aiomatic_log_to_file($api_service . ' responded successfully, SEO meta title generated for post ID: ' . $post->ID);
10346 + }
10347 + if (trim($comm) != '') {
10348 + if (isset($aiomatic_Spinner_Settings['seo_max_char_title']) && $aiomatic_Spinner_Settings['seo_max_char_title'] != '')
10349 + {
10350 + $comm = aiomatic_substr($comm, 0, intval($aiomatic_Spinner_Settings['seo_max_char_title']));
10351 + }
10352 + aiomatic_save_seo_title($post->ID, trim($comm));
10353 + update_post_meta($post->ID, $custom_name, "pub");
10354 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
10355 + {
10356 + $args = array();
10357 + $args['ID'] = $post->ID;
10358 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
10359 + remove_filter('content_save_pre', 'wp_filter_post_kses');
10360 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
10361 + remove_filter('title_save_pre', 'wp_filter_kses');
10362 + $post_updated = wp_update_post($args);
10363 + $changes_made = true;
10364 + add_filter('content_save_pre', 'wp_filter_post_kses');
10365 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
10366 + add_filter('title_save_pre', 'wp_filter_kses');
10367 + if (is_wp_error($post_updated)) {
10368 + $errors = $post_updated->get_error_messages();
10369 + foreach ($errors as $error) {
10370 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
10371 + }
10372 + }
10373 + }
10374 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
10375 + {
10376 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
10377 + if($date_stamp)
10378 + {
10379 + $args = array();
10380 + $args['ID'] = $post->ID;
10381 + $postdate = date("Y-m-d H:i:s", $date_stamp);
10382 + $args['post_date'] = $postdate;
10383 + remove_filter('content_save_pre', 'wp_filter_post_kses');
10384 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
10385 + remove_filter('title_save_pre', 'wp_filter_kses');
10386 + $post_updated = wp_update_post($args);
10387 + $changes_made = true;
10388 + add_filter('content_save_pre', 'wp_filter_post_kses');
10389 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
10390 + add_filter('title_save_pre', 'wp_filter_kses');
10391 + if (is_wp_error($post_updated)) {
10392 + $errors = $post_updated->get_error_messages();
10393 + foreach ($errors as $error) {
10394 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
10395 + }
10396 + }
10397 + }
10398 + }
10399 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10400 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated SEO meta title.');
10401 + }
10402 + }
10403 + }
10404 + }
10405 + if (isset($aiomatic_Spinner_Settings['ai_featured_image']) && $aiomatic_Spinner_Settings['ai_featured_image'] != '' && $aiomatic_Spinner_Settings['ai_featured_image'] != 'disabled')
10406 + {
10407 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10408 + {
10409 + aiomatic_log_to_file('Starting post featured image generator...');
10410 + }
10411 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
10412 + {
10413 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
10414 + }
10415 + else
10416 + {
10417 + $image_size = '1024x1024';
10418 + }
10419 + if (isset($aiomatic_Spinner_Settings['image_model']) && trim($aiomatic_Spinner_Settings['image_model']) != '')
10420 + {
10421 + $image_model = trim($aiomatic_Spinner_Settings['image_model']);
10422 + }
10423 + else
10424 + {
10425 + $image_model = 'dalle2';
10426 + }
10427 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
10428 + {
10429 + $aicontent = trim(strip_tags($aiomatic_Spinner_Settings['ai_image_command']));
10430 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
10431 + $post_link = get_permalink($post->ID);
10432 + $blog_title = html_entity_decode(get_bloginfo('title'));
10433 + $author_obj = get_user_by('id', $post->post_author);
10434 + if($author_obj !== false && isset($author_obj->user_nicename))
10435 + {
10436 + $user_name = $author_obj->user_nicename;
10437 + }
10438 + else
10439 + {
10440 + $user_name = '';
10441 + }
10442 + $featured_image = '';
10443 + wp_suspend_cache_addition(true);
10444 + $metas = get_post_custom($post->ID);
10445 + wp_suspend_cache_addition(false);
10446 + if(is_array($metas))
10447 + {
10448 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
10449 + }
10450 + else
10451 + {
10452 + $rez_meta = array();
10453 + }
10454 + if(count($rez_meta) > 0)
10455 + {
10456 + foreach($rez_meta as $rm)
10457 + {
10458 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
10459 + {
10460 + $featured_image = $rm[0];
10461 + break;
10462 + }
10463 + }
10464 + }
10465 + if($featured_image == '')
10466 + {
10467 + $featured_image = aiomatic_generate_thumbmail($post->ID);
10468 + }
10469 + if($featured_image == '' && $final_content != '')
10470 + {
10471 + $dom = new DOMDocument();
10472 + $internalErrors = libxml_use_internal_errors(true);
10473 + $dom->loadHTML($final_content);
10474 + libxml_use_internal_errors($internalErrors);
10475 + $tags = $dom->getElementsByTagName('img');
10476 + foreach ($tags as $tag) {
10477 + $temp_get_img = $tag->getAttribute('src');
10478 + if ($temp_get_img != '') {
10479 + $temp_get_img = strtok($temp_get_img, '?');
10480 + $featured_image = rtrim($temp_get_img, '/');
10481 + }
10482 + }
10483 + }
10484 + $post_cats = '';
10485 + $post_categories = wp_get_post_categories( $post->ID );
10486 + foreach($post_categories as $c){
10487 + $cat = get_category( $c );
10488 + $post_cats .= $cat->name . ',';
10489 + }
10490 + $post_cats = trim($post_cats, ',');
10491 + if($post_cats != '')
10492 + {
10493 + $post_categories = explode(',', $post_cats);
10494 + }
10495 + else
10496 + {
10497 + $post_categories = array();
10498 + }
10499 + if(count($post_categories) == 0)
10500 + {
10501 + $terms = get_the_terms( $post->ID, 'product_cat' );
10502 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
10503 + foreach ( $terms as $term ) {
10504 + $post_categories[] = $term->slug;
10505 + }
10506 + $post_cats = implode(',', $post_categories);
10507 + }
10508 +
10509 + }
10510 + foreach($post_categories as $pc)
10511 + {
10512 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
10513 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
10514 + {
10515 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
10516 + {
10517 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10518 + {
10519 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
10520 + }
10521 + update_post_meta($post->ID, $custom_name, "skip");
10522 + return;
10523 + }
10524 + }
10525 + }
10526 + }
10527 + $post_tagz = '';
10528 + $post_tags = wp_get_post_tags( $post->ID );
10529 + foreach($post_tags as $t){
10530 + $post_tagz .= $t->name . ',';
10531 + }
10532 + $post_tagz = trim($post_tagz, ',');
10533 + if($post_tagz != '')
10534 + {
10535 + $post_tags = explode(',', $post_tagz);
10536 + }
10537 + else
10538 + {
10539 + $post_tags = array();
10540 + }
10541 + if(count($post_tags) == 0)
10542 + {
10543 + $terms = get_the_terms( $post->ID, 'product_tag' );
10544 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
10545 + foreach ( $terms as $term ) {
10546 + $post_tags[] = $term->slug;
10547 + }
10548 + $post_tagz = implode(',', $post_tags);
10549 + }
10550 +
10551 + }
10552 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
10553 +
10554 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
10555 + foreach($disable_users as $disable_user)
10556 + {
10557 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
10558 + {
10559 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10560 + {
10561 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
10562 + }
10563 + update_post_meta($post->ID, $custom_name, "skip");
10564 + return;
10565 + }
10566 + }
10567 + }
10568 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
10569 +
10570 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
10571 + $found = false;
10572 + foreach($enable_users as $disable_user)
10573 + {
10574 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
10575 + {
10576 + $found = true;
10577 + }
10578 + }
10579 + if($found === false)
10580 + {
10581 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10582 + {
10583 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
10584 + }
10585 + update_post_meta($post->ID, $custom_name, "skip");
10586 + return;
10587 + }
10588 + }
10589 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
10590 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
10591 + {
10592 + if ( !has_post_thumbnail($post->ID) )
10593 + {
10594 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10595 + {
10596 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
10597 + }
10598 + update_post_meta($post->ID, $custom_name, "skip");
10599 + return;
10600 + }
10601 + }
10602 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
10603 + {
10604 + if ( has_post_thumbnail($post->ID) )
10605 + {
10606 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10607 + {
10608 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
10609 + }
10610 + update_post_meta($post->ID, $custom_name, "skip");
10611 + return;
10612 + }
10613 + }
10614 + }
10615 +
10616 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
10617 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
10618 + {
10619 + if ( !aiomatic_check_seo_description($post->ID) )
10620 + {
10621 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10622 + {
10623 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
10624 + }
10625 + update_post_meta($post->ID, $custom_name, "skip");
10626 + return;
10627 + }
10628 + }
10629 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
10630 + {
10631 + if ( aiomatic_check_seo_description($post->ID) )
10632 + {
10633 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10634 + {
10635 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
10636 + }
10637 + update_post_meta($post->ID, $custom_name, "skip");
10638 + return;
10639 + }
10640 + }
10641 + }
10642 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
10643 + $skip_me = true;
10644 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
10645 + foreach($enable_field as $fieldx)
10646 + {
10647 + if(strstr($fieldx, '=>') !== false)
10648 + {
10649 + $fna = explode('=>', $fieldx);
10650 + if(isset($fna[1]))
10651 + {
10652 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
10653 + if ($custom_val)
10654 + {
10655 + if(trim($fna[1]) == '*')
10656 + {
10657 + $skip_me = false;
10658 + }
10659 + if($custom_val == trim($fna[1]))
10660 + {
10661 + $skip_me = false;
10662 + }
10663 + }
10664 + }
10665 + }
10666 + }
10667 + if($skip_me === true)
10668 + {
10669 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10670 + {
10671 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
10672 + }
10673 + update_post_meta($post->ID, $custom_name, "skip");
10674 + return;
10675 + }
10676 + }
10677 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
10678 + $skip_me = false;
10679 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
10680 + foreach($disable_field as $fieldx)
10681 + {
10682 + if(strstr($fieldx, '=>') !== false)
10683 + {
10684 + $fna = explode('=>', $fieldx);
10685 + if(isset($fna[1]))
10686 + {
10687 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
10688 + if ($custom_val)
10689 + {
10690 + if(trim($fna[1]) == '*')
10691 + {
10692 + $skip_me = true;
10693 + }
10694 + if($custom_val == trim($fna[1]))
10695 + {
10696 + $skip_me = true;
10697 + }
10698 + }
10699 + }
10700 + }
10701 + }
10702 + if($skip_me === true)
10703 + {
10704 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10705 + {
10706 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
10707 + }
10708 + update_post_meta($post->ID, $custom_name, "skip");
10709 + return;
10710 + }
10711 + }
10712 + foreach($post_tags as $pt)
10713 + {
10714 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
10715 +
10716 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
10717 + foreach($disable_tags as $disabled_tag)
10718 + {
10719 + if($manual != true && trim($pt) == trim($disabled_tag))
10720 + {
10721 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10722 + {
10723 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
10724 + }
10725 + update_post_meta($post->ID, $custom_name, "skip");
10726 + return;
10727 + }
10728 + }
10729 + }
10730 + }
10731 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
10732 + $skip = true;
10733 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
10734 + foreach($post_tags as $pt)
10735 + {
10736 + foreach($enable_tags as $enable_tag)
10737 + {
10738 + if(trim($pt) == trim($enable_tag))
10739 + {
10740 + $skip = false;
10741 + break;
10742 + }
10743 + }
10744 + }
10745 + if($skip == true)
10746 + {
10747 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
10748 + {
10749 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
10750 + }
10751 + update_post_meta($post->ID, $custom_name, "skip");
10752 + return;
10753 + }
10754 + }
10755 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
10756 + }
10757 + else
10758 + {
10759 + $aicontent = trim(strip_tags($final_content));
10760 + if(empty($aicontent))
10761 + {
10762 + $aicontent = trim(strip_tags($post->post_excerpt));
10763 + }
10764 + if(empty($aicontent))
10765 + {
10766 + $aicontent = trim(strip_tags($post_title));
10767 + $last_char = aiomatic_substr($aicontent, -1, null);
10768 + if(!ctype_punct($last_char))
10769 + {
10770 + $aicontent .= '.';
10771 + }
10772 + }
10773 + }
10774 + if(isset($aiomatic_Spinner_Settings['ai_featured_image_source']) && $aiomatic_Spinner_Settings['ai_featured_image_source'] != '')
10775 + {
10776 + $fisource = $aiomatic_Spinner_Settings['ai_featured_image_source'];
10777 + }
10778 + else
10779 + {
10780 + $fisource = '1';
10781 + }
10782 + $img_saved = false;
10783 + if($fisource == '1')
10784 + {
10785 + $aicontent = trim($aicontent);
10786 + if(aiomatic_strlen($aicontent) > 400)
10787 + {
10788 + $aicontent = aiomatic_substr($aicontent, 0, 400);
10789 + }
10790 + $skip_this_copy = true;
10791 + if(!function_exists('is_plugin_active'))
10792 + {
10793 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
10794 + }
10795 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
10796 + {
10797 + $skip_this_copy = false;
10798 + }
10799 + $aierror = '';
10800 + $temp_get_imgs = aiomatic_generate_ai_image($token, 1, $aicontent, $image_size, 'editFeaturedImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
10801 + if($temp_get_imgs !== false)
10802 + {
10803 + foreach($temp_get_imgs as $tmpimg)
10804 + {
10805 + if (!aiomatic_generate_featured_image($tmpimg, $post->ID)) {
10806 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10807 + aiomatic_log_to_file('aiomatic_generate_featured_image failed using OpenAI/AiomaticAPI for ' . $tmpimg);
10808 + }
10809 + }
10810 + else
10811 + {
10812 + $img_saved = true;
10813 + }
10814 + break;
10815 + }
10816 + }
10817 + else
10818 + {
10819 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10820 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using OpenAI/AiomaticAPI: ' . $aierror);
10821 + }
10822 + }
10823 + }
10824 + elseif($fisource == '2')
10825 + {
10826 + $aicontent = trim($aicontent);
10827 + if(aiomatic_strlen($aicontent) > 2000)
10828 + {
10829 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
10830 + }
10831 + if($image_size == '256x256')
10832 + {
10833 + $width = '512';
10834 + $height = '512';
10835 + }
10836 + elseif($image_size == '512x512')
10837 + {
10838 + $width = '512';
10839 + $height = '512';
10840 + }
10841 + elseif($image_size == '1024x1024')
10842 + {
10843 + $width = '1024';
10844 + $height = '1024';
10845 + }
10846 + else
10847 + {
10848 + $width = '512';
10849 + $height = '512';
10850 + }
10851 + $skip_this_copy = true;
10852 + if(!function_exists('is_plugin_active'))
10853 + {
10854 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
10855 + }
10856 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
10857 + {
10858 + $skip_this_copy = false;
10859 + }
10860 + $ierror = '';
10861 + $temp_get_imgs = aiomatic_generate_stability_image($aicontent, $height, $width, 'editorFeaturedStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
10862 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
10863 + {
10864 + $temp_get_img_local = $temp_get_imgs[0];
10865 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
10866 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10867 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Stability.AI for ' .$temp_get_imgs[1]);
10868 + }
10869 + }
10870 + else
10871 + {
10872 + $img_saved = true;
10873 + }
10874 + }
10875 + else
10876 + {
10877 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10878 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using Stability.AI: ' . $ierror);
10879 + }
10880 + }
10881 + }
10882 + elseif($fisource == '7')
10883 + {
10884 + $aicontent = trim($aicontent);
10885 + if(aiomatic_strlen($aicontent) > 2000)
10886 + {
10887 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
10888 + }
10889 + $skip_this_copy = true;
10890 + if(!function_exists('is_plugin_active'))
10891 + {
10892 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
10893 + }
10894 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
10895 + {
10896 + $skip_this_copy = false;
10897 + }
10898 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
10899 + {
10900 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
10901 + }
10902 + else
10903 + {
10904 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
10905 + }
10906 + $ierror = '';
10907 + $temp_get_imgs = aiomatic_generate_image_google($aicontent, $model, 'editorFeaturedGoogleImage', false, $ierror, false);
10908 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
10909 + {
10910 + $temp_get_img_local = $temp_get_imgs[0];
10911 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
10912 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10913 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Stability.AI for ' .$temp_get_imgs[1]);
10914 + }
10915 + }
10916 + else
10917 + {
10918 + $img_saved = true;
10919 + }
10920 + }
10921 + else
10922 + {
10923 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10924 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using Stability.AI: ' . $ierror);
10925 + }
10926 + }
10927 + }
10928 + elseif($fisource == '4')
10929 + {
10930 + $aicontent = trim($aicontent);
10931 + if(aiomatic_strlen($aicontent) > 2000)
10932 + {
10933 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
10934 + }
10935 + if($image_size == '256x256')
10936 + {
10937 + $width = '512';
10938 + $height = '512';
10939 + }
10940 + elseif($image_size == '512x512')
10941 + {
10942 + $width = '512';
10943 + $height = '512';
10944 + }
10945 + elseif($image_size == '1024x1024')
10946 + {
10947 + $width = '1024';
10948 + $height = '1024';
10949 + }
10950 + elseif($image_size == '1792x1024')
10951 + {
10952 + $width = '1792';
10953 + $height = '1024';
10954 + }
10955 + elseif($image_size == '1024x1792')
10956 + {
10957 + $width = '1024';
10958 + $height = '1792';
10959 + }
10960 + else
10961 + {
10962 + $width = '512';
10963 + $height = '512';
10964 + }
10965 + $skip_this_copy = true;
10966 + if(!function_exists('is_plugin_active'))
10967 + {
10968 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
10969 + }
10970 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
10971 + {
10972 + $skip_this_copy = false;
10973 + }
10974 + $ierror = '';
10975 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($aicontent, $width, $height, 'editorFeaturedMidjourneyImage', $skip_this_copy, $ierror);
10976 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
10977 + {
10978 + $temp_get_img_local = $temp_get_imgs;
10979 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
10980 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10981 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Midjourney for ' .$temp_get_imgs);
10982 + }
10983 + }
10984 + else
10985 + {
10986 + $img_saved = true;
10987 + }
10988 + }
10989 + else
10990 + {
10991 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10992 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using Midjourney: ' . $ierror);
10993 + }
10994 + }
10995 + }
10996 + elseif($fisource == '5')
10997 + {
10998 + $aicontent = trim($aicontent);
10999 + if(aiomatic_strlen($aicontent) > 2000)
11000 + {
11001 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
11002 + }
11003 + if($image_size == '256x256')
11004 + {
11005 + $width = '512';
11006 + $height = '512';
11007 + }
11008 + elseif($image_size == '512x512')
11009 + {
11010 + $width = '512';
11011 + $height = '512';
11012 + }
11013 + elseif($image_size == '1024x1024')
11014 + {
11015 + $width = '1024';
11016 + $height = '1024';
11017 + }
11018 + elseif($image_size == '1792x1024')
11019 + {
11020 + $width = '1792';
11021 + $height = '1024';
11022 + }
11023 + elseif($image_size == '1024x1792')
11024 + {
11025 + $width = '1024';
11026 + $height = '1792';
11027 + }
11028 + else
11029 + {
11030 + $width = '512';
11031 + $height = '512';
11032 + }
11033 + $skip_this_copy = true;
11034 + if(!function_exists('is_plugin_active'))
11035 + {
11036 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
11037 + }
11038 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
11039 + {
11040 + $skip_this_copy = false;
11041 + }
11042 + $ierror = '';
11043 + $temp_get_imgs = aiomatic_generate_replicate_image($aicontent, $width, $height, 'editorFeaturedReplicateImage', $skip_this_copy, $ierror);
11044 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
11045 + {
11046 + $temp_get_img_local = $temp_get_imgs;
11047 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
11048 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11049 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Replicate for ' .$temp_get_imgs);
11050 + }
11051 + }
11052 + else
11053 + {
11054 + $img_saved = true;
11055 + }
11056 + }
11057 + else
11058 + {
11059 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11060 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using Replicate: ' . $ierror);
11061 + }
11062 + }
11063 + }
11064 + elseif($fisource == '6')
11065 + {
11066 + $aicontent = trim($aicontent);
11067 + if(aiomatic_strlen($aicontent) > 2000)
11068 + {
11069 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
11070 + }
11071 + if($image_size == '256x256')
11072 + {
11073 + $width = '512';
11074 + $height = '512';
11075 + }
11076 + elseif($image_size == '512x512')
11077 + {
11078 + $width = '512';
11079 + $height = '512';
11080 + }
11081 + elseif($image_size == '1024x1024')
11082 + {
11083 + $width = '1024';
11084 + $height = '1024';
11085 + }
11086 + elseif($image_size == '1792x1024')
11087 + {
11088 + $width = '1792';
11089 + $height = '1024';
11090 + }
11091 + elseif($image_size == '1024x1792')
11092 + {
11093 + $width = '1024';
11094 + $height = '1792';
11095 + }
11096 + else
11097 + {
11098 + $width = '512';
11099 + $height = '512';
11100 + }
11101 + $skip_this_copy = true;
11102 + if(!function_exists('is_plugin_active'))
11103 + {
11104 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
11105 + }
11106 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
11107 + {
11108 + $skip_this_copy = false;
11109 + }
11110 + $ierror = '';
11111 + $temp_get_imgs = aiomatic_generate_ideogram_image($aicontent, $width, $height, 'editorFeaturedIdeogramImage', $skip_this_copy, $ierror);
11112 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
11113 + {
11114 + $temp_get_img_local = $temp_get_imgs;
11115 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
11116 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11117 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Ideogram for ' .$temp_get_imgs);
11118 + }
11119 + }
11120 + else
11121 + {
11122 + $img_saved = true;
11123 + }
11124 + }
11125 + else
11126 + {
11127 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11128 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to generated a featured image using Ideogram: ' . $ierror);
11129 + }
11130 + }
11131 + }
11132 + elseif($fisource == '3')
11133 + {
11134 + if(isset($aiomatic_Spinner_Settings['url_image_list']) && $aiomatic_Spinner_Settings['url_image_list'] != '')
11135 + {
11136 + $url_image_list = $aiomatic_Spinner_Settings['url_image_list'];
11137 + }
11138 + else
11139 + {
11140 + $url_image_list = '';
11141 + }
11142 + if ($url_image_list != '')
11143 + {
11144 + $zget_img = '';
11145 + $zreplacement = str_replace(array('[', ']'), '', $post->post_title);
11146 + $image_url_temp = str_replace('%%item_title%%', $zreplacement, $url_image_list);
11147 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
11148 + if(isset($matches[2]))
11149 + {
11150 + $chance = trim($matches[2], '[]');
11151 + }
11152 + else
11153 + {
11154 + $chance = '';
11155 + }
11156 + $arv = array();
11157 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
11158 + return $my_img;
11159 + }, $image_url_temp);
11160 + $spintax = new Aiomatic_Spintax();
11161 + $img_rulx = $spintax->Parse(trim($image_url_temp));
11162 + $selected_img = aiomatic_select_ai_image($post->post_title, $img_rulx);
11163 + if($selected_img === false)
11164 + {
11165 + $img_rulx = explode(',', $img_rulx);
11166 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
11167 + if($img_rulx != '')
11168 + {
11169 + $zget_img = $img_rulx;
11170 + }
11171 + }
11172 + else
11173 + {
11174 + $zget_img = $selected_img;
11175 + }
11176 + if($zget_img != '')
11177 + {
11178 + if (!aiomatic_generate_featured_image($zget_img, $post->ID)) {
11179 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11180 + aiomatic_log_to_file('aiomatic_generate_featured_image failed using manual image: ' . $zget_img);
11181 + }
11182 + }
11183 + else
11184 + {
11185 + $img_saved = true;
11186 + }
11187 + }
11188 + }
11189 + }
11190 + elseif($fisource == '0')
11191 + {
11192 + $img_set = false;
11193 + $img_attr = '';
11194 + $postID = $post->ID;
11195 + $post_excerpt = $post->post_excerpt;
11196 + $query_words = '';
11197 + $image_query = $post_title;
11198 + if (isset($aiomatic_Spinner_Settings['ai_image_command']) && $aiomatic_Spinner_Settings['ai_image_command'] != '')
11199 + {
11200 + $image_query = $aiomatic_Spinner_Settings['ai_image_command'];
11201 + }
11202 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
11203 + {
11204 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
11205 + {
11206 + try
11207 + {
11208 + if(!class_exists('TextRazor'))
11209 + {
11210 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
11211 + }
11212 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
11213 + $textrazor = new TextRazor();
11214 + $textrazor->addExtractor('entities');
11215 + $response = $textrazor->analyze($aicontent);
11216 + if (isset($response['response']['entities']))
11217 + {
11218 + foreach ($response['response']['entities'] as $entity)
11219 + {
11220 + $query_words = '';
11221 + if(isset($entity['entityEnglishId']))
11222 + {
11223 + $query_words = $entity['entityEnglishId'];
11224 + }
11225 + else
11226 + {
11227 + $query_words = $entity['entityId'];
11228 + }
11229 + if($query_words != '')
11230 + {
11231 + $attached_id = '';
11232 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
11233 + if(!empty($z_img))
11234 + {
11235 + if (!aiomatic_generate_featured_image($z_img, $post->ID)) {
11236 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11237 + aiomatic_log_to_file('aiomatic_generate_featured_image failed using royalty free image: ' . $z_img);
11238 + }
11239 + }
11240 + else
11241 + {
11242 + $img_saved = true;
11243 + $img_set = true;
11244 + }
11245 + break;
11246 + }
11247 + }
11248 + }
11249 + }
11250 + }
11251 + catch(Exception $e)
11252 + {
11253 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11254 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
11255 + }
11256 + }
11257 + }
11258 + }
11259 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
11260 + {
11261 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
11262 + {
11263 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
11264 + {
11265 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
11266 + }
11267 + else
11268 + {
11269 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
11270 + }
11271 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
11272 + {
11273 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
11274 + }
11275 + else
11276 + {
11277 + $keyword_assistant_id = '';
11278 + }
11279 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
11280 + $title_ai_command = str_replace('%%default_post_cats%%', '', $title_ai_command);
11281 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
11282 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
11283 + $title_ai_command = array_filter($title_ai_command);
11284 + if(count($title_ai_command) > 0)
11285 + {
11286 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
11287 + }
11288 + else
11289 + {
11290 + $title_ai_command = '';
11291 + }
11292 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
11293 + if(!empty($title_ai_command))
11294 + {
11295 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
11296 + }
11297 + $title_ai_command = trim($title_ai_command);
11298 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
11299 + {
11300 + $txt_content = aiomatic_get_web_page($title_ai_command);
11301 + if ($txt_content !== FALSE)
11302 + {
11303 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11304 + $txt_content = array_filter($txt_content);
11305 + if(count($txt_content) > 0)
11306 + {
11307 + $txt_content = $txt_content[array_rand($txt_content)];
11308 + if(trim($txt_content) != '')
11309 + {
11310 + $title_ai_command = $txt_content;
11311 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
11312 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
11313 + }
11314 + }
11315 + }
11316 + }
11317 + if(empty($title_ai_command))
11318 + {
11319 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
11320 + $title_ai_command = 'Extract a comma separated list of relevant keywords from the text: ' . trim(strip_tags($post_title));
11321 + }
11322 + if(!empty($max_seed_tokens) && aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
11323 + {
11324 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
11325 + }
11326 + $title_ai_command = trim($title_ai_command);
11327 + if(empty($title_ai_command))
11328 + {
11329 + aiomatic_log_to_file('Empty API title seed expression provided(8)! ' . print_r($title_ai_command, true));
11330 + }
11331 + else
11332 + {
11333 + $query_token_count = count(aiomatic_encode($title_ai_command));
11334 + $max_tokens = aimogenpro_get_max_tokens($kw_model);
11335 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
11336 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
11337 + {
11338 + $string_len = aiomatic_strlen($title_ai_command);
11339 + $string_len = $string_len / 2;
11340 + $string_len = intval(0 - $string_len);
11341 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
11342 + $title_ai_command = trim($title_ai_command);
11343 + $query_token_count = count(aiomatic_encode($title_ai_command));
11344 + $available_tokens = $max_tokens - $query_token_count;
11345 + }
11346 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11347 + {
11348 + $api_service = aiomatic_get_api_service($token, $kw_model);
11349 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $kw_model . ')(' . $available_tokens . ') for title text3: ' . $title_ai_command);
11350 + }
11351 + $aierror = '';
11352 + $finish_reason = '';
11353 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordCompletion', 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
11354 + if($generated_text === false)
11355 + {
11356 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
11357 + $ai_title = '';
11358 + }
11359 + else
11360 + {
11361 + $ai_title = trim(trim(trim(trim($generated_text), '.'), ' "\''));
11362 + $ai_titles = explode(',', $ai_title);
11363 + foreach($ai_titles as $query_words)
11364 + {
11365 + $attached_id = '';
11366 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, trim($query_words), $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
11367 + if(!empty($z_img))
11368 + {
11369 + if (!aiomatic_generate_featured_image($z_img, $post->ID)) {
11370 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11371 + aiomatic_log_to_file('aiomatic_generate_featured_image failed using royalty free image: ' . $z_img);
11372 + }
11373 + }
11374 + else
11375 + {
11376 + $img_saved = true;
11377 + $img_set = true;
11378 + }
11379 + break;
11380 + }
11381 + }
11382 + }
11383 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11384 + {
11385 + $api_service = aiomatic_get_api_service($token, $kw_model);
11386 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
11387 + }
11388 + }
11389 + }
11390 + }
11391 + if($img_set == false)
11392 + {
11393 + $keyword_class = new Aiomatic_keywords();
11394 + $query_words = $keyword_class->keywords($image_query, 2);
11395 + $attached_id = '';
11396 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
11397 + if($z_img == '' || $z_img === false)
11398 + {
11399 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
11400 + {
11401 + $query_words = $keyword_class->keywords($image_query, 1);
11402 + $attached_id = '';
11403 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
11404 + }
11405 + }
11406 + if(!empty($z_img))
11407 + {
11408 + if (!aiomatic_generate_featured_image($z_img, $post->ID)) {
11409 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11410 + aiomatic_log_to_file('aiomatic_generate_featured_image failed using royalty free image: ' . $z_img);
11411 + }
11412 + }
11413 + else
11414 + {
11415 + $img_saved = true;
11416 + }
11417 + }
11418 + }
11419 + }
11420 + if($img_saved == true)
11421 + {
11422 + update_post_meta($post->ID, $custom_name, "pub");
11423 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
11424 + {
11425 + $args = array();
11426 + $args['ID'] = $post->ID;
11427 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
11428 + remove_filter('content_save_pre', 'wp_filter_post_kses');
11429 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11430 + remove_filter('title_save_pre', 'wp_filter_kses');
11431 + $post_updated = wp_update_post($args);
11432 + $changes_made = true;
11433 + add_filter('content_save_pre', 'wp_filter_post_kses');
11434 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11435 + add_filter('title_save_pre', 'wp_filter_kses');
11436 + if (is_wp_error($post_updated)) {
11437 + $errors = $post_updated->get_error_messages();
11438 + foreach ($errors as $error) {
11439 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
11440 + }
11441 + }
11442 + }
11443 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
11444 + {
11445 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
11446 + if($date_stamp)
11447 + {
11448 + $args = array();
11449 + $args['ID'] = $post->ID;
11450 + $postdate = date("Y-m-d H:i:s", $date_stamp);
11451 + $args['post_date'] = $postdate;
11452 + remove_filter('content_save_pre', 'wp_filter_post_kses');
11453 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11454 + remove_filter('title_save_pre', 'wp_filter_kses');
11455 + $post_updated = wp_update_post($args);
11456 + $changes_made = true;
11457 + add_filter('content_save_pre', 'wp_filter_post_kses');
11458 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11459 + add_filter('title_save_pre', 'wp_filter_kses');
11460 + if (is_wp_error($post_updated)) {
11461 + $errors = $post_updated->get_error_messages();
11462 + foreach ($errors as $error) {
11463 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
11464 + }
11465 + }
11466 + }
11467 + }
11468 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11469 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with royalty free image.');
11470 + }
11471 + }
11472 + }
11473 + if (isset($aiomatic_Spinner_Settings['ai_featured_image_edit']) && $aiomatic_Spinner_Settings['ai_featured_image_edit'] != '' && $aiomatic_Spinner_Settings['ai_featured_image_edit'] != 'disabled')
11474 + {
11475 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11476 + {
11477 + aiomatic_log_to_file('Starting post featured image editor...');
11478 + }
11479 + if (isset($aiomatic_Spinner_Settings['image_strength']) && trim($aiomatic_Spinner_Settings['image_strength']) != '')
11480 + {
11481 + $image_strength = trim($aiomatic_Spinner_Settings['image_strength']);
11482 + }
11483 + else
11484 + {
11485 + $image_strength = '0.90';
11486 + }
11487 + wp_suspend_cache_addition(true);
11488 + $metas = get_post_custom($post->ID);
11489 + wp_suspend_cache_addition(false);
11490 + if(is_array($metas))
11491 + {
11492 + $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
11493 + }
11494 + else
11495 + {
11496 + $rez_meta = array();
11497 + }
11498 + if(count($rez_meta) > 0)
11499 + {
11500 + foreach($rez_meta as $rm)
11501 + {
11502 + if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
11503 + {
11504 + $featured_image = $rm[0];
11505 + break;
11506 + }
11507 + }
11508 + }
11509 + if(empty($featured_image))
11510 + {
11511 + $featured_image = get_the_post_thumbnail_url($post->ID, 'full');
11512 + }
11513 + if(empty($featured_image))
11514 + {
11515 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11516 + aiomatic_log_to_file('Post ID ' . $post->ID . ' does not have a featured image assigned, nothing to edit!');
11517 + }
11518 + }
11519 + else
11520 + {
11521 + $img_saved = false;
11522 + if (isset($aiomatic_Spinner_Settings['ai_image_command_edit']) && $aiomatic_Spinner_Settings['ai_image_command_edit'] != '')
11523 + {
11524 + $aicontent = trim(strip_tags($aiomatic_Spinner_Settings['ai_image_command_edit']));
11525 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
11526 + $post_link = get_permalink($post->ID);
11527 + $blog_title = html_entity_decode(get_bloginfo('title'));
11528 + $author_obj = get_user_by('id', $post->post_author);
11529 + if($author_obj !== false && isset($author_obj->user_nicename))
11530 + {
11531 + $user_name = $author_obj->user_nicename;
11532 + }
11533 + else
11534 + {
11535 + $user_name = '';
11536 + }
11537 + $post_cats = '';
11538 + $post_categories = wp_get_post_categories( $post->ID );
11539 + foreach($post_categories as $c){
11540 + $cat = get_category( $c );
11541 + $post_cats .= $cat->name . ',';
11542 + }
11543 + $post_cats = trim($post_cats, ',');
11544 + if($post_cats != '')
11545 + {
11546 + $post_categories = explode(',', $post_cats);
11547 + }
11548 + else
11549 + {
11550 + $post_categories = array();
11551 + }
11552 + if(count($post_categories) == 0)
11553 + {
11554 + $terms = get_the_terms( $post->ID, 'product_cat' );
11555 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
11556 + foreach ( $terms as $term ) {
11557 + $post_categories[] = $term->slug;
11558 + }
11559 + $post_cats = implode(',', $post_categories);
11560 + }
11561 +
11562 + }
11563 + foreach($post_categories as $pc)
11564 + {
11565 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
11566 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
11567 + {
11568 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
11569 + {
11570 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11571 + {
11572 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
11573 + }
11574 + update_post_meta($post->ID, $custom_name, "skip");
11575 + return;
11576 + }
11577 + }
11578 + }
11579 + }
11580 + $post_tagz = '';
11581 + $post_tags = wp_get_post_tags( $post->ID );
11582 + foreach($post_tags as $t){
11583 + $post_tagz .= $t->name . ',';
11584 + }
11585 + $post_tagz = trim($post_tagz, ',');
11586 + if($post_tagz != '')
11587 + {
11588 + $post_tags = explode(',', $post_tagz);
11589 + }
11590 + else
11591 + {
11592 + $post_tags = array();
11593 + }
11594 + if(count($post_tags) == 0)
11595 + {
11596 + $terms = get_the_terms( $post->ID, 'product_tag' );
11597 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
11598 + foreach ( $terms as $term ) {
11599 + $post_tags[] = $term->slug;
11600 + }
11601 + $post_tagz = implode(',', $post_tags);
11602 + }
11603 +
11604 + }
11605 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
11606 +
11607 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
11608 + foreach($disable_users as $disable_user)
11609 + {
11610 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
11611 + {
11612 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11613 + {
11614 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
11615 + }
11616 + update_post_meta($post->ID, $custom_name, "skip");
11617 + return;
11618 + }
11619 + }
11620 + }
11621 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
11622 +
11623 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
11624 + $found = false;
11625 + foreach($enable_users as $disable_user)
11626 + {
11627 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
11628 + {
11629 + $found = true;
11630 + }
11631 + }
11632 + if($found === false)
11633 + {
11634 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11635 + {
11636 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
11637 + }
11638 + update_post_meta($post->ID, $custom_name, "skip");
11639 + return;
11640 + }
11641 + }
11642 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
11643 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
11644 + {
11645 + if ( !has_post_thumbnail($post->ID) )
11646 + {
11647 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11648 + {
11649 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
11650 + }
11651 + update_post_meta($post->ID, $custom_name, "skip");
11652 + return;
11653 + }
11654 + }
11655 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
11656 + {
11657 + if ( has_post_thumbnail($post->ID) )
11658 + {
11659 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11660 + {
11661 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
11662 + }
11663 + update_post_meta($post->ID, $custom_name, "skip");
11664 + return;
11665 + }
11666 + }
11667 + }
11668 +
11669 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
11670 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
11671 + {
11672 + if ( !aiomatic_check_seo_description($post->ID) )
11673 + {
11674 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11675 + {
11676 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
11677 + }
11678 + update_post_meta($post->ID, $custom_name, "skip");
11679 + return;
11680 + }
11681 + }
11682 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
11683 + {
11684 + if ( aiomatic_check_seo_description($post->ID) )
11685 + {
11686 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11687 + {
11688 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
11689 + }
11690 + update_post_meta($post->ID, $custom_name, "skip");
11691 + return;
11692 + }
11693 + }
11694 + }
11695 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
11696 + $skip_me = true;
11697 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
11698 + foreach($enable_field as $fieldx)
11699 + {
11700 + if(strstr($fieldx, '=>') !== false)
11701 + {
11702 + $fna = explode('=>', $fieldx);
11703 + if(isset($fna[1]))
11704 + {
11705 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
11706 + if ($custom_val)
11707 + {
11708 + if(trim($fna[1]) == '*')
11709 + {
11710 + $skip_me = false;
11711 + }
11712 + if($custom_val == trim($fna[1]))
11713 + {
11714 + $skip_me = false;
11715 + }
11716 + }
11717 + }
11718 + }
11719 + }
11720 + if($skip_me === true)
11721 + {
11722 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11723 + {
11724 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
11725 + }
11726 + update_post_meta($post->ID, $custom_name, "skip");
11727 + return;
11728 + }
11729 + }
11730 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
11731 + $skip_me = false;
11732 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
11733 + foreach($disable_field as $fieldx)
11734 + {
11735 + if(strstr($fieldx, '=>') !== false)
11736 + {
11737 + $fna = explode('=>', $fieldx);
11738 + if(isset($fna[1]))
11739 + {
11740 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
11741 + if ($custom_val)
11742 + {
11743 + if(trim($fna[1]) == '*')
11744 + {
11745 + $skip_me = true;
11746 + }
11747 + if($custom_val == trim($fna[1]))
11748 + {
11749 + $skip_me = true;
11750 + }
11751 + }
11752 + }
11753 + }
11754 + }
11755 + if($skip_me === true)
11756 + {
11757 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11758 + {
11759 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
11760 + }
11761 + update_post_meta($post->ID, $custom_name, "skip");
11762 + return;
11763 + }
11764 + }
11765 + foreach($post_tags as $pt)
11766 + {
11767 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
11768 +
11769 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
11770 + foreach($disable_tags as $disabled_tag)
11771 + {
11772 + if($manual != true && trim($pt) == trim($disabled_tag))
11773 + {
11774 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11775 + {
11776 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
11777 + }
11778 + update_post_meta($post->ID, $custom_name, "skip");
11779 + return;
11780 + }
11781 + }
11782 + }
11783 + }
11784 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
11785 + $skip = true;
11786 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
11787 + foreach($post_tags as $pt)
11788 + {
11789 + foreach($enable_tags as $enable_tag)
11790 + {
11791 + if(trim($pt) == trim($enable_tag))
11792 + {
11793 + $skip = false;
11794 + break;
11795 + }
11796 + }
11797 + }
11798 + if($skip == true)
11799 + {
11800 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11801 + {
11802 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
11803 + }
11804 + update_post_meta($post->ID, $custom_name, "skip");
11805 + return;
11806 + }
11807 + }
11808 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
11809 + }
11810 + else
11811 + {
11812 + $aicontent = trim(strip_tags($final_content));
11813 + if(empty($aicontent))
11814 + {
11815 + $aicontent = trim(strip_tags($post->post_excerpt));
11816 + }
11817 + if(empty($aicontent))
11818 + {
11819 + $aicontent = trim(strip_tags($post_title));
11820 + $last_char = aiomatic_substr($aicontent, -1, null);
11821 + if(!ctype_punct($last_char))
11822 + {
11823 + $aicontent .= '.';
11824 + }
11825 + }
11826 + }
11827 + if(isset($aiomatic_Spinner_Settings['ai_featured_image_engine']) && $aiomatic_Spinner_Settings['ai_featured_image_engine'] != '')
11828 + {
11829 + $fisource = $aiomatic_Spinner_Settings['ai_featured_image_engine'];
11830 + }
11831 + else
11832 + {
11833 + $fisource = '2';
11834 + }
11835 + if($fisource == '2')
11836 + {
11837 +
11838 + if (!isset($aiomatic_Main_Settings['stability_app_id']) || trim($aiomatic_Main_Settings['stability_app_id']) == '')
11839 + {
11840 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11841 + {
11842 + aiomatic_log_to_file('You need to enter a Stability.AI API key in the plugin\'s settings for this feature to work.');
11843 + }
11844 + }
11845 + else
11846 + {
11847 + $aicontent = trim($aicontent);
11848 + if(aiomatic_strlen($aicontent) > 2000)
11849 + {
11850 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
11851 + }
11852 + $skip_this_copy = true;
11853 + if(!function_exists('is_plugin_active'))
11854 + {
11855 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
11856 + }
11857 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
11858 + {
11859 + $skip_this_copy = false;
11860 + }
11861 + $ierror = '';
11862 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
11863 + {
11864 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
11865 + }
11866 + else
11867 + {
11868 + $image_size = '1024x1024';
11869 + }
11870 + if($image_size == '256x256')
11871 + {
11872 + $width = '512';
11873 + $height = '512';
11874 + }
11875 + elseif($image_size == '512x512')
11876 + {
11877 + $width = '512';
11878 + $height = '512';
11879 + }
11880 + elseif($image_size == '1024x1024')
11881 + {
11882 + $width = '1024';
11883 + $height = '1024';
11884 + }
11885 + elseif($image_size == '1792x1024')
11886 + {
11887 + $width = '1792';
11888 + $height = '1024';
11889 + }
11890 + elseif($image_size == '1024x1792')
11891 + {
11892 + $width = '1024';
11893 + $height = '1792';
11894 + }
11895 + else
11896 + {
11897 + $width = '512';
11898 + $height = '512';
11899 + }
11900 + $temp_get_imgs = aiomatic_generate_stability_image($aicontent, $height, $width, 'editorAIFeaturedStableImage', 0, false, $ierror, $skip_this_copy, false, $featured_image, $image_strength);
11901 + if($temp_get_imgs !== false && isset($temp_get_imgs[0]))
11902 + {
11903 + $temp_get_img_local = $temp_get_imgs[0];
11904 + if (!aiomatic_assign_featured_image_path($temp_get_img_local, $post->ID)) {
11905 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11906 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed using Stability.AI image editor for ' .$temp_get_imgs[1]);
11907 + }
11908 + }
11909 + else
11910 + {
11911 + $img_saved = true;
11912 + }
11913 + }
11914 + else
11915 + {
11916 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11917 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit the featured image using Stability.AI: ' . $ierror);
11918 + }
11919 + }
11920 + }
11921 + }
11922 + if($img_saved == true)
11923 + {
11924 + update_post_meta($post->ID, $custom_name, "pub");
11925 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
11926 + {
11927 + $args = array();
11928 + $args['ID'] = $post->ID;
11929 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
11930 + remove_filter('content_save_pre', 'wp_filter_post_kses');
11931 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11932 + remove_filter('title_save_pre', 'wp_filter_kses');
11933 + $post_updated = wp_update_post($args);
11934 + $changes_made = true;
11935 + add_filter('content_save_pre', 'wp_filter_post_kses');
11936 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11937 + add_filter('title_save_pre', 'wp_filter_kses');
11938 + if (is_wp_error($post_updated)) {
11939 + $errors = $post_updated->get_error_messages();
11940 + foreach ($errors as $error) {
11941 + aiomatic_log_to_file('Error occured while updating post for title "' . $post->post_title . '": ' . $error);
11942 + }
11943 + }
11944 + }
11945 + if (isset($aiomatic_Spinner_Settings['change_date']) && $aiomatic_Spinner_Settings['change_date'] != '')
11946 + {
11947 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
11948 + if($date_stamp)
11949 + {
11950 + $args = array();
11951 + $args['ID'] = $post->ID;
11952 + $postdate = date("Y-m-d H:i:s", $date_stamp);
11953 + $args['post_date'] = $postdate;
11954 + remove_filter('content_save_pre', 'wp_filter_post_kses');
11955 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11956 + remove_filter('title_save_pre', 'wp_filter_kses');
11957 + $post_updated = wp_update_post($args);
11958 + $changes_made = true;
11959 + add_filter('content_save_pre', 'wp_filter_post_kses');
11960 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
11961 + add_filter('title_save_pre', 'wp_filter_kses');
11962 + if (is_wp_error($post_updated)) {
11963 + $errors = $post_updated->get_error_messages();
11964 + foreach ($errors as $error) {
11965 + aiomatic_log_to_file('Error occured while updating post status for title "' . $post->post_title . '": ' . $error);
11966 + }
11967 + }
11968 + }
11969 + }
11970 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11971 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated image.');
11972 + }
11973 + }
11974 + }
11975 + }
11976 + if (isset($aiomatic_Spinner_Settings['ai_featured_image_edit_content']) && $aiomatic_Spinner_Settings['ai_featured_image_edit_content'] != '' && $aiomatic_Spinner_Settings['ai_featured_image_edit_content'] != 'disabled')
11977 + {
11978 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
11979 + {
11980 + aiomatic_log_to_file('Starting post content image editor...');
11981 + }
11982 + if (isset($aiomatic_Spinner_Settings['image_strength_content']) && trim($aiomatic_Spinner_Settings['image_strength_content']) != '')
11983 + {
11984 + $image_strength = trim($aiomatic_Spinner_Settings['image_strength_content']);
11985 + }
11986 + else
11987 + {
11988 + $image_strength = '0.90';
11989 + }
11990 + $pattern = '/<img[^>]+src="([^"]+)"/i';
11991 + $srcs = [];
11992 + if (preg_match_all($pattern, $post->post_content, $matches) && isset($matches[1]))
11993 + {
11994 + $srcs = $matches[1];
11995 + }
11996 + if(count($srcs) > 0)
11997 + {
11998 + if (isset($aiomatic_Spinner_Settings['ai_image_command_edit_content']) && $aiomatic_Spinner_Settings['ai_image_command_edit_content'] != '')
11999 + {
12000 + $aicontent = trim(strip_tags($aiomatic_Spinner_Settings['ai_image_command_edit_content']));
12001 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
12002 + $post_link = get_permalink($post->ID);
12003 + $blog_title = html_entity_decode(get_bloginfo('title'));
12004 + $author_obj = get_user_by('id', $post->post_author);
12005 + if($author_obj !== false && isset($author_obj->user_nicename))
12006 + {
12007 + $user_name = $author_obj->user_nicename;
12008 + }
12009 + else
12010 + {
12011 + $user_name = '';
12012 + }
12013 + $post_cats = '';
12014 + $post_categories = wp_get_post_categories( $post->ID );
12015 + foreach($post_categories as $c){
12016 + $cat = get_category( $c );
12017 + $post_cats .= $cat->name . ',';
12018 + }
12019 + $post_cats = trim($post_cats, ',');
12020 + if($post_cats != '')
12021 + {
12022 + $post_categories = explode(',', $post_cats);
12023 + }
12024 + else
12025 + {
12026 + $post_categories = array();
12027 + }
12028 + if(count($post_categories) == 0)
12029 + {
12030 + $terms = get_the_terms( $post->ID, 'product_cat' );
12031 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
12032 + foreach ( $terms as $term ) {
12033 + $post_categories[] = $term->slug;
12034 + }
12035 + $post_cats = implode(',', $post_categories);
12036 + }
12037 +
12038 + }
12039 + foreach($post_categories as $pc)
12040 + {
12041 + if (!$manual && isset($aiomatic_Spinner_Settings['disabled_categories']) && !empty($aiomatic_Spinner_Settings['disabled_categories'])) {
12042 + foreach($aiomatic_Spinner_Settings['disabled_categories'] as $disabled_cat)
12043 + {
12044 + if($manual != true && trim($pc) == get_cat_name($disabled_cat))
12045 + {
12046 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12047 + {
12048 + aiomatic_log_to_file('Skipping post, has a disabled category (' . get_cat_name($disabled_cat) . '): ' . $post->post_title);
12049 + }
12050 + update_post_meta($post->ID, $custom_name, "skip");
12051 + return;
12052 + }
12053 + }
12054 + }
12055 + }
12056 + $post_tagz = '';
12057 + $post_tags = wp_get_post_tags( $post->ID );
12058 + foreach($post_tags as $t){
12059 + $post_tagz .= $t->name . ',';
12060 + }
12061 + $post_tagz = trim($post_tagz, ',');
12062 + if($post_tagz != '')
12063 + {
12064 + $post_tags = explode(',', $post_tagz);
12065 + }
12066 + else
12067 + {
12068 + $post_tags = array();
12069 + }
12070 + if(count($post_tags) == 0)
12071 + {
12072 + $terms = get_the_terms( $post->ID, 'product_tag' );
12073 + if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
12074 + foreach ( $terms as $term ) {
12075 + $post_tags[] = $term->slug;
12076 + }
12077 + $post_tagz = implode(',', $post_tags);
12078 + }
12079 +
12080 + }
12081 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_users']) && $aiomatic_Spinner_Settings['disable_users'] != '') {
12082 +
12083 + $disable_users = explode(",", $aiomatic_Spinner_Settings['disable_users']);
12084 + foreach($disable_users as $disable_user)
12085 + {
12086 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
12087 + {
12088 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12089 + {
12090 + aiomatic_log_to_file('Skipping post, has a disabled author user ID: ' . $post->post_author);
12091 + }
12092 + update_post_meta($post->ID, $custom_name, "skip");
12093 + return;
12094 + }
12095 + }
12096 + }
12097 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_users']) && $aiomatic_Spinner_Settings['enable_users'] != '') {
12098 +
12099 + $enable_users = explode(",", $aiomatic_Spinner_Settings['enable_users']);
12100 + $found = false;
12101 + foreach($enable_users as $disable_user)
12102 + {
12103 + if(!empty(trim($disable_user)) && $manual != true && $post->post_author == intval(trim($disable_user)))
12104 + {
12105 + $found = true;
12106 + }
12107 + }
12108 + if($found === false)
12109 + {
12110 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12111 + {
12112 + aiomatic_log_to_file('Skipping post, does not have enabled author user ID: ' . $post->post_author);
12113 + }
12114 + update_post_meta($post->ID, $custom_name, "skip");
12115 + return;
12116 + }
12117 + }
12118 + if (!$manual && isset($aiomatic_Spinner_Settings['featured_status']) && $aiomatic_Spinner_Settings['featured_status'] != '' && $aiomatic_Spinner_Settings['featured_status'] != 'any') {
12119 + if ($aiomatic_Spinner_Settings['featured_status'] == 'yes')
12120 + {
12121 + if ( !has_post_thumbnail($post->ID) )
12122 + {
12123 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12124 + {
12125 + aiomatic_log_to_file('Skipping post, does not have a featured image: ' . $post->ID);
12126 + }
12127 + update_post_meta($post->ID, $custom_name, "skip");
12128 + return;
12129 + }
12130 + }
12131 + elseif ($aiomatic_Spinner_Settings['featured_status'] == 'no')
12132 + {
12133 + if ( has_post_thumbnail($post->ID) )
12134 + {
12135 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12136 + {
12137 + aiomatic_log_to_file('Skipping post, already has a featured image: ' . $post->ID);
12138 + }
12139 + update_post_meta($post->ID, $custom_name, "skip");
12140 + return;
12141 + }
12142 + }
12143 + }
12144 +
12145 + if (!$manual && isset($aiomatic_Spinner_Settings['seo_status']) && $aiomatic_Spinner_Settings['seo_status'] != '' && $aiomatic_Spinner_Settings['seo_status'] != 'any') {
12146 + if ($aiomatic_Spinner_Settings['seo_status'] == 'yes')
12147 + {
12148 + if ( !aiomatic_check_seo_description($post->ID) )
12149 + {
12150 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12151 + {
12152 + aiomatic_log_to_file('Skipping post, does not have a SEO meta description: ' . $post->ID);
12153 + }
12154 + update_post_meta($post->ID, $custom_name, "skip");
12155 + return;
12156 + }
12157 + }
12158 + elseif ($aiomatic_Spinner_Settings['seo_status'] == 'no')
12159 + {
12160 + if ( aiomatic_check_seo_description($post->ID) )
12161 + {
12162 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12163 + {
12164 + aiomatic_log_to_file('Skipping post, already has a SEO meta description: ' . $post->ID);
12165 + }
12166 + update_post_meta($post->ID, $custom_name, "skip");
12167 + return;
12168 + }
12169 + }
12170 + }
12171 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_field']) && trim($aiomatic_Spinner_Settings['enable_field']) != '') {
12172 + $skip_me = true;
12173 + $enable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['enable_field']));
12174 + foreach($enable_field as $fieldx)
12175 + {
12176 + if(strstr($fieldx, '=>') !== false)
12177 + {
12178 + $fna = explode('=>', $fieldx);
12179 + if(isset($fna[1]))
12180 + {
12181 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
12182 + if ($custom_val)
12183 + {
12184 + if(trim($fna[1]) == '*')
12185 + {
12186 + $skip_me = false;
12187 + }
12188 + if($custom_val == trim($fna[1]))
12189 + {
12190 + $skip_me = false;
12191 + }
12192 + }
12193 + }
12194 + }
12195 + }
12196 + if($skip_me === true)
12197 + {
12198 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12199 + {
12200 + aiomatic_log_to_file('Skipping post, does not have required custom field set: ' . $post->ID);
12201 + }
12202 + update_post_meta($post->ID, $custom_name, "skip");
12203 + return;
12204 + }
12205 + }
12206 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_field']) && trim($aiomatic_Spinner_Settings['disable_field']) != '') {
12207 + $skip_me = false;
12208 + $disable_field = preg_split('/\r\n|\r|\n/', trim($aiomatic_Spinner_Settings['disable_field']));
12209 + foreach($disable_field as $fieldx)
12210 + {
12211 + if(strstr($fieldx, '=>') !== false)
12212 + {
12213 + $fna = explode('=>', $fieldx);
12214 + if(isset($fna[1]))
12215 + {
12216 + $custom_val = get_post_meta($post->ID, trim($fna[0]), true);
12217 + if ($custom_val)
12218 + {
12219 + if(trim($fna[1]) == '*')
12220 + {
12221 + $skip_me = true;
12222 + }
12223 + if($custom_val == trim($fna[1]))
12224 + {
12225 + $skip_me = true;
12226 + }
12227 + }
12228 + }
12229 + }
12230 + }
12231 + if($skip_me === true)
12232 + {
12233 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12234 + {
12235 + aiomatic_log_to_file('Skipping post, has skipped custom field set: ' . $post->ID);
12236 + }
12237 + update_post_meta($post->ID, $custom_name, "skip");
12238 + return;
12239 + }
12240 + }
12241 + foreach($post_tags as $pt)
12242 + {
12243 + if (!$manual && isset($aiomatic_Spinner_Settings['disable_tags']) && $aiomatic_Spinner_Settings['disable_tags'] != '') {
12244 +
12245 + $disable_tags = explode(",", $aiomatic_Spinner_Settings['disable_tags']);
12246 + foreach($disable_tags as $disabled_tag)
12247 + {
12248 + if($manual != true && trim($pt) == trim($disabled_tag))
12249 + {
12250 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12251 + {
12252 + aiomatic_log_to_file('Skipping post, has a disabled tag: ' . $post->post_title);
12253 + }
12254 + update_post_meta($post->ID, $custom_name, "skip");
12255 + return;
12256 + }
12257 + }
12258 + }
12259 + }
12260 + if (!$manual && isset($aiomatic_Spinner_Settings['enable_tags']) && $aiomatic_Spinner_Settings['enable_tags'] != '') {
12261 + $skip = true;
12262 + $enable_tags = explode(",", $aiomatic_Spinner_Settings['enable_tags']);
12263 + foreach($post_tags as $pt)
12264 + {
12265 + foreach($enable_tags as $enable_tag)
12266 + {
12267 + if(trim($pt) == trim($enable_tag))
12268 + {
12269 + $skip = false;
12270 + break;
12271 + }
12272 + }
12273 + }
12274 + if($skip == true)
12275 + {
12276 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12277 + {
12278 + aiomatic_log_to_file('Skipping post, does not have required tag: ' . $post->post_title);
12279 + }
12280 + update_post_meta($post->ID, $custom_name, "skip");
12281 + return;
12282 + }
12283 + }
12284 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post->post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $post->ID, '', '', '', '', '', '');
12285 + }
12286 + else
12287 + {
12288 + $aicontent = trim(strip_tags($final_content));
12289 + if(empty($aicontent))
12290 + {
12291 + $aicontent = trim(strip_tags($post->post_excerpt));
12292 + }
12293 + if(empty($aicontent))
12294 + {
12295 + $aicontent = trim(strip_tags($post_title));
12296 + $last_char = aiomatic_substr($aicontent, -1, null);
12297 + if(!ctype_punct($last_char))
12298 + {
12299 + $aicontent .= '.';
12300 + }
12301 + }
12302 + }
12303 + $aicontent = trim($aicontent);
12304 + if(aiomatic_strlen($aicontent) > 2000)
12305 + {
12306 + $aicontent = aiomatic_substr($aicontent, 0, 2000);
12307 + }
12308 + if(isset($aiomatic_Spinner_Settings['ai_featured_image_engine_content']) && $aiomatic_Spinner_Settings['ai_featured_image_engine_content'] != '')
12309 + {
12310 + $fisource = $aiomatic_Spinner_Settings['ai_featured_image_engine_content'];
12311 + }
12312 + else
12313 + {
12314 + $fisource = '2';
12315 + }
12316 + if($fisource == '2')
12317 + {
12318 + if (!isset($aiomatic_Main_Settings['stability_app_id']) || trim($aiomatic_Main_Settings['stability_app_id']) == '')
12319 + {
12320 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12321 + {
12322 + aiomatic_log_to_file('You need to enter a Stability.AI API key in the plugin\'s settings for this feature to work.');
12323 + }
12324 + }
12325 + if(!function_exists('is_plugin_active'))
12326 + {
12327 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
12328 + }
12329 + $skip_this_copy = true;
12330 + if ((is_plugin_active('featured-image-from-url/featured-image-from-url.php') || is_plugin_active('fifu-premium/fifu-premium.php')) && isset($aiomatic_Main_Settings['url_image']) && trim($aiomatic_Main_Settings['url_image']) == 'on')
12331 + {
12332 + $skip_this_copy = false;
12333 + }
12334 + $changemade = 0;
12335 + foreach($srcs as $fimg)
12336 + {
12337 + if($fisource == '2')
12338 + {
12339 + $ierror = '';
12340 + if (isset($aiomatic_Spinner_Settings['image_size']) && trim($aiomatic_Spinner_Settings['image_size']) != '')
12341 + {
12342 + $image_size = trim($aiomatic_Spinner_Settings['image_size']);
12343 + }
12344 + else
12345 + {
12346 + $image_size = '1024x1024';
12347 + }
12348 + if($image_size == '256x256')
12349 + {
12350 + $width = '512';
12351 + $height = '512';
12352 + }
12353 + elseif($image_size == '512x512')
12354 + {
12355 + $width = '512';
12356 + $height = '512';
12357 + }
12358 + elseif($image_size == '1024x1024')
12359 + {
12360 + $width = '1024';
12361 + $height = '1024';
12362 + }
12363 + elseif($image_size == '1792x1024')
12364 + {
12365 + $width = '1792';
12366 + $height = '1024';
12367 + }
12368 + elseif($image_size == '1024x1792')
12369 + {
12370 + $width = '1024';
12371 + $height = '1792';
12372 + }
12373 + else
12374 + {
12375 + $width = '512';
12376 + $height = '512';
12377 + }
12378 + $temp_get_imgs = aiomatic_generate_stability_image($aicontent, $height, $width, 'editorAIFeaturedStableImage', 0, false, $ierror, $skip_this_copy, false, $fimg, $image_strength);
12379 + if($temp_get_imgs !== false)
12380 + {
12381 + $temp_get_img_remote = $temp_get_imgs[1];
12382 + $post->post_content = str_replace($fimg, $temp_get_img_remote, $post->post_content);
12383 + $changemade++;
12384 + }
12385 + else
12386 + {
12387 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12388 + aiomatic_log_to_file('Post ID ' . $post->ID . ' failed to edit the content image (' . $fimg . ') using Stability.AI: ' . $ierror);
12389 + }
12390 + }
12391 + }
12392 + }
12393 + if ($changemade > 0)
12394 + {
12395 + $args = array();
12396 + $args['ID'] = $post->ID;
12397 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
12398 + {
12399 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
12400 + }
12401 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
12402 + {
12403 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
12404 + if($date_stamp)
12405 + {
12406 + $postdate = date("Y-m-d H:i:s", $date_stamp);
12407 + $args['post_date'] = $postdate;
12408 + }
12409 + }
12410 + $args['post_content'] = $post->post_content;
12411 + remove_filter('content_save_pre', 'wp_filter_post_kses');
12412 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
12413 + remove_filter('title_save_pre', 'wp_filter_kses');
12414 + update_post_meta($post->ID, $custom_name, "pub");
12415 + $post_updated = wp_update_post($args);
12416 + $changes_made = true;
12417 + add_filter('content_save_pre', 'wp_filter_post_kses');
12418 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
12419 + add_filter('title_save_pre', 'wp_filter_kses');
12420 + if (is_wp_error($post_updated)) {
12421 + $errors = $post_updated->get_error_messages();
12422 + foreach ($errors as $error) {
12423 + aiomatic_log_to_file('Error occured while updating AI generated images for post title "' . $post->post_title . '": ' . $error);
12424 + }
12425 + }
12426 + else
12427 + {
12428 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12429 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with ' . $changemade . ' Stability AI edited images in post content.');
12430 + }
12431 + }
12432 + }
12433 + }
12434 + }
12435 + }
12436 + if (isset($aiomatic_Spinner_Settings['content_text_speech']) && $aiomatic_Spinner_Settings['content_text_speech'] != '' && $aiomatic_Spinner_Settings['content_text_speech'] != 'off')
12437 + {
12438 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12439 + {
12440 + aiomatic_log_to_file('Starting post text-to-speech module...');
12441 + }
12442 + $copy_it = 'local';
12443 + if(isset($aiomatic_Spinner_Settings['copy_location']) && !empty($aiomatic_Spinner_Settings['copy_location']))
12444 + {
12445 + $copy_it = $aiomatic_Spinner_Settings['copy_location'];
12446 + }
12447 + $updated = false;
12448 + if($aiomatic_Spinner_Settings['content_text_speech'] == 'openai')
12449 + {
12450 + if(!isset($aiomatic_Spinner_Settings['text_to_audio']) || empty($aiomatic_Spinner_Settings['text_to_audio']))
12451 + {
12452 + aiomatic_log_to_file('No text to send to text-to-speech!');
12453 + }
12454 + else
12455 + {
12456 + if (!isset($aiomatic_Main_Settings['app_id']))
12457 + {
12458 + $aiomatic_Main_Settings['app_id'] = '';
12459 + }
12460 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
12461 + $appids = array_filter($appids);
12462 + $token = $appids[array_rand($appids)];
12463 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
12464 + if (empty($token))
12465 + {
12466 + aiomatic_log_to_file('You need to enter an OpenAI API key for this to work!');
12467 + }
12468 + else
12469 + {
12470 + if(aiomatic_is_aiomaticapi_key($token) || (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)))
12471 + {
12472 + aiomatic_log_to_file('Only OpenAI API keys are supported at the moment.');
12473 + }
12474 + else
12475 + {
12476 + if(isset($aiomatic_Spinner_Settings['open_model_id']) && $aiomatic_Spinner_Settings['open_model_id'] != '')
12477 + {
12478 + $open_model_id = $aiomatic_Spinner_Settings['open_model_id'];
12479 + }
12480 + else
12481 + {
12482 + $open_model_id = 'tts-1';
12483 + }
12484 + if(isset($aiomatic_Spinner_Settings['open_voice']) && $aiomatic_Spinner_Settings['open_voice'] != '')
12485 + {
12486 + $open_voice = $aiomatic_Spinner_Settings['open_voice'];
12487 + }
12488 + else
12489 + {
12490 + $open_voice = 'alloy';
12491 + }
12492 + if(isset($aiomatic_Spinner_Settings['open_format']) && $aiomatic_Spinner_Settings['open_format'] != '')
12493 + {
12494 + $open_format = $aiomatic_Spinner_Settings['open_format'];
12495 + }
12496 + else
12497 + {
12498 + $open_format = 'mp3';
12499 + }
12500 + if(isset($aiomatic_Spinner_Settings['open_speed']) && $aiomatic_Spinner_Settings['open_speed'] != '')
12501 + {
12502 + $open_speed = $aiomatic_Spinner_Settings['open_speed'];
12503 + }
12504 + else
12505 + {
12506 + $open_speed = '1';
12507 + }
12508 + $message = trim($aiomatic_Spinner_Settings['text_to_audio']);
12509 + $message = aiomatic_replaceSynergyShortcodes($message);
12510 + if(!empty($message))
12511 + {
12512 + $message = aiomatic_replaceAIPostShortcodes($message, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
12513 + $session = aiomatic_get_session_id();
12514 + $message = wp_strip_all_tags($message);
12515 + if(!empty($message))
12516 + {
12517 + $query = new Aiomatic_Query($message, 0, 'openai-' . $open_model_id, '0', '', 'text-to-speech', 'text-to-speech', $token, $session, 1, '', '');
12518 + $result = aiomatic_openai_voice_stream($token, $open_model_id, $open_voice, $open_format, $open_speed, $message);
12519 + if(is_array($result))
12520 + {
12521 + aiomatic_log_to_file('Error occurred in OpenAI audio processing: ' . print_r($result, true));
12522 + }
12523 + else
12524 + {
12525 + apply_filters( 'aiomatic_ai_reply_text', $query, $message );
12526 + $localfile = aiomatic_copy_audio_stream_locally($result, 'audio_' . time() . '.' . $open_format, $copy_it);
12527 + if($localfile === false)
12528 + {
12529 + aiomatic_log_to_file('Failed to save audio file locally to your server.');
12530 + }
12531 + else
12532 + {
12533 + $retpath = $localfile[0];
12534 + $prep_txt = '';
12535 + if (isset($aiomatic_Spinner_Settings['prep_audio']) && $aiomatic_Spinner_Settings['prep_audio'] != '')
12536 + {
12537 + $prep_txt = $aiomatic_Spinner_Settings['prep_audio'];
12538 + }
12539 + if (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'append')
12540 + {
12541 + $final_content = $final_content . $prep_txt . ' <br/> [audio src="' . $retpath . '"]';
12542 + $updated = true;
12543 + }
12544 + elseif (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'preppend')
12545 + {
12546 + $final_content = $prep_txt . '[audio src="' . $retpath . '"] <br/> ' . $final_content;
12547 + $updated = true;
12548 + }
12549 + }
12550 + }
12551 + }
12552 + else
12553 + {
12554 + aiomatic_log_to_file('Empty input message after strippinig html tags');
12555 + }
12556 + }
12557 + else
12558 + {
12559 + aiomatic_log_to_file('Empty input message after audio processing');
12560 + }
12561 + }
12562 + }
12563 + }
12564 + }
12565 + elseif($aiomatic_Spinner_Settings['content_text_speech'] == 'elevenlabs')
12566 + {
12567 + if(!isset($aiomatic_Spinner_Settings['text_to_audio']) || empty($aiomatic_Spinner_Settings['text_to_audio']))
12568 + {
12569 + aiomatic_log_to_file('No text to send to text-to-speech!');
12570 + }
12571 + else
12572 + {
12573 + if (!isset($aiomatic_Main_Settings['elevenlabs_app_id']))
12574 + {
12575 + $aiomatic_Main_Settings['elevenlabs_app_id'] = '';
12576 + }
12577 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['elevenlabs_app_id']));
12578 + $appids = array_filter($appids);
12579 + $token = $appids[array_rand($appids)];
12580 + $token = apply_filters('aimogen_elevenlabs_api_key', $token);$token = apply_filters('aiomatic_elevenlabs_api_key', $token);
12581 + if (empty($token))
12582 + {
12583 + aiomatic_log_to_file('You need to enter an ElevenLabs API key for this to work!');
12584 + }
12585 + else
12586 + {
12587 + if(isset($aiomatic_Spinner_Settings['eleven_voice_custom']) && $aiomatic_Spinner_Settings['eleven_voice_custom'] != '')
12588 + {
12589 + $voice = $aiomatic_Spinner_Settings['eleven_voice_custom'];
12590 + }
12591 + else
12592 + {
12593 + if(isset($aiomatic_Spinner_Settings['eleven_voice']) && $aiomatic_Spinner_Settings['eleven_voice'] != '')
12594 + {
12595 + $voice = $aiomatic_Spinner_Settings['eleven_voice'];
12596 + }
12597 + else
12598 + {
12599 + $voice = '21m00Tcm4TlvDq8ikWAM';
12600 + }
12601 + }
12602 + $message = trim($aiomatic_Spinner_Settings['text_to_audio']);
12603 + $message = aiomatic_replaceSynergyShortcodes($message);
12604 + if(!empty($message))
12605 + {
12606 + $message = aiomatic_replaceAIPostShortcodes($message, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
12607 + $session = aiomatic_get_session_id();
12608 + $message = wp_strip_all_tags($message);
12609 + if(!empty($message))
12610 + {
12611 + $query = new Aiomatic_Query($message, 0, 'elevenlabs', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['elevenlabs_app_id']), $session, 1, '', '');
12612 + $result = aiomatic_elevenlabs_stream($voice, $message, 'aiomatic_Spinner_Settings');
12613 + if(is_array($result)){
12614 + aiomatic_log_to_file('Error occurred in ElevenLabs AI audio processing: ' . print_r($result, true));
12615 + }
12616 + else
12617 + {
12618 + apply_filters( 'aiomatic_ai_reply_text', $query, $message );
12619 + $localfile = aiomatic_copy_audio_stream_locally($result, 'audio_' . time() . '.mp3', $copy_it);
12620 + if($localfile === false)
12621 + {
12622 + aiomatic_log_to_file('Failed to save audio file locally to your server.');
12623 + }
12624 + else
12625 + {
12626 + $retpath = $localfile[0];
12627 + $prep_txt = '';
12628 + if (isset($aiomatic_Spinner_Settings['prep_audio']) && $aiomatic_Spinner_Settings['prep_audio'] != '')
12629 + {
12630 + $prep_txt = $aiomatic_Spinner_Settings['prep_audio'];
12631 + }
12632 + if (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'append')
12633 + {
12634 + $final_content = $final_content . $prep_txt . ' <br/> [audio src="' . $retpath . '"]';
12635 + $updated = true;
12636 + }
12637 + elseif (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'preppend')
12638 + {
12639 + $final_content = $prep_txt . '[audio src="' . $retpath . '"] <br/> ' . $final_content;
12640 + $updated = true;
12641 + }
12642 + }
12643 + }
12644 + }
12645 + }
12646 + }
12647 + }
12648 + }
12649 + elseif($aiomatic_Spinner_Settings['content_text_speech'] == 'google')
12650 + {
12651 + if(!isset($aiomatic_Spinner_Settings['text_to_audio']) || empty($aiomatic_Spinner_Settings['text_to_audio']))
12652 + {
12653 + aiomatic_log_to_file('No text to send to text-to-speech!');
12654 + }
12655 + else
12656 + {
12657 + if (!isset($aiomatic_Main_Settings['google_app_id']))
12658 + {
12659 + $aiomatic_Main_Settings['google_app_id'] = '';
12660 + }
12661 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['google_app_id']));
12662 + $appids = array_filter($appids);
12663 + $token = $appids[array_rand($appids)];
12664 + $token = apply_filters('aimogen_google_api_key', $token);$token = apply_filters('aiomatic_google_api_key', $token);
12665 + if (empty($token))
12666 + {
12667 + aiomatic_log_to_file('You need to enter an Google API key for this to work!');
12668 + }
12669 + else
12670 + {
12671 + if(isset($aiomatic_Spinner_Settings['google_voice']) && $aiomatic_Spinner_Settings['google_voice'] != '')
12672 + {
12673 + $voice = $aiomatic_Spinner_Settings['google_voice'];
12674 + if(isset($aiomatic_Spinner_Settings['audio_profile']) && $aiomatic_Spinner_Settings['audio_profile'] != '')
12675 + {
12676 + $audio_profile = $aiomatic_Spinner_Settings['audio_profile'];
12677 + }
12678 + else
12679 + {
12680 + $audio_profile = '';
12681 + }
12682 + if(isset($aiomatic_Spinner_Settings['voice_language']) && $aiomatic_Spinner_Settings['voice_language'] != '')
12683 + {
12684 + $voice_language = $aiomatic_Spinner_Settings['voice_language'];
12685 + if(isset($aiomatic_Spinner_Settings['voice_speed']) && $aiomatic_Spinner_Settings['voice_speed'] != '')
12686 + {
12687 + $voice_speed = $aiomatic_Spinner_Settings['voice_speed'];
12688 + }
12689 + else
12690 + {
12691 + $voice_speed = '';
12692 + }
12693 + if(isset($aiomatic_Spinner_Settings['google_voice_model']) && $aiomatic_Spinner_Settings['google_voice_model'] != '')
12694 + {
12695 + $google_voice_model = $aiomatic_Spinner_Settings['google_voice_model'];
12696 + }
12697 + else
12698 + {
12699 + $google_voice_model = '';
12700 + }
12701 + if(isset($aiomatic_Spinner_Settings['voice_pitch']) && $aiomatic_Spinner_Settings['voice_pitch'] != '')
12702 + {
12703 + $voice_pitch = $aiomatic_Spinner_Settings['voice_pitch'];
12704 + }
12705 + else
12706 + {
12707 + $voice_pitch = '';
12708 + }
12709 + $message = trim($aiomatic_Spinner_Settings['text_to_audio']);
12710 + $message = aiomatic_replaceSynergyShortcodes($message);
12711 + if(!empty($message))
12712 + {
12713 + $message = aiomatic_replaceAIPostShortcodes($message, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
12714 + $session = aiomatic_get_session_id();
12715 + $message = wp_strip_all_tags($message);
12716 + if(!empty($message))
12717 + {
12718 + $query = new Aiomatic_Query($message, 0, 'google', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['google_app_id']), $session, 1, '', '');
12719 + $result = aiomatic_google_stream($voice, $voice_language, $audio_profile, $voice_speed, $voice_pitch, $google_voice_model, $message);
12720 + if(is_array($result))
12721 + {
12722 + if(isset($result['status']) && $result['status'] == 'success')
12723 + {
12724 + apply_filters( 'aiomatic_ai_reply_text', $query, $message );
12725 + $decodedAudio = base64_decode($result['audio']);
12726 + $localfile = aiomatic_copy_audio_stream_locally($decodedAudio, 'audio_' . time() . '.mp3', $copy_it);
12727 + if($localfile === false)
12728 + {
12729 + aiomatic_log_to_file('Failed to save audio file locally to your server.');
12730 + }
12731 + else
12732 + {
12733 + $retpath = $localfile[0];
12734 + $prep_txt = '';
12735 + if (isset($aiomatic_Spinner_Settings['prep_audio']) && $aiomatic_Spinner_Settings['prep_audio'] != '')
12736 + {
12737 + $prep_txt = $aiomatic_Spinner_Settings['prep_audio'];
12738 + }
12739 + if (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'append')
12740 + {
12741 + $final_content = $final_content . $prep_txt . ' <br/> [audio src="' . $retpath . '"]';
12742 + $updated = true;
12743 + }
12744 + elseif (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'preppend')
12745 + {
12746 + $final_content = $prep_txt . '[audio src="' . $retpath . '"] <br/> ' . $final_content;
12747 + $updated = true;
12748 + }
12749 + }
12750 + }
12751 + else
12752 + {
12753 + aiomatic_log_to_file('Failed to generate Google Audio AI output: ' . print_r($result, true));
12754 + }
12755 + }
12756 + else
12757 + {
12758 + aiomatic_log_to_file('Failed to generate Google AI Audio output: ' . print_r($result, true));
12759 + }
12760 + }
12761 + }
12762 + }
12763 + else
12764 + {
12765 + aiomatic_log_to_file('You need to select a Google Text-to-Speech Voice Language for this feature to work.');
12766 + }
12767 + }
12768 + else
12769 + {
12770 + aiomatic_log_to_file('You need to select a Google Text-to-Speech Voice Name for this feature to work.');
12771 + }
12772 + }
12773 + }
12774 + }
12775 + elseif($aiomatic_Spinner_Settings['content_text_speech'] == 'did')
12776 + {
12777 + if(!isset($aiomatic_Spinner_Settings['text_to_audio']) || empty($aiomatic_Spinner_Settings['text_to_audio']))
12778 + {
12779 + aiomatic_log_to_file('No text to send to text-to-video!');
12780 + }
12781 + else
12782 + {
12783 + if (!isset($aiomatic_Main_Settings['did_app_id']))
12784 + {
12785 + $aiomatic_Main_Settings['did_app_id'] = '';
12786 + }
12787 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['did_app_id']));
12788 + $appids = array_filter($appids);
12789 + $token = $appids[array_rand($appids)];
12790 + $token = apply_filters('aimogen_did_api_key', $token);$token = apply_filters('aiomatic_did_api_key', $token);
12791 + if (empty($token))
12792 + {
12793 + aiomatic_log_to_file('You need to enter an D-ID API key for this to work!');
12794 + }
12795 + else
12796 + {
12797 + if(isset($aiomatic_Spinner_Settings['did_image']) && $aiomatic_Spinner_Settings['did_image'] != '')
12798 + {
12799 + $did_image = $aiomatic_Spinner_Settings['did_image'];
12800 + }
12801 + else
12802 + {
12803 + $did_image = 'https://create-images-results.d-id.com/api_docs/assets/noelle.jpeg';
12804 + }
12805 + if(isset($aiomatic_Spinner_Settings['did_voice']) && $aiomatic_Spinner_Settings['did_voice'] != '')
12806 + {
12807 + $did_voice = $aiomatic_Spinner_Settings['did_voice'];
12808 + }
12809 + else
12810 + {
12811 + $did_voice = 'microsoft:en-US-JennyNeural:Cheerful';
12812 + }
12813 + $message = trim($aiomatic_Spinner_Settings['text_to_audio']);
12814 + $message = aiomatic_replaceSynergyShortcodes($message);
12815 + if(!empty($message))
12816 + {
12817 + $message = aiomatic_replaceAIPostShortcodes($message, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
12818 + $session = aiomatic_get_session_id();
12819 + $message = wp_strip_all_tags($message);
12820 + if(!empty($message))
12821 + {
12822 + $query = new Aiomatic_Query($message, 0, 'd-id', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['did_app_id']), $session, 1, '', '');
12823 + $result = aiomatic_d_id_video($did_image, $message, $did_voice);
12824 + if(is_array($result)){
12825 + if(isset($result['status']) && $result['status'] == 'success')
12826 + {
12827 + apply_filters( 'aiomatic_ai_reply_text', $query, $message );
12828 + $video_url = $result['video'];
12829 + $localfile = aiomatic_copy_video_locally($video_url, 'video_' . time(), $copy_it);
12830 + if($localfile === false)
12831 + {
12832 + aiomatic_log_to_file('Failed to save video file locally to your server.');
12833 + }
12834 + else
12835 + {
12836 + $retpath = $localfile[0];
12837 + $prep_txt = '';
12838 + if (isset($aiomatic_Spinner_Settings['prep_audio']) && $aiomatic_Spinner_Settings['prep_audio'] != '')
12839 + {
12840 + $prep_txt = $aiomatic_Spinner_Settings['prep_audio'];
12841 + }
12842 + if (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'append')
12843 + {
12844 + $final_content = $final_content . $prep_txt . ' <br/> [video src="' . $retpath . '"]';
12845 + $updated = true;
12846 + }
12847 + elseif (isset($aiomatic_Spinner_Settings['audio_location']) && $aiomatic_Spinner_Settings['audio_location'] == 'preppend')
12848 + {
12849 + $final_content = $prep_txt . '[video src="' . $retpath . '"] <br/> ' . $final_content;
12850 + $updated = true;
12851 + }
12852 + }
12853 + }
12854 + else
12855 + {
12856 + aiomatic_log_to_file('D-ID AI video failed: ' . print_r($result, true));
12857 + }
12858 + }
12859 + else
12860 + {
12861 + aiomatic_log_to_file('Failed to generate D-ID AI video output: ' . print_r($result, true));
12862 + }
12863 + }
12864 + }
12865 + }
12866 + }
12867 + }
12868 + if($updated == true)
12869 + {
12870 + $args = array();
12871 + $args['ID'] = $post->ID;
12872 + $args['post_content'] = $final_content;
12873 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
12874 + {
12875 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
12876 + }
12877 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
12878 + {
12879 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
12880 + if($date_stamp)
12881 + {
12882 + $postdate = date("Y-m-d H:i:s", $date_stamp);
12883 + $args['post_date'] = $postdate;
12884 + }
12885 + }
12886 + remove_filter('content_save_pre', 'wp_filter_post_kses');
12887 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
12888 + remove_filter('title_save_pre', 'wp_filter_kses');
12889 + update_post_meta($post->ID, $custom_name, "pub");
12890 + $post_updated = wp_update_post($args);
12891 + $changes_made = true;
12892 + add_filter('content_save_pre', 'wp_filter_post_kses');
12893 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
12894 + add_filter('title_save_pre', 'wp_filter_kses');
12895 + if (is_wp_error($post_updated)) {
12896 + $errors = $post_updated->get_error_messages();
12897 + foreach ($errors as $error) {
12898 + aiomatic_log_to_file('Error occured while updating post for AI content "' . $post->post_title . '": ' . $error);
12899 + }
12900 + }
12901 + else
12902 + {
12903 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12904 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated audio/video content.');
12905 + }
12906 + }
12907 + }
12908 + }
12909 + if (isset($aiomatic_Spinner_Settings['content_speech_text']) && $aiomatic_Spinner_Settings['content_speech_text'] != '' && $aiomatic_Spinner_Settings['content_speech_text'] != 'off')
12910 + {
12911 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
12912 + {
12913 + aiomatic_log_to_file('Starting post speech-to-text module...');
12914 + }
12915 + $updated = false;
12916 + if($aiomatic_Spinner_Settings['content_speech_text'] == 'openai')
12917 + {
12918 + if(!isset($aiomatic_Spinner_Settings['audio_to_text']) || empty($aiomatic_Spinner_Settings['audio_to_text']))
12919 + {
12920 + $audio_to_text = '%%audio_to_text%%';
12921 + }
12922 + else
12923 + {
12924 + $audio_to_text = $aiomatic_Spinner_Settings['audio_to_text'];
12925 + }
12926 + if (!isset($aiomatic_Main_Settings['app_id']))
12927 + {
12928 + $aiomatic_Main_Settings['app_id'] = '';
12929 + }
12930 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
12931 + $appids = array_filter($appids);
12932 + $token = $appids[array_rand($appids)];
12933 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
12934 + if (empty($token))
12935 + {
12936 + aiomatic_log_to_file('You need to enter an OpenAI API key for this to work!');
12937 + }
12938 + else
12939 + {
12940 + if(aiomatic_is_aiomaticapi_key($token) || (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)))
12941 + {
12942 + aiomatic_log_to_file('Only OpenAI API keys are supported at the moment.');
12943 + }
12944 + else
12945 + {
12946 + require_once (dirname(__FILE__) . "/res/openai/Url.php");
12947 + require_once (dirname(__FILE__) . "/res/openai/OpenAi.php");
12948 + $open_ai = new OpenAi($token);
12949 + if(!$open_ai){
12950 + aiomatic_log_to_file('Failed to init speech-to-text OpenAI API');
12951 + }
12952 + else
12953 + {
12954 + if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '')
12955 + {
12956 + $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
12957 + }
12958 + if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
12959 + {
12960 + $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
12961 + }
12962 + if(!isset($aiomatic_Spinner_Settings['audio_to_text_prompt']) || empty($aiomatic_Spinner_Settings['audio_to_text_prompt']))
12963 + {
12964 + $audio_to_text_prompt = '';
12965 + }
12966 + else
12967 + {
12968 + $audio_to_text_prompt = $aiomatic_Spinner_Settings['audio_to_text_prompt'];
12969 + $audio_to_text_prompt = aiomatic_replaceSynergyShortcodes($audio_to_text_prompt);
12970 + $audio_to_text_prompt = aiomatic_replaceAIPostShortcodes($audio_to_text_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
12971 + }
12972 + if(!isset($aiomatic_Spinner_Settings['speech_temperature']) || empty($aiomatic_Spinner_Settings['speech_temperature']))
12973 + {
12974 + $speech_temperature = '';
12975 + }
12976 + else
12977 + {
12978 + $speech_temperature = $aiomatic_Spinner_Settings['speech_temperature'];
12979 + }
12980 + if(!isset($aiomatic_Spinner_Settings['speech_model']) || empty($aiomatic_Spinner_Settings['speech_model']))
12981 + {
12982 + $speech_model = 'whisper-1';
12983 + }
12984 + else
12985 + {
12986 + $speech_model = $aiomatic_Spinner_Settings['speech_model'];
12987 + }
12988 + if(!isset($aiomatic_Spinner_Settings['max_speech']) || empty($aiomatic_Spinner_Settings['max_speech']))
12989 + {
12990 + $max_speech = '';
12991 + }
12992 + else
12993 + {
12994 + $max_speech = intval($aiomatic_Spinner_Settings['max_speech']);
12995 + }
12996 + $xpattern = '/https?:\/\/[^\s"]+?\.(mp3|mp4|mpeg|mpga|m4a|wav|webm)/i';
12997 + preg_match_all($xpattern, $final_content, $matches);
12998 + if(count($matches[0]) > 0)
12999 + {
13000 + $processd = 0;
13001 + foreach($matches[0] as $url)
13002 + {
13003 + if($max_speech !== '' && $processd >= $max_speech)
13004 + {
13005 + break;
13006 + }
13007 + if(!function_exists('download_url')){
13008 + include_once( ABSPATH . 'wp-admin/includes/file.php' );
13009 + }
13010 + $tmp_file = download_url($url);
13011 + if ( is_wp_error( $tmp_file ) ){
13012 + aiomatic_log_to_file('Failed to read audio file in speech-to-text: ' . $tmp_file->get_error_message());
13013 + }
13014 + else
13015 + {
13016 + $response_format = 'text';
13017 + global $wp_filesystem;
13018 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
13019 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
13020 + wp_filesystem($creds);
13021 + }
13022 + $file_name = 'speech-to-text.mp3';
13023 + $data_request = array(
13024 + 'audio' => array(
13025 + 'filename' => $file_name,
13026 + 'data' => $wp_filesystem->get_contents($tmp_file)
13027 + ),
13028 + 'model' => $speech_model,
13029 + 'temperature' => $speech_temperature,
13030 + 'response_format' => $response_format,
13031 + 'prompt' => $audio_to_text_prompt
13032 + );
13033 + if(!empty($language)){
13034 + $data_request['language'] = $language;
13035 + }
13036 + $delay = '';
13037 + if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '')
13038 + {
13039 + if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
13040 + {
13041 + $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
13042 + if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
13043 + {
13044 + $delay = rand(trim($tempo[0]), trim($tempo[1]));
13045 + }
13046 + }
13047 + else
13048 + {
13049 + if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
13050 + {
13051 + $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
13052 + }
13053 + }
13054 + }
13055 + if($delay != '' && is_numeric($delay))
13056 + {
13057 + usleep($delay);
13058 + }
13059 + $session = aiomatic_get_session_id();
13060 + $query = new Aiomatic_Query($audio_to_text_prompt, 0, 'openai-' . $speech_model, '0', '', 'speech-to-text', 'speech-to-text', $token, $session, 1, '', '');
13061 + $completion = $open_ai->transcribe($data_request);
13062 + $result = json_decode($completion);
13063 + if($result && isset($result->error))
13064 + {
13065 + aiomatic_log_to_file('Failed to transcribe audio to text using OpenAI: ' . $result->error->message);
13066 + }
13067 + else
13068 + {
13069 + apply_filters( 'aiomatic_ai_reply_text', $query, $audio_to_text );
13070 + $audio_to_text = str_replace('%%audio_to_text%%', $completion, $audio_to_text);
13071 + $audio_to_text = aiomatic_replaceSynergyShortcodes($audio_to_text);
13072 + $audio_to_text = aiomatic_replaceAIPostShortcodes($audio_to_text, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, '', '', '', '', '');
13073 + if (isset($aiomatic_Spinner_Settings['audio_text_location']) && $aiomatic_Spinner_Settings['audio_text_location'] == 'append')
13074 + {
13075 + $final_content = $final_content . ' ' . $audio_to_text;
13076 + $updated = true;
13077 + }
13078 + elseif (isset($aiomatic_Spinner_Settings['audio_text_location']) && $aiomatic_Spinner_Settings['audio_text_location'] == 'preppend')
13079 + {
13080 + $final_content = $audio_to_text . ' ' . $final_content;
13081 + $updated = true;
13082 + }
13083 + }
13084 + }
13085 + }
13086 + }
13087 + else
13088 + {
13089 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
13090 + aiomatic_log_to_file('Post ID ' . $post->ID . ' does not contain any audio files to transcribe.');
13091 + }
13092 + }
13093 + }
13094 + }
13095 + }
13096 + }
13097 + if($updated == true)
13098 + {
13099 + $args = array();
13100 + $args['ID'] = $post->ID;
13101 + $args['post_content'] = $final_content;
13102 + if (isset($aiomatic_Spinner_Settings['change_status']) && $aiomatic_Spinner_Settings['change_status'] != '' && $aiomatic_Spinner_Settings['change_status'] != 'no')
13103 + {
13104 + $args['post_status'] = $aiomatic_Spinner_Settings['change_status'];
13105 + }
13106 + if (isset($aiomatic_Spinner_Settings['change_date']) && trim($aiomatic_Spinner_Settings['change_date']) != '')
13107 + {
13108 + $date_stamp = strtotime(str_replace('%%post_date%%', $post->post_date, trim($aiomatic_Spinner_Settings['change_date'])));
13109 + if($date_stamp)
13110 + {
13111 + $postdate = date("Y-m-d H:i:s", $date_stamp);
13112 + $args['post_date'] = $postdate;
13113 + }
13114 + }
13115 + remove_filter('content_save_pre', 'wp_filter_post_kses');
13116 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
13117 + remove_filter('title_save_pre', 'wp_filter_kses');
13118 + update_post_meta($post->ID, $custom_name, "pub");
13119 + $post_updated = wp_update_post($args);
13120 + $changes_made = true;
13121 + add_filter('content_save_pre', 'wp_filter_post_kses');
13122 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
13123 + add_filter('title_save_pre', 'wp_filter_kses');
13124 + if (is_wp_error($post_updated)) {
13125 + $errors = $post_updated->get_error_messages();
13126 + foreach ($errors as $error) {
13127 + aiomatic_log_to_file('Error occured while updating post for AI content "' . $post->post_title . '": ' . $error);
13128 + }
13129 + }
13130 + else
13131 + {
13132 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
13133 + aiomatic_log_to_file('Post ID ' . $post->ID . ' "' . $post->post_title . '" was successfully updated with AI generated audio-to-text content.');
13134 + }
13135 + }
13136 + }
13137 + }
13138 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
13139 + {
13140 + aiomatic_log_to_file('Finished editing post ID: ' . $post->ID);
13141 + }
13142 + if (isset($aiomatic_Spinner_Settings['publish_new']) && $aiomatic_Spinner_Settings['publish_new'] === 'yes')
13143 + {
13144 + if($changes_made === false)
13145 + {
13146 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
13147 + {
13148 + aiomatic_log_to_file('Deleting duplicated post, as no changes were made: ' . $post->ID);
13149 + }
13150 + wp_delete_post($post->ID);
13151 + }
13152 + }
13153 + }
13154 + }
13155 + }
13156 + ?>