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

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + defined('ABSPATH') or die();
3 + use Aws\S3\S3Client;
4 + function aiomatic_write_content($prompt, $model, $temperature, $max_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, $no_internet = false, $no_embeddings = false, $source = 'content', $role = 'user', $assistant_id = '', $thread_id = '', $store_data = 'off')
5 + {
6 + $max_tokens = aimogenpro_get_max_tokens($model);
7 + if(intval($max_tokens) > AIMOGEN_DEFAULT_MAX_TOKENS && ( !stristr($model, 'gpt') || aiomatic_is_trained_model($model)))
8 + {
9 + $max_tokens = AIMOGEN_DEFAULT_MAX_TOKENS;
10 + }
11 + $new_post_content = false;
12 + $query_token_count = count(aiomatic_encode($prompt));
13 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
14 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
15 + {
16 + $string_len = aiomatic_strlen($prompt);
17 + $string_len = $string_len / 2;
18 + $string_len = intval(0 - $string_len);
19 + $prompt = aiomatic_substr($prompt, 0, $string_len);
20 + $prompt = trim($prompt);
21 + if(empty($prompt))
22 + {
23 + wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
24 + }
25 + $query_token_count = count(aiomatic_encode($prompt));
26 + $available_tokens = $max_tokens - $query_token_count;
27 + }
28 + $aierror = '';
29 + $finish_reason = '';
30 + if(!empty($global_prepend))
31 + {
32 + $prompt = $global_prepend . ' ' . $prompt;
33 + }
34 + if(!empty($global_append))
35 + {
36 + $prompt = $prompt . ' ' . $global_append;
37 + }
38 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
39 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
40 + {
41 + $api_service = aiomatic_get_api_service($token, $model);
42 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for topic ' . $source . ' text: ' . $prompt);
43 + }
44 + $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'topicContentWriter' . $source, 0, $finish_reason, $aierror, $no_internet, $no_embeddings, false, '', '', $role, $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
45 + if($generated_text === false)
46 + {
47 + aiomatic_log_to_file('Failed to generate content using AI writer: ' . $model . ' - error: ' . $aierror);
48 + }
49 + else
50 + {
51 + $new_post_content = aiomatic_sanitize_ai_result($generated_text);
52 + if(empty($new_post_content))
53 + {
54 + aiomatic_log_to_file('No content returned by the AI writer: ' . $model . ' - prompt: ' . $prompt);
55 + return false;
56 + }
57 + else
58 + {
59 + if($finish_reason == 'length')
60 + {
61 + $query_token_count = count(aiomatic_encode($new_post_content));
62 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $new_post_content, $query_token_count);
63 + $generated_text = aiomatic_generate_text($token, $model, $new_post_content, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'topicContentWriter' . $source, 0, $finish_reason, $aierror, $no_internet, $no_embeddings, false, '', '', $role, $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
64 + if($generated_text !== false)
65 + {
66 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
67 + {
68 + $new_post_content .= ' ' . ucfirst(trim(nl2br(trim($generated_text))));
69 + }
70 + else
71 + {
72 + $new_post_content .= ' ' . ucfirst(trim($generated_text));
73 + }
74 + }
75 + }
76 + }
77 + }
78 + return $new_post_content;
79 + }
80 + function aiomatic_replacetopics($prompt, $title, $language, $writing_style, $writing_tone, $topics, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list = '', $img_attr = '')
81 + {
82 + $blog_title = html_entity_decode(get_bloginfo('title'));
83 + if(empty($topics))
84 + {
85 + $topics = $title;
86 + }
87 + $img_attr = str_replace('%%image_source_name%%', '', $img_attr);
88 + $img_attr = str_replace('%%image_source_url%%', '', $img_attr);
89 + $img_attr = str_replace('%%image_source_website%%', '', $img_attr);
90 + $prompt = str_replace('%%royalty_free_image_attribution%%', $img_attr, $prompt);
91 + $prompt = str_replace('%%topic%%', $topics, $prompt);
92 + $prompt = str_replace('%%title%%', $title, $prompt);
93 + $prompt = str_replace('%%post_title%%', $title, $prompt);
94 + $prompt = str_replace('%%post_title_keywords%%', $post_title_keywords, $prompt);
95 + $prompt = str_replace('%%post_original_title%%', $title, $prompt);
96 + $prompt = str_replace('%%blog_title%%', $blog_title, $prompt);
97 + $prompt = str_replace('%%language%%', $language, $prompt);
98 + $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
99 + $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
100 + $prompt = str_replace('%%sections%%', $sections, $prompt);
101 + $prompt = str_replace('%%current_section%%', $current_section, $prompt);
102 + $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
103 + $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
104 + $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
105 + $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
106 + $prompt = aiomatic_replaceSynergyShortcodes($prompt);
107 + $prompt = str_replace('%%default_post_cats%%', $default_cat_list, $prompt);
108 + $prompt = apply_filters('aiomatic_replace_aicontent_shortcode', $prompt);
109 + preg_match_all('#%%related_questions_([^%]*?)%%#i', $prompt, $mxatches);
110 + if(isset($mxatches[1][0]))
111 + {
112 + foreach($mxatches[1] as $googlematch)
113 + {
114 + $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
115 + if(is_array($mtchres) && !empty($mtchres))
116 + {
117 + $quests = array();
118 + foreach($mtchres as $mra)
119 + {
120 + if(isset($mra['q']))
121 + {
122 + $quests[] = $mra['q'];
123 + }
124 + }
125 + $mtchres = implode(',', $quests);
126 + }
127 + $prompt = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $prompt);
128 + }
129 + }
130 + if($custom_shortcodes != '')
131 + {
132 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
133 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
134 + {
135 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for the custom shortcode creator to work!');
136 + }
137 + else
138 + {
139 + $allmodels = aiomatic_get_all_models();
140 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
141 + $appids = array_filter($appids);
142 + $token = $appids[array_rand($appids)];
143 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
144 + if($custom_shortcodes != '' && !is_array($custom_shortcodes))
145 + {
146 + $custom_shortcodes = aiomatic_replacetopics($custom_shortcodes, $title, $language, $writing_style, $writing_tone, $topics, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, '', $global_prepend, $global_append, $default_cat_list, $img_attr);
147 + }
148 + $custom_shortcodes = str_replace('%%royalty_free_image_attribution%%', $img_attr, $custom_shortcodes);
149 + $custom_shortcodes = str_replace('%%topic%%', $topics, $custom_shortcodes);
150 + $custom_shortcodes = str_replace('%%title%%', $title, $custom_shortcodes);
151 + $custom_shortcodes = str_replace('%%post_title%%', $title, $custom_shortcodes);
152 + $custom_shortcodes = str_replace('%%post_title_keywords%%', $post_title_keywords, $custom_shortcodes);
153 + $custom_shortcodes = str_replace('%%post_original_title%%', $title, $custom_shortcodes);
154 + $custom_shortcodes = str_replace('%%blog_title%%', $blog_title, $custom_shortcodes);
155 + $custom_shortcodes = str_replace('%%language%%', $language, $custom_shortcodes);
156 + $custom_shortcodes = str_replace('%%writing_style%%', $writing_style, $custom_shortcodes);
157 + $custom_shortcodes = str_replace('%%writing_tone%%', $writing_tone, $custom_shortcodes);
158 + $custom_shortcodes = str_replace('%%sections%%', $sections, $custom_shortcodes);
159 + $custom_shortcodes = str_replace('%%current_section%%', $current_section, $custom_shortcodes);
160 + $custom_shortcodes = str_replace('%%sections_count%%', $sections_count, $custom_shortcodes);
161 + $custom_shortcodes = str_replace('%%paragraphs_per_section%%', $paragraph_count, $custom_shortcodes);
162 + $custom_shortcodes = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $custom_shortcodes);
163 + $custom_shortcodes = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $custom_shortcodes);
164 + $custom_shortcodes = aiomatic_replaceSynergyShortcodes($custom_shortcodes);
165 + $custom_shortcodes = str_replace('%%default_post_cats%%', $default_cat_list, $custom_shortcodes);
166 +
167 + $custom_shortcodes = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
168 + foreach($custom_shortcodes as $my_short)
169 + {
170 + $name_part = explode('=>', $my_short);
171 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
172 + {
173 + $shortname = trim($name_part[0]);
174 + if(strstr($prompt, '%%' . $shortname . '%%'))
175 + {
176 + $shortval = '';
177 + $ai_part = explode('@@', $name_part[1]);
178 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
179 + {
180 + if(!in_array(trim($ai_part[0]), $allmodels))
181 + {
182 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
183 + }
184 + else
185 + {
186 + $aimodel = trim($ai_part[0]);
187 + }
188 + $ai_command = trim($ai_part[1]);
189 +
190 + $ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $ai_command);
191 + foreach($custom_shortcodes as $my_check_short)
192 + {
193 + $check_name_part = explode('=>', $my_check_short);
194 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
195 + {
196 + $check_shortname = trim($check_name_part[0]);
197 + if($shortname !== $check_shortname && strstr($ai_command, '%%' . $check_shortname . '%%') !== false)
198 + {
199 + $check_shortval = '';
200 + $check_ai_part = explode('@@', $check_name_part[1]);
201 + if(!in_array(trim($check_ai_part[0]), $allmodels))
202 + {
203 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
204 + }
205 + else
206 + {
207 + $check_aimodel = trim($check_ai_part[0]);
208 + }
209 + $check_ai_command = trim($check_ai_part[1]);
210 + $aierror = '';
211 +
212 + $check_ai_command = str_replace('%%royalty_free_image_attribution%%', $img_attr, $check_ai_command);
213 + $check_ai_command = str_replace('%%topic%%', $topics, $check_ai_command);
214 + $check_ai_command = str_replace('%%title%%', $title, $check_ai_command);
215 + $check_ai_command = str_replace('%%post_title%%', $title, $check_ai_command);
216 + $check_ai_command = str_replace('%%post_title_keywords%%', $post_title_keywords, $check_ai_command);
217 + $check_ai_command = str_replace('%%post_original_title%%', $title, $check_ai_command);
218 + $check_ai_command = str_replace('%%blog_title%%', $blog_title, $check_ai_command);
219 + $check_ai_command = str_replace('%%language%%', $language, $check_ai_command);
220 + $check_ai_command = str_replace('%%writing_style%%', $writing_style, $check_ai_command);
221 + $check_ai_command = str_replace('%%writing_tone%%', $writing_tone, $check_ai_command);
222 + $check_ai_command = str_replace('%%sections%%', $sections, $check_ai_command);
223 + $check_ai_command = str_replace('%%current_section%%', $current_section, $check_ai_command);
224 + $check_ai_command = str_replace('%%sections_count%%', $sections_count, $check_ai_command);
225 + $check_ai_command = str_replace('%%paragraphs_per_section%%', $paragraph_count, $check_ai_command);
226 + $check_ai_command = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $check_ai_command);
227 + $check_ai_command = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $check_ai_command);
228 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
229 + $check_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $check_ai_command);
230 +
231 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
232 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
233 + if($check_generated_text === false)
234 + {
235 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
236 + }
237 + else
238 + {
239 + $check_shortval = trim(trim($check_generated_text), '.');
240 + }
241 + $ai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $ai_command);
242 + }
243 + }
244 + }
245 + $aierror = '';
246 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
247 + if($generated_text === false)
248 + {
249 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
250 + }
251 + else
252 + {
253 + $shortval = trim(trim($generated_text), '.');
254 + }
255 + }
256 + $prompt = str_replace('%%' . $shortname . '%%', $shortval, $prompt);
257 + }
258 + }
259 + }
260 + }
261 + }
262 + return $prompt;
263 + }
264 + function aiomatic_generate_custom_shortcode_content($token, $aimodel, $ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, &$aierror)
265 + {
266 + $max_tokens = aimogenpro_get_max_tokens($aimodel);
267 + $query_token_count = count(aiomatic_encode($ai_command));
268 + $available_tokens = aiomatic_compute_available_tokens($aimodel, $max_tokens, $ai_command, $query_token_count);
269 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
270 + {
271 + $string_len = aiomatic_strlen($ai_command);
272 + $string_len = $string_len / 2;
273 + $string_len = intval(0 - $string_len);
274 + $ai_command = aiomatic_substr($ai_command, 0, $string_len);
275 + $ai_command = trim($ai_command);
276 + $query_token_count = count(aiomatic_encode($ai_command));
277 + $available_tokens = $max_tokens - $query_token_count;
278 + }
279 + $thread_id = '';
280 + $finish_reason = '';
281 + $temperature = 1;
282 + $top_p = 1;
283 + $presence_penalty = 0;
284 + $frequency_penalty = 0;
285 + if(!empty($global_prepend))
286 + {
287 + $ai_command = $global_prepend . ' ' . $ai_command;
288 + }
289 + if(!empty($global_append))
290 + {
291 + $ai_command = $ai_command . ' ' . $global_append;
292 + }
293 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
294 + {
295 + $api_service = aiomatic_get_api_service($token, $aimodel);
296 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $aimodel . ')(' . $available_tokens . ') for custom shortcode content: ' . $ai_command);
297 + }
298 + $generated_text = aiomatic_generate_text($token, $aimodel, $ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'customShortcode', 0, $finish_reason, $aierror, true, false, false, '', '', 'user', '', $thread_id, '', 'disabled', '', true, false, array(), '');
299 + return $generated_text;
300 + }
301 + function aiomatic_replace_youtube($prompt, $video_title, $post_desc, $vid_url, $returned_caption, $language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list = '')
302 + {
303 + $blog_title = html_entity_decode(get_bloginfo('title'));
304 + $prompt = str_replace('%%video_title%%', $video_title, $prompt);
305 + $prompt = str_replace('%%video_descripton%%', $post_desc, $prompt);
306 + $prompt = str_replace('%%video_url%%', $vid_url, $prompt);
307 + $regex = '/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/';
308 + if (preg_match($regex, $vid_url, $matches))
309 + {
310 + $prompt = str_replace('%%video_id%%', $matches[1], $prompt);
311 + }
312 + else
313 + {
314 + $prompt = str_replace('%%video_id%%', '', $prompt);
315 + }
316 + $prompt = str_replace('%%video_captions%%', $returned_caption, $prompt);
317 + $prompt = str_replace('%%post_title_keywords%%', $post_title_keywords, $prompt);
318 + $prompt = str_replace('%%blog_title%%', $blog_title, $prompt);
319 + $prompt = str_replace('%%language%%', $language, $prompt);
320 + $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
321 + $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
322 + $prompt = str_replace('%%sections%%', $sections, $prompt);
323 + $prompt = str_replace('%%current_section%%', $current_section, $prompt);
324 + $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
325 + $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
326 + $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
327 + $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
328 + $prompt = aiomatic_replaceSynergyShortcodes($prompt);
329 + if($custom_shortcodes != '')
330 + {
331 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
332 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
333 + {
334 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for the custom shortcode creator to work!');
335 + }
336 + else
337 + {
338 + $allmodels = aiomatic_get_all_models();
339 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
340 + $appids = array_filter($appids);
341 + $token = $appids[array_rand($appids)];
342 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
343 + if($custom_shortcodes != '' && !is_array($custom_shortcodes))
344 + {
345 + $custom_shortcodes = aiomatic_replace_youtube($custom_shortcodes, $video_title, $post_desc, $vid_url, $returned_caption, $language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, '', $global_prepend, $global_append, $default_cat_list);
346 + }
347 + $custom_shortcodes = str_replace('%%video_title%%', $video_title, $custom_shortcodes);
348 + $custom_shortcodes = str_replace('%%video_descripton%%', $post_desc, $custom_shortcodes);
349 + $custom_shortcodes = str_replace('%%video_url%%', $vid_url, $custom_shortcodes);
350 + $regex = '/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/';
351 + if (preg_match($regex, $vid_url, $matches))
352 + {
353 + $custom_shortcodes = str_replace('%%video_id%%', $matches[1], $custom_shortcodes);
354 + }
355 + else
356 + {
357 + $custom_shortcodes = str_replace('%%video_id%%', '', $custom_shortcodes);
358 + }
359 + $custom_shortcodes = str_replace('%%video_captions%%', $returned_caption, $custom_shortcodes);
360 + $custom_shortcodes = str_replace('%%post_title_keywords%%', $post_title_keywords, $custom_shortcodes);
361 + $custom_shortcodes = str_replace('%%blog_title%%', $blog_title, $custom_shortcodes);
362 + $custom_shortcodes = str_replace('%%language%%', $language, $custom_shortcodes);
363 + $custom_shortcodes = str_replace('%%writing_style%%', $writing_style, $custom_shortcodes);
364 + $custom_shortcodes = str_replace('%%writing_tone%%', $writing_tone, $custom_shortcodes);
365 + $custom_shortcodes = str_replace('%%sections%%', $sections, $custom_shortcodes);
366 + $custom_shortcodes = str_replace('%%current_section%%', $current_section, $custom_shortcodes);
367 + $custom_shortcodes = str_replace('%%sections_count%%', $sections_count, $custom_shortcodes);
368 + $custom_shortcodes = str_replace('%%paragraphs_per_section%%', $paragraph_count, $custom_shortcodes);
369 + $custom_shortcodes = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $custom_shortcodes);
370 + $custom_shortcodes = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $custom_shortcodes);
371 + $custom_shortcodes = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
372 + foreach($custom_shortcodes as $my_short)
373 + {
374 + $name_part = explode('=>', $my_short);
375 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
376 + {
377 + $shortname = trim($name_part[0]);
378 + if(strstr($prompt, '%%' . $shortname . '%%'))
379 + {
380 + $shortval = '';
381 + $ai_part = explode('@@', $name_part[1]);
382 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
383 + {
384 + if(!in_array(trim($ai_part[0]), $allmodels))
385 + {
386 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
387 + }
388 + else
389 + {
390 + $aimodel = trim($ai_part[0]);
391 + }
392 + $ai_command = trim($ai_part[1]);
393 + $ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $ai_command);
394 + foreach($custom_shortcodes as $my_check_short)
395 + {
396 + $check_name_part = explode('=>', $my_check_short);
397 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
398 + {
399 + $check_shortname = trim($check_name_part[0]);
400 + if($shortname !== $check_shortname && strstr($ai_command, '%%' . $check_shortname . '%%') !== false)
401 + {
402 + $check_shortval = '';
403 + $check_ai_part = explode('@@', $check_name_part[1]);
404 + if(!in_array(trim($check_ai_part[0]), $allmodels))
405 + {
406 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
407 + }
408 + else
409 + {
410 + $check_aimodel = trim($check_ai_part[0]);
411 + }
412 + $check_ai_command = trim($check_ai_part[1]);
413 + $aierror = '';
414 +
415 + $check_ai_command = str_replace('%%video_title%%', $video_title, $check_ai_command);
416 + $check_ai_command = str_replace('%%video_descripton%%', $post_desc, $check_ai_command);
417 + $check_ai_command = str_replace('%%video_url%%', $vid_url, $check_ai_command);
418 + $regex = '/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/';
419 + if (preg_match($regex, $vid_url, $matches))
420 + {
421 + $check_ai_command = str_replace('%%video_id%%', $matches[1], $check_ai_command);
422 + }
423 + else
424 + {
425 + $check_ai_command = str_replace('%%video_id%%', '', $check_ai_command);
426 + }
427 + $check_ai_command = str_replace('%%video_captions%%', $returned_caption, $check_ai_command);
428 + $check_ai_command = str_replace('%%post_title_keywords%%', $post_title_keywords, $check_ai_command);
429 + $check_ai_command = str_replace('%%blog_title%%', $blog_title, $check_ai_command);
430 + $check_ai_command = str_replace('%%language%%', $language, $check_ai_command);
431 + $check_ai_command = str_replace('%%writing_style%%', $writing_style, $check_ai_command);
432 + $check_ai_command = str_replace('%%writing_tone%%', $writing_tone, $check_ai_command);
433 + $check_ai_command = str_replace('%%sections%%', $sections, $check_ai_command);
434 + $check_ai_command = str_replace('%%current_section%%', $current_section, $check_ai_command);
435 + $check_ai_command = str_replace('%%sections_count%%', $sections_count, $check_ai_command);
436 + $check_ai_command = str_replace('%%paragraphs_per_section%%', $paragraph_count, $check_ai_command);
437 + $check_ai_command = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $check_ai_command);
438 + $check_ai_command = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $check_ai_command);
439 +
440 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
441 +
442 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
443 + if($check_generated_text === false)
444 + {
445 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
446 + }
447 + else
448 + {
449 + $check_shortval = trim(trim($check_generated_text), '.');
450 + }
451 + $ai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $ai_command);
452 + }
453 + }
454 + }
455 + $aierror = '';
456 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
457 + if($generated_text === false)
458 + {
459 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
460 + }
461 + else
462 + {
463 + $shortval = trim(trim($generated_text), '.');
464 + }
465 + }
466 + $prompt = str_replace('%%' . $shortname . '%%', $shortval, $prompt);
467 + }
468 + }
469 + }
470 + }
471 + }
472 + $prompt = str_replace('%%default_post_cats%%', $default_cat_list, $prompt);
473 + $prompt = apply_filters('aiomatic_replace_aicontent_shortcode', $prompt);
474 + preg_match_all('#%%related_questions_([^%]*?)%%#i', $prompt, $mxatches);
475 + if(isset($mxatches[1][0]))
476 + {
477 + foreach($mxatches[1] as $googlematch)
478 + {
479 + $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
480 + if(is_array($mtchres) && !empty($mtchres))
481 + {
482 + $quests = array();
483 + foreach($mtchres as $mra)
484 + {
485 + if(isset($mra['q']))
486 + {
487 + $quests[] = $mra['q'];
488 + }
489 + }
490 + $mtchres = implode(',', $quests);
491 + }
492 + $prompt = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $prompt);
493 + }
494 + }
495 + return $prompt;
496 + }
497 +
498 + function aiomatic_replace_amazon($prompt, $post_title, $search_keywords, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list = '', $item_score = '', $edition = '', $language = '', $pages_count = '', $publication_date = '', $contributors = '', $manufacturer = '', $binding = '', $product_group = '', $rating = '', $eans = '', $part_no = '', $model = '', $warranty = '', $color = '', $is_adult = 'false', $dimensions = '', $date = '', $size = '', $unit_count = '', $amazresult = array())
499 + {
500 + $blog_title = html_entity_decode(get_bloginfo('title'));
501 +
502 + $product_info = '';
503 + foreach($amazresult as $amr)
504 + {
505 + if(empty($product_info))
506 + {
507 + $product_info .= '\n\n--------------\n\n';
508 + }
509 + $product_info .= 'Title: ' . $amr->offer_title . '\n\n';
510 + $product_info .= 'Desciption: ' . $amr->offer_desc . '\n\n';
511 + $product_info .= 'Price: ' . $amr->offer_price . '\n\n';
512 + $product_info .= 'Image: ' . $amr->offer_img . '\n\n';
513 + $product_info .= 'URL: ' . $amr->offer_url . '\n\n';
514 + $product_info .= '--------------\n\n';
515 + }
516 + if($color === null)
517 + {
518 + $color = '';
519 + }
520 + $prompt = str_replace('%%search_keywords%%', $search_keywords, $prompt);
521 + $prompt = str_replace('%%post_title%%', $post_title, $prompt);
522 + $prompt = str_replace('%%product_title%%', $product_title, $prompt);
523 + $prompt = str_replace('%%product_description%%', $product_description, $prompt);
524 + $prompt = str_replace('%%aff_url%%', $aff_url, $prompt);
525 + $prompt = str_replace('%%product_author%%', $product_author, $prompt);
526 + $prompt = str_replace('%%offer_price%%', $offer_price, $prompt);
527 + $prompt = str_replace('%%product_price%%', $product_price, $prompt);
528 + $prompt = str_replace('%%product_list_price%%', $product_list_price, $prompt);
529 + $prompt = str_replace('%%price_numeric%%', $price_numeric, $prompt);
530 + $prompt = str_replace('%%price_currency%%', $price_currency, $prompt);
531 + $prompt = str_replace('%%product_brand%%', $product_brand, $prompt);
532 + $prompt = str_replace('%%product_reviews%%', $product_reviews, $prompt);
533 + $prompt = str_replace('%%product_isbn%%', $product_isbn, $prompt);
534 + $prompt = str_replace('%%product_upc%%', $product_upc, $prompt);
535 + $prompt = str_replace('%%product_asin%%', $product_asin, $prompt);
536 + $prompt = str_replace('%%cart_url%%', $cart_url, $prompt);
537 + $prompt = str_replace('%%list_price_numeric%%', $list_price_numeric, $prompt);
538 + $prompt = str_replace('%%product_imgs%%', $product_imgs, $prompt);
539 + $prompt = str_replace('%%product_imgs%%', $product_imgs, $prompt);
540 + //new
541 + $prompt = str_replace('%%product_score%%', $item_score, $prompt);
542 + $prompt = str_replace('%%product_edition%%', $edition, $prompt);
543 + $prompt = str_replace('%%product_language%%', $language, $prompt);
544 + $prompt = str_replace('%%product_pages_count%%', $pages_count, $prompt);
545 + $prompt = str_replace('%%product_publication_date%%', $publication_date, $prompt);
546 + $prompt = str_replace('%%product_contributors%%', $contributors, $prompt);
547 + $prompt = str_replace('%%product_manufacturer%%', $manufacturer, $prompt);
548 + $prompt = str_replace('%%product_binding%%', $binding, $prompt);
549 + $prompt = str_replace('%%product_product_group%%', $product_group, $prompt);
550 + $prompt = str_replace('%%product_rating%%', $rating, $prompt);
551 + $prompt = str_replace('%%product_ean%%', $eans, $prompt);
552 + $prompt = str_replace('%%product_part_no%%', $part_no, $prompt);
553 + $prompt = str_replace('%%product_model%%', $model, $prompt);
554 + $prompt = str_replace('%%product_warranty%%', $warranty, $prompt);
555 + $prompt = str_replace('%%product_color%%', $color, $prompt);
556 + $prompt = str_replace('%%product_is_adult%%', $is_adult, $prompt);
557 + $prompt = str_replace('%%product_dimensions%%', $dimensions, $prompt);
558 + $prompt = str_replace('%%product_date%%', $date, $prompt);
559 + $prompt = str_replace('%%product_size%%', $size, $prompt);
560 + $prompt = str_replace('%%product_unit_count%%', $unit_count, $prompt);
561 +
562 + $prompt = str_replace('%%all_product_info%%', $product_info, $prompt);
563 + $prompt = str_replace('%%all_product_titles%%', $product_titles, $prompt);
564 + $prompt = str_replace('%%post_title_keywords%%', $post_title_keywords, $prompt);
565 + $prompt = str_replace('%%blog_title%%', $blog_title, $prompt);
566 + $prompt = str_replace('%%language%%', $content_language, $prompt);
567 + $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
568 + $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
569 + $prompt = str_replace('%%sections%%', $sections, $prompt);
570 + $prompt = str_replace('%%current_section%%', $current_section, $prompt);
571 + $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
572 + $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
573 + $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
574 + $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
575 + $prompt = aiomatic_replaceSynergyShortcodes($prompt);
576 + if($custom_shortcodes != '')
577 + {
578 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
579 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
580 + {
581 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for the custom shortcode creator to work!');
582 + }
583 + else
584 + {
585 + $allmodels = aiomatic_get_all_models();
586 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
587 + $appids = array_filter($appids);
588 + $token = $appids[array_rand($appids)];
589 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
590 + if($custom_shortcodes != '' && !is_array($custom_shortcodes))
591 + {
592 + $custom_shortcodes = aiomatic_replace_amazon($custom_shortcodes, $post_title, $search_keywords, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, '', $global_prepend, $global_append, $product_reviews, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $default_cat_list);
593 + }
594 +
595 + $custom_shortcodes = str_replace('%%search_keywords%%', $search_keywords, $custom_shortcodes);
596 + $custom_shortcodes = str_replace('%%post_title%%', $post_title, $custom_shortcodes);
597 + $custom_shortcodes = str_replace('%%product_title%%', $product_title, $custom_shortcodes);
598 + $custom_shortcodes = str_replace('%%product_description%%', $product_description, $custom_shortcodes);
599 + $custom_shortcodes = str_replace('%%aff_url%%', $aff_url, $custom_shortcodes);
600 + $custom_shortcodes = str_replace('%%product_author%%', $product_author, $custom_shortcodes);
601 + $custom_shortcodes = str_replace('%%offer_price%%', $offer_price, $custom_shortcodes);
602 + $custom_shortcodes = str_replace('%%product_price%%', $product_price, $custom_shortcodes);
603 + $custom_shortcodes = str_replace('%%product_list_price%%', $product_list_price, $custom_shortcodes);
604 + $custom_shortcodes = str_replace('%%price_numeric%%', $price_numeric, $custom_shortcodes);
605 + $custom_shortcodes = str_replace('%%price_currency%%', $price_currency, $custom_shortcodes);
606 + $custom_shortcodes = str_replace('%%product_brand%%', $product_brand, $custom_shortcodes);
607 + $custom_shortcodes = str_replace('%%product_reviews%%', $product_reviews, $custom_shortcodes);
608 + $custom_shortcodes = str_replace('%%product_isbn%%', $product_isbn, $custom_shortcodes);
609 + $custom_shortcodes = str_replace('%%product_upc%%', $product_upc, $custom_shortcodes);
610 + $custom_shortcodes = str_replace('%%product_asin%%', $product_asin, $custom_shortcodes);
611 + $custom_shortcodes = str_replace('%%cart_url%%', $cart_url, $custom_shortcodes);
612 + $custom_shortcodes = str_replace('%%list_price_numeric%%', $list_price_numeric, $custom_shortcodes);
613 + $custom_shortcodes = str_replace('%%product_imgs%%', $product_imgs, $custom_shortcodes);
614 + $custom_shortcodes = str_replace('%%product_imgs%%', $product_imgs, $custom_shortcodes);
615 + //new
616 + $custom_shortcodes = str_replace('%%product_score%%', $item_score, $custom_shortcodes);
617 + $custom_shortcodes = str_replace('%%product_edition%%', $edition, $custom_shortcodes);
618 + $custom_shortcodes = str_replace('%%product_language%%', $language, $custom_shortcodes);
619 + $custom_shortcodes = str_replace('%%product_pages_count%%', $pages_count, $custom_shortcodes);
620 + $custom_shortcodes = str_replace('%%product_publication_date%%', $publication_date, $custom_shortcodes);
621 + $custom_shortcodes = str_replace('%%product_contributors%%', $contributors, $custom_shortcodes);
622 + $custom_shortcodes = str_replace('%%product_manufacturer%%', $manufacturer, $custom_shortcodes);
623 + $custom_shortcodes = str_replace('%%product_binding%%', $binding, $custom_shortcodes);
624 + $custom_shortcodes = str_replace('%%product_product_group%%', $product_group, $custom_shortcodes);
625 + $custom_shortcodes = str_replace('%%product_rating%%', $rating, $custom_shortcodes);
626 + $custom_shortcodes = str_replace('%%product_ean%%', $eans, $custom_shortcodes);
627 + $custom_shortcodes = str_replace('%%product_part_no%%', $part_no, $custom_shortcodes);
628 + $custom_shortcodes = str_replace('%%product_model%%', $model, $custom_shortcodes);
629 + $custom_shortcodes = str_replace('%%product_warranty%%', $warranty, $custom_shortcodes);
630 + $custom_shortcodes = str_replace('%%product_color%%', $color, $custom_shortcodes);
631 + $custom_shortcodes = str_replace('%%product_is_adult%%', $is_adult, $custom_shortcodes);
632 + $custom_shortcodes = str_replace('%%product_dimensions%%', $dimensions, $custom_shortcodes);
633 + $custom_shortcodes = str_replace('%%product_date%%', $date, $custom_shortcodes);
634 + $custom_shortcodes = str_replace('%%product_size%%', $size, $custom_shortcodes);
635 + $custom_shortcodes = str_replace('%%product_unit_count%%', $unit_count, $custom_shortcodes);
636 +
637 + $custom_shortcodes = str_replace('%%all_product_info%%', $product_info, $custom_shortcodes);
638 + $custom_shortcodes = str_replace('%%all_product_titles%%', $product_titles, $custom_shortcodes);
639 + $custom_shortcodes = str_replace('%%post_title_keywords%%', $post_title_keywords, $custom_shortcodes);
640 + $custom_shortcodes = str_replace('%%blog_title%%', $blog_title, $custom_shortcodes);
641 + $custom_shortcodes = str_replace('%%language%%', $content_language, $custom_shortcodes);
642 + $custom_shortcodes = str_replace('%%writing_style%%', $writing_style, $custom_shortcodes);
643 + $custom_shortcodes = str_replace('%%writing_tone%%', $writing_tone, $custom_shortcodes);
644 + $custom_shortcodes = str_replace('%%sections%%', $sections, $custom_shortcodes);
645 + $custom_shortcodes = str_replace('%%current_section%%', $current_section, $custom_shortcodes);
646 + $custom_shortcodes = str_replace('%%sections_count%%', $sections_count, $custom_shortcodes);
647 + $custom_shortcodes = str_replace('%%paragraphs_per_section%%', $paragraph_count, $custom_shortcodes);
648 + $custom_shortcodes = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $custom_shortcodes);
649 + $custom_shortcodes = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $custom_shortcodes);
650 + $custom_shortcodes = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
651 + foreach($custom_shortcodes as $my_short)
652 + {
653 + $name_part = explode('=>', $my_short);
654 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
655 + {
656 + $shortname = trim($name_part[0]);
657 + if(strstr($prompt, '%%' . $shortname . '%%'))
658 + {
659 + $shortval = '';
660 + $ai_part = explode('@@', $name_part[1]);
661 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
662 + {
663 + if(!in_array(trim($ai_part[0]), $allmodels))
664 + {
665 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
666 + }
667 + else
668 + {
669 + $aimodel = trim($ai_part[0]);
670 + }
671 + $ai_command = trim($ai_part[1]);
672 + $ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $ai_command);
673 + foreach($custom_shortcodes as $my_check_short)
674 + {
675 + $check_name_part = explode('=>', $my_check_short);
676 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
677 + {
678 + $check_shortname = trim($check_name_part[0]);
679 + if($shortname !== $check_shortname && strstr($ai_command, '%%' . $check_shortname . '%%') !== false)
680 + {
681 + $check_shortval = '';
682 + $check_ai_part = explode('@@', $check_name_part[1]);
683 + if(!in_array(trim($check_ai_part[0]), $allmodels))
684 + {
685 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
686 + }
687 + else
688 + {
689 + $check_aimodel = trim($check_ai_part[0]);
690 + }
691 + $check_ai_command = trim($check_ai_part[1]);
692 + $aierror = '';
693 +
694 + $check_ai_command = str_replace('%%search_keywords%%', $search_keywords, $check_ai_command);
695 + $check_ai_command = str_replace('%%post_title%%', $post_title, $check_ai_command);
696 + $check_ai_command = str_replace('%%product_title%%', $product_title, $check_ai_command);
697 + $check_ai_command = str_replace('%%product_description%%', $product_description, $check_ai_command);
698 + $check_ai_command = str_replace('%%aff_url%%', $aff_url, $check_ai_command);
699 + $check_ai_command = str_replace('%%product_author%%', $product_author, $check_ai_command);
700 + $check_ai_command = str_replace('%%offer_price%%', $offer_price, $check_ai_command);
701 + $check_ai_command = str_replace('%%product_price%%', $product_price, $check_ai_command);
702 + $check_ai_command = str_replace('%%product_list_price%%', $product_list_price, $check_ai_command);
703 + $check_ai_command = str_replace('%%price_numeric%%', $price_numeric, $check_ai_command);
704 + $check_ai_command = str_replace('%%price_currency%%', $price_currency, $check_ai_command);
705 + $check_ai_command = str_replace('%%product_brand%%', $product_brand, $check_ai_command);
706 + $check_ai_command = str_replace('%%product_reviews%%', $product_reviews, $check_ai_command);
707 + $check_ai_command = str_replace('%%product_isbn%%', $product_isbn, $check_ai_command);
708 + $check_ai_command = str_replace('%%product_upc%%', $product_upc, $check_ai_command);
709 + $check_ai_command = str_replace('%%product_asin%%', $product_asin, $check_ai_command);
710 + $check_ai_command = str_replace('%%cart_url%%', $cart_url, $check_ai_command);
711 + $check_ai_command = str_replace('%%list_price_numeric%%', $list_price_numeric, $check_ai_command);
712 + $check_ai_command = str_replace('%%product_imgs%%', $product_imgs, $check_ai_command);
713 + $check_ai_command = str_replace('%%product_imgs%%', $product_imgs, $check_ai_command);
714 + //new
715 + $check_ai_command = str_replace('%%product_score%%', $item_score, $check_ai_command);
716 + $check_ai_command = str_replace('%%product_edition%%', $edition, $check_ai_command);
717 + $check_ai_command = str_replace('%%product_language%%', $language, $check_ai_command);
718 + $check_ai_command = str_replace('%%product_pages_count%%', $pages_count, $check_ai_command);
719 + $check_ai_command = str_replace('%%product_publication_date%%', $publication_date, $check_ai_command);
720 + $check_ai_command = str_replace('%%product_contributors%%', $contributors, $check_ai_command);
721 + $check_ai_command = str_replace('%%product_manufacturer%%', $manufacturer, $check_ai_command);
722 + $check_ai_command = str_replace('%%product_binding%%', $binding, $check_ai_command);
723 + $check_ai_command = str_replace('%%product_product_group%%', $product_group, $check_ai_command);
724 + $check_ai_command = str_replace('%%product_rating%%', $rating, $check_ai_command);
725 + $check_ai_command = str_replace('%%product_ean%%', $eans, $check_ai_command);
726 + $check_ai_command = str_replace('%%product_part_no%%', $part_no, $check_ai_command);
727 + $check_ai_command = str_replace('%%product_model%%', $model, $check_ai_command);
728 + $check_ai_command = str_replace('%%product_warranty%%', $warranty, $check_ai_command);
729 + $check_ai_command = str_replace('%%product_color%%', $color, $check_ai_command);
730 + $check_ai_command = str_replace('%%product_is_adult%%', $is_adult, $check_ai_command);
731 + $check_ai_command = str_replace('%%product_dimensions%%', $dimensions, $check_ai_command);
732 + $check_ai_command = str_replace('%%product_date%%', $date, $check_ai_command);
733 + $check_ai_command = str_replace('%%product_size%%', $size, $check_ai_command);
734 + $check_ai_command = str_replace('%%product_unit_count%%', $unit_count, $check_ai_command);
735 +
736 + $check_ai_command = str_replace('%%all_product_info%%', $product_info, $check_ai_command);
737 + $check_ai_command = str_replace('%%all_product_titles%%', $product_titles, $check_ai_command);
738 + $check_ai_command = str_replace('%%post_title_keywords%%', $post_title_keywords, $check_ai_command);
739 + $check_ai_command = str_replace('%%blog_title%%', $blog_title, $check_ai_command);
740 + $check_ai_command = str_replace('%%language%%', $content_language, $check_ai_command);
741 + $check_ai_command = str_replace('%%writing_style%%', $writing_style, $check_ai_command);
742 + $check_ai_command = str_replace('%%writing_tone%%', $writing_tone, $check_ai_command);
743 + $check_ai_command = str_replace('%%sections%%', $sections, $check_ai_command);
744 + $check_ai_command = str_replace('%%current_section%%', $current_section, $check_ai_command);
745 + $check_ai_command = str_replace('%%sections_count%%', $sections_count, $check_ai_command);
746 + $check_ai_command = str_replace('%%paragraphs_per_section%%', $paragraph_count, $check_ai_command);
747 + $check_ai_command = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $check_ai_command);
748 + $check_ai_command = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $check_ai_command);
749 +
750 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
751 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
752 + if($check_generated_text === false)
753 + {
754 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
755 + }
756 + else
757 + {
758 + $check_shortval = trim(trim($check_generated_text), '.');
759 + }
760 + $ai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $ai_command);
761 + }
762 + }
763 + }
764 + $aierror = '';
765 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
766 + if($generated_text === false)
767 + {
768 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
769 + }
770 + else
771 + {
772 + $shortval = trim(trim($generated_text), '.');
773 + }
774 + }
775 + $prompt = str_replace('%%' . $shortname . '%%', $shortval, $prompt);
776 + }
777 + }
778 + }
779 + }
780 + }
781 + $prompt = str_replace('%%default_post_cats%%', $default_cat_list, $prompt);
782 + $prompt = apply_filters('aiomatic_replace_aicontent_shortcode', $prompt);
783 + preg_match_all('#%%related_questions_([^%]*?)%%#i', $prompt, $mxatches);
784 + if(isset($mxatches[1][0]))
785 + {
786 + foreach($mxatches[1] as $googlematch)
787 + {
788 + $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
789 + if(is_array($mtchres) && !empty($mtchres))
790 + {
791 + $quests = array();
792 + foreach($mtchres as $mra)
793 + {
794 + if(isset($mra['q']))
795 + {
796 + $quests[] = $mra['q'];
797 + }
798 + }
799 + $mtchres = implode(',', $quests);
800 + }
801 + $prompt = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $prompt);
802 + }
803 + }
804 + return $prompt;
805 + }
806 + function aiomatic_replace_review($prompt, $post_title, $search_keywords, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list = '', $item_score = '', $edition = '', $language = '', $pages_count = '', $publication_date = '', $contributors = '', $manufacturer = '', $binding = '', $product_group = '', $rating = '', $eans = '', $part_no = '', $model = '', $warranty = '', $color = '', $is_adult = '', $dimensions = '', $date = '', $size = '', $unit_count = '')
807 + {
808 + $blog_title = html_entity_decode(get_bloginfo('title'));
809 + $prompt = str_replace('%%search_keywords%%', $search_keywords, $prompt);
810 + $prompt = str_replace('%%post_title%%', $post_title, $prompt);
811 + $prompt = str_replace('%%product_title%%', $product_title, $prompt);
812 + $prompt = str_replace('%%product_description%%', $product_description, $prompt);
813 + $prompt = str_replace('%%aff_url%%', $aff_url, $prompt);
814 + $prompt = str_replace('%%product_author%%', $product_author, $prompt);
815 + $prompt = str_replace('%%offer_price%%', $offer_price, $prompt);
816 + $prompt = str_replace('%%product_price%%', $product_price, $prompt);
817 + $prompt = str_replace('%%product_list_price%%', $product_list_price, $prompt);
818 + $prompt = str_replace('%%price_numeric%%', $price_numeric, $prompt);
819 + $prompt = str_replace('%%price_currency%%', $price_currency, $prompt);
820 + $prompt = str_replace('%%product_brand%%', $product_brand, $prompt);
821 + $prompt = str_replace('%%product_reviews%%', $product_reviews, $prompt);
822 + $prompt = str_replace('%%product_isbn%%', $product_isbn, $prompt);
823 + $prompt = str_replace('%%product_upc%%', $product_upc, $prompt);
824 + $prompt = str_replace('%%product_asin%%', $product_asin, $prompt);
825 + $prompt = str_replace('%%cart_url%%', $cart_url, $prompt);
826 + $prompt = str_replace('%%list_price_numeric%%', $list_price_numeric, $prompt);
827 + $prompt = str_replace('%%product_imgs%%', $product_imgs, $prompt);
828 + $prompt = str_replace('%%all_product_titles%%', $product_titles, $prompt);
829 + $prompt = str_replace('%%post_title_keywords%%', $post_title_keywords, $prompt);
830 + $prompt = str_replace('%%blog_title%%', $blog_title, $prompt);
831 + $prompt = str_replace('%%language%%', $content_language, $prompt);
832 + $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
833 + $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
834 + $prompt = str_replace('%%sections%%', $sections, $prompt);
835 + $prompt = str_replace('%%current_section%%', $current_section, $prompt);
836 + $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
837 + $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
838 + $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
839 + $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
840 + $prompt = str_replace('%%point_of_view%%', $point_of_view, $prompt);
841 + //new
842 + $prompt = str_replace('%%product_score%%', $item_score, $prompt);
843 + $prompt = str_replace('%%product_edition%%', $edition, $prompt);
844 + $prompt = str_replace('%%product_language%%', $language, $prompt);
845 + $prompt = str_replace('%%product_pages_count%%', $pages_count, $prompt);
846 + $prompt = str_replace('%%product_publication_date%%', $publication_date, $prompt);
847 + $prompt = str_replace('%%product_contributors%%', $contributors, $prompt);
848 + $prompt = str_replace('%%product_manufacturer%%', $manufacturer, $prompt);
849 + $prompt = str_replace('%%product_binding%%', $binding, $prompt);
850 + $prompt = str_replace('%%product_product_group%%', $product_group, $prompt);
851 + $prompt = str_replace('%%product_rating%%', $rating, $prompt);
852 + $prompt = str_replace('%%product_ean%%', $eans, $prompt);
853 + $prompt = str_replace('%%product_part_no%%', $part_no, $prompt);
854 + $prompt = str_replace('%%product_model%%', $model, $prompt);
855 + $prompt = str_replace('%%product_warranty%%', $warranty, $prompt);
856 + $prompt = str_replace('%%product_color%%', $color, $prompt);
857 + $prompt = str_replace('%%product_is_adult%%', $is_adult, $prompt);
858 + $prompt = str_replace('%%product_dimensions%%', $dimensions, $prompt);
859 + $prompt = str_replace('%%product_date%%', $date, $prompt);
860 + $prompt = str_replace('%%product_size%%', $size, $prompt);
861 + $prompt = str_replace('%%product_unit_count%%', $unit_count, $prompt);
862 + $prompt = aiomatic_replaceSynergyShortcodes($prompt);
863 + if($custom_shortcodes != '')
864 + {
865 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
866 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
867 + {
868 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for the custom shortcode creator to work!');
869 + }
870 + else
871 + {
872 + $allmodels = aiomatic_get_all_models();
873 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
874 + $appids = array_filter($appids);
875 + $token = $appids[array_rand($appids)];
876 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
877 + if($custom_shortcodes != '' && !is_array($custom_shortcodes))
878 + {
879 + $custom_shortcodes = aiomatic_replace_review($custom_shortcodes, $post_title, $search_keywords, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $language, $writing_style, $writing_tone, $sections, $current_section, $sections_count, $paragraph_count, $post_title_keywords, '', $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
880 + }
881 + $custom_shortcodes = str_replace('%%search_keywords%%', $search_keywords, $custom_shortcodes);
882 + $custom_shortcodes = str_replace('%%post_title%%', $post_title, $custom_shortcodes);
883 + $custom_shortcodes = str_replace('%%product_title%%', $product_title, $custom_shortcodes);
884 + $custom_shortcodes = str_replace('%%product_description%%', $product_description, $custom_shortcodes);
885 + $custom_shortcodes = str_replace('%%aff_url%%', $aff_url, $custom_shortcodes);
886 + $custom_shortcodes = str_replace('%%product_author%%', $product_author, $custom_shortcodes);
887 + $custom_shortcodes = str_replace('%%offer_price%%', $offer_price, $custom_shortcodes);
888 + $custom_shortcodes = str_replace('%%product_price%%', $product_price, $custom_shortcodes);
889 + $custom_shortcodes = str_replace('%%product_list_price%%', $product_list_price, $custom_shortcodes);
890 + $custom_shortcodes = str_replace('%%price_numeric%%', $price_numeric, $prcustom_shortcodesompt);
891 + $custom_shortcodes = str_replace('%%price_currency%%', $price_currency, $custom_shortcodes);
892 + $custom_shortcodes = str_replace('%%product_brand%%', $product_brand, $custom_shortcodes);
893 + $custom_shortcodes = str_replace('%%product_reviews%%', $product_reviews, $custom_shortcodes);
894 + $custom_shortcodes = str_replace('%%product_isbn%%', $product_isbn, $custom_shortcodes);
895 + $custom_shortcodes = str_replace('%%product_upc%%', $product_upc, $custom_shortcodes);
896 + $custom_shortcodes = str_replace('%%product_asin%%', $product_asin, $custom_shortcodes);
897 + $custom_shortcodes = str_replace('%%cart_url%%', $cart_url, $custom_shortcodes);
898 + $custom_shortcodes = str_replace('%%list_price_numeric%%', $list_price_numeric, $custom_shortcodes);
899 + $custom_shortcodes = str_replace('%%product_imgs%%', $product_imgs, $custom_shortcodes);
900 + $custom_shortcodes = str_replace('%%all_product_titles%%', $product_titles, $custom_shortcodes);
901 + $custom_shortcodes = str_replace('%%post_title_keywords%%', $post_title_keywords, $custom_shortcodes);
902 + $custom_shortcodes = str_replace('%%blog_title%%', $blog_title, $custom_shortcodes);
903 + $custom_shortcodes = str_replace('%%language%%', $content_language, $custom_shortcodes);
904 + $custom_shortcodes = str_replace('%%writing_style%%', $writing_style, $custom_shortcodes);
905 + $custom_shortcodes = str_replace('%%writing_tone%%', $writing_tone, $custom_shortcodes);
906 + $custom_shortcodes = str_replace('%%sections%%', $sections, $custom_shortcodes);
907 + $custom_shortcodes = str_replace('%%current_section%%', $current_section, $custom_shortcodes);
908 + $custom_shortcodes = str_replace('%%sections_count%%', $sections_count, $custom_shortcodes);
909 + $custom_shortcodes = str_replace('%%paragraphs_per_section%%', $paragraph_count, $custom_shortcodes);
910 + $custom_shortcodes = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $custom_shortcodes);
911 + $custom_shortcodes = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $custom_shortcodes);
912 + $custom_shortcodes = str_replace('%%point_of_view%%', $point_of_view, $custom_shortcodes);
913 + //new
914 + $custom_shortcodes = str_replace('%%product_score%%', $item_score, $custom_shortcodes);
915 + $custom_shortcodes = str_replace('%%product_edition%%', $edition, $custom_shortcodes);
916 + $custom_shortcodes = str_replace('%%product_language%%', $language, $custom_shortcodes);
917 + $custom_shortcodes = str_replace('%%product_pages_count%%', $pages_count, $custom_shortcodes);
918 + $custom_shortcodes = str_replace('%%product_publication_date%%', $publication_date, $custom_shortcodes);
919 + $custom_shortcodes = str_replace('%%product_contributors%%', $contributors, $custom_shortcodes);
920 + $custom_shortcodes = str_replace('%%product_manufacturer%%', $manufacturer, $custom_shortcodes);
921 + $custom_shortcodes = str_replace('%%product_binding%%', $binding, $custom_shortcodes);
922 + $custom_shortcodes = str_replace('%%product_product_group%%', $product_group, $custom_shortcodes);
923 + $custom_shortcodes = str_replace('%%product_rating%%', $rating, $custom_shortcodes);
924 + $custom_shortcodes = str_replace('%%product_ean%%', $eans, $custom_shortcodes);
925 + $custom_shortcodes = str_replace('%%product_part_no%%', $part_no, $custom_shortcodes);
926 + $custom_shortcodes = str_replace('%%product_model%%', $model, $custom_shortcodes);
927 + $custom_shortcodes = str_replace('%%product_warranty%%', $warranty, $custom_shortcodes);
928 + $custom_shortcodes = str_replace('%%product_color%%', $color, $custom_shortcodes);
929 + $custom_shortcodes = str_replace('%%product_is_adult%%', $is_adult, $custom_shortcodes);
930 + $custom_shortcodes = str_replace('%%product_dimensions%%', $dimensions, $custom_shortcodes);
931 + $custom_shortcodes = str_replace('%%product_date%%', $date, $custom_shortcodes);
932 + $custom_shortcodes = str_replace('%%product_size%%', $size, $custom_shortcodes);
933 + $custom_shortcodes = str_replace('%%product_unit_count%%', $unit_count, $custom_shortcodes);
934 + $custom_shortcodes = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
935 + foreach($custom_shortcodes as $my_short)
936 + {
937 + $name_part = explode('=>', $my_short);
938 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
939 + {
940 + $shortname = trim($name_part[0]);
941 + if(strstr($prompt, '%%' . $shortname . '%%'))
942 + {
943 + $shortval = '';
944 + $ai_part = explode('@@', $name_part[1]);
945 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
946 + {
947 + if(!in_array(trim($ai_part[0]), $allmodels))
948 + {
949 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
950 + }
951 + else
952 + {
953 + $aimodel = trim($ai_part[0]);
954 + }
955 + $ai_command = trim($ai_part[1]);
956 + $ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $ai_command);
957 + foreach($custom_shortcodes as $my_check_short)
958 + {
959 + $check_name_part = explode('=>', $my_check_short);
960 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
961 + {
962 + $check_shortname = trim($check_name_part[0]);
963 + if($shortname !== $check_shortname && strstr($ai_command, '%%' . $check_shortname . '%%') !== false)
964 + {
965 + $check_shortval = '';
966 + $check_ai_part = explode('@@', $check_name_part[1]);
967 + if(!in_array(trim($check_ai_part[0]), $allmodels))
968 + {
969 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
970 + }
971 + else
972 + {
973 + $check_aimodel = trim($check_ai_part[0]);
974 + }
975 + $check_ai_command = trim($check_ai_part[1]);
976 + $aierror = '';
977 +
978 + $check_ai_command = str_replace('%%search_keywords%%', $search_keywords, $check_ai_command);
979 + $check_ai_command = str_replace('%%post_title%%', $post_title, $check_ai_command);
980 + $check_ai_command = str_replace('%%product_title%%', $product_title, $check_ai_command);
981 + $check_ai_command = str_replace('%%product_description%%', $product_description, $check_ai_command);
982 + $check_ai_command = str_replace('%%aff_url%%', $aff_url, $check_ai_command);
983 + $check_ai_command = str_replace('%%product_author%%', $product_author, $check_ai_command);
984 + $check_ai_command = str_replace('%%offer_price%%', $offer_price, $check_ai_command);
985 + $check_ai_command = str_replace('%%product_price%%', $product_price, $check_ai_command);
986 + $check_ai_command = str_replace('%%product_list_price%%', $product_list_price, $check_ai_command);
987 + $check_ai_command = str_replace('%%price_numeric%%', $price_numeric, $prcheck_ai_commandompt);
988 + $check_ai_command = str_replace('%%price_currency%%', $price_currency, $check_ai_command);
989 + $check_ai_command = str_replace('%%product_brand%%', $product_brand, $check_ai_command);
990 + $check_ai_command = str_replace('%%product_reviews%%', $product_reviews, $check_ai_command);
991 + $check_ai_command = str_replace('%%product_isbn%%', $product_isbn, $check_ai_command);
992 + $check_ai_command = str_replace('%%product_upc%%', $product_upc, $check_ai_command);
993 + $check_ai_command = str_replace('%%product_asin%%', $product_asin, $check_ai_command);
994 + $check_ai_command = str_replace('%%cart_url%%', $cart_url, $check_ai_command);
995 + $check_ai_command = str_replace('%%list_price_numeric%%', $list_price_numeric, $check_ai_command);
996 + $check_ai_command = str_replace('%%product_imgs%%', $product_imgs, $check_ai_command);
997 + $check_ai_command = str_replace('%%all_product_titles%%', $product_titles, $check_ai_command);
998 + $check_ai_command = str_replace('%%post_title_keywords%%', $post_title_keywords, $check_ai_command);
999 + $check_ai_command = str_replace('%%blog_title%%', $blog_title, $check_ai_command);
1000 + $check_ai_command = str_replace('%%language%%', $content_language, $check_ai_command);
1001 + $check_ai_command = str_replace('%%writing_style%%', $writing_style, $check_ai_command);
1002 + $check_ai_command = str_replace('%%writing_tone%%', $writing_tone, $check_ai_command);
1003 + $check_ai_command = str_replace('%%sections%%', $sections, $check_ai_command);
1004 + $check_ai_command = str_replace('%%current_section%%', $current_section, $check_ai_command);
1005 + $check_ai_command = str_replace('%%sections_count%%', $sections_count, $check_ai_command);
1006 + $check_ai_command = str_replace('%%paragraphs_per_section%%', $paragraph_count, $check_ai_command);
1007 + $check_ai_command = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $check_ai_command);
1008 + $check_ai_command = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $check_ai_command);
1009 + $check_ai_command = str_replace('%%point_of_view%%', $point_of_view, $check_ai_command);
1010 + //new
1011 + $check_ai_command = str_replace('%%product_score%%', $item_score, $check_ai_command);
1012 + $check_ai_command = str_replace('%%product_edition%%', $edition, $check_ai_command);
1013 + $check_ai_command = str_replace('%%product_language%%', $language, $check_ai_command);
1014 + $check_ai_command = str_replace('%%product_pages_count%%', $pages_count, $check_ai_command);
1015 + $check_ai_command = str_replace('%%product_publication_date%%', $publication_date, $check_ai_command);
1016 + $check_ai_command = str_replace('%%product_contributors%%', $contributors, $check_ai_command);
1017 + $check_ai_command = str_replace('%%product_manufacturer%%', $manufacturer, $check_ai_command);
1018 + $check_ai_command = str_replace('%%product_binding%%', $binding, $check_ai_command);
1019 + $check_ai_command = str_replace('%%product_product_group%%', $product_group, $check_ai_command);
1020 + $check_ai_command = str_replace('%%product_rating%%', $rating, $check_ai_command);
1021 + $check_ai_command = str_replace('%%product_ean%%', $eans, $check_ai_command);
1022 + $check_ai_command = str_replace('%%product_part_no%%', $part_no, $check_ai_command);
1023 + $check_ai_command = str_replace('%%product_model%%', $model, $check_ai_command);
1024 + $check_ai_command = str_replace('%%product_warranty%%', $warranty, $check_ai_command);
1025 + $check_ai_command = str_replace('%%product_color%%', $color, $check_ai_command);
1026 + $check_ai_command = str_replace('%%product_is_adult%%', $is_adult, $check_ai_command);
1027 + $check_ai_command = str_replace('%%product_dimensions%%', $dimensions, $check_ai_command);
1028 + $check_ai_command = str_replace('%%product_date%%', $date, $check_ai_command);
1029 + $check_ai_command = str_replace('%%product_size%%', $size, $check_ai_command);
1030 + $check_ai_command = str_replace('%%product_unit_count%%', $unit_count, $check_ai_command);
1031 +
1032 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
1033 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
1034 + if($check_generated_text === false)
1035 + {
1036 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
1037 + }
1038 + else
1039 + {
1040 + $check_shortval = trim(trim($check_generated_text), '.');
1041 + }
1042 + $ai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $ai_command);
1043 + }
1044 + }
1045 + }
1046 + preg_match_all('#%%related_questions_([^%]*?)%%#i', $ai_command, $mxatches);
1047 + if(isset($mxatches[1][0]))
1048 + {
1049 + foreach($mxatches[1] as $googlematch)
1050 + {
1051 + $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
1052 + if(is_array($mtchres) && !empty($mtchres))
1053 + {
1054 + $quests = array();
1055 + foreach($mtchres as $mra)
1056 + {
1057 + if(isset($mra['q']))
1058 + {
1059 + $quests[] = $mra['q'];
1060 + }
1061 + }
1062 + $mtchres = implode(',', $quests);
1063 + }
1064 + $ai_command = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $ai_command);
1065 + }
1066 + }
1067 + $aierror = '';
1068 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
1069 + if($generated_text === false)
1070 + {
1071 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
1072 + }
1073 + else
1074 + {
1075 + $shortval = trim(trim($generated_text), '.');
1076 + }
1077 + }
1078 + $prompt = str_replace('%%' . $shortname . '%%', $shortval, $prompt);
1079 + }
1080 + }
1081 + }
1082 + }
1083 + }
1084 + $prompt = str_replace('%%default_post_cats%%', $default_cat_list, $prompt);
1085 + $prompt = apply_filters('aiomatic_replace_aicontent_shortcode', $prompt);
1086 + preg_match_all('#%%related_questions_([^%]*?)%%#i', $prompt, $mxatches);
1087 + if(isset($mxatches[1][0]))
1088 + {
1089 + foreach($mxatches[1] as $googlematch)
1090 + {
1091 + $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
1092 + if(is_array($mtchres) && !empty($mtchres))
1093 + {
1094 + $quests = array();
1095 + foreach($mtchres as $mra)
1096 + {
1097 + if(isset($mra['q']))
1098 + {
1099 + $quests[] = $mra['q'];
1100 + }
1101 + }
1102 + $mtchres = implode(',', $quests);
1103 + }
1104 + $prompt = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $prompt);
1105 + }
1106 + }
1107 + return $prompt;
1108 + }
1109 + function aiomatic_get_royalty_free_image($query_words, $enable_ai_images, $image_size, &$images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, &$img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, &$attached_id, &$raw_img_list = array(), $default_cat_list = '', $image_model = 'dalle2')
1110 + {
1111 + $ret_image = false;
1112 + $full_result_list = array();
1113 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
1114 + if(count($images_arr) > 0)
1115 + {
1116 + $first_el = array_shift($images_arr);
1117 + $ret_image = $first_el;
1118 + }
1119 + elseif($enable_ai_images == '1')
1120 + {
1121 + if(aiomatic_strlen($query_words) > 1000)
1122 + {
1123 + $query_words = aiomatic_substr($query_words, 0, 1000);
1124 + }
1125 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1126 + {
1127 + $api_service = aiomatic_get_api_service($token, $image_model);
1128 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1129 + }
1130 + $aierror = '';
1131 + $temp_get_imgs = aiomatic_generate_ai_image($token, 1, $query_words, $image_size, 'contentImageAI', false, 0, $aierror, $image_model, $old_title);
1132 + if($temp_get_imgs !== false)
1133 + {
1134 + foreach($temp_get_imgs as $tmpimg)
1135 + {
1136 + $ret_image = $tmpimg;
1137 + }
1138 + }
1139 + else
1140 + {
1141 + aiomatic_log_to_file('Failed to generate AI image: ' . $aierror);
1142 + }
1143 + }
1144 + elseif($enable_ai_images == '2')
1145 + {
1146 + if(aiomatic_strlen($query_words) > 2000)
1147 + {
1148 + $query_words = aiomatic_substr($query_words, 0, 2000);
1149 + }
1150 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1151 + {
1152 + $api_service = 'Stability.AI';
1153 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1154 + }
1155 + if($image_size == '256x256')
1156 + {
1157 + $width = '512';
1158 + $height = '512';
1159 + }
1160 + elseif($image_size == '512x512')
1161 + {
1162 + $width = '512';
1163 + $height = '512';
1164 + }
1165 + elseif($image_size == '1024x1024')
1166 + {
1167 + $width = '1024';
1168 + $height = '1024';
1169 + }
1170 + else
1171 + {
1172 + $width = '512';
1173 + $height = '512';
1174 + }
1175 + $ierror = '';
1176 + $temp_get_imgs = aiomatic_generate_stability_image($query_words, $height, $width, 'contentStableImageAI', 0, false, $ierror, false, false, '', 0);
1177 + if($temp_get_imgs !== false)
1178 + {
1179 + $ret_image = $temp_get_imgs[1];
1180 + }
1181 + else
1182 + {
1183 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
1184 + }
1185 + }
1186 + elseif($enable_ai_images == '7')
1187 + {
1188 + if(aiomatic_strlen($query_words) > 2000)
1189 + {
1190 + $query_words = aiomatic_substr($query_words, 0, 2000);
1191 + }
1192 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1193 + {
1194 + $api_service = 'Google';
1195 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1196 + }
1197 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
1198 + {
1199 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
1200 + }
1201 + else
1202 + {
1203 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
1204 + }
1205 + $ierror = '';
1206 + $temp_get_imgs = aiomatic_generate_image_google($query_words, $model, 'contentGoogleImageAI', false, $ierror, false);
1207 + if($temp_get_imgs !== false)
1208 + {
1209 + $ret_image = $temp_get_imgs[1];
1210 + }
1211 + else
1212 + {
1213 + aiomatic_log_to_file('Failed to generate Google image: ' . $ierror);
1214 + }
1215 + }
1216 + elseif($enable_ai_images == '4')
1217 + {
1218 + if(aiomatic_strlen($query_words) > 2000)
1219 + {
1220 + $query_words = aiomatic_substr($query_words, 0, 2000);
1221 + }
1222 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1223 + {
1224 + $api_service = 'Midjourney';
1225 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1226 + }
1227 + if($image_size == '256x256')
1228 + {
1229 + $width = '512';
1230 + $height = '512';
1231 + }
1232 + elseif($image_size == '512x512')
1233 + {
1234 + $width = '512';
1235 + $height = '512';
1236 + }
1237 + elseif($image_size == '1024x1024')
1238 + {
1239 + $width = '1024';
1240 + $height = '1024';
1241 + }
1242 + elseif($image_size == '1024x1792')
1243 + {
1244 + $width = '1024';
1245 + $height = '1792';
1246 + }
1247 + elseif($image_size == '1792x1024')
1248 + {
1249 + $width = '1792';
1250 + $height = '1024';
1251 + }
1252 + else
1253 + {
1254 + $width = '512';
1255 + $height = '512';
1256 + }
1257 + $ierror = '';
1258 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($query_words, $width, $height, 'contentMidjourneyImageAI', false, $ierror);
1259 + if($temp_get_imgs !== false)
1260 + {
1261 + $ret_image = $temp_get_imgs;
1262 + }
1263 + else
1264 + {
1265 + aiomatic_log_to_file('Failed to generate Midjourney image: ' . $ierror);
1266 + }
1267 + }
1268 + elseif($enable_ai_images == '5')
1269 + {
1270 + if(aiomatic_strlen($query_words) > 2000)
1271 + {
1272 + $query_words = aiomatic_substr($query_words, 0, 2000);
1273 + }
1274 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1275 + {
1276 + $api_service = 'Replicate';
1277 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1278 + }
1279 + if($image_size == '256x256')
1280 + {
1281 + $width = '512';
1282 + $height = '512';
1283 + }
1284 + elseif($image_size == '512x512')
1285 + {
1286 + $width = '512';
1287 + $height = '512';
1288 + }
1289 + elseif($image_size == '1024x1024')
1290 + {
1291 + $width = '1024';
1292 + $height = '1024';
1293 + }
1294 + elseif($image_size == '1024x1792')
1295 + {
1296 + $width = '1024';
1297 + $height = '1792';
1298 + }
1299 + elseif($image_size == '1792x1024')
1300 + {
1301 + $width = '1792';
1302 + $height = '1024';
1303 + }
1304 + else
1305 + {
1306 + $width = '512';
1307 + $height = '512';
1308 + }
1309 + $ierror = '';
1310 + $temp_get_imgs = aiomatic_generate_replicate_image($query_words, $width, $height, 'contentReplicateImageAI', false, $ierror);
1311 + if($temp_get_imgs !== false)
1312 + {
1313 + $ret_image = $temp_get_imgs;
1314 + }
1315 + else
1316 + {
1317 + aiomatic_log_to_file('Failed to generate Replicate image: ' . $ierror);
1318 + }
1319 + }
1320 + elseif($enable_ai_images == '6')
1321 + {
1322 + if(aiomatic_strlen($query_words) > 3000)
1323 + {
1324 + $query_words = aiomatic_substr($query_words, 0, 3000);
1325 + }
1326 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1327 + {
1328 + $api_service = 'Ideogram';
1329 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $query_words);
1330 + }
1331 + if($image_size == '256x256')
1332 + {
1333 + $width = '512';
1334 + $height = '512';
1335 + }
1336 + elseif($image_size == '512x512')
1337 + {
1338 + $width = '512';
1339 + $height = '512';
1340 + }
1341 + elseif($image_size == '1024x1024')
1342 + {
1343 + $width = '1024';
1344 + $height = '1024';
1345 + }
1346 + elseif($image_size == '1024x1792')
1347 + {
1348 + $width = '1024';
1349 + $height = '1792';
1350 + }
1351 + elseif($image_size == '1792x1024')
1352 + {
1353 + $width = '1792';
1354 + $height = '1024';
1355 + }
1356 + else
1357 + {
1358 + $width = '512';
1359 + $height = '512';
1360 + }
1361 + $ierror = '';
1362 + $temp_get_imgs = aiomatic_generate_ideogram_image($query_words, $width, $height, 'contentIdeogramImageAI', false, $ierror);
1363 + if($temp_get_imgs !== false)
1364 + {
1365 + $ret_image = $temp_get_imgs;
1366 + }
1367 + else
1368 + {
1369 + aiomatic_log_to_file('Failed to generate Ideogram image: ' . $ierror);
1370 + }
1371 + }
1372 + elseif($enable_ai_images == '3')
1373 + {
1374 + if ($image_url != '')
1375 + {
1376 + $replacement = str_replace(array('[', ']'), '', $query_words);
1377 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
1378 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
1379 + if(isset($matches[2]))
1380 + {
1381 + $chance = trim($matches[2], '[]');
1382 + }
1383 + else
1384 + {
1385 + $chance = '';
1386 + }
1387 + $arv = array();
1388 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
1389 + return $my_img;
1390 + }, $image_url_temp);
1391 + $spintax = new Aiomatic_Spintax();
1392 + $img_rulx = $spintax->Parse(trim($image_url_temp));
1393 + $img_rulx = explode(',', $img_rulx);
1394 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
1395 + if($img_rulx != '')
1396 + {
1397 + $ret_image = $img_rulx;
1398 + }
1399 + }
1400 + }
1401 + else
1402 + {
1403 + $image_query = aiomatic_extract_keyword($query_words, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $token, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
1404 + if(!empty($image_query))
1405 + {
1406 + $kws = explode(',', $image_query);
1407 + $image_query = trim($kws[0]);
1408 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1409 + {
1410 + aiomatic_log_to_file('Keyword "' . $image_query . '" was extracted from text: ' . $query_words);
1411 + }
1412 + $query_words = $image_query;
1413 + }
1414 + $attached_id = '';
1415 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
1416 + if($temp_get_img == '' || $temp_get_img === false)
1417 + {
1418 + aiomatic_log_to_file('Failed to find royalty free image for: ' . $query_words);
1419 + }
1420 + else
1421 + {
1422 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1423 + {
1424 + aiomatic_log_to_file('Got royalty free image: ' . $temp_get_img);
1425 + }
1426 + $ret_image = $temp_get_img;
1427 + }
1428 + }
1429 + return $ret_image;
1430 + }
1431 + function aiomatic_extract_keyword($image_query, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $token, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list)
1432 + {
1433 + $query_words = '';
1434 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
1435 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
1436 + {
1437 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
1438 + {
1439 + try
1440 + {
1441 + if(!class_exists('TextRazor'))
1442 + {
1443 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
1444 + }
1445 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
1446 + $textrazor = new TextRazor();
1447 + $textrazor->addExtractor('entities');
1448 + $response = $textrazor->analyze($image_query);
1449 + if (isset($response['response']['entities']))
1450 + {
1451 + foreach ($response['response']['entities'] as $entity)
1452 + {
1453 + if(isset($entity['entityEnglishId']))
1454 + {
1455 + $query_words = $entity['entityEnglishId'];
1456 + if(!empty($query_words))
1457 + {
1458 + break;
1459 + }
1460 + }
1461 + else
1462 + {
1463 + $query_words = $entity['entityId'];
1464 + if(!empty($query_words))
1465 + {
1466 + break;
1467 + }
1468 + }
1469 + }
1470 + }
1471 + }
1472 + catch(Exception $e)
1473 + {
1474 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1475 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (!): ' . $e->getMessage());
1476 + }
1477 + }
1478 + }
1479 + }
1480 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
1481 + {
1482 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
1483 + {
1484 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
1485 + {
1486 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
1487 + }
1488 + else
1489 + {
1490 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1491 + }
1492 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
1493 + {
1494 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
1495 + }
1496 + else
1497 + {
1498 + $keyword_assistant_id = '';
1499 + }
1500 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
1501 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
1502 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
1503 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
1504 + {
1505 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
1506 + $title_ai_command = array_filter($title_ai_command);
1507 + if(count($title_ai_command) > 0)
1508 + {
1509 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
1510 + }
1511 + else
1512 + {
1513 + $title_ai_command = '';
1514 + }
1515 + }
1516 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
1517 + if(!empty($title_ai_command))
1518 + {
1519 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $image_query, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, $global_prepend, $global_append);
1520 + }
1521 + $title_ai_command = trim($title_ai_command);
1522 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
1523 + {
1524 + $txt_content = aiomatic_get_web_page($title_ai_command);
1525 + if ($txt_content !== FALSE)
1526 + {
1527 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
1528 + $txt_content = array_filter($txt_content);
1529 + if(count($txt_content) > 0)
1530 + {
1531 + $txt_content = $txt_content[array_rand($txt_content)];
1532 + if(trim($txt_content) != '')
1533 + {
1534 + $title_ai_command = $txt_content;
1535 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
1536 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $image_query, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, $global_prepend, $global_append);
1537 + }
1538 + }
1539 + }
1540 + }
1541 + if(empty($title_ai_command))
1542 + {
1543 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
1544 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($image_query));
1545 + }
1546 + $max_seed_tokens = AIMOGEN_DEFAULT_COMPLETION_TOKENS;
1547 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
1548 + {
1549 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
1550 + }
1551 + $title_ai_command = trim($title_ai_command);
1552 + if(empty($title_ai_command))
1553 + {
1554 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
1555 + }
1556 + else
1557 + {
1558 + $max_tokens = 2048;
1559 + $query_token_count = count(aiomatic_encode($title_ai_command));
1560 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
1561 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
1562 + {
1563 + $string_len = aiomatic_strlen($title_ai_command);
1564 + $string_len = $string_len / 2;
1565 + $string_len = intval(0 - $string_len);
1566 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
1567 + $title_ai_command = trim($title_ai_command);
1568 + $query_token_count = count(aiomatic_encode($title_ai_command));
1569 + $available_tokens = $max_tokens - $query_token_count;
1570 + }
1571 + $thread_id = '';
1572 + $aierror = '';
1573 + $finish_reason = '';
1574 + $temperature = 0;
1575 + $top_p = 0;
1576 + $presence_penalty = 0;
1577 + $frequency_penalty = 0;
1578 + if(!empty($global_prepend))
1579 + {
1580 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
1581 + }
1582 + if(!empty($global_append))
1583 + {
1584 + $title_ai_command = $title_ai_command . ' ' . $global_append;
1585 + }
1586 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
1587 + {
1588 + $api_service = aiomatic_get_api_service($token, $kw_model);
1589 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $kw_model . ')(' . $available_tokens . ') for keyword text: ' . $title_ai_command);
1590 + }
1591 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordImg', 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, false, array(), '');
1592 + if($generated_text === false)
1593 + {
1594 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
1595 + }
1596 + else
1597 + {
1598 + $query_words = trim(trim($generated_text), '.');
1599 + }
1600 + }
1601 + }
1602 + }
1603 + if(empty($query_words))
1604 + {
1605 + $keyword_class = new Aiomatic_keywords();
1606 + $query_words = $keyword_class->keywords($image_query, 1);
1607 + }
1608 + return $query_words;
1609 + }
1610 +
1611 + function aiomatic_run_rule($param, $type = 0, $auto = 1, $ret_content = 0, $rules = null, $stop_omnmiblock_id = '', $omniWebhookID = '', $return_last_result = false, $load_variables = [])
1612 + {
1613 + global $shortcode_tags;
1614 + $param = intval($param);
1615 + $type = intval($type);
1616 + $plugin = plugin_basename(__FILE__);
1617 + $plugin_slug = explode('/', $plugin);
1618 + $plugin_slug = $plugin_slug[0];
1619 + $uoptions = array();
1620 + $event_context = array();
1621 + $is_activated = aiomatic_is_activated($plugin_slug, $uoptions);
1622 + if($is_activated !== true && $is_activated !== 2 && $is_activated !== -2)
1623 + {
1624 + 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.');
1625 + return 'fail';
1626 + }
1627 + $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
1628 + if (!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
1629 + {
1630 + aiomatic_log_to_file('You need to insert a valid OpenAI/AiomaticAPI API Key for this to work!');
1631 + return 'fail';
1632 + }
1633 + if(is_array($omniWebhookID) && !empty($omniWebhookID) && isset($omniWebhookID[1]))
1634 + {
1635 + $event_context = $omniWebhookID[1];
1636 + $omniWebhookID = $omniWebhookID[0];
1637 + }
1638 + $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
1639 + $appids = array_filter($appids);
1640 + $token = $appids[array_rand($appids)];
1641 + $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
1642 + if($ret_content == 0)
1643 + {
1644 + $f = fopen(get_temp_dir() . 'aiomatic_' . $type . '_' . $param, 'w');
1645 + if($f !== false)
1646 + {
1647 + $flock_disabled = explode(',', ini_get('disable_functions'));
1648 + if(!in_array('flock', $flock_disabled))
1649 + {
1650 + if (!flock($f, LOCK_EX | LOCK_NB)) {
1651 + return 'nochange';
1652 + }
1653 + }
1654 + }
1655 +
1656 + $GLOBALS['wp_object_cache']->delete('aiomatic_running_list', 'options');
1657 + if (!get_option('aiomatic_running_list')) {
1658 + $running = array();
1659 + } else {
1660 + $running = get_option('aiomatic_running_list');
1661 + }
1662 + if (!empty($running)) {
1663 + if (in_array(array(
1664 + $param => $type
1665 + ), $running))
1666 + {
1667 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
1668 + aiomatic_log_to_file('Only one instance of this rule is allowed. Rule is already running!');
1669 + }
1670 + return 'nochange';
1671 + }
1672 + }
1673 + $running[] = array(
1674 + $param => $type
1675 + );
1676 + aiomatic_update_option('aiomatic_running_list', $running, false);
1677 + register_shutdown_function('aiomatic_clear_flag_at_shutdown', $param, $type);
1678 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on')
1679 + {
1680 + aiomatic_log_exec_time('Run Rule Type ' . $type . ' ID ' . $param);
1681 + }
1682 + }
1683 + if (isset($aiomatic_Main_Settings['rule_timeout']) && $aiomatic_Main_Settings['rule_timeout'] != '') {
1684 + $timeout = intval($aiomatic_Main_Settings['rule_timeout']);
1685 + } else {
1686 + $timeout = 36000;
1687 + }
1688 + ini_set('safe_mode', 'Off');
1689 + ini_set('max_execution_time', $timeout);
1690 + ini_set('ignore_user_abort', 1);
1691 + ini_set('user_agent', aiomatic_get_random_user_agent());
1692 + if(function_exists('ignore_user_abort'))
1693 + {
1694 + ignore_user_abort(true);
1695 + }
1696 + if(function_exists('set_time_limit'))
1697 + {
1698 + set_time_limit($timeout);
1699 + }
1700 + require_once(dirname(__FILE__) . "/res/aiomatic-chars.php");
1701 + $posts_inserted = 0;
1702 + $attached_id = '';
1703 + $additional_attachments = array();
1704 + if (isset($aiomatic_Main_Settings['aiomatic_enabled']) && $aiomatic_Main_Settings['aiomatic_enabled'] == 'on') {
1705 + try
1706 + {
1707 + $old_title = '';
1708 + $selected_forum = 0;
1709 + $cont = 0;
1710 + $found = 0;
1711 + $enable_comments = '1';
1712 + $enable_pingback = '1';
1713 + $ai_command = '';
1714 + $headings = '';
1715 + $images = '';
1716 + $videos = '';
1717 + $post_title = '';
1718 + $default_category = '';
1719 + $extra_categories = '';
1720 + $min_char = '1';
1721 + $post_status = 'publish';
1722 + $remove_default = '';
1723 + $random_order = '';
1724 + $csv_separator = '';
1725 + $title_ai_command = '';
1726 + $strip_title = '';
1727 + $img_all_headings = '';
1728 + $heading_img_location = '';
1729 + $title_source = '';
1730 + $skipt = 0;
1731 + $headings_ai_command = '';
1732 + $title_once = '';
1733 + $category_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1734 + $category_ai_command = '';
1735 + $custom_shortcodes= '';
1736 + $strip_by_regex = '';
1737 + $strip_by_regex_prompts = '';
1738 + $replace_regex = '';
1739 + $replace_regex_prompts = '';
1740 + $run_regex_on = '';
1741 + $max_links = '';
1742 + $search_query_repetition = '';
1743 + $link_post_types = 'post';
1744 + $enable_toc = '';
1745 + $tag_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1746 + $tag_ai_command = '';
1747 + $min_time = '';
1748 + $max_time = '';
1749 + $assistant_id = '';
1750 + $skip_spin = '';
1751 + $skip_translate = '';
1752 + $title_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1753 + $post_type = 'post';
1754 + $accept_comments = 'closed';
1755 + $post_user_name = 1;
1756 + $item_create_tag = '';
1757 + $can_create_tag = 'disabled';
1758 + $item_tags = '';
1759 + $max = 50;
1760 + $auto_categories = 'disabled';
1761 + $custom_fields = '';
1762 + $custom_tax = '';
1763 + $temperature = '';
1764 + $post_prepend = '';
1765 + $post_append = '';
1766 + $enable_ai_images = '';
1767 + $enable_ai_images_article = '';
1768 + $ai_command_image_article = '';
1769 + $image_model_article = '';
1770 + $image_size_article = '';
1771 + $top_p = '';
1772 + $presence_penalty = '';
1773 + $frequency_penalty = '';
1774 + $royalty_free = '';
1775 + $image_size = '256x256';
1776 + $headings_list = '';
1777 + $images_list = '';
1778 + $headings_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1779 + $wpml_lang = '';
1780 + $post_format = 'post-format-standard';
1781 + $max_tokens = AIMOGEN_DEFAULT_MAX_TOKENS;
1782 + $max_seed_tokens = AIMOGEN_DEFAULT_COMPLETION_TOKENS;
1783 + $posting_mode = 'title';
1784 + $post_topic_list = '';
1785 + $post_sections_list = '';
1786 + $content_language = '';
1787 + $writing_style = '';
1788 + $writing_tone = '';
1789 + $topic_proscons_model = '';
1790 + $proscons_prompt = '';
1791 + $topic_reviews_model = '';
1792 + $reviews_prompt = '';
1793 + $sections_prompt = '';
1794 + $title_prompt = '';
1795 + $content_prompt = '';
1796 + $excerpt_prompt = '';
1797 + $section_count = '';
1798 + $paragraph_count = '';
1799 + $topic_title_model = '';
1800 + $topic_sections_model = '';
1801 + $topic_content_model = '';
1802 + $topic_excerpt_model = '';
1803 + $single_content_call = '';
1804 + $intro_prompt = '';
1805 + $topic_intro_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1806 + $outro_prompt = '';
1807 + $topic_outro_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1808 + $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1809 + $topic_qa_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1810 + $topic_images = '';
1811 + $qa_prompt = '';
1812 + $topic_videos = '';
1813 + $no_random = '';
1814 + $title_toc = '';
1815 + $title_qa = '';
1816 + $title_outro = '';
1817 + $link_type = 'internal';
1818 + $link_list = '';
1819 + $link_nofollow = '';
1820 + $parent_id = '';
1821 + $skip_inexist = '';
1822 + $global_prepend = '';
1823 + $image_url = '';
1824 + $draft_me = false;
1825 + $global_append = '';
1826 + $enable_qa = '';
1827 + $sections_role = '';
1828 + $url_list = '';
1829 + $affiliate_id = '';
1830 + $first_hand = '';
1831 + $default_lang = '';
1832 + $max_products = '';
1833 + $min_price = '';
1834 + $max_price = '';
1835 + $sort_results = '';
1836 + $title_proscons = '';
1837 + $enable_proscons = '';
1838 + $enable_reviews = '';
1839 + $title_reviews = '';
1840 + $shuffle_products = '';
1841 + $amazon_keyword = '';
1842 + $rule_description = '';
1843 + $ai_titles = '';
1844 + $days_no_run = '';
1845 + $overwrite_existing = '';
1846 + $max_caption = '';
1847 + $max_continue_tokens = 500;
1848 + $title_generator_method = '';
1849 + $update_post_id = '';
1850 + $point_of_view = '';
1851 + $user_role = '';
1852 + $ai_command_image = '';
1853 + $rule_unique_id = '';
1854 + $no_headlink = '';
1855 + $enable_table = '';
1856 + $table_prompt = '';
1857 + $topic_table_model= '';
1858 + $more_replies = '';
1859 + $image_model = 'dalle2';
1860 + $reply_count = '';
1861 + $reply_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
1862 + $choose_prompt = '';
1863 + $reply_prompt = '';
1864 + if (isset($aiomatic_Main_Settings['store_data_rules']) && $aiomatic_Main_Settings['store_data_rules'] == 'on')
1865 + {
1866 + $store_data = 'on';
1867 + }
1868 + else
1869 + {
1870 + $store_data = 'off';
1871 + }
1872 + if($type == 0 || empty($type))
1873 + {
1874 + if($rules === null)
1875 + {
1876 + $GLOBALS['wp_object_cache']->delete('aiomatic_rules_list', 'options');
1877 + if (!get_option('aiomatic_rules_list')) {
1878 + $rules = array();
1879 + } else {
1880 + $rules = get_option('aiomatic_rules_list');
1881 + }
1882 + }
1883 + if (!empty($rules)) {
1884 + foreach ($rules as $request => $bundle[]) {
1885 + if ($cont == $param) {
1886 + $bundle_values = array_values($bundle);
1887 + $myValues = $bundle_values[$cont];
1888 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
1889 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
1890 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
1891 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
1892 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
1893 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
1894 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
1895 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
1896 + $item_create_tag = isset($array_my_values[7]) ? $array_my_values[7] : '';
1897 + $default_category = isset($array_my_values[8]) ? $array_my_values[8] : '';
1898 + $auto_categories = isset($array_my_values[9]) ? $array_my_values[9] : '';
1899 + $can_create_tag = isset($array_my_values[10]) ? $array_my_values[10] : '';
1900 + $enable_comments = isset($array_my_values[11]) ? $array_my_values[11] : '';
1901 + $image_url = isset($array_my_values[12]) ? $array_my_values[12] : '';
1902 + $post_title = isset($array_my_values[13]) ? htmlspecialchars_decode($array_my_values[13]) : '';
1903 + $enable_pingback = isset($array_my_values[14]) ? $array_my_values[14] : '';
1904 + $post_format = isset($array_my_values[15]) ? $array_my_values[15] : '';
1905 + $min_char = isset($array_my_values[16]) ? $array_my_values[16] : '';
1906 + $custom_fields = isset($array_my_values[17]) ? $array_my_values[17] : '';
1907 + $custom_tax = isset($array_my_values[18]) ? $array_my_values[18] : '';
1908 + $temperature = isset($array_my_values[19]) ? $array_my_values[19] : '';
1909 + $top_p = isset($array_my_values[20]) ? $array_my_values[20] : '';
1910 + $presence_penalty = isset($array_my_values[21]) ? $array_my_values[21] : '';
1911 + $frequency_penalty = isset($array_my_values[22]) ? $array_my_values[22] : '';
1912 + $royalty_free = isset($array_my_values[23]) ? $array_my_values[23] : '';
1913 + $ai_command = isset($array_my_values[24]) ? $array_my_values[24] : '';
1914 + $max_tokens = isset($array_my_values[25]) ? $array_my_values[25] : '';
1915 + $max_seed_tokens = isset($array_my_values[26]) ? $array_my_values[26] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
1916 + $max_continue_tokens= isset($array_my_values[27]) ? $array_my_values[27] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
1917 + $model = isset($array_my_values[28]) ? $array_my_values[28] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1918 + $headings = isset($array_my_values[29]) ? $array_my_values[29] : '';
1919 + $images = isset($array_my_values[30]) ? $array_my_values[30] : '';
1920 + $videos = isset($array_my_values[31]) ? $array_my_values[31] : '';
1921 + $post_prepend = isset($array_my_values[32]) ? $array_my_values[32] : '';
1922 + $post_append = isset($array_my_values[33]) ? $array_my_values[33] : '';
1923 + $enable_ai_images = isset($array_my_values[34]) ? $array_my_values[34] : '';
1924 + $ai_command_image = isset($array_my_values[35]) ? $array_my_values[35] : '';
1925 + $image_size = isset($array_my_values[36]) ? $array_my_values[36] : '';
1926 + $headings_list = isset($array_my_values[37]) ? $array_my_values[37] : '';
1927 + $images_list = isset($array_my_values[38]) ? $array_my_values[38] : '';
1928 + $wpml_lang = isset($array_my_values[39]) ? $array_my_values[39] : '';
1929 + $remove_default = isset($array_my_values[40]) ? $array_my_values[40] : '';
1930 + $title_model = isset($array_my_values[41]) ? $array_my_values[41] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1931 + $title_ai_command = isset($array_my_values[42]) ? $array_my_values[42] : '';
1932 + $strip_title = isset($array_my_values[43]) ? $array_my_values[43] : '';
1933 + $title_once = isset($array_my_values[44]) ? $array_my_values[44] : '';
1934 + $category_model = isset($array_my_values[45]) ? $array_my_values[45] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1935 + $category_ai_command= isset($array_my_values[46]) ? $array_my_values[46] : '';
1936 + $tag_model = isset($array_my_values[47]) ? $array_my_values[47] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1937 + $tag_ai_command = isset($array_my_values[48]) ? $array_my_values[48] : '';
1938 + $min_time = isset($array_my_values[49]) ? $array_my_values[49] : '';
1939 + $max_time = isset($array_my_values[50]) ? $array_my_values[50] : '';
1940 + $skip_spin = isset($array_my_values[51]) ? $array_my_values[51] : '';
1941 + $skip_translate = isset($array_my_values[52]) ? $array_my_values[52] : '';
1942 + $title_source = isset($array_my_values[53]) ? $array_my_values[53] : '';
1943 + $headings_ai_command= isset($array_my_values[54]) ? $array_my_values[54] : '';
1944 + $headings_model = isset($array_my_values[55]) ? $array_my_values[55] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1945 + $posting_mode = isset($array_my_values[56]) ? $array_my_values[56] : 'title';
1946 + $post_topic_list = isset($array_my_values[57]) ? $array_my_values[57] : '';
1947 + $post_sections_list= isset($array_my_values[58]) ? $array_my_values[58] : '';
1948 + $content_language = isset($array_my_values[59]) ? $array_my_values[59] : '';
1949 + $writing_style = isset($array_my_values[60]) ? $array_my_values[60] : '';
1950 + $writing_tone = isset($array_my_values[61]) ? $array_my_values[61] : '';
1951 + $title_prompt = isset($array_my_values[62]) ? $array_my_values[62] : '';
1952 + $sections_prompt = isset($array_my_values[63]) ? $array_my_values[63] : '';
1953 + $content_prompt = isset($array_my_values[64]) ? $array_my_values[64] : '';
1954 + $excerpt_prompt = isset($array_my_values[65]) ? $array_my_values[65] : '';
1955 + $section_count = isset($array_my_values[66]) ? $array_my_values[66] : '';
1956 + $paragraph_count = isset($array_my_values[67]) ? $array_my_values[67] : '';
1957 + $topic_title_model = isset($array_my_values[68]) ? $array_my_values[68] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1958 + $topic_sections_model = isset($array_my_values[69]) ? $array_my_values[69] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1959 + $topic_content_model = isset($array_my_values[70]) ? $array_my_values[70] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1960 + $topic_excerpt_model = isset($array_my_values[71]) ? $array_my_values[71] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1961 + $single_content_call = isset($array_my_values[72]) ? $array_my_values[72] : '';
1962 + $intro_prompt = isset($array_my_values[73]) ? $array_my_values[73] : '';
1963 + $topic_intro_model = isset($array_my_values[74]) ? $array_my_values[74] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1964 + $outro_prompt = isset($array_my_values[75]) ? $array_my_values[75] : '';
1965 + $topic_outro_model = isset($array_my_values[76]) ? $array_my_values[76] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1966 + $topic_images = isset($array_my_values[77]) ? $array_my_values[77] : '';
1967 + $sections_role = isset($array_my_values[78]) ? $array_my_values[78] : '';
1968 + $topic_videos = isset($array_my_values[79]) ? $array_my_values[79] : '';
1969 + $rule_description = isset($array_my_values[80]) ? $array_my_values[80] : '';
1970 + $custom_shortcodes = isset($array_my_values[81]) ? $array_my_values[81] : '';
1971 + $strip_by_regex = isset($array_my_values[82]) ? $array_my_values[82] : '';
1972 + $replace_regex = isset($array_my_values[83]) ? $array_my_values[83] : '';
1973 + $strip_by_regex_prompts= isset($array_my_values[84]) ? $array_my_values[84] : '';
1974 + $replace_regex_prompts= isset($array_my_values[85]) ? $array_my_values[85] : '';
1975 + $run_regex_on = isset($array_my_values[86]) ? $array_my_values[86] : '';
1976 + $max_links = isset($array_my_values[87]) ? $array_my_values[87] : '';
1977 + $link_post_types = isset($array_my_values[88]) ? $array_my_values[88] : '';
1978 + $enable_toc = isset($array_my_values[89]) ? $array_my_values[89] : '';
1979 + $title_toc = isset($array_my_values[90]) ? $array_my_values[90] : '';
1980 + $qa_prompt = isset($array_my_values[91]) ? $array_my_values[91] : '';
1981 + $topic_qa_model = isset($array_my_values[92]) ? $array_my_values[92] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
1982 + $enable_qa = isset($array_my_values[93]) ? $array_my_values[93] : '';
1983 + $title_qa = isset($array_my_values[94]) ? $array_my_values[94] : '';
1984 + $title_outro = isset($array_my_values[95]) ? $array_my_values[95] : '';
1985 + $link_type = isset($array_my_values[96]) ? $array_my_values[96] : 'internal';
1986 + $link_list = isset($array_my_values[97]) ? $array_my_values[97] : '';
1987 + $skip_inexist = isset($array_my_values[98]) ? $array_my_values[98] : '';
1988 + $global_prepend = isset($array_my_values[99]) ? $array_my_values[99] : '';
1989 + $global_append = isset($array_my_values[100]) ? $array_my_values[100] : '';
1990 + $search_query_repetition = isset($array_my_values[101]) ? $array_my_values[101] : '';
1991 + $img_all_headings = isset($array_my_values[102]) ? $array_my_values[102] : '';
1992 + $heading_img_location = isset($array_my_values[103]) ? $array_my_values[103] : 'bottom';
1993 + $days_no_run = isset($array_my_values[104]) ? $array_my_values[104] : '';
1994 + $overwrite_existing= isset($array_my_values[105]) ? $array_my_values[105] : '';
1995 + $link_nofollow = isset($array_my_values[106]) ? $array_my_values[106] : '';
1996 + $title_generator_method = isset($array_my_values[107]) ? $array_my_values[107] : '';
1997 + $parent_id = isset($array_my_values[108]) ? $array_my_values[108] : '';
1998 + $rule_unique_id = isset($array_my_values[109]) ? $array_my_values[109] : '';
1999 + $image_model = isset($array_my_values[110]) ? $array_my_values[110] : '';
2000 + $assistant_id = isset($array_my_values[111]) ? $array_my_values[111] : '';
2001 + $enable_ai_images_article = isset($array_my_values[112]) ? $array_my_values[112] : '';
2002 + $ai_command_image_article = isset($array_my_values[113]) ? $array_my_values[113] : '';
2003 + $image_model_article = isset($array_my_values[114]) ? $array_my_values[114] : '';
2004 + $image_size_article = isset($array_my_values[115]) ? $array_my_values[115] : '';
2005 + $found = 1;
2006 + break;
2007 + }
2008 + $cont = $cont + 1;
2009 + }
2010 + } else {
2011 + aiomatic_log_to_file('No rules found for aiomatic_rules_list!');
2012 + if($auto == 1)
2013 + {
2014 + aiomatic_clearFromList($param, $type);
2015 + }
2016 + return 'fail';
2017 + }
2018 + }
2019 + elseif($type == 1)
2020 + {
2021 + if($rules === null)
2022 + {
2023 + $GLOBALS['wp_object_cache']->delete('aiomatic_youtube_list', 'options');
2024 + if (!get_option('aiomatic_youtube_list')) {
2025 + $rules = array();
2026 + } else {
2027 + $rules = get_option('aiomatic_youtube_list');
2028 + }
2029 + }
2030 + if (!empty($rules)) {
2031 + foreach ($rules as $request => $bundle[]) {
2032 + if ($cont == $param) {
2033 + $bundle_values = array_values($bundle);
2034 + $myValues = $bundle_values[$cont];
2035 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2036 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2037 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2038 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2039 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2040 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
2041 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
2042 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
2043 + $item_create_tag = isset($array_my_values[7]) ? $array_my_values[7] : '';
2044 + $default_category = isset($array_my_values[8]) ? $array_my_values[8] : '';
2045 + $auto_categories = isset($array_my_values[9]) ? $array_my_values[9] : '';
2046 + $can_create_tag = isset($array_my_values[10]) ? $array_my_values[10] : '';
2047 + $enable_comments = isset($array_my_values[11]) ? $array_my_values[11] : '';
2048 + $image_url = isset($array_my_values[12]) ? $array_my_values[12] : '';
2049 + $url_list = isset($array_my_values[13]) ? $array_my_values[13] : '';
2050 + $enable_pingback = isset($array_my_values[14]) ? $array_my_values[14] : '';
2051 + $post_format = isset($array_my_values[15]) ? $array_my_values[15] : '';
2052 + $custom_fields = isset($array_my_values[16]) ? $array_my_values[16] : '';
2053 + $custom_tax = isset($array_my_values[17]) ? $array_my_values[17] : '';
2054 + $temperature = isset($array_my_values[18]) ? $array_my_values[18] : '';
2055 + $top_p = isset($array_my_values[19]) ? $array_my_values[19] : '';
2056 + $presence_penalty = isset($array_my_values[20]) ? $array_my_values[20] : '';
2057 + $frequency_penalty = isset($array_my_values[21]) ? $array_my_values[21] : '';
2058 + $royalty_free = isset($array_my_values[22]) ? $array_my_values[22] : '';
2059 + $max_tokens = isset($array_my_values[23]) ? $array_my_values[23] : '';
2060 + $max_seed_tokens = isset($array_my_values[24]) ? $array_my_values[24] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2061 + $max_continue_tokens= isset($array_my_values[25]) ? $array_my_values[25] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2062 + $post_prepend = isset($array_my_values[26]) ? $array_my_values[26] : '';
2063 + $post_append = isset($array_my_values[27]) ? $array_my_values[27] : '';
2064 + $enable_ai_images = isset($array_my_values[28]) ? $array_my_values[28] : '';
2065 + $ai_command_image = isset($array_my_values[29]) ? $array_my_values[29] : '';
2066 + $image_size = isset($array_my_values[30]) ? $array_my_values[30] : '';
2067 + $wpml_lang = isset($array_my_values[31]) ? $array_my_values[31] : '';
2068 + $remove_default = isset($array_my_values[32]) ? $array_my_values[32] : '';
2069 + $strip_title = isset($array_my_values[33]) ? $array_my_values[33] : '';
2070 + $title_once = isset($array_my_values[34]) ? $array_my_values[34] : '';
2071 + $category_model = isset($array_my_values[35]) ? $array_my_values[35] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2072 + $category_ai_command= isset($array_my_values[36]) ? $array_my_values[36] : '';
2073 + $tag_model = isset($array_my_values[37]) ? $array_my_values[37] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2074 + $tag_ai_command = isset($array_my_values[38]) ? $array_my_values[38] : '';
2075 + $min_time = isset($array_my_values[39]) ? $array_my_values[39] : '';
2076 + $max_time = isset($array_my_values[40]) ? $array_my_values[40] : '';
2077 + $skip_spin = isset($array_my_values[41]) ? $array_my_values[41] : '';
2078 + $skip_translate = isset($array_my_values[42]) ? $array_my_values[42] : '';
2079 + $default_lang = isset($array_my_values[43]) ? $array_my_values[43] : '';
2080 + $ai_titles = isset($array_my_values[44]) ? $array_my_values[44] : '';
2081 + $post_sections_list= isset($array_my_values[45]) ? $array_my_values[45] : '';
2082 + $content_language = isset($array_my_values[46]) ? $array_my_values[46] : '';
2083 + $writing_style = isset($array_my_values[47]) ? $array_my_values[47] : '';
2084 + $writing_tone = isset($array_my_values[48]) ? $array_my_values[48] : '';
2085 + $title_prompt = isset($array_my_values[49]) ? $array_my_values[49] : '';
2086 + $sections_prompt = isset($array_my_values[50]) ? $array_my_values[50] : '';
2087 + $content_prompt = isset($array_my_values[51]) ? $array_my_values[51] : '';
2088 + $excerpt_prompt = isset($array_my_values[52]) ? $array_my_values[52] : '';
2089 + $section_count = isset($array_my_values[53]) ? $array_my_values[53] : '';
2090 + $paragraph_count = isset($array_my_values[54]) ? $array_my_values[54] : '';
2091 + $topic_title_model = isset($array_my_values[55]) ? $array_my_values[55] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2092 + $topic_sections_model = isset($array_my_values[56]) ? $array_my_values[56] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2093 + $topic_content_model = isset($array_my_values[57]) ? $array_my_values[57] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2094 + $topic_excerpt_model = isset($array_my_values[58]) ? $array_my_values[58] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2095 + $single_content_call = isset($array_my_values[59]) ? $array_my_values[59] : '';
2096 + $intro_prompt = isset($array_my_values[60]) ? $array_my_values[60] : '';
2097 + $topic_intro_model = isset($array_my_values[61]) ? $array_my_values[61] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2098 + $outro_prompt = isset($array_my_values[62]) ? $array_my_values[62] : '';
2099 + $topic_outro_model = isset($array_my_values[63]) ? $array_my_values[63] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2100 + $topic_images = isset($array_my_values[64]) ? $array_my_values[64] : '';
2101 + $sections_role = isset($array_my_values[65]) ? $array_my_values[65] : '';
2102 + $topic_videos = isset($array_my_values[66]) ? $array_my_values[66] : '';
2103 + $rule_description = isset($array_my_values[67]) ? $array_my_values[67] : '';
2104 + $custom_shortcodes = isset($array_my_values[68]) ? $array_my_values[68] : '';
2105 + $strip_by_regex = isset($array_my_values[69]) ? $array_my_values[69] : '';
2106 + $replace_regex = isset($array_my_values[70]) ? $array_my_values[70] : '';
2107 + $strip_by_regex_prompts= isset($array_my_values[71]) ? $array_my_values[71] : '';
2108 + $replace_regex_prompts= isset($array_my_values[72]) ? $array_my_values[72] : '';
2109 + $run_regex_on = isset($array_my_values[73]) ? $array_my_values[73] : '';
2110 + $max_links = isset($array_my_values[74]) ? $array_my_values[74] : '';
2111 + $link_post_types = isset($array_my_values[75]) ? $array_my_values[75] : '';
2112 + $enable_toc = isset($array_my_values[76]) ? $array_my_values[76] : '';
2113 + $title_toc = isset($array_my_values[77]) ? $array_my_values[77] : '';
2114 + $qa_prompt = isset($array_my_values[78]) ? $array_my_values[78] : '';
2115 + $topic_qa_model = isset($array_my_values[79]) ? $array_my_values[79] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2116 + $enable_qa = isset($array_my_values[80]) ? $array_my_values[80] : '';
2117 + $title_qa = isset($array_my_values[81]) ? $array_my_values[81] : '';
2118 + $title_outro = isset($array_my_values[82]) ? $array_my_values[82] : '';
2119 + $link_type = isset($array_my_values[83]) ? $array_my_values[83] : 'internal';
2120 + $link_list = isset($array_my_values[84]) ? $array_my_values[84] : '';
2121 + $skip_inexist = isset($array_my_values[85]) ? $array_my_values[85] : '';
2122 + $max_caption = isset($array_my_values[86]) ? $array_my_values[86] : '';
2123 + $global_prepend = isset($array_my_values[87]) ? $array_my_values[87] : '';
2124 + $global_append = isset($array_my_values[88]) ? $array_my_values[88] : '';
2125 + $search_query_repetition = isset($array_my_values[89]) ? $array_my_values[89] : '';
2126 + $img_all_headings = isset($array_my_values[90]) ? $array_my_values[90] : '';
2127 + $heading_img_location = isset($array_my_values[91]) ? $array_my_values[91] : 'bottom';
2128 + $days_no_run = isset($array_my_values[92]) ? $array_my_values[92] : '';
2129 + $no_random = isset($array_my_values[93]) ? $array_my_values[93] : '';
2130 + $overwrite_existing= isset($array_my_values[94]) ? $array_my_values[94] : '';
2131 + $link_nofollow = isset($array_my_values[95]) ? $array_my_values[95] : '';
2132 + $parent_id = isset($array_my_values[96]) ? $array_my_values[96] : '';
2133 + $rule_unique_id = isset($array_my_values[97]) ? $array_my_values[97] : '';
2134 + $image_model = isset($array_my_values[98]) ? $array_my_values[98] : '';
2135 + $assistant_id = isset($array_my_values[99]) ? $array_my_values[99] : '';
2136 + $found = 1;
2137 + break;
2138 + }
2139 + $cont = $cont + 1;
2140 + }
2141 + } else {
2142 + aiomatic_log_to_file('No rules found for aiomatic_youtube_list!');
2143 + if($auto == 1)
2144 + {
2145 + aiomatic_clearFromList($param, $type);
2146 + }
2147 + return 'fail';
2148 + }
2149 + }
2150 + elseif($type == 2)
2151 + {
2152 + if($rules === null)
2153 + {
2154 + $GLOBALS['wp_object_cache']->delete('aiomatic_amazon_list', 'options');
2155 + if (!get_option('aiomatic_amazon_list')) {
2156 + $rules = array();
2157 + } else {
2158 + $rules = get_option('aiomatic_amazon_list');
2159 + }
2160 + }
2161 + if (!empty($rules)) {
2162 + foreach ($rules as $request => $bundle[]) {
2163 + if ($cont == $param) {
2164 + $bundle_values = array_values($bundle);
2165 + $myValues = $bundle_values[$cont];
2166 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2167 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2168 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2169 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2170 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2171 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
2172 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
2173 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
2174 + $item_create_tag = isset($array_my_values[7]) ? $array_my_values[7] : '';
2175 + $default_category = isset($array_my_values[8]) ? $array_my_values[8] : '';
2176 + $auto_categories = isset($array_my_values[9]) ? $array_my_values[9] : '';
2177 + $can_create_tag = isset($array_my_values[10]) ? $array_my_values[10] : '';
2178 + $enable_comments = isset($array_my_values[11]) ? $array_my_values[11] : '';
2179 + $image_url = isset($array_my_values[12]) ? $array_my_values[12] : '';
2180 + $amazon_keyword = isset($array_my_values[13]) ? $array_my_values[13] : '';
2181 + $enable_pingback = isset($array_my_values[14]) ? $array_my_values[14] : '';
2182 + $post_format = isset($array_my_values[15]) ? $array_my_values[15] : '';
2183 + $custom_fields = isset($array_my_values[16]) ? $array_my_values[16] : '';
2184 + $custom_tax = isset($array_my_values[17]) ? $array_my_values[17] : '';
2185 + $temperature = isset($array_my_values[18]) ? $array_my_values[18] : '';
2186 + $top_p = isset($array_my_values[19]) ? $array_my_values[19] : '';
2187 + $presence_penalty = isset($array_my_values[20]) ? $array_my_values[20] : '';
2188 + $frequency_penalty = isset($array_my_values[21]) ? $array_my_values[21] : '';
2189 + $royalty_free = isset($array_my_values[22]) ? $array_my_values[22] : '';
2190 + $max_tokens = isset($array_my_values[23]) ? $array_my_values[23] : '';
2191 + $max_seed_tokens = isset($array_my_values[24]) ? $array_my_values[24] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2192 + $max_continue_tokens= isset($array_my_values[25]) ? $array_my_values[25] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2193 + $post_prepend = isset($array_my_values[26]) ? $array_my_values[26] : '';
2194 + $post_append = isset($array_my_values[27]) ? $array_my_values[27] : '';
2195 + $enable_ai_images = isset($array_my_values[28]) ? $array_my_values[28] : '';
2196 + $ai_command_image = isset($array_my_values[29]) ? $array_my_values[29] : '';
2197 + $image_size = isset($array_my_values[30]) ? $array_my_values[30] : '';
2198 + $wpml_lang = isset($array_my_values[31]) ? $array_my_values[31] : '';
2199 + $remove_default = isset($array_my_values[32]) ? $array_my_values[32] : '';
2200 + $strip_title = isset($array_my_values[33]) ? $array_my_values[33] : '';
2201 + $title_once = isset($array_my_values[34]) ? $array_my_values[34] : '';
2202 + $category_model = isset($array_my_values[35]) ? $array_my_values[35] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2203 + $category_ai_command= isset($array_my_values[36]) ? $array_my_values[36] : '';
2204 + $tag_model = isset($array_my_values[37]) ? $array_my_values[37] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2205 + $tag_ai_command = isset($array_my_values[38]) ? $array_my_values[38] : '';
2206 + $min_time = isset($array_my_values[39]) ? $array_my_values[39] : '';
2207 + $max_time = isset($array_my_values[40]) ? $array_my_values[40] : '';
2208 + $skip_spin = isset($array_my_values[41]) ? $array_my_values[41] : '';
2209 + $skip_translate = isset($array_my_values[42]) ? $array_my_values[42] : '';
2210 + $affiliate_id = isset($array_my_values[43]) ? $array_my_values[43] : '';
2211 + $first_hand = isset($array_my_values[44]) ? $array_my_values[44] : '';
2212 + $content_language = isset($array_my_values[45]) ? $array_my_values[45] : '';
2213 + $writing_style = isset($array_my_values[46]) ? $array_my_values[46] : '';
2214 + $writing_tone = isset($array_my_values[47]) ? $array_my_values[47] : '';
2215 + $title_prompt = isset($array_my_values[48]) ? $array_my_values[48] : '';
2216 + $content_prompt = isset($array_my_values[49]) ? $array_my_values[49] : '';
2217 + $excerpt_prompt = isset($array_my_values[50]) ? $array_my_values[50] : '';
2218 + $max_products = isset($array_my_values[51]) ? $array_my_values[51] : '';
2219 + $paragraph_count = isset($array_my_values[52]) ? $array_my_values[52] : '';
2220 + $topic_title_model = isset($array_my_values[53]) ? $array_my_values[53] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2221 + $topic_content_model = isset($array_my_values[54]) ? $array_my_values[54] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2222 + $topic_excerpt_model = isset($array_my_values[55]) ? $array_my_values[55] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2223 + $intro_prompt = isset($array_my_values[56]) ? $array_my_values[56] : '';
2224 + $topic_intro_model = isset($array_my_values[57]) ? $array_my_values[57] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2225 + $outro_prompt = isset($array_my_values[58]) ? $array_my_values[58] : '';
2226 + $topic_outro_model = isset($array_my_values[59]) ? $array_my_values[59] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2227 + $topic_images = isset($array_my_values[60]) ? $array_my_values[60] : '';
2228 + $sections_role = isset($array_my_values[61]) ? $array_my_values[61] : '';
2229 + $topic_videos = isset($array_my_values[62]) ? $array_my_values[62] : '';
2230 + $rule_description = isset($array_my_values[63]) ? $array_my_values[63] : '';
2231 + $custom_shortcodes = isset($array_my_values[64]) ? $array_my_values[64] : '';
2232 + $strip_by_regex = isset($array_my_values[65]) ? $array_my_values[65] : '';
2233 + $replace_regex = isset($array_my_values[66]) ? $array_my_values[66] : '';
2234 + $strip_by_regex_prompts= isset($array_my_values[67]) ? $array_my_values[67] : '';
2235 + $replace_regex_prompts= isset($array_my_values[68]) ? $array_my_values[68] : '';
2236 + $run_regex_on = isset($array_my_values[69]) ? $array_my_values[69] : '';
2237 + $max_links = isset($array_my_values[70]) ? $array_my_values[70] : '';
2238 + $link_post_types = isset($array_my_values[71]) ? $array_my_values[71] : '';
2239 + $enable_toc = isset($array_my_values[72]) ? $array_my_values[72] : '';
2240 + $title_toc = isset($array_my_values[73]) ? $array_my_values[73] : '';
2241 + $qa_prompt = isset($array_my_values[74]) ? $array_my_values[74] : '';
2242 + $topic_qa_model = isset($array_my_values[75]) ? $array_my_values[75] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2243 + $enable_qa = isset($array_my_values[76]) ? $array_my_values[76] : '';
2244 + $title_qa = isset($array_my_values[77]) ? $array_my_values[77] : '';
2245 + $title_outro = isset($array_my_values[78]) ? $array_my_values[78] : '';
2246 + $link_type = isset($array_my_values[79]) ? $array_my_values[79] : 'internal';
2247 + $link_list = isset($array_my_values[80]) ? $array_my_values[80] : '';
2248 + $skip_inexist = isset($array_my_values[81]) ? $array_my_values[81] : '';
2249 + $target_country = isset($array_my_values[82]) ? $array_my_values[82] : '';
2250 + $min_price = isset($array_my_values[83]) ? $array_my_values[83] : '';
2251 + $max_price = isset($array_my_values[84]) ? $array_my_values[84] : '';
2252 + $sort_results = isset($array_my_values[85]) ? $array_my_values[85] : '';
2253 + $shuffle_products = isset($array_my_values[86]) ? $array_my_values[86] : '';
2254 + $global_prepend = isset($array_my_values[87]) ? $array_my_values[87] : '';
2255 + $global_append = isset($array_my_values[88]) ? $array_my_values[88] : '';
2256 + $search_query_repetition = isset($array_my_values[89]) ? $array_my_values[89] : '';
2257 + $days_no_run = isset($array_my_values[90]) ? $array_my_values[90] : '';
2258 + $overwrite_existing= isset($array_my_values[91]) ? $array_my_values[91] : '';
2259 + $link_nofollow = isset($array_my_values[92]) ? $array_my_values[92] : '';
2260 + $parent_id = isset($array_my_values[93]) ? $array_my_values[93] : '';
2261 + $rule_unique_id = isset($array_my_values[94]) ? $array_my_values[94] : '';
2262 + $no_headlink = isset($array_my_values[95]) ? $array_my_values[95] : '';
2263 + $enable_table = isset($array_my_values[96]) ? $array_my_values[96] : '';
2264 + $table_prompt = isset($array_my_values[97]) ? $array_my_values[97] : '';
2265 + $topic_table_model= isset($array_my_values[98]) ? $array_my_values[98] : '';
2266 + $image_model = isset($array_my_values[99]) ? $array_my_values[99] : '';
2267 + $assistant_id = isset($array_my_values[100]) ? $array_my_values[100] : '';
2268 + $found = 1;
2269 + break;
2270 + }
2271 + $cont = $cont + 1;
2272 + }
2273 + } else {
2274 + aiomatic_log_to_file('No rules found for aiomatic_amazon_list!');
2275 + if($auto == 1)
2276 + {
2277 + aiomatic_clearFromList($param, $type);
2278 + }
2279 + return 'fail';
2280 + }
2281 + }
2282 + elseif($type == 3)
2283 + {
2284 + if($rules === null)
2285 + {
2286 + $GLOBALS['wp_object_cache']->delete('aiomatic_review_list', 'options');
2287 + if (!get_option('aiomatic_review_list')) {
2288 + $rules = array();
2289 + } else {
2290 + $rules = get_option('aiomatic_review_list');
2291 + }
2292 + }
2293 + if (!empty($rules)) {
2294 + foreach ($rules as $request => $bundle[]) {
2295 + if ($cont == $param) {
2296 + $bundle_values = array_values($bundle);
2297 + $myValues = $bundle_values[$cont];
2298 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2299 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2300 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2301 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2302 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2303 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
2304 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
2305 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
2306 + $item_create_tag = isset($array_my_values[7]) ? $array_my_values[7] : '';
2307 + $default_category = isset($array_my_values[8]) ? $array_my_values[8] : '';
2308 + $auto_categories = isset($array_my_values[9]) ? $array_my_values[9] : '';
2309 + $can_create_tag = isset($array_my_values[10]) ? $array_my_values[10] : '';
2310 + $enable_comments = isset($array_my_values[11]) ? $array_my_values[11] : '';
2311 + $image_url = isset($array_my_values[12]) ? $array_my_values[12] : '';
2312 + $amazon_keyword = isset($array_my_values[13]) ? $array_my_values[13] : '';
2313 + $enable_pingback = isset($array_my_values[14]) ? $array_my_values[14] : '';
2314 + $post_format = isset($array_my_values[15]) ? $array_my_values[15] : '';
2315 + $custom_fields = isset($array_my_values[16]) ? $array_my_values[16] : '';
2316 + $custom_tax = isset($array_my_values[17]) ? $array_my_values[17] : '';
2317 + $temperature = isset($array_my_values[18]) ? $array_my_values[18] : '';
2318 + $top_p = isset($array_my_values[19]) ? $array_my_values[19] : '';
2319 + $presence_penalty = isset($array_my_values[20]) ? $array_my_values[20] : '';
2320 + $frequency_penalty = isset($array_my_values[21]) ? $array_my_values[21] : '';
2321 + $royalty_free = isset($array_my_values[22]) ? $array_my_values[22] : '';
2322 + $max_tokens = isset($array_my_values[23]) ? $array_my_values[23] : '';
2323 + $max_seed_tokens = isset($array_my_values[24]) ? $array_my_values[24] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2324 + $max_continue_tokens= isset($array_my_values[25]) ? $array_my_values[25] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2325 + $post_prepend = isset($array_my_values[26]) ? $array_my_values[26] : '';
2326 + $post_append = isset($array_my_values[27]) ? $array_my_values[27] : '';
2327 + $enable_ai_images = isset($array_my_values[28]) ? $array_my_values[28] : '';
2328 + $ai_command_image = isset($array_my_values[29]) ? $array_my_values[29] : '';
2329 + $image_size = isset($array_my_values[30]) ? $array_my_values[30] : '';
2330 + $wpml_lang = isset($array_my_values[31]) ? $array_my_values[31] : '';
2331 + $remove_default = isset($array_my_values[32]) ? $array_my_values[32] : '';
2332 + $strip_title = isset($array_my_values[33]) ? $array_my_values[33] : '';
2333 + $title_once = isset($array_my_values[34]) ? $array_my_values[34] : '';
2334 + $category_model = isset($array_my_values[35]) ? $array_my_values[35] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2335 + $category_ai_command= isset($array_my_values[36]) ? $array_my_values[36] : '';
2336 + $tag_model = isset($array_my_values[37]) ? $array_my_values[37] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2337 + $tag_ai_command = isset($array_my_values[38]) ? $array_my_values[38] : '';
2338 + $min_time = isset($array_my_values[39]) ? $array_my_values[39] : '';
2339 + $max_time = isset($array_my_values[40]) ? $array_my_values[40] : '';
2340 + $skip_spin = isset($array_my_values[41]) ? $array_my_values[41] : '';
2341 + $skip_translate = isset($array_my_values[42]) ? $array_my_values[42] : '';
2342 + $affiliate_id = isset($array_my_values[43]) ? $array_my_values[43] : '';
2343 + $content_language = isset($array_my_values[44]) ? $array_my_values[44] : '';
2344 + $writing_style = isset($array_my_values[45]) ? $array_my_values[45] : '';
2345 + $writing_tone = isset($array_my_values[46]) ? $array_my_values[46] : '';
2346 + $title_prompt = isset($array_my_values[47]) ? $array_my_values[47] : '';
2347 + $content_prompt = isset($array_my_values[48]) ? $array_my_values[48] : '';
2348 + $excerpt_prompt = isset($array_my_values[49]) ? $array_my_values[49] : '';
2349 + $paragraph_count = isset($array_my_values[50]) ? $array_my_values[50] : '';
2350 + $topic_title_model = isset($array_my_values[51]) ? $array_my_values[51] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2351 + $topic_content_model = isset($array_my_values[52]) ? $array_my_values[52] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2352 + $topic_excerpt_model = isset($array_my_values[53]) ? $array_my_values[53] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2353 + $intro_prompt = isset($array_my_values[54]) ? $array_my_values[54] : '';
2354 + $topic_intro_model = isset($array_my_values[55]) ? $array_my_values[55] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2355 + $outro_prompt = isset($array_my_values[56]) ? $array_my_values[56] : '';
2356 + $topic_outro_model = isset($array_my_values[57]) ? $array_my_values[57] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2357 + $topic_images = isset($array_my_values[58]) ? $array_my_values[58] : '';
2358 + $sections_role = isset($array_my_values[59]) ? $array_my_values[59] : '';
2359 + $topic_videos = isset($array_my_values[60]) ? $array_my_values[60] : '';
2360 + $rule_description = isset($array_my_values[61]) ? $array_my_values[61] : '';
2361 + $custom_shortcodes = isset($array_my_values[62]) ? $array_my_values[62] : '';
2362 + $strip_by_regex = isset($array_my_values[63]) ? $array_my_values[63] : '';
2363 + $replace_regex = isset($array_my_values[64]) ? $array_my_values[64] : '';
2364 + $strip_by_regex_prompts= isset($array_my_values[65]) ? $array_my_values[65] : '';
2365 + $replace_regex_prompts= isset($array_my_values[66]) ? $array_my_values[66] : '';
2366 + $run_regex_on = isset($array_my_values[67]) ? $array_my_values[67] : '';
2367 + $max_links = isset($array_my_values[68]) ? $array_my_values[68] : '';
2368 + $link_post_types = isset($array_my_values[69]) ? $array_my_values[69] : '';
2369 + $enable_toc = isset($array_my_values[70]) ? $array_my_values[70] : '';
2370 + $title_toc = isset($array_my_values[71]) ? $array_my_values[71] : '';
2371 + $qa_prompt = isset($array_my_values[72]) ? $array_my_values[72] : '';
2372 + $topic_qa_model = isset($array_my_values[73]) ? $array_my_values[73] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2373 + $enable_qa = isset($array_my_values[74]) ? $array_my_values[74] : '';
2374 + $title_qa = isset($array_my_values[75]) ? $array_my_values[75] : '';
2375 + $title_outro = isset($array_my_values[76]) ? $array_my_values[76] : '';
2376 + $link_type = isset($array_my_values[77]) ? $array_my_values[77] : 'internal';
2377 + $link_list = isset($array_my_values[78]) ? $array_my_values[78] : '';
2378 + $skip_inexist = isset($array_my_values[79]) ? $array_my_values[79] : '';
2379 + $target_country = isset($array_my_values[80]) ? $array_my_values[80] : '';
2380 + $global_prepend = isset($array_my_values[81]) ? $array_my_values[81] : '';
2381 + $global_append = isset($array_my_values[82]) ? $array_my_values[82] : '';
2382 + $search_query_repetition = isset($array_my_values[83]) ? $array_my_values[83] : '';
2383 + $days_no_run = isset($array_my_values[84]) ? $array_my_values[84] : '';
2384 + $overwrite_existing= isset($array_my_values[85]) ? $array_my_values[85] : '';
2385 + $link_nofollow = isset($array_my_values[86]) ? $array_my_values[86] : '';
2386 + $parent_id = isset($array_my_values[87]) ? $array_my_values[87] : '';
2387 + $rule_unique_id = isset($array_my_values[88]) ? $array_my_values[88] : '';
2388 + $point_of_view = isset($array_my_values[89]) ? $array_my_values[89] : '';
2389 + $sections_prompt = isset($array_my_values[90]) ? $array_my_values[90] : '';
2390 + $topic_sections_model= isset($array_my_values[91]) ? $array_my_values[91] : '';
2391 + $post_sections_list= isset($array_my_values[92]) ? $array_my_values[92] : '';
2392 + $section_count = isset($array_my_values[93]) ? $array_my_values[93] : '';
2393 + $proscons_prompt = isset($array_my_values[94]) ? $array_my_values[94] : '';
2394 + $topic_proscons_model= isset($array_my_values[95]) ? $array_my_values[95] : '';
2395 + $title_proscons = isset($array_my_values[96]) ? $array_my_values[96] : '';
2396 + $enable_proscons = isset($array_my_values[97]) ? $array_my_values[97] : '';
2397 + $title_reviews = isset($array_my_values[98]) ? $array_my_values[98] : '';
2398 + $enable_reviews = isset($array_my_values[99]) ? $array_my_values[99] : '';
2399 + $reviews_prompt = isset($array_my_values[100]) ? $array_my_values[100] : '';
2400 + $topic_reviews_model= isset($array_my_values[101]) ? $array_my_values[101] : '';
2401 + $no_headlink = isset($array_my_values[102]) ? $array_my_values[102] : '';
2402 + $image_model = isset($array_my_values[103]) ? $array_my_values[103] : '';
2403 + $assistant_id = isset($array_my_values[104]) ? $array_my_values[104] : '';
2404 + $found = 1;
2405 + break;
2406 + }
2407 + $cont = $cont + 1;
2408 + }
2409 + } else {
2410 + aiomatic_log_to_file('No rules found for aiomatic_review_list!');
2411 + if($auto == 1)
2412 + {
2413 + aiomatic_clearFromList($param, $type);
2414 + }
2415 + return 'fail';
2416 + }
2417 + }
2418 + elseif($type == 4)
2419 + {
2420 + if($rules === null)
2421 + {
2422 + $GLOBALS['wp_object_cache']->delete('aiomatic_csv_list', 'options');
2423 + if (!get_option('aiomatic_csv_list')) {
2424 + $rules = array();
2425 + } else {
2426 + $rules = get_option('aiomatic_csv_list');
2427 + }
2428 + }
2429 + if (!empty($rules)) {
2430 + foreach ($rules as $request => $bundle[]) {
2431 + if ($cont == $param) {
2432 + $bundle_values = array_values($bundle);
2433 + $myValues = $bundle_values[$cont];
2434 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2435 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2436 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2437 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2438 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2439 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
2440 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
2441 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
2442 + $enable_comments = isset($array_my_values[7]) ? $array_my_values[7] : '';
2443 + $post_title = isset($array_my_values[8]) ? htmlspecialchars_decode($array_my_values[8]) : '';
2444 + $enable_pingback = isset($array_my_values[9]) ? $array_my_values[9] : '';
2445 + $post_format = isset($array_my_values[10]) ? $array_my_values[10] : '';
2446 + $custom_fields = isset($array_my_values[11]) ? $array_my_values[11] : '';
2447 + $custom_tax = isset($array_my_values[12]) ? $array_my_values[12] : '';
2448 + $wpml_lang = isset($array_my_values[13]) ? $array_my_values[13] : '';
2449 + $strip_title = isset($array_my_values[14]) ? $array_my_values[14] : '';
2450 + $title_once = isset($array_my_values[15]) ? $array_my_values[15] : '';
2451 + $min_time = isset($array_my_values[16]) ? $array_my_values[16] : '';
2452 + $max_time = isset($array_my_values[17]) ? $array_my_values[17] : '';
2453 + $skip_spin = isset($array_my_values[18]) ? $array_my_values[18] : '';
2454 + $skip_translate = isset($array_my_values[19]) ? $array_my_values[19] : '';
2455 + $rule_description = isset($array_my_values[20]) ? $array_my_values[20] : '';
2456 + $strip_by_regex = isset($array_my_values[21]) ? $array_my_values[21] : '';
2457 + $replace_regex = isset($array_my_values[22]) ? $array_my_values[22] : '';
2458 + $max_links = isset($array_my_values[23]) ? $array_my_values[23] : '';
2459 + $link_post_types = isset($array_my_values[24]) ? $array_my_values[24] : '';
2460 + $link_type = isset($array_my_values[25]) ? $array_my_values[25] : 'internal';
2461 + $link_list = isset($array_my_values[26]) ? $array_my_values[26] : '';
2462 + $days_no_run = isset($array_my_values[27]) ? $array_my_values[27] : '';
2463 + $overwrite_existing= isset($array_my_values[28]) ? $array_my_values[28] : '';
2464 + $link_nofollow = isset($array_my_values[29]) ? $array_my_values[29] : '';
2465 + $parent_id = isset($array_my_values[30]) ? $array_my_values[30] : '';
2466 + $rule_unique_id = isset($array_my_values[31]) ? $array_my_values[31] : '';
2467 + $remove_default = isset($array_my_values[32]) ? $array_my_values[32] : '';
2468 + $random_order = isset($array_my_values[33]) ? $array_my_values[33] : '';
2469 + $csv_separator = isset($array_my_values[34]) ? $array_my_values[34] : '';
2470 + $found = 1;
2471 + break;
2472 + }
2473 + $cont = $cont + 1;
2474 + }
2475 + } else {
2476 + aiomatic_log_to_file('No rules found for aiomatic_csv_list!');
2477 + if($auto == 1)
2478 + {
2479 + aiomatic_clearFromList($param, $type);
2480 + }
2481 + return 'fail';
2482 + }
2483 + }
2484 + elseif($type == 5)
2485 + {
2486 + if($rules === null)
2487 + {
2488 + $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
2489 + if (!get_option('aiomatic_omni_list')) {
2490 + $rules = array();
2491 + } else {
2492 + $rules = get_option('aiomatic_omni_list');
2493 + }
2494 + }
2495 + if (!empty($rules)) {
2496 + foreach ($rules as $request => $bundle[]) {
2497 + if ($cont == $param) {
2498 + $bundle_values = array_values($bundle);
2499 + $myValues = $bundle_values[$cont];
2500 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2501 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2502 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2503 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2504 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2505 + $main_keywords = isset($array_my_values[4]) ? $array_my_values[4] : '';
2506 + $title_once = isset($array_my_values[5]) ? $array_my_values[5] : '';
2507 + $rule_description = isset($array_my_values[6]) ? $array_my_values[6] : '';
2508 + $rule_unique_id = isset($array_my_values[7]) ? $array_my_values[7] : '';
2509 + $sortable_cards = isset($array_my_values[8]) ? $array_my_values[8] : '';
2510 + $more_keywords = isset($array_my_values[9]) ? $array_my_values[9] : '';
2511 + $days_no_run = isset($array_my_values[10]) ? $array_my_values[10] : '';
2512 + $found = 1;
2513 + break;
2514 + }
2515 + $cont = $cont + 1;
2516 + }
2517 + } else {
2518 + aiomatic_log_to_file('No rules found for aiomatic_omni_list!');
2519 + if($auto == 1)
2520 + {
2521 + aiomatic_clearFromList($param, $type);
2522 + }
2523 + return 'fail';
2524 + }
2525 + }
2526 + elseif($type == 6)
2527 + {
2528 + if($rules === null)
2529 + {
2530 + $GLOBALS['wp_object_cache']->delete('aiomatic_listicle_list', 'options');
2531 + if (!get_option('aiomatic_listicle_list')) {
2532 + $rules = array();
2533 + } else {
2534 + $rules = get_option('aiomatic_listicle_list');
2535 + }
2536 + }
2537 + if (!empty($rules)) {
2538 + foreach ($rules as $request => $bundle[]) {
2539 + if ($cont == $param) {
2540 + $bundle_values = array_values($bundle);
2541 + $myValues = $bundle_values[$cont];
2542 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2543 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2544 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2545 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2546 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2547 + $post_status = isset($array_my_values[4]) ? $array_my_values[4] : '';
2548 + $post_type = isset($array_my_values[5]) ? $array_my_values[5] : '';
2549 + $post_user_name = isset($array_my_values[6]) ? $array_my_values[6] : '';
2550 + $item_create_tag = isset($array_my_values[7]) ? $array_my_values[7] : '';
2551 + $default_category = isset($array_my_values[8]) ? $array_my_values[8] : '';
2552 + $auto_categories = isset($array_my_values[9]) ? $array_my_values[9] : '';
2553 + $can_create_tag = isset($array_my_values[10]) ? $array_my_values[10] : '';
2554 + $enable_comments = isset($array_my_values[11]) ? $array_my_values[11] : '';
2555 + $image_url = isset($array_my_values[12]) ? $array_my_values[12] : '';
2556 + $post_title = isset($array_my_values[13]) ? htmlspecialchars_decode($array_my_values[13]) : '';
2557 + $enable_pingback = isset($array_my_values[14]) ? $array_my_values[14] : '';
2558 + $post_format = isset($array_my_values[15]) ? $array_my_values[15] : '';
2559 + $custom_fields = isset($array_my_values[16]) ? $array_my_values[16] : '';
2560 + $custom_tax = isset($array_my_values[17]) ? $array_my_values[17] : '';
2561 + $temperature = isset($array_my_values[18]) ? $array_my_values[18] : '';
2562 + $top_p = isset($array_my_values[19]) ? $array_my_values[19] : '';
2563 + $presence_penalty = isset($array_my_values[20]) ? $array_my_values[20] : '';
2564 + $frequency_penalty = isset($array_my_values[21]) ? $array_my_values[21] : '';
2565 + $royalty_free = isset($array_my_values[22]) ? $array_my_values[22] : '';
2566 + $max_tokens = isset($array_my_values[23]) ? $array_my_values[23] : '';
2567 + $max_seed_tokens = isset($array_my_values[24]) ? $array_my_values[24] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2568 + $post_prepend = isset($array_my_values[25]) ? $array_my_values[25] : '';
2569 + $post_append = isset($array_my_values[26]) ? $array_my_values[26] : '';
2570 + $enable_ai_images = isset($array_my_values[27]) ? $array_my_values[27] : '';
2571 + $ai_command_image = isset($array_my_values[28]) ? $array_my_values[28] : '';
2572 + $image_size = isset($array_my_values[29]) ? $array_my_values[29] : '';
2573 + $wpml_lang = isset($array_my_values[30]) ? $array_my_values[30] : '';
2574 + $remove_default = isset($array_my_values[31]) ? $array_my_values[31] : '';
2575 + $strip_title = isset($array_my_values[32]) ? $array_my_values[32] : '';
2576 + $title_once = isset($array_my_values[33]) ? $array_my_values[33] : '';
2577 + $category_model = isset($array_my_values[34]) ? $array_my_values[34] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2578 + $category_ai_command= isset($array_my_values[35]) ? $array_my_values[35] : '';
2579 + $tag_model = isset($array_my_values[36]) ? $array_my_values[36] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2580 + $tag_ai_command = isset($array_my_values[37]) ? $array_my_values[37] : '';
2581 + $min_time = isset($array_my_values[38]) ? $array_my_values[38] : '';
2582 + $max_time = isset($array_my_values[39]) ? $array_my_values[39] : '';
2583 + $skip_spin = isset($array_my_values[40]) ? $array_my_values[40] : '';
2584 + $skip_translate = isset($array_my_values[41]) ? $array_my_values[41] : '';
2585 + $post_topic_list = isset($array_my_values[42]) ? $array_my_values[42] : '';
2586 + $post_sections_list= isset($array_my_values[43]) ? $array_my_values[43] : '';
2587 + $content_language = isset($array_my_values[44]) ? $array_my_values[44] : '';
2588 + $writing_style = isset($array_my_values[45]) ? $array_my_values[45] : '';
2589 + $writing_tone = isset($array_my_values[46]) ? $array_my_values[46] : '';
2590 + $title_prompt = isset($array_my_values[47]) ? $array_my_values[47] : '';
2591 + $sections_prompt = isset($array_my_values[48]) ? $array_my_values[48] : '';
2592 + $content_prompt = isset($array_my_values[49]) ? $array_my_values[49] : '';
2593 + $excerpt_prompt = isset($array_my_values[50]) ? $array_my_values[50] : '';
2594 + $section_count = isset($array_my_values[51]) ? $array_my_values[51] : '';
2595 + $paragraph_count = isset($array_my_values[52]) ? $array_my_values[52] : '';
2596 + $topic_title_model = isset($array_my_values[53]) ? $array_my_values[53] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2597 + $topic_sections_model = isset($array_my_values[54]) ? $array_my_values[54] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2598 + $topic_content_model = isset($array_my_values[55]) ? $array_my_values[55] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2599 + $topic_excerpt_model = isset($array_my_values[56]) ? $array_my_values[56] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2600 + $single_content_call = isset($array_my_values[57]) ? $array_my_values[57] : '';
2601 + $intro_prompt = isset($array_my_values[58]) ? $array_my_values[58] : '';
2602 + $topic_intro_model = isset($array_my_values[59]) ? $array_my_values[59] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2603 + $outro_prompt = isset($array_my_values[60]) ? $array_my_values[60] : '';
2604 + $topic_outro_model = isset($array_my_values[61]) ? $array_my_values[61] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2605 + $topic_images = isset($array_my_values[62]) ? $array_my_values[62] : '';
2606 + $sections_role = isset($array_my_values[63]) ? $array_my_values[63] : '';
2607 + $topic_videos = isset($array_my_values[64]) ? $array_my_values[64] : '';
2608 + $rule_description = isset($array_my_values[65]) ? $array_my_values[65] : '';
2609 + $custom_shortcodes = isset($array_my_values[66]) ? $array_my_values[66] : '';
2610 + $strip_by_regex = isset($array_my_values[67]) ? $array_my_values[67] : '';
2611 + $replace_regex = isset($array_my_values[68]) ? $array_my_values[68] : '';
2612 + $strip_by_regex_prompts= isset($array_my_values[69]) ? $array_my_values[69] : '';
2613 + $replace_regex_prompts= isset($array_my_values[70]) ? $array_my_values[70] : '';
2614 + $run_regex_on = isset($array_my_values[71]) ? $array_my_values[71] : '';
2615 + $max_links = isset($array_my_values[72]) ? $array_my_values[72] : '';
2616 + $link_post_types = isset($array_my_values[73]) ? $array_my_values[73] : '';
2617 + $enable_toc = isset($array_my_values[74]) ? $array_my_values[74] : '';
2618 + $title_toc = isset($array_my_values[75]) ? $array_my_values[75] : '';
2619 + $qa_prompt = isset($array_my_values[76]) ? $array_my_values[76] : '';
2620 + $topic_qa_model = isset($array_my_values[77]) ? $array_my_values[77] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2621 + $enable_qa = isset($array_my_values[78]) ? $array_my_values[78] : '';
2622 + $title_qa = isset($array_my_values[79]) ? $array_my_values[79] : '';
2623 + $title_outro = isset($array_my_values[80]) ? $array_my_values[80] : '';
2624 + $link_type = isset($array_my_values[81]) ? $array_my_values[81] : 'internal';
2625 + $link_list = isset($array_my_values[82]) ? $array_my_values[82] : '';
2626 + $skip_inexist = isset($array_my_values[83]) ? $array_my_values[83] : '';
2627 + $global_prepend = isset($array_my_values[84]) ? $array_my_values[84] : '';
2628 + $global_append = isset($array_my_values[85]) ? $array_my_values[85] : '';
2629 + $search_query_repetition = isset($array_my_values[86]) ? $array_my_values[86] : '';
2630 + $img_all_headings = isset($array_my_values[87]) ? $array_my_values[87] : '';
2631 + $heading_img_location = isset($array_my_values[88]) ? $array_my_values[88] : 'bottom';
2632 + $days_no_run = isset($array_my_values[89]) ? $array_my_values[89] : '';
2633 + $overwrite_existing= isset($array_my_values[90]) ? $array_my_values[90] : '';
2634 + $link_nofollow = isset($array_my_values[91]) ? $array_my_values[91] : '';
2635 + $title_generator_method = isset($array_my_values[92]) ? $array_my_values[92] : '';
2636 + $parent_id = isset($array_my_values[93]) ? $array_my_values[93] : '';
2637 + $rule_unique_id = isset($array_my_values[94]) ? $array_my_values[94] : '';
2638 + $image_model = isset($array_my_values[95]) ? $array_my_values[95] : '';
2639 + $assistant_id = isset($array_my_values[96]) ? $array_my_values[96] : '';
2640 + $found = 1;
2641 + $posting_mode = 'topic';
2642 + break;
2643 + }
2644 + $cont = $cont + 1;
2645 + }
2646 + } else {
2647 + aiomatic_log_to_file('No rules found for aiomatic_listicle_list!');
2648 + if($auto == 1)
2649 + {
2650 + aiomatic_clearFromList($param, $type);
2651 + }
2652 + return 'fail';
2653 + }
2654 + }
2655 + elseif($type == 7)
2656 + {
2657 + if($rules === null)
2658 + {
2659 + $GLOBALS['wp_object_cache']->delete('aiomatic_forum_list', 'options');
2660 + if (!get_option('aiomatic_forum_list')) {
2661 + $rules = array();
2662 + } else {
2663 + $rules = get_option('aiomatic_forum_list');
2664 + }
2665 + }
2666 + if (!empty($rules)) {
2667 + foreach ($rules as $request => $bundle[]) {
2668 + if ($cont == $param) {
2669 + $bundle_values = array_values($bundle);
2670 + $myValues = $bundle_values[$cont];
2671 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2672 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2673 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2674 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2675 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2676 + $post_user_name = isset($array_my_values[4]) ? $array_my_values[4] : '';
2677 + $post_title = isset($array_my_values[5]) ? htmlspecialchars_decode($array_my_values[5]) : '';
2678 + $min_char = isset($array_my_values[6]) ? $array_my_values[6] : '';
2679 + $temperature = isset($array_my_values[7]) ? $array_my_values[7] : '';
2680 + $top_p = isset($array_my_values[8]) ? $array_my_values[8] : '';
2681 + $presence_penalty = isset($array_my_values[9]) ? $array_my_values[9] : '';
2682 + $frequency_penalty = isset($array_my_values[10]) ? $array_my_values[10] : '';
2683 + $ai_command = isset($array_my_values[11]) ? $array_my_values[11] : '';
2684 + $max_tokens = isset($array_my_values[12]) ? $array_my_values[12] : '';
2685 + $max_seed_tokens = isset($array_my_values[13]) ? $array_my_values[13] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2686 + $model = isset($array_my_values[14]) ? $array_my_values[14] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2687 + $post_prepend = isset($array_my_values[15]) ? $array_my_values[15] : '';
2688 + $post_append = isset($array_my_values[16]) ? $array_my_values[16] : '';
2689 + $title_model = isset($array_my_values[17]) ? $array_my_values[17] : aiomatic_get_default_model_name($aiomatic_Main_Settings);
2690 + $title_ai_command = isset($array_my_values[18]) ? $array_my_values[18] : '';
2691 + $strip_title = isset($array_my_values[19]) ? $array_my_values[19] : '';
2692 + $min_time = isset($array_my_values[20]) ? $array_my_values[20] : '';
2693 + $max_time = isset($array_my_values[21]) ? $array_my_values[21] : '';
2694 + $skip_spin = isset($array_my_values[22]) ? $array_my_values[22] : '';
2695 + $skip_translate = isset($array_my_values[23]) ? $array_my_values[23] : '';
2696 + $title_source = isset($array_my_values[24]) ? $array_my_values[24] : '';
2697 + $selected_forum = isset($array_my_values[25]) ? $array_my_values[25] : 0;
2698 + $rule_description = isset($array_my_values[26]) ? $array_my_values[26] : '';
2699 + $custom_shortcodes = isset($array_my_values[27]) ? $array_my_values[27] : '';
2700 + $strip_by_regex = isset($array_my_values[28]) ? $array_my_values[28] : '';
2701 + $replace_regex = isset($array_my_values[29]) ? $array_my_values[29] : '';
2702 + $link_post_types = isset($array_my_values[30]) ? $array_my_values[30] : '';
2703 + $link_type = isset($array_my_values[31]) ? $array_my_values[31] : 'internal';
2704 + $link_list = isset($array_my_values[32]) ? $array_my_values[32] : '';
2705 + $global_prepend = isset($array_my_values[33]) ? $array_my_values[33] : '';
2706 + $global_append = isset($array_my_values[34]) ? $array_my_values[34] : '';
2707 + $overwrite_existing= isset($array_my_values[35]) ? $array_my_values[35] : '';
2708 + $link_nofollow = isset($array_my_values[36]) ? $array_my_values[36] : '';
2709 + $rule_unique_id = isset($array_my_values[37]) ? $array_my_values[37] : '';
2710 + $assistant_id = isset($array_my_values[38]) ? $array_my_values[38] : '';
2711 + $max_links = isset($array_my_values[39]) ? $array_my_values[39] : '';
2712 + $title_once = isset($array_my_values[40]) ? $array_my_values[40] : '';
2713 + $reply_count = isset($array_my_values[41]) ? $array_my_values[41] : '';
2714 + $reply_model = isset($array_my_values[42]) ? $array_my_values[42] : '';
2715 + $choose_prompt = isset($array_my_values[43]) ? $array_my_values[43] : '';
2716 + $reply_prompt = isset($array_my_values[44]) ? $array_my_values[44] : '';
2717 + $content_language = isset($array_my_values[45]) ? $array_my_values[45] : '';
2718 + $writing_style = isset($array_my_values[46]) ? $array_my_values[46] : '';
2719 + $writing_tone = isset($array_my_values[47]) ? $array_my_values[47] : '';
2720 + $user_role = isset($array_my_values[48]) ? $array_my_values[48] : '';
2721 + $found = 1;
2722 + break;
2723 + }
2724 + $cont = $cont + 1;
2725 + }
2726 + } else {
2727 + aiomatic_log_to_file('No rules found for aiomatic_forum_list!');
2728 + if($auto == 1)
2729 + {
2730 + aiomatic_clearFromList($param, $type);
2731 + }
2732 + return 'fail';
2733 + }
2734 + }
2735 + elseif($type == 8)
2736 + {
2737 + if($rules === null)
2738 + {
2739 + $GLOBALS['wp_object_cache']->delete('aiomatic_reply_list', 'options');
2740 + if (!get_option('aiomatic_reply_list')) {
2741 + $rules = array();
2742 + } else {
2743 + $rules = get_option('aiomatic_reply_list');
2744 + }
2745 + }
2746 + if (!empty($rules)) {
2747 + foreach ($rules as $request => $bundle[]) {
2748 + if ($cont == $param) {
2749 + $bundle_values = array_values($bundle);
2750 + $myValues = $bundle_values[$cont];
2751 + $array_my_values = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
2752 + $schedule = isset($array_my_values[0]) ? $array_my_values[0] : '';
2753 + $active = isset($array_my_values[1]) ? $array_my_values[1] : '';
2754 + $last_run = isset($array_my_values[2]) ? $array_my_values[2] : '';
2755 + $max = isset($array_my_values[3]) ? $array_my_values[3] : '';
2756 + $post_title = isset($array_my_values[4]) ? htmlspecialchars_decode($array_my_values[4]) : '';
2757 + $temperature = isset($array_my_values[5]) ? $array_my_values[5] : '';
2758 + $top_p = isset($array_my_values[6]) ? $array_my_values[6] : '';
2759 + $presence_penalty = isset($array_my_values[7]) ? $array_my_values[7] : '';
2760 + $frequency_penalty = isset($array_my_values[8]) ? $array_my_values[8] : '';
2761 + $max_tokens = isset($array_my_values[9]) ? $array_my_values[9] : '';
2762 + $max_seed_tokens = isset($array_my_values[10]) ? $array_my_values[10] : AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2763 + $post_prepend = isset($array_my_values[11]) ? $array_my_values[11] : '';
2764 + $post_append = isset($array_my_values[12]) ? $array_my_values[12] : '';
2765 + $skip_spin = isset($array_my_values[13]) ? $array_my_values[13] : '';
2766 + $skip_translate = isset($array_my_values[14]) ? $array_my_values[14] : '';
2767 + $selected_forum = isset($array_my_values[15]) ? $array_my_values[15] : 0;
2768 + $rule_description = isset($array_my_values[16]) ? $array_my_values[16] : '';
2769 + $custom_shortcodes = isset($array_my_values[17]) ? $array_my_values[17] : '';
2770 + $strip_by_regex = isset($array_my_values[18]) ? $array_my_values[18] : '';
2771 + $replace_regex = isset($array_my_values[19]) ? $array_my_values[19] : '';
2772 + $global_prepend = isset($array_my_values[20]) ? $array_my_values[20] : '';
2773 + $global_append = isset($array_my_values[21]) ? $array_my_values[21] : '';
2774 + $rule_unique_id = isset($array_my_values[22]) ? $array_my_values[22] : '';
2775 + $assistant_id = isset($array_my_values[23]) ? $array_my_values[23] : '';
2776 + $reply_count = isset($array_my_values[24]) ? $array_my_values[24] : '';
2777 + $reply_model = isset($array_my_values[25]) ? $array_my_values[25] : '';
2778 + $choose_prompt = isset($array_my_values[26]) ? $array_my_values[26] : '';
2779 + $reply_prompt = isset($array_my_values[27]) ? $array_my_values[27] : '';
2780 + $content_language = isset($array_my_values[28]) ? $array_my_values[28] : '';
2781 + $writing_style = isset($array_my_values[29]) ? $array_my_values[29] : '';
2782 + $writing_tone = isset($array_my_values[30]) ? $array_my_values[30] : '';
2783 + $more_replies = isset($array_my_values[31]) ? $array_my_values[31] : '';
2784 + $user_role = isset($array_my_values[32]) ? $array_my_values[32] : '';
2785 + $found = 1;
2786 + break;
2787 + }
2788 + $cont = $cont + 1;
2789 + }
2790 + } else {
2791 + aiomatic_log_to_file('No rules found for aiomatic_reply_list!');
2792 + if($auto == 1)
2793 + {
2794 + aiomatic_clearFromList($param, $type);
2795 + }
2796 + return 'fail';
2797 + }
2798 + }
2799 + else
2800 + {
2801 + aiomatic_log_to_file('Invalid rule type provided: ' . $type);
2802 + if($auto == 1)
2803 + {
2804 + aiomatic_clearFromList($param, $type);
2805 + }
2806 + return 'fail';
2807 + }
2808 + if(empty($enable_ai_images_article))
2809 + {
2810 + $enable_ai_images_article = $enable_ai_images;
2811 + }
2812 + if(empty($ai_command_image_article))
2813 + {
2814 + $ai_command_image_article = $ai_command_image;
2815 + }
2816 + if(empty($image_model_article))
2817 + {
2818 + $image_model_article = $image_model;
2819 + }
2820 + if(empty($image_size_article))
2821 + {
2822 + $image_size_article = $image_size;
2823 + }
2824 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Parameters parsed'));
2825 + $default_cat_list = '';
2826 + if (isset($default_category) && $default_category != '' && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
2827 + {
2828 + if(is_array($default_category))
2829 + {
2830 + $cextra = '';
2831 + foreach($default_category as $dc)
2832 + {
2833 + $cextra .= ',' . get_cat_name($dc);
2834 + }
2835 + $default_cat_list = trim( $cextra, ',');
2836 + }
2837 + else
2838 + {
2839 + $default_cat_list = trim(get_cat_name($default_category) . ',');
2840 + }
2841 + }
2842 + $days_no_run = explode(',', $days_no_run);
2843 + $days_no_run = array_map('trim', $days_no_run);
2844 + if(!empty($days_no_run))
2845 + {
2846 + if(in_array(date('l', time()), $days_no_run))
2847 + {
2848 + aiomatic_log_to_file('Skipping rule ID from running today: ' . $param . ', rule type: ' . $type);
2849 + if($auto == 1)
2850 + {
2851 + aiomatic_clearFromList($param, $type);
2852 + }
2853 + return 'nochange';
2854 + }
2855 + else
2856 + {
2857 + foreach($days_no_run as $nrd)
2858 + {
2859 + if(strtolower($nrd) == 'mo' && date('l', time()) == 'Monday')
2860 + {
2861 + aiomatic_log_to_file('Skipping rule ID from running on Monday: ' . $param . ', rule type: ' . $type);
2862 + if($auto == 1)
2863 + {
2864 + aiomatic_clearFromList($param, $type);
2865 + }
2866 + return 'nochange';
2867 + }
2868 + elseif(strtolower($nrd) == 'tu' && date('l', time()) == 'Tuesday')
2869 + {
2870 + aiomatic_log_to_file('Skipping rule ID from running on Tuesday: ' . $param . ', rule type: ' . $type);
2871 + if($auto == 1)
2872 + {
2873 + aiomatic_clearFromList($param, $type);
2874 + }
2875 + return 'nochange';
2876 + }
2877 + elseif(strtolower($nrd) == 'we' && date('l', time()) == 'Wednesday')
2878 + {
2879 + aiomatic_log_to_file('Skipping rule ID from running on Wednesday: ' . $param . ', rule type: ' . $type);
2880 + if($auto == 1)
2881 + {
2882 + aiomatic_clearFromList($param, $type);
2883 + }
2884 + return 'nochange';
2885 + }
2886 + elseif(strtolower($nrd) == 'th' && date('l', time()) == 'Thursday')
2887 + {
2888 + aiomatic_log_to_file('Skipping rule ID from running on Thursday: ' . $param . ', rule type: ' . $type);
2889 + if($auto == 1)
2890 + {
2891 + aiomatic_clearFromList($param, $type);
2892 + }
2893 + return 'nochange';
2894 + }
2895 + elseif(strtolower($nrd) == 'fr' && date('l', time()) == 'Friday')
2896 + {
2897 + aiomatic_log_to_file('Skipping rule ID from running on Friday: ' . $param . ', rule type: ' . $type);
2898 + if($auto == 1)
2899 + {
2900 + aiomatic_clearFromList($param, $type);
2901 + }
2902 + return 'nochange';
2903 + }
2904 + elseif(strtolower($nrd) == 'sa' && date('l', time()) == 'Saturday')
2905 + {
2906 + aiomatic_log_to_file('Skipping rule ID from running on Saturday: ' . $param . ', rule type: ' . $type);
2907 + if($auto == 1)
2908 + {
2909 + aiomatic_clearFromList($param, $type);
2910 + }
2911 + return 'nochange';
2912 + }
2913 + elseif(strtolower($nrd) == 'su' && date('l', time()) == 'Sunday')
2914 + {
2915 + aiomatic_log_to_file('Skipping rule ID from running on Sunday: ' . $param . ', rule type: ' . $type);
2916 + if($auto == 1)
2917 + {
2918 + aiomatic_clearFromList($param, $type);
2919 + }
2920 + return 'nochange';
2921 + }
2922 + }
2923 + }
2924 + }
2925 + $topic_images_bck = $topic_images;
2926 + $inboundlinker = null;
2927 + if($max_links !== '' && $type != '5')
2928 + {
2929 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $max_links, $mxatches);
2930 + if(isset($mxatches[2][0]))
2931 + {
2932 + $min_l = $mxatches[1][0];
2933 + $max_l = $mxatches[2][0];
2934 + $max_links = wp_rand(intval($min_l), intval($max_l));
2935 + }
2936 + else
2937 + {
2938 + $max_links = intval($max_links);
2939 + }
2940 + require_once(dirname(__FILE__) . "/res/InboundLinks.php");
2941 + $inboundlinker = new AiomaticAutoInboundLinks();
2942 + }
2943 + if(empty($max_tokens) || intval($max_tokens) <= 0)
2944 + {
2945 + $max_tokens = aimogenpro_get_max_tokens($model);
2946 + }
2947 + if(intval($max_tokens) > AIMOGEN_DEFAULT_MAX_TOKENS && ( !stristr($model, 'gpt') || aiomatic_is_trained_model($model)))
2948 + {
2949 + $max_tokens = AIMOGEN_DEFAULT_MAX_TOKENS;
2950 + }
2951 + if($max_seed_tokens === '')
2952 + {
2953 + $max_seed_tokens = AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2954 + }
2955 + if($max_continue_tokens === '')
2956 + {
2957 + $max_continue_tokens = AIMOGEN_DEFAULT_COMPLETION_TOKENS;
2958 + }
2959 + if($type == 0 || empty($type))
2960 + {
2961 + if ($found == 0) {
2962 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in rules!');
2963 + if($auto == 1)
2964 + {
2965 + aiomatic_clearFromList($param, $type);
2966 + }
2967 + return 'fail';
2968 + } else {
2969 + if($ret_content == 0)
2970 + {
2971 + $GLOBALS['wp_object_cache']->delete('aiomatic_rules_list', 'options');
2972 + $rules = get_option('aiomatic_rules_list');
2973 + $rules[$param][2] = aiomatic_get_date_now();
2974 + aiomatic_update_option('aiomatic_rules_list', $rules, false);
2975 + }
2976 + }
2977 + }
2978 + elseif($type == 1)
2979 + {
2980 + if ($found == 0) {
2981 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in YouTube rules!');
2982 + if($auto == 1)
2983 + {
2984 + aiomatic_clearFromList($param, $type);
2985 + }
2986 + return 'fail';
2987 + } else {
2988 + if($ret_content == 0)
2989 + {
2990 + $GLOBALS['wp_object_cache']->delete('aiomatic_youtube_list', 'options');
2991 + $rules = get_option('aiomatic_youtube_list');
2992 + $rules[$param][2] = aiomatic_get_date_now();
2993 + aiomatic_update_option('aiomatic_youtube_list', $rules, false);
2994 + }
2995 + }
2996 + }
2997 + elseif($type == 2)
2998 + {
2999 + if ($found == 0) {
3000 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in Amazon rules!');
3001 + if($auto == 1)
3002 + {
3003 + aiomatic_clearFromList($param, $type);
3004 + }
3005 + return 'fail';
3006 + } else {
3007 + if($ret_content == 0)
3008 + {
3009 + $GLOBALS['wp_object_cache']->delete('aiomatic_amazon_list', 'options');
3010 + $rules = get_option('aiomatic_amazon_list');
3011 + $rules[$param][2] = aiomatic_get_date_now();
3012 + aiomatic_update_option('aiomatic_amazon_list', $rules, false);
3013 + }
3014 + }
3015 + }
3016 + elseif($type == 3)
3017 + {
3018 + if ($found == 0) {
3019 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in Amazon review rules!');
3020 + if($auto == 1)
3021 + {
3022 + aiomatic_clearFromList($param, $type);
3023 + }
3024 + return 'fail';
3025 + } else {
3026 + if($ret_content == 0)
3027 + {
3028 + $GLOBALS['wp_object_cache']->delete('aiomatic_review_list', 'options');
3029 + $rules = get_option('aiomatic_review_list');
3030 + $rules[$param][2] = aiomatic_get_date_now();
3031 + aiomatic_update_option('aiomatic_review_list', $rules, false);
3032 + }
3033 + }
3034 + }
3035 + elseif($type == 4)
3036 + {
3037 + if ($found == 0) {
3038 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in CSV rules!');
3039 + if($auto == 1)
3040 + {
3041 + aiomatic_clearFromList($param, $type);
3042 + }
3043 + return 'fail';
3044 + } else {
3045 + if($ret_content == 0)
3046 + {
3047 + $GLOBALS['wp_object_cache']->delete('aiomatic_csv_list', 'options');
3048 + $rules = get_option('aiomatic_csv_list');
3049 + $rules[$param][2] = aiomatic_get_date_now();
3050 + aiomatic_update_option('aiomatic_csv_list', $rules, false);
3051 + }
3052 + }
3053 + }
3054 + elseif($type == 5)
3055 + {
3056 + if ($found == 0) {
3057 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in OmniBlock rules!');
3058 + if($auto == 1)
3059 + {
3060 + aiomatic_clearFromList($param, $type);
3061 + }
3062 + return 'fail';
3063 + } else {
3064 + if($ret_content == 0)
3065 + {
3066 + $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
3067 + $rules = get_option('aiomatic_omni_list');
3068 + $rules[$param][2] = aiomatic_get_date_now();
3069 + aiomatic_update_option('aiomatic_omni_list', $rules, false);
3070 + }
3071 + }
3072 + }
3073 + elseif($type == 6)
3074 + {
3075 + if ($found == 0) {
3076 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in rules!');
3077 + if($auto == 1)
3078 + {
3079 + aiomatic_clearFromList($param, $type);
3080 + }
3081 + return 'fail';
3082 + } else {
3083 + if($ret_content == 0)
3084 + {
3085 + $GLOBALS['wp_object_cache']->delete('aiomatic_listicle_list', 'options');
3086 + $rules = get_option('aiomatic_listicle_list');
3087 + $rules[$param][2] = aiomatic_get_date_now();
3088 + aiomatic_update_option('aiomatic_listicle_list', $rules, false);
3089 + }
3090 + }
3091 + }
3092 + elseif($type == 7)
3093 + {
3094 + if ($found == 0) {
3095 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in rules!');
3096 + if($auto == 1)
3097 + {
3098 + aiomatic_clearFromList($param, $type);
3099 + }
3100 + return 'fail';
3101 + } else {
3102 + if($ret_content == 0)
3103 + {
3104 + $GLOBALS['wp_object_cache']->delete('aiomatic_forum_list', 'options');
3105 + $rules = get_option('aiomatic_forum_list');
3106 + $rules[$param][2] = aiomatic_get_date_now();
3107 + aiomatic_update_option('aiomatic_forum_list', $rules, false);
3108 + }
3109 + }
3110 + }
3111 + elseif($type == 8)
3112 + {
3113 + if ($found == 0) {
3114 + aiomatic_log_to_file($type . ' - ' . $param . ' not found in rules!');
3115 + if($auto == 1)
3116 + {
3117 + aiomatic_clearFromList($param, $type);
3118 + }
3119 + return 'fail';
3120 + } else {
3121 + if($ret_content == 0)
3122 + {
3123 + $GLOBALS['wp_object_cache']->delete('aiomatic_reply_list', 'options');
3124 + $rules = get_option('aiomatic_reply_list');
3125 + $rules[$param][2] = aiomatic_get_date_now();
3126 + aiomatic_update_option('aiomatic_reply_list', $rules, false);
3127 + }
3128 + }
3129 + }
3130 + $post_link = '';
3131 + $rss_items = array();
3132 + if ($enable_comments == '1') {
3133 + $accept_comments = 'open';
3134 + }
3135 + $count = 1;
3136 + if($temperature == '')
3137 + {
3138 + $temperature = 1;
3139 + }
3140 + else
3141 + {
3142 + $temperature = floatval($temperature);
3143 + }
3144 + if($top_p == '')
3145 + {
3146 + $top_p = 1;
3147 + }
3148 + else
3149 + {
3150 + $top_p = floatval($top_p);
3151 + }
3152 + if($frequency_penalty == '')
3153 + {
3154 + $frequency_penalty = 0;
3155 + }
3156 + else
3157 + {
3158 + $frequency_penalty = floatval($frequency_penalty);
3159 + }
3160 + if($presence_penalty == '')
3161 + {
3162 + $presence_penalty = 0;
3163 + }
3164 + else
3165 + {
3166 + $presence_penalty = floatval($presence_penalty);
3167 + }
3168 + $max_tokens = intval($max_tokens);
3169 + $max_seed_tokens = intval($max_seed_tokens);
3170 + $max_continue_tokens = intval($max_continue_tokens);
3171 + $blog_title = html_entity_decode(get_bloginfo('title'));
3172 + $post_title = aiomatic_replaceSynergyShortcodes($post_title);
3173 + $posted_items = array();
3174 + $posted_topics = array();
3175 + if(($title_once == '1' || $overwrite_existing == '1') && $type != '5' && $type != '7')
3176 + {
3177 + $postsPerPage = 50000;
3178 + $paged = 0;
3179 + wp_suspend_cache_addition(true);
3180 + $post_stati = get_post_stati();
3181 + foreach ($post_stati as $key => $val) {
3182 + if ($val == 'auto-draft') {
3183 + unset($post_stati[$key]);
3184 + }
3185 + if ($val == 'inherit') {
3186 + unset($post_stati[$key]);
3187 + }
3188 + if ($val == 'request-pending') {
3189 + unset($post_stati[$key]);
3190 + }
3191 + if ($val == 'request-confirmed') {
3192 + unset($post_stati[$key]);
3193 + }
3194 + if ($val == 'request-failed') {
3195 + unset($post_stati[$key]);
3196 + }
3197 + if ($val == 'request-completed') {
3198 + unset($post_stati[$key]);
3199 + }
3200 + }
3201 + do
3202 + {
3203 + $postOffset = $paged * $postsPerPage;
3204 + $query = array(
3205 + 'post_status' => $post_stati,
3206 + 'post_type' => array(
3207 + 'any'
3208 + ),
3209 + 'numberposts' => $postsPerPage,
3210 + 'fields' => 'ids',
3211 + 'meta_key' => 'aiomatic_source_title',
3212 + 'offset' => $postOffset
3213 + );
3214 + $post_list = get_posts($query);
3215 + foreach ($post_list as $post) {
3216 + $orig_tit = get_post_meta($post, 'aiomatic_source_title', true);
3217 + if(!empty($orig_tit))
3218 + {
3219 + $posted_items[$orig_tit] = $post;
3220 + }
3221 + }
3222 + $paged++;
3223 + }while(!empty($post_list));
3224 + wp_suspend_cache_addition(false);
3225 + unset($post_list);
3226 + $postsPerPage = 50000;
3227 + $paged = 0;
3228 + wp_suspend_cache_addition(true);
3229 + $post_stati = get_post_stati();
3230 + foreach ($post_stati as $key => $val) {
3231 + if ($val == 'auto-draft') {
3232 + unset($post_stati[$key]);
3233 + }
3234 + if ($val == 'inherit') {
3235 + unset($post_stati[$key]);
3236 + }
3237 + if ($val == 'request-pending') {
3238 + unset($post_stati[$key]);
3239 + }
3240 + if ($val == 'request-confirmed') {
3241 + unset($post_stati[$key]);
3242 + }
3243 + if ($val == 'request-failed') {
3244 + unset($post_stati[$key]);
3245 + }
3246 + if ($val == 'request-completed') {
3247 + unset($post_stati[$key]);
3248 + }
3249 + }
3250 + do
3251 + {
3252 + $postOffset = $paged * $postsPerPage;
3253 + $query = array(
3254 + 'post_status' => $post_stati,
3255 + 'post_type' => array(
3256 + 'any'
3257 + ),
3258 + 'numberposts' => $postsPerPage,
3259 + 'fields' => 'ids',
3260 + 'meta_key' => 'aiomatic_post_topic',
3261 + 'offset' => $postOffset
3262 + );
3263 + $post_list = get_posts($query);
3264 + foreach ($post_list as $post) {
3265 + $aiomatic_post_topic = get_post_meta($post, 'aiomatic_post_topic', true);
3266 + if(!empty($aiomatic_post_topic))
3267 + {
3268 + $posted_topics[$aiomatic_post_topic] = $post;
3269 + }
3270 + }
3271 + $paged++;
3272 + }while(!empty($post_list));
3273 + wp_suspend_cache_addition(false);
3274 + unset($post_list);
3275 + }
3276 + $user_name_type = $post_user_name;
3277 + if($title_once == '1' && $type != '4' && $type != '5')
3278 + {
3279 + $postsPerPage = 50000;
3280 + $paged = 0;
3281 + wp_suspend_cache_addition(true);
3282 + $post_stati = get_post_stati();
3283 + foreach ($post_stati as $key => $val) {
3284 + if ($val == 'auto-draft') {
3285 + unset($post_stati[$key]);
3286 + }
3287 + if ($val == 'inherit') {
3288 + unset($post_stati[$key]);
3289 + }
3290 + if ($val == 'request-pending') {
3291 + unset($post_stati[$key]);
3292 + }
3293 + if ($val == 'request-confirmed') {
3294 + unset($post_stati[$key]);
3295 + }
3296 + if ($val == 'request-failed') {
3297 + unset($post_stati[$key]);
3298 + }
3299 + if ($val == 'request-completed') {
3300 + unset($post_stati[$key]);
3301 + }
3302 + }
3303 + do
3304 + {
3305 + $postOffset = $paged * $postsPerPage;
3306 + $query = array(
3307 + 'post_status' => $post_stati,
3308 + 'post_type' => array(
3309 + 'any'
3310 + ),
3311 + 'numberposts' => $postsPerPage,
3312 + 'fields' => 'ids',
3313 + 'meta_key' => 'aiomatic_rss_link',
3314 + 'offset' => $postOffset
3315 + );
3316 + $zpost_list = get_posts($query);
3317 + foreach ($zpost_list as $zpost) {
3318 + $orig_link = get_post_meta($zpost, 'aiomatic_rss_link', true);
3319 + if(!empty($orig_link))
3320 + {
3321 + $rss_items[$orig_link] = $zpost;
3322 + }
3323 + }
3324 + $paged++;
3325 + }while(!empty($zpost_list));
3326 + wp_suspend_cache_addition(false);
3327 + }
3328 + if($type == 0 || $type == 6 || empty($type))
3329 + {
3330 + if($post_title != '')
3331 + {
3332 + $post_title_lines = preg_split('/\r\n|\r|\n/', $post_title);
3333 + }
3334 + else
3335 + {
3336 + $post_title_lines = array();
3337 + }
3338 + $additional_kws = array();
3339 + $user_name = '';
3340 + $featured_image = '';
3341 + $post_cats = '';
3342 + $post_tagz = '';
3343 + $post_excerpt = '';
3344 + $final_content = '';
3345 + $postID = '';
3346 + $heading_val = '';
3347 + $image_query = '';
3348 + $temp_post = '';
3349 + $cntx = count($post_title_lines);
3350 + $rss_feeds = array();
3351 + for($ji = 0; $ji < $cntx; $ji++)
3352 + {
3353 + if (filter_var($post_title_lines[$ji], FILTER_VALIDATE_URL) !== false)
3354 + {
3355 + if(aiomatic_endsWith($post_title_lines[$ji], '.txt'))
3356 + {
3357 +
3358 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing txt file'));
3359 + $txt_content = aiomatic_get_web_page($post_title_lines[$ji]);
3360 + if ($txt_content === FALSE)
3361 + {
3362 + aiomatic_log_to_file('Failed to read text file: ' . $post_title_lines[$ji]);
3363 + unset($post_title_lines[$ji]);
3364 + if($auto == 1)
3365 + {
3366 + aiomatic_log_to_file($param);
3367 + }
3368 + continue;
3369 + }
3370 + unset($post_title_lines[$ji]);
3371 + $additional_kws = preg_split('/\r\n|\r|\n/', $txt_content);
3372 + }
3373 + else
3374 + {
3375 + aiomatic_log_to_file('Trying to parse RSS feed items(1): ' . $post_title_lines[$ji]);
3376 + try
3377 + {
3378 + if(!class_exists('SimplePie_Autoloader', false))
3379 + {
3380 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
3381 + }
3382 + }
3383 + catch(Exception $e)
3384 + {
3385 + unset($post_title_lines[$ji]);
3386 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
3387 + if($auto == 1)
3388 + {
3389 + aiomatic_log_to_file($param);
3390 + }
3391 + continue;
3392 + }
3393 +
3394 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing RSS feed'));
3395 + $feed = new SimplePie();
3396 + $feed->set_timeout(120);
3397 + $feed->set_feed_url($post_title_lines[$ji]);
3398 + $feed->enable_cache(false);
3399 + $feed->strip_htmltags(false);
3400 + $feed->init();
3401 + $feed->handle_content_type();
3402 + if ($feed->error())
3403 + {
3404 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $post_title_lines[$ji]);
3405 + unset($post_title_lines[$ji]);
3406 + if($auto == 1)
3407 + {
3408 + aiomatic_clearFromList($param, $type);
3409 + }
3410 + continue;
3411 + }
3412 + $items = $feed->get_items();
3413 + $zero = true;
3414 + foreach($items as $itemx)
3415 + {
3416 + if($zero == true)
3417 + {
3418 + $post_link = trim($itemx->get_permalink());
3419 + if(isset($rss_items[$post_link]))
3420 + {
3421 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
3422 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
3423 + }
3424 + continue;
3425 + }
3426 + if ($fauthor = $itemx->get_author())
3427 + {
3428 + $user_name = $fauthor->get_name();
3429 + }
3430 + else
3431 + {
3432 + $user_name = '';
3433 + }
3434 + $feed_cats = array();
3435 + $varx = $itemx->get_categories();
3436 + if(is_array($varx))
3437 + {
3438 + foreach ($varx as $xcategory)
3439 + {
3440 + $feed_cats[] = $xcategory->get_label();
3441 + }
3442 + }
3443 + $post_cats = implode(',', $feed_cats);
3444 + $post_excerpt = $itemx->get_description();
3445 + $final_content = $itemx->get_content();
3446 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
3447 + }
3448 + else
3449 + {
3450 + $post_link_temp = trim($itemx->get_permalink());
3451 + if(isset($rss_items[$post_link_temp]))
3452 + {
3453 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
3454 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
3455 + }
3456 + continue;
3457 + }
3458 + if ($fauthor = $itemx->get_author())
3459 + {
3460 + $user_name_temp = $fauthor->get_name();
3461 + }
3462 + else
3463 + {
3464 + $user_name_temp = '';
3465 + }
3466 + $feed_cats = array();
3467 + $varx = $itemx->get_categories();
3468 + if(is_array($varx))
3469 + {
3470 + foreach ($varx as $xcategory)
3471 + {
3472 + $feed_cats[] = $xcategory->get_label();
3473 + }
3474 + }
3475 + $post_cats_temp = implode(',', $feed_cats);
3476 + $post_excerpt_temp = $itemx->get_description();
3477 + $final_content_temp = $itemx->get_content();
3478 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link_temp, 'author' => $user_name_temp, 'cats' => $post_cats_temp, 'excerpt' => $post_excerpt_temp, 'content' => $final_content_temp );
3479 + }
3480 + $additional_kws[] = $itemx->get_title();
3481 + $zero = false;
3482 + }
3483 + unset($post_title_lines[$ji]);
3484 + }
3485 + }
3486 + }
3487 + if(count($additional_kws) > 0)
3488 + {
3489 + $post_title_lines = array_merge($post_title_lines, $additional_kws);
3490 + }
3491 + $post_title_lines = array_unique($post_title_lines);
3492 + if($overwrite_existing == '1')
3493 + {
3494 + $post_title_lines_processed = array();
3495 + foreach($post_title_lines as $zind => $ptl)
3496 + {
3497 + $ptlprocessed = explode('!###!', $ptl);
3498 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
3499 + {
3500 + $post_title_lines_processed[$zind] = $ptlprocessed[0];
3501 + }
3502 + else
3503 + {
3504 + $post_title_lines_processed[$zind] = $ptl;
3505 + }
3506 + }
3507 + foreach($posted_items as $ptit => $pid)
3508 + {
3509 + if (($key = array_search($ptit, $post_title_lines_processed)) !== false) {
3510 + $update_post_id = $pid;
3511 + break;
3512 + }
3513 + }
3514 + }
3515 + else
3516 + {
3517 + if($title_once == '1')
3518 + {
3519 + $skipt = 0;
3520 + $post_title_lines_processed = array();
3521 + foreach($post_title_lines as $zind => $ptl)
3522 + {
3523 + $ptlprocessed = explode('!###!', $ptl);
3524 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
3525 + {
3526 + $post_title_lines_processed[$zind] = $ptlprocessed[0];
3527 + }
3528 + else
3529 + {
3530 + $post_title_lines_processed[$zind] = $ptl;
3531 + }
3532 + }
3533 + foreach($posted_items as $ptit => $pid)
3534 + {
3535 + if (($key = array_search($ptit, $post_title_lines_processed)) !== false) {
3536 + if (filter_var($post_title_lines[$key], FILTER_VALIDATE_URL) === false && stristr($post_title_lines[$key], '%%ai_generated_title%%') === false)
3537 + {
3538 + aiomatic_log_to_file('Skipping title, already processed: ' . $ptit);
3539 + unset($post_title_lines[$key]);
3540 + unset($post_title_lines_processed[$key]);
3541 + $skipt++;
3542 + }
3543 + }
3544 + }
3545 + if(count($post_title_lines) == 0 && $skipt > 0)
3546 + {
3547 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
3548 + {
3549 + if($count === 1)
3550 + {
3551 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
3552 + foreach($email_list as $thisaddr)
3553 + {
3554 + $thisaddr = trim($thisaddr);
3555 + try
3556 + {
3557 + $admin_mail = get_option('admin_email');
3558 + $to = $thisaddr;
3559 + $subject = '[Aimogen] Titles depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
3560 + $message = 'Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its titles, no new posts publised! Website URL: ' . get_site_url();
3561 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
3562 + $headers[] = 'Reply-To: ' . $admin_mail;
3563 + $headers[] = 'X-Mailer: PHP/' . phpversion();
3564 + $headers[] = 'Content-Type: text/html';
3565 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
3566 + wp_mail($to, $subject, $message, $headers);
3567 + }
3568 + catch (Exception $e) {
3569 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
3570 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
3571 + }
3572 + }
3573 + }
3574 + }
3575 + }
3576 + aiomatic_log_to_file('All titles already processed, nothing to do.');
3577 + if($auto == 1)
3578 + {
3579 + aiomatic_clearFromList($param, $type);
3580 + }
3581 + return 'nochange';
3582 + }
3583 + }
3584 + }
3585 + $spintax = new Aiomatic_Spintax();
3586 + $orig_ai_command = $ai_command;
3587 + $orig_ai_command_title = $title_ai_command;
3588 + $orig_ai_command_category = $category_ai_command;
3589 + $orig_ai_command_tag = $tag_ai_command;
3590 + $orig_ai_command_image = $ai_command_image;
3591 + $orig_ai_command_image_article = $ai_command_image_article;
3592 + $already_spinned = 0;
3593 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
3594 + {
3595 + $img_attr = $aiomatic_Main_Settings['attr_text'];
3596 + }
3597 + else
3598 + {
3599 + $img_attr = '';
3600 + }
3601 + if($headings_list != '')
3602 + {
3603 + $headings_arr_temp = preg_split('/\r\n|\r|\n/', $headings_list);
3604 + $headings_arr_temp = array_map('trim', $headings_arr_temp);
3605 + $headings_arr = array();
3606 + foreach($headings_arr_temp as $hat)
3607 + {
3608 + $hat = aiomatic_replaceSynergyShortcodes($hat);
3609 + $hat = aiomatic_replaceAIPostShortcodes($hat, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, $global_prepend, $global_append);
3610 + $headings_arr[] = array('q' => $hat, 'a' => '');
3611 + }
3612 + }
3613 + else
3614 + {
3615 + $headings_arr = array();
3616 + }
3617 + if($images_list != '')
3618 + {
3619 + $images_arr = preg_split('/\r\n|\r|\n/', $images_list);
3620 + $images_arr = array_map('trim', $images_arr);
3621 + }
3622 + else
3623 + {
3624 + $images_arr = array();
3625 + }
3626 + $no_init_topics = false;
3627 + if(empty($post_topic_list))
3628 + {
3629 + $no_init_topics = true;
3630 + $post_topic_list_arr = array();
3631 + }
3632 + else
3633 + {
3634 + $post_topic_list_arr = preg_split('/\r\n|\r|\n/', $post_topic_list);
3635 + $post_topic_list_arr = array_unique($post_topic_list_arr);
3636 + if(count($post_topic_list_arr) == 0 || empty($post_topic_list))
3637 + {
3638 + $no_init_topics = true;
3639 + }
3640 + }
3641 + while(true)
3642 + {
3643 + $thread_id = '';
3644 + $post_link = '';
3645 + $user_name = '';
3646 + $post_cats = '';
3647 + $post_excerpt = '';
3648 + $final_content = '';
3649 + $my_title_outro = '';
3650 + $update_post_id = '';
3651 + $featured_image = '';
3652 + $post_tagz = '';
3653 + $postID = '';
3654 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
3655 + {
3656 + $img_attr = $aiomatic_Main_Settings['attr_text'];
3657 + }
3658 + else
3659 + {
3660 + $img_attr = '';
3661 + }
3662 + $old_title = '';
3663 + if ($count > intval($max)) {
3664 + break;
3665 + }
3666 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Starting processing post ID #' . $count));
3667 + $heading_image_loc = $heading_img_location;
3668 + if($heading_image_loc == 'random')
3669 + {
3670 + $xwords = array('top', 'heading', 'bottom');
3671 + $randomWord = $xwords[array_rand($xwords)];
3672 + $heading_image_loc = $randomWord;
3673 + }
3674 + $current_section = '';
3675 + $post_sections = '';
3676 + $post_topic = '';
3677 + $post_title_keywords = '';
3678 + $added_vid_list = array();
3679 + if($posting_mode == 'topic')
3680 + {
3681 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Topic based posting detected'));
3682 +
3683 + $norm = function($s){
3684 + $s = html_entity_decode($s, ENT_QUOTES | ENT_HTML5, 'UTF-8');
3685 + $s = mb_strtolower($s, 'UTF-8');
3686 + if (class_exists('Normalizer')) {
3687 + $s = Normalizer::normalize($s, Normalizer::FORM_C);
3688 + }
3689 + $s = preg_replace('/\s+/u', ' ', $s);
3690 + $s = trim($s);
3691 + return $s;
3692 + };
3693 +
3694 + $posted_lookup = [];
3695 + foreach ($posted_topics as $ptit => $_pid) {
3696 + if (!strstr($ptit, '%%title%%')) {
3697 + $posted_lookup[$norm($ptit)] = true;
3698 + }
3699 + }
3700 +
3701 + if ($title_once == '1' && !empty($post_topic_list_arr)) {
3702 + $post_topic_list_arr = array_values(array_filter(
3703 + $post_topic_list_arr,
3704 + function($t) use ($posted_lookup, $norm){
3705 + return !isset($posted_lookup[$norm($t)]);
3706 + }
3707 + ));
3708 + }
3709 + if(!empty($post_topic_list_arr))
3710 + {
3711 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
3712 + {
3713 + $nrx = array_rand($post_topic_list_arr);
3714 + }
3715 + else
3716 + {
3717 + $nrx = array_key_first($post_topic_list_arr);
3718 + }
3719 + }
3720 + else
3721 + {
3722 + $nrx = null;
3723 + }
3724 +
3725 + if(($nrx === null || $nrx === false) && $no_init_topics === false)
3726 + {
3727 + aiomatic_log_to_file('No more topics to process.');
3728 + if($auto == 1)
3729 + {
3730 + aiomatic_clearFromList($param, $type);
3731 + }
3732 + return 'nochange';
3733 + }
3734 +
3735 + if($nrx !== null && $nrx !== false)
3736 + {
3737 + $post_topic = $post_topic_list_arr[$nrx];
3738 + if (filter_var($post_topic, FILTER_VALIDATE_URL) !== false)
3739 + {
3740 + $additional_topics = array();
3741 + if(aiomatic_endsWith($post_topic, '.txt'))
3742 + {
3743 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing txt file'));
3744 + $txt_content = aiomatic_get_web_page($post_topic);
3745 + if ($txt_content === FALSE)
3746 + {
3747 + unset($post_topic_list_arr[$nrx]);
3748 + $post_topic_list_arr = array_values($post_topic_list_arr);
3749 + aiomatic_log_to_file('Failed to read text file: ' . $post_topic);
3750 + if($auto == 1)
3751 + {
3752 + aiomatic_log_to_file($param);
3753 + }
3754 + break;
3755 + }
3756 + $additional_topics = preg_split('/\r\n|\r|\n/', $txt_content);
3757 + }
3758 + else
3759 + {
3760 + aiomatic_log_to_file('Trying to parse RSS feed items(2): ' . $post_topic);
3761 + try
3762 + {
3763 + if(!class_exists('SimplePie_Autoloader', false))
3764 + {
3765 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
3766 + }
3767 + }
3768 + catch(Exception $e)
3769 + {
3770 + unset($post_topic_list_arr[$nrx]);
3771 + $post_topic_list_arr = array_values($post_topic_list_arr);
3772 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
3773 + if($auto == 1)
3774 + {
3775 + aiomatic_log_to_file($param);
3776 + }
3777 + break;
3778 + }
3779 +
3780 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing RSS feed'));
3781 + $feed = new SimplePie();
3782 + $feed->set_timeout(120);
3783 + $feed->set_feed_url($post_topic);
3784 + $feed->enable_cache(false);
3785 + $feed->strip_htmltags(false);
3786 + $feed->init();
3787 + $feed->handle_content_type();
3788 + if ($feed->error())
3789 + {
3790 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $post_topic);
3791 + unset($post_topic_list_arr[$nrx]);
3792 + $post_topic_list_arr = array_values($post_topic_list_arr);
3793 + if($auto == 1)
3794 + {
3795 + aiomatic_clearFromList($param, $type);
3796 + }
3797 + break;
3798 + }
3799 + unset($post_topic_list_arr[$nrx]);
3800 + $post_topic_list_arr = array_values($post_topic_list_arr);
3801 + $items = $feed->get_items();
3802 + foreach($items as $itemx)
3803 + {
3804 + $additional_topics[] = $itemx->get_title();
3805 + $post_topic_list_arr[] = $itemx->get_title();
3806 + }
3807 + }
3808 + if(!empty($additional_topics))
3809 + {
3810 + $post_topic = $additional_topics[array_rand($additional_topics)];
3811 + }
3812 + }
3813 +
3814 + }
3815 + else
3816 + {
3817 + $post_topic = '';
3818 + }
3819 +
3820 + if($overwrite_existing == '1')
3821 + {
3822 + foreach($posted_topics as $ptit => $pid)
3823 + {
3824 + if(!strstr($ptit, '%%title%%'))
3825 + {
3826 + if (($key = array_search($ptit, $post_topic_list_arr)) !== false)
3827 + {
3828 + $post_topic = $ptit;
3829 + $update_post_id = $pid;
3830 + break;
3831 + }
3832 + }
3833 + }
3834 + }
3835 + else
3836 + {
3837 + if($title_once == '1')
3838 + {
3839 + $post_topic_list_arr = array_values($post_topic_list_arr);
3840 + }
3841 + if($no_init_topics === false && count($post_topic_list_arr) == 0)
3842 + {
3843 + if($title_once == '1')
3844 + {
3845 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
3846 + {
3847 + if($count === 1)
3848 + {
3849 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
3850 + foreach($email_list as $thisaddr)
3851 + {
3852 + $thisaddr = trim($thisaddr);
3853 + try
3854 + {
3855 + $admin_mail = get_option('admin_email');
3856 + $to = $thisaddr;
3857 + $subject = '[Aimogen] Topics depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
3858 + $message = 'Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its topics, no new posts publised! Website URL: ' . get_site_url();
3859 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
3860 + $headers[] = 'Reply-To: ' . $admin_mail;
3861 + $headers[] = 'X-Mailer: PHP/' . phpversion();
3862 + $headers[] = 'Content-Type: text/html';
3863 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
3864 + wp_mail($to, $subject, $message, $headers);
3865 + }
3866 + catch (Exception $e) {
3867 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
3868 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
3869 + }
3870 + }
3871 + }
3872 + }
3873 + }
3874 + }
3875 + aiomatic_log_to_file('All topics already processed, nothing to do.');
3876 + if($auto == 1)
3877 + {
3878 + aiomatic_clearFromList($param, $type);
3879 + }
3880 + return 'nochange';
3881 + }
3882 + }
3883 +
3884 + if ($title_once == '1' && isset($posted_lookup[$norm($post_topic)])) {
3885 + aiomatic_log_to_file('Duplicate detected after selection: ' . $post_topic);
3886 + unset($post_topic_list_arr[$nrx]);
3887 + continue;
3888 + }
3889 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
3890 + {
3891 + if(empty($post_topic))
3892 + {
3893 + if($type == 6)
3894 + {
3895 + aiomatic_log_to_file('Starting processing listicle keyword by title');
3896 + }
3897 + else
3898 + {
3899 + aiomatic_log_to_file('Starting processing post topic by title');
3900 + }
3901 + }
3902 + else
3903 + {
3904 + if($type == 6)
3905 + {
3906 + aiomatic_log_to_file('Starting processing listicle keyword: ' . $post_topic);
3907 + }
3908 + else
3909 + {
3910 + aiomatic_log_to_file('Starting processing post topic: ' . $post_topic);
3911 + }
3912 + }
3913 + }
3914 + if($title_once == '1')
3915 + {
3916 + if(!empty($post_topic_list))
3917 + {
3918 + unset($post_topic_list_arr[$nrx]);
3919 + }
3920 + }
3921 + $post_sections = '';
3922 + $current_section = '';
3923 + $headings_arr_copy = $headings_arr;
3924 + $added_img_list = array();
3925 + $raw_img_list = array();
3926 + $full_result_list = array();
3927 + $added_images = 0;
3928 + $added_videos = 0;
3929 + $heading_results = array();
3930 + if(!empty($global_prepend))
3931 + {
3932 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
3933 + $global_prepend = aiomatic_replaceContentShortcodes($global_prepend, $img_attr, $ai_command);
3934 + $global_prepend = aiomatic_replacetopics($global_prepend, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, '', '', $default_cat_list, $img_attr);
3935 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
3936 + }
3937 + if(!empty($global_append))
3938 + {
3939 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
3940 + $global_append = aiomatic_replaceContentShortcodes($global_append, $img_attr, $ai_command);
3941 + $global_append = aiomatic_replacetopics($global_append, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, '', '', $default_cat_list, $img_attr);
3942 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
3943 + }
3944 + if(($title_generator_method == 'ai' || empty($title_generator_method)) && $title_prompt != '' && $post_topic_list != '')
3945 + {
3946 + if(count($post_title_lines) == 0)
3947 + {
3948 + aiomatic_log_to_file('Stopping execution, no titles added: ' . $post_topic);
3949 + break;
3950 + }
3951 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
3952 + {
3953 + $current_index = array_rand($post_title_lines);
3954 + $post_title = trim($post_title_lines[$current_index]);
3955 + }
3956 + else
3957 + {
3958 + $current_index = array_key_first($post_title_lines);
3959 + $post_title = trim($post_title_lines[$current_index]);
3960 + if (filter_var($post_title, FILTER_VALIDATE_URL) === false && stristr($post_title, '%%ai_generated_title%%') === false)
3961 + {
3962 + unset($post_title_lines[$current_index]);
3963 + }
3964 + }
3965 + $ptlprocessed = explode('!###!', $post_title);
3966 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
3967 + {
3968 + $post_title = $ptlprocessed[0];
3969 + $post_title_keywords = $ptlprocessed[1];
3970 + }
3971 + $post_title = apply_filters('aiomatic_replace_aicontent_shortcode', $post_title);
3972 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
3973 + {
3974 + $title_prompt_arr = preg_split('/\r\n|\r|\n/', $title_prompt);
3975 + $my_title_prompt = $title_prompt_arr[array_rand($title_prompt_arr)];
3976 + }
3977 + else
3978 + {
3979 + $my_title_prompt = $title_prompt;
3980 + }
3981 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
3982 + $zatitle_prompt = aiomatic_replacetopics($my_title_prompt, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
3983 + $zatitle_prompt = aiomatic_replaceAIPostShortcodes($zatitle_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
3984 + if(stristr($run_regex_on, 'title') !== false)
3985 + {
3986 + if ($strip_by_regex_prompts !== '')
3987 + {
3988 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
3989 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
3990 + $xcnt = 0;
3991 + foreach($xstrip_by_regex as $sbr)
3992 + {
3993 + if(isset($xreplace_regex[$xcnt]))
3994 + {
3995 + $repreg = $xreplace_regex[$xcnt];
3996 + }
3997 + else
3998 + {
3999 + $repreg = '';
4000 + }
4001 + $xcnt++;
4002 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $zatitle_prompt);
4003 + if($temp_cont !== NULL)
4004 + {
4005 + $zatitle_prompt = $temp_cont;
4006 + }
4007 + }
4008 + }
4009 + }
4010 + if($zatitle_prompt != '')
4011 + {
4012 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based title'));
4013 + $written = aiomatic_write_content($zatitle_prompt, $topic_title_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'title', 'user', $assistant_id, $thread_id, $store_data);
4014 + if($written === false)
4015 + {
4016 + aiomatic_log_to_file('Failed to generate title!');
4017 + if($auto == 1)
4018 + {
4019 + aiomatic_clearFromList($param, $type);
4020 + }
4021 + return 'fail';
4022 + }
4023 + else
4024 + {
4025 + $written = ucfirst(trim(trim($written), '.'));
4026 + $post_title = $written;
4027 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
4028 + {
4029 + $posts = get_posts(
4030 + array(
4031 + 'post_type' => $post_type,
4032 + 'title' => html_entity_decode($post_title),
4033 + 'post_status' => 'all',
4034 + 'numberposts' => 1,
4035 + 'update_post_term_cache' => false,
4036 + 'update_post_meta_cache' => false,
4037 + 'orderby' => 'post_date ID',
4038 + 'order' => 'ASC',
4039 + )
4040 + );
4041 + if ( ! empty( $posts ) ) {
4042 + $zap = $posts[0];
4043 + } else {
4044 + $zap = null;
4045 + }
4046 + if($zap !== null)
4047 + {
4048 + if($overwrite_existing)
4049 + {
4050 + $update_post_id = $zap->ID;
4051 + }
4052 + else
4053 + {
4054 + aiomatic_log_to_file('Post with specified AI generated title already published, skipping it: ' . $post_title);
4055 + unset($post_topic_list_arr[$nrx]);
4056 + continue;
4057 + }
4058 + }
4059 + }
4060 + }
4061 + }
4062 + else
4063 + {
4064 + aiomatic_log_to_file('Empty processed title prompt. Nothing to do.');
4065 + if($auto == 1)
4066 + {
4067 + aiomatic_clearFromList($param, $type);
4068 + }
4069 + return 'fail';
4070 + }
4071 + }
4072 + elseif($title_generator_method == 'serp' && $post_topic_list != '')
4073 + {
4074 + $post_topic = apply_filters('aiomatic_replace_aicontent_shortcode', $post_topic);
4075 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting title from SERP results'));
4076 + $heading_results_ai = aiomatic_scrape_related_questions($post_topic, $headings, $headings_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $headings_ai_command, $assistant_id);
4077 + if(is_array($heading_results_ai) && !empty($heading_results_ai))
4078 + {
4079 + $rand_index = array_rand($heading_results_ai);
4080 + $post_title = ucfirst($heading_results_ai[$rand_index]['q']);
4081 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
4082 + {
4083 + $posts = get_posts(
4084 + array(
4085 + 'post_type' => $post_type,
4086 + 'title' => html_entity_decode($post_title),
4087 + 'post_status' => 'all',
4088 + 'numberposts' => 1,
4089 + 'update_post_term_cache' => false,
4090 + 'update_post_meta_cache' => false,
4091 + 'orderby' => 'post_date ID',
4092 + 'order' => 'ASC',
4093 + )
4094 + );
4095 + if ( ! empty( $posts ) ) {
4096 + $zap = $posts[0];
4097 + } else {
4098 + $zap = null;
4099 + }
4100 + if($zap !== null)
4101 + {
4102 + if($overwrite_existing)
4103 + {
4104 + $update_post_id = $zap->ID;
4105 + }
4106 + else
4107 + {
4108 + aiomatic_log_to_file('Post with specified SERP results generated title already published, skipping it: ' . $post_title);
4109 + unset($post_title_lines[$current_index]);
4110 + continue;
4111 + }
4112 + }
4113 + }
4114 + }
4115 + }
4116 + else
4117 + {
4118 + if(count($post_title_lines) == 0)
4119 + {
4120 + aiomatic_log_to_file('Stopping execution because no titles added: ' . $post_topic);
4121 + break;
4122 + }
4123 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
4124 + {
4125 + $current_index = array_rand($post_title_lines);
4126 + }
4127 + else
4128 + {
4129 + $current_index = array_key_first($post_title_lines);
4130 + }
4131 + $post_title = trim($post_title_lines[$current_index]);
4132 + $ptlprocessed = explode('!###!', $post_title);
4133 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
4134 + {
4135 + $post_title = $ptlprocessed[0];
4136 + $post_title_keywords = $ptlprocessed[1];
4137 + }
4138 + if(isset($rss_feeds[$post_title]))
4139 + {
4140 + $post_link = $rss_feeds[$post_title]['url'];
4141 + $user_name = $rss_feeds[$post_title]['author'];
4142 + $post_cats = $rss_feeds[$post_title]['cats'];
4143 + $post_excerpt = $rss_feeds[$post_title]['excerpt'];
4144 + $final_content = $rss_feeds[$post_title]['content'];
4145 + }
4146 + $tprepp = $spintax->Parse($post_title);
4147 + if($tprepp != false && $tprepp != '')
4148 + {
4149 + $post_title = $tprepp;
4150 + }
4151 + $old_title = $post_title;
4152 + $already_spinned = 0;
4153 + if (filter_var($post_title, FILTER_VALIDATE_URL) === false && stristr($post_title, '%%ai_generated_title%%') === false)
4154 + {
4155 + unset($post_title_lines[$current_index]);
4156 + }
4157 + $post_title = apply_filters('aiomatic_replace_aicontent_shortcode', $post_title);
4158 + if (strpos($post_title, '%%') === false)
4159 + {
4160 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
4161 + {
4162 + $posts = get_posts(
4163 + array(
4164 + 'post_type' => $post_type,
4165 + 'title' => html_entity_decode($post_title),
4166 + 'post_status' => 'all',
4167 + 'numberposts' => 1,
4168 + 'update_post_term_cache' => false,
4169 + 'update_post_meta_cache' => false,
4170 + 'orderby' => 'post_date ID',
4171 + 'order' => 'ASC',
4172 + )
4173 + );
4174 + if ( ! empty( $posts ) ) {
4175 + $zap = $posts[0];
4176 + } else {
4177 + $zap = null;
4178 + }
4179 + if($zap !== null)
4180 + {
4181 + if($overwrite_existing)
4182 + {
4183 + $update_post_id = $zap->ID;
4184 + }
4185 + else
4186 + {
4187 + aiomatic_log_to_file('Post with specified title existing, skipping it: ' . $post_title);
4188 + unset($post_title_lines[$current_index]);
4189 + continue;
4190 + }
4191 + }
4192 + }
4193 + $new_post_title = $post_title;
4194 + }
4195 + else
4196 + {
4197 + $new_post_title = $post_title;
4198 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
4199 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
4200 + {
4201 + $posts = get_posts(
4202 + array(
4203 + 'post_type' => $post_type,
4204 + 'title' => html_entity_decode($new_post_title),
4205 + 'post_status' => 'all',
4206 + 'numberposts' => 1,
4207 + 'update_post_term_cache' => false,
4208 + 'update_post_meta_cache' => false,
4209 + 'orderby' => 'post_date ID',
4210 + 'order' => 'ASC',
4211 + )
4212 + );
4213 + if ( ! empty( $posts ) ) {
4214 + $zap = $posts[0];
4215 + } else {
4216 + $zap = null;
4217 + }
4218 + if($zap !== null)
4219 + {
4220 + if($overwrite_existing)
4221 + {
4222 + $update_post_id = $zap->ID;
4223 + }
4224 + else
4225 + {
4226 + if (filter_var($post_title, FILTER_VALIDATE_URL) === false && stristr($post_title, '%%ai_generated_title%%') === false)
4227 + {
4228 + aiomatic_log_to_file('Post with specified title already published, skipping it: ' . $new_post_title);
4229 + unset($post_title_lines[$current_index]);
4230 + continue;
4231 + }
4232 + }
4233 + }
4234 + }
4235 + }
4236 + }
4237 + if(empty($post_title))
4238 + {
4239 + aiomatic_log_to_file('Empty post title submitted, nothing to do!');
4240 + unset($post_title_lines[$current_index]);
4241 + continue;
4242 + }
4243 + if(stristr($run_regex_on, 'sections') !== false)
4244 + {
4245 + if ($strip_by_regex_prompts !== '')
4246 + {
4247 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
4248 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
4249 + $xcnt = 0;
4250 + foreach($xstrip_by_regex as $sbr)
4251 + {
4252 + if(isset($xreplace_regex[$xcnt]))
4253 + {
4254 + $repreg = $xreplace_regex[$xcnt];
4255 + }
4256 + else
4257 + {
4258 + $repreg = '';
4259 + }
4260 + $xcnt++;
4261 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $sections_prompt);
4262 + if($temp_cont !== NULL)
4263 + {
4264 + $sections_prompt = $temp_cont;
4265 + }
4266 + }
4267 + }
4268 + }
4269 + if($sections_prompt != '' && $post_sections_list == '')
4270 + {
4271 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
4272 + {
4273 + $sections_prompt_arr = preg_split('/\r\n|\r|\n/', $sections_prompt);
4274 + $my_post_section = $sections_prompt_arr[array_rand($sections_prompt_arr)];
4275 + }
4276 + else
4277 + {
4278 + $my_post_section = $sections_prompt;
4279 + }
4280 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4281 + $my_post_section = aiomatic_replacetopics($my_post_section, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4282 + $my_post_section = aiomatic_replaceAIPostShortcodes($my_post_section, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4283 + if($my_post_section != '')
4284 + {
4285 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based sections'));
4286 + $written = aiomatic_write_content($my_post_section, $topic_sections_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'sections', 'user', $assistant_id, $thread_id, $store_data);
4287 + if($written === false)
4288 + {
4289 + aiomatic_log_to_file('Failed to generate sections!');
4290 + if($auto == 1)
4291 + {
4292 + aiomatic_clearFromList($param, $type);
4293 + }
4294 + return 'fail';
4295 + }
4296 + else
4297 + {
4298 + $post_sections = $written;
4299 + }
4300 + }
4301 + else
4302 + {
4303 + aiomatic_log_to_file('Empty processed sections prompt (2). Nothing to do.');
4304 + if($auto == 1)
4305 + {
4306 + aiomatic_clearFromList($param, $type);
4307 + }
4308 + return 'fail';
4309 + }
4310 + }
4311 + else
4312 + {
4313 + if($post_sections_list != '')
4314 + {
4315 + $post_sections = $spintax->Parse($post_sections_list);
4316 + $post_sections = str_replace('%%topic%%', $post_topic, $post_sections);
4317 + }
4318 + }
4319 + $post_sections = aiomatic_filter_sections($post_sections);
4320 + $new_post_content = '';
4321 + if(stristr($run_regex_on, 'intro') !== false)
4322 + {
4323 + if ($strip_by_regex_prompts !== '')
4324 + {
4325 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
4326 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
4327 + $xcnt = 0;
4328 + foreach($xstrip_by_regex as $sbr)
4329 + {
4330 + if(isset($xreplace_regex[$xcnt]))
4331 + {
4332 + $repreg = $xreplace_regex[$xcnt];
4333 + }
4334 + else
4335 + {
4336 + $repreg = '';
4337 + }
4338 + $xcnt++;
4339 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $intro_prompt);
4340 + if($temp_cont !== NULL)
4341 + {
4342 + $intro_prompt = $temp_cont;
4343 + }
4344 + }
4345 + }
4346 + }
4347 + if($intro_prompt != '')
4348 + {
4349 + $my_intro_prompt = $intro_prompt;
4350 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4351 + $my_intro_prompt = aiomatic_replacetopics($my_intro_prompt, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4352 + $my_intro_prompt = aiomatic_replaceAIPostShortcodes($my_intro_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4353 + if($my_intro_prompt != '')
4354 + {
4355 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based intro'));
4356 + $written = aiomatic_write_content($my_intro_prompt, $topic_intro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'intro', 'user', $assistant_id, $thread_id, $store_data);
4357 + if($written === false)
4358 + {
4359 + aiomatic_log_to_file('Failed to generate intro for section: ' . $current_section);
4360 + unset($post_title_lines[$current_index]);
4361 + continue;
4362 + }
4363 + else
4364 + {
4365 + if($new_post_content != '')
4366 + {
4367 + $new_post_content .= ' ';
4368 + }
4369 + $new_post_content .= $written;
4370 + }
4371 + }
4372 + else
4373 + {
4374 + aiomatic_log_to_file('Empty processed intro prompt. Nothing to do.');
4375 + }
4376 + }
4377 + if(stristr($topic_images_bck, '-') !== false)
4378 + {
4379 + preg_match('/\s*(\d+)\s*-\s*(\d+)\s*/', $topic_images_bck, $matches);
4380 + if(isset($matches[2]) && $matches[2] != '' && isset($matches[1]) && $matches[1] != '')
4381 + {
4382 + $txmin = intval($matches[1]);
4383 + $txmax = intval($matches[2]);
4384 + $topic_images = mt_rand($txmin, $txmax);
4385 + }
4386 + else
4387 + {
4388 + $topic_images = $topic_images_bck;
4389 + }
4390 + }
4391 + if($heading_image_loc == 'bottom')
4392 + {
4393 + $img_go = false;
4394 + $vid_go = false;
4395 + $vid_stopped = false;
4396 + $imgage_fail = true;
4397 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
4398 + {
4399 + $img_go = true;
4400 + }
4401 + if($topic_videos != '' && is_numeric($topic_videos) && $topic_videos > $added_videos)
4402 + {
4403 + $vid_go = true;
4404 + }
4405 + if($img_go == true && $vid_go == true)
4406 + {
4407 + $locrandme = wp_rand(1,100);
4408 + if($locrandme > 50)
4409 + {
4410 + $vid_go = false;
4411 + $vid_stopped = true;
4412 + }
4413 + else
4414 + {
4415 + $img_go = false;
4416 + }
4417 + }
4418 + if(isset($aiomatic_Main_Settings['no_intro_image']) && trim($aiomatic_Main_Settings['no_intro_image']) == 'on')
4419 + {
4420 + $img_go = false;
4421 + }
4422 + if($img_go == true)
4423 + {
4424 + if($new_post_content !== '')
4425 + {
4426 + $the_image_prompt = $post_title;
4427 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
4428 + {
4429 + $ai_command_image_article = $orig_ai_command_image_article;
4430 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
4431 + {
4432 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
4433 + $ai_command_image_article = array_filter($ai_command_image_article);
4434 + if(count($ai_command_image_article) > 0)
4435 + {
4436 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
4437 + }
4438 + else
4439 + {
4440 + $ai_command_image_article = '';
4441 + }
4442 + }
4443 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
4444 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4445 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4446 + if(!empty($ai_command_image_article))
4447 + {
4448 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4449 + }
4450 + else
4451 + {
4452 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
4453 + }
4454 + $ai_command_image_article = trim($ai_command_image_article);
4455 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
4456 + {
4457 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
4458 + if ($txt_content !== FALSE)
4459 + {
4460 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4461 + $txt_content = array_filter($txt_content);
4462 + if(count($txt_content) > 0)
4463 + {
4464 + $txt_content = $txt_content[array_rand($txt_content)];
4465 + if(trim($txt_content) != '')
4466 + {
4467 + $ai_command_image_article = $txt_content;
4468 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4469 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4470 + }
4471 + }
4472 + }
4473 + }
4474 + if(empty($ai_command_image_article))
4475 + {
4476 + aiomatic_log_to_file('Empty API image seed expression provided!');
4477 + }
4478 + else
4479 + {
4480 + if(aiomatic_strlen($ai_command_image_article) > 1000)
4481 + {
4482 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
4483 + }
4484 + $the_image_prompt = $ai_command_image_article;
4485 + }
4486 + }
4487 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Searching for images'));
4488 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
4489 + if(!empty($attached_id))
4490 + {
4491 + $additional_attachments[] = $attached_id;
4492 + }
4493 + if($new_img !== false && !in_array($new_img, $added_img_list))
4494 + {
4495 + $new_post_content .= '<br/><img class="timage_class" src="' . $new_img . '" alt="' . esc_attr($post_title) . '"><br/>';
4496 + $added_img_list[] = $new_img;
4497 + $added_images++;
4498 + $imgage_fail = false;
4499 + }
4500 + }
4501 + }
4502 + if($vid_go == true || ($imgage_fail == true && $vid_stopped == true))
4503 + {
4504 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
4505 + {
4506 + $social_list = $aiomatic_Main_Settings['social_list'];
4507 + }
4508 + else
4509 + {
4510 + $social_list = array();
4511 + }
4512 + $new_vid = aiomatic_get_video($current_section, '', $social_list);
4513 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
4514 + {
4515 + $new_post_content .= $new_vid;
4516 + $added_vid_list[] = $new_vid;
4517 + $added_videos++;
4518 + }
4519 + }
4520 + }
4521 + if($title_outro != '')
4522 + {
4523 + $my_title_outro = $title_outro;
4524 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4525 + $my_title_outro = aiomatic_replacetopics($my_title_outro, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4526 + $my_title_outro = aiomatic_replaceAIPostShortcodes($my_title_outro, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4527 + $my_title_outro = $spintax->Parse(trim($my_title_outro));
4528 + }
4529 + if(!empty($title_toc))
4530 + {
4531 + $my_title_toc = $title_toc;
4532 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4533 + $my_title_toc = aiomatic_replacetopics($my_title_toc, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4534 + $my_title_toc = aiomatic_replaceAIPostShortcodes($my_title_toc, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4535 + $my_title_toc = $spintax->Parse(trim($my_title_toc));
4536 + }
4537 + if($title_qa != '')
4538 + {
4539 + $my_title_qa = $title_qa;
4540 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4541 + $my_title_qa = aiomatic_replacetopics($my_title_qa, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4542 + $my_title_qa = aiomatic_replaceAIPostShortcodes($my_title_qa, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4543 + $my_title_qa = $spintax->Parse(trim($my_title_qa));
4544 + }
4545 + if($content_prompt != '')
4546 + {
4547 + $last_section_content = '';
4548 + if($single_content_call == '1')
4549 + {
4550 + $current_section = $post_sections;
4551 + $my_post_content = $content_prompt;
4552 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4553 + $my_post_content = aiomatic_replacetopics($my_post_content, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4554 + $my_post_content = aiomatic_replaceAIPostShortcodes($my_post_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4555 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
4556 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
4557 + if(stristr($run_regex_on, 'content') !== false)
4558 + {
4559 + if ($strip_by_regex_prompts !== '')
4560 + {
4561 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
4562 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
4563 + $xcnt = 0;
4564 + foreach($xstrip_by_regex as $sbr)
4565 + {
4566 + if(isset($xreplace_regex[$xcnt]))
4567 + {
4568 + $repreg = $xreplace_regex[$xcnt];
4569 + }
4570 + else
4571 + {
4572 + $repreg = '';
4573 + }
4574 + $xcnt++;
4575 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
4576 + if($temp_cont !== NULL)
4577 + {
4578 + $my_post_content = $temp_cont;
4579 + }
4580 + }
4581 + }
4582 + }
4583 + if($my_post_content != '')
4584 + {
4585 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based single content'));
4586 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
4587 + if($written === false)
4588 + {
4589 + aiomatic_log_to_file('Failed to generate topic list for section: ' . $current_section);
4590 + unset($post_title_lines[$current_index]);
4591 + continue;
4592 + }
4593 + else
4594 + {
4595 + if($new_post_content != '')
4596 + {
4597 + $new_post_content .= ' ';
4598 + }
4599 + $new_img_arr = array();
4600 + if($topic_images != '' && is_numeric($topic_images) && intval($topic_images) - 2 > $added_images)
4601 + {
4602 + $howmore = intval($topic_images) - 2;
4603 + for($cont = 0; $cont < $howmore; $cont++)
4604 + {
4605 + if($search_query_repetition == '1')
4606 + {
4607 + $the_image_prompt = $post_title;
4608 + }
4609 + else
4610 + {
4611 + $the_image_prompt = $current_section;
4612 + }
4613 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
4614 + {
4615 + $ai_command_image_article = $orig_ai_command_image_article;
4616 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
4617 + {
4618 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
4619 + $ai_command_image_article = array_filter($ai_command_image_article);
4620 + if(count($ai_command_image_article) > 0)
4621 + {
4622 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
4623 + }
4624 + else
4625 + {
4626 + $ai_command_image_article = '';
4627 + }
4628 + }
4629 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
4630 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4631 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4632 + if(!empty($ai_command_image_article))
4633 + {
4634 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4635 + }
4636 + else
4637 + {
4638 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
4639 + }
4640 + $ai_command_image_article = trim($ai_command_image_article);
4641 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
4642 + {
4643 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
4644 + if ($txt_content !== FALSE)
4645 + {
4646 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4647 + $txt_content = array_filter($txt_content);
4648 + if(count($txt_content) > 0)
4649 + {
4650 + $txt_content = $txt_content[array_rand($txt_content)];
4651 + if(trim($txt_content) != '')
4652 + {
4653 + $ai_command_image_article = $txt_content;
4654 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4655 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4656 + }
4657 + }
4658 + }
4659 + }
4660 + if(empty($ai_command_image_article))
4661 + {
4662 + aiomatic_log_to_file('Empty API image seed expression provided!');
4663 + }
4664 + else
4665 + {
4666 + if(aiomatic_strlen($ai_command_image_article) > 1000)
4667 + {
4668 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
4669 + }
4670 + $the_image_prompt = $ai_command_image_article;
4671 + }
4672 + }
4673 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
4674 + if(!empty($attached_id))
4675 + {
4676 + $additional_attachments[] = $attached_id;
4677 + }
4678 + if($new_img !== false && !in_array($new_img, $added_img_list))
4679 + {
4680 + $new_post_content .= '<br/><img class="pimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
4681 + $added_img_list[] = $new_img;
4682 + $new_img_arr[] = $new_img;
4683 + $added_images++;
4684 + }
4685 + }
4686 + }
4687 + if(count($new_img_arr) > 0)
4688 + {
4689 + $written_arr = preg_split("/\r\n|\n|\r/", $written);
4690 + if(count($written_arr) > 1)
4691 + {
4692 + foreach($new_img_arr as $new_img_x)
4693 + {
4694 + array_splice($written_arr, wp_rand(0, count($written_arr)), 0, '<br/><img src="' . $new_img_x . '">');
4695 + }
4696 + $written = implode('\r\n', $written_arr);
4697 + }
4698 + }
4699 + $new_vid_arr = array();
4700 + $new_post_content .= $written;
4701 + if($topic_videos != '' && is_numeric($topic_videos) && intval($topic_videos) > $added_videos)
4702 + {
4703 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
4704 + {
4705 + $social_list = $aiomatic_Main_Settings['social_list'];
4706 + }
4707 + else
4708 + {
4709 + $social_list = array();
4710 + }
4711 + $howmore = intval($topic_videos) - $added_videos;
4712 + for($cont = 0; $cont < $howmore; $cont++)
4713 + {
4714 + $new_vid = aiomatic_get_video($current_section, '', $social_list);
4715 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
4716 + {
4717 + $new_post_content .= $new_vid;
4718 + $added_vid_list[] = $new_vid;
4719 + $new_vid_arr[] = $new_vid;
4720 + $added_videos++;
4721 + }
4722 + }
4723 + }
4724 + if(count($new_vid_arr) > 0)
4725 + {
4726 + $written_arr = preg_split("/\r\n|\n|\r/", $written);
4727 + if(count($written_arr) > 1)
4728 + {
4729 + foreach($new_vid_arr as $new_img_x)
4730 + {
4731 + array_splice($written_arr, wp_rand(0, count($written_arr)), 0, '<br/><img src="' . $new_img_x . '">');
4732 + }
4733 + $written = implode('\r\n', $written_arr);
4734 + }
4735 + }
4736 + }
4737 + }
4738 + else
4739 + {
4740 + aiomatic_log_to_file('Empty processed topic prompt. Nothing to do.');
4741 + }
4742 + }
4743 + else
4744 + {
4745 + if($post_sections != '')
4746 + {
4747 + $post_sections_arr = preg_split('/\r\n|\r|\n/', $post_sections);
4748 + }
4749 + else
4750 + {
4751 + $post_sections_arr = array();
4752 + }
4753 + if(stristr($section_count, '-') === false && is_numeric($section_count))
4754 + {
4755 + $snum = intval($section_count);
4756 + if($snum > 0 && count($post_sections_arr) > $snum)
4757 + {
4758 + $post_sections_arr = array_slice($post_sections_arr, 0, $snum);
4759 + }
4760 + }
4761 + $toc = '';
4762 + if($enable_toc == '1')
4763 + {
4764 + if(!empty($title_toc))
4765 + {
4766 + if($my_title_toc != '')
4767 + {
4768 + $toc .= '<h2>' . $my_title_toc . '</h2>';
4769 + }
4770 + }
4771 + $toc .= '<ul class="toc-class">';
4772 + foreach($post_sections_arr as $current_section)
4773 + {
4774 + if(!empty($current_section))
4775 + {
4776 + $toc .= '<li><a href="#' . sanitize_title($current_section) . '">' . $current_section . '</a></li>';
4777 + }
4778 + }
4779 + if($title_qa != '')
4780 + {
4781 + if($my_title_qa != '')
4782 + {
4783 + if($qa_prompt != '' && $enable_qa == '1')
4784 + {
4785 + $toc .= '<li><a href="#qa">' . $my_title_qa . '</a></li>';
4786 + }
4787 + }
4788 + }
4789 + if($title_outro != '')
4790 + {
4791 + if($my_title_outro != '')
4792 + {
4793 + if($outro_prompt != '')
4794 + {
4795 + $toc .= '<li><a href="#outro">' . $my_title_outro . '</a></li>';
4796 + }
4797 + }
4798 + }
4799 + $toc .= '</ul>';
4800 + $new_post_content .= '<br/>' . $toc . '<br/>';
4801 + }
4802 + $section_cont = 1;
4803 + $last_section_content = '';
4804 + foreach($post_sections_arr as $current_section)
4805 + {
4806 + $current_section = trim($current_section);
4807 + $current_section = trim($current_section, '.;');
4808 + if(empty($current_section))
4809 + {
4810 + continue;
4811 + }
4812 + $my_post_content = $content_prompt;
4813 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
4814 + $my_post_content = aiomatic_replacetopics($my_post_content, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4815 + $my_post_content = aiomatic_replaceAIPostShortcodes($my_post_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4816 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
4817 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
4818 + if(stristr($run_regex_on, 'content') !== false)
4819 + {
4820 + if ($strip_by_regex_prompts !== '')
4821 + {
4822 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
4823 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
4824 + $xcnt = 0;
4825 + foreach($xstrip_by_regex as $sbr)
4826 + {
4827 + if(isset($xreplace_regex[$xcnt]))
4828 + {
4829 + $repreg = $xreplace_regex[$xcnt];
4830 + }
4831 + else
4832 + {
4833 + $repreg = '';
4834 + }
4835 + $xcnt++;
4836 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
4837 + if($temp_cont !== NULL)
4838 + {
4839 + $my_post_content = $temp_cont;
4840 + }
4841 + }
4842 + }
4843 + }
4844 + if($my_post_content != '')
4845 + {
4846 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based section #' . $section_cont));
4847 + $section_cont++;
4848 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
4849 + if($written === false)
4850 + {
4851 + aiomatic_log_to_file('Failed to generate content for section: ' . $current_section);
4852 + continue;
4853 + }
4854 + else
4855 + {
4856 + if($heading_image_loc == 'top')
4857 + {
4858 + $img_go = false;
4859 + $vid_go = false;
4860 + $vid_stopped = false;
4861 + $imgage_fail = true;
4862 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
4863 + {
4864 + $img_go = true;
4865 + }
4866 + if($topic_videos != '' && is_numeric($topic_videos) && $topic_videos > $added_videos)
4867 + {
4868 + $vid_go = true;
4869 + }
4870 + if($img_go == true && $vid_go == true)
4871 + {
4872 + $locrandme = wp_rand(1,100);
4873 + if($locrandme > 50)
4874 + {
4875 + $vid_go = false;
4876 + $vid_stopped = true;
4877 + }
4878 + else
4879 + {
4880 + $img_go = false;
4881 + }
4882 + }
4883 + if($img_go == true)
4884 + {
4885 + if($new_post_content !== '')
4886 + {
4887 + if($search_query_repetition == '1')
4888 + {
4889 + $the_image_prompt = $post_title;
4890 + }
4891 + else
4892 + {
4893 + $the_image_prompt = $current_section;
4894 + }
4895 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
4896 + {
4897 + $ai_command_image_article = $orig_ai_command_image_article;
4898 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
4899 + {
4900 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
4901 + $ai_command_image_article = array_filter($ai_command_image_article);
4902 + if(count($ai_command_image_article) > 0)
4903 + {
4904 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
4905 + }
4906 + else
4907 + {
4908 + $ai_command_image_article = '';
4909 + }
4910 + }
4911 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
4912 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
4913 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4914 + if(!empty($ai_command_image_article))
4915 + {
4916 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4917 + }
4918 + else
4919 + {
4920 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
4921 + }
4922 + $ai_command_image_article = trim($ai_command_image_article);
4923 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
4924 + {
4925 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
4926 + if ($txt_content !== FALSE)
4927 + {
4928 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
4929 + $txt_content = array_filter($txt_content);
4930 + if(count($txt_content) > 0)
4931 + {
4932 + $txt_content = $txt_content[array_rand($txt_content)];
4933 + if(trim($txt_content) != '')
4934 + {
4935 + $ai_command_image_article = $txt_content;
4936 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
4937 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
4938 + }
4939 + }
4940 + }
4941 + }
4942 + if(empty($ai_command_image_article))
4943 + {
4944 + aiomatic_log_to_file('Empty API image seed expression provided!');
4945 + }
4946 + else
4947 + {
4948 + if(aiomatic_strlen($ai_command_image_article) > 1000)
4949 + {
4950 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
4951 + }
4952 + $the_image_prompt = $ai_command_image_article;
4953 + }
4954 + }
4955 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
4956 + if(!empty($attached_id))
4957 + {
4958 + $additional_attachments[] = $attached_id;
4959 + }
4960 + if($new_img !== false && !in_array($new_img, $added_img_list))
4961 + {
4962 + $new_post_content .= '<br/><img class="kimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
4963 + $added_img_list[] = $new_img;
4964 + $added_images++;
4965 + $imgage_fail = false;
4966 + }
4967 + }
4968 + }
4969 + if($vid_go == true || ($imgage_fail == true && $vid_stopped == true))
4970 + {
4971 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
4972 + {
4973 + $social_list = $aiomatic_Main_Settings['social_list'];
4974 + }
4975 + else
4976 + {
4977 + $social_list = array();
4978 + }
4979 + $new_vid = aiomatic_get_video($current_section, '', $social_list);
4980 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
4981 + {
4982 + $new_post_content .= $new_vid;
4983 + $added_vid_list[] = $new_vid;
4984 + $added_videos++;
4985 + }
4986 + }
4987 + }
4988 + $written = str_ireplace($current_section, '', $written);
4989 + preg_match_all('#\d+\.\s*([\s\S]*)#', $current_section, $mxatches);
4990 + if(isset($mxatches[1][0]))
4991 + {
4992 + $written = str_ireplace(trim($mxatches[1][0]), '', $written);
4993 + $written = str_ireplace(str_replace(['"', '\''], '', $mxatches[1][0]), '', $written);
4994 + }
4995 + $written = str_ireplace('<h2></h2>', '', $written);
4996 + $written = str_ireplace('<h3></h3>', '', $written);
4997 + if($sections_role == 'h2')
4998 + {
4999 + $new_post_content .= '<h2 id="' . sanitize_title($current_section) . '">' . $current_section . '</h2>';
5000 + }
5001 + elseif($sections_role == 'h3')
5002 + {
5003 + $new_post_content .= '<h3 id="' . sanitize_title($current_section) . '">' . $current_section . '</h3>';
5004 + }
5005 + elseif($sections_role == 'b')
5006 + {
5007 + $new_post_content .= '<b id="' . sanitize_title($current_section) . '">' . $current_section . '</b>';
5008 + }
5009 + elseif($sections_role == 'i')
5010 + {
5011 + $new_post_content .= '<i id="' . sanitize_title($current_section) . '">' . $current_section . '</i>';
5012 + }
5013 + elseif($sections_role == 'bi')
5014 + {
5015 + $new_post_content .= '<b><i id="' . sanitize_title($current_section) . '">' . $current_section . '</i></b>';
5016 + }
5017 + elseif($sections_role == 'p')
5018 + {
5019 + $new_post_content .= '<p id="' . sanitize_title($current_section) . '">' . $current_section . '</p>';
5020 + }
5021 + elseif($sections_role == 'x')
5022 + {
5023 + $new_post_content .= '<br/><span id="' . sanitize_title($current_section) . '">' . $current_section . '</span><br/>';
5024 + }
5025 + if($heading_image_loc == 'heading')
5026 + {
5027 + $img_go = false;
5028 + $vid_go = false;
5029 + $vid_stopped = false;
5030 + $imgage_fail = true;
5031 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
5032 + {
5033 + $img_go = true;
5034 + }
5035 + if($topic_videos != '' && is_numeric($topic_videos) && $topic_videos > $added_videos)
5036 + {
5037 + $vid_go = true;
5038 + }
5039 + if($img_go == true && $vid_go == true)
5040 + {
5041 + $locrandme = wp_rand(1,100);
5042 + if($locrandme > 50)
5043 + {
5044 + $vid_go = false;
5045 + $vid_stopped = true;
5046 + }
5047 + else
5048 + {
5049 + $img_go = false;
5050 + }
5051 + }
5052 + if($img_go == true)
5053 + {
5054 + if($new_post_content !== '')
5055 + {
5056 + if($search_query_repetition == '1')
5057 + {
5058 + $the_image_prompt = $post_title;
5059 + }
5060 + else
5061 + {
5062 + $the_image_prompt = $current_section;
5063 + }
5064 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
5065 + {
5066 + $ai_command_image_article = $orig_ai_command_image_article;
5067 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5068 + {
5069 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
5070 + $ai_command_image_article = array_filter($ai_command_image_article);
5071 + if(count($ai_command_image_article) > 0)
5072 + {
5073 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
5074 + }
5075 + else
5076 + {
5077 + $ai_command_image_article = '';
5078 + }
5079 + }
5080 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
5081 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5082 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5083 + if(!empty($ai_command_image_article))
5084 + {
5085 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5086 + }
5087 + else
5088 + {
5089 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
5090 + }
5091 + $ai_command_image_article = trim($ai_command_image_article);
5092 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
5093 + {
5094 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
5095 + if ($txt_content !== FALSE)
5096 + {
5097 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5098 + $txt_content = array_filter($txt_content);
5099 + if(count($txt_content) > 0)
5100 + {
5101 + $txt_content = $txt_content[array_rand($txt_content)];
5102 + if(trim($txt_content) != '')
5103 + {
5104 + $ai_command_image_article = $txt_content;
5105 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5106 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5107 + }
5108 + }
5109 + }
5110 + }
5111 + if(empty($ai_command_image_article))
5112 + {
5113 + aiomatic_log_to_file('Empty API image seed expression provided!');
5114 + }
5115 + else
5116 + {
5117 + if(aiomatic_strlen($ai_command_image_article) > 1000)
5118 + {
5119 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
5120 + }
5121 + $the_image_prompt = $ai_command_image_article;
5122 + }
5123 + }
5124 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
5125 + if(!empty($attached_id))
5126 + {
5127 + $additional_attachments[] = $attached_id;
5128 + }
5129 + if($new_img !== false && !in_array($new_img, $added_img_list))
5130 + {
5131 + $new_post_content .= '<br/><img class="gimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
5132 + $added_img_list[] = $new_img;
5133 + $added_images++;
5134 + $imgage_fail = false;
5135 + }
5136 + }
5137 + }
5138 + if($vid_go == true || ($imgage_fail == true && $vid_stopped == true))
5139 + {
5140 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
5141 + {
5142 + $social_list = $aiomatic_Main_Settings['social_list'];
5143 + }
5144 + else
5145 + {
5146 + $social_list = array();
5147 + }
5148 + $new_vid = aiomatic_get_video($current_section, '', $social_list);
5149 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
5150 + {
5151 + $new_post_content .= $new_vid;
5152 + $added_vid_list[] = $new_vid;
5153 + $added_videos++;
5154 + }
5155 + }
5156 + }
5157 + $last_section_content = $written;
5158 + $new_post_content .= $written;
5159 + }
5160 + if($heading_image_loc == 'bottom')
5161 + {
5162 + $img_go = false;
5163 + $vid_go = false;
5164 + $vid_stopped = false;
5165 + $imgage_fail = true;
5166 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
5167 + {
5168 + $img_go = true;
5169 + }
5170 + if($topic_videos != '' && is_numeric($topic_videos) && $topic_videos > $added_videos)
5171 + {
5172 + $vid_go = true;
5173 + }
5174 + if($img_go == true && $vid_go == true)
5175 + {
5176 + $locrandme = wp_rand(1,100);
5177 + if($locrandme > 50)
5178 + {
5179 + $vid_go = false;
5180 + $vid_stopped = true;
5181 + }
5182 + else
5183 + {
5184 + $img_go = false;
5185 + }
5186 + }
5187 + if($img_go)
5188 + {
5189 + if($new_post_content !== '')
5190 + {
5191 + if($search_query_repetition == '1')
5192 + {
5193 + $the_image_prompt = $post_title;
5194 + }
5195 + else
5196 + {
5197 + $the_image_prompt = $current_section;
5198 + }
5199 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
5200 + {
5201 + $ai_command_image_article = $orig_ai_command_image_article;
5202 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5203 + {
5204 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
5205 + $ai_command_image_article = array_filter($ai_command_image_article);
5206 + if(count($ai_command_image_article) > 0)
5207 + {
5208 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
5209 + }
5210 + else
5211 + {
5212 + $ai_command_image_article = '';
5213 + }
5214 + }
5215 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
5216 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5217 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5218 + if(!empty($ai_command_image_article))
5219 + {
5220 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5221 + }
5222 + else
5223 + {
5224 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
5225 + }
5226 + $ai_command_image_article = trim($ai_command_image_article);
5227 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
5228 + {
5229 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
5230 + if ($txt_content !== FALSE)
5231 + {
5232 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5233 + $txt_content = array_filter($txt_content);
5234 + if(count($txt_content) > 0)
5235 + {
5236 + $txt_content = $txt_content[array_rand($txt_content)];
5237 + if(trim($txt_content) != '')
5238 + {
5239 + $ai_command_image_article = $txt_content;
5240 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5241 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5242 + }
5243 + }
5244 + }
5245 + }
5246 + if(empty($ai_command_image_article))
5247 + {
5248 + aiomatic_log_to_file('Empty API image seed expression provided!');
5249 + }
5250 + else
5251 + {
5252 + if(aiomatic_strlen($ai_command_image_article) > 1000)
5253 + {
5254 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
5255 + }
5256 + $the_image_prompt = $ai_command_image_article;
5257 + }
5258 + }
5259 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
5260 + if(!empty($attached_id))
5261 + {
5262 + $additional_attachments[] = $attached_id;
5263 + }
5264 + if($new_img !== false && !in_array($new_img, $added_img_list))
5265 + {
5266 + $new_post_content .= '<br/><img class="kimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
5267 + $added_img_list[] = $new_img;
5268 + $added_images++;
5269 + $imgage_fail = false;
5270 + }
5271 + }
5272 + }
5273 + if($vid_go == true || ($imgage_fail == true && $vid_stopped == true))
5274 + {
5275 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
5276 + {
5277 + $social_list = $aiomatic_Main_Settings['social_list'];
5278 + }
5279 + else
5280 + {
5281 + $social_list = array();
5282 + }
5283 + $new_vid = aiomatic_get_video($current_section, '', $social_list);
5284 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
5285 + {
5286 + $new_post_content .= $new_vid;
5287 + $added_vid_list[] = $new_vid;
5288 + $added_videos++;
5289 + }
5290 + }
5291 + }
5292 + }
5293 + else
5294 + {
5295 + aiomatic_log_to_file('Empty processed content prompt. Nothing to do.');
5296 + }
5297 + }
5298 + }
5299 + }
5300 + if($enable_qa == '1')
5301 + {
5302 + if($qa_prompt != '')
5303 + {
5304 + $my_qa_prompt = $qa_prompt;
5305 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5306 + $my_qa_prompt = aiomatic_replacetopics($my_qa_prompt, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5307 + $my_qa_prompt = aiomatic_replaceAIPostShortcodes($my_qa_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5308 + if(stristr($run_regex_on, 'qa') !== false)
5309 + {
5310 + if ($strip_by_regex_prompts !== '')
5311 + {
5312 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
5313 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
5314 + $xcnt = 0;
5315 + foreach($xstrip_by_regex as $sbr)
5316 + {
5317 + if(isset($xreplace_regex[$xcnt]))
5318 + {
5319 + $repreg = $xreplace_regex[$xcnt];
5320 + }
5321 + else
5322 + {
5323 + $repreg = '';
5324 + }
5325 + $xcnt++;
5326 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_qa_prompt);
5327 + if($temp_cont !== NULL)
5328 + {
5329 + $my_qa_prompt = $temp_cont;
5330 + }
5331 + }
5332 + }
5333 + }
5334 + if($my_qa_prompt != '')
5335 + {
5336 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based qa'));
5337 + $written = aiomatic_write_content($my_qa_prompt, $topic_qa_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'qa', 'user', $assistant_id, $thread_id, $store_data);
5338 + if($written === false)
5339 + {
5340 + aiomatic_log_to_file('Failed to generate Q&A for topic: ' . $current_section);
5341 + unset($post_title_lines[$current_index]);
5342 + continue;
5343 + }
5344 + else
5345 + {
5346 + if($new_post_content != '')
5347 + {
5348 + $new_post_content .= ' ';
5349 + }
5350 + if($my_title_qa !== '')
5351 + {
5352 + if($sections_role == 'h2')
5353 + {
5354 + $new_post_content .= '<h2 id="qa">' . $my_title_qa . '</h2>';
5355 + }
5356 + elseif($sections_role == 'h3')
5357 + {
5358 + $new_post_content .= '<h3 id="qa">' . $my_title_qa . '</h3>';
5359 + }
5360 + elseif($sections_role == 'b')
5361 + {
5362 + $new_post_content .= '<b id="qa">' . $my_title_qa . '</b>';
5363 + }
5364 + elseif($sections_role == 'i')
5365 + {
5366 + $new_post_content .= '<i id="qa">' . $my_title_qa . '</i>';
5367 + }
5368 + elseif($sections_role == 'bi')
5369 + {
5370 + $new_post_content .= '<b><i id="qa">' . $my_title_qa . '</i></b>';
5371 + }
5372 + elseif($sections_role == 'p')
5373 + {
5374 + $new_post_content .= '<p id="qa">' . $my_title_qa . '</p>';
5375 + }
5376 + elseif($sections_role == 'x')
5377 + {
5378 + $new_post_content .= '<br/><span id="qa">' . $my_title_qa . '</span><br/>';
5379 + }
5380 + else
5381 + {
5382 + $new_post_content .= '<br/>' . $my_title_qa . '<br/>';
5383 + }
5384 + }
5385 + $new_post_content .= $written;
5386 + }
5387 + }
5388 + else
5389 + {
5390 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
5391 + }
5392 + }
5393 + }
5394 + if($outro_prompt != '')
5395 + {
5396 + $my_outro_prompt = $outro_prompt;
5397 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5398 + $my_outro_prompt = aiomatic_replacetopics($my_outro_prompt, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5399 + $my_outro_prompt = aiomatic_replaceAIPostShortcodes($my_outro_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5400 + if(stristr($run_regex_on, 'outro') !== false)
5401 + {
5402 + if ($strip_by_regex_prompts !== '')
5403 + {
5404 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
5405 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
5406 + $xcnt = 0;
5407 + foreach($xstrip_by_regex as $sbr)
5408 + {
5409 + if(isset($xreplace_regex[$xcnt]))
5410 + {
5411 + $repreg = $xreplace_regex[$xcnt];
5412 + }
5413 + else
5414 + {
5415 + $repreg = '';
5416 + }
5417 + $xcnt++;
5418 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_outro_prompt);
5419 + if($temp_cont !== NULL)
5420 + {
5421 + $my_outro_prompt = $temp_cont;
5422 + }
5423 + }
5424 + }
5425 + }
5426 + if($my_outro_prompt != '')
5427 + {
5428 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based outro'));
5429 + $written = aiomatic_write_content($my_outro_prompt, $topic_outro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'outro', 'user', $assistant_id, $thread_id, $store_data);
5430 + if($written === false)
5431 + {
5432 + aiomatic_log_to_file('Failed to generate outro for section: ' . $current_section);
5433 + unset($post_title_lines[$current_index]);
5434 + continue;
5435 + }
5436 + else
5437 + {
5438 + if($new_post_content != '')
5439 + {
5440 + $new_post_content .= ' ';
5441 + }
5442 + if($title_outro !== '')
5443 + {
5444 + if($my_title_outro != '')
5445 + {
5446 + if($sections_role == 'h2')
5447 + {
5448 + $new_post_content .= '<h2 id="outro">' . $my_title_outro . '</h2>';
5449 + }
5450 + elseif($sections_role == 'h3')
5451 + {
5452 + $new_post_content .= '<h3 id="outro">' . $my_title_outro . '</h3>';
5453 + }
5454 + elseif($sections_role == 'b')
5455 + {
5456 + $new_post_content .= '<b id="outro">' . $my_title_outro . '</b>';
5457 + }
5458 + elseif($sections_role == 'i')
5459 + {
5460 + $new_post_content .= '<i id="outro">' . $my_title_outro . '</i>';
5461 + }
5462 + elseif($sections_role == 'bi')
5463 + {
5464 + $new_post_content .= '<b><i id="outro">' . $my_title_outro . '</i></b>';
5465 + }
5466 + elseif($sections_role == 'p')
5467 + {
5468 + $new_post_content .= '<p id="outro">' . $my_title_outro . '</p>';
5469 + }
5470 + elseif($sections_role == 'x')
5471 + {
5472 + $new_post_content .= '<br/><span id="outro">' . $my_title_outro . '</span><br/>';
5473 + }
5474 + else
5475 + {
5476 + $new_post_content .= '<br/>' . $my_title_outro . '<br/>';
5477 + }
5478 + }
5479 + }
5480 + else
5481 + {
5482 + $new_post_content .= '<br/><br/>';
5483 + }
5484 + $new_post_content .= $written;
5485 + }
5486 + }
5487 + else
5488 + {
5489 + aiomatic_log_to_file('Empty processed outro prompt. Nothing to do.');
5490 + }
5491 + }
5492 + if($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images)
5493 + {
5494 + if($new_post_content !== '')
5495 + {
5496 + $the_image_prompt = $post_title;
5497 + if($enable_ai_images_article != '0' && $enable_ai_images_article != '3')
5498 + {
5499 + $ai_command_image_article = $orig_ai_command_image_article;
5500 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5501 + {
5502 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
5503 + $ai_command_image_article = array_filter($ai_command_image_article);
5504 + if(count($ai_command_image_article) > 0)
5505 + {
5506 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
5507 + }
5508 + else
5509 + {
5510 + $ai_command_image_article = '';
5511 + }
5512 + }
5513 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
5514 + $ai_command_image_article = aiomatic_replacetopics($ai_command_image_article, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5515 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5516 + if(!empty($ai_command_image_article))
5517 + {
5518 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5519 + }
5520 + else
5521 + {
5522 + $ai_command_image_article = trim(strip_tags($the_image_prompt));
5523 + }
5524 + $ai_command_image_article = trim($ai_command_image_article);
5525 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
5526 + {
5527 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
5528 + if ($txt_content !== FALSE)
5529 + {
5530 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5531 + $txt_content = array_filter($txt_content);
5532 + if(count($txt_content) > 0)
5533 + {
5534 + $txt_content = $txt_content[array_rand($txt_content)];
5535 + if(trim($txt_content) != '')
5536 + {
5537 + $ai_command_image_article = $txt_content;
5538 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
5539 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5540 + }
5541 + }
5542 + }
5543 + }
5544 + if(empty($ai_command_image_article))
5545 + {
5546 + aiomatic_log_to_file('Empty API image seed expression provided!');
5547 + }
5548 + else
5549 + {
5550 + if(aiomatic_strlen($ai_command_image_article) > 1000)
5551 + {
5552 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
5553 + }
5554 + $the_image_prompt = $ai_command_image_article;
5555 + }
5556 + }
5557 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images_article, $image_size_article, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model_article);
5558 + if(!empty($attached_id))
5559 + {
5560 + $additional_attachments[] = $attached_id;
5561 + }
5562 + if($new_img !== false && !in_array($new_img, $added_img_list))
5563 + {
5564 + $new_post_content .= '<br/><img class="bimage_class" src="' . $new_img . '" alt="' . esc_attr($post_title) . '"><br/>';
5565 + $added_img_list[] = $new_img;
5566 + $added_images++;
5567 + }
5568 + }
5569 + }
5570 + if($new_post_content == '')
5571 + {
5572 + aiomatic_log_to_file("Warning, empty post content because of empty content prompt!");
5573 + }
5574 +
5575 + $new_post_excerpt = '';
5576 + $current_section = $post_sections;
5577 + if($excerpt_prompt != '')
5578 + {
5579 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5580 + {
5581 + $excerpt_prompt_arr = preg_split('/\r\n|\r|\n/', $excerpt_prompt);
5582 + $my_post_excerpt = $excerpt_prompt_arr[array_rand($excerpt_prompt_arr)];
5583 + }
5584 + else
5585 + {
5586 + $my_post_excerpt = $excerpt_prompt;
5587 + }
5588 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5589 + $my_post_excerpt = aiomatic_replacetopics($my_post_excerpt, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5590 + $my_post_excerpt = aiomatic_replaceAIPostShortcodes($my_post_excerpt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5591 + if(stristr($run_regex_on, 'excerpt') !== false)
5592 + {
5593 + if ($strip_by_regex_prompts !== '')
5594 + {
5595 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
5596 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
5597 + $xcnt = 0;
5598 + foreach($xstrip_by_regex as $sbr)
5599 + {
5600 + if(isset($xreplace_regex[$xcnt]))
5601 + {
5602 + $repreg = $xreplace_regex[$xcnt];
5603 + }
5604 + else
5605 + {
5606 + $repreg = '';
5607 + }
5608 + $xcnt++;
5609 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_excerpt);
5610 + if($temp_cont !== NULL)
5611 + {
5612 + $my_post_excerpt = $temp_cont;
5613 + }
5614 + }
5615 + }
5616 + }
5617 + if($my_post_excerpt != '')
5618 + {
5619 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating topic based excerpt'));
5620 + $written = aiomatic_write_content($my_post_excerpt, $topic_excerpt_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'excerpt', 'user', $assistant_id, $thread_id, $store_data);
5621 + if($written === false)
5622 + {
5623 + aiomatic_log_to_file('Failed to generate excerpt!');
5624 + }
5625 + else
5626 + {
5627 + $new_post_excerpt = $written;
5628 + }
5629 + }
5630 + else
5631 + {
5632 + aiomatic_log_to_file('Empty processed excerpt prompt. Nothing to do.');
5633 + }
5634 + }
5635 +
5636 + $get_img = '';
5637 + if($royalty_free == '1')
5638 + {
5639 + if($enable_ai_images == '1')
5640 + {
5641 + $query_words = $post_title;
5642 + if($image_query == '')
5643 + {
5644 + $image_query = $temp_post;
5645 + }
5646 + if($orig_ai_command_image == '')
5647 + {
5648 + $orig_ai_command_image = $image_query;
5649 + }
5650 + if($orig_ai_command_image != '')
5651 + {
5652 + $ai_command_image = $orig_ai_command_image;
5653 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5654 + {
5655 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5656 + $ai_command_image = array_filter($ai_command_image);
5657 + if(count($ai_command_image) > 0)
5658 + {
5659 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5660 + }
5661 + else
5662 + {
5663 + $ai_command_image = '';
5664 + }
5665 + }
5666 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5667 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5668 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5669 + if(!empty($ai_command_image))
5670 + {
5671 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5672 + }
5673 + else
5674 + {
5675 + $ai_command_image = trim(strip_tags($post_title));
5676 + }
5677 + $ai_command_image = trim($ai_command_image);
5678 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5679 + {
5680 + $txt_content = aiomatic_get_web_page($ai_command_image);
5681 + if ($txt_content !== FALSE)
5682 + {
5683 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5684 + $txt_content = array_filter($txt_content);
5685 + if(count($txt_content) > 0)
5686 + {
5687 + $txt_content = $txt_content[array_rand($txt_content)];
5688 + if(trim($txt_content) != '')
5689 + {
5690 + $ai_command_image = $txt_content;
5691 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5692 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5693 + }
5694 + }
5695 + }
5696 + }
5697 + if(empty($ai_command_image))
5698 + {
5699 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
5700 + }
5701 + else
5702 + {
5703 + if(aiomatic_strlen($ai_command_image) > 1000)
5704 + {
5705 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
5706 + }
5707 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5708 + {
5709 + $api_service = aiomatic_get_api_service($token, $image_model);
5710 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
5711 + }
5712 + $skip_this_copy = true;
5713 + if(!function_exists('is_plugin_active'))
5714 + {
5715 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
5716 + }
5717 + 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')
5718 + {
5719 + $skip_this_copy = false;
5720 + }
5721 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
5722 + {
5723 + $skip_this_copy = false;
5724 + }
5725 + $aierror = '';
5726 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
5727 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
5728 + if($get_img !== false)
5729 + {
5730 + foreach($get_img as $tmpimg)
5731 + {
5732 + $get_img = $tmpimg;
5733 + break;
5734 + }
5735 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5736 + {
5737 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
5738 + }
5739 + }
5740 + else
5741 + {
5742 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
5743 + $get_img = '';
5744 + }
5745 + }
5746 + }
5747 + else
5748 + {
5749 + aiomatic_log_to_file('Empty AI featured image query entered.');
5750 + }
5751 + }
5752 + elseif($enable_ai_images == '2')
5753 + {
5754 + $query_words = $post_title;
5755 + if($image_query == '')
5756 + {
5757 + $image_query = $temp_post;
5758 + }
5759 + if($orig_ai_command_image == '')
5760 + {
5761 + $orig_ai_command_image = $image_query;
5762 + }
5763 + if($orig_ai_command_image != '')
5764 + {
5765 + $ai_command_image = $orig_ai_command_image;
5766 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5767 + {
5768 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5769 + $ai_command_image = array_filter($ai_command_image);
5770 + if(count($ai_command_image) > 0)
5771 + {
5772 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5773 + }
5774 + else
5775 + {
5776 + $ai_command_image = '';
5777 + }
5778 + }
5779 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5780 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5781 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5782 + if(!empty($ai_command_image))
5783 + {
5784 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5785 + }
5786 + else
5787 + {
5788 + $ai_command_image = trim(strip_tags($post_title));
5789 + }
5790 + $ai_command_image = trim($ai_command_image);
5791 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5792 + {
5793 + $txt_content = aiomatic_get_web_page($ai_command_image);
5794 + if ($txt_content !== FALSE)
5795 + {
5796 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5797 + $txt_content = array_filter($txt_content);
5798 + if(count($txt_content) > 0)
5799 + {
5800 + $txt_content = $txt_content[array_rand($txt_content)];
5801 + if(trim($txt_content) != '')
5802 + {
5803 + $ai_command_image = $txt_content;
5804 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5805 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5806 + }
5807 + }
5808 + }
5809 + }
5810 + if(empty($ai_command_image))
5811 + {
5812 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
5813 + }
5814 + else
5815 + {
5816 + if(aiomatic_strlen($ai_command_image) > 2000)
5817 + {
5818 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
5819 + }
5820 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5821 + {
5822 + $api_service = 'Stability.AI';
5823 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
5824 + }
5825 + if($image_size == '256x256')
5826 + {
5827 + $width = '512';
5828 + $height = '512';
5829 + }
5830 + elseif($image_size == '512x512')
5831 + {
5832 + $width = '512';
5833 + $height = '512';
5834 + }
5835 + elseif($image_size == '1024x1024')
5836 + {
5837 + $width = '1024';
5838 + $height = '1024';
5839 + }
5840 + else
5841 + {
5842 + $width = '512';
5843 + $height = '512';
5844 + }
5845 + $skip_this_copy = true;
5846 + if(!function_exists('is_plugin_active'))
5847 + {
5848 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
5849 + }
5850 + 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')
5851 + {
5852 + $skip_this_copy = false;
5853 + }
5854 + $ierror = '';
5855 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating ai image (Stable Diffusion)'));
5856 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
5857 + if($get_img !== false)
5858 + {
5859 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5860 + {
5861 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
5862 + }
5863 + }
5864 + else
5865 + {
5866 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
5867 + $get_img = '';
5868 + }
5869 + }
5870 + }
5871 + else
5872 + {
5873 + aiomatic_log_to_file('Empty AI featured image query entered.');
5874 + }
5875 + }
5876 + elseif($enable_ai_images == '7')
5877 + {
5878 + $query_words = $post_title;
5879 + if($image_query == '')
5880 + {
5881 + $image_query = $temp_post;
5882 + }
5883 + if($orig_ai_command_image == '')
5884 + {
5885 + $orig_ai_command_image = $image_query;
5886 + }
5887 + if($orig_ai_command_image != '')
5888 + {
5889 + $ai_command_image = $orig_ai_command_image;
5890 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
5891 + {
5892 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
5893 + $ai_command_image = array_filter($ai_command_image);
5894 + if(count($ai_command_image) > 0)
5895 + {
5896 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
5897 + }
5898 + else
5899 + {
5900 + $ai_command_image = '';
5901 + }
5902 + }
5903 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
5904 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
5905 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5906 + if(!empty($ai_command_image))
5907 + {
5908 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5909 + }
5910 + else
5911 + {
5912 + $ai_command_image = trim(strip_tags($post_title));
5913 + }
5914 + $ai_command_image = trim($ai_command_image);
5915 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
5916 + {
5917 + $txt_content = aiomatic_get_web_page($ai_command_image);
5918 + if ($txt_content !== FALSE)
5919 + {
5920 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
5921 + $txt_content = array_filter($txt_content);
5922 + if(count($txt_content) > 0)
5923 + {
5924 + $txt_content = $txt_content[array_rand($txt_content)];
5925 + if(trim($txt_content) != '')
5926 + {
5927 + $ai_command_image = $txt_content;
5928 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
5929 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
5930 + }
5931 + }
5932 + }
5933 + }
5934 + if(empty($ai_command_image))
5935 + {
5936 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
5937 + }
5938 + else
5939 + {
5940 + if(aiomatic_strlen($ai_command_image) > 2000)
5941 + {
5942 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
5943 + }
5944 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5945 + {
5946 + $api_service = 'Google';
5947 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
5948 + }
5949 + $skip_this_copy = true;
5950 + if(!function_exists('is_plugin_active'))
5951 + {
5952 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
5953 + }
5954 + 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')
5955 + {
5956 + $skip_this_copy = false;
5957 + }
5958 + $ierror = '';
5959 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
5960 + {
5961 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
5962 + }
5963 + else
5964 + {
5965 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
5966 + }
5967 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating ai image (Google)'));
5968 + $get_img = aiomatic_generate_image_google($ai_command_image, $model, 'featuredGoogleImage', false, $ierror, false);
5969 + if($get_img !== false)
5970 + {
5971 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
5972 + {
5973 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
5974 + }
5975 + }
5976 + else
5977 + {
5978 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
5979 + $get_img = '';
5980 + }
5981 + }
5982 + }
5983 + else
5984 + {
5985 + aiomatic_log_to_file('Empty AI featured image query entered.');
5986 + }
5987 + }
5988 + elseif($enable_ai_images == '4')
5989 + {
5990 + $query_words = $post_title;
5991 + if($image_query == '')
5992 + {
5993 + $image_query = $temp_post;
5994 + }
5995 + if($orig_ai_command_image == '')
5996 + {
5997 + $orig_ai_command_image = $image_query;
5998 + }
5999 + if($orig_ai_command_image != '')
6000 + {
6001 + $ai_command_image = $orig_ai_command_image;
6002 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
6003 + {
6004 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
6005 + $ai_command_image = array_filter($ai_command_image);
6006 + if(count($ai_command_image) > 0)
6007 + {
6008 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
6009 + }
6010 + else
6011 + {
6012 + $ai_command_image = '';
6013 + }
6014 + }
6015 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
6016 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
6017 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6018 + if(!empty($ai_command_image))
6019 + {
6020 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6021 + }
6022 + else
6023 + {
6024 + $ai_command_image = trim(strip_tags($post_title));
6025 + }
6026 + $ai_command_image = trim($ai_command_image);
6027 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
6028 + {
6029 + $txt_content = aiomatic_get_web_page($ai_command_image);
6030 + if ($txt_content !== FALSE)
6031 + {
6032 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
6033 + $txt_content = array_filter($txt_content);
6034 + if(count($txt_content) > 0)
6035 + {
6036 + $txt_content = $txt_content[array_rand($txt_content)];
6037 + if(trim($txt_content) != '')
6038 + {
6039 + $ai_command_image = $txt_content;
6040 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6041 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6042 + }
6043 + }
6044 + }
6045 + }
6046 + if(empty($ai_command_image))
6047 + {
6048 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
6049 + }
6050 + else
6051 + {
6052 + if(aiomatic_strlen($ai_command_image) > 2000)
6053 + {
6054 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
6055 + }
6056 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6057 + {
6058 + $api_service = 'Midjourney';
6059 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
6060 + }
6061 + if($image_size == '256x256')
6062 + {
6063 + $width = '512';
6064 + $height = '512';
6065 + }
6066 + elseif($image_size == '512x512')
6067 + {
6068 + $width = '512';
6069 + $height = '512';
6070 + }
6071 + elseif($image_size == '1024x1024')
6072 + {
6073 + $width = '1024';
6074 + $height = '1024';
6075 + }
6076 + elseif($image_size == '1792x1024')
6077 + {
6078 + $width = '1792';
6079 + $height = '1024';
6080 + }
6081 + elseif($image_size == '1024x1792')
6082 + {
6083 + $width = '1024';
6084 + $height = '1792';
6085 + }
6086 + else
6087 + {
6088 + $width = '512';
6089 + $height = '512';
6090 + }
6091 + $skip_this_copy = true;
6092 + if(!function_exists('is_plugin_active'))
6093 + {
6094 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6095 + }
6096 + 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')
6097 + {
6098 + $skip_this_copy = false;
6099 + }
6100 + $ierror = '';
6101 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating ai image (Midjourney)'));
6102 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', false, $ierror);
6103 + if($get_img !== false)
6104 + {
6105 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6106 + {
6107 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
6108 + }
6109 + }
6110 + else
6111 + {
6112 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
6113 + $get_img = '';
6114 + }
6115 + }
6116 + }
6117 + else
6118 + {
6119 + aiomatic_log_to_file('Empty AI featured image query entered.');
6120 + }
6121 + }
6122 + elseif($enable_ai_images == '5')
6123 + {
6124 + $query_words = $post_title;
6125 + if($image_query == '')
6126 + {
6127 + $image_query = $temp_post;
6128 + }
6129 + if($orig_ai_command_image == '')
6130 + {
6131 + $orig_ai_command_image = $image_query;
6132 + }
6133 + if($orig_ai_command_image != '')
6134 + {
6135 + $ai_command_image = $orig_ai_command_image;
6136 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
6137 + {
6138 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
6139 + $ai_command_image = array_filter($ai_command_image);
6140 + if(count($ai_command_image) > 0)
6141 + {
6142 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
6143 + }
6144 + else
6145 + {
6146 + $ai_command_image = '';
6147 + }
6148 + }
6149 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
6150 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
6151 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6152 + if(!empty($ai_command_image))
6153 + {
6154 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6155 + }
6156 + else
6157 + {
6158 + $ai_command_image = trim(strip_tags($post_title));
6159 + }
6160 + $ai_command_image = trim($ai_command_image);
6161 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
6162 + {
6163 + $txt_content = aiomatic_get_web_page($ai_command_image);
6164 + if ($txt_content !== FALSE)
6165 + {
6166 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
6167 + $txt_content = array_filter($txt_content);
6168 + if(count($txt_content) > 0)
6169 + {
6170 + $txt_content = $txt_content[array_rand($txt_content)];
6171 + if(trim($txt_content) != '')
6172 + {
6173 + $ai_command_image = $txt_content;
6174 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6175 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6176 + }
6177 + }
6178 + }
6179 + }
6180 + if(empty($ai_command_image))
6181 + {
6182 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
6183 + }
6184 + else
6185 + {
6186 + if(aiomatic_strlen($ai_command_image) > 2000)
6187 + {
6188 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
6189 + }
6190 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6191 + {
6192 + $api_service = 'Replicate';
6193 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
6194 + }
6195 + if($image_size == '256x256')
6196 + {
6197 + $width = '512';
6198 + $height = '512';
6199 + }
6200 + elseif($image_size == '512x512')
6201 + {
6202 + $width = '512';
6203 + $height = '512';
6204 + }
6205 + elseif($image_size == '1024x1024')
6206 + {
6207 + $width = '1024';
6208 + $height = '1024';
6209 + }
6210 + elseif($image_size == '1792x1024')
6211 + {
6212 + $width = '1792';
6213 + $height = '1024';
6214 + }
6215 + elseif($image_size == '1024x1792')
6216 + {
6217 + $width = '1024';
6218 + $height = '1792';
6219 + }
6220 + else
6221 + {
6222 + $width = '512';
6223 + $height = '512';
6224 + }
6225 + $skip_this_copy = true;
6226 + if(!function_exists('is_plugin_active'))
6227 + {
6228 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6229 + }
6230 + 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')
6231 + {
6232 + $skip_this_copy = false;
6233 + }
6234 + $ierror = '';
6235 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating ai image (Replicate)'));
6236 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredReplicateImage', false, $ierror);
6237 + if($get_img !== false)
6238 + {
6239 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6240 + {
6241 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
6242 + }
6243 + }
6244 + else
6245 + {
6246 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
6247 + $get_img = '';
6248 + }
6249 + }
6250 + }
6251 + else
6252 + {
6253 + aiomatic_log_to_file('Empty AI featured image query entered.');
6254 + }
6255 + }
6256 + elseif($enable_ai_images == '6')
6257 + {
6258 + $query_words = $post_title;
6259 + if($image_query == '')
6260 + {
6261 + $image_query = $temp_post;
6262 + }
6263 + if($orig_ai_command_image == '')
6264 + {
6265 + $orig_ai_command_image = $image_query;
6266 + }
6267 + if($orig_ai_command_image != '')
6268 + {
6269 + $ai_command_image = $orig_ai_command_image;
6270 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
6271 + {
6272 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
6273 + $ai_command_image = array_filter($ai_command_image);
6274 + if(count($ai_command_image) > 0)
6275 + {
6276 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
6277 + }
6278 + else
6279 + {
6280 + $ai_command_image = '';
6281 + }
6282 + }
6283 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
6284 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
6285 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6286 + if(!empty($ai_command_image))
6287 + {
6288 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6289 + }
6290 + else
6291 + {
6292 + $ai_command_image = trim(strip_tags($post_title));
6293 + }
6294 + $ai_command_image = trim($ai_command_image);
6295 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
6296 + {
6297 + $txt_content = aiomatic_get_web_page($ai_command_image);
6298 + if ($txt_content !== FALSE)
6299 + {
6300 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
6301 + $txt_content = array_filter($txt_content);
6302 + if(count($txt_content) > 0)
6303 + {
6304 + $txt_content = $txt_content[array_rand($txt_content)];
6305 + if(trim($txt_content) != '')
6306 + {
6307 + $ai_command_image = $txt_content;
6308 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6309 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6310 + }
6311 + }
6312 + }
6313 + }
6314 + if(empty($ai_command_image))
6315 + {
6316 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
6317 + }
6318 + else
6319 + {
6320 + if(aiomatic_strlen($ai_command_image) > 2000)
6321 + {
6322 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
6323 + }
6324 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6325 + {
6326 + $api_service = 'Ideogram';
6327 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
6328 + }
6329 + if($image_size == '256x256')
6330 + {
6331 + $width = '512';
6332 + $height = '512';
6333 + }
6334 + elseif($image_size == '512x512')
6335 + {
6336 + $width = '512';
6337 + $height = '512';
6338 + }
6339 + elseif($image_size == '1024x1024')
6340 + {
6341 + $width = '1024';
6342 + $height = '1024';
6343 + }
6344 + elseif($image_size == '1792x1024')
6345 + {
6346 + $width = '1792';
6347 + $height = '1024';
6348 + }
6349 + elseif($image_size == '1024x1792')
6350 + {
6351 + $width = '1024';
6352 + $height = '1792';
6353 + }
6354 + else
6355 + {
6356 + $width = '512';
6357 + $height = '512';
6358 + }
6359 + $skip_this_copy = true;
6360 + if(!function_exists('is_plugin_active'))
6361 + {
6362 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6363 + }
6364 + 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')
6365 + {
6366 + $skip_this_copy = false;
6367 + }
6368 + $ierror = '';
6369 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating ai image (Ideogram)'));
6370 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredIdeogramImage', false, $ierror);
6371 + if($get_img !== false)
6372 + {
6373 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6374 + {
6375 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
6376 + }
6377 + }
6378 + else
6379 + {
6380 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
6381 + $get_img = '';
6382 + }
6383 + }
6384 + }
6385 + else
6386 + {
6387 + aiomatic_log_to_file('Empty AI featured image query entered.');
6388 + }
6389 + }
6390 + elseif($enable_ai_images == '3')
6391 + {
6392 + if ($image_url != '')
6393 + {
6394 + $replacement = str_replace(array('[', ']'), '', $query_words);
6395 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
6396 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
6397 + if(isset($matches[2]))
6398 + {
6399 + $chance = trim($matches[2], '[]');
6400 + }
6401 + else
6402 + {
6403 + $chance = '';
6404 + }
6405 + $arv = array();
6406 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
6407 + return $my_img;
6408 + }, $image_url_temp);
6409 + $img_rulx = $spintax->Parse(trim($image_url_temp));
6410 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
6411 + if($selected_img === false)
6412 + {
6413 + $img_rulx = explode(',', $img_rulx);
6414 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
6415 + if($img_rulx != '')
6416 + {
6417 + $get_img = $img_rulx;
6418 + }
6419 + }
6420 + else
6421 + {
6422 + $get_img = $selected_img;
6423 + }
6424 + }
6425 + }
6426 + else
6427 + {
6428 + $image_query_set = false;
6429 + $query_words = '';
6430 + $ai_command_image = '';
6431 + if($orig_ai_command_image == '')
6432 + {
6433 + $orig_ai_command_image = $image_query;
6434 + }
6435 + if($orig_ai_command_image != '')
6436 + {
6437 + $ai_command_image = $orig_ai_command_image;
6438 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
6439 + {
6440 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
6441 + $ai_command_image = array_filter($ai_command_image);
6442 + if(count($ai_command_image) > 0)
6443 + {
6444 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
6445 + }
6446 + else
6447 + {
6448 + $ai_command_image = '';
6449 + }
6450 + }
6451 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
6452 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
6453 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6454 + if(!empty($ai_command_image))
6455 + {
6456 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6457 + }
6458 + else
6459 + {
6460 + $ai_command_image = trim(strip_tags($post_title));
6461 + }
6462 + $ai_command_image = trim($ai_command_image);
6463 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
6464 + {
6465 + $txt_content = aiomatic_get_web_page($ai_command_image);
6466 + if ($txt_content !== FALSE)
6467 + {
6468 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
6469 + $txt_content = array_filter($txt_content);
6470 + if(count($txt_content) > 0)
6471 + {
6472 + $txt_content = $txt_content[array_rand($txt_content)];
6473 + if(trim($txt_content) != '')
6474 + {
6475 + $ai_command_image = $txt_content;
6476 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
6477 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6478 + }
6479 + }
6480 + }
6481 + }
6482 + }
6483 + if($ai_command_image != '')
6484 + {
6485 + $query_words = $ai_command_image;
6486 + $image_query = $ai_command_image;
6487 + $image_query_set = true;
6488 + }
6489 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
6490 + {
6491 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
6492 + {
6493 + try
6494 + {
6495 + if(!class_exists('TextRazor'))
6496 + {
6497 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
6498 + }
6499 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
6500 + $textrazor = new TextRazor();
6501 + $textrazor->addExtractor('entities');
6502 + $response = $textrazor->analyze($image_query);
6503 + if (isset($response['response']['entities']))
6504 + {
6505 + foreach ($response['response']['entities'] as $entity)
6506 + {
6507 + $query_words = '';
6508 + if(isset($entity['entityEnglishId']))
6509 + {
6510 + $query_words = $entity['entityEnglishId'];
6511 + }
6512 + else
6513 + {
6514 + $query_words = $entity['entityId'];
6515 + }
6516 + if($query_words != '')
6517 + {
6518 + $attached_id = '';
6519 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
6520 + if(!empty($attached_id))
6521 + {
6522 + $additional_attachments[] = $attached_id;
6523 + }
6524 + if(!empty($z_img))
6525 + {
6526 + $get_img = $z_img;
6527 + $go_local_image = false;
6528 + if(!function_exists('is_plugin_active'))
6529 + {
6530 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6531 + }
6532 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
6533 + {
6534 + $go_local_image = true;
6535 + }
6536 + if($go_local_image == true)
6537 + {
6538 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
6539 + if(!empty($attached_id))
6540 + {
6541 + $additional_attachments[] = $attached_id;
6542 + }
6543 + if($localpath !== false)
6544 + {
6545 + $get_img = $localpath[0];
6546 + }
6547 + }
6548 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6549 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
6550 + }
6551 + break;
6552 + }
6553 + }
6554 + }
6555 + }
6556 + }
6557 + catch(Exception $e)
6558 + {
6559 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6560 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
6561 + }
6562 + }
6563 + }
6564 + }
6565 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
6566 + {
6567 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
6568 + {
6569 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
6570 + {
6571 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
6572 + }
6573 + else
6574 + {
6575 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
6576 + }
6577 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
6578 + {
6579 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
6580 + }
6581 + else
6582 + {
6583 + $keyword_assistant_id = '';
6584 + }
6585 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
6586 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
6587 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
6588 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
6589 + {
6590 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
6591 + $title_ai_command = array_filter($title_ai_command);
6592 + if(count($title_ai_command) > 0)
6593 + {
6594 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
6595 + }
6596 + else
6597 + {
6598 + $title_ai_command = '';
6599 + }
6600 + }
6601 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
6602 + if(!empty($title_ai_command))
6603 + {
6604 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6605 + }
6606 + $title_ai_command = trim($title_ai_command);
6607 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
6608 + {
6609 + $txt_content = aiomatic_get_web_page($title_ai_command);
6610 + if ($txt_content !== FALSE)
6611 + {
6612 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
6613 + $txt_content = array_filter($txt_content);
6614 + if(count($txt_content) > 0)
6615 + {
6616 + $txt_content = $txt_content[array_rand($txt_content)];
6617 + if(trim($txt_content) != '')
6618 + {
6619 + $title_ai_command = $txt_content;
6620 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
6621 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
6622 + }
6623 + }
6624 + }
6625 + }
6626 + if(empty($title_ai_command))
6627 + {
6628 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
6629 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
6630 + }
6631 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
6632 + {
6633 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
6634 + }
6635 + $title_ai_command = trim($title_ai_command);
6636 + if(empty($title_ai_command))
6637 + {
6638 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
6639 + }
6640 + else
6641 + {
6642 + $query_token_count = count(aiomatic_encode($title_ai_command));
6643 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
6644 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
6645 + {
6646 + $string_len = aiomatic_strlen($title_ai_command);
6647 + $string_len = $string_len / 2;
6648 + $string_len = intval(0 - $string_len);
6649 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
6650 + $title_ai_command = trim($title_ai_command);
6651 + $query_token_count = count(aiomatic_encode($title_ai_command));
6652 + $available_tokens = $max_tokens - $query_token_count;
6653 + }
6654 + $aierror = '';
6655 + $finish_reason = '';
6656 + if(!empty($global_prepend))
6657 + {
6658 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
6659 + }
6660 + if(!empty($global_append))
6661 + {
6662 + $title_ai_command = $title_ai_command . ' ' . $global_append;
6663 + }
6664 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6665 + {
6666 + $api_service = aiomatic_get_api_service($token, $kw_model);
6667 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
6668 + }
6669 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Improving royalty free image search keywords'));
6670 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
6671 + if($generated_text === false)
6672 + {
6673 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
6674 + $ai_title = '';
6675 + }
6676 + else
6677 + {
6678 + $ai_title = trim(trim($generated_text), '.');
6679 + $ai_titles = explode(',', $ai_title);
6680 + foreach($ai_titles as $query_words)
6681 + {
6682 + $attached_id = '';
6683 + $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);
6684 + if(!empty($attached_id))
6685 + {
6686 + $additional_attachments[] = $attached_id;
6687 + }
6688 + if(!empty($z_img))
6689 + {
6690 + $get_img = $z_img;
6691 + $go_local_image = false;
6692 + if(!function_exists('is_plugin_active'))
6693 + {
6694 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6695 + }
6696 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
6697 + {
6698 + $go_local_image = true;
6699 + }
6700 + if($go_local_image == true)
6701 + {
6702 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
6703 + if(!empty($attached_id))
6704 + {
6705 + $additional_attachments[] = $attached_id;
6706 + }
6707 + if($localpath !== false)
6708 + {
6709 + $get_img = $localpath[0];
6710 + }
6711 + }
6712 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6713 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
6714 + }
6715 + break;
6716 + }
6717 + }
6718 + }
6719 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
6720 + {
6721 + $api_service = aiomatic_get_api_service($token, $kw_model);
6722 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
6723 + }
6724 + }
6725 + }
6726 + }
6727 + if(empty($get_img))
6728 + {
6729 + if($image_query_set == true && !empty($image_query))
6730 + {
6731 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free image'));
6732 + $attached_id = '';
6733 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
6734 + if(!empty($attached_id))
6735 + {
6736 + $additional_attachments[] = $attached_id;
6737 + }
6738 + if($get_img == '' || $get_img === false)
6739 + {
6740 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
6741 + {
6742 + $keyword_class = new Aiomatic_keywords();
6743 + $image_query = $keyword_class->keywords($image_query, 1);
6744 + $attached_id = '';
6745 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
6746 + if(!empty($attached_id))
6747 + {
6748 + $additional_attachments[] = $attached_id;
6749 + }
6750 + }
6751 + }
6752 + }
6753 + if(empty($get_img))
6754 + {
6755 + $keyword_class = new Aiomatic_keywords();
6756 + $query_words = $keyword_class->keywords($post_title, 2);
6757 + $attached_id = '';
6758 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
6759 + if(!empty($attached_id))
6760 + {
6761 + $additional_attachments[] = $attached_id;
6762 + }
6763 + if($get_img == '' || $get_img === false)
6764 + {
6765 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
6766 + {
6767 + $query_words = $keyword_class->keywords($post_title, 1);
6768 + $attached_id = '';
6769 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
6770 + if(!empty($attached_id))
6771 + {
6772 + $additional_attachments[] = $attached_id;
6773 + }
6774 + if($get_img == '' || $get_img === false)
6775 + {
6776 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
6777 + {
6778 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6779 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
6780 + }
6781 + unset($post_title_lines[$current_index]);
6782 + continue;
6783 + }
6784 + }
6785 + }
6786 + else
6787 + {
6788 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
6789 + {
6790 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6791 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
6792 + }
6793 + unset($post_title_lines[$current_index]);
6794 + continue;
6795 + }
6796 + }
6797 + }
6798 + }
6799 + if(!empty($get_img))
6800 + {
6801 + $go_local_image = false;
6802 + if(!function_exists('is_plugin_active'))
6803 + {
6804 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6805 + }
6806 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
6807 + {
6808 + $go_local_image = true;
6809 + }
6810 + if($go_local_image == true)
6811 + {
6812 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
6813 + if(!empty($attached_id))
6814 + {
6815 + $additional_attachments[] = $attached_id;
6816 + }
6817 + if($localpath !== false)
6818 + {
6819 + $get_img = $localpath[0];
6820 + }
6821 + }
6822 + }
6823 + }
6824 + }
6825 + }
6826 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
6827 + {
6828 + $already_spinned = '1';
6829 + }
6830 + $my_post = array();
6831 + $my_post['aiomatic_post_image'] = $get_img;
6832 + if($enable_ai_images == '2')
6833 + {
6834 + $my_post['aiomatic_local_image'] = '1';
6835 + }
6836 + else
6837 + {
6838 + $my_post['aiomatic_local_image'] = '0';
6839 + }
6840 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
6841 + $my_post['default_category'] = $default_category;
6842 + $my_post['post_type'] = $post_type;
6843 + $my_post['comment_status'] = $accept_comments;
6844 +
6845 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
6846 + {
6847 + if($post_status == 'publish')
6848 + {
6849 + $draft_me = true;
6850 + $my_post['post_status'] = 'draft';
6851 + }
6852 + else
6853 + {
6854 + $my_post['post_status'] = $post_status;
6855 + }
6856 + }
6857 + else
6858 + {
6859 + $my_post['post_status'] = $post_status;
6860 + }
6861 + if($user_name_type == 'rand')
6862 + {
6863 + $randid = aiomatic_display_random_user();
6864 + if($randid === false)
6865 + {
6866 + $my_post['post_author'] = aiomatic_randomName();
6867 + }
6868 + else
6869 + {
6870 + $my_post['post_author'] = $randid->ID;
6871 + }
6872 + }
6873 + else
6874 + {
6875 + $my_post['post_author'] = $post_user_name;
6876 + }
6877 + if($strip_title == '1')
6878 + {
6879 + $new_post_content = str_replace($post_title, '', $new_post_content);
6880 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
6881 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
6882 + }
6883 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
6884 + {
6885 + require_once(dirname(__FILE__) . "/res/swear.php");
6886 + $new_post_content = aimogenpro_filterwords($new_post_content);
6887 + }
6888 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
6889 + $continue = false;
6890 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
6891 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
6892 + foreach ($banned_list as $banned_word) {
6893 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
6894 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6895 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
6896 + }
6897 + $continue = true;
6898 + break;
6899 + }
6900 + if (stripos($post_title, trim($banned_word)) !== FALSE) {
6901 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6902 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
6903 + }
6904 + $continue = true;
6905 + break;
6906 + }
6907 + }
6908 + if ($continue === true) {
6909 + unset($post_title_lines[$current_index]);
6910 + continue;
6911 + }
6912 + }
6913 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
6914 + {
6915 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
6916 + {
6917 + $continue = true;
6918 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
6919 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
6920 + foreach ($required_list as $required_word) {
6921 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($post_title, trim($required_word)) !== FALSE) {
6922 + $continue = false;
6923 + break;
6924 + }
6925 + }
6926 + if ($continue === true) {
6927 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6928 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
6929 + }
6930 + unset($post_title_lines[$current_index]);
6931 + continue;
6932 + }
6933 + }
6934 + else
6935 + {
6936 + $continue = false;
6937 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
6938 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
6939 + foreach ($required_list as $required_word) {
6940 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($post_title, trim($required_word)) === FALSE) {
6941 + $continue = true;
6942 + break;
6943 + }
6944 + }
6945 + if ($continue === true) {
6946 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
6947 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
6948 + }
6949 + unset($post_title_lines[$current_index]);
6950 + continue;
6951 + }
6952 + }
6953 + }
6954 + $new_post_title = $post_title;
6955 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
6956 + {
6957 + $skip_spin = '1';
6958 + }
6959 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
6960 + if($arr[0] != $new_post_title)
6961 + {
6962 + $new_post_title = $arr[0];
6963 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
6964 + {
6965 + $posts = get_posts(
6966 + array(
6967 + 'post_type' => $post_type,
6968 + 'title' => html_entity_decode($new_post_title),
6969 + 'post_status' => 'all',
6970 + 'numberposts' => 1,
6971 + 'update_post_term_cache' => false,
6972 + 'update_post_meta_cache' => false,
6973 + 'orderby' => 'post_date ID',
6974 + 'order' => 'ASC',
6975 + )
6976 + );
6977 + if ( ! empty( $posts ) ) {
6978 + $zap = $posts[0];
6979 + } else {
6980 + $zap = null;
6981 + }
6982 + if($zap !== null)
6983 + {
6984 + if($overwrite_existing)
6985 + {
6986 + $update_post_id = $zap->ID;
6987 + }
6988 + else
6989 + {
6990 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $new_post_title);
6991 + unset($post_title_lines[$current_index]);
6992 + continue;
6993 + }
6994 + }
6995 + }
6996 + }
6997 + $new_post_content = $arr[1];
6998 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
6999 + {
7000 + $already_spinned = '1';
7001 + }
7002 + if ($auto_categories == 'content') {
7003 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
7004 + $extra_categories = implode(',', $extra_categories);
7005 + }
7006 + elseif ($auto_categories == 'title') {
7007 + $extra_categories = aiomatic_extractKeyWords($new_post_title);
7008 + $extra_categories = implode(',', $extra_categories);
7009 + }
7010 + elseif ($auto_categories == 'both') {
7011 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
7012 + $extra_categories = implode(',', $extra_categories);
7013 + $extra_categories2 = aiomatic_extractKeyWords($new_post_title);
7014 + $extra_categories2 = implode(',', $extra_categories2);
7015 + if($extra_categories2 != '')
7016 + {
7017 + $extra_categories .= ',' . $extra_categories2;
7018 + }
7019 + }
7020 + elseif ($auto_categories == 'ai')
7021 + {
7022 + $category_ai_command = $orig_ai_command_category;
7023 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
7024 + {
7025 + $category_ai_command = preg_split('/\r\n|\r|\n/', $category_ai_command);
7026 + $category_ai_command = array_filter($category_ai_command);
7027 + if(count($category_ai_command) > 0)
7028 + {
7029 + $category_ai_command = $category_ai_command[array_rand($category_ai_command)];
7030 + }
7031 + else
7032 + {
7033 + $category_ai_command = '';
7034 + }
7035 + }
7036 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
7037 + if(!empty($category_ai_command))
7038 + {
7039 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7040 + $category_ai_command = aiomatic_replacetopics($category_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7041 + }
7042 + else
7043 + {
7044 + $category_ai_command = trim(strip_tags('Write a comma separated list of categories, for the post title: %%post_title%%'));
7045 + }
7046 + $category_ai_command = trim($category_ai_command);
7047 + if (filter_var($category_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($category_ai_command, '.txt'))
7048 + {
7049 + $txt_content = aiomatic_get_web_page($category_ai_command);
7050 + if ($txt_content !== FALSE)
7051 + {
7052 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
7053 + $txt_content = array_filter($txt_content);
7054 + if(count($txt_content) > 0)
7055 + {
7056 + $txt_content = $txt_content[array_rand($txt_content)];
7057 + if(trim($txt_content) != '')
7058 + {
7059 + $category_ai_command = $txt_content;
7060 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
7061 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7062 + $category_ai_command = aiomatic_replacetopics($category_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7063 + }
7064 + }
7065 + }
7066 + }
7067 + if(empty($category_ai_command))
7068 + {
7069 + aiomatic_log_to_file('Empty API post category seed expression provided!');
7070 + }
7071 + else
7072 + {
7073 + if(aiomatic_strlen($category_ai_command) > $max_seed_tokens * 4)
7074 + {
7075 + $category_ai_command = aiomatic_substr($category_ai_command, 0, (0 - ($max_seed_tokens * 4)));
7076 + }
7077 + $category_ai_command = trim($category_ai_command);
7078 + if(empty($category_ai_command))
7079 + {
7080 + aiomatic_log_to_file('Empty API category seed expression provided! ' . print_r($category_ai_command, true));
7081 + break;
7082 + }
7083 + $query_token_count = count(aiomatic_encode($category_ai_command));
7084 + $available_tokens = aiomatic_compute_available_tokens($category_model, $max_tokens, $category_ai_command, $query_token_count);
7085 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
7086 + {
7087 + $string_len = aiomatic_strlen($category_ai_command);
7088 + $string_len = $string_len / 2;
7089 + $string_len = intval(0 - $string_len);
7090 + $category_ai_command = aiomatic_substr($category_ai_command, 0, $string_len);
7091 + $category_ai_command = trim($category_ai_command);
7092 + if(empty($category_ai_command))
7093 + {
7094 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($category_ai_command, true));
7095 + break;
7096 + }
7097 + $query_token_count = count(aiomatic_encode($category_ai_command));
7098 + $available_tokens = $max_tokens - $query_token_count;
7099 + }
7100 + $aierror = '';
7101 + $finish_reason = '';
7102 + if(!empty($global_prepend))
7103 + {
7104 + $category_ai_command = $global_prepend . ' ' . $category_ai_command;
7105 + }
7106 + if(!empty($global_append))
7107 + {
7108 + $category_ai_command = $category_ai_command . ' ' . $global_append;
7109 + }
7110 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7111 + {
7112 + $api_service = aiomatic_get_api_service($token, $category_model);
7113 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $category_model . ')(' . $available_tokens . ') for category generator: ' . $category_ai_command);
7114 + }
7115 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating category'));
7116 + $generated_text = aiomatic_generate_text($token, $category_model, $category_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
7117 + if($generated_text === false)
7118 + {
7119 + aiomatic_log_to_file('Category generator error: ' . $aierror);
7120 + break;
7121 + }
7122 + else
7123 + {
7124 + $extra_categories = $generated_text;
7125 + }
7126 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7127 + {
7128 + $api_service = aiomatic_get_api_service($token, $category_model);
7129 + aiomatic_log_to_file('Successfully got API category result from ' . $api_service . ': ' . $generated_text);
7130 + }
7131 + }
7132 + }
7133 + else
7134 + {
7135 + $extra_categories = '';
7136 + }
7137 + $my_post['extra_categories'] = $extra_categories;
7138 +
7139 + $item_tags = aiomatic_extractKeyWords($new_post_content, 3);
7140 + $item_tags = implode(',', $item_tags);
7141 + $title_tags = aiomatic_extractKeyWords($new_post_title, 3);
7142 + $title_tags = implode(',', $title_tags);
7143 + $item_create_tag_sp = $spintax->Parse($item_create_tag);
7144 + if ($can_create_tag == 'content') {
7145 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $item_tags;
7146 + $my_post['extra_tags'] = $item_tags;
7147 + } else if ($can_create_tag == 'title') {
7148 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $title_tags;
7149 + $my_post['extra_tags'] = $title_tags;
7150 + } else if ($can_create_tag == 'both') {
7151 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
7152 + $my_post['extra_tags'] = ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
7153 + } else if ($can_create_tag == 'ai') {
7154 + $ai_tags = '';
7155 + $tag_ai_command = $orig_ai_command_tag;
7156 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
7157 + {
7158 + $tag_ai_command = preg_split('/\r\n|\r|\n/', $tag_ai_command);
7159 + $tag_ai_command = array_filter($tag_ai_command);
7160 + if(count($tag_ai_command) > 0)
7161 + {
7162 + $tag_ai_command = $tag_ai_command[array_rand($tag_ai_command)];
7163 + }
7164 + else
7165 + {
7166 + $tag_ai_command = '';
7167 + }
7168 + }
7169 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
7170 + if(!empty($tag_ai_command))
7171 + {
7172 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7173 + $tag_ai_command = aiomatic_replacetopics($tag_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7174 + }
7175 + else
7176 + {
7177 + $tag_ai_command = trim(strip_tags('Write a comma separated list of tags, for the post title: %%post_title%%'));
7178 + }
7179 + $tag_ai_command = trim($tag_ai_command);
7180 + if (filter_var($tag_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($tag_ai_command, '.txt'))
7181 + {
7182 + $txt_content = aiomatic_get_web_page($tag_ai_command);
7183 + if ($txt_content !== FALSE)
7184 + {
7185 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
7186 + $txt_content = array_filter($txt_content);
7187 + if(count($txt_content) > 0)
7188 + {
7189 + $txt_content = $txt_content[array_rand($txt_content)];
7190 + if(trim($txt_content) != '')
7191 + {
7192 + $tag_ai_command = $txt_content;
7193 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
7194 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7195 + $tag_ai_command = aiomatic_replacetopics($tag_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7196 + }
7197 + }
7198 + }
7199 + }
7200 + if(empty($tag_ai_command))
7201 + {
7202 + aiomatic_log_to_file('Empty API post tag seed expression provided!');
7203 + }
7204 + else
7205 + {
7206 + if(aiomatic_strlen($tag_ai_command) > $max_seed_tokens * 4)
7207 + {
7208 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, (0 - ($max_seed_tokens * 4)));
7209 + }
7210 + $tag_ai_command = trim($tag_ai_command);
7211 + if(empty($tag_ai_command))
7212 + {
7213 + aiomatic_log_to_file('Empty API tag seed expression provided! ' . print_r($tag_ai_command, true));
7214 + break;
7215 + }
7216 + $query_token_count = count(aiomatic_encode($tag_ai_command));
7217 + $available_tokens = aiomatic_compute_available_tokens($tag_model, $max_tokens, $tag_ai_command, $query_token_count);
7218 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
7219 + {
7220 + $string_len = aiomatic_strlen($tag_ai_command);
7221 + $string_len = $string_len / 2;
7222 + $string_len = intval(0 - $string_len);
7223 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, $string_len);
7224 + $tag_ai_command = trim($tag_ai_command);
7225 + if(empty($tag_ai_command))
7226 + {
7227 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($tag_ai_command, true));
7228 + break;
7229 + }
7230 + $query_token_count = count(aiomatic_encode($tag_ai_command));
7231 + $available_tokens = $max_tokens - $query_token_count;
7232 + }
7233 + $aierror = '';
7234 + $finish_reason = '';
7235 + if(!empty($global_prepend))
7236 + {
7237 + $tag_ai_command = $global_prepend . ' ' . $tag_ai_command;
7238 + }
7239 + if(!empty($global_append))
7240 + {
7241 + $tag_ai_command = $tag_ai_command . ' ' . $global_append;
7242 + }
7243 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7244 + {
7245 + $api_service = aiomatic_get_api_service($token, $tag_model);
7246 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $tag_model . ')(' . $available_tokens . ') for tag generator: ' . $tag_ai_command);
7247 + }
7248 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
7249 + $generated_text = aiomatic_generate_text($token, $tag_model, $tag_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
7250 + if($generated_text === false)
7251 + {
7252 + aiomatic_log_to_file('Tag generator error: ' . $aierror);
7253 + break;
7254 + }
7255 + else
7256 + {
7257 + $ai_tags = $generated_text;
7258 + }
7259 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7260 + {
7261 + $api_service = aiomatic_get_api_service($token, $tag_model);
7262 + aiomatic_log_to_file('Successfully got API tag result from ' . $api_service . ': ' . $generated_text);
7263 + }
7264 + }
7265 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $ai_tags;
7266 + $my_post['extra_tags'] = $ai_tags;
7267 + } else {
7268 + $post_the_tags = $item_create_tag_sp;
7269 + $my_post['extra_tags'] = '';
7270 + }
7271 + $my_post['tags_input'] = $post_the_tags;
7272 + $new_post_content = html_entity_decode($new_post_content);
7273 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
7274 + if ($strip_by_regex !== '')
7275 + {
7276 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
7277 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
7278 + $xcnt = 0;
7279 + foreach($xstrip_by_regex as $sbr)
7280 + {
7281 + if(isset($xreplace_regex[$xcnt]))
7282 + {
7283 + $repreg = $xreplace_regex[$xcnt];
7284 + }
7285 + else
7286 + {
7287 + $repreg = '';
7288 + }
7289 + $xcnt++;
7290 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
7291 + if($temp_cont !== NULL)
7292 + {
7293 + $new_post_content = $temp_cont;
7294 + }
7295 + }
7296 + }
7297 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7298 + $post_prepender = aiomatic_replacetopics($post_prepender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7299 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7300 + $post_appender = aiomatic_replacetopics($post_appender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7301 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
7302 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
7303 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
7304 + {
7305 + if($new_post_content === strip_tags($new_post_content))
7306 + {
7307 + $new_post_content = nl2br($new_post_content);
7308 + }
7309 + }
7310 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
7311 + {
7312 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
7313 + {
7314 + if(!isset($xchars))
7315 + {
7316 + $xchars = array();
7317 + }
7318 + $rand_percentage = wp_rand(10, 30);
7319 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
7320 + }
7321 + }
7322 + $zlang = 'en_US';
7323 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
7324 + $zlang = $aiomatic_Main_Settings['kw_lang'];
7325 + }
7326 + $rel_search = array('post_title', 'post_content');
7327 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
7328 + $rel_search = $aiomatic_Main_Settings['rel_search'];
7329 + }
7330 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
7331 + {
7332 + try
7333 + {
7334 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
7335 + }
7336 + catch(Exception $ex)
7337 + {
7338 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
7339 + aiomatic_log_to_file('Failed to add new inbound links to content: ' . $ex->getMessage());
7340 + }
7341 + }
7342 + }
7343 + if($ret_content == 1)
7344 + {
7345 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
7346 + aiomatic_log_to_file('Returning AI generated content, finished.');
7347 + }
7348 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
7349 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
7350 + }
7351 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
7352 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
7353 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
7354 + {
7355 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
7356 + }
7357 + else
7358 + {
7359 + $my_post['post_content'] = $trunchi_content;
7360 + }
7361 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
7362 + {
7363 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
7364 + {
7365 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
7366 + }
7367 + }
7368 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
7369 + $my_post['aiomatic_source_title'] = $post_title;
7370 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
7371 + $my_post['aiomatic_post_format'] = $post_format;
7372 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
7373 + {
7374 + if(is_array($default_category))
7375 + {
7376 + $cextra = '';
7377 + foreach($default_category as $dc)
7378 + {
7379 + $cextra .= ',' . get_cat_name($dc);
7380 + }
7381 + $extra_categories_temp = trim( $cextra . ',' . $extra_categories, ',');
7382 + }
7383 + else
7384 + {
7385 + $extra_categories_temp = trim(get_cat_name($default_category) . ',' .$extra_categories, ',');
7386 + }
7387 + }
7388 + else
7389 + {
7390 + $extra_categories_temp = $extra_categories;
7391 + }
7392 + $block_arr = array();
7393 + $custom_arr = array();
7394 + if($custom_fields != '')
7395 + {
7396 + if(stristr($custom_fields, '=>') != false)
7397 + {
7398 + $rule_arr = explode(',', trim($custom_fields));
7399 + foreach($rule_arr as $rule)
7400 + {
7401 + $my_args = explode('=>', trim($rule));
7402 + if(isset($my_args[1]))
7403 + {
7404 + if ( isset( $shortcode_tags['aicontent'] ) ) {
7405 + $aicontent_handler = $shortcode_tags['aicontent'];
7406 + unset( $shortcode_tags['aicontent'] );
7407 + } else {
7408 + $aicontent_handler = null;
7409 + }
7410 + $my_args[1] = do_shortcode($my_args[1]);
7411 + $my_args[0] = do_shortcode($my_args[0]);
7412 + if ( $aicontent_handler ) {
7413 + $shortcode_tags['aicontent'] = $aicontent_handler;
7414 + }
7415 + $custom_field_content = trim($my_args[1]);
7416 + $custom_field_content = aiomatic_replaceAIPostShortcodes($custom_field_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7417 + $custom_field_content = aiomatic_replacetopics($custom_field_content, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7418 + $custom_field_content = aiomatic_replaceSynergyShortcodes($custom_field_content);
7419 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
7420 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
7421 + if(stristr($my_args[1], 'serialize_') !== false)
7422 + {
7423 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
7424 + }
7425 + else
7426 + {
7427 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
7428 + {
7429 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
7430 + if(isset($cfm[2][0]))
7431 + {
7432 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
7433 + {
7434 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
7435 + }
7436 + else
7437 + {
7438 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
7439 + }
7440 + }
7441 + else
7442 + {
7443 + $custom_arr[trim($my_args[0])] = $custom_field_content;
7444 + }
7445 + }
7446 + else
7447 + {
7448 + $custom_arr[trim($my_args[0])] = $custom_field_content;
7449 + }
7450 + }
7451 + }
7452 + }
7453 + }
7454 + }
7455 + $block_arr = array();
7456 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $extra_categories_temp, 'aiomatic_post_tags' => $post_the_tags));
7457 + $custom_tax_arr = array();
7458 + if($custom_tax != '')
7459 + {
7460 + if(stristr($custom_tax, '=>') != false)
7461 + {
7462 + $rule_arr = explode(';', trim($custom_tax));
7463 + foreach($rule_arr as $rule)
7464 + {
7465 + $my_args = explode('=>', trim($rule));
7466 + if(isset($my_args[1]))
7467 + {
7468 + $custom_tax_content = trim($my_args[1]);
7469 + $custom_tax_content = aiomatic_replaceAIPostShortcodes($custom_tax_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7470 + $custom_tax_content = aiomatic_replacetopics($custom_tax_content, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
7471 + $custom_tax_content = aiomatic_replaceSynergyShortcodes($custom_tax_content);
7472 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
7473 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
7474 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
7475 + {
7476 + if(isset($custom_arr['_product_attributes']))
7477 + {
7478 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
7479 + 'name' => trim($my_args[0]),
7480 + 'value' => $custom_tax_content,
7481 + 'is_visible' => '1',
7482 + 'is_taxonomy' => '1'
7483 + )));
7484 + }
7485 + else
7486 + {
7487 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
7488 + 'name' => trim($my_args[0]),
7489 + 'value' => $custom_tax_content,
7490 + 'is_visible' => '1',
7491 + 'is_taxonomy' => '1'
7492 + ));
7493 + }
7494 + }
7495 + if(isset($custom_tax_arr[trim($my_args[0])]))
7496 + {
7497 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
7498 + }
7499 + else
7500 + {
7501 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
7502 + }
7503 + }
7504 + }
7505 + }
7506 + }
7507 + if(count($custom_tax_arr) > 0)
7508 + {
7509 + $my_post['taxo_input'] = $custom_tax_arr;
7510 + }
7511 + $my_post['meta_input'] = $custom_arr;
7512 + if($parent_id != '')
7513 + {
7514 + $my_post['post_parent'] = intval($parent_id);
7515 + }
7516 + if ($enable_pingback == '1') {
7517 + $my_post['ping_status'] = 'open';
7518 + } else {
7519 + $my_post['ping_status'] = 'closed';
7520 + }
7521 + if($min_time != '' && $max_time != '')
7522 + {
7523 + $t1 = strtotime($min_time);
7524 + $t2 = strtotime($max_time);
7525 + if($t1 != false && $t2 != false)
7526 + {
7527 + $int = wp_rand($t1, $t2);
7528 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
7529 + }
7530 + }
7531 + elseif($min_time != '')
7532 + {
7533 + $t1 = strtotime($min_time);
7534 + if($t1 != false)
7535 + {
7536 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
7537 + }
7538 + }
7539 + elseif($max_time != '')
7540 + {
7541 + $t1 = strtotime($max_time);
7542 + if($t1 != false)
7543 + {
7544 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
7545 + }
7546 + }
7547 + if($new_post_excerpt != '')
7548 + {
7549 + $my_post['post_excerpt'] = $new_post_excerpt;
7550 + }
7551 + $count++;
7552 + }
7553 + elseif($posting_mode == 'title')
7554 + {
7555 + $headings_arr_copy = $headings_arr;
7556 + $added_img_list = array();
7557 + $raw_img_list = array();
7558 + $full_result_list = array();
7559 + $added_images = 0;
7560 + $heading_results = array();
7561 + if(count($post_title_lines) == 0)
7562 + {
7563 + break;
7564 + }
7565 + if ($count > intval($max)) {
7566 + break;
7567 + }
7568 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
7569 + {
7570 + $current_index = array_rand($post_title_lines);
7571 + }
7572 + else
7573 + {
7574 + $current_index = array_key_first($post_title_lines);
7575 + }
7576 + $post_title = trim($post_title_lines[$current_index]);
7577 + if($title_once == '1')
7578 + {
7579 + if(array_key_exists($post_title, $posted_items))
7580 + {
7581 + aiomatic_log_to_file('Skipping Bulk Post Creator title, already processed: ' . $post_title);
7582 + unset($post_title_lines[$current_index]);
7583 + continue;
7584 + }
7585 + }
7586 + $ptlprocessed = explode('!###!', $post_title);
7587 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
7588 + {
7589 + $post_title = $ptlprocessed[0];
7590 + $post_title_keywords = $ptlprocessed[1];
7591 + }
7592 + if(isset($rss_feeds[$post_title]))
7593 + {
7594 + $post_link = $rss_feeds[$post_title]['url'];
7595 + $user_name = $rss_feeds[$post_title]['author'];
7596 + $post_cats = $rss_feeds[$post_title]['cats'];
7597 + $post_excerpt = $rss_feeds[$post_title]['excerpt'];
7598 + $final_content = $rss_feeds[$post_title]['content'];
7599 + }
7600 + $tprepp = $spintax->Parse($post_title);
7601 + if($tprepp != false && $tprepp != '')
7602 + {
7603 + $post_title = $tprepp;
7604 + }
7605 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7606 + {
7607 + aiomatic_log_to_file('Starting processing post title: ' . $post_title);
7608 + }
7609 + $old_title = $post_title;
7610 + $already_spinned = 0;
7611 + if (filter_var($post_title, FILTER_VALIDATE_URL) === false && stristr($post_title, '%%ai_generated_title%%') === false)
7612 + {
7613 + unset($post_title_lines[$current_index]);
7614 + }
7615 + $allmodels = aiomatic_get_all_models();
7616 + if(!empty($global_prepend))
7617 + {
7618 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
7619 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
7620 + }
7621 + if(!empty($global_append))
7622 + {
7623 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
7624 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
7625 + }
7626 + if($custom_shortcodes != '')
7627 + {
7628 + $custom_shortcodes_arr = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
7629 + foreach($custom_shortcodes_arr as $my_short)
7630 + {
7631 + $name_part = explode('=>', $my_short);
7632 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
7633 + {
7634 + $shortname = trim($name_part[0]);
7635 + if(strstr($post_title, '%%' . $shortname . '%%'))
7636 + {
7637 + $shortval = '';
7638 + $name_part[1] = str_replace('%%post_title%%', $post_title, $name_part[1]);
7639 + $ai_part = explode('@@', $name_part[1]);
7640 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
7641 + {
7642 + if(!in_array(trim($ai_part[0]), $allmodels))
7643 + {
7644 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
7645 + }
7646 + else
7647 + {
7648 + $aimodel = trim($ai_part[0]);
7649 + }
7650 + $zai_command = trim($ai_part[1]);
7651 + $zai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $zai_command);
7652 + $zai_command = aiomatic_replaceSynergyShortcodes($zai_command);
7653 + $zai_command = aiomatic_replaceAIPostShortcodes($zai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
7654 + foreach($custom_shortcodes_arr as $my_check_short)
7655 + {
7656 + $check_name_part = explode('=>', $my_check_short);
7657 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
7658 + {
7659 + $check_shortname = trim($check_name_part[0]);
7660 + if($shortname !== $check_shortname && strstr($zai_command, '%%' . $check_shortname . '%%') !== false)
7661 + {
7662 + $check_shortval = '';
7663 + $check_ai_part = explode('@@', $check_name_part[1]);
7664 + if(!in_array(trim($check_ai_part[0]), $allmodels))
7665 + {
7666 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
7667 + }
7668 + else
7669 + {
7670 + $check_aimodel = trim($check_ai_part[0]);
7671 + }
7672 + $check_ai_command = trim($check_ai_part[1]);
7673 + $aierror = '';
7674 +
7675 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
7676 + $check_ai_command = aiomatic_replaceAIPostShortcodes($check_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
7677 +
7678 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
7679 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
7680 + if($check_generated_text === false)
7681 + {
7682 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
7683 + }
7684 + else
7685 + {
7686 + $check_shortval = trim(trim($check_generated_text), '.');
7687 + }
7688 + $zai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $zai_command);
7689 + }
7690 + }
7691 + }
7692 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating title based shortcodes'));
7693 + $aierror = '';
7694 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $zai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
7695 + if($generated_text === false)
7696 + {
7697 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
7698 + }
7699 + else
7700 + {
7701 + $shortval = trim(trim($generated_text), '.');
7702 + }
7703 + }
7704 + $post_title = str_replace('%%' . $shortname . '%%', $shortval, $post_title);
7705 + }
7706 + }
7707 + }
7708 + }
7709 + if(stristr($post_title, '%%ai_generated_title%%') !== false || $title_source == 'ai')
7710 + {
7711 + if($orig_ai_command_title == '')
7712 + {
7713 + $orig_ai_command_title = $post_title;
7714 + }
7715 + if($orig_ai_command_title != '')
7716 + {
7717 + $title_ai_command = $orig_ai_command_title;
7718 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
7719 + {
7720 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
7721 + $title_ai_command = array_filter($title_ai_command);
7722 + if(count($title_ai_command) > 0)
7723 + {
7724 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
7725 + }
7726 + else
7727 + {
7728 + $title_ai_command = '';
7729 + }
7730 + }
7731 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
7732 + if(!empty($title_ai_command))
7733 + {
7734 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7735 + }
7736 + else
7737 + {
7738 + $title_ai_command = trim(strip_tags($post_title));
7739 + }
7740 + $title_ai_command = trim($title_ai_command);
7741 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
7742 + {
7743 + $txt_content = aiomatic_get_web_page($title_ai_command);
7744 + if ($txt_content !== FALSE)
7745 + {
7746 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
7747 + $txt_content = array_filter($txt_content);
7748 + if(count($txt_content) > 0)
7749 + {
7750 + $txt_content = $txt_content[array_rand($txt_content)];
7751 + if(trim($txt_content) != '')
7752 + {
7753 + $title_ai_command = $txt_content;
7754 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
7755 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7756 + }
7757 + }
7758 + }
7759 + }
7760 + if(empty($title_ai_command))
7761 + {
7762 + aiomatic_log_to_file('Empty API post title seed expression provided!');
7763 + }
7764 + else
7765 + {
7766 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
7767 + {
7768 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
7769 + }
7770 + $title_ai_command = trim($title_ai_command);
7771 + if(empty($title_ai_command))
7772 + {
7773 + aiomatic_log_to_file('Empty API title seed expression provided(2)! ' . print_r($title_ai_command, true));
7774 + break;
7775 + }
7776 + $query_token_count = count(aiomatic_encode($title_ai_command));
7777 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $title_ai_command, $query_token_count);
7778 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
7779 + {
7780 + $string_len = aiomatic_strlen($title_ai_command);
7781 + $string_len = $string_len / 2;
7782 + $string_len = intval(0 - $string_len);
7783 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
7784 + $title_ai_command = trim($title_ai_command);
7785 + if(empty($title_ai_command))
7786 + {
7787 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($title_ai_command, true));
7788 + break;
7789 + }
7790 + $query_token_count = count(aiomatic_encode($title_ai_command));
7791 + $available_tokens = $max_tokens - $query_token_count;
7792 + }
7793 + $aierror = '';
7794 + $finish_reason = '';
7795 + if(!empty($global_prepend))
7796 + {
7797 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
7798 + }
7799 + if(!empty($global_append))
7800 + {
7801 + $title_ai_command = $title_ai_command . ' ' . $global_append;
7802 + }
7803 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7804 + {
7805 + $api_service = aiomatic_get_api_service($token, $title_model);
7806 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $title_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
7807 + }
7808 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating title'));
7809 + $generated_text = aiomatic_generate_text($token, $title_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'titleID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
7810 + if($generated_text === false)
7811 + {
7812 + aiomatic_log_to_file('Title generator error: ' . $aierror);
7813 + break;
7814 + }
7815 + else
7816 + {
7817 + $ai_title = ucfirst(trim(trim($generated_text), '.'));
7818 + if($title_source == 'ai')
7819 + {
7820 + $post_title = $ai_title;
7821 + }
7822 + else
7823 + {
7824 + $post_title = str_ireplace('%%ai_generated_title%%', $ai_title, $post_title);
7825 + }
7826 + }
7827 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7828 + {
7829 + $api_service = aiomatic_get_api_service($token, $title_model);
7830 + aiomatic_log_to_file('Successfully got API title result from ' . $api_service . ': ' . $post_title);
7831 + }
7832 + }
7833 + }
7834 + else
7835 + {
7836 + aiomatic_log_to_file('Empty AI title query entered.');
7837 + }
7838 + }
7839 + else
7840 + {
7841 + $post_title = aiomatic_replaceSynergyShortcodes($post_title);
7842 + }
7843 + $post_title = apply_filters('aiomatic_replace_aicontent_shortcode', $post_title);
7844 + if(empty($post_title))
7845 + {
7846 + unset($post_title_lines[$current_index]);
7847 + continue;
7848 + }
7849 + if (strpos($post_title, '%%') === false)
7850 + {
7851 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
7852 + {
7853 + $xposts = get_posts(
7854 + array(
7855 + 'post_type' => $post_type,
7856 + 'title' => html_entity_decode($post_title),
7857 + 'post_status' => 'all',
7858 + 'numberposts' => 1,
7859 + 'update_post_term_cache' => false,
7860 + 'update_post_meta_cache' => false,
7861 + 'orderby' => 'post_date ID',
7862 + 'order' => 'ASC',
7863 + )
7864 + );
7865 + if ( ! empty( $xposts ) ) {
7866 + $zap = $xposts[0];
7867 + } else {
7868 + $zap = null;
7869 + }
7870 + if($zap !== null)
7871 + {
7872 + if($overwrite_existing)
7873 + {
7874 + $update_post_id = $zap->ID;
7875 + }
7876 + else
7877 + {
7878 + aiomatic_log_to_file('Post with specified title is already existing, skipping it: ' . $post_title);
7879 + unset($post_title_lines[$current_index]);
7880 + continue;
7881 + }
7882 + }
7883 + }
7884 + $new_post_title = $post_title;
7885 + }
7886 + else
7887 + {
7888 + $new_post_title = $post_title;
7889 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
7890 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
7891 + {
7892 + $xposts = get_posts(
7893 + array(
7894 + 'post_type' => $post_type,
7895 + 'title' => html_entity_decode($new_post_title),
7896 + 'post_status' => 'all',
7897 + 'numberposts' => 1,
7898 + 'update_post_term_cache' => false,
7899 + 'update_post_meta_cache' => false,
7900 + 'orderby' => 'post_date ID',
7901 + 'order' => 'ASC',
7902 + )
7903 + );
7904 + if ( ! empty( $xposts ) ) {
7905 + $zap = $xposts[0];
7906 + } else {
7907 + $zap = null;
7908 + }
7909 + if($zap !== null)
7910 + {
7911 + if($overwrite_existing)
7912 + {
7913 + $update_post_id = $zap->ID;
7914 + }
7915 + else
7916 + {
7917 + aiomatic_log_to_file('Post with specified title already published, skipping it: ' . $new_post_title);
7918 + unset($post_title_lines[$current_index]);
7919 + continue;
7920 + }
7921 + }
7922 + }
7923 + }
7924 + $get_img = '';
7925 + if($royalty_free == '1')
7926 + {
7927 + if($enable_ai_images == '1')
7928 + {
7929 + $query_words = $post_title;
7930 + if($image_query == '')
7931 + {
7932 + $image_query = $temp_post;
7933 + }
7934 + if($orig_ai_command_image == '')
7935 + {
7936 + $orig_ai_command_image = $image_query;
7937 + }
7938 + if($orig_ai_command_image != '')
7939 + {
7940 + $ai_command_image = $orig_ai_command_image;
7941 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
7942 + {
7943 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
7944 + $ai_command_image = array_filter($ai_command_image);
7945 + if(count($ai_command_image) > 0)
7946 + {
7947 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
7948 + }
7949 + else
7950 + {
7951 + $ai_command_image = '';
7952 + }
7953 + }
7954 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
7955 + if(!empty($ai_command_image))
7956 + {
7957 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7958 + }
7959 + else
7960 + {
7961 + $ai_command_image = trim(strip_tags($post_title));
7962 + }
7963 + $ai_command_image = trim($ai_command_image);
7964 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
7965 + {
7966 + $txt_content = aiomatic_get_web_page($ai_command_image);
7967 + if ($txt_content !== FALSE)
7968 + {
7969 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
7970 + $txt_content = array_filter($txt_content);
7971 + if(count($txt_content) > 0)
7972 + {
7973 + $txt_content = $txt_content[array_rand($txt_content)];
7974 + if(trim($txt_content) != '')
7975 + {
7976 + $ai_command_image = $txt_content;
7977 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
7978 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
7979 + }
7980 + }
7981 + }
7982 + }
7983 + if(empty($ai_command_image))
7984 + {
7985 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
7986 + }
7987 + else
7988 + {
7989 + if(aiomatic_strlen($ai_command_image) > 1000)
7990 + {
7991 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
7992 + }
7993 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
7994 + {
7995 + $api_service = aiomatic_get_api_service($token, $image_model);
7996 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
7997 + }
7998 + $skip_this_copy = true;
7999 + if(!function_exists('is_plugin_active'))
8000 + {
8001 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8002 + }
8003 + 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')
8004 + {
8005 + $skip_this_copy = false;
8006 + }
8007 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
8008 + {
8009 + $skip_this_copy = false;
8010 + }
8011 + $aierror = '';
8012 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
8013 + if($get_img !== false)
8014 + {
8015 + foreach($get_img as $tmpimg)
8016 + {
8017 + $get_img = $tmpimg;
8018 + break;
8019 + }
8020 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8021 + {
8022 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
8023 + }
8024 + }
8025 + else
8026 + {
8027 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
8028 + $get_img = '';
8029 + }
8030 + }
8031 + }
8032 + else
8033 + {
8034 + aiomatic_log_to_file('Empty AI featured image query entered.');
8035 + }
8036 + }
8037 + elseif($enable_ai_images == '2')
8038 + {
8039 + $query_words = $post_title;
8040 + if($image_query == '')
8041 + {
8042 + $image_query = $temp_post;
8043 + }
8044 + if($orig_ai_command_image == '')
8045 + {
8046 + $orig_ai_command_image = $image_query;
8047 + }
8048 + if($orig_ai_command_image != '')
8049 + {
8050 + $ai_command_image = $orig_ai_command_image;
8051 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8052 + {
8053 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8054 + $ai_command_image = array_filter($ai_command_image);
8055 + if(count($ai_command_image) > 0)
8056 + {
8057 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8058 + }
8059 + else
8060 + {
8061 + $ai_command_image = '';
8062 + }
8063 + }
8064 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8065 + if(!empty($ai_command_image))
8066 + {
8067 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8068 + }
8069 + else
8070 + {
8071 + $ai_command_image = trim(strip_tags($post_title));
8072 + }
8073 + $ai_command_image = trim($ai_command_image);
8074 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8075 + {
8076 + $txt_content = aiomatic_get_web_page($ai_command_image);
8077 + if ($txt_content !== FALSE)
8078 + {
8079 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8080 + $txt_content = array_filter($txt_content);
8081 + if(count($txt_content) > 0)
8082 + {
8083 + $txt_content = $txt_content[array_rand($txt_content)];
8084 + if(trim($txt_content) != '')
8085 + {
8086 + $ai_command_image = $txt_content;
8087 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8088 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8089 + }
8090 + }
8091 + }
8092 + }
8093 + if(empty($ai_command_image))
8094 + {
8095 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
8096 + }
8097 + else
8098 + {
8099 + if(aiomatic_strlen($ai_command_image) > 2000)
8100 + {
8101 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
8102 + }
8103 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8104 + {
8105 + $api_service = 'Stability.AI';
8106 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
8107 + }
8108 + if($image_size == '256x256')
8109 + {
8110 + $width = '512';
8111 + $height = '512';
8112 + }
8113 + elseif($image_size == '512x512')
8114 + {
8115 + $width = '512';
8116 + $height = '512';
8117 + }
8118 + elseif($image_size == '1024x1024')
8119 + {
8120 + $width = '1024';
8121 + $height = '1024';
8122 + }
8123 + else
8124 + {
8125 + $width = '512';
8126 + $height = '512';
8127 + }
8128 + $skip_this_copy = true;
8129 + if(!function_exists('is_plugin_active'))
8130 + {
8131 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8132 + }
8133 + 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')
8134 + {
8135 + $skip_this_copy = false;
8136 + }
8137 + $ierror = '';
8138 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
8139 + if($get_img !== false)
8140 + {
8141 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8142 + {
8143 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
8144 + }
8145 + }
8146 + else
8147 + {
8148 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
8149 + $get_img = '';
8150 + }
8151 + }
8152 + }
8153 + else
8154 + {
8155 + aiomatic_log_to_file('Empty AI featured image query entered.');
8156 + }
8157 + }
8158 + elseif($enable_ai_images == '7')
8159 + {
8160 + $query_words = $post_title;
8161 + if($image_query == '')
8162 + {
8163 + $image_query = $temp_post;
8164 + }
8165 + if($orig_ai_command_image == '')
8166 + {
8167 + $orig_ai_command_image = $image_query;
8168 + }
8169 + if($orig_ai_command_image != '')
8170 + {
8171 + $ai_command_image = $orig_ai_command_image;
8172 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8173 + {
8174 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8175 + $ai_command_image = array_filter($ai_command_image);
8176 + if(count($ai_command_image) > 0)
8177 + {
8178 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8179 + }
8180 + else
8181 + {
8182 + $ai_command_image = '';
8183 + }
8184 + }
8185 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8186 + if(!empty($ai_command_image))
8187 + {
8188 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8189 + }
8190 + else
8191 + {
8192 + $ai_command_image = trim(strip_tags($post_title));
8193 + }
8194 + $ai_command_image = trim($ai_command_image);
8195 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8196 + {
8197 + $txt_content = aiomatic_get_web_page($ai_command_image);
8198 + if ($txt_content !== FALSE)
8199 + {
8200 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8201 + $txt_content = array_filter($txt_content);
8202 + if(count($txt_content) > 0)
8203 + {
8204 + $txt_content = $txt_content[array_rand($txt_content)];
8205 + if(trim($txt_content) != '')
8206 + {
8207 + $ai_command_image = $txt_content;
8208 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8209 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8210 + }
8211 + }
8212 + }
8213 + }
8214 + if(empty($ai_command_image))
8215 + {
8216 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
8217 + }
8218 + else
8219 + {
8220 + if(aiomatic_strlen($ai_command_image) > 2000)
8221 + {
8222 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
8223 + }
8224 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8225 + {
8226 + $api_service = 'Google';
8227 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
8228 + }
8229 + $skip_this_copy = true;
8230 + if(!function_exists('is_plugin_active'))
8231 + {
8232 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8233 + }
8234 + 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')
8235 + {
8236 + $skip_this_copy = false;
8237 + }
8238 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
8239 + {
8240 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
8241 + }
8242 + else
8243 + {
8244 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
8245 + }
8246 + $ierror = '';
8247 + $get_img = aiomatic_generate_image_google($ai_command_image, $model, 'featuredGoogleImage', false, $ierror, false);
8248 + if($get_img !== false)
8249 + {
8250 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8251 + {
8252 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
8253 + }
8254 + }
8255 + else
8256 + {
8257 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
8258 + $get_img = '';
8259 + }
8260 + }
8261 + }
8262 + else
8263 + {
8264 + aiomatic_log_to_file('Empty AI featured image query entered.');
8265 + }
8266 + }
8267 + elseif($enable_ai_images == '4')
8268 + {
8269 + $query_words = $post_title;
8270 + if($image_query == '')
8271 + {
8272 + $image_query = $temp_post;
8273 + }
8274 + if($orig_ai_command_image == '')
8275 + {
8276 + $orig_ai_command_image = $image_query;
8277 + }
8278 + if($orig_ai_command_image != '')
8279 + {
8280 + $ai_command_image = $orig_ai_command_image;
8281 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8282 + {
8283 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8284 + $ai_command_image = array_filter($ai_command_image);
8285 + if(count($ai_command_image) > 0)
8286 + {
8287 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8288 + }
8289 + else
8290 + {
8291 + $ai_command_image = '';
8292 + }
8293 + }
8294 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8295 + if(!empty($ai_command_image))
8296 + {
8297 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8298 + }
8299 + else
8300 + {
8301 + $ai_command_image = trim(strip_tags($post_title));
8302 + }
8303 + $ai_command_image = trim($ai_command_image);
8304 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8305 + {
8306 + $txt_content = aiomatic_get_web_page($ai_command_image);
8307 + if ($txt_content !== FALSE)
8308 + {
8309 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8310 + $txt_content = array_filter($txt_content);
8311 + if(count($txt_content) > 0)
8312 + {
8313 + $txt_content = $txt_content[array_rand($txt_content)];
8314 + if(trim($txt_content) != '')
8315 + {
8316 + $ai_command_image = $txt_content;
8317 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8318 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8319 + }
8320 + }
8321 + }
8322 + }
8323 + if(empty($ai_command_image))
8324 + {
8325 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
8326 + }
8327 + else
8328 + {
8329 + if(aiomatic_strlen($ai_command_image) > 2000)
8330 + {
8331 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
8332 + }
8333 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8334 + {
8335 + $api_service = 'Midjourney';
8336 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
8337 + }
8338 + if($image_size == '256x256')
8339 + {
8340 + $width = '512';
8341 + $height = '512';
8342 + }
8343 + elseif($image_size == '512x512')
8344 + {
8345 + $width = '512';
8346 + $height = '512';
8347 + }
8348 + elseif($image_size == '1024x1024')
8349 + {
8350 + $width = '1024';
8351 + $height = '1024';
8352 + }
8353 + elseif($image_size == '1792x1024')
8354 + {
8355 + $width = '1792';
8356 + $height = '1024';
8357 + }
8358 + elseif($image_size == '1024x1792')
8359 + {
8360 + $width = '1024';
8361 + $height = '1792';
8362 + }
8363 + else
8364 + {
8365 + $width = '512';
8366 + $height = '512';
8367 + }
8368 + $skip_this_copy = true;
8369 + if(!function_exists('is_plugin_active'))
8370 + {
8371 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8372 + }
8373 + 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')
8374 + {
8375 + $skip_this_copy = false;
8376 + }
8377 + $ierror = '';
8378 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
8379 + if($get_img !== false)
8380 + {
8381 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8382 + {
8383 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
8384 + }
8385 + }
8386 + else
8387 + {
8388 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
8389 + $get_img = '';
8390 + }
8391 + }
8392 + }
8393 + else
8394 + {
8395 + aiomatic_log_to_file('Empty AI featured image query entered.');
8396 + }
8397 + }
8398 + elseif($enable_ai_images == '5')
8399 + {
8400 + $query_words = $post_title;
8401 + if($image_query == '')
8402 + {
8403 + $image_query = $temp_post;
8404 + }
8405 + if($orig_ai_command_image == '')
8406 + {
8407 + $orig_ai_command_image = $image_query;
8408 + }
8409 + if($orig_ai_command_image != '')
8410 + {
8411 + $ai_command_image = $orig_ai_command_image;
8412 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8413 + {
8414 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8415 + $ai_command_image = array_filter($ai_command_image);
8416 + if(count($ai_command_image) > 0)
8417 + {
8418 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8419 + }
8420 + else
8421 + {
8422 + $ai_command_image = '';
8423 + }
8424 + }
8425 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8426 + if(!empty($ai_command_image))
8427 + {
8428 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8429 + }
8430 + else
8431 + {
8432 + $ai_command_image = trim(strip_tags($post_title));
8433 + }
8434 + $ai_command_image = trim($ai_command_image);
8435 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8436 + {
8437 + $txt_content = aiomatic_get_web_page($ai_command_image);
8438 + if ($txt_content !== FALSE)
8439 + {
8440 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8441 + $txt_content = array_filter($txt_content);
8442 + if(count($txt_content) > 0)
8443 + {
8444 + $txt_content = $txt_content[array_rand($txt_content)];
8445 + if(trim($txt_content) != '')
8446 + {
8447 + $ai_command_image = $txt_content;
8448 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8449 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8450 + }
8451 + }
8452 + }
8453 + }
8454 + if(empty($ai_command_image))
8455 + {
8456 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
8457 + }
8458 + else
8459 + {
8460 + if(aiomatic_strlen($ai_command_image) > 2000)
8461 + {
8462 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
8463 + }
8464 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8465 + {
8466 + $api_service = 'Replicate';
8467 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
8468 + }
8469 + if($image_size == '256x256')
8470 + {
8471 + $width = '512';
8472 + $height = '512';
8473 + }
8474 + elseif($image_size == '512x512')
8475 + {
8476 + $width = '512';
8477 + $height = '512';
8478 + }
8479 + elseif($image_size == '1024x1024')
8480 + {
8481 + $width = '1024';
8482 + $height = '1024';
8483 + }
8484 + elseif($image_size == '1792x1024')
8485 + {
8486 + $width = '1792';
8487 + $height = '1024';
8488 + }
8489 + elseif($image_size == '1024x1792')
8490 + {
8491 + $width = '1024';
8492 + $height = '1792';
8493 + }
8494 + else
8495 + {
8496 + $width = '512';
8497 + $height = '512';
8498 + }
8499 + $skip_this_copy = true;
8500 + if(!function_exists('is_plugin_active'))
8501 + {
8502 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8503 + }
8504 + 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')
8505 + {
8506 + $skip_this_copy = false;
8507 + }
8508 + $ierror = '';
8509 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
8510 + if($get_img !== false)
8511 + {
8512 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8513 + {
8514 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
8515 + }
8516 + }
8517 + else
8518 + {
8519 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
8520 + $get_img = '';
8521 + }
8522 + }
8523 + }
8524 + else
8525 + {
8526 + aiomatic_log_to_file('Empty AI featured image query entered.');
8527 + }
8528 + }
8529 + elseif($enable_ai_images == '6')
8530 + {
8531 + $query_words = $post_title;
8532 + if($image_query == '')
8533 + {
8534 + $image_query = $temp_post;
8535 + }
8536 + if($orig_ai_command_image == '')
8537 + {
8538 + $orig_ai_command_image = $image_query;
8539 + }
8540 + if($orig_ai_command_image != '')
8541 + {
8542 + $ai_command_image = $orig_ai_command_image;
8543 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8544 + {
8545 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8546 + $ai_command_image = array_filter($ai_command_image);
8547 + if(count($ai_command_image) > 0)
8548 + {
8549 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8550 + }
8551 + else
8552 + {
8553 + $ai_command_image = '';
8554 + }
8555 + }
8556 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8557 + if(!empty($ai_command_image))
8558 + {
8559 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8560 + }
8561 + else
8562 + {
8563 + $ai_command_image = trim(strip_tags($post_title));
8564 + }
8565 + $ai_command_image = trim($ai_command_image);
8566 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8567 + {
8568 + $txt_content = aiomatic_get_web_page($ai_command_image);
8569 + if ($txt_content !== FALSE)
8570 + {
8571 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8572 + $txt_content = array_filter($txt_content);
8573 + if(count($txt_content) > 0)
8574 + {
8575 + $txt_content = $txt_content[array_rand($txt_content)];
8576 + if(trim($txt_content) != '')
8577 + {
8578 + $ai_command_image = $txt_content;
8579 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8580 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8581 + }
8582 + }
8583 + }
8584 + }
8585 + if(empty($ai_command_image))
8586 + {
8587 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
8588 + }
8589 + else
8590 + {
8591 + if(aiomatic_strlen($ai_command_image) > 2000)
8592 + {
8593 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
8594 + }
8595 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8596 + {
8597 + $api_service = 'Ideogram';
8598 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
8599 + }
8600 + if($image_size == '256x256')
8601 + {
8602 + $width = '512';
8603 + $height = '512';
8604 + }
8605 + elseif($image_size == '512x512')
8606 + {
8607 + $width = '512';
8608 + $height = '512';
8609 + }
8610 + elseif($image_size == '1024x1024')
8611 + {
8612 + $width = '1024';
8613 + $height = '1024';
8614 + }
8615 + elseif($image_size == '1792x1024')
8616 + {
8617 + $width = '1792';
8618 + $height = '1024';
8619 + }
8620 + elseif($image_size == '1024x1792')
8621 + {
8622 + $width = '1024';
8623 + $height = '1792';
8624 + }
8625 + else
8626 + {
8627 + $width = '512';
8628 + $height = '512';
8629 + }
8630 + $skip_this_copy = true;
8631 + if(!function_exists('is_plugin_active'))
8632 + {
8633 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8634 + }
8635 + 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')
8636 + {
8637 + $skip_this_copy = false;
8638 + }
8639 + $ierror = '';
8640 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
8641 + if($get_img !== false)
8642 + {
8643 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8644 + {
8645 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
8646 + }
8647 + }
8648 + else
8649 + {
8650 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
8651 + $get_img = '';
8652 + }
8653 + }
8654 + }
8655 + else
8656 + {
8657 + aiomatic_log_to_file('Empty AI featured image query entered.');
8658 + }
8659 + }
8660 + elseif($enable_ai_images == '3')
8661 + {
8662 + $query_words = $post_title;
8663 + if ($image_url != '')
8664 + {
8665 + $replacement = str_replace(array('[', ']'), '', $query_words);
8666 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
8667 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
8668 + if(isset($matches[2]))
8669 + {
8670 + $chance = trim($matches[2], '[]');
8671 + }
8672 + else
8673 + {
8674 + $chance = '';
8675 + }
8676 + $arv = array();
8677 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
8678 + return $my_img;
8679 + }, $image_url_temp);
8680 + $img_rulx = $spintax->Parse(trim($image_url_temp));
8681 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
8682 + if($selected_img === false)
8683 + {
8684 + $img_rulx = explode(',', $img_rulx);
8685 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
8686 + if($img_rulx != '')
8687 + {
8688 + $get_img = $img_rulx;
8689 + }
8690 + }
8691 + else
8692 + {
8693 + $get_img = $selected_img;
8694 + }
8695 + }
8696 + }
8697 + else
8698 + {
8699 + $image_query_set = false;
8700 + $query_words = '';
8701 + $ai_command_image = '';
8702 + if($orig_ai_command_image == '')
8703 + {
8704 + $orig_ai_command_image = $image_query;
8705 + }
8706 + if($orig_ai_command_image != '')
8707 + {
8708 + $ai_command_image = $orig_ai_command_image;
8709 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8710 + {
8711 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
8712 + $ai_command_image = array_filter($ai_command_image);
8713 + if(count($ai_command_image) > 0)
8714 + {
8715 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
8716 + }
8717 + else
8718 + {
8719 + $ai_command_image = '';
8720 + }
8721 + }
8722 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8723 + if(!empty($ai_command_image))
8724 + {
8725 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8726 + }
8727 + else
8728 + {
8729 + $ai_command_image = trim(strip_tags($post_title));
8730 + }
8731 + $ai_command_image = trim($ai_command_image);
8732 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
8733 + {
8734 + $txt_content = aiomatic_get_web_page($ai_command_image);
8735 + if ($txt_content !== FALSE)
8736 + {
8737 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8738 + $txt_content = array_filter($txt_content);
8739 + if(count($txt_content) > 0)
8740 + {
8741 + $txt_content = $txt_content[array_rand($txt_content)];
8742 + if(trim($txt_content) != '')
8743 + {
8744 + $ai_command_image = $txt_content;
8745 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
8746 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8747 + }
8748 + }
8749 + }
8750 + }
8751 + }
8752 + if($ai_command_image != '')
8753 + {
8754 + $query_words = $ai_command_image;
8755 + $image_query = $ai_command_image;
8756 + $image_query_set = true;
8757 + }
8758 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
8759 + {
8760 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
8761 + {
8762 + try
8763 + {
8764 + if(!class_exists('TextRazor'))
8765 + {
8766 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
8767 + }
8768 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
8769 + $textrazor = new TextRazor();
8770 + $textrazor->addExtractor('entities');
8771 + $response = $textrazor->analyze($image_query);
8772 + if (isset($response['response']['entities']))
8773 + {
8774 + foreach ($response['response']['entities'] as $entity)
8775 + {
8776 + $query_words = '';
8777 + if(isset($entity['entityEnglishId']))
8778 + {
8779 + $query_words = $entity['entityEnglishId'];
8780 + }
8781 + else
8782 + {
8783 + $query_words = $entity['entityId'];
8784 + }
8785 + if($query_words != '')
8786 + {
8787 + $attached_id = '';
8788 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
8789 + if(!empty($attached_id))
8790 + {
8791 + $additional_attachments[] = $attached_id;
8792 + }
8793 + if(!empty($z_img))
8794 + {
8795 + $get_img = $z_img;
8796 + $go_local_image = false;
8797 + if(!function_exists('is_plugin_active'))
8798 + {
8799 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8800 + }
8801 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
8802 + {
8803 + $go_local_image = true;
8804 + }
8805 + if($go_local_image == true)
8806 + {
8807 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
8808 + if(!empty($attached_id))
8809 + {
8810 + $additional_attachments[] = $attached_id;
8811 + }
8812 + if($localpath !== false)
8813 + {
8814 + $get_img = $localpath[0];
8815 + }
8816 + }
8817 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
8818 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
8819 + }
8820 + break;
8821 + }
8822 + }
8823 + }
8824 + }
8825 + }
8826 + catch(Exception $e)
8827 + {
8828 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
8829 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
8830 + }
8831 + }
8832 + }
8833 + }
8834 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
8835 + {
8836 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
8837 + {
8838 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
8839 + {
8840 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
8841 + }
8842 + else
8843 + {
8844 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
8845 + }
8846 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
8847 + {
8848 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
8849 + }
8850 + else
8851 + {
8852 + $keyword_assistant_id = '';
8853 + }
8854 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
8855 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
8856 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
8857 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
8858 + {
8859 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
8860 + $title_ai_command = array_filter($title_ai_command);
8861 + if(count($title_ai_command) > 0)
8862 + {
8863 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
8864 + }
8865 + else
8866 + {
8867 + $title_ai_command = '';
8868 + }
8869 + }
8870 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
8871 + if(!empty($title_ai_command))
8872 + {
8873 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8874 + }
8875 + $title_ai_command = trim($title_ai_command);
8876 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
8877 + {
8878 + $txt_content = aiomatic_get_web_page($title_ai_command);
8879 + if ($txt_content !== FALSE)
8880 + {
8881 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
8882 + $txt_content = array_filter($txt_content);
8883 + if(count($txt_content) > 0)
8884 + {
8885 + $txt_content = $txt_content[array_rand($txt_content)];
8886 + if(trim($txt_content) != '')
8887 + {
8888 + $title_ai_command = $txt_content;
8889 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
8890 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
8891 + }
8892 + }
8893 + }
8894 + }
8895 + if(empty($title_ai_command))
8896 + {
8897 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
8898 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
8899 + }
8900 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
8901 + {
8902 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
8903 + }
8904 + $title_ai_command = trim($title_ai_command);
8905 + if(empty($title_ai_command))
8906 + {
8907 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
8908 + }
8909 + else
8910 + {
8911 + $query_token_count = count(aiomatic_encode($title_ai_command));
8912 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
8913 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
8914 + {
8915 + $string_len = aiomatic_strlen($title_ai_command);
8916 + $string_len = $string_len / 2;
8917 + $string_len = intval(0 - $string_len);
8918 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
8919 + $title_ai_command = trim($title_ai_command);
8920 + $query_token_count = count(aiomatic_encode($title_ai_command));
8921 + $available_tokens = $max_tokens - $query_token_count;
8922 + }
8923 + $aierror = '';
8924 + $finish_reason = '';
8925 + if(!empty($global_prepend))
8926 + {
8927 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
8928 + }
8929 + if(!empty($global_append))
8930 + {
8931 + $title_ai_command = $title_ai_command . ' ' . $global_append;
8932 + }
8933 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8934 + {
8935 + $api_service = aiomatic_get_api_service($token, $kw_model);
8936 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
8937 + }
8938 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating keywords for image search'));
8939 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
8940 + if($generated_text === false)
8941 + {
8942 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
8943 + $ai_title = '';
8944 + }
8945 + else
8946 + {
8947 + $ai_title = trim(trim($generated_text), '.');
8948 + $ai_titles = explode(',', $ai_title);
8949 + foreach($ai_titles as $query_words)
8950 + {
8951 + $attached_id = '';
8952 + $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);
8953 + if(!empty($attached_id))
8954 + {
8955 + $additional_attachments[] = $attached_id;
8956 + }
8957 + if(!empty($z_img))
8958 + {
8959 + $get_img = $z_img;
8960 + $go_local_image = false;
8961 + if(!function_exists('is_plugin_active'))
8962 + {
8963 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
8964 + }
8965 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
8966 + {
8967 + $go_local_image = true;
8968 + }
8969 + if($go_local_image == true)
8970 + {
8971 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
8972 + if(!empty($attached_id))
8973 + {
8974 + $additional_attachments[] = $attached_id;
8975 + }
8976 + if($localpath !== false)
8977 + {
8978 + $get_img = $localpath[0];
8979 + }
8980 + }
8981 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
8982 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
8983 + }
8984 + break;
8985 + }
8986 + }
8987 + }
8988 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
8989 + {
8990 + $api_service = aiomatic_get_api_service($token, $kw_model);
8991 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
8992 + }
8993 + }
8994 + }
8995 + }
8996 + if(empty($get_img))
8997 + {
8998 + if($image_query_set == true && !empty($image_query))
8999 + {
9000 + $attached_id = '';
9001 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
9002 + if(!empty($attached_id))
9003 + {
9004 + $additional_attachments[] = $attached_id;
9005 + }
9006 + if($get_img == '' || $get_img === false)
9007 + {
9008 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
9009 + {
9010 + $keyword_class = new Aiomatic_keywords();
9011 + $image_query = $keyword_class->keywords($image_query, 1);
9012 + $attached_id = '';
9013 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
9014 + if(!empty($attached_id))
9015 + {
9016 + $additional_attachments[] = $attached_id;
9017 + }
9018 + }
9019 + }
9020 + }
9021 + if(empty($get_img))
9022 + {
9023 + $keyword_class = new Aiomatic_keywords();
9024 + $query_words = $keyword_class->keywords($post_title, 2);
9025 + $attached_id = '';
9026 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
9027 + if(!empty($attached_id))
9028 + {
9029 + $additional_attachments[] = $attached_id;
9030 + }
9031 + if($get_img == '' || $get_img === false)
9032 + {
9033 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
9034 + {
9035 + $query_words = $keyword_class->keywords($post_title, 1);
9036 + $attached_id = '';
9037 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
9038 + if(!empty($attached_id))
9039 + {
9040 + $additional_attachments[] = $attached_id;
9041 + }
9042 + if($get_img == '' || $get_img === false)
9043 + {
9044 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
9045 + {
9046 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
9047 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
9048 + }
9049 + unset($post_title_lines[$current_index]);
9050 + continue;
9051 + }
9052 + }
9053 + }
9054 + else
9055 + {
9056 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
9057 + {
9058 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
9059 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
9060 + }
9061 + unset($post_title_lines[$current_index]);
9062 + continue;
9063 + }
9064 + }
9065 + }
9066 + }
9067 + if(!empty($get_img))
9068 + {
9069 +
9070 + $go_local_image = false;
9071 + if(!function_exists('is_plugin_active'))
9072 + {
9073 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
9074 + }
9075 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
9076 + {
9077 + $go_local_image = true;
9078 + }
9079 + if($go_local_image == true)
9080 + {
9081 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
9082 + if(!empty($attached_id))
9083 + {
9084 + $additional_attachments[] = $attached_id;
9085 + }
9086 + if($localpath !== false)
9087 + {
9088 + $get_img = $localpath[0];
9089 + }
9090 + }
9091 + }
9092 + }
9093 + }
9094 + }
9095 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
9096 + {
9097 + $already_spinned = '1';
9098 + }
9099 + $my_post = array();
9100 + $my_post['aiomatic_post_image'] = $get_img;
9101 + if($enable_ai_images == '2')
9102 + {
9103 + $my_post['aiomatic_local_image'] = '1';
9104 + }
9105 + else
9106 + {
9107 + $my_post['aiomatic_local_image'] = '0';
9108 + }
9109 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
9110 + $my_post['default_category'] = $default_category;
9111 + $my_post['post_type'] = $post_type;
9112 + $my_post['comment_status'] = $accept_comments;
9113 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
9114 + {
9115 + if($post_status == 'publish')
9116 + {
9117 + $draft_me = true;
9118 + $my_post['post_status'] = 'draft';
9119 + }
9120 + else
9121 + {
9122 + $my_post['post_status'] = $post_status;
9123 + }
9124 + }
9125 + else
9126 + {
9127 + $my_post['post_status'] = $post_status;
9128 + }
9129 + if($user_name_type == 'rand')
9130 + {
9131 + $randid = aiomatic_display_random_user();
9132 + if($randid === false)
9133 + {
9134 + $my_post['post_author'] = aiomatic_randomName();
9135 + }
9136 + else
9137 + {
9138 + $my_post['post_author'] = $randid->ID;
9139 + }
9140 + }
9141 + else
9142 + {
9143 + $my_post['post_author'] = $post_user_name;
9144 + }
9145 + $ai_command = $orig_ai_command;
9146 + $ai_command = aiomatic_replaceSynergyShortcodes($ai_command);
9147 + if(!empty($ai_command))
9148 + {
9149 + $aicontent = aiomatic_replaceAIPostShortcodes($ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9150 + }
9151 + else
9152 + {
9153 + $aicontent = trim(strip_tags($post_title));
9154 + }
9155 + if (filter_var($aicontent, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($aicontent, '.txt'))
9156 + {
9157 + $txt_content = aiomatic_get_web_page($aicontent);
9158 + if ($txt_content !== FALSE)
9159 + {
9160 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9161 + $txt_content = array_filter($txt_content);
9162 + if(count($txt_content) > 0)
9163 + {
9164 + $txt_content = $txt_content[array_rand($txt_content)];
9165 + if(trim($txt_content) != '')
9166 + {
9167 + $aicontent = $txt_content;
9168 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
9169 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9170 + }
9171 + }
9172 + }
9173 + }
9174 + $last_char = aiomatic_substr($aicontent, -1, null);
9175 + if(!ctype_punct($last_char))
9176 + {
9177 + $aicontent .= '.';
9178 + }
9179 + if(aiomatic_strlen($aicontent) > $max_seed_tokens * 4)
9180 + {
9181 + $aicontent = aiomatic_substr($aicontent, 0, (0-($max_seed_tokens * 4)));
9182 + }
9183 + $aicontent = trim($aicontent);
9184 + if(empty($aicontent))
9185 + {
9186 + aiomatic_log_to_file('Empty API seed expression provided! ' . print_r($ai_command, true));
9187 + break;
9188 + }
9189 + $query_token_count = count(aiomatic_encode($aicontent));
9190 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $aicontent, $query_token_count);
9191 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
9192 + {
9193 + $string_len = aiomatic_strlen($aicontent);
9194 + $string_len = $string_len / 2;
9195 + $string_len = intval(0 - $string_len);
9196 + $aicontent = aiomatic_substr($aicontent, 0, $string_len);
9197 + $aicontent = trim($aicontent);
9198 + if(empty($aicontent))
9199 + {
9200 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_command, true));
9201 + break;
9202 + }
9203 + $query_token_count = count(aiomatic_encode($aicontent));
9204 + $available_tokens = $max_tokens - $query_token_count;
9205 + }
9206 + $aierror = '';
9207 + $finish_reason = '';
9208 + if(!empty($global_prepend))
9209 + {
9210 + $aicontent = $global_prepend . ' ' . $aicontent;
9211 + }
9212 + if(!empty($global_append))
9213 + {
9214 + $aicontent = $aicontent . ' ' . $global_append;
9215 + }
9216 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9217 + {
9218 + $api_service = aiomatic_get_api_service($token, $model);
9219 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for text: ' . $aicontent);
9220 + }
9221 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating post content'));
9222 + $generated_text = aiomatic_generate_text($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentID' . $param, 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
9223 + if($generated_text === false)
9224 + {
9225 + aiomatic_log_to_file($aierror);
9226 + break;
9227 + }
9228 + else
9229 + {
9230 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
9231 + {
9232 + $new_post_content = ucfirst(trim(nl2br(trim($generated_text))));
9233 + }
9234 + else
9235 + {
9236 + $new_post_content = ucfirst(trim($generated_text));
9237 + }
9238 + if($finish_reason == 'length')
9239 + {
9240 + $query_token_count = count(aiomatic_encode($new_post_content));
9241 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $new_post_content, $query_token_count);
9242 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating post content (2)'));
9243 + $generated_text = aiomatic_generate_text($token, $model, $new_post_content, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentID' . $param, 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
9244 + if($generated_text !== false)
9245 + {
9246 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
9247 + {
9248 + $new_post_content .= ' ' . ucfirst(trim(nl2br(trim($generated_text))));
9249 + }
9250 + else
9251 + {
9252 + $new_post_content .= ' ' . ucfirst(trim($generated_text));
9253 + }
9254 + }
9255 + }
9256 + }
9257 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9258 + {
9259 + $api_service = aiomatic_get_api_service($token, $model);
9260 + aiomatic_log_to_file('Successfully got API result from ' . $api_service . '.');
9261 + }
9262 + if($min_char == '')
9263 + {
9264 + $min_char = 0;
9265 + }
9266 + else
9267 + {
9268 + $min_char = intval($min_char);
9269 + }
9270 + $cnt = 1;
9271 + $max_fails = 10;
9272 + $failed_calls = 0;
9273 + $heading_results = $headings_arr;
9274 + if($headings != '' && is_numeric($headings))
9275 + {
9276 + if(count($heading_results) < $headings)
9277 + {
9278 + $heading_results_ai = aiomatic_scrape_related_questions($new_post_title, $headings, $headings_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $headings_ai_command, $assistant_id);
9279 + $heading_results = array_merge($heading_results, $heading_results_ai);
9280 + }
9281 + }
9282 + $ai_retry = false;
9283 + if($image_size == '')
9284 + {
9285 + $image_size = '256x256';
9286 + }
9287 + if(aiomatic_strlen($new_post_content) > $min_char)
9288 + {
9289 + $add_my_image = '';
9290 + $temp_get_img = '';
9291 + if(count($heading_results) > 0)
9292 + {
9293 + $rand_heading = '';
9294 + $saverand = array_rand($heading_results);
9295 + $rand_heading = $heading_results[$saverand];
9296 + unset($heading_results[$saverand]);
9297 + if(isset($rand_heading['q']))
9298 + {
9299 + $rand_heading['q'] = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $rand_heading['q']);
9300 + $heading_val = '<h2>' . $rand_heading['q'] . '</h2>';
9301 + if($rand_heading['a'] != '')
9302 + {
9303 + $heading_val .= '<span>' . $rand_heading['a'] . '</span>';
9304 + }
9305 + $image_query = $rand_heading['q'];
9306 + }
9307 + }
9308 + if($heading_val == '')
9309 + {
9310 + $temp_post = trim($new_post_content);
9311 + }
9312 + else
9313 + {
9314 + $temp_post = trim($heading_val);
9315 + }
9316 +
9317 + if($images != '' && is_numeric($images) && $images > $added_images)
9318 + {
9319 + $query_words = $post_title;
9320 + if($image_query == '')
9321 + {
9322 + $image_query = $temp_post;
9323 + }
9324 + if($enable_ai_images_article == '1')
9325 + {
9326 + if($orig_ai_command_image_article == '')
9327 + {
9328 + $orig_ai_command_image_article = $image_query;
9329 + }
9330 + if($orig_ai_command_image_article != '')
9331 + {
9332 + $ai_command_image_article = $orig_ai_command_image_article;
9333 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9334 + {
9335 + $ai_command_image_article = preg_split('/\r\n|\r|\n/', $ai_command_image_article);
9336 + $ai_command_image_article = array_filter($ai_command_image_article);
9337 + if(count($ai_command_image_article) > 0)
9338 + {
9339 + $ai_command_image_article = $ai_command_image_article[array_rand($ai_command_image_article)];
9340 + }
9341 + else
9342 + {
9343 + $ai_command_image_article = '';
9344 + }
9345 + }
9346 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
9347 + if(!empty($ai_command_image_article))
9348 + {
9349 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9350 + }
9351 + else
9352 + {
9353 + $ai_command_image_article = trim(strip_tags($post_title));
9354 + }
9355 + $ai_command_image_article = trim($ai_command_image_article);
9356 + if (filter_var($ai_command_image_article, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image_article, '.txt'))
9357 + {
9358 + $txt_content = aiomatic_get_web_page($ai_command_image_article);
9359 + if ($txt_content !== FALSE)
9360 + {
9361 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9362 + $txt_content = array_filter($txt_content);
9363 + if(count($txt_content) > 0)
9364 + {
9365 + $txt_content = $txt_content[array_rand($txt_content)];
9366 + if(trim($txt_content) != '')
9367 + {
9368 + $ai_command_image_article = $txt_content;
9369 + $ai_command_image_article = aiomatic_replaceSynergyShortcodes($ai_command_image_article);
9370 + $ai_command_image_article = aiomatic_replaceAIPostShortcodes($ai_command_image_article, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9371 + }
9372 + }
9373 + }
9374 + }
9375 + if(empty($ai_command_image_article))
9376 + {
9377 + aiomatic_log_to_file('Empty API image seed expression provided!');
9378 + }
9379 + else
9380 + {
9381 + if(aiomatic_strlen($ai_command_image_article) > 1000)
9382 + {
9383 + $ai_command_image_article = aiomatic_substr($ai_command_image_article, 0, 1000);
9384 + }
9385 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9386 + {
9387 + $api_service = aiomatic_get_api_service($token, $image_model_article);
9388 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image_article);
9389 + }
9390 + $aierror = '';
9391 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
9392 + $temp_get_imgs = aiomatic_generate_ai_image($token, 1, $ai_command_image_article, $image_size_article, 'contentImage', false, 0, $aierror, $image_model_article, $post_title);
9393 + if($temp_get_imgs !== false)
9394 + {
9395 + foreach($temp_get_imgs as $tmpimg)
9396 + {
9397 + $added_images++;
9398 + $added_img_list[] = $tmpimg;
9399 + $temp_get_img = $tmpimg;
9400 + }
9401 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9402 + {
9403 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_img);
9404 + }
9405 + }
9406 + else
9407 + {
9408 + aiomatic_log_to_file('Failed to generate AI image: ' . $aierror);
9409 + $temp_get_img = '';
9410 + }
9411 + }
9412 + }
9413 + else
9414 + {
9415 + aiomatic_log_to_file('Empty AI image query entered.');
9416 + }
9417 + }
9418 + elseif($enable_ai_images_article == '2')
9419 + {
9420 + if($orig_ai_command_image_article == '')
9421 + {
9422 + $orig_ai_command_image_article = $image_query;
9423 + }
9424 + if($orig_ai_command_image_article != '')
9425 + {
9426 + $ai_command_image = $orig_ai_command_image_article;
9427 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9428 + {
9429 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
9430 + $ai_command_image = array_filter($ai_command_image);
9431 + if(count($ai_command_image) > 0)
9432 + {
9433 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
9434 + }
9435 + else
9436 + {
9437 + $ai_command_image = '';
9438 + }
9439 + }
9440 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9441 + if(!empty($ai_command_image))
9442 + {
9443 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9444 + }
9445 + else
9446 + {
9447 + $ai_command_image = trim(strip_tags($post_title));
9448 + }
9449 + $ai_command_image = trim($ai_command_image);
9450 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
9451 + {
9452 + $txt_content = aiomatic_get_web_page($ai_command_image);
9453 + if ($txt_content !== FALSE)
9454 + {
9455 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9456 + $txt_content = array_filter($txt_content);
9457 + if(count($txt_content) > 0)
9458 + {
9459 + $txt_content = $txt_content[array_rand($txt_content)];
9460 + if(trim($txt_content) != '')
9461 + {
9462 + $ai_command_image = $txt_content;
9463 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9464 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9465 + }
9466 + }
9467 + }
9468 + }
9469 + if(empty($ai_command_image))
9470 + {
9471 + aiomatic_log_to_file('Empty API image seed expression provided!');
9472 + }
9473 + else
9474 + {
9475 + if(aiomatic_strlen($ai_command_image) > 2000)
9476 + {
9477 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
9478 + }
9479 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9480 + {
9481 + $api_service = 'Stability.AI';
9482 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
9483 + }
9484 + if($image_size_article == '256x256')
9485 + {
9486 + $width = '512';
9487 + $height = '512';
9488 + }
9489 + elseif($image_size_article == '512x512')
9490 + {
9491 + $width = '512';
9492 + $height = '512';
9493 + }
9494 + elseif($image_size_article == '1024x1024')
9495 + {
9496 + $width = '1024';
9497 + $height = '1024';
9498 + }
9499 + else
9500 + {
9501 + $width = '512';
9502 + $height = '512';
9503 + }
9504 + $ierror = '';
9505 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image (Stable Diffusion)'));
9506 + $temp_get_imgs = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'contentStableImage', 0, false, $ierror, false, false, '', 0);
9507 + if($temp_get_imgs !== false)
9508 + {
9509 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9510 + {
9511 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
9512 + }
9513 + $added_images++;
9514 + $added_img_list[] = $temp_get_imgs[1];
9515 + $temp_get_img = $temp_get_imgs[1];
9516 + }
9517 + else
9518 + {
9519 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
9520 + $temp_get_img = '';
9521 + }
9522 + }
9523 + }
9524 + else
9525 + {
9526 + aiomatic_log_to_file('Empty AI image query entered.');
9527 + }
9528 + }
9529 + elseif($enable_ai_images_article == '7')
9530 + {
9531 + if($orig_ai_command_image_article == '')
9532 + {
9533 + $orig_ai_command_image_article = $image_query;
9534 + }
9535 + if($orig_ai_command_image_article != '')
9536 + {
9537 + $ai_command_image = $orig_ai_command_image_article;
9538 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9539 + {
9540 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
9541 + $ai_command_image = array_filter($ai_command_image);
9542 + if(count($ai_command_image) > 0)
9543 + {
9544 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
9545 + }
9546 + else
9547 + {
9548 + $ai_command_image = '';
9549 + }
9550 + }
9551 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9552 + if(!empty($ai_command_image))
9553 + {
9554 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9555 + }
9556 + else
9557 + {
9558 + $ai_command_image = trim(strip_tags($post_title));
9559 + }
9560 + $ai_command_image = trim($ai_command_image);
9561 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
9562 + {
9563 + $txt_content = aiomatic_get_web_page($ai_command_image);
9564 + if ($txt_content !== FALSE)
9565 + {
9566 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9567 + $txt_content = array_filter($txt_content);
9568 + if(count($txt_content) > 0)
9569 + {
9570 + $txt_content = $txt_content[array_rand($txt_content)];
9571 + if(trim($txt_content) != '')
9572 + {
9573 + $ai_command_image = $txt_content;
9574 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9575 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9576 + }
9577 + }
9578 + }
9579 + }
9580 + if(empty($ai_command_image))
9581 + {
9582 + aiomatic_log_to_file('Empty API image seed expression provided!');
9583 + }
9584 + else
9585 + {
9586 + if(aiomatic_strlen($ai_command_image) > 2000)
9587 + {
9588 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
9589 + }
9590 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9591 + {
9592 + $api_service = 'Google';
9593 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
9594 + }
9595 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
9596 + {
9597 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
9598 + }
9599 + else
9600 + {
9601 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
9602 + }
9603 + $ierror = '';
9604 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image (Google)'));
9605 + $temp_get_imgs = aiomatic_generate_image_google($ai_command_image, $model, 'contentGoogleImage', false, $ierror, false);
9606 + if($temp_get_imgs !== false)
9607 + {
9608 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9609 + {
9610 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
9611 + }
9612 + $added_images++;
9613 + $added_img_list[] = $temp_get_imgs[1];
9614 + $temp_get_img = $temp_get_imgs[1];
9615 + }
9616 + else
9617 + {
9618 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
9619 + $temp_get_img = '';
9620 + }
9621 + }
9622 + }
9623 + else
9624 + {
9625 + aiomatic_log_to_file('Empty AI image query entered.');
9626 + }
9627 + }
9628 + elseif($enable_ai_images_article == '4')
9629 + {
9630 + if($orig_ai_command_image_article == '')
9631 + {
9632 + $orig_ai_command_image_article = $image_query;
9633 + }
9634 + if($orig_ai_command_image_article != '')
9635 + {
9636 + $ai_command_image = $orig_ai_command_image_article;
9637 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9638 + {
9639 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
9640 + $ai_command_image = array_filter($ai_command_image);
9641 + if(count($ai_command_image) > 0)
9642 + {
9643 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
9644 + }
9645 + else
9646 + {
9647 + $ai_command_image = '';
9648 + }
9649 + }
9650 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9651 + if(!empty($ai_command_image))
9652 + {
9653 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9654 + }
9655 + else
9656 + {
9657 + $ai_command_image = trim(strip_tags($post_title));
9658 + }
9659 + $ai_command_image = trim($ai_command_image);
9660 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
9661 + {
9662 + $txt_content = aiomatic_get_web_page($ai_command_image);
9663 + if ($txt_content !== FALSE)
9664 + {
9665 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9666 + $txt_content = array_filter($txt_content);
9667 + if(count($txt_content) > 0)
9668 + {
9669 + $txt_content = $txt_content[array_rand($txt_content)];
9670 + if(trim($txt_content) != '')
9671 + {
9672 + $ai_command_image = $txt_content;
9673 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9674 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9675 + }
9676 + }
9677 + }
9678 + }
9679 + if(empty($ai_command_image))
9680 + {
9681 + aiomatic_log_to_file('Empty API image seed expression provided!');
9682 + }
9683 + else
9684 + {
9685 + if(aiomatic_strlen($ai_command_image) > 2000)
9686 + {
9687 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
9688 + }
9689 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9690 + {
9691 + $api_service = 'Midjourney';
9692 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
9693 + }
9694 + if($image_size_article == '256x256')
9695 + {
9696 + $width = '512';
9697 + $height = '512';
9698 + }
9699 + elseif($image_size_article == '512x512')
9700 + {
9701 + $width = '512';
9702 + $height = '512';
9703 + }
9704 + elseif($image_size_article == '1024x1024')
9705 + {
9706 + $width = '1024';
9707 + $height = '1024';
9708 + }
9709 + elseif($image_size_article == '1792x1024')
9710 + {
9711 + $width = '1792';
9712 + $height = '1024';
9713 + }
9714 + elseif($image_size_article == '1024x1792')
9715 + {
9716 + $width = '1024';
9717 + $height = '1792';
9718 + }
9719 + else
9720 + {
9721 + $width = '512';
9722 + $height = '512';
9723 + }
9724 + $ierror = '';
9725 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image (Midjourney)'));
9726 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'contentMidjourneyImage', false, $ierror);
9727 + if($temp_get_imgs !== false)
9728 + {
9729 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9730 + {
9731 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
9732 + }
9733 + $added_images++;
9734 + $added_img_list[] = $temp_get_imgs;
9735 + $temp_get_img = $temp_get_imgs;
9736 + }
9737 + else
9738 + {
9739 + aiomatic_log_to_file('Failed to generate Midjourney image: ' . $ierror);
9740 + $temp_get_img = '';
9741 + }
9742 + }
9743 + }
9744 + else
9745 + {
9746 + aiomatic_log_to_file('Empty AI image query entered.');
9747 + }
9748 + }
9749 + elseif($enable_ai_images_article == '5')
9750 + {
9751 + if($orig_ai_command_image_article == '')
9752 + {
9753 + $orig_ai_command_image_article = $image_query;
9754 + }
9755 + if($orig_ai_command_image_article != '')
9756 + {
9757 + $ai_command_image = $orig_ai_command_image_article;
9758 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9759 + {
9760 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
9761 + $ai_command_image = array_filter($ai_command_image);
9762 + if(count($ai_command_image) > 0)
9763 + {
9764 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
9765 + }
9766 + else
9767 + {
9768 + $ai_command_image = '';
9769 + }
9770 + }
9771 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9772 + if(!empty($ai_command_image))
9773 + {
9774 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9775 + }
9776 + else
9777 + {
9778 + $ai_command_image = trim(strip_tags($post_title));
9779 + }
9780 + $ai_command_image = trim($ai_command_image);
9781 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
9782 + {
9783 + $txt_content = aiomatic_get_web_page($ai_command_image);
9784 + if ($txt_content !== FALSE)
9785 + {
9786 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9787 + $txt_content = array_filter($txt_content);
9788 + if(count($txt_content) > 0)
9789 + {
9790 + $txt_content = $txt_content[array_rand($txt_content)];
9791 + if(trim($txt_content) != '')
9792 + {
9793 + $ai_command_image = $txt_content;
9794 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9795 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9796 + }
9797 + }
9798 + }
9799 + }
9800 + if(empty($ai_command_image))
9801 + {
9802 + aiomatic_log_to_file('Empty API image seed expression provided!');
9803 + }
9804 + else
9805 + {
9806 + if(aiomatic_strlen($ai_command_image) > 2000)
9807 + {
9808 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
9809 + }
9810 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9811 + {
9812 + $api_service = 'Replicate';
9813 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
9814 + }
9815 + if($image_size_article == '256x256')
9816 + {
9817 + $width = '512';
9818 + $height = '512';
9819 + }
9820 + elseif($image_size_article == '512x512')
9821 + {
9822 + $width = '512';
9823 + $height = '512';
9824 + }
9825 + elseif($image_size_article == '1024x1024')
9826 + {
9827 + $width = '1024';
9828 + $height = '1024';
9829 + }
9830 + elseif($image_size_article == '1792x1024')
9831 + {
9832 + $width = '1792';
9833 + $height = '1024';
9834 + }
9835 + elseif($image_size_article == '1024x1792')
9836 + {
9837 + $width = '1024';
9838 + $height = '1792';
9839 + }
9840 + else
9841 + {
9842 + $width = '512';
9843 + $height = '512';
9844 + }
9845 + $ierror = '';
9846 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image (Replicate)'));
9847 + $temp_get_imgs = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'contentReplicateImage', false, $ierror);
9848 + if($temp_get_imgs !== false)
9849 + {
9850 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9851 + {
9852 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
9853 + }
9854 + $added_images++;
9855 + $added_img_list[] = $temp_get_imgs;
9856 + $temp_get_img = $temp_get_imgs;
9857 + }
9858 + else
9859 + {
9860 + aiomatic_log_to_file('Failed to generate Replicate image: ' . $ierror);
9861 + $temp_get_img = '';
9862 + }
9863 + }
9864 + }
9865 + else
9866 + {
9867 + aiomatic_log_to_file('Empty AI image query entered.');
9868 + }
9869 + }
9870 + elseif($enable_ai_images_article == '6')
9871 + {
9872 + if($orig_ai_command_image_article == '')
9873 + {
9874 + $orig_ai_command_image_article = $image_query;
9875 + }
9876 + if($orig_ai_command_image_article != '')
9877 + {
9878 + $ai_command_image = $orig_ai_command_image_article;
9879 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
9880 + {
9881 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
9882 + $ai_command_image = array_filter($ai_command_image);
9883 + if(count($ai_command_image) > 0)
9884 + {
9885 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
9886 + }
9887 + else
9888 + {
9889 + $ai_command_image = '';
9890 + }
9891 + }
9892 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9893 + if(!empty($ai_command_image))
9894 + {
9895 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9896 + }
9897 + else
9898 + {
9899 + $ai_command_image = trim(strip_tags($post_title));
9900 + }
9901 + $ai_command_image = trim($ai_command_image);
9902 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
9903 + {
9904 + $txt_content = aiomatic_get_web_page($ai_command_image);
9905 + if ($txt_content !== FALSE)
9906 + {
9907 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
9908 + $txt_content = array_filter($txt_content);
9909 + if(count($txt_content) > 0)
9910 + {
9911 + $txt_content = $txt_content[array_rand($txt_content)];
9912 + if(trim($txt_content) != '')
9913 + {
9914 + $ai_command_image = $txt_content;
9915 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
9916 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
9917 + }
9918 + }
9919 + }
9920 + }
9921 + if(empty($ai_command_image))
9922 + {
9923 + aiomatic_log_to_file('Empty API image seed expression provided!');
9924 + }
9925 + else
9926 + {
9927 + if(aiomatic_strlen($ai_command_image) > 2000)
9928 + {
9929 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
9930 + }
9931 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9932 + {
9933 + $api_service = 'Ideogram';
9934 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
9935 + }
9936 + if($image_size_article == '256x256')
9937 + {
9938 + $width = '512';
9939 + $height = '512';
9940 + }
9941 + elseif($image_size_article == '512x512')
9942 + {
9943 + $width = '512';
9944 + $height = '512';
9945 + }
9946 + elseif($image_size_article == '1024x1024')
9947 + {
9948 + $width = '1024';
9949 + $height = '1024';
9950 + }
9951 + elseif($image_size_article == '1792x1024')
9952 + {
9953 + $width = '1792';
9954 + $height = '1024';
9955 + }
9956 + elseif($image_size_article == '1024x1792')
9957 + {
9958 + $width = '1024';
9959 + $height = '1792';
9960 + }
9961 + else
9962 + {
9963 + $width = '512';
9964 + $height = '512';
9965 + }
9966 + $ierror = '';
9967 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image (Ideogram)'));
9968 + $temp_get_imgs = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'contentIdeogramImage', false, $ierror);
9969 + if($temp_get_imgs !== false)
9970 + {
9971 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
9972 + {
9973 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs);
9974 + }
9975 + $added_images++;
9976 + $added_img_list[] = $temp_get_imgs;
9977 + $temp_get_img = $temp_get_imgs;
9978 + }
9979 + else
9980 + {
9981 + aiomatic_log_to_file('Failed to generate Ideogram image: ' . $ierror);
9982 + $temp_get_img = '';
9983 + }
9984 + }
9985 + }
9986 + else
9987 + {
9988 + aiomatic_log_to_file('Empty AI image query entered.');
9989 + }
9990 + }
9991 + elseif($enable_ai_images_article == '3')
9992 + {
9993 + if ($image_url != '')
9994 + {
9995 + $replacement = str_replace(array('[', ']'), '', $query_words);
9996 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
9997 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
9998 + if(isset($matches[2]))
9999 + {
10000 + $chance = trim($matches[2], '[]');
10001 + }
10002 + else
10003 + {
10004 + $chance = '';
10005 + }
10006 + $arv = array();
10007 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
10008 + return $my_img;
10009 + }, $image_url_temp);
10010 + $img_rulx = $spintax->Parse(trim($image_url_temp));
10011 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
10012 + if($selected_img === false)
10013 + {
10014 + $img_rulx = explode(',', $img_rulx);
10015 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
10016 + if($img_rulx != '')
10017 + {
10018 + $added_images++;
10019 + $added_img_list[] = $img_rulx;
10020 + $temp_get_img = $img_rulx;
10021 + }
10022 + }
10023 + else
10024 + {
10025 + $added_images++;
10026 + $added_img_list[] = $selected_img;
10027 + $temp_get_img = $selected_img;
10028 + }
10029 + }
10030 + }
10031 + elseif(count($images_arr) > 0)
10032 + {
10033 + $first_el = array_shift($images_arr);
10034 + $first_el = aiomatic_replaceSynergyShortcodes($first_el);
10035 + $added_images++;
10036 + $added_img_list[] = $first_el;
10037 + $temp_get_img = $first_el;
10038 + }
10039 + else
10040 + {
10041 + $image_query_set = false;
10042 + $query_words = '';
10043 + $ai_command_image = '';
10044 + if($orig_ai_command_image == '')
10045 + {
10046 + $orig_ai_command_image = $image_query;
10047 + }
10048 + if($orig_ai_command_image != '')
10049 + {
10050 + $ai_command_image = $orig_ai_command_image;
10051 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10052 + {
10053 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10054 + $ai_command_image = array_filter($ai_command_image);
10055 + if(count($ai_command_image) > 0)
10056 + {
10057 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10058 + }
10059 + else
10060 + {
10061 + $ai_command_image = '';
10062 + }
10063 + }
10064 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10065 + if(!empty($ai_command_image))
10066 + {
10067 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10068 + }
10069 + else
10070 + {
10071 + $ai_command_image = trim(strip_tags($post_title));
10072 + }
10073 + $ai_command_image = trim($ai_command_image);
10074 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10075 + {
10076 + $txt_content = aiomatic_get_web_page($ai_command_image);
10077 + if ($txt_content !== FALSE)
10078 + {
10079 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10080 + $txt_content = array_filter($txt_content);
10081 + if(count($txt_content) > 0)
10082 + {
10083 + $txt_content = $txt_content[array_rand($txt_content)];
10084 + if(trim($txt_content) != '')
10085 + {
10086 + $ai_command_image = $txt_content;
10087 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10088 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10089 + }
10090 + }
10091 + }
10092 + }
10093 + }
10094 + if($ai_command_image != '')
10095 + {
10096 + $query_words = $ai_command_image;
10097 + $image_query = $ai_command_image;
10098 + $image_query_set = true;
10099 + }
10100 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
10101 + {
10102 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
10103 + {
10104 + try
10105 + {
10106 + if(!class_exists('TextRazor'))
10107 + {
10108 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
10109 + }
10110 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
10111 + $textrazor = new TextRazor();
10112 + $textrazor->addExtractor('entities');
10113 + $response = $textrazor->analyze($image_query);
10114 + if (isset($response['response']['entities']))
10115 + {
10116 + foreach ($response['response']['entities'] as $entity)
10117 + {
10118 + $query_words = '';
10119 + if(isset($entity['entityEnglishId']))
10120 + {
10121 + $query_words = $entity['entityEnglishId'];
10122 + }
10123 + else
10124 + {
10125 + $query_words = $entity['entityId'];
10126 + }
10127 + if($query_words != '')
10128 + {
10129 + $attached_id = '';
10130 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
10131 + if(!empty($attached_id))
10132 + {
10133 + $additional_attachments[] = $attached_id;
10134 + }
10135 + if(!empty($z_img))
10136 + {
10137 + $added_images++;
10138 + $added_img_list[] = $z_img;
10139 + $temp_get_img = $z_img;
10140 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10141 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
10142 + }
10143 + break;
10144 + }
10145 + }
10146 + }
10147 + }
10148 + }
10149 + catch(Exception $e)
10150 + {
10151 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10152 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
10153 + }
10154 + }
10155 + }
10156 + }
10157 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
10158 + {
10159 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
10160 + {
10161 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
10162 + {
10163 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
10164 + }
10165 + else
10166 + {
10167 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
10168 + }
10169 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
10170 + {
10171 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
10172 + }
10173 + else
10174 + {
10175 + $keyword_assistant_id = '';
10176 + }
10177 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
10178 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
10179 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
10180 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10181 + {
10182 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
10183 + $title_ai_command = array_filter($title_ai_command);
10184 + if(count($title_ai_command) > 0)
10185 + {
10186 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
10187 + }
10188 + else
10189 + {
10190 + $title_ai_command = '';
10191 + }
10192 + }
10193 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
10194 + if(!empty($title_ai_command))
10195 + {
10196 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10197 + }
10198 + $title_ai_command = trim($title_ai_command);
10199 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
10200 + {
10201 + $txt_content = aiomatic_get_web_page($title_ai_command);
10202 + if ($txt_content !== FALSE)
10203 + {
10204 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10205 + $txt_content = array_filter($txt_content);
10206 + if(count($txt_content) > 0)
10207 + {
10208 + $txt_content = $txt_content[array_rand($txt_content)];
10209 + if(trim($txt_content) != '')
10210 + {
10211 + $title_ai_command = $txt_content;
10212 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
10213 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10214 + }
10215 + }
10216 + }
10217 + }
10218 + if(empty($title_ai_command))
10219 + {
10220 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
10221 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
10222 + }
10223 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
10224 + {
10225 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
10226 + }
10227 + $title_ai_command = trim($title_ai_command);
10228 + if(empty($title_ai_command))
10229 + {
10230 + aiomatic_log_to_file('Empty API title seed expression provided(4)! ' . print_r($title_ai_command, true));
10231 + }
10232 + else
10233 + {
10234 + $query_token_count = count(aiomatic_encode($title_ai_command));
10235 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
10236 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
10237 + {
10238 + $string_len = aiomatic_strlen($title_ai_command);
10239 + $string_len = $string_len / 2;
10240 + $string_len = intval(0 - $string_len);
10241 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
10242 + $title_ai_command = trim($title_ai_command);
10243 + $query_token_count = count(aiomatic_encode($title_ai_command));
10244 + $available_tokens = $max_tokens - $query_token_count;
10245 + }
10246 + $aierror = '';
10247 + $finish_reason = '';
10248 + if(!empty($global_prepend))
10249 + {
10250 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
10251 + }
10252 + if(!empty($global_append))
10253 + {
10254 + $title_ai_command = $title_ai_command . ' ' . $global_append;
10255 + }
10256 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10257 + {
10258 + $api_service = aiomatic_get_api_service($token, $kw_model);
10259 + aiomatic_log_to_file('Calling ' . $api_service . ' for (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') title text: ' . $title_ai_command);
10260 + }
10261 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating keywords for image search'));
10262 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
10263 + if($generated_text === false)
10264 + {
10265 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
10266 + $ai_title = '';
10267 + }
10268 + else
10269 + {
10270 + $ai_title = trim(trim($generated_text), '.');
10271 + $ai_titles = explode(',', $ai_title);
10272 + $heading_cnt = 1;
10273 + foreach($ai_titles as $query_words)
10274 + {
10275 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating heading images #' . $heading_cnt));
10276 + $heading_cnt++;
10277 + $attached_id = '';
10278 + $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);
10279 + if(!empty($attached_id))
10280 + {
10281 + $additional_attachments[] = $attached_id;
10282 + }
10283 + if(!empty($z_img))
10284 + {
10285 + $added_images++;
10286 + $added_img_list[] = $z_img;
10287 + $temp_get_img = $z_img;
10288 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10289 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
10290 + }
10291 + break;
10292 + }
10293 + }
10294 + }
10295 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10296 + {
10297 + $api_service = aiomatic_get_api_service($token, $kw_model);
10298 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
10299 + }
10300 + }
10301 + }
10302 + }
10303 + if(empty($temp_get_img))
10304 + {
10305 + $keyword_class = new Aiomatic_keywords();
10306 + $query_words = $keyword_class->keywords($image_query, 2);
10307 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free image'));
10308 + $attached_id = '';
10309 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
10310 + if(!empty($attached_id))
10311 + {
10312 + $additional_attachments[] = $attached_id;
10313 + }
10314 + if($temp_get_img == '' || $temp_get_img === false)
10315 + {
10316 + $query_words = $keyword_class->keywords($image_query, 1);
10317 + $attached_id = '';
10318 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, false, $raw_img_list, array(), $full_result_list);
10319 + if(!empty($attached_id))
10320 + {
10321 + $additional_attachments[] = $attached_id;
10322 + }
10323 + if($temp_get_img == '' || $temp_get_img === false)
10324 + {
10325 + $temp_get_img = '';
10326 + }
10327 + else
10328 + {
10329 + if(!in_array($temp_get_img, $added_img_list))
10330 + {
10331 + $added_images++;
10332 + $added_img_list[] = $temp_get_img;
10333 + }
10334 + else
10335 + {
10336 + $temp_get_img = '';
10337 + }
10338 + }
10339 + }
10340 + else
10341 + {
10342 + if(!in_array($temp_get_img, $added_img_list))
10343 + {
10344 + $added_images++;
10345 + $added_img_list[] = $temp_get_img;
10346 + }
10347 + else
10348 + {
10349 + $temp_get_img = '';
10350 + }
10351 + }
10352 + }
10353 + }
10354 + if($temp_get_img != '')
10355 + {
10356 + $add_my_image = '<br/><img class="vimage_class" src="' . $temp_get_img . '" alt="' . esc_attr($query_words) . '"><br/>';
10357 + }
10358 + }
10359 + if($heading_val == '')
10360 + {
10361 + $new_post_content = $add_my_image . $new_post_content;
10362 + }
10363 + else
10364 + {
10365 + $new_post_content = $add_my_image . $heading_val . ' ' . $new_post_content;
10366 + }
10367 + }
10368 + else
10369 + {
10370 + $ai_continue_title = $post_title;
10371 + $extend_id = 1;
10372 + while(aiomatic_strlen(strip_tags($new_post_content)) < $min_char)
10373 + {
10374 + 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)
10375 + {
10376 + break;
10377 + }
10378 + $just_set_fallback = false;
10379 + $image_query = '';
10380 + $heading_val = '';
10381 + if(count($heading_results) > 0)
10382 + {
10383 + $rand_heading = '';
10384 + $saverand = array_rand($heading_results);
10385 + $rand_heading = $heading_results[$saverand];
10386 + unset($heading_results[$saverand]);
10387 + if(isset($rand_heading['q']))
10388 + {
10389 + $rand_heading['q'] = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $rand_heading['q']);
10390 + $heading_val = '<h2>' . $rand_heading['q'] . '</h2>' . '<span>' . $rand_heading['a'];
10391 + $image_query = $rand_heading['q'];
10392 + }
10393 + }
10394 +
10395 + if($heading_val == '')
10396 + {
10397 + $temp_post = trim($new_post_content);
10398 + }
10399 + else
10400 + {
10401 + $temp_post = trim($heading_val);
10402 + }
10403 + if(aiomatic_strlen($temp_post) > $max_continue_tokens * 4)
10404 + {
10405 + $negative_contiue_tokens = 0 - ($max_continue_tokens * 4);
10406 + $newaicontent = aiomatic_substr($temp_post, $negative_contiue_tokens, null);
10407 + }
10408 + else
10409 + {
10410 + $newaicontent = $temp_post;
10411 + }
10412 + $add_me_to_text = '';
10413 + if($ai_retry == true)
10414 + {
10415 + $just_set_fallback = true;
10416 + if(count($headings_arr_copy) == 0)
10417 + {
10418 + if (isset($aiomatic_Main_Settings['alternate_continue']) && $aiomatic_Main_Settings['alternate_continue'] == 'on')
10419 + {
10420 + $newaicontent = $newaicontent . ' ' . $ai_continue_title;
10421 + }
10422 + else
10423 + {
10424 + $prai = 'Write a People Also Asked question related to "' . $ai_continue_title . '"';
10425 + $aierror = '';
10426 + $finish_reason = '';
10427 + if(!empty($global_prepend))
10428 + {
10429 + $prai = $global_prepend . ' ' . $prai;
10430 + }
10431 + if(!empty($global_append))
10432 + {
10433 + $prai = $prai . ' ' . $global_append;
10434 + }
10435 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10436 + {
10437 + $api_service = aiomatic_get_api_service($token, $model);
10438 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for PAA question generator: ' . $prai);
10439 + }
10440 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating PAA questions'));
10441 + $generated_text = aiomatic_generate_text($token, $model, $prai, aimogenpro_get_max_tokens($model), $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'headingID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
10442 + if($generated_text === false)
10443 + {
10444 + aiomatic_log_to_file('Similarity finding failed: ' . $aierror);
10445 + $newaicontent = $aicontent;
10446 + }
10447 + else
10448 + {
10449 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
10450 + {
10451 + $newaicontent = ucfirst(trim(nl2br(trim($generated_text))));
10452 + }
10453 + else
10454 + {
10455 + $newaicontent = ucfirst(trim($generated_text));
10456 + }
10457 +
10458 + if(empty($newaicontent))
10459 + {
10460 + $newaicontent = $aicontent;
10461 + }
10462 + else
10463 + {
10464 + $newaicontent = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $newaicontent);
10465 + $add_me_to_text = '<h3>' . $newaicontent . '</h3> ';
10466 + $ai_continue_title = $newaicontent;
10467 + }
10468 + }
10469 + }
10470 + }
10471 + else
10472 + {
10473 + $randomIndex = array_rand($headings_arr_copy);
10474 + $newaicontent = $headings_arr_copy[$randomIndex];
10475 + unset($headings_arr_copy[$randomIndex]);
10476 + $newaicontent = preg_replace('#^\d+\.([\s\S]*)#i', '$1', $newaicontent);
10477 + $add_me_to_text = '<h3>' . $newaicontent . '</h3> ';
10478 + }
10479 + }
10480 + $ai_retry = false;
10481 + $newaicontent = trim($newaicontent);
10482 + $query_token_count = count(aiomatic_encode($newaicontent));
10483 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $newaicontent, $query_token_count);
10484 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
10485 + {
10486 + $string_len = aiomatic_strlen($newaicontent);
10487 + $string_len = $string_len / 2;
10488 + $string_len = intval(0 - $string_len);
10489 + $newaicontent = aiomatic_substr($newaicontent, 0, $string_len);
10490 + $newaicontent = trim($newaicontent);
10491 + if(empty($newaicontent))
10492 + {
10493 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($temp_post, true));
10494 + break;
10495 + }
10496 + $query_token_count = count(aiomatic_encode($newaicontent));
10497 + $available_tokens = $max_tokens - $query_token_count;
10498 + }
10499 + $aiwriter = '';
10500 + $aierror = '';
10501 + $finish_reason = '';
10502 + if(!empty($global_prepend))
10503 + {
10504 + $newaicontent = $global_prepend . ' ' . $newaicontent;
10505 + }
10506 + if(!empty($global_append))
10507 + {
10508 + $newaicontent = $newaicontent . ' ' . $global_append;
10509 + }
10510 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
10511 + $api_service = aiomatic_get_api_service($token, $model);
10512 + $rwair = '';
10513 + if($just_set_fallback == true)
10514 + {
10515 + $rwair = '(fallback)';
10516 + }
10517 + aiomatic_log_to_file('Calling ' . $api_service . ' again (' . $cnt . ')(' . $available_tokens . ')' . $rwair . ', to meet minimum character limit: ' . $min_char . ' - current char count: ' . aiomatic_strlen(strip_tags($new_post_content)));
10518 + }
10519 + if (isset($aiomatic_Main_Settings['continue_prepend']) && $aiomatic_Main_Settings['continue_prepend'] != '')
10520 + {
10521 + $newaicontent = $aiomatic_Main_Settings['continue_prepend'] . '\n\n' . $newaicontent;
10522 + }
10523 + if (isset($aiomatic_Main_Settings['continue_append']) && $aiomatic_Main_Settings['continue_append'] != '')
10524 + {
10525 + $newaicontent = $newaicontent . '\n\n' . $aiomatic_Main_Settings['continue_append'];
10526 + }
10527 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating post content extensions #' . $extend_id));
10528 + $extend_id++;
10529 + $generated_text = aiomatic_generate_text($token, $model, $newaicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentID' . $param, 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
10530 + if($generated_text === false)
10531 + {
10532 + aiomatic_log_to_file($aierror);
10533 + break;
10534 + }
10535 + else
10536 + {
10537 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
10538 + {
10539 + $aiwriter = $add_me_to_text . ucfirst(trim(nl2br(trim($generated_text))));
10540 + }
10541 + else
10542 + {
10543 + $aiwriter = $add_me_to_text . ucfirst(trim($generated_text));
10544 + }
10545 + }
10546 + $add_my_image = '';
10547 + $temp_get_img = '';
10548 + if($aiwriter == '')
10549 + {
10550 + $ai_retry = true;
10551 + if($just_set_fallback == true)
10552 + {
10553 + aiomatic_log_to_file('Ending execution, already retried once');
10554 + break;
10555 + }
10556 + unset($post_title_lines[$current_index]);
10557 + continue;
10558 + }
10559 + if($images != '' && is_numeric($images) && $images > $added_images)
10560 + {
10561 + $image_query_set = false;
10562 + $query_words = '';
10563 + $ai_command_image = '';
10564 + if($orig_ai_command_image == '')
10565 + {
10566 + $orig_ai_command_image = $image_query;
10567 + }
10568 + if($orig_ai_command_image != '')
10569 + {
10570 + $ai_command_image = $orig_ai_command_image;
10571 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10572 + {
10573 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10574 + $ai_command_image = array_filter($ai_command_image);
10575 + if(count($ai_command_image) > 0)
10576 + {
10577 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10578 + }
10579 + else
10580 + {
10581 + $ai_command_image = '';
10582 + }
10583 + }
10584 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10585 + if(!empty($ai_command_image))
10586 + {
10587 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10588 + }
10589 + else
10590 + {
10591 + $ai_command_image = trim(strip_tags($post_title));
10592 + }
10593 + $ai_command_image = trim($ai_command_image);
10594 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10595 + {
10596 + $txt_content = aiomatic_get_web_page($ai_command_image);
10597 + if ($txt_content !== FALSE)
10598 + {
10599 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10600 + $txt_content = array_filter($txt_content);
10601 + if(count($txt_content) > 0)
10602 + {
10603 + $txt_content = $txt_content[array_rand($txt_content)];
10604 + if(trim($txt_content) != '')
10605 + {
10606 + $ai_command_image = $txt_content;
10607 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10608 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10609 + }
10610 + }
10611 + }
10612 + }
10613 + }
10614 + if($ai_command_image != '')
10615 + {
10616 + $query_words = $ai_command_image;
10617 + $image_query = $ai_command_image;
10618 + $image_query_set = true;
10619 + }
10620 + else
10621 + {
10622 + $query_words = $post_title;
10623 + }
10624 + if($image_query == '')
10625 + {
10626 + $image_query = $temp_post;
10627 + }
10628 + if($enable_ai_images_article == '1')
10629 + {
10630 + if($orig_ai_command_image_article == '')
10631 + {
10632 + $orig_ai_command_image_article = $image_query;
10633 + }
10634 + if($orig_ai_command_image_article != '')
10635 + {
10636 + $ai_command_image = $orig_ai_command_image_article;
10637 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10638 + {
10639 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10640 + $ai_command_image = array_filter($ai_command_image);
10641 + if(count($ai_command_image) > 0)
10642 + {
10643 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10644 + }
10645 + else
10646 + {
10647 + $ai_command_image = '';
10648 + }
10649 + }
10650 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10651 + if(!empty($ai_command_image))
10652 + {
10653 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10654 + }
10655 + else
10656 + {
10657 + $ai_command_image = trim(strip_tags($post_title));
10658 + }
10659 + $ai_command_image = trim($ai_command_image);
10660 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10661 + {
10662 + $txt_content = aiomatic_get_web_page($ai_command_image);
10663 + if ($txt_content !== FALSE)
10664 + {
10665 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10666 + $txt_content = array_filter($txt_content);
10667 + if(count($txt_content) > 0)
10668 + {
10669 + $txt_content = $txt_content[array_rand($txt_content)];
10670 + if(trim($txt_content) != '')
10671 + {
10672 + $ai_command_image = $txt_content;
10673 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10674 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10675 + }
10676 + }
10677 + }
10678 + }
10679 + if(empty($ai_command_image))
10680 + {
10681 + aiomatic_log_to_file('Empty API image seed expression provided!');
10682 + }
10683 + else
10684 + {
10685 + if(aiomatic_strlen($ai_command_image) > 1000)
10686 + {
10687 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
10688 + }
10689 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10690 + {
10691 + $api_service = aiomatic_get_api_service($token, $image_model_article);
10692 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
10693 + }
10694 + $aierror = '';
10695 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
10696 + $temp_get_imgs = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size_article, 'contentImage', false, 0, $aierror, $image_model_article, $post_title);
10697 + if($temp_get_imgs !== false)
10698 + {
10699 + foreach($temp_get_imgs as $tmpimg)
10700 + {
10701 + $added_images++;
10702 + $added_img_list[] = $tmpimg;
10703 + $temp_get_img = $tmpimg;
10704 + }
10705 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10706 + {
10707 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_img);
10708 + }
10709 + }
10710 + else
10711 + {
10712 + aiomatic_log_to_file('Failed to generate AI image: ' . $aierror);
10713 + $temp_get_img = '';
10714 + }
10715 + }
10716 + }
10717 + else
10718 + {
10719 + aiomatic_log_to_file('Empty AI image query entered.');
10720 + }
10721 + }
10722 + elseif($enable_ai_images_article == '2')
10723 + {
10724 + if($orig_ai_command_image_article == '')
10725 + {
10726 + $orig_ai_command_image_article = $image_query;
10727 + }
10728 + if($orig_ai_command_image_article != '')
10729 + {
10730 + $ai_command_image = $orig_ai_command_image_article;
10731 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10732 + {
10733 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10734 + $ai_command_image = array_filter($ai_command_image);
10735 + if(count($ai_command_image) > 0)
10736 + {
10737 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10738 + }
10739 + else
10740 + {
10741 + $ai_command_image = '';
10742 + }
10743 + }
10744 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10745 + if(!empty($ai_command_image))
10746 + {
10747 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10748 + }
10749 + else
10750 + {
10751 + $ai_command_image = trim(strip_tags($post_title));
10752 + }
10753 + $ai_command_image = trim($ai_command_image);
10754 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10755 + {
10756 + $txt_content = aiomatic_get_web_page($ai_command_image);
10757 + if ($txt_content !== FALSE)
10758 + {
10759 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10760 + $txt_content = array_filter($txt_content);
10761 + if(count($txt_content) > 0)
10762 + {
10763 + $txt_content = $txt_content[array_rand($txt_content)];
10764 + if(trim($txt_content) != '')
10765 + {
10766 + $ai_command_image = $txt_content;
10767 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10768 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10769 + }
10770 + }
10771 + }
10772 + }
10773 + if(empty($ai_command_image))
10774 + {
10775 + aiomatic_log_to_file('Empty API image seed expression provided!');
10776 + }
10777 + else
10778 + {
10779 + if(aiomatic_strlen($ai_command_image) > 2000)
10780 + {
10781 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
10782 + }
10783 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10784 + {
10785 + $api_service = 'Stability.AI';
10786 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
10787 + }
10788 + if($image_size_article == '256x256')
10789 + {
10790 + $width = '512';
10791 + $height = '512';
10792 + }
10793 + elseif($image_size_article == '512x512')
10794 + {
10795 + $width = '512';
10796 + $height = '512';
10797 + }
10798 + elseif($image_size_article == '1024x1024')
10799 + {
10800 + $width = '1024';
10801 + $height = '1024';
10802 + }
10803 + else
10804 + {
10805 + $width = '512';
10806 + $height = '512';
10807 + }
10808 + $ierror = '';
10809 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Stable Diffusion image'));
10810 + $temp_get_imgs = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'contentStableImage', 0, false, $ierror, false, false, '', 0);
10811 + if($temp_get_imgs !== false)
10812 + {
10813 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10814 + {
10815 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
10816 + }
10817 + $added_images++;
10818 + $added_img_list[] = $temp_get_imgs[1];
10819 + $temp_get_img = $temp_get_imgs[1];
10820 + }
10821 + else
10822 + {
10823 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
10824 + $temp_get_img = '';
10825 + }
10826 + }
10827 + }
10828 + else
10829 + {
10830 + aiomatic_log_to_file('Empty AI image query entered.');
10831 + }
10832 + }
10833 + elseif($enable_ai_images_article == '7')
10834 + {
10835 + if($orig_ai_command_image_article == '')
10836 + {
10837 + $orig_ai_command_image_article = $image_query;
10838 + }
10839 + if($orig_ai_command_image_article != '')
10840 + {
10841 + $ai_command_image = $orig_ai_command_image_article;
10842 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10843 + {
10844 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10845 + $ai_command_image = array_filter($ai_command_image);
10846 + if(count($ai_command_image) > 0)
10847 + {
10848 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10849 + }
10850 + else
10851 + {
10852 + $ai_command_image = '';
10853 + }
10854 + }
10855 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10856 + if(!empty($ai_command_image))
10857 + {
10858 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10859 + }
10860 + else
10861 + {
10862 + $ai_command_image = trim(strip_tags($post_title));
10863 + }
10864 + $ai_command_image = trim($ai_command_image);
10865 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10866 + {
10867 + $txt_content = aiomatic_get_web_page($ai_command_image);
10868 + if ($txt_content !== FALSE)
10869 + {
10870 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10871 + $txt_content = array_filter($txt_content);
10872 + if(count($txt_content) > 0)
10873 + {
10874 + $txt_content = $txt_content[array_rand($txt_content)];
10875 + if(trim($txt_content) != '')
10876 + {
10877 + $ai_command_image = $txt_content;
10878 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10879 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10880 + }
10881 + }
10882 + }
10883 + }
10884 + if(empty($ai_command_image))
10885 + {
10886 + aiomatic_log_to_file('Empty API image seed expression provided!');
10887 + }
10888 + else
10889 + {
10890 + if(aiomatic_strlen($ai_command_image) > 2000)
10891 + {
10892 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
10893 + }
10894 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10895 + {
10896 + $api_service = 'Google';
10897 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
10898 + }
10899 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
10900 + {
10901 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
10902 + }
10903 + else
10904 + {
10905 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
10906 + }
10907 + $ierror = '';
10908 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Google image'));
10909 + $temp_get_imgs = aiomatic_generate_image_google($ai_command_image, $model, 'contentGoogleImage', false, $ierror, false);
10910 + if($temp_get_imgs !== false)
10911 + {
10912 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10913 + {
10914 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
10915 + }
10916 + $added_images++;
10917 + $added_img_list[] = $temp_get_imgs[1];
10918 + $temp_get_img = $temp_get_imgs[1];
10919 + }
10920 + else
10921 + {
10922 + aiomatic_log_to_file('Failed to generate Stability.AI image: ' . $ierror);
10923 + $temp_get_img = '';
10924 + }
10925 + }
10926 + }
10927 + else
10928 + {
10929 + aiomatic_log_to_file('Empty AI image query entered.');
10930 + }
10931 + }
10932 + elseif($enable_ai_images_article == '4')
10933 + {
10934 + if($orig_ai_command_image_article == '')
10935 + {
10936 + $orig_ai_command_image_article = $image_query;
10937 + }
10938 + if($orig_ai_command_image_article != '')
10939 + {
10940 + $ai_command_image = $orig_ai_command_image_article;
10941 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
10942 + {
10943 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
10944 + $ai_command_image = array_filter($ai_command_image);
10945 + if(count($ai_command_image) > 0)
10946 + {
10947 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
10948 + }
10949 + else
10950 + {
10951 + $ai_command_image = '';
10952 + }
10953 + }
10954 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10955 + if(!empty($ai_command_image))
10956 + {
10957 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10958 + }
10959 + else
10960 + {
10961 + $ai_command_image = trim(strip_tags($post_title));
10962 + }
10963 + $ai_command_image = trim($ai_command_image);
10964 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
10965 + {
10966 + $txt_content = aiomatic_get_web_page($ai_command_image);
10967 + if ($txt_content !== FALSE)
10968 + {
10969 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
10970 + $txt_content = array_filter($txt_content);
10971 + if(count($txt_content) > 0)
10972 + {
10973 + $txt_content = $txt_content[array_rand($txt_content)];
10974 + if(trim($txt_content) != '')
10975 + {
10976 + $ai_command_image = $txt_content;
10977 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
10978 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
10979 + }
10980 + }
10981 + }
10982 + }
10983 + if(empty($ai_command_image))
10984 + {
10985 + aiomatic_log_to_file('Empty API image seed expression provided!');
10986 + }
10987 + else
10988 + {
10989 + if(aiomatic_strlen($ai_command_image) > 2000)
10990 + {
10991 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
10992 + }
10993 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
10994 + {
10995 + $api_service = 'Midjourney';
10996 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
10997 + }
10998 + if($image_size_article == '256x256')
10999 + {
11000 + $width = '512';
11001 + $height = '512';
11002 + }
11003 + elseif($image_size_article == '512x512')
11004 + {
11005 + $width = '512';
11006 + $height = '512';
11007 + }
11008 + elseif($image_size_article == '1024x1024')
11009 + {
11010 + $width = '1024';
11011 + $height = '1024';
11012 + }
11013 + elseif($image_size_article == '1792x1024')
11014 + {
11015 + $width = '1792';
11016 + $height = '1024';
11017 + }
11018 + elseif($image_size_article == '1024x1792')
11019 + {
11020 + $width = '1024';
11021 + $height = '1792';
11022 + }
11023 + else
11024 + {
11025 + $width = '512';
11026 + $height = '512';
11027 + }
11028 + $ierror = '';
11029 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Midjourney image'));
11030 + $temp_get_imgs = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'contentMidjourneyImage', false, $ierror);
11031 + if($temp_get_imgs !== false)
11032 + {
11033 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11034 + {
11035 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
11036 + }
11037 + $added_images++;
11038 + $added_img_list[] = $temp_get_imgs;
11039 + $temp_get_img = $temp_get_imgs;
11040 + }
11041 + else
11042 + {
11043 + aiomatic_log_to_file('Failed to generate Midjourney image: ' . $ierror);
11044 + $temp_get_img = '';
11045 + }
11046 + }
11047 + }
11048 + else
11049 + {
11050 + aiomatic_log_to_file('Empty AI image query entered.');
11051 + }
11052 + }
11053 + elseif($enable_ai_images_article == '5')
11054 + {
11055 + if($orig_ai_command_image_article == '')
11056 + {
11057 + $orig_ai_command_image_article = $image_query;
11058 + }
11059 + if($orig_ai_command_image_article != '')
11060 + {
11061 + $ai_command_image = $orig_ai_command_image_article;
11062 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
11063 + {
11064 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
11065 + $ai_command_image = array_filter($ai_command_image);
11066 + if(count($ai_command_image) > 0)
11067 + {
11068 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
11069 + }
11070 + else
11071 + {
11072 + $ai_command_image = '';
11073 + }
11074 + }
11075 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
11076 + if(!empty($ai_command_image))
11077 + {
11078 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11079 + }
11080 + else
11081 + {
11082 + $ai_command_image = trim(strip_tags($post_title));
11083 + }
11084 + $ai_command_image = trim($ai_command_image);
11085 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
11086 + {
11087 + $txt_content = aiomatic_get_web_page($ai_command_image);
11088 + if ($txt_content !== FALSE)
11089 + {
11090 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11091 + $txt_content = array_filter($txt_content);
11092 + if(count($txt_content) > 0)
11093 + {
11094 + $txt_content = $txt_content[array_rand($txt_content)];
11095 + if(trim($txt_content) != '')
11096 + {
11097 + $ai_command_image = $txt_content;
11098 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
11099 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11100 + }
11101 + }
11102 + }
11103 + }
11104 + if(empty($ai_command_image))
11105 + {
11106 + aiomatic_log_to_file('Empty API image seed expression provided!');
11107 + }
11108 + else
11109 + {
11110 + if(aiomatic_strlen($ai_command_image) > 2000)
11111 + {
11112 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
11113 + }
11114 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11115 + {
11116 + $api_service = 'Replicate';
11117 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
11118 + }
11119 + if($image_size_article == '256x256')
11120 + {
11121 + $width = '512';
11122 + $height = '512';
11123 + }
11124 + elseif($image_size_article == '512x512')
11125 + {
11126 + $width = '512';
11127 + $height = '512';
11128 + }
11129 + elseif($image_size_article == '1024x1024')
11130 + {
11131 + $width = '1024';
11132 + $height = '1024';
11133 + }
11134 + elseif($image_size_article == '1792x1024')
11135 + {
11136 + $width = '1792';
11137 + $height = '1024';
11138 + }
11139 + elseif($image_size_article == '1024x1792')
11140 + {
11141 + $width = '1024';
11142 + $height = '1792';
11143 + }
11144 + else
11145 + {
11146 + $width = '512';
11147 + $height = '512';
11148 + }
11149 + $ierror = '';
11150 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Replicate image'));
11151 + $det = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'contentReplicateImage', false, $ierror);
11152 + if($temp_get_imgs !== false)
11153 + {
11154 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11155 + {
11156 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
11157 + }
11158 + $added_images++;
11159 + $added_img_list[] = $temp_get_imgs;
11160 + $temp_get_img = $temp_get_imgs;
11161 + }
11162 + else
11163 + {
11164 + aiomatic_log_to_file('Failed to generate Replicate image: ' . $ierror);
11165 + $temp_get_img = '';
11166 + }
11167 + }
11168 + }
11169 + else
11170 + {
11171 + aiomatic_log_to_file('Empty AI image query entered.');
11172 + }
11173 + }
11174 + elseif($enable_ai_images_article == '6')
11175 + {
11176 + if($orig_ai_command_image_article == '')
11177 + {
11178 + $orig_ai_command_image_article = $image_query;
11179 + }
11180 + if($orig_ai_command_image_article != '')
11181 + {
11182 + $ai_command_image = $orig_ai_command_image_article;
11183 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
11184 + {
11185 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
11186 + $ai_command_image = array_filter($ai_command_image);
11187 + if(count($ai_command_image) > 0)
11188 + {
11189 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
11190 + }
11191 + else
11192 + {
11193 + $ai_command_image = '';
11194 + }
11195 + }
11196 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
11197 + if(!empty($ai_command_image))
11198 + {
11199 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11200 + }
11201 + else
11202 + {
11203 + $ai_command_image = trim(strip_tags($post_title));
11204 + }
11205 + $ai_command_image = trim($ai_command_image);
11206 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
11207 + {
11208 + $txt_content = aiomatic_get_web_page($ai_command_image);
11209 + if ($txt_content !== FALSE)
11210 + {
11211 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11212 + $txt_content = array_filter($txt_content);
11213 + if(count($txt_content) > 0)
11214 + {
11215 + $txt_content = $txt_content[array_rand($txt_content)];
11216 + if(trim($txt_content) != '')
11217 + {
11218 + $ai_command_image = $txt_content;
11219 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
11220 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11221 + }
11222 + }
11223 + }
11224 + }
11225 + if(empty($ai_command_image))
11226 + {
11227 + aiomatic_log_to_file('Empty API image seed expression provided!');
11228 + }
11229 + else
11230 + {
11231 + if(aiomatic_strlen($ai_command_image) > 2000)
11232 + {
11233 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
11234 + }
11235 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11236 + {
11237 + $api_service = 'Ideogram';
11238 + aiomatic_log_to_file('Calling ' . $api_service . ' for image: ' . $ai_command_image);
11239 + }
11240 + if($image_size_article == '256x256')
11241 + {
11242 + $width = '512';
11243 + $height = '512';
11244 + }
11245 + elseif($image_size_article == '512x512')
11246 + {
11247 + $width = '512';
11248 + $height = '512';
11249 + }
11250 + elseif($image_size_article == '1024x1024')
11251 + {
11252 + $width = '1024';
11253 + $height = '1024';
11254 + }
11255 + elseif($image_size_article == '1792x1024')
11256 + {
11257 + $width = '1792';
11258 + $height = '1024';
11259 + }
11260 + elseif($image_size_article == '1024x1792')
11261 + {
11262 + $width = '1024';
11263 + $height = '1792';
11264 + }
11265 + else
11266 + {
11267 + $width = '512';
11268 + $height = '512';
11269 + }
11270 + $ierror = '';
11271 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Ideogram image'));
11272 + $det = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'contentIdeogramImage', false, $ierror);
11273 + if($temp_get_imgs !== false)
11274 + {
11275 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11276 + {
11277 + aiomatic_log_to_file('AI generated image returned: ' . $temp_get_imgs[1]);
11278 + }
11279 + $added_images++;
11280 + $added_img_list[] = $temp_get_imgs;
11281 + $temp_get_img = $temp_get_imgs;
11282 + }
11283 + else
11284 + {
11285 + aiomatic_log_to_file('Failed to generate Ideogram image: ' . $ierror);
11286 + $temp_get_img = '';
11287 + }
11288 + }
11289 + }
11290 + else
11291 + {
11292 + aiomatic_log_to_file('Empty AI image query entered.');
11293 + }
11294 + }
11295 + elseif($enable_ai_images_article == '3')
11296 + {
11297 + if ($image_url != '')
11298 + {
11299 + $replacement = str_replace(array('[', ']'), '', $query_words);
11300 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
11301 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
11302 + if(isset($matches[2]))
11303 + {
11304 + $chance = trim($matches[2], '[]');
11305 + }
11306 + else
11307 + {
11308 + $chance = '';
11309 + }
11310 + $arv = array();
11311 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
11312 + return $my_img;
11313 + }, $image_url_temp);
11314 + $img_rulx = $spintax->Parse(trim($image_url_temp));
11315 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
11316 + if($selected_img === false)
11317 + {
11318 + $img_rulx = explode(',', $img_rulx);
11319 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
11320 + if($img_rulx != '')
11321 + {
11322 + $added_images++;
11323 + $added_img_list[] = $img_rulx;
11324 + $temp_get_img = $img_rulx;
11325 + }
11326 + }
11327 + else
11328 + {
11329 + $added_images++;
11330 + $added_img_list[] = $selected_img;
11331 + $temp_get_img = $selected_img;
11332 + }
11333 + }
11334 + }
11335 + elseif(count($images_arr) > 0)
11336 + {
11337 + $first_el = array_shift($images_arr);
11338 + $first_el = aiomatic_replaceSynergyShortcodes($first_el);
11339 + $added_images++;
11340 + $added_img_list[] = $first_el;
11341 + $temp_get_img = $first_el;
11342 + }
11343 + else
11344 + {
11345 + $query_words = '';
11346 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
11347 + {
11348 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
11349 + {
11350 + try
11351 + {
11352 + if(!class_exists('TextRazor'))
11353 + {
11354 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
11355 + }
11356 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
11357 + $textrazor = new TextRazor();
11358 + $textrazor->addExtractor('entities');
11359 + $response = $textrazor->analyze($image_query);
11360 + if (isset($response['response']['entities']))
11361 + {
11362 + foreach ($response['response']['entities'] as $entity)
11363 + {
11364 + $query_words = '';
11365 + if(isset($entity['entityEnglishId']))
11366 + {
11367 + $query_words = $entity['entityEnglishId'];
11368 + }
11369 + else
11370 + {
11371 + $query_words = $entity['entityId'];
11372 + }
11373 + if($query_words != '')
11374 + {
11375 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free image'));
11376 + $attached_id = '';
11377 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
11378 + if(!empty($attached_id))
11379 + {
11380 + $additional_attachments[] = $attached_id;
11381 + }
11382 + if(!empty($z_img))
11383 + {
11384 + $added_images++;
11385 + $added_img_list[] = $z_img;
11386 + $temp_get_img = $z_img;
11387 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11388 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
11389 + }
11390 + break;
11391 + }
11392 + }
11393 + }
11394 + }
11395 + }
11396 + catch(Exception $e)
11397 + {
11398 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11399 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
11400 + }
11401 + }
11402 + }
11403 + }
11404 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
11405 + {
11406 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
11407 + {
11408 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
11409 + {
11410 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
11411 + }
11412 + else
11413 + {
11414 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
11415 + }
11416 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
11417 + {
11418 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
11419 + }
11420 + else
11421 + {
11422 + $keyword_assistant_id = '';
11423 + }
11424 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
11425 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
11426 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
11427 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
11428 + {
11429 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
11430 + $title_ai_command = array_filter($title_ai_command);
11431 + if(count($title_ai_command) > 0)
11432 + {
11433 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
11434 + }
11435 + else
11436 + {
11437 + $title_ai_command = '';
11438 + }
11439 + }
11440 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
11441 + if(!empty($title_ai_command))
11442 + {
11443 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11444 + }
11445 + $title_ai_command = trim($title_ai_command);
11446 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
11447 + {
11448 + $txt_content = aiomatic_get_web_page($title_ai_command);
11449 + if ($txt_content !== FALSE)
11450 + {
11451 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11452 + $txt_content = array_filter($txt_content);
11453 + if(count($txt_content) > 0)
11454 + {
11455 + $txt_content = $txt_content[array_rand($txt_content)];
11456 + if(trim($txt_content) != '')
11457 + {
11458 + $title_ai_command = $txt_content;
11459 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
11460 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11461 + }
11462 + }
11463 + }
11464 + }
11465 + if(empty($title_ai_command))
11466 + {
11467 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
11468 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
11469 + }
11470 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
11471 + {
11472 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
11473 + }
11474 + $title_ai_command = trim($title_ai_command);
11475 + if(empty($title_ai_command))
11476 + {
11477 + aiomatic_log_to_file('Empty API title seed expression provided(5)! ' . print_r($title_ai_command, true));
11478 + }
11479 + else
11480 + {
11481 + $query_token_count = count(aiomatic_encode($title_ai_command));
11482 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
11483 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
11484 + {
11485 + $string_len = aiomatic_strlen($title_ai_command);
11486 + $string_len = $string_len / 2;
11487 + $string_len = intval(0 - $string_len);
11488 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
11489 + $title_ai_command = trim($title_ai_command);
11490 + $query_token_count = count(aiomatic_encode($title_ai_command));
11491 + $available_tokens = $max_tokens - $query_token_count;
11492 + }
11493 + $aierror = '';
11494 + $finish_reason = '';
11495 + if(!empty($global_prepend))
11496 + {
11497 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
11498 + }
11499 + if(!empty($global_append))
11500 + {
11501 + $title_ai_command = $title_ai_command . ' ' . $global_append;
11502 + }
11503 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11504 + {
11505 + $api_service = aiomatic_get_api_service($token, $kw_model);
11506 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
11507 + }
11508 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image search keywords'));
11509 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
11510 + if($generated_text === false)
11511 + {
11512 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
11513 + $ai_title = '';
11514 + }
11515 + else
11516 + {
11517 + $ai_title = trim(trim($generated_text), '.');
11518 + $ai_titles = explode(',', $ai_title);
11519 + foreach($ai_titles as $query_words)
11520 + {
11521 + $attached_id = '';
11522 + $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);
11523 + if(!empty($attached_id))
11524 + {
11525 + $additional_attachments[] = $attached_id;
11526 + }
11527 + if(!empty($z_img))
11528 + {
11529 + $added_images++;
11530 + $added_img_list[] = $z_img;
11531 + $temp_get_img = $z_img;
11532 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11533 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
11534 + }
11535 + break;
11536 + }
11537 + }
11538 + }
11539 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11540 + {
11541 + $api_service = aiomatic_get_api_service($token, $kw_model);
11542 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
11543 + }
11544 + }
11545 + }
11546 + }
11547 + if(empty($temp_get_img))
11548 + {
11549 + $keyword_class = new Aiomatic_keywords();
11550 + $query_words = $keyword_class->keywords($image_query, 2);
11551 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free images'));
11552 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
11553 + if(!empty($attached_id))
11554 + {
11555 + $additional_attachments[] = $attached_id;
11556 + }
11557 + if($temp_get_img == '' || $temp_get_img === false)
11558 + {
11559 + $query_words = $keyword_class->keywords($image_query, 1);
11560 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, false, $raw_img_list, array(), $full_result_list);
11561 + if(!empty($attached_id))
11562 + {
11563 + $additional_attachments[] = $attached_id;
11564 + }
11565 + if($temp_get_img == '' || $temp_get_img === false)
11566 + {
11567 + $temp_get_img = '';
11568 + }
11569 + else
11570 + {
11571 + if(!in_array($temp_get_img, $added_img_list))
11572 + {
11573 + $added_images++;
11574 + $added_img_list[] = $temp_get_img;
11575 + }
11576 + else
11577 + {
11578 + $temp_get_img = '';
11579 + }
11580 + }
11581 + }
11582 + else
11583 + {
11584 + if(!in_array($temp_get_img, $added_img_list))
11585 + {
11586 + $added_images++;
11587 + $added_img_list[] = $temp_get_img;
11588 + }
11589 + else
11590 + {
11591 + $temp_get_img = '';
11592 + }
11593 + }
11594 + }
11595 + }
11596 + if($temp_get_img != '')
11597 + {
11598 + $add_my_image = '<br/><img class="ximage_class" src="' . $temp_get_img . '" alt="' . esc_attr($query_words) . '"><br/>';
11599 + }
11600 + }
11601 + if($heading_val == '')
11602 + {
11603 + if($add_my_image == '')
11604 + {
11605 + $add_my_image = ' ';
11606 + }
11607 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
11608 + {
11609 + $new_post_content .= $add_my_image . trim(nl2br($aiwriter));
11610 + }
11611 + else
11612 + {
11613 + $new_post_content .= $add_my_image . trim($aiwriter);
11614 + }
11615 + }
11616 + else
11617 + {
11618 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
11619 + {
11620 + $new_post_content .= $add_my_image . $heading_val . ' ' . trim(nl2br($aiwriter)) . '</span>';
11621 + }
11622 + else
11623 + {
11624 + $new_post_content .= $add_my_image . $heading_val . ' ' . trim($aiwriter) . '</span>';
11625 + }
11626 + }
11627 + if($enable_ai_images == '0')
11628 + {
11629 + sleep(1);
11630 + }
11631 + $cnt++;
11632 + }
11633 + }
11634 + if($strip_title == '1')
11635 + {
11636 + $new_post_content = str_replace($post_title, '', $new_post_content);
11637 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
11638 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
11639 + }
11640 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
11641 + {
11642 + require_once(dirname(__FILE__) . "/res/swear.php");
11643 + $new_post_content = aimogenpro_filterwords($new_post_content);
11644 + }
11645 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
11646 + $continue = false;
11647 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
11648 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
11649 + foreach ($banned_list as $banned_word) {
11650 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
11651 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11652 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
11653 + }
11654 + $continue = true;
11655 + break;
11656 + }
11657 + if (stripos($new_post_title, trim($banned_word)) !== FALSE) {
11658 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11659 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
11660 + }
11661 + $continue = true;
11662 + break;
11663 + }
11664 + }
11665 + if ($continue === true) {
11666 + unset($post_title_lines[$current_index]);
11667 + continue;
11668 + }
11669 + }
11670 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
11671 + {
11672 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
11673 + {
11674 + $continue = true;
11675 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
11676 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
11677 + foreach ($required_list as $required_word) {
11678 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($new_post_title, trim($required_word)) !== FALSE) {
11679 + $continue = false;
11680 + break;
11681 + }
11682 + }
11683 + if ($continue === true) {
11684 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11685 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content doesn\'t contain global required words.');
11686 + }
11687 + unset($post_title_lines[$current_index]);
11688 + continue;
11689 + }
11690 + }
11691 + else
11692 + {
11693 + $continue = false;
11694 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
11695 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
11696 + foreach ($required_list as $required_word) {
11697 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($new_post_title, trim($required_word)) === FALSE) {
11698 + $continue = true;
11699 + break;
11700 + }
11701 + }
11702 + if ($continue === true) {
11703 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
11704 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content doesn\'t contain global required words.');
11705 + }
11706 + unset($post_title_lines[$current_index]);
11707 + continue;
11708 + }
11709 + }
11710 + }
11711 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
11712 + {
11713 + $skip_spin = '1';
11714 + }
11715 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
11716 + if($arr[0] != $new_post_title)
11717 + {
11718 + $new_post_title = $arr[0];
11719 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
11720 + {
11721 + $posts = get_posts(
11722 + array(
11723 + 'post_type' => $post_type,
11724 + 'title' => html_entity_decode($new_post_title),
11725 + 'post_status' => 'all',
11726 + 'numberposts' => 1,
11727 + 'update_post_term_cache' => false,
11728 + 'update_post_meta_cache' => false,
11729 + 'orderby' => 'post_date ID',
11730 + 'order' => 'ASC',
11731 + )
11732 + );
11733 + if ( ! empty( $posts ) ) {
11734 + $zap = $posts[0];
11735 + } else {
11736 + $zap = null;
11737 + }
11738 + if($zap !== null)
11739 + {
11740 + if($overwrite_existing)
11741 + {
11742 + $update_post_id = $zap->ID;
11743 + }
11744 + else
11745 + {
11746 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $new_post_title);
11747 + unset($post_title_lines[$current_index]);
11748 + continue;
11749 + }
11750 + }
11751 + }
11752 + }
11753 + $new_post_content = $arr[1];
11754 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
11755 + {
11756 + $already_spinned = '1';
11757 + }
11758 + if ($auto_categories == 'content') {
11759 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
11760 + $extra_categories = implode(',', $extra_categories);
11761 + }
11762 + elseif ($auto_categories == 'title') {
11763 + $extra_categories = aiomatic_extractKeyWords($new_post_title);
11764 + $extra_categories = implode(',', $extra_categories);
11765 + }
11766 + elseif ($auto_categories == 'both') {
11767 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
11768 + $extra_categories = implode(',', $extra_categories);
11769 + $extra_categories2 = aiomatic_extractKeyWords($new_post_title);
11770 + $extra_categories2 = implode(',', $extra_categories2);
11771 + if($extra_categories2 != '')
11772 + {
11773 + $extra_categories .= ',' . $extra_categories2;
11774 + }
11775 + }
11776 + elseif ($auto_categories == 'ai')
11777 + {
11778 + $category_ai_command = $orig_ai_command_category;
11779 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
11780 + {
11781 + $category_ai_command = preg_split('/\r\n|\r|\n/', $category_ai_command);
11782 + $category_ai_command = array_filter($category_ai_command);
11783 + if(count($category_ai_command) > 0)
11784 + {
11785 + $category_ai_command = $category_ai_command[array_rand($category_ai_command)];
11786 + }
11787 + else
11788 + {
11789 + $category_ai_command = '';
11790 + }
11791 + }
11792 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
11793 + if(!empty($category_ai_command))
11794 + {
11795 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11796 + $category_ai_command = aiomatic_replacetopics($category_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
11797 + }
11798 + else
11799 + {
11800 + $category_ai_command = trim(strip_tags('Write a comma separated list of categories, for the post title: %%post_title%%'));
11801 + }
11802 + $category_ai_command = trim($category_ai_command);
11803 + if (filter_var($category_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($category_ai_command, '.txt'))
11804 + {
11805 + $txt_content = aiomatic_get_web_page($category_ai_command);
11806 + if ($txt_content !== FALSE)
11807 + {
11808 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11809 + $txt_content = array_filter($txt_content);
11810 + if(count($txt_content) > 0)
11811 + {
11812 + $txt_content = $txt_content[array_rand($txt_content)];
11813 + if(trim($txt_content) != '')
11814 + {
11815 + $category_ai_command = $txt_content;
11816 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
11817 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11818 + $category_ai_command = aiomatic_replacetopics($category_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
11819 + }
11820 + }
11821 + }
11822 + }
11823 + if(empty($category_ai_command))
11824 + {
11825 + aiomatic_log_to_file('Empty API post category seed expression provided!');
11826 + }
11827 + else
11828 + {
11829 + if(aiomatic_strlen($category_ai_command) > $max_seed_tokens * 4)
11830 + {
11831 + $category_ai_command = aiomatic_substr($category_ai_command, 0, (0 - ($max_seed_tokens * 4)));
11832 + }
11833 + $category_ai_command = trim($category_ai_command);
11834 + if(empty($category_ai_command))
11835 + {
11836 + aiomatic_log_to_file('Empty API category seed expression provided! ' . print_r($category_ai_command, true));
11837 + break;
11838 + }
11839 + $query_token_count = count(aiomatic_encode($category_ai_command));
11840 + $available_tokens = aiomatic_compute_available_tokens($category_model, $max_tokens, $category_ai_command, $query_token_count);
11841 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
11842 + {
11843 + $string_len = aiomatic_strlen($category_ai_command);
11844 + $string_len = $string_len / 2;
11845 + $string_len = intval(0 - $string_len);
11846 + $category_ai_command = aiomatic_substr($category_ai_command, 0, $string_len);
11847 + $category_ai_command = trim($category_ai_command);
11848 + if(empty($category_ai_command))
11849 + {
11850 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($category_ai_command, true));
11851 + break;
11852 + }
11853 + $query_token_count = count(aiomatic_encode($category_ai_command));
11854 + $available_tokens = $max_tokens - $query_token_count;
11855 + }
11856 + $aierror = '';
11857 + $finish_reason = '';
11858 + if(!empty($global_prepend))
11859 + {
11860 + $category_ai_command = $global_prepend . ' ' . $category_ai_command;
11861 + }
11862 + if(!empty($global_append))
11863 + {
11864 + $category_ai_command = $category_ai_command . ' ' . $global_append;
11865 + }
11866 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11867 + {
11868 + $api_service = aiomatic_get_api_service($token, $category_model);
11869 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $category_model . ')(' . $available_tokens . ') for category generator: ' . $category_ai_command);
11870 + }
11871 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating categories'));
11872 + $generated_text = aiomatic_generate_text($token, $category_model, $category_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
11873 + if($generated_text === false)
11874 + {
11875 + aiomatic_log_to_file('Category generator error: ' . $aierror);
11876 + break;
11877 + }
11878 + else
11879 + {
11880 + $extra_categories = $generated_text;
11881 + }
11882 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
11883 + {
11884 + $api_service = aiomatic_get_api_service($token, $category_model);
11885 + aiomatic_log_to_file('Successfully got API category result from ' . $api_service . ': ' . $generated_text);
11886 + }
11887 + }
11888 + }
11889 + else
11890 + {
11891 + $extra_categories = '';
11892 + }
11893 + $my_post['extra_categories'] = $extra_categories;
11894 +
11895 + $item_tags = aiomatic_extractKeyWords($new_post_content, 3);
11896 + $item_tags = implode(',', $item_tags);
11897 + $title_tags = aiomatic_extractKeyWords($new_post_title, 3);
11898 + $title_tags = implode(',', $title_tags);
11899 + $item_create_tag_sp = $spintax->Parse($item_create_tag);
11900 + if ($can_create_tag == 'content') {
11901 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $item_tags;
11902 + $my_post['extra_tags'] = $item_tags;
11903 + } else if ($can_create_tag == 'title') {
11904 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $title_tags;
11905 + $my_post['extra_tags'] = $title_tags;
11906 + } else if ($can_create_tag == 'both') {
11907 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
11908 + $my_post['extra_tags'] = ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
11909 + } else if ($can_create_tag == 'ai') {
11910 + $ai_tags = '';
11911 + $tag_ai_command = $orig_ai_command_tag;
11912 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
11913 + {
11914 + $tag_ai_command = preg_split('/\r\n|\r|\n/', $tag_ai_command);
11915 + $tag_ai_command = array_filter($tag_ai_command);
11916 + if(count($tag_ai_command) > 0)
11917 + {
11918 + $tag_ai_command = $tag_ai_command[array_rand($tag_ai_command)];
11919 + }
11920 + else
11921 + {
11922 + $tag_ai_command = '';
11923 + }
11924 + }
11925 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
11926 + if(!empty($tag_ai_command))
11927 + {
11928 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11929 + $tag_ai_command = aiomatic_replacetopics($tag_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
11930 + }
11931 + else
11932 + {
11933 + $tag_ai_command = trim(strip_tags('Write a comma separated list of tags, for the post title: %%post_title%%'));
11934 + }
11935 + $tag_ai_command = trim($tag_ai_command);
11936 + if (filter_var($tag_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($tag_ai_command, '.txt'))
11937 + {
11938 + $txt_content = aiomatic_get_web_page($tag_ai_command);
11939 + if ($txt_content !== FALSE)
11940 + {
11941 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
11942 + $txt_content = array_filter($txt_content);
11943 + if(count($txt_content) > 0)
11944 + {
11945 + $txt_content = $txt_content[array_rand($txt_content)];
11946 + if(trim($txt_content) != '')
11947 + {
11948 + $tag_ai_command = $txt_content;
11949 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
11950 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
11951 + $tag_ai_command = aiomatic_replacetopics($tag_ai_command, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
11952 + }
11953 + }
11954 + }
11955 + }
11956 + if(empty($tag_ai_command))
11957 + {
11958 + aiomatic_log_to_file('Empty API post tag seed expression provided!');
11959 + }
11960 + else
11961 + {
11962 + if(aiomatic_strlen($tag_ai_command) > $max_seed_tokens * 4)
11963 + {
11964 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, (0 - ($max_seed_tokens * 4)));
11965 + }
11966 + $tag_ai_command = trim($tag_ai_command);
11967 + if(empty($tag_ai_command))
11968 + {
11969 + aiomatic_log_to_file('Empty API tag seed expression provided! ' . print_r($tag_ai_command, true));
11970 + break;
11971 + }
11972 + $query_token_count = count(aiomatic_encode($tag_ai_command));
11973 + $available_tokens = aiomatic_compute_available_tokens($tag_model, $max_tokens, $tag_ai_command, $query_token_count);
11974 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
11975 + {
11976 + $string_len = aiomatic_strlen($tag_ai_command);
11977 + $string_len = $string_len / 2;
11978 + $string_len = intval(0 - $string_len);
11979 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, $string_len);
11980 + $tag_ai_command = trim($tag_ai_command);
11981 + if(empty($tag_ai_command))
11982 + {
11983 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($tag_ai_command, true));
11984 + break;
11985 + }
11986 + $query_token_count = count(aiomatic_encode($tag_ai_command));
11987 + $available_tokens = $max_tokens - $query_token_count;
11988 + }
11989 + $aierror = '';
11990 + $finish_reason = '';
11991 + if(!empty($global_prepend))
11992 + {
11993 + $tag_ai_command = $global_prepend . ' ' . $tag_ai_command;
11994 + }
11995 + if(!empty($global_append))
11996 + {
11997 + $tag_ai_command = $tag_ai_command . ' ' . $global_append;
11998 + }
11999 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
12000 + {
12001 + $api_service = aiomatic_get_api_service($token, $tag_model);
12002 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $tag_model . ')(' . $available_tokens . ') for tag generator: ' . $tag_ai_command);
12003 + }
12004 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
12005 + $generated_text = aiomatic_generate_text($token, $tag_model, $tag_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
12006 + if($generated_text === false)
12007 + {
12008 + aiomatic_log_to_file('Tag generator error: ' . $aierror);
12009 + break;
12010 + }
12011 + else
12012 + {
12013 + $ai_tags = $generated_text;
12014 + }
12015 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
12016 + {
12017 + $api_service = aiomatic_get_api_service($token, $tag_model);
12018 + aiomatic_log_to_file('Successfully got API tag result from ' . $api_service . ': ' . $generated_text);
12019 + }
12020 + }
12021 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $ai_tags;
12022 + $my_post['extra_tags'] = $ai_tags;
12023 + } else {
12024 + $post_the_tags = $item_create_tag_sp;
12025 + $my_post['extra_tags'] = '';
12026 + }
12027 + $my_post['tags_input'] = $post_the_tags;
12028 + $new_post_content = html_entity_decode($new_post_content);
12029 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
12030 + if ($videos == '1')
12031 + {
12032 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
12033 + {
12034 + $social_list = $aiomatic_Main_Settings['social_list'];
12035 + }
12036 + else
12037 + {
12038 + $social_list = array();
12039 + }
12040 + $new_vid = aiomatic_get_video($new_post_title, '', $social_list);
12041 + if($new_vid !== false)
12042 + {
12043 + $new_post_content .= $new_vid;
12044 + }
12045 + }
12046 + if ($strip_by_regex !== '')
12047 + {
12048 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
12049 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
12050 + $xcnt = 0;
12051 + foreach($xstrip_by_regex as $sbr)
12052 + {
12053 + if(isset($xreplace_regex[$xcnt]))
12054 + {
12055 + $repreg = $xreplace_regex[$xcnt];
12056 + }
12057 + else
12058 + {
12059 + $repreg = '';
12060 + }
12061 + $xcnt++;
12062 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
12063 + if($temp_cont !== NULL)
12064 + {
12065 + $new_post_content = $temp_cont;
12066 + }
12067 + }
12068 + }
12069 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
12070 + $post_prepender = aiomatic_replacetopics($post_prepender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
12071 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
12072 + $post_appender = aiomatic_replacetopics($post_appender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
12073 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
12074 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
12075 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
12076 + {
12077 + if($new_post_content === strip_tags($new_post_content))
12078 + {
12079 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
12080 + {
12081 + $new_post_content = nl2br($new_post_content);
12082 + }
12083 + }
12084 + }
12085 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
12086 + {
12087 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
12088 + {
12089 + if(!isset($xchars))
12090 + {
12091 + $xchars = array();
12092 + }
12093 + $rand_percentage = wp_rand(10, 20);
12094 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
12095 + }
12096 + }
12097 + $zlang = 'en_US';
12098 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
12099 + $zlang = $aiomatic_Main_Settings['kw_lang'];
12100 + }
12101 + $rel_search = array('post_title', 'post_content');
12102 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
12103 + $rel_search = $aiomatic_Main_Settings['rel_search'];
12104 + }
12105 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
12106 + {
12107 + try
12108 + {
12109 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
12110 + }
12111 + catch(Exception $ex)
12112 + {
12113 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12114 + aiomatic_log_to_file('Failed to add inbound links to content: ' . $ex->getMessage());
12115 + }
12116 + }
12117 + }
12118 + if($ret_content == 1)
12119 + {
12120 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12121 + aiomatic_log_to_file('Returning AI generated content, finished.');
12122 + }
12123 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
12124 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
12125 + }
12126 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
12127 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
12128 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
12129 + {
12130 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
12131 + }
12132 + else
12133 + {
12134 + $my_post['post_content'] = $trunchi_content;
12135 + }
12136 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
12137 + {
12138 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
12139 + {
12140 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
12141 + }
12142 + }
12143 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
12144 + $my_post['aiomatic_source_title'] = $old_title;
12145 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
12146 + $my_post['aiomatic_post_format'] = $post_format;
12147 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
12148 + {
12149 + if(is_array($default_category))
12150 + {
12151 + $cextra = '';
12152 + foreach($default_category as $dc)
12153 + {
12154 + $cextra .= ',' . get_cat_name($dc);
12155 + }
12156 + $extra_categories_temp = trim( $cextra . ',' . $extra_categories, ',');
12157 + }
12158 + else
12159 + {
12160 + $extra_categories_temp = trim(get_cat_name($default_category) . ',' .$extra_categories, ',');
12161 + }
12162 + }
12163 + else
12164 + {
12165 + $extra_categories_temp = $extra_categories;
12166 + }
12167 + $block_arr = array();
12168 + $custom_arr = array();
12169 + if($custom_fields != '')
12170 + {
12171 + if(stristr($custom_fields, '=>') != false)
12172 + {
12173 + $rule_arr = explode(',', trim($custom_fields));
12174 + foreach($rule_arr as $rule)
12175 + {
12176 + $my_args = explode('=>', trim($rule));
12177 + if(isset($my_args[1]))
12178 + {
12179 + if ( isset( $shortcode_tags['aicontent'] ) ) {
12180 + $aicontent_handler = $shortcode_tags['aicontent'];
12181 + unset( $shortcode_tags['aicontent'] );
12182 + } else {
12183 + $aicontent_handler = null;
12184 + }
12185 + $my_args[1] = do_shortcode($my_args[1]);
12186 + $my_args[0] = do_shortcode($my_args[0]);
12187 + if ( $aicontent_handler ) {
12188 + $shortcode_tags['aicontent'] = $aicontent_handler;
12189 + }
12190 + $custom_field_content = trim($my_args[1]);
12191 + $custom_field_content = aiomatic_replaceAIPostShortcodes($custom_field_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
12192 + $custom_field_content = aiomatic_replacetopics($custom_field_content, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
12193 + $custom_field_content = aiomatic_replaceSynergyShortcodes($custom_field_content);
12194 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
12195 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
12196 + if(stristr($my_args[1], 'serialize_') !== false)
12197 + {
12198 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
12199 + }
12200 + else
12201 + {
12202 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
12203 + {
12204 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
12205 + if(isset($cfm[2][0]))
12206 + {
12207 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
12208 + {
12209 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
12210 + }
12211 + else
12212 + {
12213 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
12214 + }
12215 + }
12216 + else
12217 + {
12218 + $custom_arr[trim($my_args[0])] = $custom_field_content;
12219 + }
12220 + }
12221 + else
12222 + {
12223 + $custom_arr[trim($my_args[0])] = $custom_field_content;
12224 + }
12225 + }
12226 + }
12227 + }
12228 + }
12229 + }
12230 + $block_arr = array();
12231 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $extra_categories_temp, 'aiomatic_post_tags' => $post_the_tags));
12232 + $custom_tax_arr = array();
12233 + if($custom_tax != '')
12234 + {
12235 + if(stristr($custom_tax, '=>') != false)
12236 + {
12237 + $rule_arr = explode(';', trim($custom_tax));
12238 + foreach($rule_arr as $rule)
12239 + {
12240 + $my_args = explode('=>', trim($rule));
12241 + if(isset($my_args[1]))
12242 + {
12243 + $custom_tax_content = trim($my_args[1]);
12244 + $custom_tax_content = aiomatic_replaceAIPostShortcodes($custom_tax_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
12245 + $custom_tax_content = aiomatic_replacetopics($custom_tax_content, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
12246 + $custom_tax_content = aiomatic_replaceSynergyShortcodes($custom_tax_content);
12247 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
12248 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
12249 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
12250 + {
12251 + if(isset($custom_arr['_product_attributes']))
12252 + {
12253 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
12254 + 'name' => trim($my_args[0]),
12255 + 'value' => $custom_tax_content,
12256 + 'is_visible' => '1',
12257 + 'is_taxonomy' => '1'
12258 + )));
12259 + }
12260 + else
12261 + {
12262 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
12263 + 'name' => trim($my_args[0]),
12264 + 'value' => $custom_tax_content,
12265 + 'is_visible' => '1',
12266 + 'is_taxonomy' => '1'
12267 + ));
12268 + }
12269 + }
12270 + if(isset($custom_tax_arr[trim($my_args[0])]))
12271 + {
12272 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
12273 + }
12274 + else
12275 + {
12276 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
12277 + }
12278 + }
12279 + }
12280 + }
12281 + }
12282 + if(count($custom_tax_arr) > 0)
12283 + {
12284 + $my_post['taxo_input'] = $custom_tax_arr;
12285 + }
12286 + $my_post['meta_input'] = $custom_arr;
12287 + if($parent_id != '')
12288 + {
12289 + $my_post['post_parent'] = intval($parent_id);
12290 + }
12291 + if ($enable_pingback == '1') {
12292 + $my_post['ping_status'] = 'open';
12293 + } else {
12294 + $my_post['ping_status'] = 'closed';
12295 + }
12296 + if($min_time != '' && $max_time != '')
12297 + {
12298 + $t1 = strtotime($min_time);
12299 + $t2 = strtotime($max_time);
12300 + if($t1 != false && $t2 != false)
12301 + {
12302 + $int = wp_rand($t1, $t2);
12303 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
12304 + }
12305 + }
12306 + elseif($min_time != '')
12307 + {
12308 + $t1 = strtotime($min_time);
12309 + if($t1 != false)
12310 + {
12311 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
12312 + }
12313 + }
12314 + elseif($max_time != '')
12315 + {
12316 + $t1 = strtotime($max_time);
12317 + if($t1 != false)
12318 + {
12319 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
12320 + }
12321 + }
12322 + $count++;
12323 + }
12324 + else
12325 + {
12326 + aiomatic_log_to_file('Unknown posting mode submitted: ' . $posting_mode . '!');
12327 + if($auto == 1)
12328 + {
12329 + aiomatic_clearFromList($param, $type);
12330 + }
12331 + return 'fail';
12332 + }
12333 + if($update_post_id != '')
12334 + {
12335 + $my_post['ID'] = $update_post_id;
12336 + }
12337 + remove_filter('content_save_pre', 'wp_filter_post_kses');
12338 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
12339 + try
12340 + {
12341 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
12342 + $post_id = wp_insert_post($my_post, true);
12343 + if (is_wp_error($post_id))
12344 + {
12345 + aiomatic_log_to_file('Trying publishing again');
12346 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
12347 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
12348 + }
12349 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
12350 + $my_post['post_content'] = mb_convert_encoding($my_post['post_content'], 'UTF-8', 'UTF-8');
12351 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
12352 + $post_id = wp_insert_post($my_post, true);
12353 + }
12354 + }
12355 + catch(Exception $e)
12356 + {
12357 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
12358 + }
12359 + add_filter('content_save_pre', 'wp_filter_post_kses');
12360 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
12361 + if (!is_wp_error($post_id)) {
12362 + if($post_id === 0)
12363 + {
12364 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
12365 + continue;
12366 + }
12367 + $posts_inserted++;
12368 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
12369 + {
12370 + pll_set_post_language($post_id, $wpml_lang);
12371 + }
12372 + if(!empty($additional_attachments))
12373 + {
12374 + foreach($additional_attachments as $add_att)
12375 + {
12376 + wp_update_post([
12377 + 'ID' => $add_att,
12378 + 'post_parent' => $post_id,
12379 + ]);
12380 + }
12381 + }
12382 + $default_categories = array();
12383 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
12384 + {
12385 + $default_categories = wp_get_post_categories($post_id);
12386 + }
12387 + if(isset($my_post['taxo_input']))
12388 + {
12389 + foreach($my_post['taxo_input'] as $taxn => $taxval)
12390 + {
12391 + $taxn = trim($taxn);
12392 + $taxval = trim($taxval);
12393 + if(is_taxonomy_hierarchical($taxn))
12394 + {
12395 + $taxval = array_map('trim', explode(',', $taxval));
12396 + for($ii = 0; $ii < count($taxval); $ii++)
12397 + {
12398 + if(!is_numeric($taxval[$ii]))
12399 + {
12400 + $term_ids = [];
12401 + $parent_id = 0;
12402 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
12403 + foreach($hierarchy_parts as $hp)
12404 + {
12405 + if(!is_numeric($hp))
12406 + {
12407 + $xtermid = get_term_by('name', $hp, $taxn);
12408 + if($xtermid !== false)
12409 + {
12410 + $parent_id = $xtermid->term_id;
12411 + if(!is_numeric($taxval[$ii]))
12412 + {
12413 + $taxval[$ii] = intval($xtermid->term_id);
12414 + }
12415 + else
12416 + {
12417 + $taxval[] = intval($xtermid->term_id);
12418 + }
12419 + }
12420 + else
12421 + {
12422 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
12423 + $xtermid = get_term_by('name', $hp, $taxn);
12424 + if($xtermid !== false)
12425 + {
12426 + $parent_id = $xtermid->term_id;
12427 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
12428 + {
12429 + pll_set_term_language($xtermid->term_id, $wpml_lang);
12430 + }
12431 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
12432 + {
12433 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
12434 + $pars['element_id'] = $xtermid->term_id;
12435 + $pars['element_type'] = $wpml_element_type;
12436 + $pars['language_code'] = $wpml_lang;
12437 + $pars['trid'] = FALSE;
12438 + $pars['source_language_code'] = NULL;
12439 + do_action('wpml_set_element_language_details', $pars);
12440 + }
12441 + if(!is_numeric($taxval[$ii]))
12442 + {
12443 + $taxval[$ii] = intval($xtermid->term_id);
12444 + }
12445 + else
12446 + {
12447 + $taxval[] = intval($xtermid->term_id);
12448 + }
12449 + }
12450 + }
12451 + }
12452 + else
12453 + {
12454 + if(!is_numeric($taxval[$ii]))
12455 + {
12456 + $taxval[$ii] = intval($hp);
12457 + }
12458 + else
12459 + {
12460 + $taxval[] = intval($hp);
12461 + }
12462 + }
12463 + }
12464 + }
12465 + }
12466 + wp_set_post_terms($post_id, $taxval, $taxn, true);
12467 + }
12468 + else
12469 + {
12470 + $jterms = array_map('trim', explode(',', $taxval));
12471 + wp_set_post_terms($post_id, $jterms, $taxn, true);
12472 + }
12473 + }
12474 + }
12475 + if (isset($my_post['aiomatic_post_format']) && $my_post['aiomatic_post_format'] != '' && $my_post['aiomatic_post_format'] != 'post-format-standard') {
12476 + wp_set_post_terms($post_id, $my_post['aiomatic_post_format'], 'post_format', true);
12477 + }
12478 + $featured_path = '';
12479 + $get_img = $my_post['aiomatic_post_image'];
12480 + if ($get_img != '') {
12481 + if($my_post['aiomatic_local_image'] == '1')
12482 + {
12483 + $local_get_img = $get_img[0];
12484 + if (!aiomatic_assign_featured_image_path($local_get_img, $post_id)) {
12485 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12486 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed for ' . $local_get_img);
12487 + }
12488 + } else {
12489 + $featured_path = $get_img[1];
12490 + }
12491 + }
12492 + else
12493 + {
12494 + if(is_numeric($get_img))
12495 + {
12496 + $featured_path = aiomatic_assign_featured_image($get_img, $post_id);
12497 + }
12498 + else
12499 + {
12500 + if (!aiomatic_generate_featured_image($get_img, $post_id)) {
12501 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12502 + aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $get_img);
12503 + }
12504 + } else {
12505 + $featured_path = $get_img;
12506 + }
12507 + }
12508 + }
12509 + }
12510 + if($featured_path == '')
12511 + {
12512 + if ($image_url != '') {
12513 + $replacement = str_replace(array('[', ']'), '', $my_post['post_title']);
12514 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
12515 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
12516 + if(isset($matches[2]))
12517 + {
12518 + $chance = trim($matches[2], '[]');
12519 + }
12520 + else
12521 + {
12522 + $chance = '';
12523 + }
12524 + $arv = array();
12525 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
12526 + return $my_img;
12527 + }, $image_url_temp);
12528 + $img_rulx = $spintax->Parse(trim($image_url_temp));
12529 + $selected_img = aiomatic_select_ai_image($my_post['post_title'], $img_rulx);
12530 + if($selected_img === false)
12531 + {
12532 + $img_rulx = explode(',', $img_rulx);
12533 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
12534 + }
12535 + else
12536 + {
12537 + $img_rulx = $selected_img;
12538 + }
12539 + if(is_numeric($img_rulx))
12540 + {
12541 + $featured_path = aiomatic_assign_featured_image($img_rulx, $post_id);
12542 + }
12543 + else
12544 + {
12545 + if($img_rulx != '')
12546 + {
12547 + if (!aiomatic_generate_featured_image($img_rulx, $post_id)) {
12548 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12549 + aiomatic_log_to_file('aiomatic_generate_featured_image failed to default value: ' . $img_rulx . '!');
12550 + }
12551 + } else {
12552 + $featured_path = $img_rulx;
12553 + }
12554 + }
12555 + }
12556 + }
12557 + }
12558 + if ($auto_categories != 'disabled') {
12559 + if ($my_post['extra_categories'] != '') {
12560 + $extra_cats = explode(',', $my_post['extra_categories']);
12561 + if($post_type == 'product')
12562 + {
12563 + $product_tax = 'product_cat';
12564 + }
12565 + else
12566 + {
12567 + $product_tax = 'category';
12568 + }
12569 + foreach($extra_cats as $extra_cat)
12570 + {
12571 + $extra_cat = trim($extra_cat);
12572 + $extra_cat = strip_tags($extra_cat);
12573 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
12574 + if(empty($extra_cat))
12575 + {
12576 + continue;
12577 + }
12578 + if ($skip_inexist == '1')
12579 + {
12580 + if(!term_exists($extra_cat, $product_tax))
12581 + {
12582 + continue;
12583 + }
12584 + }
12585 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
12586 + wp_set_post_terms($post_id, $termid, $product_tax, true);
12587 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
12588 + {
12589 + foreach($termid as $tx)
12590 + {
12591 + pll_set_term_language($tx, $wpml_lang);
12592 + }
12593 + }
12594 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
12595 + {
12596 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
12597 + foreach($termid as $tx)
12598 + {
12599 + $pars['element_id'] = $tx;
12600 + $pars['element_type'] = $wpml_element_type;
12601 + $pars['language_code'] = $wpml_lang;
12602 + $pars['trid'] = FALSE;
12603 + $pars['source_language_code'] = NULL;
12604 + do_action('wpml_set_element_language_details', $pars);
12605 + }
12606 + }
12607 + }
12608 + }
12609 + }
12610 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678') {
12611 + $cats = array();
12612 + if(is_array($default_category))
12613 + {
12614 + foreach($default_category as $dc)
12615 + {
12616 + $cats[] = $dc;
12617 + }
12618 + }
12619 + else
12620 + {
12621 + $cats[] = $default_category;
12622 + }
12623 + global $sitepress;
12624 + if($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
12625 + {
12626 + $current_language = apply_filters( 'wpml_current_language', NULL );
12627 + $sitepress->switch_lang($wpml_lang);
12628 + }
12629 + $target_cat = 'category';
12630 + if($post_type == 'product')
12631 + {
12632 + $target_cat = 'product_cat';
12633 + }
12634 + wp_set_object_terms($post_id, array_map( 'intval', $cats ), $target_cat, true);
12635 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
12636 + {
12637 + foreach($cats as $cc)
12638 + {
12639 + pll_set_term_language($cc, $wpml_lang);
12640 + }
12641 + }
12642 + elseif($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
12643 + {
12644 + $sitepress->switch_lang($current_language);
12645 + }
12646 + }
12647 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
12648 + {
12649 + if($post_type == 'product')
12650 + {
12651 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
12652 + }
12653 + }
12654 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
12655 + if (is_wp_error($tax_rez)) {
12656 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12657 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
12658 + }
12659 + }
12660 + if($post_type == 'topic' && $parent_id != '')
12661 + {
12662 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
12663 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
12664 + update_post_meta($post_id, '_bbp_voice_count', '0');
12665 + update_post_meta($post_id, '_bbp_reply_count', '0');
12666 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
12667 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
12668 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
12669 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
12670 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
12671 + }
12672 + if($post_type == 'reply' && $parent_id != '')
12673 + {
12674 + if(function_exists('bbp_get_topic_forum_id'))
12675 + {
12676 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
12677 + if(empty($forum_aidi))
12678 + {
12679 + $forum_aidi = 0;
12680 + }
12681 + }
12682 + else
12683 + {
12684 + $forum_aidi = 0;
12685 + }
12686 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
12687 + }
12688 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
12689 + {
12690 + $new_categories = wp_get_post_categories($post_id);
12691 + $removed_cat = false;
12692 + if(isset($default_categories) && !($default_categories == $new_categories))
12693 + {
12694 + foreach($default_categories as $dc)
12695 + {
12696 + $rem_cat = get_category( $dc );
12697 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
12698 + $removed_cat = true;
12699 + }
12700 + }
12701 + if($removed_cat == false)
12702 + {
12703 + wp_remove_object_terms( $post_id, 'uncategorized', 'category' );
12704 + }
12705 + }
12706 + aiomatic_addPostMeta($post_id, $my_post, $param, $type, $featured_path, $post_topic, $rule_unique_id, $post_link);
12707 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
12708 + {
12709 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
12710 + $pars['element_id'] = $post_id;
12711 + $pars['element_type'] = $wpml_element_type;
12712 + $pars['language_code'] = $wpml_lang;
12713 + $pars['source_language_code'] = NULL;
12714 + do_action('wpml_set_element_language_details', $pars);
12715 +
12716 + global $wp_filesystem;
12717 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
12718 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
12719 + wp_filesystem($creds);
12720 + }
12721 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
12722 + {
12723 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
12724 + }
12725 + $wpml_lang = trim($wpml_lang);
12726 + if(function_exists('wpml_update_translatable_content'))
12727 + {
12728 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
12729 + if($my_post['post_title'] != '')
12730 + {
12731 + global $sitepress;
12732 + global $wpdb;
12733 + $keyid = md5($my_post['post_title']);
12734 + $keyName = $keyid . '_wpml';
12735 + $rezxxxa = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '$keyName' limit 1", ARRAY_A );
12736 + if(count($rezxxxa) != 0)
12737 + {
12738 + $metaRow = $rezxxxa[0];
12739 + $metaValue = $metaRow['meta_value'];
12740 + $metaParts = explode('_', $metaValue);
12741 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
12742 + }
12743 + else
12744 + {
12745 + $ptrid = $sitepress->get_element_trid($post_id);
12746 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
12747 + }
12748 + }
12749 +
12750 + }
12751 + }
12752 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
12753 + {
12754 + aiomatic_change_post_status($post_id, 'publish');
12755 + }
12756 + } else {
12757 + aiomatic_log_to_file('Failed to insert post into wp database(1)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
12758 + continue;
12759 + }
12760 + }
12761 + }
12762 + elseif($type == '1')
12763 + {
12764 + if($default_lang != '')
12765 + {
12766 + $default_lang = explode(',', $default_lang);
12767 + $default_lang = array_map( 'trim', $default_lang );
12768 + }
12769 + else
12770 + {
12771 + $default_lang = array();
12772 + }
12773 + $url_list_lines = array();
12774 + if($url_list != '')
12775 + {
12776 + $url_list_lines = preg_split('/\r\n|\r|\n/', $url_list);
12777 + }
12778 + else
12779 + {
12780 + aiomatic_log_to_file('You need to enter a YouTube video URL to use this feature!');
12781 + if($auto == 1)
12782 + {
12783 + aiomatic_clearFromList($param, $type);
12784 + }
12785 + return 'fail';
12786 + }
12787 + $additional_kws = array();
12788 + $post_link = '';
12789 + $user_name = '';
12790 + $featured_image = '';
12791 + $post_cats = '';
12792 + $post_tagz = '';
12793 + $post_excerpt = '';
12794 + $final_content = '';
12795 + $postID = '';
12796 + $heading_val = '';
12797 + $image_query = '';
12798 + $temp_post = '';
12799 + $cntx = count($url_list_lines);
12800 + $rss_feeds = array();
12801 + for($ji = 0; $ji < $cntx; $ji++)
12802 + {
12803 + if (filter_var($url_list_lines[$ji], FILTER_VALIDATE_URL) !== false && stristr($url_list_lines[$ji], 'youtu') === false)
12804 + {
12805 + if(aiomatic_endsWith($url_list_lines[$ji], '.txt'))
12806 + {
12807 + $txt_content = aiomatic_get_web_page($url_list_lines[$ji]);
12808 + if ($txt_content === FALSE)
12809 + {
12810 + aiomatic_log_to_file('Failed to read text file: ' . $url_list_lines[$ji]);
12811 + unset($url_list_lines[$ji]);
12812 + if($auto == 1)
12813 + {
12814 + aiomatic_log_to_file($param);
12815 + }
12816 + continue;
12817 + }
12818 + unset($url_list_lines[$ji]);
12819 + $additional_kws = preg_split('/\r\n|\r|\n/', $txt_content);
12820 + }
12821 + else
12822 + {
12823 + aiomatic_log_to_file('Trying to parse RSS feed items(3): ' . $url_list_lines[$ji]);
12824 + try
12825 + {
12826 + if(!class_exists('SimplePie_Autoloader', false))
12827 + {
12828 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
12829 + }
12830 + }
12831 + catch(Exception $e)
12832 + {
12833 + unset($url_list_lines[$ji]);
12834 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
12835 + if($auto == 1)
12836 + {
12837 + aiomatic_log_to_file($param);
12838 + }
12839 + continue;
12840 + }
12841 + $feed = new SimplePie();
12842 + $feed->set_timeout(120);
12843 + $feed->set_feed_url($url_list_lines[$ji]);
12844 + $feed->enable_cache(false);
12845 + $feed->strip_htmltags(false);
12846 + $feed->init();
12847 + $feed->handle_content_type();
12848 + if ($feed->error())
12849 + {
12850 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $url_list_lines[$ji]);
12851 + unset($url_list_lines[$ji]);
12852 + if($auto == 1)
12853 + {
12854 + aiomatic_clearFromList($param, $type);
12855 + }
12856 + continue;
12857 + }
12858 + $items = $feed->get_items();
12859 + $zero = true;
12860 + foreach($items as $itemx)
12861 + {
12862 + if($zero == true)
12863 + {
12864 + $post_link = trim($itemx->get_permalink());
12865 + if(isset($rss_items[$post_link]))
12866 + {
12867 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12868 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
12869 + }
12870 + continue;
12871 + }
12872 + if ($fauthor = $itemx->get_author())
12873 + {
12874 + $user_name = $fauthor->get_name();
12875 + }
12876 + else
12877 + {
12878 + $user_name = '';
12879 + }
12880 + $feed_cats = array();
12881 + $varx = $itemx->get_categories();
12882 + if(is_array($varx))
12883 + {
12884 + foreach ($varx as $xcategory)
12885 + {
12886 + $feed_cats[] = $xcategory->get_label();
12887 + }
12888 + }
12889 + $post_cats = implode(',', $feed_cats);
12890 + $post_excerpt = $itemx->get_description();
12891 + $final_content = $itemx->get_content();
12892 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
12893 + }
12894 + else
12895 + {
12896 + $post_link_temp = trim($itemx->get_permalink());
12897 + if(isset($rss_items[$post_link_temp]))
12898 + {
12899 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
12900 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
12901 + }
12902 + continue;
12903 + }
12904 + if ($fauthor = $itemx->get_author())
12905 + {
12906 + $user_name_temp = $fauthor->get_name();
12907 + }
12908 + else
12909 + {
12910 + $user_name_temp = '';
12911 + }
12912 + $feed_cats = array();
12913 + $varx = $itemx->get_categories();
12914 + if(is_array($varx))
12915 + {
12916 + foreach ($varx as $xcategory)
12917 + {
12918 + $feed_cats[] = $xcategory->get_label();
12919 + }
12920 + }
12921 + $post_cats_temp = implode(',', $feed_cats);
12922 + $post_excerpt_temp = $itemx->get_description();
12923 + $final_content_temp = $itemx->get_content();
12924 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link_temp, 'author' => $user_name_temp, 'cats' => $post_cats_temp, 'excerpt' => $post_excerpt_temp, 'content' => $final_content_temp );
12925 + }
12926 + $additional_kws[] = $itemx->get_permalink();
12927 + $zero = false;
12928 + }
12929 + unset($url_list_lines[$ji]);
12930 + }
12931 + }
12932 + }
12933 + if(count($additional_kws) > 0)
12934 + {
12935 + $url_list_lines = array_merge($url_list_lines, $additional_kws);
12936 + }
12937 + $url_list_lines = array_unique($url_list_lines);
12938 + foreach($url_list_lines as $zindedx => $urlline)
12939 + {
12940 + preg_match_all('#https:\/\/(?:www\.)?youtube\.com\/@([^\/\n\s]+)#i', $urlline, $hmatches);
12941 + if(isset($hmatches[1][0]))
12942 + {
12943 + $videos_ok = false;
12944 + if (isset($aiomatic_Main_Settings['yt_app_id']) && trim($aiomatic_Main_Settings['yt_app_id']) != '')
12945 + {
12946 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting videos from channel'));
12947 + $channel_id = '';
12948 + $za_app = explode(',', $aiomatic_Main_Settings['yt_app_id']);
12949 + $za_app = trim($za_app[array_rand($za_app)]);
12950 + $chfound = false;
12951 + $channel_link="https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&maxResults=1&q=" . trim(trim($hmatches[1][0]), '@') . "&key=" . $za_app;
12952 + $chid = aiomatic_get_web_page($channel_link);
12953 + if (stristr($chid, 'items')) {
12954 + $ch_json = json_decode($chid);
12955 + $channels = $ch_json->items;
12956 + foreach ($channels as $channel)
12957 + {
12958 + if(isset($channel->id->channelId))
12959 + {
12960 + $channel_id = $channel->id->channelId;
12961 + $chfound = true;
12962 + break;
12963 + }
12964 + }
12965 + }
12966 + else
12967 + {
12968 + aiomatic_log_to_file('Failed to find YouTube channel ID: ' . $hmatches[1][0]);
12969 + }
12970 + if($chfound == true)
12971 + {
12972 + $feed_uri = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&key=' . $za_app;
12973 + $feed_uri .= '&maxResults=50';
12974 + $feed_uri .= '&channelId=' . $channel_id;
12975 + $feed_uri .= '&order=date';
12976 + $chid = aiomatic_get_web_page($feed_uri);
12977 + $ytjson = json_decode($chid);
12978 + if($ytjson === null)
12979 + {
12980 + aiomatic_log_to_file('Failed to get video info for channel: ' . $channel_id);
12981 + }
12982 + else
12983 + {
12984 + if(isset($ytjson->items))
12985 + {
12986 + $zitems = $ytjson->items;
12987 + foreach ($zitems as $zitem)
12988 + {
12989 + $url_list_lines[] = 'https://www.youtube.com/watch?v=' . $zitem->id->videoId;
12990 + $videos_ok = true;
12991 + }
12992 + }
12993 + else
12994 + {
12995 + aiomatic_log_to_file('Failed to decode channel results: ' . print_r($ytjson, true));
12996 + }
12997 + }
12998 + }
12999 + }
13000 + if($videos_ok === false)
13001 + {
13002 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting videos from channel using direct method'));
13003 + $chch = curl_init();
13004 + if ($chch !== FALSE)
13005 + {
13006 + if (isset($aiomatic_Main_Settings['proxy_url']) && $aiomatic_Main_Settings['proxy_url'] != '' && $aiomatic_Main_Settings['proxy_url'] != 'disable' && $aiomatic_Main_Settings['proxy_url'] != 'disabled') {
13007 + $prx = explode(',', $aiomatic_Main_Settings['proxy_url']);
13008 + $randomness = array_rand($prx);
13009 + curl_setopt( $chch, CURLOPT_PROXY, trim($prx[$randomness]));
13010 + if (isset($aiomatic_Main_Settings['proxy_auth']) && $aiomatic_Main_Settings['proxy_auth'] != '')
13011 + {
13012 + $prx_auth = explode(',', $aiomatic_Main_Settings['proxy_auth']);
13013 + if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '')
13014 + {
13015 + curl_setopt( $chch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));
13016 + }
13017 + }
13018 + }
13019 + curl_setopt($chch, CURLOPT_CONNECTTIMEOUT, 10);
13020 + if (isset($aiomatic_Main_Settings['max_timeout']) && $aiomatic_Main_Settings['max_timeout'] != '')
13021 + {
13022 + $ztime = intval($aiomatic_Main_Settings['max_timeout']);
13023 + }
13024 + else
13025 + {
13026 + $ztime = 300;
13027 + }
13028 + curl_setopt($chch, CURLOPT_TIMEOUT, $ztime);
13029 + curl_setopt($chch, CURLOPT_HTTPGET, 1);
13030 + curl_setopt($chch, CURLOPT_REFERER, get_site_url());
13031 + curl_setopt($chch, CURLOPT_URL, 'https://www.youtube.com/@' . $hmatches[1][0] . '/');
13032 + curl_setopt($chch, CURLOPT_RETURNTRANSFER, 1);
13033 + curl_setopt($chch, CURLOPT_SSL_VERIFYHOST, 0);
13034 + curl_setopt($chch, CURLOPT_SSL_VERIFYPEER, 0);
13035 + $za_video_page = curl_exec($chch);
13036 + if($za_video_page === false)
13037 + {
13038 + aiomatic_log_to_file('Failed to download channel listing: ' . $urlline);
13039 + }
13040 + else
13041 + {
13042 + preg_match_all('#"videoIds":\["([^"]*?)"\]#i', $za_video_page, $hmatches);
13043 + if(isset($hmatches[1][0]))
13044 + {
13045 + $vms = array_unique($hmatches[1]);
13046 + $vms = array_reverse($vms);
13047 + foreach($vms as $vma)
13048 + {
13049 + $url_list_lines[] = 'https://www.youtube.com/watch?v=' . $vma;
13050 + }
13051 + }
13052 + }
13053 + curl_close($chch);
13054 + $rand_sleep = wp_rand(400, 700);
13055 + usleep($rand_sleep * 1000);
13056 + }
13057 + else
13058 + {
13059 + aiomatic_log_to_file('Failed to init curl in channel listing: ' . $urlline);
13060 + }
13061 + }
13062 + unset($url_list_lines[$zindedx]);
13063 + }
13064 + if(stristr($urlline, 'list=') !== false)
13065 + {
13066 + $playlist_id = aiomatic_getPlaylistIdFromUrl(trim($urlline));
13067 + if(!empty($playlist_id))
13068 + {
13069 + $videos_ok = false;
13070 + if (isset($aiomatic_Main_Settings['yt_app_id']) && trim($aiomatic_Main_Settings['yt_app_id']) != '')
13071 + {
13072 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting videos from playlist'));
13073 + $channel_id = '';
13074 + $za_app = explode(',', $aiomatic_Main_Settings['yt_app_id']);
13075 + $za_app = trim($za_app[array_rand($za_app)]);
13076 + $chfound = false;
13077 + $channel_link='https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key=' . $za_app . '&maxResults=50&playlistId=' . trim($playlist_id);
13078 + $chid = aiomatic_get_web_page($channel_link);
13079 + if (stristr($chid, 'items')) {
13080 + $ch_json = json_decode($chid);
13081 + if(isset($ch_json->items))
13082 + {
13083 + $items = $ch_json->items;
13084 + foreach ($items as $zitem)
13085 + {
13086 + $url_list_lines[] = 'https://www.youtube.com/watch?v=' . $zitem->snippet->resourceId->videoId;
13087 + $videos_ok = true;
13088 + }
13089 + }
13090 + }
13091 + else
13092 + {
13093 + aiomatic_log_to_file('Failed to find YouTube playlist ID: ' . $playlist_id);
13094 + }
13095 + }
13096 + if($videos_ok === false)
13097 + {
13098 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting videos from playlist using direct method'));
13099 + $chch = curl_init();
13100 + if ($chch !== FALSE)
13101 + {
13102 + if (isset($aiomatic_Main_Settings['proxy_url']) && $aiomatic_Main_Settings['proxy_url'] != '' && $aiomatic_Main_Settings['proxy_url'] != 'disable' && $aiomatic_Main_Settings['proxy_url'] != 'disabled') {
13103 + $prx = explode(',', $aiomatic_Main_Settings['proxy_url']);
13104 + $randomness = array_rand($prx);
13105 + curl_setopt( $chch, CURLOPT_PROXY, trim($prx[$randomness]));
13106 + if (isset($aiomatic_Main_Settings['proxy_auth']) && $aiomatic_Main_Settings['proxy_auth'] != '')
13107 + {
13108 + $prx_auth = explode(',', $aiomatic_Main_Settings['proxy_auth']);
13109 + if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '')
13110 + {
13111 + curl_setopt( $chch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));
13112 + }
13113 + }
13114 + }
13115 + curl_setopt($chch, CURLOPT_CONNECTTIMEOUT, 10);
13116 + if (isset($aiomatic_Main_Settings['max_timeout']) && $aiomatic_Main_Settings['max_timeout'] != '')
13117 + {
13118 + $ztime = intval($aiomatic_Main_Settings['max_timeout']);
13119 + }
13120 + else
13121 + {
13122 + $ztime = 300;
13123 + }
13124 + curl_setopt($chch, CURLOPT_TIMEOUT, $ztime);
13125 + curl_setopt($chch, CURLOPT_HTTPGET, 1);
13126 + curl_setopt($chch, CURLOPT_REFERER, get_site_url());
13127 + curl_setopt($chch, CURLOPT_URL, 'https://www.youtube.com/playlist?list=' . $playlist_id);
13128 + curl_setopt($chch, CURLOPT_RETURNTRANSFER, 1);
13129 + curl_setopt($chch, CURLOPT_SSL_VERIFYHOST, 0);
13130 + curl_setopt($chch, CURLOPT_SSL_VERIFYPEER, 0);
13131 + $za_video_page = curl_exec($chch);
13132 + if($za_video_page === false)
13133 + {
13134 + aiomatic_log_to_file('Failed to download playlist listing: ' . $urlline);
13135 + }
13136 + else
13137 + {
13138 + preg_match_all('#"videoIds":\["([^"]*?)"\]#i', $za_video_page, $hmatches);
13139 + if(isset($hmatches[1][0]))
13140 + {
13141 + $vms = array_unique($hmatches[1]);
13142 + $vms = array_reverse($vms);
13143 + foreach($vms as $vma)
13144 + {
13145 + $url_list_lines[] = 'https://www.youtube.com/watch?v=' . $vma;
13146 + }
13147 + }
13148 + }
13149 + curl_close($chch);
13150 + $rand_sleep = wp_rand(400, 700);
13151 + usleep($rand_sleep * 1000);
13152 + }
13153 + else
13154 + {
13155 + aiomatic_log_to_file('Failed to init curl in playlist listing: ' . $urlline);
13156 + }
13157 + }
13158 + unset($url_list_lines[$zindedx]);
13159 + }
13160 + }
13161 + }
13162 + if($overwrite_existing == '1')
13163 + {
13164 + $url_list_lines_processed = array();
13165 + foreach($url_list_lines as $indx => $ptl)
13166 + {
13167 + $ptlprocessed = explode('!###!', $ptl);
13168 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
13169 + {
13170 + $url_list_lines_processed[$indx] = $ptlprocessed[0];
13171 + }
13172 + else
13173 + {
13174 + $url_list_lines_processed[$indx] = $ptl;
13175 + }
13176 + }
13177 + foreach($posted_items as $ptit => $pid)
13178 + {
13179 + if (($key = array_search($ptit, $url_list_lines_processed)) !== false) {
13180 + $update_post_id = $pid;
13181 + break;
13182 + }
13183 + }
13184 + }
13185 + else
13186 + {
13187 + if($title_once == '1')
13188 + {
13189 + $skipt = 0;
13190 + $url_list_lines_processed = array();
13191 + foreach($url_list_lines as $indx => $ptl)
13192 + {
13193 + $ptlprocessed = explode('!###!', $ptl);
13194 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
13195 + {
13196 + $url_list_lines_processed[$indx] = $ptlprocessed[0];
13197 + }
13198 + else
13199 + {
13200 + $url_list_lines_processed[$indx] = $ptl;
13201 + }
13202 + }
13203 + foreach($posted_items as $ptit => $pid)
13204 + {
13205 + if (($key = array_search($ptit, $url_list_lines_processed)) !== false) {
13206 + aiomatic_log_to_file('Skipping YouTube title, already processed: ' . $ptit);
13207 + unset($url_list_lines[$key]);
13208 + unset($url_list_lines_processed[$key]);
13209 + $skipt++;
13210 + }
13211 + }
13212 + if(count($url_list_lines) == 0 && $skipt > 0)
13213 + {
13214 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
13215 + {
13216 + if($count === 1)
13217 + {
13218 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
13219 + foreach($email_list as $thisaddr)
13220 + {
13221 + $thisaddr = trim($thisaddr);
13222 + try
13223 + {
13224 + $admin_mail = get_option('admin_email');
13225 + $to = $thisaddr;
13226 + $subject = '[Aimogen] YouTube URLs depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
13227 + $message = 'YouTube Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its YouTube URLs list, no new posts publised! Website URL: ' . get_site_url();
13228 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
13229 + $headers[] = 'Reply-To: ' . $admin_mail;
13230 + $headers[] = 'X-Mailer: PHP/' . phpversion();
13231 + $headers[] = 'Content-Type: text/html';
13232 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
13233 + wp_mail($to, $subject, $message, $headers);
13234 + }
13235 + catch (Exception $e) {
13236 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
13237 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
13238 + }
13239 + }
13240 + }
13241 + }
13242 + }
13243 + aiomatic_log_to_file('All YouTube URLs already processed, nothing to do.');
13244 + if($auto == 1)
13245 + {
13246 + aiomatic_clearFromList($param, $type);
13247 + }
13248 + return 'nochange';
13249 + }
13250 + }
13251 + }
13252 + $spintax = new Aiomatic_Spintax();
13253 + $orig_ai_command = $ai_command;
13254 + $orig_ai_command_title = $title_ai_command;
13255 + $orig_ai_command_category = $category_ai_command;
13256 + $orig_ai_command_tag = $tag_ai_command;
13257 + $orig_ai_command_image = $ai_command_image;
13258 + $already_spinned = 0;
13259 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
13260 + {
13261 + $img_attr = $aiomatic_Main_Settings['attr_text'];
13262 + }
13263 + else
13264 + {
13265 + $img_attr = '';
13266 + }
13267 + $headings_arr = array();
13268 + if($images_list != '')
13269 + {
13270 + $images_arr = preg_split('/\r\n|\r|\n/', $images_list);
13271 + $images_arr = array_map('trim', $images_arr);
13272 + }
13273 + else
13274 + {
13275 + $images_arr = array();
13276 + }
13277 + while(true)
13278 + {
13279 + $thread_id = '';
13280 + $my_title_outro = '';
13281 + if ($count > intval($max)) {
13282 + break;
13283 + }
13284 + $heading_image_loc = $heading_img_location;
13285 + if($heading_image_loc == 'random')
13286 + {
13287 + $xwords = array('top', 'heading', 'bottom');
13288 + $randomWord = $xwords[array_rand($xwords)];
13289 + $heading_image_loc = $randomWord;
13290 + }
13291 + $update_post_id = '';
13292 + $current_section = '';
13293 + $post_sections = '';
13294 + $post_topic = '';
13295 + $post_title_keywords = '';
13296 + $headings_arr_copy = $headings_arr;
13297 + $added_img_list = array();
13298 + $raw_img_list = array();
13299 + $full_result_list = array();
13300 + $added_images = 0;
13301 + $heading_results = array();
13302 + $returned_caption = '';
13303 + if(count($url_list_lines) == 0)
13304 + {
13305 + break;
13306 + }
13307 + if ($count > intval($max)) {
13308 + break;
13309 + }
13310 + if($no_random == '1')
13311 + {
13312 + $current_index = array_key_first($url_list_lines);
13313 + }
13314 + else
13315 + {
13316 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
13317 + {
13318 + $current_index = array_rand($url_list_lines);
13319 + }
13320 + else
13321 + {
13322 + $current_index = array_key_first($url_list_lines);
13323 + }
13324 + }
13325 + $video_url = trim($url_list_lines[$current_index]);
13326 + if($title_once == '1')
13327 + {
13328 + unset($url_list_lines[$current_index]);
13329 + }
13330 + $ptlprocessed = explode('!###!', $video_url);
13331 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
13332 + {
13333 + $video_url = $ptlprocessed[0];
13334 + $post_title_keywords = $ptlprocessed[1];
13335 + }
13336 + if(isset($rss_feeds[$video_url]))
13337 + {
13338 + $post_link = $rss_feeds[$video_url]['url'];
13339 + $user_name = $rss_feeds[$video_url]['author'];
13340 + $post_cats = $rss_feeds[$video_url]['cats'];
13341 + $post_excerpt = $rss_feeds[$video_url]['excerpt'];
13342 + $final_content = $rss_feeds[$video_url]['content'];
13343 + }
13344 + $tprepp = $spintax->Parse($video_url);
13345 + if($tprepp != false && $tprepp != '')
13346 + {
13347 + $video_url = $tprepp;
13348 + }
13349 + $video_url = aiomatic_replaceSynergyShortcodes($video_url);
13350 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
13351 + {
13352 + aiomatic_log_to_file('Starting processing YouTube data: ' . $video_url);
13353 + }
13354 + $old_title = $video_url;
13355 + $already_spinned = 0;
13356 + if (!stristr($video_url, 'youtu') && !stristr($video_url, 'http'))
13357 + {
13358 + if (isset($aiomatic_Main_Settings['yt_app_id']) && trim($aiomatic_Main_Settings['yt_app_id']) != '')
13359 + {
13360 + $za_app = explode(',', $aiomatic_Main_Settings['yt_app_id']);
13361 + $za_app = trim($za_app[array_rand($za_app)]);
13362 + $feed_uri = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&key=' . $za_app;
13363 + $feed_uri .= '&maxResults=50';
13364 + $feed_uri .= '&q='.urlencode(trim(stripslashes(str_replace('&quot;', '"', $video_url))));
13365 + $feed_uri .= '&order=date';
13366 + $chid = aiomatic_get_web_page($feed_uri);
13367 + $ytjson = json_decode($chid);
13368 + if($ytjson === null)
13369 + {
13370 + aiomatic_log_to_file('Failed to get video info for youtube keyword: ' . $video_url);
13371 + unset($url_list_lines[$current_index]);
13372 + break;
13373 + }
13374 + else
13375 + {
13376 + if(isset($ytjson->items))
13377 + {
13378 + $zitems = $ytjson->items;
13379 + foreach ($zitems as $zitem)
13380 + {
13381 + $video_url = 'https://www.youtube.com/watch?v=' . $zitem->id->videoId;
13382 + }
13383 + }
13384 + else
13385 + {
13386 + aiomatic_log_to_file('Failed to decode video search results: ' . print_r($ytjson, true));
13387 + unset($url_list_lines[$current_index]);
13388 + break;
13389 + }
13390 + }
13391 + }
13392 + else
13393 + {
13394 + aiomatic_log_to_file('Only YouTube URLs are supported, this does not seem to be a YouTube video URL: ' . $video_url);
13395 + unset($url_list_lines[$current_index]);
13396 + break;
13397 + }
13398 + }
13399 + $allmodels = aiomatic_get_all_models();
13400 + if($custom_shortcodes != '')
13401 + {
13402 + $custom_shortcodes_arr = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
13403 + foreach($custom_shortcodes_arr as $my_short)
13404 + {
13405 + $name_part = explode('=>', $my_short);
13406 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
13407 + {
13408 + $shortname = trim($name_part[0]);
13409 + if(strstr($video_url, '%%' . $shortname . '%%'))
13410 + {
13411 + $shortval = '';
13412 + $name_part[1] = str_replace('%%video_url%%', $video_url, $name_part[1]);
13413 + $ai_part = explode('@@', $name_part[1]);
13414 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
13415 + {
13416 + if(!in_array(trim($ai_part[0]), $allmodels))
13417 + {
13418 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
13419 + }
13420 + else
13421 + {
13422 + $aimodel = trim($ai_part[0]);
13423 + }
13424 + $zai_command = trim($ai_part[1]);
13425 + $zai_command = aiomatic_replaceSynergyShortcodes($zai_command);
13426 + $zai_command = aiomatic_replaceAIPostShortcodes($zai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
13427 + $zai_command = aiomatic_replace_youtube($zai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13428 + $zai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $zai_command);
13429 + foreach($custom_shortcodes_arr as $my_check_short)
13430 + {
13431 + $check_name_part = explode('=>', $my_check_short);
13432 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
13433 + {
13434 + $check_shortname = trim($check_name_part[0]);
13435 + if($shortname !== $check_shortname && strstr($zai_command, '%%' . $check_shortname . '%%') !== false)
13436 + {
13437 + $check_shortval = '';
13438 + $check_ai_part = explode('@@', $check_name_part[1]);
13439 + if(!in_array(trim($check_ai_part[0]), $allmodels))
13440 + {
13441 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
13442 + }
13443 + else
13444 + {
13445 + $check_aimodel = trim($check_ai_part[0]);
13446 + }
13447 + $check_ai_command = trim($check_ai_part[1]);
13448 + $aierror = '';
13449 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
13450 + $check_ai_command = aiomatic_replaceAIPostShortcodes($check_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
13451 + $check_ai_command = aiomatic_replace_youtube($check_ai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13452 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
13453 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
13454 + if($check_generated_text === false)
13455 + {
13456 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
13457 + }
13458 + else
13459 + {
13460 + $check_shortval = trim(trim($check_generated_text), '.');
13461 + }
13462 + $zai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $zai_command);
13463 + }
13464 + }
13465 + }
13466 + $aierror = '';
13467 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $zai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
13468 + if($generated_text === false)
13469 + {
13470 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
13471 + }
13472 + else
13473 + {
13474 + $shortval = trim(trim($generated_text), '.');
13475 + }
13476 + }
13477 + $video_url = str_replace('%%' . $shortname . '%%', $shortval, $video_url);
13478 + }
13479 + }
13480 + }
13481 + }
13482 + if(!empty($global_prepend))
13483 + {
13484 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
13485 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
13486 + $global_prepend = aiomatic_replace_youtube($global_prepend, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13487 + }
13488 + if(!empty($global_append))
13489 + {
13490 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
13491 + $global_append = aiomatic_replace_youtube($global_append, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13492 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
13493 + }
13494 + $video_url = aiomatic_replaceSynergyShortcodes($video_url);
13495 + $video_url = apply_filters('aiomatic_replace_aicontent_shortcode', $video_url);
13496 + $video_url = aiomatic_extractYouTubeVideoId($video_url);
13497 + if(empty($video_url))
13498 + {
13499 + aiomatic_log_to_file('Empty Video URL Submitted: ' . $video_url);
13500 + unset($url_list_lines[$current_index]);
13501 + break;
13502 + }
13503 +
13504 + $za_video_page = '';
13505 + $returned_caption = '';
13506 + $ch = curl_init();
13507 + if ($ch !== FALSE)
13508 + {
13509 + if (isset($aiomatic_Main_Settings['proxy_url']) && $aiomatic_Main_Settings['proxy_url'] != '' && $aiomatic_Main_Settings['proxy_url'] != 'disable' && $aiomatic_Main_Settings['proxy_url'] != 'disabled') {
13510 + $prx = explode(',', $aiomatic_Main_Settings['proxy_url']);
13511 + $randomness = array_rand($prx);
13512 + curl_setopt( $ch, CURLOPT_PROXY, trim($prx[$randomness]));
13513 + if (isset($aiomatic_Main_Settings['proxy_auth']) && $aiomatic_Main_Settings['proxy_auth'] != '')
13514 + {
13515 + $prx_auth = explode(',', $aiomatic_Main_Settings['proxy_auth']);
13516 + if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '')
13517 + {
13518 + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));
13519 + }
13520 + }
13521 + }
13522 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
13523 + if (isset($aiomatic_Main_Settings['max_timeout']) && $aiomatic_Main_Settings['max_timeout'] != '')
13524 + {
13525 + $ztime = intval($aiomatic_Main_Settings['max_timeout']);
13526 + }
13527 + else
13528 + {
13529 + $ztime = 300;
13530 + }
13531 + if (isset($aiomatic_Main_Settings['youtube_cookies']) && $aiomatic_Main_Settings['youtube_cookies'] != '')
13532 + {
13533 + curl_setopt($ch, CURLOPT_COOKIE, $aiomatic_Main_Settings['youtube_cookies']);
13534 + }
13535 + curl_setopt($ch, CURLOPT_TIMEOUT, $ztime);
13536 + curl_setopt($ch, CURLOPT_HTTPGET, 1);
13537 + curl_setopt($ch, CURLOPT_REFERER, get_site_url());
13538 + curl_setopt($ch, CURLOPT_URL, $video_url);
13539 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
13540 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
13541 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
13542 + $za_video_page = curl_exec($ch);
13543 + if($za_video_page === false)
13544 + {
13545 + aiomatic_log_to_file('Failed to download video URL: ' . $video_url);
13546 + }
13547 + curl_close($ch);
13548 + }
13549 + else
13550 + {
13551 + aiomatic_log_to_file('Failed to init curl in video listing: ' . $video_url);
13552 + }
13553 + $post_title = '';
13554 + $post_desc = '';
13555 + if($za_video_page !== false)
13556 + {
13557 + preg_match_all('#<meta name="title" content="([^"]*?)">#i', $za_video_page, $zmatches);
13558 + if(isset($zmatches[1][0]))
13559 + {
13560 + $post_title = $zmatches[1][0];
13561 + }
13562 + else
13563 + {
13564 + preg_match_all('#<title>([^"]*?) - YouTube<\/title>#i', $za_video_page, $zmatches);
13565 + if(isset($zmatches[1][0]))
13566 + {
13567 + $post_title = $zmatches[1][0];
13568 + }
13569 + else
13570 + {
13571 + preg_match_all('#<meta property="og:title" content="([^"]*?)">#i', $za_video_page, $zmatches);
13572 + if(isset($zmatches[1][0]))
13573 + {
13574 + $post_title = $zmatches[1][0];
13575 + }
13576 + else
13577 + {
13578 + preg_match_all('#<meta property="twitter:title" content="([^"]*?)">#i', $za_video_page, $zmatches);
13579 + if(isset($zmatches[1][0]))
13580 + {
13581 + $post_title = $zmatches[1][0];
13582 + }
13583 + else
13584 + {
13585 + preg_match_all('#<meta itemprop="name" content="([^"]*?)">#i', $za_video_page, $zmatches);
13586 + if(isset($zmatches[1][0]))
13587 + {
13588 + $post_title = $zmatches[1][0];
13589 + }
13590 + }
13591 + }
13592 + }
13593 + }
13594 + preg_match_all('#"description":{"simpleText":"([^"]*?)"#i', $za_video_page, $zmatches);
13595 + if(isset($zmatches[1][0]))
13596 + {
13597 + $post_desc = $zmatches[1][0];
13598 + }
13599 + else
13600 + {
13601 + preg_match_all('#"attributedDescription":{"content":"([^"]*?)"#i', $za_video_page, $zmatches);
13602 + if(isset($zmatches[1][0]))
13603 + {
13604 + $post_desc = $zmatches[1][0];
13605 + }
13606 + else
13607 + {
13608 + preg_match_all('#"attributedDescriptionBodyText":{"content":"([^"]*?)"#i', $za_video_page, $zmatches);
13609 + if(isset($zmatches[1][0]))
13610 + {
13611 + $post_desc = $zmatches[1][0];
13612 + }
13613 + else
13614 + {
13615 + preg_match_all('#"shortDescription":"([^"]*?)"#i', $za_video_page, $zmatches);
13616 + if(isset($zmatches[1][0]))
13617 + {
13618 + $post_desc = $zmatches[1][0];
13619 + }
13620 + }
13621 + }
13622 + }
13623 + $video_id = '';
13624 + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $amatches);
13625 + if(isset($amatches[1]))
13626 + {
13627 + $video_id = $amatches[1];
13628 + }
13629 + $caprez = false;
13630 + if(!empty($video_id))
13631 + {
13632 + try
13633 + {
13634 + $caprez = aiomatic_get_video_transcript($video_id);
13635 + }
13636 + catch(Exception $e)
13637 + {
13638 + try
13639 + {
13640 + $caprez = aiomatic_get_video_transcript_2($video_id);
13641 + }
13642 + catch (Exception $e)
13643 + {
13644 + }
13645 + }
13646 + }
13647 + if(is_array($caprez) && isset($caprez['text']))
13648 + {
13649 + $returned_caption = $caprez['text'];
13650 + }
13651 + }
13652 + if(empty($returned_caption))
13653 + {
13654 + aiomatic_log_to_file('[WARNING] Failed to find subtitles for video URL: ' . $video_url);
13655 + break;
13656 + }
13657 + if($max_caption != '')
13658 + {
13659 + $max_caption = intval($max_caption);
13660 + $returned_caption = aiomatic_substr($returned_caption, 0, $max_caption);
13661 + }
13662 + $youtube_thumb = '';
13663 + preg_match_all('#<link rel="image_src" href="([^"]*?)">#i', $za_video_page, $zmatches);
13664 + if(isset($zmatches[1][0]))
13665 + {
13666 + $youtube_thumb = $zmatches[1][0];
13667 + }
13668 + else
13669 + {
13670 + preg_match_all('#<meta property="og:image" content="([^"]*?)">#i', $za_video_page, $zmatches);
13671 + if(isset($zmatches[1][0]))
13672 + {
13673 + $youtube_thumb = $zmatches[1][0];
13674 + }
13675 + else
13676 + {
13677 + preg_match_all('#<meta name="twitter:image" content="([^"]*?)">#i', $za_video_page, $zmatches);
13678 + if(isset($zmatches[1][0]))
13679 + {
13680 + $youtube_thumb = $zmatches[1][0];
13681 + }
13682 + else
13683 + {
13684 + preg_match_all('#<link itemprop="thumbnailUrl" href="([^"]*?)">#i', $za_video_page, $zmatches);
13685 + if(isset($zmatches[1][0]))
13686 + {
13687 + $youtube_thumb = $zmatches[1][0];
13688 + }
13689 + }
13690 + }
13691 + }
13692 + if($ai_titles == '1')
13693 + {
13694 + if($title_prompt != '')
13695 + {
13696 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
13697 + {
13698 + $title_prompt_arr = preg_split('/\r\n|\r|\n/', $title_prompt);
13699 + $my_title_prompt = $title_prompt_arr[array_rand($title_prompt_arr)];
13700 + }
13701 + else
13702 + {
13703 + $my_title_prompt = $title_prompt;
13704 + }
13705 + $zatitle_prompt = aiomatic_replace_youtube($my_title_prompt, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13706 + if(stristr($run_regex_on, 'title') !== false)
13707 + {
13708 + if ($strip_by_regex_prompts !== '')
13709 + {
13710 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
13711 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
13712 + $xcnt = 0;
13713 + foreach($xstrip_by_regex as $sbr)
13714 + {
13715 + if(isset($xreplace_regex[$xcnt]))
13716 + {
13717 + $repreg = $xreplace_regex[$xcnt];
13718 + }
13719 + else
13720 + {
13721 + $repreg = '';
13722 + }
13723 + $xcnt++;
13724 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $zatitle_prompt);
13725 + if($temp_cont !== NULL)
13726 + {
13727 + $zatitle_prompt = $temp_cont;
13728 + }
13729 + }
13730 + }
13731 + }
13732 + if($zatitle_prompt != '')
13733 + {
13734 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating title based on YouTube video'));
13735 + $written = aiomatic_write_content($zatitle_prompt, $topic_title_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'title', 'user', $assistant_id, $thread_id, $store_data);
13736 + if($written === false)
13737 + {
13738 + aiomatic_log_to_file('Failed to generate title!');
13739 + if($auto == 1)
13740 + {
13741 + aiomatic_clearFromList($param, $type);
13742 + }
13743 + return 'fail';
13744 + }
13745 + else
13746 + {
13747 + $written = ucfirst(trim(trim($written), '.'));
13748 + $post_title = $written;
13749 + }
13750 + }
13751 + else
13752 + {
13753 + aiomatic_log_to_file('Empty processed title prompt. Nothing to do.');
13754 + if($auto == 1)
13755 + {
13756 + aiomatic_clearFromList($param, $type);
13757 + }
13758 + return 'fail';
13759 + }
13760 + }
13761 + }
13762 + if (strpos($post_title, '%%') === false)
13763 + {
13764 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
13765 + {
13766 + $xposts = get_posts(
13767 + array(
13768 + 'post_type' => $post_type,
13769 + 'title' => html_entity_decode($post_title),
13770 + 'post_status' => 'all',
13771 + 'numberposts' => 1,
13772 + 'update_post_term_cache' => false,
13773 + 'update_post_meta_cache' => false,
13774 + 'orderby' => 'post_date ID',
13775 + 'order' => 'ASC',
13776 + )
13777 + );
13778 + if ( ! empty( $xposts ) ) {
13779 + $zap = $xposts[0];
13780 + } else {
13781 + $zap = null;
13782 + }
13783 + if($zap !== null)
13784 + {
13785 + if($overwrite_existing)
13786 + {
13787 + $update_post_id = $zap->ID;
13788 + }
13789 + else
13790 + {
13791 + aiomatic_log_to_file('Post with specified title found to be existing, skipping it: ' . $post_title);
13792 + unset($url_list_lines[$current_index]);
13793 + continue;
13794 + }
13795 + }
13796 + }
13797 + $new_post_title = $post_title;
13798 + }
13799 + else
13800 + {
13801 + $new_post_title = $post_title;
13802 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
13803 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
13804 + {
13805 + $xposts = get_posts(
13806 + array(
13807 + 'post_type' => $post_type,
13808 + 'title' => html_entity_decode($new_post_title),
13809 + 'post_status' => 'all',
13810 + 'numberposts' => 1,
13811 + 'update_post_term_cache' => false,
13812 + 'update_post_meta_cache' => false,
13813 + 'orderby' => 'post_date ID',
13814 + 'order' => 'ASC',
13815 + )
13816 + );
13817 + if ( ! empty( $xposts ) ) {
13818 + $zap = $xposts[0];
13819 + } else {
13820 + $zap = null;
13821 + }
13822 + if($zap !== null)
13823 + {
13824 + if($overwrite_existing)
13825 + {
13826 + $update_post_id = $zap->ID;
13827 + }
13828 + else
13829 + {
13830 + aiomatic_log_to_file('Post with specified title already published, skipping it: ' . $new_post_title);
13831 + unset($url_list_lines[$current_index]);
13832 + continue;
13833 + }
13834 + }
13835 + }
13836 + }
13837 + $post_sections = '';
13838 + $current_section = '';
13839 + $headings_arr_copy = $headings_arr;
13840 + $added_img_list = array();
13841 + $raw_img_list = array();
13842 + $full_result_list = array();
13843 + $added_vid_list = array();
13844 + $added_images = 0;
13845 + $added_videos = 0;
13846 + $heading_results = array();
13847 + if(empty($post_title))
13848 + {
13849 + aiomatic_log_to_file('Empty post title submitted, nothing to do!');
13850 + continue;
13851 + }
13852 + if(stristr($run_regex_on, 'sections') !== false)
13853 + {
13854 + if ($strip_by_regex_prompts !== '')
13855 + {
13856 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
13857 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
13858 + $xcnt = 0;
13859 + foreach($xstrip_by_regex as $sbr)
13860 + {
13861 + if(isset($xreplace_regex[$xcnt]))
13862 + {
13863 + $repreg = $xreplace_regex[$xcnt];
13864 + }
13865 + else
13866 + {
13867 + $repreg = '';
13868 + }
13869 + $xcnt++;
13870 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $sections_prompt);
13871 + if($temp_cont !== NULL)
13872 + {
13873 + $sections_prompt = $temp_cont;
13874 + }
13875 + }
13876 + }
13877 + }
13878 + if($sections_prompt != '' && $post_sections_list == '')
13879 + {
13880 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
13881 + {
13882 + $sections_prompt_arr = preg_split('/\r\n|\r|\n/', $sections_prompt);
13883 + $my_post_section = $sections_prompt_arr[array_rand($sections_prompt_arr)];
13884 + }
13885 + else
13886 + {
13887 + $my_post_section = $sections_prompt;
13888 + }
13889 + $my_post_section = aiomatic_replace_youtube($my_post_section, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13890 + if($my_post_section != '')
13891 + {
13892 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
13893 + $written = aiomatic_write_content($my_post_section, $topic_sections_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'sections', 'user', $assistant_id, $thread_id, $store_data);
13894 + if($written === false)
13895 + {
13896 + aiomatic_log_to_file('Failed to generate sections!');
13897 + if($auto == 1)
13898 + {
13899 + aiomatic_clearFromList($param, $type);
13900 + }
13901 + return 'fail';
13902 + }
13903 + else
13904 + {
13905 + $post_sections = $written;
13906 + }
13907 + }
13908 + else
13909 + {
13910 + aiomatic_log_to_file('Empty processed sections prompt. Nothing to do.');
13911 + if($auto == 1)
13912 + {
13913 + aiomatic_clearFromList($param, $type);
13914 + }
13915 + return 'fail';
13916 + }
13917 + }
13918 + else
13919 + {
13920 + if($post_sections_list != '')
13921 + {
13922 + $post_sections = $spintax->Parse($post_sections_list);
13923 + }
13924 + }
13925 + $post_sections = aiomatic_filter_sections($post_sections);
13926 + $new_post_content = '';
13927 + if(stristr($run_regex_on, 'intro') !== false)
13928 + {
13929 + if ($strip_by_regex_prompts !== '')
13930 + {
13931 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
13932 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
13933 + $xcnt = 0;
13934 + foreach($xstrip_by_regex as $sbr)
13935 + {
13936 + if(isset($xreplace_regex[$xcnt]))
13937 + {
13938 + $repreg = $xreplace_regex[$xcnt];
13939 + }
13940 + else
13941 + {
13942 + $repreg = '';
13943 + }
13944 + $xcnt++;
13945 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $intro_prompt);
13946 + if($temp_cont !== NULL)
13947 + {
13948 + $intro_prompt = $temp_cont;
13949 + }
13950 + }
13951 + }
13952 + }
13953 + if($intro_prompt != '')
13954 + {
13955 + $my_intro_prompt = $intro_prompt;
13956 + $my_intro_prompt = aiomatic_replace_youtube($my_intro_prompt, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
13957 + if($my_intro_prompt != '')
13958 + {
13959 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based intro'));
13960 + $written = aiomatic_write_content($my_intro_prompt, $topic_intro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'intro', 'user', $assistant_id, $thread_id, $store_data);
13961 + if($written === false)
13962 + {
13963 + aiomatic_log_to_file('Failed to generate intro for section: ' . $current_section);
13964 + continue;
13965 + }
13966 + else
13967 + {
13968 + if($new_post_content != '')
13969 + {
13970 + $new_post_content .= ' ';
13971 + }
13972 + $new_post_content .= $written;
13973 + }
13974 + }
13975 + else
13976 + {
13977 + aiomatic_log_to_file('Empty processed intro prompt. Nothing to do.');
13978 + }
13979 + }
13980 + if(stristr($topic_images_bck, '-') !== false)
13981 + {
13982 + preg_match('/\s*(\d+)\s*-\s*(\d+)\s*/', $topic_images_bck, $matches);
13983 + if(isset($matches[2]) && $matches[2] != '' && isset($matches[1]) && $matches[1] != '')
13984 + {
13985 + $txmin = intval($matches[1]);
13986 + $txmax = intval($matches[2]);
13987 + $topic_images = mt_rand($txmin, $txmax);
13988 + }
13989 + else
13990 + {
13991 + $topic_images = $topic_images_bck;
13992 + }
13993 + }
13994 + if($heading_image_loc != 'top')
13995 + {
13996 + if(!isset($aiomatic_Main_Settings['no_intro_image']) || trim($aiomatic_Main_Settings['no_intro_image']) != 'on')
13997 + {
13998 + if($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images)
13999 + {
14000 + if($new_post_content !== '')
14001 + {
14002 + $the_image_prompt = $post_title;
14003 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14004 + {
14005 + $ai_command_image = $orig_ai_command_image;
14006 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14007 + {
14008 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14009 + $ai_command_image = array_filter($ai_command_image);
14010 + if(count($ai_command_image) > 0)
14011 + {
14012 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14013 + }
14014 + else
14015 + {
14016 + $ai_command_image = '';
14017 + }
14018 + }
14019 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14020 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14021 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14022 + if(!empty($ai_command_image))
14023 + {
14024 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14025 + }
14026 + else
14027 + {
14028 + $ai_command_image = trim(strip_tags($the_image_prompt));
14029 + }
14030 + $ai_command_image = trim($ai_command_image);
14031 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14032 + {
14033 + $txt_content = aiomatic_get_web_page($ai_command_image);
14034 + if ($txt_content !== FALSE)
14035 + {
14036 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14037 + $txt_content = array_filter($txt_content);
14038 + if(count($txt_content) > 0)
14039 + {
14040 + $txt_content = $txt_content[array_rand($txt_content)];
14041 + if(trim($txt_content) != '')
14042 + {
14043 + $ai_command_image = $txt_content;
14044 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14045 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14046 + }
14047 + }
14048 + }
14049 + }
14050 + if(empty($ai_command_image))
14051 + {
14052 + aiomatic_log_to_file('Empty API image seed expression provided!');
14053 + }
14054 + else
14055 + {
14056 + if(aiomatic_strlen($ai_command_image) > 1000)
14057 + {
14058 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14059 + }
14060 + $the_image_prompt = $ai_command_image;
14061 + }
14062 + }
14063 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14064 + if(!empty($attached_id))
14065 + {
14066 + $additional_attachments[] = $attached_id;
14067 + }
14068 + if($new_img !== false && !in_array($new_img, $added_img_list))
14069 + {
14070 + $new_post_content .= '<br/><img class="yimage_class" src="' . $new_img . '" alt="' . esc_attr($post_title) . '"><br/>';
14071 + $added_img_list[] = $new_img;
14072 + $added_images++;
14073 + }
14074 + }
14075 + }
14076 + }
14077 + }
14078 + if($title_outro != '')
14079 + {
14080 + $my_title_outro = $title_outro;
14081 + $my_title_outro = aiomatic_replace_youtube($my_title_outro, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14082 + $my_title_outro = $spintax->Parse(trim($my_title_outro));
14083 + }
14084 + if(!empty($title_toc))
14085 + {
14086 + $my_title_toc = $title_toc;
14087 + $my_title_toc = aiomatic_replace_youtube($my_title_toc, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14088 + $my_title_toc = $spintax->Parse(trim($my_title_toc));
14089 + }
14090 + if($title_qa != '')
14091 + {
14092 + $my_title_qa = $title_qa;
14093 + $my_title_qa = aiomatic_replace_youtube($my_title_qa, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14094 + $my_title_qa = $spintax->Parse(trim($my_title_qa));
14095 + }
14096 + if($content_prompt != '')
14097 + {
14098 + if($single_content_call == '1')
14099 + {
14100 + $last_section_content = '';
14101 + $current_section = $post_sections;
14102 + $my_post_content = $content_prompt;
14103 + $my_post_content = aiomatic_replace_youtube($my_post_content, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14104 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
14105 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
14106 + if(stristr($run_regex_on, 'content') !== false)
14107 + {
14108 + if ($strip_by_regex_prompts !== '')
14109 + {
14110 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
14111 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
14112 + $xcnt = 0;
14113 + foreach($xstrip_by_regex as $sbr)
14114 + {
14115 + if(isset($xreplace_regex[$xcnt]))
14116 + {
14117 + $repreg = $xreplace_regex[$xcnt];
14118 + }
14119 + else
14120 + {
14121 + $repreg = '';
14122 + }
14123 + $xcnt++;
14124 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
14125 + if($temp_cont !== NULL)
14126 + {
14127 + $my_post_content = $temp_cont;
14128 + }
14129 + }
14130 + }
14131 + }
14132 + if($my_post_content != '')
14133 + {
14134 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based topic list'));
14135 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
14136 + if($written === false)
14137 + {
14138 + aiomatic_log_to_file('Failed to generate topic list for section: ' . $current_section);
14139 + continue;
14140 + }
14141 + else
14142 + {
14143 + if($new_post_content != '')
14144 + {
14145 + $new_post_content .= ' ';
14146 + }
14147 + $new_img_arr = array();
14148 + if($topic_images != '' && is_numeric($topic_images) && intval($topic_images) - 2 > $added_images)
14149 + {
14150 + $howmore = intval($topic_images) - 2;
14151 + for($cont = 0; $cont < $howmore; $cont++)
14152 + {
14153 + if($search_query_repetition == '1')
14154 + {
14155 + $the_image_prompt = $post_title;
14156 + }
14157 + else
14158 + {
14159 + $the_image_prompt = $current_section;
14160 + }
14161 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14162 + {
14163 + $ai_command_image = $orig_ai_command_image;
14164 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14165 + {
14166 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14167 + $ai_command_image = array_filter($ai_command_image);
14168 + if(count($ai_command_image) > 0)
14169 + {
14170 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14171 + }
14172 + else
14173 + {
14174 + $ai_command_image = '';
14175 + }
14176 + }
14177 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14178 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14179 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14180 + if(!empty($ai_command_image))
14181 + {
14182 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14183 + }
14184 + else
14185 + {
14186 + $ai_command_image = trim(strip_tags($the_image_prompt));
14187 + }
14188 + $ai_command_image = trim($ai_command_image);
14189 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14190 + {
14191 + $txt_content = aiomatic_get_web_page($ai_command_image);
14192 + if ($txt_content !== FALSE)
14193 + {
14194 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14195 + $txt_content = array_filter($txt_content);
14196 + if(count($txt_content) > 0)
14197 + {
14198 + $txt_content = $txt_content[array_rand($txt_content)];
14199 + if(trim($txt_content) != '')
14200 + {
14201 + $ai_command_image = $txt_content;
14202 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14203 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14204 + }
14205 + }
14206 + }
14207 + }
14208 + if(empty($ai_command_image))
14209 + {
14210 + aiomatic_log_to_file('Empty API image seed expression provided!');
14211 + }
14212 + else
14213 + {
14214 + if(aiomatic_strlen($ai_command_image) > 1000)
14215 + {
14216 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14217 + }
14218 + $the_image_prompt = $ai_command_image;
14219 + }
14220 + }
14221 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14222 + if(!empty($attached_id))
14223 + {
14224 + $additional_attachments[] = $attached_id;
14225 + }
14226 + if($new_img !== false && !in_array($new_img, $added_img_list))
14227 + {
14228 + $new_post_content .= '<br/><img class="zimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
14229 + $added_img_list[] = $new_img;
14230 + $new_img_arr[] = $new_img;
14231 + $added_images++;
14232 + }
14233 + }
14234 + }
14235 + if(count($new_img_arr) > 0)
14236 + {
14237 + $written_arr = preg_split("/\r\n|\n|\r/", $written);
14238 + if(count($written_arr) > 1)
14239 + {
14240 + foreach($new_img_arr as $new_img_x)
14241 + {
14242 + array_splice($written_arr, wp_rand(0, count($written_arr)), 0, '<br/><img src="' . $new_img_x . '">');
14243 + }
14244 + $written = implode('\r\n', $written_arr);
14245 + }
14246 + }
14247 + $new_vid_arr = array();
14248 + $new_post_content .= $written;
14249 + if($topic_videos == '1')
14250 + {
14251 + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $amatches);
14252 + if(isset($amatches[1]))
14253 + {
14254 + $video_id = $amatches[1];
14255 + if (isset($aiomatic_Main_Settings['player_width']) && $aiomatic_Main_Settings['player_width'] !== '') {
14256 + $vwidth = esc_attr($aiomatic_Main_Settings['player_width']);
14257 + }
14258 + else
14259 + {
14260 + $vwidth = 580;
14261 + }
14262 + if (isset($aiomatic_Main_Settings['player_height']) && $aiomatic_Main_Settings['player_height'] !== '') {
14263 + $vheight = esc_attr($aiomatic_Main_Settings['player_height']);
14264 + }
14265 + else
14266 + {
14267 + $vheight = 380;
14268 + }
14269 + $new_post_content .= '<br/><br/><div class="automaticx-video-container"><iframe allow="autoplay" width="' . $vwidth . '" height="' . $vheight . '" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
14270 + $added_vid_list[] = $video_id;
14271 + $new_vid_arr[] = $new_img;
14272 + $added_videos++;
14273 + }
14274 + }
14275 + if(count($new_vid_arr) > 0)
14276 + {
14277 + $written_arr = preg_split("/\r\n|\n|\r/", $written);
14278 + if(count($written_arr) > 1)
14279 + {
14280 + foreach($new_vid_arr as $new_img_x)
14281 + {
14282 + array_splice($written_arr, wp_rand(0, count($written_arr)), 0, '<br/><img src="' . $new_img_x . '">');
14283 + }
14284 + $written = implode('\r\n', $written_arr);
14285 + }
14286 + }
14287 + }
14288 + }
14289 + else
14290 + {
14291 + aiomatic_log_to_file('Empty processed topic prompt. Nothing to do.');
14292 + }
14293 + }
14294 + else
14295 + {
14296 + if($post_sections != '')
14297 + {
14298 + $post_sections_arr = preg_split('/\r\n|\r|\n/', $post_sections);
14299 + }
14300 + else
14301 + {
14302 + $post_sections_arr = array();
14303 + }
14304 + $toc = '';
14305 + if($enable_toc == '1')
14306 + {
14307 + if(!empty($title_toc))
14308 + {
14309 + if($my_title_toc != '')
14310 + {
14311 + $toc .= '<h2>' . $my_title_toc . '</h2>';
14312 + }
14313 + }
14314 + $toc .= '<ul class="toc-class">';
14315 + foreach($post_sections_arr as $current_section)
14316 + {
14317 + if(!empty($current_section))
14318 + {
14319 + $toc .= '<li><a href="#' . sanitize_title($current_section) . '">' . $current_section . '</a></li>';
14320 + }
14321 + }
14322 + if($title_qa != '')
14323 + {
14324 + if($my_title_qa != '')
14325 + {
14326 + if($qa_prompt != '' && $enable_qa == '1')
14327 + {
14328 + $toc .= '<li><a href="#qa">' . $my_title_qa . '</a></li>';
14329 + }
14330 + }
14331 + }
14332 + if($title_outro != '')
14333 + {
14334 + if($my_title_outro != '')
14335 + {
14336 + if($outro_prompt != '')
14337 + {
14338 + $toc .= '<li><a href="#outro">' . $my_title_outro . '</a></li>';
14339 + }
14340 + }
14341 + }
14342 + $toc .= '</ul>';
14343 + $new_post_content .= '<br/>' . $toc . '<br/>';
14344 + }
14345 + $section_id = 1;
14346 + $last_section_content = '';
14347 + foreach($post_sections_arr as $current_section)
14348 + {
14349 + $current_section = trim($current_section);
14350 + $current_section = trim($current_section, '.;');
14351 + if(empty($current_section))
14352 + {
14353 + continue;
14354 + }
14355 + $my_post_content = $content_prompt;
14356 + $my_post_content = aiomatic_replace_youtube($my_post_content, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14357 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
14358 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
14359 + if(stristr($run_regex_on, 'content') !== false)
14360 + {
14361 + if ($strip_by_regex_prompts !== '')
14362 + {
14363 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
14364 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
14365 + $xcnt = 0;
14366 + foreach($xstrip_by_regex as $sbr)
14367 + {
14368 + if(isset($xreplace_regex[$xcnt]))
14369 + {
14370 + $repreg = $xreplace_regex[$xcnt];
14371 + }
14372 + else
14373 + {
14374 + $repreg = '';
14375 + }
14376 + $xcnt++;
14377 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
14378 + if($temp_cont !== NULL)
14379 + {
14380 + $my_post_content = $temp_cont;
14381 + }
14382 + }
14383 + }
14384 + }
14385 + if($my_post_content != '')
14386 + {
14387 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based content for section ID #' . $section_id));
14388 + $section_id++;
14389 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
14390 + if($written === false)
14391 + {
14392 + aiomatic_log_to_file('Failed to generate content for section: ' . $current_section);
14393 + continue;
14394 + }
14395 + else
14396 + {
14397 + if($heading_image_loc == 'top')
14398 + {
14399 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
14400 + {
14401 + if($new_post_content !== '')
14402 + {
14403 + if($search_query_repetition == '1')
14404 + {
14405 + $the_image_prompt = $post_title;
14406 + }
14407 + else
14408 + {
14409 + $the_image_prompt = $current_section;
14410 + }
14411 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14412 + {
14413 + $ai_command_image = $orig_ai_command_image;
14414 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14415 + {
14416 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14417 + $ai_command_image = array_filter($ai_command_image);
14418 + if(count($ai_command_image) > 0)
14419 + {
14420 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14421 + }
14422 + else
14423 + {
14424 + $ai_command_image = '';
14425 + }
14426 + }
14427 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14428 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14429 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14430 + if(!empty($ai_command_image))
14431 + {
14432 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14433 + }
14434 + else
14435 + {
14436 + $ai_command_image = trim(strip_tags($the_image_prompt));
14437 + }
14438 + $ai_command_image = trim($ai_command_image);
14439 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14440 + {
14441 + $txt_content = aiomatic_get_web_page($ai_command_image);
14442 + if ($txt_content !== FALSE)
14443 + {
14444 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14445 + $txt_content = array_filter($txt_content);
14446 + if(count($txt_content) > 0)
14447 + {
14448 + $txt_content = $txt_content[array_rand($txt_content)];
14449 + if(trim($txt_content) != '')
14450 + {
14451 + $ai_command_image = $txt_content;
14452 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14453 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14454 + }
14455 + }
14456 + }
14457 + }
14458 + if(empty($ai_command_image))
14459 + {
14460 + aiomatic_log_to_file('Empty API image seed expression provided!');
14461 + }
14462 + else
14463 + {
14464 + if(aiomatic_strlen($ai_command_image) > 1000)
14465 + {
14466 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14467 + }
14468 + $the_image_prompt = $ai_command_image;
14469 + }
14470 + }
14471 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14472 + if(!empty($attached_id))
14473 + {
14474 + $additional_attachments[] = $attached_id;
14475 + }
14476 + if($new_img !== false && !in_array($new_img, $added_img_list))
14477 + {
14478 + $new_post_content .= '<br/><img class="qimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
14479 + $added_img_list[] = $new_img;
14480 + $added_images++;
14481 + }
14482 + }
14483 + }
14484 + }
14485 + $written = str_ireplace($current_section, '', $written);
14486 + preg_match_all('#\d+\.\s*([\s\S]*)#', $current_section, $mxatches);
14487 + if(isset($mxatches[1][0]))
14488 + {
14489 + $written = str_ireplace(trim($mxatches[1][0]), '', $written);
14490 + $written = str_ireplace(str_replace(['"', '\''], '', $mxatches[1][0]), '', $written);
14491 + }
14492 + $written = str_ireplace('<h2></h2>', '', $written);
14493 + $written = str_ireplace('<h3></h3>', '', $written);
14494 + if($sections_role == 'h2')
14495 + {
14496 + $new_post_content .= '<h2 id="' . sanitize_title($current_section) . '">' . $current_section . '</h2>';
14497 + }
14498 + elseif($sections_role == 'h3')
14499 + {
14500 + $new_post_content .= '<h3 id="' . sanitize_title($current_section) . '">' . $current_section . '</h3>';
14501 + }
14502 + elseif($sections_role == 'b')
14503 + {
14504 + $new_post_content .= '<b id="' . sanitize_title($current_section) . '">' . $current_section . '</b>';
14505 + }
14506 + elseif($sections_role == 'i')
14507 + {
14508 + $new_post_content .= '<i id="' . sanitize_title($current_section) . '">' . $current_section . '</i>';
14509 + }
14510 + elseif($sections_role == 'bi')
14511 + {
14512 + $new_post_content .= '<b><i id="' . sanitize_title($current_section) . '">' . $current_section . '</i></b>';
14513 + }
14514 + elseif($sections_role == 'p')
14515 + {
14516 + $new_post_content .= '<p id="' . sanitize_title($current_section) . '">' . $current_section . '</p>';
14517 + }
14518 + elseif($sections_role == 'x')
14519 + {
14520 + $new_post_content .= '<br/><span id="' . sanitize_title($current_section) . '">' . $current_section . '</span><br/>';
14521 + }
14522 + if($heading_image_loc == 'heading')
14523 + {
14524 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
14525 + {
14526 + if($new_post_content !== '')
14527 + {
14528 + if($search_query_repetition == '1')
14529 + {
14530 + $the_image_prompt = $post_title;
14531 + }
14532 + else
14533 + {
14534 + $the_image_prompt = $current_section;
14535 + }
14536 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14537 + {
14538 + $ai_command_image = $orig_ai_command_image;
14539 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14540 + {
14541 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14542 + $ai_command_image = array_filter($ai_command_image);
14543 + if(count($ai_command_image) > 0)
14544 + {
14545 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14546 + }
14547 + else
14548 + {
14549 + $ai_command_image = '';
14550 + }
14551 + }
14552 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14553 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14554 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14555 + if(!empty($ai_command_image))
14556 + {
14557 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14558 + }
14559 + else
14560 + {
14561 + $ai_command_image = trim(strip_tags($the_image_prompt));
14562 + }
14563 + $ai_command_image = trim($ai_command_image);
14564 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14565 + {
14566 + $txt_content = aiomatic_get_web_page($ai_command_image);
14567 + if ($txt_content !== FALSE)
14568 + {
14569 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14570 + $txt_content = array_filter($txt_content);
14571 + if(count($txt_content) > 0)
14572 + {
14573 + $txt_content = $txt_content[array_rand($txt_content)];
14574 + if(trim($txt_content) != '')
14575 + {
14576 + $ai_command_image = $txt_content;
14577 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14578 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14579 + }
14580 + }
14581 + }
14582 + }
14583 + if(empty($ai_command_image))
14584 + {
14585 + aiomatic_log_to_file('Empty API image seed expression provided!');
14586 + }
14587 + else
14588 + {
14589 + if(aiomatic_strlen($ai_command_image) > 1000)
14590 + {
14591 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14592 + }
14593 + $the_image_prompt = $ai_command_image;
14594 + }
14595 + }
14596 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14597 + if(!empty($attached_id))
14598 + {
14599 + $additional_attachments[] = $attached_id;
14600 + }
14601 + if($new_img !== false && !in_array($new_img, $added_img_list))
14602 + {
14603 + $new_post_content .= '<br/><img class="qimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
14604 + $added_img_list[] = $new_img;
14605 + $added_images++;
14606 + }
14607 + }
14608 + }
14609 + }
14610 + $last_section_content = $written;
14611 + $new_post_content .= $written;
14612 + }
14613 + if($heading_image_loc == 'bottom')
14614 + {
14615 + if($img_all_headings == '1' || ($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images))
14616 + {
14617 + if($new_post_content !== '')
14618 + {
14619 + if($search_query_repetition == '1')
14620 + {
14621 + $the_image_prompt = $post_title;
14622 + }
14623 + else
14624 + {
14625 + $the_image_prompt = $current_section;
14626 + }
14627 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14628 + {
14629 + $ai_command_image = $orig_ai_command_image;
14630 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14631 + {
14632 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14633 + $ai_command_image = array_filter($ai_command_image);
14634 + if(count($ai_command_image) > 0)
14635 + {
14636 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14637 + }
14638 + else
14639 + {
14640 + $ai_command_image = '';
14641 + }
14642 + }
14643 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14644 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14645 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14646 + if(!empty($ai_command_image))
14647 + {
14648 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14649 + }
14650 + else
14651 + {
14652 + $ai_command_image = trim(strip_tags($the_image_prompt));
14653 + }
14654 + $ai_command_image = trim($ai_command_image);
14655 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14656 + {
14657 + $txt_content = aiomatic_get_web_page($ai_command_image);
14658 + if ($txt_content !== FALSE)
14659 + {
14660 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14661 + $txt_content = array_filter($txt_content);
14662 + if(count($txt_content) > 0)
14663 + {
14664 + $txt_content = $txt_content[array_rand($txt_content)];
14665 + if(trim($txt_content) != '')
14666 + {
14667 + $ai_command_image = $txt_content;
14668 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14669 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14670 + }
14671 + }
14672 + }
14673 + }
14674 + if(empty($ai_command_image))
14675 + {
14676 + aiomatic_log_to_file('Empty API image seed expression provided!');
14677 + }
14678 + else
14679 + {
14680 + if(aiomatic_strlen($ai_command_image) > 1000)
14681 + {
14682 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14683 + }
14684 + $the_image_prompt = $ai_command_image;
14685 + }
14686 + }
14687 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14688 + if(!empty($attached_id))
14689 + {
14690 + $additional_attachments[] = $attached_id;
14691 + }
14692 + if($new_img !== false && !in_array($new_img, $added_img_list))
14693 + {
14694 + $new_post_content .= '<br/><img class="qimage_class" src="' . $new_img . '" alt="' . esc_attr($current_section) . '"><br/>';
14695 + $added_img_list[] = $new_img;
14696 + $added_images++;
14697 + }
14698 + }
14699 + }
14700 + }
14701 + }
14702 + else
14703 + {
14704 + aiomatic_log_to_file('Empty processed content prompt. Nothing to do.');
14705 + }
14706 + }
14707 + }
14708 + }
14709 + if($enable_qa == '1')
14710 + {
14711 + if($qa_prompt != '')
14712 + {
14713 + $my_qa_prompt = $qa_prompt;
14714 + $my_qa_prompt = aiomatic_replace_youtube($my_qa_prompt, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14715 + if(stristr($run_regex_on, 'qa') !== false)
14716 + {
14717 + if ($strip_by_regex_prompts !== '')
14718 + {
14719 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
14720 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
14721 + $xcnt = 0;
14722 + foreach($xstrip_by_regex as $sbr)
14723 + {
14724 + if(isset($xreplace_regex[$xcnt]))
14725 + {
14726 + $repreg = $xreplace_regex[$xcnt];
14727 + }
14728 + else
14729 + {
14730 + $repreg = '';
14731 + }
14732 + $xcnt++;
14733 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_qa_prompt);
14734 + if($temp_cont !== NULL)
14735 + {
14736 + $my_qa_prompt = $temp_cont;
14737 + }
14738 + }
14739 + }
14740 + }
14741 + if($my_qa_prompt != '')
14742 + {
14743 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based qa'));
14744 + $written = aiomatic_write_content($my_qa_prompt, $topic_qa_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'qa', 'user', $assistant_id, $thread_id, $store_data);
14745 + if($written === false)
14746 + {
14747 + aiomatic_log_to_file('Failed to generate Q&A for topic: ' . $current_section);
14748 + continue;
14749 + }
14750 + else
14751 + {
14752 + if($new_post_content != '')
14753 + {
14754 + $new_post_content .= ' ';
14755 + }
14756 + if($my_title_qa !== '')
14757 + {
14758 + if($sections_role == 'h2')
14759 + {
14760 + $new_post_content .= '<h2 id="qa">' . $my_title_qa . '</h2>';
14761 + }
14762 + elseif($sections_role == 'h3')
14763 + {
14764 + $new_post_content .= '<h3 id="qa">' . $my_title_qa . '</h3>';
14765 + }
14766 + elseif($sections_role == 'b')
14767 + {
14768 + $new_post_content .= '<b id="qa">' . $my_title_qa . '</b>';
14769 + }
14770 + elseif($sections_role == 'i')
14771 + {
14772 + $new_post_content .= '<i id="qa">' . $my_title_qa . '</i>';
14773 + }
14774 + elseif($sections_role == 'bi')
14775 + {
14776 + $new_post_content .= '<b><i id="qa">' . $my_title_qa . '</i></b>';
14777 + }
14778 + elseif($sections_role == 'p')
14779 + {
14780 + $new_post_content .= '<p id="qa">' . $my_title_qa . '</p>';
14781 + }
14782 + elseif($sections_role == 'x')
14783 + {
14784 + $new_post_content .= '<br/><span id="qa">' . $my_title_qa . '</span><br/>';
14785 + }
14786 + else
14787 + {
14788 + $new_post_content .= '<br/>' . $my_title_qa . '<br/>';
14789 + }
14790 + }
14791 + $new_post_content .= $written;
14792 + }
14793 + }
14794 + else
14795 + {
14796 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
14797 + }
14798 + }
14799 + }
14800 + if($outro_prompt != '')
14801 + {
14802 + $my_outro_prompt = $outro_prompt;
14803 + $my_outro_prompt = aiomatic_replace_youtube($my_outro_prompt, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
14804 + if(stristr($run_regex_on, 'outro') !== false)
14805 + {
14806 + if ($strip_by_regex_prompts !== '')
14807 + {
14808 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
14809 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
14810 + $xcnt = 0;
14811 + foreach($xstrip_by_regex as $sbr)
14812 + {
14813 + if(isset($xreplace_regex[$xcnt]))
14814 + {
14815 + $repreg = $xreplace_regex[$xcnt];
14816 + }
14817 + else
14818 + {
14819 + $repreg = '';
14820 + }
14821 + $xcnt++;
14822 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_outro_prompt);
14823 + if($temp_cont !== NULL)
14824 + {
14825 + $my_outro_prompt = $temp_cont;
14826 + }
14827 + }
14828 + }
14829 + }
14830 + if($my_outro_prompt != '')
14831 + {
14832 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based outro'));
14833 + $written = aiomatic_write_content($my_outro_prompt, $topic_outro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'outro', 'user', $assistant_id, $thread_id, $store_data);
14834 + if($written === false)
14835 + {
14836 + aiomatic_log_to_file('Failed to generate outro for section: ' . $current_section);
14837 + continue;
14838 + }
14839 + else
14840 + {
14841 + if($new_post_content != '')
14842 + {
14843 + $new_post_content .= ' ';
14844 + }
14845 + if($title_outro !== '')
14846 + {
14847 + if($my_title_outro != '')
14848 + {
14849 + if($sections_role == 'h2')
14850 + {
14851 + $new_post_content .= '<h2 id="outro">' . $my_title_outro . '</h2>';
14852 + }
14853 + elseif($sections_role == 'h3')
14854 + {
14855 + $new_post_content .= '<h3 id="outro">' . $my_title_outro . '</h3>';
14856 + }
14857 + elseif($sections_role == 'b')
14858 + {
14859 + $new_post_content .= '<b id="outro">' . $my_title_outro . '</b>';
14860 + }
14861 + elseif($sections_role == 'i')
14862 + {
14863 + $new_post_content .= '<i id="outro">' . $my_title_outro . '</i>';
14864 + }
14865 + elseif($sections_role == 'bi')
14866 + {
14867 + $new_post_content .= '<b><i id="outro">' . $my_title_outro . '</i></b>';
14868 + }
14869 + elseif($sections_role == 'p')
14870 + {
14871 + $new_post_content .= '<p id="outro">' . $my_title_outro . '</p>';
14872 + }
14873 + elseif($sections_role == 'x')
14874 + {
14875 + $new_post_content .= '<br/><span id="outro">' . $my_title_outro . '</span><br/>';
14876 + }
14877 + else
14878 + {
14879 + $new_post_content .= '<br/>' . $my_title_outro . '<br/>';
14880 + }
14881 + }
14882 + }
14883 + else
14884 + {
14885 + $new_post_content .= '<br/><br/>';
14886 + }
14887 + $new_post_content .= $written;
14888 + }
14889 + }
14890 + else
14891 + {
14892 + aiomatic_log_to_file('Empty processed outro prompt. Nothing to do.');
14893 + }
14894 + }
14895 + if($topic_images != '' && is_numeric($topic_images) && $topic_images > $added_images)
14896 + {
14897 + if($new_post_content !== '')
14898 + {
14899 + $the_image_prompt = $post_title;
14900 + if($enable_ai_images != '0' && $enable_ai_images != '3')
14901 + {
14902 + $ai_command_image = $orig_ai_command_image;
14903 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
14904 + {
14905 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
14906 + $ai_command_image = array_filter($ai_command_image);
14907 + if(count($ai_command_image) > 0)
14908 + {
14909 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
14910 + }
14911 + else
14912 + {
14913 + $ai_command_image = '';
14914 + }
14915 + }
14916 + $za_post_title = aiomatic_replaceContentShortcodes($the_image_prompt, $img_attr, $ai_command);
14917 + $ai_command_image = aiomatic_replacetopics($ai_command_image, $za_post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
14918 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14919 + if(!empty($ai_command_image))
14920 + {
14921 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14922 + }
14923 + else
14924 + {
14925 + $ai_command_image = trim(strip_tags($the_image_prompt));
14926 + }
14927 + $ai_command_image = trim($ai_command_image);
14928 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
14929 + {
14930 + $txt_content = aiomatic_get_web_page($ai_command_image);
14931 + if ($txt_content !== FALSE)
14932 + {
14933 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
14934 + $txt_content = array_filter($txt_content);
14935 + if(count($txt_content) > 0)
14936 + {
14937 + $txt_content = $txt_content[array_rand($txt_content)];
14938 + if(trim($txt_content) != '')
14939 + {
14940 + $ai_command_image = $txt_content;
14941 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
14942 + $ai_command_image = aiomatic_replaceAIPostShortcodes($ai_command_image, $post_link, $the_image_prompt, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
14943 + }
14944 + }
14945 + }
14946 + }
14947 + if(empty($ai_command_image))
14948 + {
14949 + aiomatic_log_to_file('Empty API image seed expression provided!');
14950 + }
14951 + else
14952 + {
14953 + if(aiomatic_strlen($ai_command_image) > 1000)
14954 + {
14955 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
14956 + }
14957 + $the_image_prompt = $ai_command_image;
14958 + }
14959 + }
14960 + $new_img = aiomatic_get_royalty_free_image($the_image_prompt, $enable_ai_images, $image_size, $images_arr, $token, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $custom_shortcodes, $global_prepend, $global_append, $image_url, $attached_id, $raw_img_list, $default_cat_list, $image_model);
14961 + if(!empty($attached_id))
14962 + {
14963 + $additional_attachments[] = $attached_id;
14964 + }
14965 + if($new_img !== false && !in_array($new_img, $added_img_list))
14966 + {
14967 + $new_post_content .= '<br/><img class="wimage_class" src="' . $new_img . '" alt="' . esc_attr($post_title) . '"><br/>';
14968 + $added_img_list[] = $new_img;
14969 + $added_images++;
14970 + }
14971 + }
14972 + }
14973 + if($topic_videos == '1')
14974 + {
14975 + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $amatches);
14976 + if(isset($amatches[1]))
14977 + {
14978 + $video_id = $amatches[1];
14979 + if (isset($aiomatic_Main_Settings['player_width']) && $aiomatic_Main_Settings['player_width'] !== '') {
14980 + $vwidth = esc_attr($aiomatic_Main_Settings['player_width']);
14981 + }
14982 + else
14983 + {
14984 + $vwidth = 580;
14985 + }
14986 + if (isset($aiomatic_Main_Settings['player_height']) && $aiomatic_Main_Settings['player_height'] !== '') {
14987 + $vheight = esc_attr($aiomatic_Main_Settings['player_height']);
14988 + }
14989 + else
14990 + {
14991 + $vheight = 380;
14992 + }
14993 + $new_post_content .= '<br/><br/><div class="automaticx-video-container"><iframe allow="autoplay" width="' . $vwidth . '" height="' . $vheight . '" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
14994 + $added_vid_list[] = $video_id;
14995 + $new_vid_arr[] = $new_img;
14996 + $added_videos++;
14997 + }
14998 + }
14999 + if($new_post_content == '')
15000 + {
15001 + aiomatic_log_to_file("Warning, empty post content because of empty content prompt!");
15002 + }
15003 +
15004 + $new_post_excerpt = '';
15005 + $current_section = $post_sections;
15006 + if($excerpt_prompt != '')
15007 + {
15008 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15009 + {
15010 + $excerpt_prompt_arr = preg_split('/\r\n|\r|\n/', $excerpt_prompt);
15011 + $my_post_excerpt = $excerpt_prompt_arr[array_rand($excerpt_prompt_arr)];
15012 + }
15013 + else
15014 + {
15015 + $my_post_excerpt = $excerpt_prompt;
15016 + }
15017 + $my_post_excerpt = aiomatic_replace_youtube($my_post_excerpt, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
15018 + if(stristr($run_regex_on, 'excerpt') !== false)
15019 + {
15020 + if ($strip_by_regex_prompts !== '')
15021 + {
15022 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
15023 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
15024 + $xcnt = 0;
15025 + foreach($xstrip_by_regex as $sbr)
15026 + {
15027 + if(isset($xreplace_regex[$xcnt]))
15028 + {
15029 + $repreg = $xreplace_regex[$xcnt];
15030 + }
15031 + else
15032 + {
15033 + $repreg = '';
15034 + }
15035 + $xcnt++;
15036 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_excerpt);
15037 + if($temp_cont !== NULL)
15038 + {
15039 + $my_post_excerpt = $temp_cont;
15040 + }
15041 + }
15042 + }
15043 + }
15044 + if($my_post_excerpt != '')
15045 + {
15046 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based excerpt'));
15047 + $written = aiomatic_write_content($my_post_excerpt, $topic_excerpt_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'excerpt', 'user', $assistant_id, $thread_id, $store_data);
15048 + if($written === false)
15049 + {
15050 + aiomatic_log_to_file('Failed to generate excerpt!');
15051 + }
15052 + else
15053 + {
15054 + $new_post_excerpt = $written;
15055 + }
15056 + }
15057 + else
15058 + {
15059 + aiomatic_log_to_file('Empty processed excerpt prompt. Nothing to do.');
15060 + }
15061 + }
15062 +
15063 + $get_img = '';
15064 + if($royalty_free == '1')
15065 + {
15066 + if($enable_ai_images == '1')
15067 + {
15068 + $query_words = $post_title;
15069 + if($image_query == '')
15070 + {
15071 + $image_query = $temp_post;
15072 + }
15073 + if($orig_ai_command_image == '')
15074 + {
15075 + $orig_ai_command_image = $image_query;
15076 + }
15077 + if($orig_ai_command_image != '')
15078 + {
15079 + $ai_command_image = $orig_ai_command_image;
15080 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15081 + {
15082 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15083 + $ai_command_image = array_filter($ai_command_image);
15084 + if(count($ai_command_image) > 0)
15085 + {
15086 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15087 + }
15088 + else
15089 + {
15090 + $ai_command_image = '';
15091 + }
15092 + }
15093 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15094 + if(!empty($ai_command_image))
15095 + {
15096 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15097 + }
15098 + else
15099 + {
15100 + $ai_command_image = trim(strip_tags($post_title));
15101 + }
15102 + $ai_command_image = trim($ai_command_image);
15103 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15104 + {
15105 + $txt_content = aiomatic_get_web_page($ai_command_image);
15106 + if ($txt_content !== FALSE)
15107 + {
15108 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15109 + $txt_content = array_filter($txt_content);
15110 + if(count($txt_content) > 0)
15111 + {
15112 + $txt_content = $txt_content[array_rand($txt_content)];
15113 + if(trim($txt_content) != '')
15114 + {
15115 + $ai_command_image = $txt_content;
15116 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15117 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15118 + }
15119 + }
15120 + }
15121 + }
15122 + if(empty($ai_command_image))
15123 + {
15124 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15125 + }
15126 + else
15127 + {
15128 + if(aiomatic_strlen($ai_command_image) > 1000)
15129 + {
15130 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
15131 + }
15132 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15133 + {
15134 + $api_service = aiomatic_get_api_service($token, $image_model);
15135 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15136 + }
15137 + $skip_this_copy = true;
15138 + if(!function_exists('is_plugin_active'))
15139 + {
15140 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15141 + }
15142 + 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')
15143 + {
15144 + $skip_this_copy = false;
15145 + }
15146 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
15147 + {
15148 + $skip_this_copy = false;
15149 + }
15150 + $aierror = '';
15151 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
15152 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
15153 + if($get_img !== false)
15154 + {
15155 + foreach($get_img as $tmpimg)
15156 + {
15157 + $get_img = $tmpimg;
15158 + break;
15159 + }
15160 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15161 + {
15162 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
15163 + }
15164 + }
15165 + else
15166 + {
15167 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
15168 + $get_img = '';
15169 + }
15170 + }
15171 + }
15172 + else
15173 + {
15174 + aiomatic_log_to_file('Empty AI featured image query entered.');
15175 + }
15176 + }
15177 + elseif($enable_ai_images == '2')
15178 + {
15179 + $query_words = $post_title;
15180 + if($image_query == '')
15181 + {
15182 + $image_query = $temp_post;
15183 + }
15184 + if($orig_ai_command_image == '')
15185 + {
15186 + $orig_ai_command_image = $image_query;
15187 + }
15188 + if($orig_ai_command_image != '')
15189 + {
15190 + $ai_command_image = $orig_ai_command_image;
15191 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15192 + {
15193 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15194 + $ai_command_image = array_filter($ai_command_image);
15195 + if(count($ai_command_image) > 0)
15196 + {
15197 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15198 + }
15199 + else
15200 + {
15201 + $ai_command_image = '';
15202 + }
15203 + }
15204 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15205 + if(!empty($ai_command_image))
15206 + {
15207 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15208 + }
15209 + else
15210 + {
15211 + $ai_command_image = trim(strip_tags($post_title));
15212 + }
15213 + $ai_command_image = trim($ai_command_image);
15214 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15215 + {
15216 + $txt_content = aiomatic_get_web_page($ai_command_image);
15217 + if ($txt_content !== FALSE)
15218 + {
15219 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15220 + $txt_content = array_filter($txt_content);
15221 + if(count($txt_content) > 0)
15222 + {
15223 + $txt_content = $txt_content[array_rand($txt_content)];
15224 + if(trim($txt_content) != '')
15225 + {
15226 + $ai_command_image = $txt_content;
15227 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15228 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15229 + }
15230 + }
15231 + }
15232 + }
15233 + if(empty($ai_command_image))
15234 + {
15235 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15236 + }
15237 + else
15238 + {
15239 + if(aiomatic_strlen($ai_command_image) > 2000)
15240 + {
15241 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
15242 + }
15243 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15244 + {
15245 + $api_service = 'Stability.AI';
15246 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15247 + }
15248 + if($image_size == '256x256')
15249 + {
15250 + $width = '512';
15251 + $height = '512';
15252 + }
15253 + elseif($image_size == '512x512')
15254 + {
15255 + $width = '512';
15256 + $height = '512';
15257 + }
15258 + elseif($image_size == '1024x1024')
15259 + {
15260 + $width = '1024';
15261 + $height = '1024';
15262 + }
15263 + else
15264 + {
15265 + $width = '512';
15266 + $height = '512';
15267 + }
15268 + $skip_this_copy = true;
15269 + if(!function_exists('is_plugin_active'))
15270 + {
15271 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15272 + }
15273 + 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')
15274 + {
15275 + $skip_this_copy = false;
15276 + }
15277 + $ierror = '';
15278 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based Stable Diffusion image'));
15279 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
15280 + if($get_img !== false)
15281 + {
15282 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15283 + {
15284 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
15285 + }
15286 + }
15287 + else
15288 + {
15289 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
15290 + $get_img = '';
15291 + }
15292 + }
15293 + }
15294 + else
15295 + {
15296 + aiomatic_log_to_file('Empty AI featured image query entered.');
15297 + }
15298 + }
15299 + elseif($enable_ai_images == '7')
15300 + {
15301 + $query_words = $post_title;
15302 + if($image_query == '')
15303 + {
15304 + $image_query = $temp_post;
15305 + }
15306 + if($orig_ai_command_image == '')
15307 + {
15308 + $orig_ai_command_image = $image_query;
15309 + }
15310 + if($orig_ai_command_image != '')
15311 + {
15312 + $ai_command_image = $orig_ai_command_image;
15313 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15314 + {
15315 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15316 + $ai_command_image = array_filter($ai_command_image);
15317 + if(count($ai_command_image) > 0)
15318 + {
15319 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15320 + }
15321 + else
15322 + {
15323 + $ai_command_image = '';
15324 + }
15325 + }
15326 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15327 + if(!empty($ai_command_image))
15328 + {
15329 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15330 + }
15331 + else
15332 + {
15333 + $ai_command_image = trim(strip_tags($post_title));
15334 + }
15335 + $ai_command_image = trim($ai_command_image);
15336 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15337 + {
15338 + $txt_content = aiomatic_get_web_page($ai_command_image);
15339 + if ($txt_content !== FALSE)
15340 + {
15341 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15342 + $txt_content = array_filter($txt_content);
15343 + if(count($txt_content) > 0)
15344 + {
15345 + $txt_content = $txt_content[array_rand($txt_content)];
15346 + if(trim($txt_content) != '')
15347 + {
15348 + $ai_command_image = $txt_content;
15349 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15350 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15351 + }
15352 + }
15353 + }
15354 + }
15355 + if(empty($ai_command_image))
15356 + {
15357 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15358 + }
15359 + else
15360 + {
15361 + if(aiomatic_strlen($ai_command_image) > 2000)
15362 + {
15363 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
15364 + }
15365 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15366 + {
15367 + $api_service = 'Google';
15368 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15369 + }
15370 + $skip_this_copy = true;
15371 + if(!function_exists('is_plugin_active'))
15372 + {
15373 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15374 + }
15375 + 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')
15376 + {
15377 + $skip_this_copy = false;
15378 + }
15379 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
15380 + {
15381 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
15382 + }
15383 + else
15384 + {
15385 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
15386 + }
15387 + $ierror = '';
15388 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based Google image'));
15389 + $get_img = aiomatic_generate_image_google($ai_command_image, $model, 'featuredGoogleImage', false, $ierror, false);
15390 + if($get_img !== false)
15391 + {
15392 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15393 + {
15394 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
15395 + }
15396 + }
15397 + else
15398 + {
15399 + aiomatic_log_to_file('Failed to generate Google featured image: ' . $ierror);
15400 + $get_img = '';
15401 + }
15402 + }
15403 + }
15404 + else
15405 + {
15406 + aiomatic_log_to_file('Empty AI featured image query entered.');
15407 + }
15408 + }
15409 + elseif($enable_ai_images == '4')
15410 + {
15411 + $query_words = $post_title;
15412 + if($image_query == '')
15413 + {
15414 + $image_query = $temp_post;
15415 + }
15416 + if($orig_ai_command_image == '')
15417 + {
15418 + $orig_ai_command_image = $image_query;
15419 + }
15420 + if($orig_ai_command_image != '')
15421 + {
15422 + $ai_command_image = $orig_ai_command_image;
15423 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15424 + {
15425 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15426 + $ai_command_image = array_filter($ai_command_image);
15427 + if(count($ai_command_image) > 0)
15428 + {
15429 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15430 + }
15431 + else
15432 + {
15433 + $ai_command_image = '';
15434 + }
15435 + }
15436 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15437 + if(!empty($ai_command_image))
15438 + {
15439 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15440 + }
15441 + else
15442 + {
15443 + $ai_command_image = trim(strip_tags($post_title));
15444 + }
15445 + $ai_command_image = trim($ai_command_image);
15446 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15447 + {
15448 + $txt_content = aiomatic_get_web_page($ai_command_image);
15449 + if ($txt_content !== FALSE)
15450 + {
15451 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15452 + $txt_content = array_filter($txt_content);
15453 + if(count($txt_content) > 0)
15454 + {
15455 + $txt_content = $txt_content[array_rand($txt_content)];
15456 + if(trim($txt_content) != '')
15457 + {
15458 + $ai_command_image = $txt_content;
15459 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15460 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15461 + }
15462 + }
15463 + }
15464 + }
15465 + if(empty($ai_command_image))
15466 + {
15467 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15468 + }
15469 + else
15470 + {
15471 + if(aiomatic_strlen($ai_command_image) > 2000)
15472 + {
15473 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
15474 + }
15475 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15476 + {
15477 + $api_service = 'Midjourney';
15478 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15479 + }
15480 + if($image_size == '256x256')
15481 + {
15482 + $width = '512';
15483 + $height = '512';
15484 + }
15485 + elseif($image_size == '512x512')
15486 + {
15487 + $width = '512';
15488 + $height = '512';
15489 + }
15490 + elseif($image_size == '1024x1024')
15491 + {
15492 + $width = '1024';
15493 + $height = '1024';
15494 + }
15495 + elseif($image_size == '1792x1024')
15496 + {
15497 + $width = '1792';
15498 + $height = '1024';
15499 + }
15500 + elseif($image_size == '1024x1792')
15501 + {
15502 + $width = '1024';
15503 + $height = '1792';
15504 + }
15505 + else
15506 + {
15507 + $width = '512';
15508 + $height = '512';
15509 + }
15510 + $skip_this_copy = true;
15511 + if(!function_exists('is_plugin_active'))
15512 + {
15513 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15514 + }
15515 + 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')
15516 + {
15517 + $skip_this_copy = false;
15518 + }
15519 + $ierror = '';
15520 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based Midjourney image'));
15521 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
15522 + if($get_img !== false)
15523 + {
15524 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15525 + {
15526 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
15527 + }
15528 + }
15529 + else
15530 + {
15531 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
15532 + $get_img = '';
15533 + }
15534 + }
15535 + }
15536 + else
15537 + {
15538 + aiomatic_log_to_file('Empty AI featured image query entered.');
15539 + }
15540 + }
15541 + elseif($enable_ai_images == '5')
15542 + {
15543 + $query_words = $post_title;
15544 + if($image_query == '')
15545 + {
15546 + $image_query = $temp_post;
15547 + }
15548 + if($orig_ai_command_image == '')
15549 + {
15550 + $orig_ai_command_image = $image_query;
15551 + }
15552 + if($orig_ai_command_image != '')
15553 + {
15554 + $ai_command_image = $orig_ai_command_image;
15555 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15556 + {
15557 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15558 + $ai_command_image = array_filter($ai_command_image);
15559 + if(count($ai_command_image) > 0)
15560 + {
15561 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15562 + }
15563 + else
15564 + {
15565 + $ai_command_image = '';
15566 + }
15567 + }
15568 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15569 + if(!empty($ai_command_image))
15570 + {
15571 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15572 + }
15573 + else
15574 + {
15575 + $ai_command_image = trim(strip_tags($post_title));
15576 + }
15577 + $ai_command_image = trim($ai_command_image);
15578 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15579 + {
15580 + $txt_content = aiomatic_get_web_page($ai_command_image);
15581 + if ($txt_content !== FALSE)
15582 + {
15583 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15584 + $txt_content = array_filter($txt_content);
15585 + if(count($txt_content) > 0)
15586 + {
15587 + $txt_content = $txt_content[array_rand($txt_content)];
15588 + if(trim($txt_content) != '')
15589 + {
15590 + $ai_command_image = $txt_content;
15591 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15592 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15593 + }
15594 + }
15595 + }
15596 + }
15597 + if(empty($ai_command_image))
15598 + {
15599 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15600 + }
15601 + else
15602 + {
15603 + if(aiomatic_strlen($ai_command_image) > 2000)
15604 + {
15605 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
15606 + }
15607 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15608 + {
15609 + $api_service = 'Replicate';
15610 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15611 + }
15612 + if($image_size == '256x256')
15613 + {
15614 + $width = '512';
15615 + $height = '512';
15616 + }
15617 + elseif($image_size == '512x512')
15618 + {
15619 + $width = '512';
15620 + $height = '512';
15621 + }
15622 + elseif($image_size == '1024x1024')
15623 + {
15624 + $width = '1024';
15625 + $height = '1024';
15626 + }
15627 + elseif($image_size == '1792x1024')
15628 + {
15629 + $width = '1792';
15630 + $height = '1024';
15631 + }
15632 + elseif($image_size == '1024x1792')
15633 + {
15634 + $width = '1024';
15635 + $height = '1792';
15636 + }
15637 + else
15638 + {
15639 + $width = '512';
15640 + $height = '512';
15641 + }
15642 + $skip_this_copy = true;
15643 + if(!function_exists('is_plugin_active'))
15644 + {
15645 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15646 + }
15647 + 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')
15648 + {
15649 + $skip_this_copy = false;
15650 + }
15651 + $ierror = '';
15652 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based Replicate image'));
15653 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredReplicateImage', $skip_this_copy, $ierror);
15654 + if($get_img !== false)
15655 + {
15656 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15657 + {
15658 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
15659 + }
15660 + }
15661 + else
15662 + {
15663 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
15664 + $get_img = '';
15665 + }
15666 + }
15667 + }
15668 + else
15669 + {
15670 + aiomatic_log_to_file('Empty AI featured image query entered.');
15671 + }
15672 + }
15673 + elseif($enable_ai_images == '6')
15674 + {
15675 + $query_words = $post_title;
15676 + if($image_query == '')
15677 + {
15678 + $image_query = $temp_post;
15679 + }
15680 + if($orig_ai_command_image == '')
15681 + {
15682 + $orig_ai_command_image = $image_query;
15683 + }
15684 + if($orig_ai_command_image != '')
15685 + {
15686 + $ai_command_image = $orig_ai_command_image;
15687 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15688 + {
15689 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15690 + $ai_command_image = array_filter($ai_command_image);
15691 + if(count($ai_command_image) > 0)
15692 + {
15693 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15694 + }
15695 + else
15696 + {
15697 + $ai_command_image = '';
15698 + }
15699 + }
15700 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15701 + if(!empty($ai_command_image))
15702 + {
15703 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15704 + }
15705 + else
15706 + {
15707 + $ai_command_image = trim(strip_tags($post_title));
15708 + }
15709 + $ai_command_image = trim($ai_command_image);
15710 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15711 + {
15712 + $txt_content = aiomatic_get_web_page($ai_command_image);
15713 + if ($txt_content !== FALSE)
15714 + {
15715 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15716 + $txt_content = array_filter($txt_content);
15717 + if(count($txt_content) > 0)
15718 + {
15719 + $txt_content = $txt_content[array_rand($txt_content)];
15720 + if(trim($txt_content) != '')
15721 + {
15722 + $ai_command_image = $txt_content;
15723 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15724 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15725 + }
15726 + }
15727 + }
15728 + }
15729 + if(empty($ai_command_image))
15730 + {
15731 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
15732 + }
15733 + else
15734 + {
15735 + if(aiomatic_strlen($ai_command_image) > 2000)
15736 + {
15737 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
15738 + }
15739 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15740 + {
15741 + $api_service = 'Ideogram';
15742 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
15743 + }
15744 + if($image_size == '256x256')
15745 + {
15746 + $width = '512';
15747 + $height = '512';
15748 + }
15749 + elseif($image_size == '512x512')
15750 + {
15751 + $width = '512';
15752 + $height = '512';
15753 + }
15754 + elseif($image_size == '1024x1024')
15755 + {
15756 + $width = '1024';
15757 + $height = '1024';
15758 + }
15759 + elseif($image_size == '1792x1024')
15760 + {
15761 + $width = '1792';
15762 + $height = '1024';
15763 + }
15764 + elseif($image_size == '1024x1792')
15765 + {
15766 + $width = '1024';
15767 + $height = '1792';
15768 + }
15769 + else
15770 + {
15771 + $width = '512';
15772 + $height = '512';
15773 + }
15774 + $skip_this_copy = true;
15775 + if(!function_exists('is_plugin_active'))
15776 + {
15777 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15778 + }
15779 + 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')
15780 + {
15781 + $skip_this_copy = false;
15782 + }
15783 + $ierror = '';
15784 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating YouTube based Ideogram image'));
15785 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredIdeogramImage', $skip_this_copy, $ierror);
15786 + if($get_img !== false)
15787 + {
15788 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
15789 + {
15790 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
15791 + }
15792 + }
15793 + else
15794 + {
15795 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
15796 + $get_img = '';
15797 + }
15798 + }
15799 + }
15800 + else
15801 + {
15802 + aiomatic_log_to_file('Empty AI featured image query entered.');
15803 + }
15804 + }
15805 + elseif($enable_ai_images == '3')
15806 + {
15807 + if ($image_url != '')
15808 + {
15809 + $replacement = str_replace(array('[', ']'), '', $query_words);
15810 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
15811 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
15812 + if(isset($matches[2]))
15813 + {
15814 + $chance = trim($matches[2], '[]');
15815 + }
15816 + else
15817 + {
15818 + $chance = '';
15819 + }
15820 + $arv = array();
15821 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
15822 + return $my_img;
15823 + }, $image_url_temp);
15824 + $img_rulx = $spintax->Parse(trim($image_url_temp));
15825 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
15826 + if($selected_img === false)
15827 + {
15828 + $img_rulx = explode(',', $img_rulx);
15829 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
15830 + if($img_rulx != '')
15831 + {
15832 + $get_img = $img_rulx;
15833 + }
15834 + }
15835 + else
15836 + {
15837 + $get_img = $selected_img;
15838 + }
15839 + }
15840 + }
15841 + else
15842 + {
15843 + $image_query_set = false;
15844 + $query_words = '';
15845 + $ai_command_image = '';
15846 + if($orig_ai_command_image == '')
15847 + {
15848 + $orig_ai_command_image = $image_query;
15849 + }
15850 + if($orig_ai_command_image != '')
15851 + {
15852 + $ai_command_image = $orig_ai_command_image;
15853 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
15854 + {
15855 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
15856 + $ai_command_image = array_filter($ai_command_image);
15857 + if(count($ai_command_image) > 0)
15858 + {
15859 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
15860 + }
15861 + else
15862 + {
15863 + $ai_command_image = '';
15864 + }
15865 + }
15866 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15867 + if(!empty($ai_command_image))
15868 + {
15869 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15870 + }
15871 + else
15872 + {
15873 + $ai_command_image = trim(strip_tags($post_title));
15874 + }
15875 + $ai_command_image = trim($ai_command_image);
15876 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
15877 + {
15878 + $txt_content = aiomatic_get_web_page($ai_command_image);
15879 + if ($txt_content !== FALSE)
15880 + {
15881 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
15882 + $txt_content = array_filter($txt_content);
15883 + if(count($txt_content) > 0)
15884 + {
15885 + $txt_content = $txt_content[array_rand($txt_content)];
15886 + if(trim($txt_content) != '')
15887 + {
15888 + $ai_command_image = $txt_content;
15889 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
15890 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
15891 + }
15892 + }
15893 + }
15894 + }
15895 + }
15896 + if($ai_command_image != '')
15897 + {
15898 + $query_words = $ai_command_image;
15899 + $image_query = $ai_command_image;
15900 + $image_query_set = true;
15901 + }
15902 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
15903 + {
15904 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
15905 + {
15906 + try
15907 + {
15908 + if(!class_exists('TextRazor'))
15909 + {
15910 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
15911 + }
15912 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
15913 + $textrazor = new TextRazor();
15914 + $textrazor->addExtractor('entities');
15915 + $response = $textrazor->analyze($image_query);
15916 + if (isset($response['response']['entities']))
15917 + {
15918 + foreach ($response['response']['entities'] as $entity)
15919 + {
15920 + $query_words = '';
15921 + if(isset($entity['entityEnglishId']))
15922 + {
15923 + $query_words = $entity['entityEnglishId'];
15924 + }
15925 + else
15926 + {
15927 + $query_words = $entity['entityId'];
15928 + }
15929 + if($query_words != '')
15930 + {
15931 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
15932 + if(!empty($attached_id))
15933 + {
15934 + $additional_attachments[] = $attached_id;
15935 + }
15936 + if(!empty($z_img))
15937 + {
15938 + $get_img = $z_img;
15939 + $go_local_image = false;
15940 + if(!function_exists('is_plugin_active'))
15941 + {
15942 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
15943 + }
15944 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
15945 + {
15946 + $go_local_image = true;
15947 + }
15948 + if($go_local_image == true)
15949 + {
15950 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
15951 + if(!empty($attached_id))
15952 + {
15953 + $additional_attachments[] = $attached_id;
15954 + }
15955 + if($localpath !== false)
15956 + {
15957 + $get_img = $localpath[0];
15958 + }
15959 + }
15960 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
15961 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
15962 + }
15963 + break;
15964 + }
15965 + }
15966 + }
15967 + }
15968 + }
15969 + catch(Exception $e)
15970 + {
15971 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
15972 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
15973 + }
15974 + }
15975 + }
15976 + }
15977 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
15978 + {
15979 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
15980 + {
15981 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
15982 + {
15983 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
15984 + }
15985 + else
15986 + {
15987 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
15988 + }
15989 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
15990 + {
15991 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
15992 + }
15993 + else
15994 + {
15995 + $keyword_assistant_id = '';
15996 + }
15997 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
15998 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
15999 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
16000 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
16001 + {
16002 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
16003 + $title_ai_command = array_filter($title_ai_command);
16004 + if(count($title_ai_command) > 0)
16005 + {
16006 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
16007 + }
16008 + else
16009 + {
16010 + $title_ai_command = '';
16011 + }
16012 + }
16013 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
16014 + if(!empty($title_ai_command))
16015 + {
16016 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16017 + }
16018 + $title_ai_command = trim($title_ai_command);
16019 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
16020 + {
16021 + $txt_content = aiomatic_get_web_page($title_ai_command);
16022 + if ($txt_content !== FALSE)
16023 + {
16024 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
16025 + $txt_content = array_filter($txt_content);
16026 + if(count($txt_content) > 0)
16027 + {
16028 + $txt_content = $txt_content[array_rand($txt_content)];
16029 + if(trim($txt_content) != '')
16030 + {
16031 + $title_ai_command = $txt_content;
16032 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
16033 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16034 + }
16035 + }
16036 + }
16037 + }
16038 + if(empty($title_ai_command))
16039 + {
16040 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
16041 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
16042 + }
16043 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
16044 + {
16045 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
16046 + }
16047 + $title_ai_command = trim($title_ai_command);
16048 + if(empty($title_ai_command))
16049 + {
16050 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
16051 + }
16052 + else
16053 + {
16054 + $query_token_count = count(aiomatic_encode($title_ai_command));
16055 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
16056 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
16057 + {
16058 + $string_len = aiomatic_strlen($title_ai_command);
16059 + $string_len = $string_len / 2;
16060 + $string_len = intval(0 - $string_len);
16061 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
16062 + $title_ai_command = trim($title_ai_command);
16063 + $query_token_count = count(aiomatic_encode($title_ai_command));
16064 + $available_tokens = $max_tokens - $query_token_count;
16065 + }
16066 + $aierror = '';
16067 + $finish_reason = '';
16068 + if(!empty($global_prepend))
16069 + {
16070 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
16071 + }
16072 + if(!empty($global_append))
16073 + {
16074 + $title_ai_command = $title_ai_command . ' ' . $global_append;
16075 + }
16076 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16077 + {
16078 + $api_service = aiomatic_get_api_service($token, $kw_model);
16079 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
16080 + }
16081 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating keywords for image search'));
16082 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
16083 + if($generated_text === false)
16084 + {
16085 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
16086 + $ai_title = '';
16087 + }
16088 + else
16089 + {
16090 + $ai_title = trim(trim($generated_text), '.');
16091 + $xai_titles = explode(',', $ai_title);
16092 + foreach($xai_titles as $query_words)
16093 + {
16094 + $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);
16095 + if(!empty($attached_id))
16096 + {
16097 + $additional_attachments[] = $attached_id;
16098 + }
16099 + if(!empty($z_img))
16100 + {
16101 + $get_img = $z_img;
16102 + $go_local_image = false;
16103 + if(!function_exists('is_plugin_active'))
16104 + {
16105 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
16106 + }
16107 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
16108 + {
16109 + $go_local_image = true;
16110 + }
16111 + if($go_local_image == true)
16112 + {
16113 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
16114 + if(!empty($attached_id))
16115 + {
16116 + $additional_attachments[] = $attached_id;
16117 + }
16118 + if($localpath !== false)
16119 + {
16120 + $get_img = $localpath[0];
16121 + }
16122 + }
16123 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16124 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
16125 + }
16126 + break;
16127 + }
16128 + }
16129 + }
16130 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16131 + {
16132 + $api_service = aiomatic_get_api_service($token, $kw_model);
16133 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
16134 + }
16135 + }
16136 + }
16137 + }
16138 + if(empty($get_img))
16139 + {
16140 + if($image_query_set == true && !empty($image_query))
16141 + {
16142 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
16143 + if(!empty($attached_id))
16144 + {
16145 + $additional_attachments[] = $attached_id;
16146 + }
16147 + if($get_img == '' || $get_img === false)
16148 + {
16149 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
16150 + {
16151 + $keyword_class = new Aiomatic_keywords();
16152 + $image_query = $keyword_class->keywords($image_query, 1);
16153 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
16154 + if(!empty($attached_id))
16155 + {
16156 + $additional_attachments[] = $attached_id;
16157 + }
16158 + }
16159 + }
16160 + }
16161 + if(empty($get_img))
16162 + {
16163 + $keyword_class = new Aiomatic_keywords();
16164 + $query_words = $keyword_class->keywords($post_title, 2);
16165 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
16166 + if(!empty($attached_id))
16167 + {
16168 + $additional_attachments[] = $attached_id;
16169 + }
16170 + if($get_img == '' || $get_img === false)
16171 + {
16172 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
16173 + {
16174 + $query_words = $keyword_class->keywords($post_title, 1);
16175 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
16176 + if(!empty($attached_id))
16177 + {
16178 + $additional_attachments[] = $attached_id;
16179 + }
16180 + if($get_img == '' || $get_img === false)
16181 + {
16182 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
16183 + {
16184 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16185 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
16186 + }
16187 + unset($post_title_lines[$current_index]);
16188 + continue;
16189 + }
16190 + }
16191 + }
16192 + else
16193 + {
16194 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
16195 + {
16196 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16197 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
16198 + }
16199 + unset($post_title_lines[$current_index]);
16200 + continue;
16201 + }
16202 + }
16203 + }
16204 + }
16205 + if(!empty($get_img))
16206 + {
16207 + $go_local_image = false;
16208 + if(!function_exists('is_plugin_active'))
16209 + {
16210 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
16211 + }
16212 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
16213 + {
16214 + $go_local_image = true;
16215 + }
16216 + if($go_local_image == true)
16217 + {
16218 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
16219 + if(!empty($attached_id))
16220 + {
16221 + $additional_attachments[] = $attached_id;
16222 + }
16223 + if($localpath !== false)
16224 + {
16225 + $get_img = $localpath[0];
16226 + }
16227 + }
16228 + }
16229 + }
16230 + }
16231 + }
16232 + elseif($royalty_free == '2')
16233 + {
16234 + $get_img = $youtube_thumb;
16235 + }
16236 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
16237 + {
16238 + $already_spinned = '1';
16239 + }
16240 + $my_post = array();
16241 + $my_post['aiomatic_post_image'] = $get_img;
16242 + if($enable_ai_images == '2')
16243 + {
16244 + $my_post['aiomatic_local_image'] = '1';
16245 + }
16246 + else
16247 + {
16248 + $my_post['aiomatic_local_image'] = '0';
16249 + }
16250 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
16251 + $my_post['default_category'] = $default_category;
16252 + $my_post['post_type'] = $post_type;
16253 + $my_post['comment_status'] = $accept_comments;
16254 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
16255 + {
16256 + if($post_status == 'publish')
16257 + {
16258 + $draft_me = true;
16259 + $my_post['post_status'] = 'draft';
16260 + }
16261 + else
16262 + {
16263 + $my_post['post_status'] = $post_status;
16264 + }
16265 + }
16266 + else
16267 + {
16268 + $my_post['post_status'] = $post_status;
16269 + }
16270 + if($user_name_type == 'rand')
16271 + {
16272 + $randid = aiomatic_display_random_user();
16273 + if($randid === false)
16274 + {
16275 + $my_post['post_author'] = aiomatic_randomName();
16276 + }
16277 + else
16278 + {
16279 + $my_post['post_author'] = $randid->ID;
16280 + }
16281 + }
16282 + else
16283 + {
16284 + $my_post['post_author'] = $post_user_name;
16285 + }
16286 + if($strip_title == '1')
16287 + {
16288 + $new_post_content = str_replace($post_title, '', $new_post_content);
16289 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
16290 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
16291 + }
16292 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
16293 + {
16294 + require_once(dirname(__FILE__) . "/res/swear.php");
16295 + $new_post_content = aimogenpro_filterwords($new_post_content);
16296 + }
16297 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
16298 + $continue = false;
16299 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
16300 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
16301 + foreach ($banned_list as $banned_word) {
16302 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
16303 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16304 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
16305 + }
16306 + $continue = true;
16307 + break;
16308 + }
16309 + if (stripos($post_title, trim($banned_word)) !== FALSE) {
16310 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16311 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
16312 + }
16313 + $continue = true;
16314 + break;
16315 + }
16316 + }
16317 + if ($continue === true) {
16318 + continue;
16319 + }
16320 + }
16321 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
16322 + {
16323 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
16324 + {
16325 + $continue = true;
16326 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
16327 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
16328 + foreach ($required_list as $required_word) {
16329 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($post_title, trim($required_word)) !== FALSE) {
16330 + $continue = false;
16331 + break;
16332 + }
16333 + }
16334 + if ($continue === true) {
16335 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16336 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
16337 + }
16338 + continue;
16339 + }
16340 + }
16341 + else
16342 + {
16343 + $continue = false;
16344 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
16345 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
16346 + foreach ($required_list as $required_word) {
16347 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($post_title, trim($required_word)) === FALSE) {
16348 + $continue = true;
16349 + break;
16350 + }
16351 + }
16352 + if ($continue === true) {
16353 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16354 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
16355 + }
16356 + continue;
16357 + }
16358 + }
16359 + }
16360 + $new_post_title = $post_title;
16361 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
16362 + {
16363 + $skip_spin = '1';
16364 + }
16365 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
16366 + if($arr[0] != $new_post_title)
16367 + {
16368 + $new_post_title = $arr[0];
16369 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
16370 + {
16371 + $posts = get_posts(
16372 + array(
16373 + 'post_type' => $post_type,
16374 + 'title' => html_entity_decode($new_post_title),
16375 + 'post_status' => 'all',
16376 + 'numberposts' => 1,
16377 + 'update_post_term_cache' => false,
16378 + 'update_post_meta_cache' => false,
16379 + 'orderby' => 'post_date ID',
16380 + 'order' => 'ASC',
16381 + )
16382 + );
16383 + if ( ! empty( $posts ) ) {
16384 + $zap = $posts[0];
16385 + } else {
16386 + $zap = null;
16387 + }
16388 + if($zap !== null)
16389 + {
16390 + if($overwrite_existing)
16391 + {
16392 + $update_post_id = $zap->ID;
16393 + }
16394 + else
16395 + {
16396 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $new_post_title);
16397 + unset($post_title_lines[$current_index]);
16398 + continue;
16399 + }
16400 + }
16401 + }
16402 + }
16403 + $new_post_content = $arr[1];
16404 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
16405 + {
16406 + $already_spinned = '1';
16407 + }
16408 + if ($auto_categories == 'content') {
16409 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
16410 + $extra_categories = implode(',', $extra_categories);
16411 + }
16412 + elseif ($auto_categories == 'title') {
16413 + $extra_categories = aiomatic_extractKeyWords($new_post_title);
16414 + $extra_categories = implode(',', $extra_categories);
16415 + }
16416 + elseif ($auto_categories == 'both') {
16417 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
16418 + $extra_categories = implode(',', $extra_categories);
16419 + $extra_categories2 = aiomatic_extractKeyWords($new_post_title);
16420 + $extra_categories2 = implode(',', $extra_categories2);
16421 + if($extra_categories2 != '')
16422 + {
16423 + $extra_categories .= ',' . $extra_categories2;
16424 + }
16425 + }
16426 + elseif ($auto_categories == 'ai')
16427 + {
16428 + $category_ai_command = $orig_ai_command_category;
16429 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
16430 + {
16431 + $category_ai_command = preg_split('/\r\n|\r|\n/', $category_ai_command);
16432 + $category_ai_command = array_filter($category_ai_command);
16433 + if(count($category_ai_command) > 0)
16434 + {
16435 + $category_ai_command = $category_ai_command[array_rand($category_ai_command)];
16436 + }
16437 + else
16438 + {
16439 + $category_ai_command = '';
16440 + }
16441 + }
16442 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
16443 + if(!empty($category_ai_command))
16444 + {
16445 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16446 + $category_ai_command = aiomatic_replace_youtube($category_ai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16447 + }
16448 + else
16449 + {
16450 + $category_ai_command = trim(strip_tags('Write a comma separated list of categories, for the post title: %%post_title%%'));
16451 + }
16452 + $category_ai_command = trim($category_ai_command);
16453 + if (filter_var($category_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($category_ai_command, '.txt'))
16454 + {
16455 + $txt_content = aiomatic_get_web_page($category_ai_command);
16456 + if ($txt_content !== FALSE)
16457 + {
16458 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
16459 + $txt_content = array_filter($txt_content);
16460 + if(count($txt_content) > 0)
16461 + {
16462 + $txt_content = $txt_content[array_rand($txt_content)];
16463 + if(trim($txt_content) != '')
16464 + {
16465 + $category_ai_command = $txt_content;
16466 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
16467 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16468 + $category_ai_command = aiomatic_replace_youtube($category_ai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16469 + }
16470 + }
16471 + }
16472 + }
16473 + if(empty($category_ai_command))
16474 + {
16475 + aiomatic_log_to_file('Empty API post category seed expression provided!');
16476 + }
16477 + else
16478 + {
16479 + if(aiomatic_strlen($category_ai_command) > $max_seed_tokens * 4)
16480 + {
16481 + $category_ai_command = aiomatic_substr($category_ai_command, 0, (0 - ($max_seed_tokens * 4)));
16482 + }
16483 + $category_ai_command = trim($category_ai_command);
16484 + if(empty($category_ai_command))
16485 + {
16486 + aiomatic_log_to_file('Empty API category seed expression provided! ' . print_r($category_ai_command, true));
16487 + break;
16488 + }
16489 + $query_token_count = count(aiomatic_encode($category_ai_command));
16490 + $available_tokens = aiomatic_compute_available_tokens($category_model, $max_tokens, $category_ai_command, $query_token_count);
16491 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
16492 + {
16493 + $string_len = aiomatic_strlen($category_ai_command);
16494 + $string_len = $string_len / 2;
16495 + $string_len = intval(0 - $string_len);
16496 + $category_ai_command = aiomatic_substr($category_ai_command, 0, $string_len);
16497 + $category_ai_command = trim($category_ai_command);
16498 + if(empty($category_ai_command))
16499 + {
16500 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($category_ai_command, true));
16501 + break;
16502 + }
16503 + $query_token_count = count(aiomatic_encode($category_ai_command));
16504 + $available_tokens = $max_tokens - $query_token_count;
16505 + }
16506 + $aierror = '';
16507 + $finish_reason = '';
16508 + if(!empty($global_prepend))
16509 + {
16510 + $category_ai_command = $global_prepend . ' ' . $category_ai_command;
16511 + }
16512 + if(!empty($global_append))
16513 + {
16514 + $category_ai_command = $category_ai_command . ' ' . $global_append;
16515 + }
16516 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16517 + {
16518 + $api_service = aiomatic_get_api_service($token, $category_model);
16519 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $category_model . ')(' . $available_tokens . ') for category generator: ' . $category_ai_command);
16520 + }
16521 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating categories'));
16522 + $generated_text = aiomatic_generate_text($token, $category_model, $category_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
16523 + if($generated_text === false)
16524 + {
16525 + aiomatic_log_to_file('Category generator error: ' . $aierror);
16526 + break;
16527 + }
16528 + else
16529 + {
16530 + $extra_categories = $generated_text;
16531 + }
16532 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16533 + {
16534 + $api_service = aiomatic_get_api_service($token, $category_model);
16535 + aiomatic_log_to_file('Successfully got API category result from ' . $api_service . ': ' . $generated_text);
16536 + }
16537 + }
16538 + }
16539 + else
16540 + {
16541 + $extra_categories = '';
16542 + }
16543 + $my_post['extra_categories'] = $extra_categories;
16544 +
16545 + $item_tags = aiomatic_extractKeyWords($new_post_content, 3);
16546 + $item_tags = implode(',', $item_tags);
16547 + $title_tags = aiomatic_extractKeyWords($new_post_title, 3);
16548 + $title_tags = implode(',', $title_tags);
16549 + $item_create_tag_sp = $spintax->Parse($item_create_tag);
16550 + if ($can_create_tag == 'content') {
16551 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $item_tags;
16552 + $my_post['extra_tags'] = $item_tags;
16553 + } else if ($can_create_tag == 'title') {
16554 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $title_tags;
16555 + $my_post['extra_tags'] = $title_tags;
16556 + } else if ($can_create_tag == 'both') {
16557 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
16558 + $my_post['extra_tags'] = ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
16559 + } else if ($can_create_tag == 'ai') {
16560 + $ai_tags = '';
16561 + $tag_ai_command = $orig_ai_command_tag;
16562 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
16563 + {
16564 + $tag_ai_command = preg_split('/\r\n|\r|\n/', $tag_ai_command);
16565 + $tag_ai_command = array_filter($tag_ai_command);
16566 + if(count($tag_ai_command) > 0)
16567 + {
16568 + $tag_ai_command = $tag_ai_command[array_rand($tag_ai_command)];
16569 + }
16570 + else
16571 + {
16572 + $tag_ai_command = '';
16573 + }
16574 + }
16575 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
16576 + if(!empty($tag_ai_command))
16577 + {
16578 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16579 + $tag_ai_command = aiomatic_replace_youtube($tag_ai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16580 + }
16581 + else
16582 + {
16583 + $tag_ai_command = trim(strip_tags('Write a comma separated list of tags, for the post title: %%post_title%%'));
16584 + }
16585 + $tag_ai_command = trim($tag_ai_command);
16586 + if (filter_var($tag_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($tag_ai_command, '.txt'))
16587 + {
16588 + $txt_content = aiomatic_get_web_page($tag_ai_command);
16589 + if ($txt_content !== FALSE)
16590 + {
16591 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
16592 + $txt_content = array_filter($txt_content);
16593 + if(count($txt_content) > 0)
16594 + {
16595 + $txt_content = $txt_content[array_rand($txt_content)];
16596 + if(trim($txt_content) != '')
16597 + {
16598 + $tag_ai_command = $txt_content;
16599 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
16600 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16601 + $tag_ai_command = aiomatic_replace_youtube($tag_ai_command, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16602 + }
16603 + }
16604 + }
16605 + }
16606 + if(empty($tag_ai_command))
16607 + {
16608 + aiomatic_log_to_file('Empty API post tag seed expression provided!');
16609 + }
16610 + else
16611 + {
16612 + if(aiomatic_strlen($tag_ai_command) > $max_seed_tokens * 4)
16613 + {
16614 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, (0 - ($max_seed_tokens * 4)));
16615 + }
16616 + $tag_ai_command = trim($tag_ai_command);
16617 + if(empty($tag_ai_command))
16618 + {
16619 + aiomatic_log_to_file('Empty API tag seed expression provided! ' . print_r($tag_ai_command, true));
16620 + break;
16621 + }
16622 + $query_token_count = count(aiomatic_encode($tag_ai_command));
16623 + $available_tokens = aiomatic_compute_available_tokens($tag_model, $max_tokens, $tag_ai_command, $query_token_count);
16624 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
16625 + {
16626 + $string_len = aiomatic_strlen($tag_ai_command);
16627 + $string_len = $string_len / 2;
16628 + $string_len = intval(0 - $string_len);
16629 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, $string_len);
16630 + $tag_ai_command = trim($tag_ai_command);
16631 + if(empty($tag_ai_command))
16632 + {
16633 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($tag_ai_command, true));
16634 + break;
16635 + }
16636 + $query_token_count = count(aiomatic_encode($tag_ai_command));
16637 + $available_tokens = $max_tokens - $query_token_count;
16638 + }
16639 + $aierror = '';
16640 + $finish_reason = '';
16641 + if(!empty($global_prepend))
16642 + {
16643 + $tag_ai_command = $global_prepend . ' ' . $tag_ai_command;
16644 + }
16645 + if(!empty($global_append))
16646 + {
16647 + $tag_ai_command = $tag_ai_command . ' ' . $global_append;
16648 + }
16649 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16650 + {
16651 + $api_service = aiomatic_get_api_service($token, $tag_model);
16652 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $tag_model . ')(' . $available_tokens . ') for tag generator: ' . $tag_ai_command);
16653 + }
16654 +
16655 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
16656 + $generated_text = aiomatic_generate_text($token, $tag_model, $tag_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
16657 + if($generated_text === false)
16658 + {
16659 + aiomatic_log_to_file('Tag generator error: ' . $aierror);
16660 + break;
16661 + }
16662 + else
16663 + {
16664 + $ai_tags = $generated_text;
16665 + }
16666 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
16667 + {
16668 + $api_service = aiomatic_get_api_service($token, $tag_model);
16669 + aiomatic_log_to_file('Successfully got API tag result from ' . $api_service . ': ' . $generated_text);
16670 + }
16671 + }
16672 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $ai_tags;
16673 + $my_post['extra_tags'] = $ai_tags;
16674 + } else {
16675 + $post_the_tags = $item_create_tag_sp;
16676 + $my_post['extra_tags'] = '';
16677 + }
16678 + $my_post['tags_input'] = $post_the_tags;
16679 + $new_post_content = html_entity_decode($new_post_content);
16680 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
16681 + if ($strip_by_regex !== '')
16682 + {
16683 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
16684 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
16685 + $xcnt = 0;
16686 + foreach($xstrip_by_regex as $sbr)
16687 + {
16688 + if(isset($xreplace_regex[$xcnt]))
16689 + {
16690 + $repreg = $xreplace_regex[$xcnt];
16691 + }
16692 + else
16693 + {
16694 + $repreg = '';
16695 + }
16696 + $xcnt++;
16697 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
16698 + if($temp_cont !== NULL)
16699 + {
16700 + $new_post_content = $temp_cont;
16701 + }
16702 + }
16703 + }
16704 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16705 + $post_prepender = aiomatic_replace_youtube($post_prepender, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16706 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16707 + $post_appender = aiomatic_replace_youtube($post_appender, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16708 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
16709 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
16710 + $zlang = 'en_US';
16711 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
16712 + $zlang = $aiomatic_Main_Settings['kw_lang'];
16713 + }
16714 + $rel_search = array('post_title', 'post_content');
16715 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
16716 + $rel_search = $aiomatic_Main_Settings['rel_search'];
16717 + }
16718 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
16719 + {
16720 + try
16721 + {
16722 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
16723 + }
16724 + catch(Exception $ex)
16725 + {
16726 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16727 + aiomatic_log_to_file('Failed to add new inbound links to content: ' . $ex->getMessage());
16728 + }
16729 + }
16730 + }
16731 + if($topic_videos == '1' && count($added_vid_list) == 0)
16732 + {
16733 + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $amatches);
16734 + if(isset($amatches[1]))
16735 + {
16736 + $video_id = $amatches[1];
16737 + if (isset($aiomatic_Main_Settings['player_width']) && $aiomatic_Main_Settings['player_width'] !== '') {
16738 + $vwidth = esc_attr($aiomatic_Main_Settings['player_width']);
16739 + }
16740 + else
16741 + {
16742 + $vwidth = 580;
16743 + }
16744 + if (isset($aiomatic_Main_Settings['player_height']) && $aiomatic_Main_Settings['player_height'] !== '') {
16745 + $vheight = esc_attr($aiomatic_Main_Settings['player_height']);
16746 + }
16747 + else
16748 + {
16749 + $vheight = 380;
16750 + }
16751 + $new_post_content .= '<br/><br/><div class="automaticx-video-container"><iframe title="' . str_replace('"', "'", $new_post_title) . '" allow="autoplay" width="' . $vwidth . '" height="' . $vheight . '" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
16752 + $added_vid_list[] = $video_id;
16753 + $new_vid_arr[] = $video_id;
16754 + $added_videos++;
16755 + }
16756 + }
16757 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
16758 + {
16759 + if($new_post_content === strip_tags($new_post_content))
16760 + {
16761 + $new_post_content = nl2br($new_post_content);
16762 + }
16763 + }
16764 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
16765 + {
16766 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
16767 + {
16768 + if(!isset($xchars))
16769 + {
16770 + $xchars = array();
16771 + }
16772 + $rand_percentage = wp_rand(10, 20);
16773 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
16774 + }
16775 + }
16776 + if($ret_content == 1)
16777 + {
16778 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
16779 + aiomatic_log_to_file('Returning AI generated content, finished.');
16780 + }
16781 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
16782 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
16783 + }
16784 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
16785 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
16786 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
16787 + {
16788 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
16789 + }
16790 + else
16791 + {
16792 + $my_post['post_content'] = $trunchi_content;
16793 + }
16794 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
16795 + {
16796 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
16797 + {
16798 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
16799 + }
16800 + }
16801 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
16802 + $my_post['aiomatic_source_title'] = $video_url;
16803 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
16804 + $my_post['aiomatic_post_format'] = $post_format;
16805 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
16806 + {
16807 + if(is_array($default_category))
16808 + {
16809 + $cextra = '';
16810 + foreach($default_category as $dc)
16811 + {
16812 + $cextra .= ',' . get_cat_name($dc);
16813 + }
16814 + $extra_categories_temp = trim( $cextra . ',' . $extra_categories, ',');
16815 + }
16816 + else
16817 + {
16818 + $extra_categories_temp = trim(get_cat_name($default_category) . ',' .$extra_categories, ',');
16819 + }
16820 + }
16821 + else
16822 + {
16823 + $extra_categories_temp = $extra_categories;
16824 + }
16825 + $block_arr = array();
16826 + $custom_arr = array();
16827 + if($custom_fields != '')
16828 + {
16829 + if(stristr($custom_fields, '=>') != false)
16830 + {
16831 + $rule_arr = explode(',', trim($custom_fields));
16832 + foreach($rule_arr as $rule)
16833 + {
16834 + $my_args = explode('=>', trim($rule));
16835 + if(isset($my_args[1]))
16836 + {
16837 + if ( isset( $shortcode_tags['aicontent'] ) ) {
16838 + $aicontent_handler = $shortcode_tags['aicontent'];
16839 + unset( $shortcode_tags['aicontent'] );
16840 + } else {
16841 + $aicontent_handler = null;
16842 + }
16843 + $my_args[1] = do_shortcode($my_args[1]);
16844 + $my_args[0] = do_shortcode($my_args[0]);
16845 + if ( $aicontent_handler ) {
16846 + $shortcode_tags['aicontent'] = $aicontent_handler;
16847 + }
16848 + $custom_field_content = trim($my_args[1]);
16849 + $custom_field_content = aiomatic_replaceAIPostShortcodes($custom_field_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16850 + $custom_field_content = aiomatic_replace_youtube($custom_field_content, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16851 + $custom_field_content = aiomatic_replaceSynergyShortcodes($custom_field_content);
16852 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
16853 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
16854 + if(stristr($my_args[1], 'serialize_') !== false)
16855 + {
16856 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
16857 + }
16858 + else
16859 + {
16860 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
16861 + {
16862 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
16863 + if(isset($cfm[2][0]))
16864 + {
16865 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
16866 + {
16867 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
16868 + }
16869 + else
16870 + {
16871 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
16872 + }
16873 + }
16874 + else
16875 + {
16876 + $custom_arr[trim($my_args[0])] = $custom_field_content;
16877 + }
16878 + }
16879 + else
16880 + {
16881 + $custom_arr[trim($my_args[0])] = $custom_field_content;
16882 + }
16883 + }
16884 + }
16885 + }
16886 + }
16887 + }
16888 + $block_arr = array();
16889 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $extra_categories_temp, 'aiomatic_post_tags' => $post_the_tags));
16890 + $custom_tax_arr = array();
16891 + if($custom_tax != '')
16892 + {
16893 + if(stristr($custom_tax, '=>') != false)
16894 + {
16895 + $rule_arr = explode(';', trim($custom_tax));
16896 + foreach($rule_arr as $rule)
16897 + {
16898 + $my_args = explode('=>', trim($rule));
16899 + if(isset($my_args[1]))
16900 + {
16901 + $custom_tax_content = trim($my_args[1]);
16902 + $custom_tax_content = aiomatic_replaceAIPostShortcodes($custom_tax_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
16903 + $custom_tax_content = aiomatic_replace_youtube($custom_tax_content, $post_title, $post_desc, $video_url, $returned_caption, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list);
16904 + $custom_tax_content = aiomatic_replaceSynergyShortcodes($custom_tax_content);
16905 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
16906 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
16907 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
16908 + {
16909 + if(isset($custom_arr['_product_attributes']))
16910 + {
16911 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
16912 + 'name' => trim($my_args[0]),
16913 + 'value' => $custom_tax_content,
16914 + 'is_visible' => '1',
16915 + 'is_taxonomy' => '1'
16916 + )));
16917 + }
16918 + else
16919 + {
16920 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
16921 + 'name' => trim($my_args[0]),
16922 + 'value' => $custom_tax_content,
16923 + 'is_visible' => '1',
16924 + 'is_taxonomy' => '1'
16925 + ));
16926 + }
16927 + }
16928 + if(isset($custom_tax_arr[trim($my_args[0])]))
16929 + {
16930 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
16931 + }
16932 + else
16933 + {
16934 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
16935 + }
16936 + }
16937 + }
16938 + }
16939 + }
16940 + if(count($custom_tax_arr) > 0)
16941 + {
16942 + $my_post['taxo_input'] = $custom_tax_arr;
16943 + }
16944 + $my_post['meta_input'] = $custom_arr;
16945 + if($parent_id != '')
16946 + {
16947 + $my_post['post_parent'] = intval($parent_id);
16948 + }
16949 + if ($enable_pingback == '1') {
16950 + $my_post['ping_status'] = 'open';
16951 + } else {
16952 + $my_post['ping_status'] = 'closed';
16953 + }
16954 + if($min_time != '' && $max_time != '')
16955 + {
16956 + $t1 = strtotime($min_time);
16957 + $t2 = strtotime($max_time);
16958 + if($t1 != false && $t2 != false)
16959 + {
16960 + $int = wp_rand($t1, $t2);
16961 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
16962 + }
16963 + }
16964 + elseif($min_time != '')
16965 + {
16966 + $t1 = strtotime($min_time);
16967 + if($t1 != false)
16968 + {
16969 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
16970 + }
16971 + }
16972 + elseif($max_time != '')
16973 + {
16974 + $t1 = strtotime($max_time);
16975 + if($t1 != false)
16976 + {
16977 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
16978 + }
16979 + }
16980 + if($new_post_excerpt != '')
16981 + {
16982 + $my_post['post_excerpt'] = $new_post_excerpt;
16983 + }
16984 + $count++;
16985 + if($update_post_id != '')
16986 + {
16987 + $my_post['ID'] = $update_post_id;
16988 + }
16989 + remove_filter('content_save_pre', 'wp_filter_post_kses');
16990 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
16991 + try
16992 + {
16993 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
16994 + $post_id = wp_insert_post($my_post, true);
16995 + if (is_wp_error($post_id))
16996 + {
16997 + aiomatic_log_to_file('Trying publishing again');
16998 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
16999 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
17000 + }
17001 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
17002 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
17003 + $post_id = wp_insert_post($my_post, true);
17004 + }
17005 + }
17006 + catch(Exception $e)
17007 + {
17008 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
17009 + }
17010 + add_filter('content_save_pre', 'wp_filter_post_kses');
17011 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
17012 + if (!is_wp_error($post_id)) {
17013 + if($post_id === 0)
17014 + {
17015 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
17016 + continue;
17017 + }
17018 + $posts_inserted++;
17019 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
17020 + {
17021 + pll_set_post_language($post_id, $wpml_lang);
17022 + }
17023 + if(!empty($additional_attachments))
17024 + {
17025 + foreach($additional_attachments as $add_att)
17026 + {
17027 + wp_update_post([
17028 + 'ID' => $add_att,
17029 + 'post_parent' => $post_id,
17030 + ]);
17031 + }
17032 + }
17033 + $default_categories = array();
17034 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
17035 + {
17036 + $default_categories = wp_get_post_categories($post_id);
17037 + }
17038 + if(isset($my_post['taxo_input']))
17039 + {
17040 + foreach($my_post['taxo_input'] as $taxn => $taxval)
17041 + {
17042 + $taxn = trim($taxn);
17043 + $taxval = trim($taxval);
17044 + if(is_taxonomy_hierarchical($taxn))
17045 + {
17046 + $taxval = array_map('trim', explode(',', $taxval));
17047 + for($ii = 0; $ii < count($taxval); $ii++)
17048 + {
17049 + $term_ids = [];
17050 + $parent_id = 0;
17051 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
17052 + foreach($hierarchy_parts as $hp)
17053 + {
17054 + if(!is_numeric($hp))
17055 + {
17056 + $xtermid = get_term_by('name', $hp, $taxn);
17057 + if($xtermid !== false)
17058 + {
17059 + $parent_id = $xtermid->term_id;
17060 + if(!is_numeric($taxval[$ii]))
17061 + {
17062 + $taxval[$ii] = intval($xtermid->term_id);
17063 + }
17064 + else
17065 + {
17066 + $taxval[] = intval($xtermid->term_id);
17067 + }
17068 + }
17069 + else
17070 + {
17071 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
17072 + $xtermid = get_term_by('name', $hp, $taxn);
17073 + if($xtermid !== false)
17074 + {
17075 + $parent_id = $xtermid->term_id;
17076 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
17077 + {
17078 + pll_set_term_language($xtermid->term_id, $wpml_lang);
17079 + }
17080 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
17081 + {
17082 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
17083 + $pars['element_id'] = $xtermid->term_id;
17084 + $pars['element_type'] = $wpml_element_type;
17085 + $pars['language_code'] = $wpml_lang;
17086 + $pars['trid'] = FALSE;
17087 + $pars['source_language_code'] = NULL;
17088 + do_action('wpml_set_element_language_details', $pars);
17089 + }
17090 + if(!is_numeric($taxval[$ii]))
17091 + {
17092 + $taxval[$ii] = intval($xtermid->term_id);
17093 + }
17094 + else
17095 + {
17096 + $taxval[] = intval($xtermid->term_id);
17097 + }
17098 + }
17099 + }
17100 + }
17101 + else
17102 + {
17103 + if(!is_numeric($taxval[$ii]))
17104 + {
17105 + $taxval[$ii] = intval($hp);
17106 + }
17107 + else
17108 + {
17109 + $taxval[] = intval($hp);
17110 + }
17111 + }
17112 + }
17113 + }
17114 + wp_set_post_terms($post_id, $taxval, $taxn, true);
17115 + }
17116 + else
17117 + {
17118 + $jterms = array_map('trim', explode(',', $taxval));
17119 + wp_set_post_terms($post_id, $jterms, $taxn, true);
17120 + }
17121 + }
17122 + }
17123 + if (isset($my_post['aiomatic_post_format']) && $my_post['aiomatic_post_format'] != '' && $my_post['aiomatic_post_format'] != 'post-format-standard') {
17124 + wp_set_post_terms($post_id, $my_post['aiomatic_post_format'], 'post_format', true);
17125 + }
17126 + $featured_path = '';
17127 + $get_img = $my_post['aiomatic_post_image'];
17128 + if ($get_img != '') {
17129 + if($my_post['aiomatic_local_image'] == '1')
17130 + {
17131 + $local_get_img = $get_img[0];
17132 + if (!aiomatic_assign_featured_image_path($local_get_img, $post_id)) {
17133 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17134 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed for ' . $local_get_img);
17135 + }
17136 + } else {
17137 + $featured_path = $get_img[1];
17138 + }
17139 + }
17140 + else
17141 + {
17142 + if(is_numeric($get_img))
17143 + {
17144 + $featured_path = aiomatic_assign_featured_image($get_img, $post_id);
17145 + }
17146 + else
17147 + {
17148 + if (!aiomatic_generate_featured_image($get_img, $post_id)) {
17149 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17150 + aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $get_img);
17151 + }
17152 + } else {
17153 + $featured_path = $get_img;
17154 + }
17155 + }
17156 + }
17157 + }
17158 + if($featured_path == '')
17159 + {
17160 + if ($image_url != '') {
17161 + $replacement = str_replace(array('[', ']'), '', $my_post['post_title']);
17162 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
17163 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
17164 + if(isset($matches[2]))
17165 + {
17166 + $chance = trim($matches[2], '[]');
17167 + }
17168 + else
17169 + {
17170 + $chance = '';
17171 + }
17172 + $arv = array();
17173 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
17174 + return $my_img;
17175 + }, $image_url_temp);
17176 + $img_rulx = $spintax->Parse(trim($image_url_temp));
17177 + $selected_img = aiomatic_select_ai_image($my_post['post_title'], $img_rulx);
17178 + if($selected_img === false)
17179 + {
17180 + $img_rulx = explode(',', $img_rulx);
17181 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
17182 + }
17183 + else
17184 + {
17185 + $img_rulx = $selected_img;
17186 + }
17187 + if(is_numeric($img_rulx))
17188 + {
17189 + $featured_path = aiomatic_assign_featured_image($img_rulx, $post_id);
17190 + }
17191 + else
17192 + {
17193 + if($img_rulx != '')
17194 + {
17195 + if (!aiomatic_generate_featured_image($img_rulx, $post_id)) {
17196 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17197 + aiomatic_log_to_file('aiomatic_generate_featured_image failed to default value: ' . $img_rulx . '!');
17198 + }
17199 + } else {
17200 + $featured_path = $img_rulx;
17201 + }
17202 + }
17203 + }
17204 + }
17205 + }
17206 + if ($auto_categories != 'disabled') {
17207 + if ($my_post['extra_categories'] != '') {
17208 + $extra_cats = explode(',', $my_post['extra_categories']);
17209 + if($post_type == 'product')
17210 + {
17211 + $product_tax = 'product_cat';
17212 + }
17213 + else
17214 + {
17215 + $product_tax = 'category';
17216 + }
17217 + foreach($extra_cats as $extra_cat)
17218 + {
17219 + $extra_cat = trim($extra_cat);
17220 + $extra_cat = strip_tags($extra_cat);
17221 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
17222 + if(empty($extra_cat))
17223 + {
17224 + continue;
17225 + }
17226 + if ($skip_inexist == '1')
17227 + {
17228 + if(!term_exists($extra_cat, $product_tax))
17229 + {
17230 + continue;
17231 + }
17232 + }
17233 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
17234 + wp_set_post_terms($post_id, $termid, $product_tax, true);
17235 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
17236 + {
17237 + foreach($termid as $tx)
17238 + {
17239 + pll_set_term_language($tx, $wpml_lang);
17240 + }
17241 + }
17242 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
17243 + {
17244 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
17245 + foreach($termid as $tx)
17246 + {
17247 + $pars['element_id'] = $tx;
17248 + $pars['element_type'] = $wpml_element_type;
17249 + $pars['language_code'] = $wpml_lang;
17250 + $pars['trid'] = FALSE;
17251 + $pars['source_language_code'] = NULL;
17252 + do_action('wpml_set_element_language_details', $pars);
17253 + }
17254 + }
17255 + }
17256 + }
17257 + }
17258 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678') {
17259 + $cats = array();
17260 + if(is_array($default_category))
17261 + {
17262 + foreach($default_category as $dc)
17263 + {
17264 + $cats[] = $dc;
17265 + }
17266 + }
17267 + else
17268 + {
17269 + $cats[] = $default_category;
17270 + }
17271 + global $sitepress;
17272 + if($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
17273 + {
17274 + $current_language = apply_filters( 'wpml_current_language', NULL );
17275 + $sitepress->switch_lang($wpml_lang);
17276 + }
17277 + $target_cat = 'category';
17278 + if($post_type == 'product')
17279 + {
17280 + $target_cat = 'product_cat';
17281 + }
17282 + wp_set_object_terms($post_id, array_map( 'intval', $cats ), $target_cat, true);
17283 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
17284 + {
17285 + foreach($cats as $cc)
17286 + {
17287 + pll_set_term_language($cc, $wpml_lang);
17288 + }
17289 + }
17290 + elseif($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
17291 + {
17292 + $sitepress->switch_lang($current_language);
17293 + }
17294 + }
17295 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
17296 + {
17297 + if($post_type == 'product')
17298 + {
17299 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
17300 + }
17301 + }
17302 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
17303 + if (is_wp_error($tax_rez)) {
17304 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17305 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
17306 + }
17307 + }
17308 + if($post_type == 'topic' && $parent_id != '')
17309 + {
17310 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
17311 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
17312 + update_post_meta($post_id, '_bbp_voice_count', '0');
17313 + update_post_meta($post_id, '_bbp_reply_count', '0');
17314 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
17315 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
17316 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
17317 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
17318 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
17319 + }
17320 + if($post_type == 'reply' && $parent_id != '')
17321 + {
17322 + if(function_exists('bbp_get_topic_forum_id'))
17323 + {
17324 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
17325 + if(empty($forum_aidi))
17326 + {
17327 + $forum_aidi = 0;
17328 + }
17329 + }
17330 + else
17331 + {
17332 + $forum_aidi = 0;
17333 + }
17334 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
17335 + }
17336 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
17337 + {
17338 + $new_categories = wp_get_post_categories($post_id);
17339 + $removed_cat = false;
17340 + if(isset($default_categories) && !($default_categories == $new_categories))
17341 + {
17342 + foreach($default_categories as $dc)
17343 + {
17344 + $rem_cat = get_category( $dc );
17345 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
17346 + $removed_cat = true;
17347 + }
17348 + }
17349 + if($removed_cat == false)
17350 + {
17351 + wp_remove_object_terms( $post_id, 'uncategorized', 'category' );
17352 + }
17353 + }
17354 + aiomatic_addPostMeta($post_id, $my_post, $param, $type, $featured_path, $post_topic, $rule_unique_id, $post_link);
17355 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
17356 + {
17357 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
17358 + $pars['element_id'] = $post_id;
17359 + $pars['element_type'] = $wpml_element_type;
17360 + $pars['language_code'] = $wpml_lang;
17361 + $pars['source_language_code'] = NULL;
17362 + do_action('wpml_set_element_language_details', $pars);
17363 +
17364 + global $wp_filesystem;
17365 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
17366 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
17367 + wp_filesystem($creds);
17368 + }
17369 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
17370 + {
17371 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
17372 + }
17373 + $wpml_lang = trim($wpml_lang);
17374 + if(function_exists('wpml_update_translatable_content'))
17375 + {
17376 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
17377 + if($my_post['post_title'] != '')
17378 + {
17379 + global $sitepress;
17380 + global $wpdb;
17381 + $keyid = md5($my_post['post_title']);
17382 + $keyName = $keyid . '_wpml';
17383 + $rezxxxa = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '$keyName' limit 1", ARRAY_A );
17384 + if(count($rezxxxa) != 0)
17385 + {
17386 + $metaRow = $rezxxxa[0];
17387 + $metaValue = $metaRow['meta_value'];
17388 + $metaParts = explode('_', $metaValue);
17389 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
17390 + }
17391 + else
17392 + {
17393 + $ptrid = $sitepress->get_element_trid($post_id);
17394 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
17395 + }
17396 + }
17397 +
17398 + }
17399 + }
17400 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
17401 + {
17402 + aiomatic_change_post_status($post_id, 'publish');
17403 + }
17404 + } else {
17405 + aiomatic_log_to_file('Failed to insert post into wp database(2)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
17406 + continue;
17407 + }
17408 + }
17409 + }
17410 + elseif($type == '2')
17411 + {
17412 + $amazon_keyword = str_replace(',', '|', $amazon_keyword);
17413 + $amazon_keyword_lines = array();
17414 + if($amazon_keyword != '')
17415 + {
17416 + $amazon_keyword_lines = preg_split('/\r\n|\r|\n/', $amazon_keyword);
17417 + }
17418 + else
17419 + {
17420 + aiomatic_log_to_file('You need to enter an Amazon product search keyword to use this feature!');
17421 + if($auto == 1)
17422 + {
17423 + aiomatic_clearFromList($param, $type);
17424 + }
17425 + return 'fail';
17426 + }
17427 + $additional_kws = array();
17428 + $post_link = '';
17429 + $user_name = '';
17430 + $featured_image = '';
17431 + $post_cats = '';
17432 + $post_tagz = '';
17433 + $post_excerpt = '';
17434 + $final_content = '';
17435 + $postID = '';
17436 + $heading_val = '';
17437 + $image_query = '';
17438 + $temp_post = '';
17439 + $cntx = count($amazon_keyword_lines);
17440 + $rss_feeds = array();
17441 + for($ji = 0; $ji < $cntx; $ji++)
17442 + {
17443 + if (filter_var($amazon_keyword_lines[$ji], FILTER_VALIDATE_URL) !== false && stristr($amazon_keyword_lines[$ji], 'youtu') === false)
17444 + {
17445 + if(aiomatic_endsWith($amazon_keyword_lines[$ji], '.txt'))
17446 + {
17447 + $txt_content = aiomatic_get_web_page($amazon_keyword_lines[$ji]);
17448 + if ($txt_content === FALSE)
17449 + {
17450 + aiomatic_log_to_file('Failed to read text file: ' . $amazon_keyword_lines[$ji]);
17451 + unset($amazon_keyword_lines[$ji]);
17452 + if($auto == 1)
17453 + {
17454 + aiomatic_log_to_file($param);
17455 + }
17456 + continue;
17457 + }
17458 + unset($amazon_keyword_lines[$ji]);
17459 + $additional_kws = preg_split('/\r\n|\r|\n/', $txt_content);
17460 + }
17461 + else
17462 + {
17463 + aiomatic_log_to_file('Trying to parse RSS feed items(4): ' . $amazon_keyword_lines[$ji]);
17464 + try
17465 + {
17466 + if(!class_exists('SimplePie_Autoloader', false))
17467 + {
17468 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
17469 + }
17470 + }
17471 + catch(Exception $e)
17472 + {
17473 + unset($amazon_keyword_lines[$ji]);
17474 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
17475 + if($auto == 1)
17476 + {
17477 + aiomatic_log_to_file($param);
17478 + }
17479 + continue;
17480 + }
17481 + $feed = new SimplePie();
17482 + $feed->set_timeout(120);
17483 + $feed->set_feed_url($amazon_keyword_lines[$ji]);
17484 + $feed->enable_cache(false);
17485 + $feed->strip_htmltags(false);
17486 + $feed->init();
17487 + $feed->handle_content_type();
17488 + if ($feed->error())
17489 + {
17490 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $amazon_keyword_lines[$ji]);
17491 + unset($amazon_keyword_lines[$ji]);
17492 + if($auto == 1)
17493 + {
17494 + aiomatic_clearFromList($param, $type);
17495 + }
17496 + continue;
17497 + }
17498 + $items = $feed->get_items();
17499 + $zero = true;
17500 + foreach($items as $itemx)
17501 + {
17502 + if($zero == true)
17503 + {
17504 + $post_link = trim($itemx->get_permalink());
17505 + if(isset($rss_items[$post_link]))
17506 + {
17507 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17508 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
17509 + }
17510 + continue;
17511 + }
17512 + if ($fauthor = $itemx->get_author())
17513 + {
17514 + $user_name = $fauthor->get_name();
17515 + }
17516 + else
17517 + {
17518 + $user_name = '';
17519 + }
17520 + $feed_cats = array();
17521 + $varx = $itemx->get_categories();
17522 + if(is_array($varx))
17523 + {
17524 + foreach ($varx as $xcategory)
17525 + {
17526 + $feed_cats[] = $xcategory->get_label();
17527 + }
17528 + }
17529 + $post_cats = implode(',', $feed_cats);
17530 + $post_excerpt = $itemx->get_description();
17531 + $final_content = $itemx->get_content();
17532 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
17533 + }
17534 + else
17535 + {
17536 + $post_link_temp = trim($itemx->get_permalink());
17537 + if(isset($rss_items[$post_link_temp]))
17538 + {
17539 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17540 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
17541 + }
17542 + continue;
17543 + }
17544 + if ($fauthor = $itemx->get_author())
17545 + {
17546 + $user_name_temp = $fauthor->get_name();
17547 + }
17548 + else
17549 + {
17550 + $user_name_temp = '';
17551 + }
17552 + $feed_cats = array();
17553 + $varx = $itemx->get_categories();
17554 + if(is_array($varx))
17555 + {
17556 + foreach ($varx as $xcategory)
17557 + {
17558 + $feed_cats[] = $xcategory->get_label();
17559 + }
17560 + }
17561 + $post_cats_temp = implode(',', $feed_cats);
17562 + $post_excerpt_temp = $itemx->get_description();
17563 + $final_content_temp = $itemx->get_content();
17564 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link_temp, 'author' => $user_name_temp, 'cats' => $post_cats_temp, 'excerpt' => $post_excerpt_temp, 'content' => $final_content_temp );
17565 + }
17566 + $additional_kws[] = $itemx->get_title();
17567 + $zero = false;
17568 + }
17569 + unset($amazon_keyword_lines[$ji]);
17570 + }
17571 + }
17572 + }
17573 + if(count($additional_kws) > 0)
17574 + {
17575 + $amazon_keyword_lines = array_merge($amazon_keyword_lines, $additional_kws);
17576 + }
17577 + $amazon_keyword_lines = array_unique($amazon_keyword_lines);
17578 + if($overwrite_existing == '1')
17579 + {
17580 + $amazon_keyword_lines_processed = array();
17581 + foreach($amazon_keyword_lines as $zind => $ptl)
17582 + {
17583 + $ptlprocessed = explode('!###!', $ptl);
17584 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
17585 + {
17586 + $amazon_keyword_lines_processed[$zind] = $ptlprocessed[0];
17587 + }
17588 + else
17589 + {
17590 + $amazon_keyword_lines_processed[$zind] = $ptl;
17591 + }
17592 + }
17593 + foreach($posted_items as $ptit => $pid)
17594 + {
17595 + if (($key = array_search($ptit, $amazon_keyword_lines_processed)) !== false) {
17596 + $update_post_id = $pid;
17597 + break;
17598 + }
17599 + }
17600 + }
17601 + else
17602 + {
17603 + if($title_once == '1')
17604 + {
17605 + $skipt = 0;
17606 + $amazon_keyword_lines_processed = array();
17607 + foreach($amazon_keyword_lines as $zind => $ptl)
17608 + {
17609 + $ptlprocessed = explode('!###!', $ptl);
17610 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
17611 + {
17612 + $amazon_keyword_lines_processed[$zind] = $ptlprocessed[0];
17613 + }
17614 + else
17615 + {
17616 + $amazon_keyword_lines_processed[$zind] = $ptl;
17617 + }
17618 + }
17619 + foreach($posted_items as $ptit => $pid)
17620 + {
17621 + if (($key = array_search($ptit, $amazon_keyword_lines_processed)) !== false) {
17622 + aiomatic_log_to_file('Skipping Amazon title, already processed: ' . $ptit);
17623 + unset($amazon_keyword_lines[$key]);
17624 + unset($amazon_keyword_lines_processed[$key]);
17625 + $skipt++;
17626 + }
17627 + }
17628 + if(count($amazon_keyword_lines) == 0 && $skipt > 0)
17629 + {
17630 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
17631 + {
17632 + if($count === 1)
17633 + {
17634 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
17635 + foreach($email_list as $thisaddr)
17636 + {
17637 + $thisaddr = trim($thisaddr);
17638 + try
17639 + {
17640 + $admin_mail = get_option('admin_email');
17641 + $to = $thisaddr;
17642 + $subject = '[Aimogen] Amazon Keywords depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
17643 + $message = 'Amazon Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its Amazon keywords list, no new posts publised! Website URL: ' . get_site_url();
17644 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
17645 + $headers[] = 'Reply-To: ' . $admin_mail;
17646 + $headers[] = 'X-Mailer: PHP/' . phpversion();
17647 + $headers[] = 'Content-Type: text/html';
17648 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
17649 + wp_mail($to, $subject, $message, $headers);
17650 + }
17651 + catch (Exception $e) {
17652 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
17653 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
17654 + }
17655 + }
17656 + }
17657 + }
17658 + }
17659 + aiomatic_log_to_file('All Amazon keywords already processed, nothing to do.');
17660 + if($auto == 1)
17661 + {
17662 + aiomatic_clearFromList($param, $type);
17663 + }
17664 + return 'nochange';
17665 + }
17666 + }
17667 + }
17668 + $spintax = new Aiomatic_Spintax();
17669 + $orig_ai_command = $ai_command;
17670 + $orig_ai_command_title = $title_ai_command;
17671 + $orig_ai_command_category = $category_ai_command;
17672 + $orig_ai_command_tag = $tag_ai_command;
17673 + $orig_ai_command_image = $ai_command_image;
17674 + $already_spinned = 0;
17675 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
17676 + {
17677 + $img_attr = $aiomatic_Main_Settings['attr_text'];
17678 + }
17679 + else
17680 + {
17681 + $img_attr = '';
17682 + }
17683 + $headings_arr = array();
17684 + if($images_list != '')
17685 + {
17686 + $images_arr = preg_split('/\r\n|\r|\n/', $images_list);
17687 + $images_arr = array_map('trim', $images_arr);
17688 + }
17689 + else
17690 + {
17691 + $images_arr = array();
17692 + }
17693 + require_once(__DIR__ . '/res/amazon-direct.php');
17694 + while(true)
17695 + {
17696 + $thread_id = '';
17697 + $my_title_outro = '';
17698 + if ($count > intval($max)) {
17699 + break;
17700 + }
17701 + $update_post_id = '';
17702 + $product_description = '';
17703 + $current_section = '';
17704 + $post_sections = array();
17705 + $post_topic = '';
17706 + $post_title_keywords = '';
17707 + $headings_arr_copy = $headings_arr;
17708 + $added_img_list = array();
17709 + $raw_img_list = array();
17710 + $full_result_list = array();
17711 + $added_images = 0;
17712 + $heading_results = array();
17713 + if(count($amazon_keyword_lines) == 0)
17714 + {
17715 + break;
17716 + }
17717 + if ($count > intval($max)) {
17718 + break;
17719 + }
17720 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
17721 + {
17722 + $current_index = array_rand($amazon_keyword_lines);
17723 + }
17724 + else
17725 + {
17726 + $current_index = array_key_first($amazon_keyword_lines);
17727 + }
17728 + $amazon_kw = trim($amazon_keyword_lines[$current_index]);
17729 + if($title_once == '1')
17730 + {
17731 + unset($amazon_keyword_lines[$current_index]);
17732 + }
17733 + $ptlprocessed = explode('!###!', $amazon_kw);
17734 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
17735 + {
17736 + $amazon_kw = $ptlprocessed[0];
17737 + $post_title_keywords = $ptlprocessed[1];
17738 + }
17739 + if(isset($rss_feeds[$amazon_kw]))
17740 + {
17741 + $post_link = $rss_feeds[$amazon_kw]['url'];
17742 + $user_name = $rss_feeds[$amazon_kw]['author'];
17743 + $post_cats = $rss_feeds[$amazon_kw]['cats'];
17744 + $post_excerpt = $rss_feeds[$amazon_kw]['excerpt'];
17745 + $final_content = $rss_feeds[$amazon_kw]['content'];
17746 + }
17747 + $tprepp = $spintax->Parse($amazon_kw);
17748 + if($tprepp != false && $tprepp != '')
17749 + {
17750 + $amazon_kw = $tprepp;
17751 + }
17752 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
17753 + {
17754 + aiomatic_log_to_file('Starting processing Amazon Roundup keyword: ' . $amazon_kw);
17755 + }
17756 + $old_title = $amazon_kw;
17757 + $already_spinned = 0;
17758 + $allmodels = aiomatic_get_all_models();
17759 + if($custom_shortcodes != '')
17760 + {
17761 + $custom_shortcodes_arr = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
17762 + foreach($custom_shortcodes_arr as $my_short)
17763 + {
17764 + $name_part = explode('=>', $my_short);
17765 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
17766 + {
17767 + $shortname = trim($name_part[0]);
17768 + if(strstr($amazon_kw, '%%' . $shortname . '%%'))
17769 + {
17770 + $shortval = '';
17771 + $name_part[1] = str_replace('%%search_keywords%%', $amazon_kw, $name_part[1]);
17772 + $ai_part = explode('@@', $name_part[1]);
17773 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
17774 + {
17775 + if(!in_array(trim($ai_part[0]), $allmodels))
17776 + {
17777 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
17778 + }
17779 + else
17780 + {
17781 + $aimodel = trim($ai_part[0]);
17782 + }
17783 + $zai_command = trim($ai_part[1]);
17784 + $zai_command = aiomatic_replaceSynergyShortcodes($zai_command);
17785 + $zai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $zai_command);
17786 + foreach($custom_shortcodes_arr as $my_check_short)
17787 + {
17788 + $check_name_part = explode('=>', $my_check_short);
17789 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
17790 + {
17791 + $check_shortname = trim($check_name_part[0]);
17792 + if($shortname !== $check_shortname && strstr($zai_command, '%%' . $check_shortname . '%%') !== false)
17793 + {
17794 + $check_shortval = '';
17795 + $check_ai_part = explode('@@', $check_name_part[1]);
17796 + if(!in_array(trim($check_ai_part[0]), $allmodels))
17797 + {
17798 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
17799 + }
17800 + else
17801 + {
17802 + $check_aimodel = trim($check_ai_part[0]);
17803 + }
17804 + $check_ai_command = trim($check_ai_part[1]);
17805 + $aierror = '';
17806 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
17807 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
17808 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
17809 + if($check_generated_text === false)
17810 + {
17811 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
17812 + }
17813 + else
17814 + {
17815 + $check_shortval = trim(trim($check_generated_text), '.');
17816 + }
17817 + $zai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $zai_command);
17818 + }
17819 + }
17820 + }
17821 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating custom shortcodes'));
17822 + $aierror = '';
17823 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $zai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
17824 + if($generated_text === false)
17825 + {
17826 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
17827 + }
17828 + else
17829 + {
17830 + $shortval = trim(trim($generated_text), '.');
17831 + }
17832 + }
17833 + $amazon_kw = str_replace('%%' . $shortname . '%%', $shortval, $amazon_kw);
17834 + }
17835 + }
17836 + }
17837 + }
17838 + $amazon_kw = aiomatic_replaceSynergyShortcodes($amazon_kw);
17839 + $amazon_kw = apply_filters('aiomatic_replace_aicontent_shortcode', $amazon_kw);
17840 + if(empty($amazon_kw))
17841 + {
17842 + aiomatic_log_to_file('Empty Amazon Keyword Submitted: ' . $amazon_kw);
17843 + unset($amazon_keyword_lines[$current_index]);
17844 + continue;
17845 + }
17846 + if(stristr($max_products, '-') !== false)
17847 + {
17848 + preg_match('/\s*(\d+)\s*-\s*(\d+)\s*/', $max_products, $matches);
17849 + if(isset($matches[2]) && $matches[2] != '' && isset($matches[1]) && $matches[1] != '')
17850 + {
17851 + $rmin = intval($matches[1]);
17852 + $rmax = intval($matches[2]);
17853 + $max_prod = mt_rand($rmin, $rmax);
17854 + }
17855 + else
17856 + {
17857 + $max_prod = $max_products;
17858 + }
17859 + }
17860 + else
17861 + {
17862 + $max_prod = $max_products;
17863 + }
17864 + $max_prod = intval($max_prod);
17865 + $amazresult = aiomatic_amazon_get_post($amazon_kw, trim($affiliate_id), $target_country, $min_price, $max_price, $sort_results, $max_prod, '1', array());
17866 + if(is_array($amazresult) && isset($amazresult['status']) && $amazresult['status'] == 'nothing')
17867 + {
17868 + aiomatic_log_to_file('No results found for keyword search: ' . $amazon_kw);
17869 + unset($amazon_keyword_lines[$current_index]);
17870 + continue;
17871 + }
17872 + if(!is_array($amazresult))
17873 + {
17874 + aiomatic_log_to_file('Empty response from Amazon: ' . $amazon_kw);
17875 + unset($amazon_keyword_lines[$current_index]);
17876 + continue;
17877 + }
17878 + if(is_array($amazresult) && count($amazresult) == 0)
17879 + {
17880 + aiomatic_log_to_file('Empty results found for keyword search: ' . $amazon_kw);
17881 + unset($amazon_keyword_lines[$current_index]);
17882 + continue;
17883 + }
17884 + if($shuffle_products == '1')
17885 + {
17886 + shuffle($amazresult);
17887 + }
17888 + $product_titles = '';
17889 + foreach($amazresult as $amr)
17890 + {
17891 + $product_titles .= '"' . $amr->offer_title . '",';
17892 + }
17893 + $product_titles = rtrim($product_titles, ',');
17894 + $post_title = '';
17895 + $amazon_thumb = $amazresult[array_rand($amazresult)]->product_img;
17896 + $product_title = '';
17897 + $product_reviews = '';
17898 + $aff_url = '';
17899 + $product_author = '';
17900 + $product_brand = '';
17901 + $product_isbn = '';
17902 + $product_upc = '';
17903 + $offer_price = '';
17904 + $product_price = '';
17905 + $product_list_price = '';
17906 + $price_numeric = '';
17907 + $price_currency = '';
17908 + $product_asin = '';
17909 + $cart_url = '';
17910 + $list_price_numeric = '';
17911 + $product_imgs = '';
17912 + //new
17913 + $item_score = '';
17914 + $edition = '';
17915 + $language = '';
17916 + $pages_count = '';
17917 + $publication_date = '';
17918 + $contributors = '';
17919 + $manufacturer = '';
17920 + $binding = '';
17921 + $product_group = '';
17922 + $rating = '';
17923 + $eans = '';
17924 + $part_no = '';
17925 + $model = '';
17926 + $warranty = '';
17927 + $color = '';
17928 + $is_adult = '';
17929 + $dimensions = '';
17930 + $date = '';
17931 + $size = '';
17932 + $unit_count = '';
17933 + if(!empty($global_prepend))
17934 + {
17935 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
17936 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
17937 + $global_prepend = aiomatic_replace_amazon($global_prepend, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
17938 + }
17939 + if(!empty($global_append))
17940 + {
17941 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
17942 + $global_append = aiomatic_replace_amazon($global_append, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
17943 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
17944 + }
17945 + if($title_prompt != '')
17946 + {
17947 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
17948 + {
17949 + $title_prompt_arr = preg_split('/\r\n|\r|\n/', $title_prompt);
17950 + $my_title_prompt = $title_prompt_arr[array_rand($title_prompt_arr)];
17951 + }
17952 + else
17953 + {
17954 + $my_title_prompt = $title_prompt;
17955 + }
17956 + $zatitle_prompt = aiomatic_replace_amazon($my_title_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
17957 + if(stristr($run_regex_on, 'title') !== false)
17958 + {
17959 + if ($strip_by_regex_prompts !== '')
17960 + {
17961 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
17962 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
17963 + $xcnt = 0;
17964 + foreach($xstrip_by_regex as $sbr)
17965 + {
17966 + if(isset($xreplace_regex[$xcnt]))
17967 + {
17968 + $repreg = $xreplace_regex[$xcnt];
17969 + }
17970 + else
17971 + {
17972 + $repreg = '';
17973 + }
17974 + $xcnt++;
17975 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $zatitle_prompt);
17976 + if($temp_cont !== NULL)
17977 + {
17978 + $zatitle_prompt = $temp_cont;
17979 + }
17980 + }
17981 + }
17982 + }
17983 + if($zatitle_prompt != '')
17984 + {
17985 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon title'));
17986 + $written = aiomatic_write_content($zatitle_prompt, $topic_title_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'title', 'user', $assistant_id, $thread_id, $store_data);
17987 + if($written === false)
17988 + {
17989 + aiomatic_log_to_file('Failed to generate title!');
17990 + if($auto == 1)
17991 + {
17992 + aiomatic_clearFromList($param, $type);
17993 + }
17994 + return 'fail';
17995 + }
17996 + else
17997 + {
17998 + $written = ucfirst(trim(trim($written), '.'));
17999 + $post_title = $written;
18000 + }
18001 + }
18002 + else
18003 + {
18004 + aiomatic_log_to_file('Empty processed title prompt. Nothing to do.');
18005 + if($auto == 1)
18006 + {
18007 + aiomatic_clearFromList($param, $type);
18008 + }
18009 + return 'fail';
18010 + }
18011 + }
18012 + if(empty($post_title))
18013 + {
18014 + aiomatic_log_to_file('Failed to automatically generate Amazon product roundup title!');
18015 + if($auto == 1)
18016 + {
18017 + aiomatic_clearFromList($param, $type);
18018 + }
18019 + return 'fail';
18020 + }
18021 + if (strpos($post_title, '%%') === false)
18022 + {
18023 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
18024 + {
18025 + $xposts = get_posts(
18026 + array(
18027 + 'post_type' => $post_type,
18028 + 'title' => html_entity_decode($post_title),
18029 + 'post_status' => 'all',
18030 + 'numberposts' => 1,
18031 + 'update_post_term_cache' => false,
18032 + 'update_post_meta_cache' => false,
18033 + 'orderby' => 'post_date ID',
18034 + 'order' => 'ASC',
18035 + )
18036 + );
18037 + if ( ! empty( $xposts ) ) {
18038 + $zap = $xposts[0];
18039 + } else {
18040 + $zap = null;
18041 + }
18042 + if($zap !== null)
18043 + {
18044 + if($overwrite_existing)
18045 + {
18046 + $update_post_id = $zap->ID;
18047 + }
18048 + else
18049 + {
18050 + aiomatic_log_to_file('Post with specified title already existing, skipping it: ' . $post_title);
18051 + unset($amazon_keyword_lines[$current_index]);
18052 + continue;
18053 + }
18054 + }
18055 + }
18056 + $new_post_title = $post_title;
18057 + }
18058 + else
18059 + {
18060 + $new_post_title = $post_title;
18061 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
18062 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
18063 + {
18064 + $xposts = get_posts(
18065 + array(
18066 + 'post_type' => $post_type,
18067 + 'title' => html_entity_decode($new_post_title),
18068 + 'post_status' => 'all',
18069 + 'numberposts' => 1,
18070 + 'update_post_term_cache' => false,
18071 + 'update_post_meta_cache' => false,
18072 + 'orderby' => 'post_date ID',
18073 + 'order' => 'ASC',
18074 + )
18075 + );
18076 + if ( ! empty( $xposts ) ) {
18077 + $zap = $xposts[0];
18078 + } else {
18079 + $zap = null;
18080 + }
18081 + if($zap !== null)
18082 + {
18083 + if($overwrite_existing)
18084 + {
18085 + $update_post_id = $zap->ID;
18086 + }
18087 + else
18088 + {
18089 + aiomatic_log_to_file('Post with specified title already published, skipping it: ' . $new_post_title);
18090 + unset($amazon_keyword_lines[$current_index]);
18091 + continue;
18092 + }
18093 + }
18094 + }
18095 + }
18096 + $current_section = '';
18097 + $headings_arr_copy = $headings_arr;
18098 + $added_img_list = array();
18099 + $raw_img_list = array();
18100 + $full_result_list = array();
18101 + $added_vid_list = array();
18102 + $added_images = 0;
18103 + $added_videos = 0;
18104 + $heading_results = array();
18105 + if(empty($post_title))
18106 + {
18107 + aiomatic_log_to_file('Empty post title submitted, nothing to do!');
18108 + continue;
18109 + }
18110 + $post_sections = array();
18111 + foreach($amazresult as $amr)
18112 + {
18113 + $post_sections[] = $amr->offer_title;
18114 + }
18115 + $new_post_content = '';
18116 + if(stristr($run_regex_on, 'intro') !== false)
18117 + {
18118 + if ($strip_by_regex_prompts !== '')
18119 + {
18120 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18121 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18122 + $xcnt = 0;
18123 + foreach($xstrip_by_regex as $sbr)
18124 + {
18125 + if(isset($xreplace_regex[$xcnt]))
18126 + {
18127 + $repreg = $xreplace_regex[$xcnt];
18128 + }
18129 + else
18130 + {
18131 + $repreg = '';
18132 + }
18133 + $xcnt++;
18134 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $intro_prompt);
18135 + if($temp_cont !== NULL)
18136 + {
18137 + $intro_prompt = $temp_cont;
18138 + }
18139 + }
18140 + }
18141 + }
18142 + if($intro_prompt != '')
18143 + {
18144 + $my_intro_prompt = $intro_prompt;
18145 + $my_intro_prompt = aiomatic_replace_amazon($my_intro_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18146 + if($my_intro_prompt != '')
18147 + {
18148 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon intro'));
18149 + $written = aiomatic_write_content($my_intro_prompt, $topic_intro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'intro', 'user', $assistant_id, $thread_id, $store_data);
18150 + if($written === false)
18151 + {
18152 + aiomatic_log_to_file('Failed to generate intro for section: ' . $current_section);
18153 + continue;
18154 + }
18155 + else
18156 + {
18157 + if($new_post_content != '')
18158 + {
18159 + $new_post_content .= ' ';
18160 + }
18161 + $new_post_content .= $written;
18162 + }
18163 + }
18164 + else
18165 + {
18166 + aiomatic_log_to_file('Empty processed intro prompt. Nothing to do.');
18167 + }
18168 + }
18169 + if($title_outro != '')
18170 + {
18171 + $my_title_outro = $title_outro;
18172 + $my_title_outro = aiomatic_replace_amazon($my_title_outro, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18173 + $my_title_outro = $spintax->Parse(trim($my_title_outro));
18174 + }
18175 + if(!empty($title_toc))
18176 + {
18177 + $my_title_toc = $title_toc;
18178 + $my_title_toc = aiomatic_replace_amazon($my_title_toc, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18179 + $my_title_toc = $spintax->Parse(trim($my_title_toc));
18180 + }
18181 + if($title_qa != '')
18182 + {
18183 + $my_title_qa = $title_qa;
18184 + $my_title_qa = aiomatic_replace_amazon($my_title_qa, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18185 + $my_title_qa = $spintax->Parse(trim($my_title_qa));
18186 + }
18187 + if($content_prompt != '')
18188 + {
18189 + $post_sections_arr = $amazresult;
18190 + $toc = '';
18191 + if($enable_toc == '1')
18192 + {
18193 + if(!empty($title_toc))
18194 + {
18195 + if($my_title_toc != '')
18196 + {
18197 + $toc .= '<h2>' . $my_title_toc . '</h2>';
18198 + }
18199 + }
18200 + $toc .= '<ul class="toc-class">';
18201 + foreach($post_sections_arr as $the_current_section)
18202 + {
18203 + if(!empty($current_section))
18204 + {
18205 + $toc .= '<li><a href="#' . sanitize_title($the_current_section->offer_title) . '">' . $the_current_section->offer_title . '</a></li>';
18206 + }
18207 + }
18208 + if($title_qa != '')
18209 + {
18210 + if($my_title_qa != '')
18211 + {
18212 + if($qa_prompt != '' && $enable_qa == '1')
18213 + {
18214 + $toc .= '<li><a href="#qa">' . $my_title_qa . '</a></li>';
18215 + }
18216 + }
18217 + }
18218 + if($title_outro != '')
18219 + {
18220 + if($my_title_outro != '')
18221 + {
18222 + if($outro_prompt != '')
18223 + {
18224 + $toc .= '<li><a href="#outro">' . $my_title_outro . '</a></li>';
18225 + }
18226 + }
18227 + }
18228 + $toc .= '</ul>';
18229 + $new_post_content .= '<br/>' . $toc . '<br/>';
18230 + }
18231 + $topic_count = 1;
18232 + $last_section_content = '';
18233 + foreach($post_sections_arr as $the_current_section)
18234 + {
18235 + if(trim($affiliate_id) != '')
18236 + {
18237 + $aff_url = $the_current_section->offer_url;
18238 + }
18239 + else
18240 + {
18241 + $aff_url = $the_current_section->source_link;
18242 + }
18243 + $current_section = trim($the_current_section->offer_title);
18244 + $current_section = trim($current_section, '.;');
18245 + $product_title = $current_section;
18246 + if(empty($current_section))
18247 + {
18248 + continue;
18249 + }
18250 + $my_post_content = $content_prompt;
18251 + $product_description = $the_current_section->offer_desc;
18252 + $product_reviews = implode('<br><br>', $the_current_section->item_reviews);
18253 + $product_author = $the_current_section->product_author;
18254 + $product_brand = $the_current_section->product_brand;
18255 + $product_isbn = $the_current_section->product_isbn;
18256 + $product_upc = $the_current_section->product_upc;
18257 + $offer_price = $the_current_section->offer_price;
18258 + $product_price = $the_current_section->product_price;
18259 + $product_list_price = $the_current_section->product_list_price;
18260 + $price_numeric = $the_current_section->price_numeric;
18261 + $price_currency = $the_current_section->price_currency;
18262 + $product_asin = $the_current_section->product_asin;
18263 + $cart_url = $the_current_section->cart_url;
18264 + $list_price_numeric = $the_current_section->list_price_numeric;
18265 + $product_imgs = $the_current_section->product_imgs;
18266 + //new
18267 + $item_score = $the_current_section->item_score;
18268 + $language = $the_current_section->language;
18269 + $edition = $the_current_section->edition;
18270 + $pages_count = $the_current_section->pages_count;
18271 + $publication_date = $the_current_section->publication_date;
18272 + $contributors = $the_current_section->contributors;
18273 + $manufacturer = $the_current_section->manufacturer;
18274 + $binding = $the_current_section->binding;
18275 + $product_group = $the_current_section->product_group;
18276 + $rating = $the_current_section->rating;
18277 + $eans = $the_current_section->eans;
18278 + $part_no = $the_current_section->part_no;
18279 + $model = $the_current_section->model;
18280 + $warranty = $the_current_section->warranty;
18281 + $color = $the_current_section->color;
18282 + $is_adult = $the_current_section->is_adult;
18283 + $dimensions = $the_current_section->dimensions;
18284 + $date = $the_current_section->date;
18285 + $size = $the_current_section->size;
18286 + $unit_count = $the_current_section->unit_count;
18287 +
18288 + $my_post_content = aiomatic_replace_amazon($my_post_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18289 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
18290 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
18291 + if(stristr($run_regex_on, 'content') !== false)
18292 + {
18293 + if ($strip_by_regex_prompts !== '')
18294 + {
18295 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18296 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18297 + $xcnt = 0;
18298 + foreach($xstrip_by_regex as $sbr)
18299 + {
18300 + if(isset($xreplace_regex[$xcnt]))
18301 + {
18302 + $repreg = $xreplace_regex[$xcnt];
18303 + }
18304 + else
18305 + {
18306 + $repreg = '';
18307 + }
18308 + $xcnt++;
18309 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
18310 + if($temp_cont !== NULL)
18311 + {
18312 + $my_post_content = $temp_cont;
18313 + }
18314 + }
18315 + }
18316 + }
18317 + if($first_hand == '1')
18318 + {
18319 + $my_post_content = str_replace('%%first_hand_experience_prompt%%', 'Write as if you had first-hand experience with the product you are describing.', $my_post_content);
18320 + }
18321 + else
18322 + {
18323 + $my_post_content = str_replace('%%first_hand_experience_prompt%%', '', $my_post_content);
18324 + }
18325 + $my_post_content = str_replace('%%product_author%%', $the_current_section->product_author, $my_post_content);
18326 + $my_post_content = str_replace('%%product_brand%%', $the_current_section->product_brand, $my_post_content);
18327 + $my_post_content = str_replace('%%product_isbn%%', $the_current_section->product_isbn, $my_post_content);
18328 + $my_post_content = str_replace('%%product_upc%%', $the_current_section->product_upc, $my_post_content);
18329 + $my_post_content = str_replace('%%offer_url%%', $the_current_section->offer_url, $my_post_content);
18330 + $my_post_content = str_replace('%%offer_price%%', $the_current_section->offer_price, $my_post_content);
18331 + $my_post_content = str_replace('%%product_list_price%%', $the_current_section->product_list_price, $my_post_content);
18332 + $my_post_content = str_replace('%%offer_img%%', $the_current_section->offer_img, $my_post_content);
18333 + $my_post_content = str_replace('%%price_numeric%%', $the_current_section->price_numeric, $my_post_content);
18334 + $my_post_content = str_replace('%%price_currency%%', $the_current_section->price_currency, $my_post_content);
18335 + $my_post_content = str_replace('%%review_link%%', $the_current_section->review_link, $my_post_content);
18336 + $my_post_content = str_replace('%%product_asin%%', $the_current_section->product_asin, $my_post_content);
18337 + $my_post_content = str_replace('%%cart_url%%', $the_current_section->cart_url, $my_post_content);
18338 + $my_post_content = str_replace('%%list_price_numeric%%', $the_current_section->list_price_numeric, $my_post_content);
18339 + $my_post_content = str_replace('%%product_imgs%%', $the_current_section->product_imgs, $my_post_content);
18340 + $my_post_content = str_replace('%%product_imgs_html%%', $the_current_section->product_imgs_html, $my_post_content);
18341 + $my_post_content = str_replace('%%price_with_discount_fixed%%', $the_current_section->price_with_discount_fixed, $my_post_content);
18342 + if($my_post_content != '')
18343 + {
18344 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon topic content #' . $topic_count));
18345 + $topic_count++;
18346 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
18347 + if($written === false)
18348 + {
18349 + aiomatic_log_to_file('Failed to generate content for section: ' . $current_section);
18350 + continue;
18351 + }
18352 + else
18353 + {
18354 + $written = str_ireplace($current_section, '', $written);
18355 + preg_match_all('#\d+\.\s*([\s\S]*)#', $current_section, $mxatches);
18356 + if(isset($mxatches[1][0]))
18357 + {
18358 + $written = str_ireplace(trim($mxatches[1][0]), '', $written);
18359 + $written = str_ireplace(str_replace(['"', '\''], '', $mxatches[1][0]), '', $written);
18360 + }
18361 + $written = str_ireplace('<h2></h2>', '', $written);
18362 + $written = str_ireplace('<h3></h3>', '', $written);
18363 + $link_to_source = $current_section;
18364 + if($no_headlink != '1')
18365 + {
18366 + $link_to_source = '<a target="_blank" rel="nofollow sponsored noopener" href="' . $aff_url . '">' . $current_section . '</a>';
18367 + }
18368 + if($sections_role == 'h2')
18369 + {
18370 + $new_post_content .= '<h2 id="' . sanitize_title($current_section) . '">' . $link_to_source . '</h2>';
18371 + }
18372 + elseif($sections_role == 'h3')
18373 + {
18374 + $new_post_content .= '<h3 id="' . sanitize_title($current_section) . '">' . $link_to_source . '</h3>';
18375 + }
18376 + elseif($sections_role == 'b')
18377 + {
18378 + $new_post_content .= '<b id="' . sanitize_title($current_section) . '">' . $link_to_source . '</b>';
18379 + }
18380 + elseif($sections_role == 'i')
18381 + {
18382 + $new_post_content .= '<i id="' . sanitize_title($current_section) . '">' . $link_to_source . '</i>';
18383 + }
18384 + elseif($sections_role == 'bi')
18385 + {
18386 + $new_post_content .= '<b><i id="' . sanitize_title($current_section) . '">' . $link_to_source . '</i></b>';
18387 + }
18388 + elseif($sections_role == 'p')
18389 + {
18390 + $new_post_content .= '<p id="' . sanitize_title($current_section) . '">' . $link_to_source . '</p>';
18391 + }
18392 + elseif($sections_role == 'x')
18393 + {
18394 + $new_post_content .= '<br/><span id="' . sanitize_title($current_section) . '">' . $link_to_source . '</span><br/>';
18395 + }
18396 + else
18397 + {
18398 + $new_post_content .= '<br/>' . $link_to_source . '<br/>';
18399 + }
18400 + if($topic_images == '1')
18401 + {
18402 + $show_url = $the_current_section->product_img;
18403 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
18404 + {
18405 + $localpath = aiomatic_copy_image_locally($show_url, $attached_id, '', false, '');
18406 + if(!empty($attached_id))
18407 + {
18408 + $additional_attachments[] = $attached_id;
18409 + }
18410 + if($localpath !== false)
18411 + {
18412 + $show_url = $localpath[0];
18413 + }
18414 + }
18415 + $new_post_content .= '<br/><img class="rimage_class" src="' . $show_url . '" alt="' . esc_attr($post_title) . '"><br/>';
18416 + $added_img_list[] = $show_url;
18417 + $added_images++;
18418 + }
18419 + $last_section_content = $written;
18420 + $new_post_content .= $written;
18421 + }
18422 + }
18423 + else
18424 + {
18425 + aiomatic_log_to_file('Empty processed content prompt. Nothing to do.');
18426 + }
18427 + }
18428 + }
18429 + if($enable_qa == '1')
18430 + {
18431 + if($qa_prompt != '')
18432 + {
18433 + $my_qa_prompt = $qa_prompt;
18434 + $my_qa_prompt = aiomatic_replace_amazon($my_qa_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18435 + if(stristr($run_regex_on, 'qa') !== false)
18436 + {
18437 + if ($strip_by_regex_prompts !== '')
18438 + {
18439 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18440 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18441 + $xcnt = 0;
18442 + foreach($xstrip_by_regex as $sbr)
18443 + {
18444 + if(isset($xreplace_regex[$xcnt]))
18445 + {
18446 + $repreg = $xreplace_regex[$xcnt];
18447 + }
18448 + else
18449 + {
18450 + $repreg = '';
18451 + }
18452 + $xcnt++;
18453 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_qa_prompt);
18454 + if($temp_cont !== NULL)
18455 + {
18456 + $my_qa_prompt = $temp_cont;
18457 + }
18458 + }
18459 + }
18460 + }
18461 + if($my_qa_prompt != '')
18462 + {
18463 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon qa'));
18464 + $written = aiomatic_write_content($my_qa_prompt, $topic_qa_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'qa', 'user', $assistant_id, $thread_id, $store_data);
18465 + if($written === false)
18466 + {
18467 + aiomatic_log_to_file('Failed to generate Q&A for topic: ' . $my_qa_prompt);
18468 + continue;
18469 + }
18470 + else
18471 + {
18472 + if($new_post_content != '')
18473 + {
18474 + $new_post_content .= ' ';
18475 + }
18476 + if($my_title_qa !== '')
18477 + {
18478 + if($sections_role == 'h2')
18479 + {
18480 + $new_post_content .= '<h2 id="qa">' . $my_title_qa . '</h2>';
18481 + }
18482 + elseif($sections_role == 'h3')
18483 + {
18484 + $new_post_content .= '<h3 id="qa">' . $my_title_qa . '</h3>';
18485 + }
18486 + elseif($sections_role == 'b')
18487 + {
18488 + $new_post_content .= '<b id="qa">' . $my_title_qa . '</b>';
18489 + }
18490 + elseif($sections_role == 'i')
18491 + {
18492 + $new_post_content .= '<i id="qa">' . $my_title_qa . '</i>';
18493 + }
18494 + elseif($sections_role == 'bi')
18495 + {
18496 + $new_post_content .= '<b><i id="qa">' . $my_title_qa . '</i></b>';
18497 + }
18498 + elseif($sections_role == 'p')
18499 + {
18500 + $new_post_content .= '<p id="qa">' . $my_title_qa . '</p>';
18501 + }
18502 + elseif($sections_role == 'x')
18503 + {
18504 + $new_post_content .= '<br/><span id="qa">' . $my_title_qa . '</span><br/>';
18505 + }
18506 + else
18507 + {
18508 + $new_post_content .= '<br/>' . $my_title_qa . '<br/>';
18509 + }
18510 + }
18511 + $new_post_content .= $written;
18512 + }
18513 + }
18514 + else
18515 + {
18516 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
18517 + }
18518 + }
18519 + }
18520 + if($enable_table == '1')
18521 + {
18522 + if($table_prompt != '')
18523 + {
18524 + $my_table_prompt = $table_prompt;
18525 + $my_table_prompt = aiomatic_replace_amazon($my_table_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18526 + if(stristr($run_regex_on, 'table') !== false)
18527 + {
18528 + if ($strip_by_regex_prompts !== '')
18529 + {
18530 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18531 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18532 + $xcnt = 0;
18533 + foreach($xstrip_by_regex as $sbr)
18534 + {
18535 + if(isset($xreplace_regex[$xcnt]))
18536 + {
18537 + $repreg = $xreplace_regex[$xcnt];
18538 + }
18539 + else
18540 + {
18541 + $repreg = '';
18542 + }
18543 + $xcnt++;
18544 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_table_prompt);
18545 + if($temp_cont !== NULL)
18546 + {
18547 + $my_table_prompt = $temp_cont;
18548 + }
18549 + }
18550 + }
18551 + }
18552 + if($my_table_prompt != '')
18553 + {
18554 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon table'));
18555 + $written = aiomatic_write_content($my_table_prompt, $topic_table_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'qa', 'user', $assistant_id, $thread_id, $store_data);
18556 + if($written === false)
18557 + {
18558 + aiomatic_log_to_file('Failed to generate Comparison Table for topic: ' . $my_table_prompt);
18559 + continue;
18560 + }
18561 + else
18562 + {
18563 + if($new_post_content != '')
18564 + {
18565 + $new_post_content .= ' ';
18566 + }
18567 + $new_post_content .= $written;
18568 + }
18569 + }
18570 + else
18571 + {
18572 + aiomatic_log_to_file('Empty processed Comparison Table prompt. Nothing to do.');
18573 + }
18574 + }
18575 + }
18576 + if($outro_prompt != '')
18577 + {
18578 + $my_outro_prompt = $outro_prompt;
18579 + $my_outro_prompt = aiomatic_replace_amazon($my_outro_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18580 + if(stristr($run_regex_on, 'outro') !== false)
18581 + {
18582 + if ($strip_by_regex_prompts !== '')
18583 + {
18584 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18585 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18586 + $xcnt = 0;
18587 + foreach($xstrip_by_regex as $sbr)
18588 + {
18589 + if(isset($xreplace_regex[$xcnt]))
18590 + {
18591 + $repreg = $xreplace_regex[$xcnt];
18592 + }
18593 + else
18594 + {
18595 + $repreg = '';
18596 + }
18597 + $xcnt++;
18598 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_outro_prompt);
18599 + if($temp_cont !== NULL)
18600 + {
18601 + $my_outro_prompt = $temp_cont;
18602 + }
18603 + }
18604 + }
18605 + }
18606 + if($my_outro_prompt != '')
18607 + {
18608 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon outro'));
18609 + $written = aiomatic_write_content($my_outro_prompt, $topic_outro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'outro', 'user', $assistant_id, $thread_id, $store_data);
18610 + if($written === false)
18611 + {
18612 + aiomatic_log_to_file('Failed to generate outro for section: ' . $current_section);
18613 + continue;
18614 + }
18615 + else
18616 + {
18617 + if($new_post_content != '')
18618 + {
18619 + $new_post_content .= ' ';
18620 + }
18621 + if($title_outro !== '')
18622 + {
18623 + if($my_title_outro != '')
18624 + {
18625 + if($sections_role == 'h2')
18626 + {
18627 + $new_post_content .= '<h2 id="outro">' . $my_title_outro . '</h2>';
18628 + }
18629 + elseif($sections_role == 'h3')
18630 + {
18631 + $new_post_content .= '<h3 id="outro">' . $my_title_outro . '</h3>';
18632 + }
18633 + elseif($sections_role == 'b')
18634 + {
18635 + $new_post_content .= '<b id="outro">' . $my_title_outro . '</b>';
18636 + }
18637 + elseif($sections_role == 'i')
18638 + {
18639 + $new_post_content .= '<i id="outro">' . $my_title_outro . '</i>';
18640 + }
18641 + elseif($sections_role == 'bi')
18642 + {
18643 + $new_post_content .= '<b><i id="outro">' . $my_title_outro . '</i></b>';
18644 + }
18645 + elseif($sections_role == 'p')
18646 + {
18647 + $new_post_content .= '<p id="outro">' . $my_title_outro . '</p>';
18648 + }
18649 + elseif($sections_role == 'x')
18650 + {
18651 + $new_post_content .= '<br/><span id="outro">' . $my_title_outro . '</span><br/>';
18652 + }
18653 + else
18654 + {
18655 + $new_post_content .= '<br/>' . $my_title_outro . '<br/>';
18656 + }
18657 + }
18658 + }
18659 + else
18660 + {
18661 + $new_post_content .= '<br/><br/>';
18662 + }
18663 + $new_post_content .= $written;
18664 + }
18665 + }
18666 + else
18667 + {
18668 + aiomatic_log_to_file('Empty processed outro prompt. Nothing to do.');
18669 + }
18670 + }
18671 + if($new_post_content == '')
18672 + {
18673 + aiomatic_log_to_file("Warning, empty post content because of empty content prompt!");
18674 + }
18675 +
18676 + $new_post_excerpt = '';
18677 + $current_section = implode(',', $post_sections);
18678 + if($excerpt_prompt != '')
18679 + {
18680 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
18681 + {
18682 + $excerpt_prompt_arr = preg_split('/\r\n|\r|\n/', $excerpt_prompt);
18683 + $my_post_excerpt = $excerpt_prompt_arr[array_rand($excerpt_prompt_arr)];
18684 + }
18685 + else
18686 + {
18687 + $my_post_excerpt = $excerpt_prompt;
18688 + }
18689 + $my_post_excerpt = aiomatic_replace_amazon($my_post_excerpt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
18690 + if(stristr($run_regex_on, 'excerpt') !== false)
18691 + {
18692 + if ($strip_by_regex_prompts !== '')
18693 + {
18694 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
18695 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
18696 + $xcnt = 0;
18697 + foreach($xstrip_by_regex as $sbr)
18698 + {
18699 + if(isset($xreplace_regex[$xcnt]))
18700 + {
18701 + $repreg = $xreplace_regex[$xcnt];
18702 + }
18703 + else
18704 + {
18705 + $repreg = '';
18706 + }
18707 + $xcnt++;
18708 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_excerpt);
18709 + if($temp_cont !== NULL)
18710 + {
18711 + $my_post_excerpt = $temp_cont;
18712 + }
18713 + }
18714 + }
18715 + }
18716 + if($my_post_excerpt != '')
18717 + {
18718 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon excerpt'));
18719 + $written = aiomatic_write_content($my_post_excerpt, $topic_excerpt_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'excerpt', 'user', $assistant_id, $thread_id, $store_data);
18720 + if($written === false)
18721 + {
18722 + aiomatic_log_to_file('Failed to generate excerpt!');
18723 + }
18724 + else
18725 + {
18726 + $new_post_excerpt = $written;
18727 + }
18728 + }
18729 + else
18730 + {
18731 + aiomatic_log_to_file('Empty processed excerpt prompt. Nothing to do.');
18732 + }
18733 + }
18734 +
18735 + $get_img = '';
18736 + if($royalty_free == '1')
18737 + {
18738 + if($enable_ai_images == '1')
18739 + {
18740 + $query_words = $post_title;
18741 + if($image_query == '')
18742 + {
18743 + $image_query = $temp_post;
18744 + }
18745 + if($orig_ai_command_image == '')
18746 + {
18747 + $orig_ai_command_image = $image_query;
18748 + }
18749 + if($orig_ai_command_image != '')
18750 + {
18751 + $ai_command_image = $orig_ai_command_image;
18752 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
18753 + {
18754 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
18755 + $ai_command_image = array_filter($ai_command_image);
18756 + if(count($ai_command_image) > 0)
18757 + {
18758 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
18759 + }
18760 + else
18761 + {
18762 + $ai_command_image = '';
18763 + }
18764 + }
18765 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
18766 + if(!empty($ai_command_image))
18767 + {
18768 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
18769 + }
18770 + else
18771 + {
18772 + $ai_command_image = trim(strip_tags($post_title));
18773 + }
18774 + $ai_command_image = trim($ai_command_image);
18775 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
18776 + {
18777 + $txt_content = aiomatic_get_web_page($ai_command_image);
18778 + if ($txt_content !== FALSE)
18779 + {
18780 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
18781 + $txt_content = array_filter($txt_content);
18782 + if(count($txt_content) > 0)
18783 + {
18784 + $txt_content = $txt_content[array_rand($txt_content)];
18785 + if(trim($txt_content) != '')
18786 + {
18787 + $ai_command_image = $txt_content;
18788 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
18789 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
18790 + }
18791 + }
18792 + }
18793 + }
18794 + if(empty($ai_command_image))
18795 + {
18796 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
18797 + }
18798 + else
18799 + {
18800 + if(aiomatic_strlen($ai_command_image) > 1000)
18801 + {
18802 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
18803 + }
18804 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
18805 + {
18806 + $api_service = aiomatic_get_api_service($token, $image_model);
18807 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
18808 + }
18809 + $skip_this_copy = true;
18810 + if(!function_exists('is_plugin_active'))
18811 + {
18812 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
18813 + }
18814 + 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')
18815 + {
18816 + $skip_this_copy = false;
18817 + }
18818 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
18819 + {
18820 + $skip_this_copy = false;
18821 + }
18822 + $aierror = '';
18823 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
18824 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
18825 + if($get_img !== false)
18826 + {
18827 + foreach($get_img as $tmpimg)
18828 + {
18829 + $get_img = $tmpimg;
18830 + break;
18831 + }
18832 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
18833 + {
18834 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
18835 + }
18836 + }
18837 + else
18838 + {
18839 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
18840 + $get_img = '';
18841 + }
18842 + }
18843 + }
18844 + else
18845 + {
18846 + aiomatic_log_to_file('Empty AI featured image query entered.');
18847 + }
18848 + }
18849 + elseif($enable_ai_images == '2')
18850 + {
18851 + $query_words = $post_title;
18852 + if($image_query == '')
18853 + {
18854 + $image_query = $temp_post;
18855 + }
18856 + if($orig_ai_command_image == '')
18857 + {
18858 + $orig_ai_command_image = $image_query;
18859 + }
18860 + if($orig_ai_command_image != '')
18861 + {
18862 + $ai_command_image = $orig_ai_command_image;
18863 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
18864 + {
18865 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
18866 + $ai_command_image = array_filter($ai_command_image);
18867 + if(count($ai_command_image) > 0)
18868 + {
18869 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
18870 + }
18871 + else
18872 + {
18873 + $ai_command_image = '';
18874 + }
18875 + }
18876 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
18877 + if(!empty($ai_command_image))
18878 + {
18879 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
18880 + }
18881 + else
18882 + {
18883 + $ai_command_image = trim(strip_tags($post_title));
18884 + }
18885 + $ai_command_image = trim($ai_command_image);
18886 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
18887 + {
18888 + $txt_content = aiomatic_get_web_page($ai_command_image);
18889 + if ($txt_content !== FALSE)
18890 + {
18891 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
18892 + $txt_content = array_filter($txt_content);
18893 + if(count($txt_content) > 0)
18894 + {
18895 + $txt_content = $txt_content[array_rand($txt_content)];
18896 + if(trim($txt_content) != '')
18897 + {
18898 + $ai_command_image = $txt_content;
18899 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
18900 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
18901 + }
18902 + }
18903 + }
18904 + }
18905 + if(empty($ai_command_image))
18906 + {
18907 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
18908 + }
18909 + else
18910 + {
18911 + if(aiomatic_strlen($ai_command_image) > 2000)
18912 + {
18913 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
18914 + }
18915 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
18916 + {
18917 + $api_service = 'Stability.AI';
18918 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
18919 + }
18920 + if($image_size == '256x256')
18921 + {
18922 + $width = '512';
18923 + $height = '512';
18924 + }
18925 + elseif($image_size == '512x512')
18926 + {
18927 + $width = '512';
18928 + $height = '512';
18929 + }
18930 + elseif($image_size == '1024x1024')
18931 + {
18932 + $width = '1024';
18933 + $height = '1024';
18934 + }
18935 + else
18936 + {
18937 + $width = '512';
18938 + $height = '512';
18939 + }
18940 + $skip_this_copy = true;
18941 + if(!function_exists('is_plugin_active'))
18942 + {
18943 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
18944 + }
18945 + 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')
18946 + {
18947 + $skip_this_copy = false;
18948 + }
18949 + $ierror = '';
18950 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Stable Diffusion image'));
18951 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
18952 + if($get_img !== false)
18953 + {
18954 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
18955 + {
18956 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
18957 + }
18958 + }
18959 + else
18960 + {
18961 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
18962 + $get_img = '';
18963 + }
18964 + }
18965 + }
18966 + else
18967 + {
18968 + aiomatic_log_to_file('Empty AI featured image query entered.');
18969 + }
18970 + }
18971 + elseif($enable_ai_images == '7')
18972 + {
18973 + $query_words = $post_title;
18974 + if($image_query == '')
18975 + {
18976 + $image_query = $temp_post;
18977 + }
18978 + if($orig_ai_command_image == '')
18979 + {
18980 + $orig_ai_command_image = $image_query;
18981 + }
18982 + if($orig_ai_command_image != '')
18983 + {
18984 + $ai_command_image = $orig_ai_command_image;
18985 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
18986 + {
18987 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
18988 + $ai_command_image = array_filter($ai_command_image);
18989 + if(count($ai_command_image) > 0)
18990 + {
18991 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
18992 + }
18993 + else
18994 + {
18995 + $ai_command_image = '';
18996 + }
18997 + }
18998 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
18999 + if(!empty($ai_command_image))
19000 + {
19001 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19002 + }
19003 + else
19004 + {
19005 + $ai_command_image = trim(strip_tags($post_title));
19006 + }
19007 + $ai_command_image = trim($ai_command_image);
19008 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
19009 + {
19010 + $txt_content = aiomatic_get_web_page($ai_command_image);
19011 + if ($txt_content !== FALSE)
19012 + {
19013 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19014 + $txt_content = array_filter($txt_content);
19015 + if(count($txt_content) > 0)
19016 + {
19017 + $txt_content = $txt_content[array_rand($txt_content)];
19018 + if(trim($txt_content) != '')
19019 + {
19020 + $ai_command_image = $txt_content;
19021 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19022 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19023 + }
19024 + }
19025 + }
19026 + }
19027 + if(empty($ai_command_image))
19028 + {
19029 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
19030 + }
19031 + else
19032 + {
19033 + if(aiomatic_strlen($ai_command_image) > 2000)
19034 + {
19035 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
19036 + }
19037 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19038 + {
19039 + $api_service = 'Google';
19040 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
19041 + }
19042 + $skip_this_copy = true;
19043 + if(!function_exists('is_plugin_active'))
19044 + {
19045 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19046 + }
19047 + 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')
19048 + {
19049 + $skip_this_copy = false;
19050 + }
19051 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
19052 + {
19053 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
19054 + }
19055 + else
19056 + {
19057 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
19058 + }
19059 + $ierror = '';
19060 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Google image'));
19061 + $get_img = aiomatic_generate_image_google($ai_command_image, $model, 'featuredGoogleImage', false, $ierror, false);
19062 + if($get_img !== false)
19063 + {
19064 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19065 + {
19066 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
19067 + }
19068 + }
19069 + else
19070 + {
19071 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
19072 + $get_img = '';
19073 + }
19074 + }
19075 + }
19076 + else
19077 + {
19078 + aiomatic_log_to_file('Empty AI featured image query entered.');
19079 + }
19080 + }
19081 + elseif($enable_ai_images == '4')
19082 + {
19083 + $query_words = $post_title;
19084 + if($image_query == '')
19085 + {
19086 + $image_query = $temp_post;
19087 + }
19088 + if($orig_ai_command_image == '')
19089 + {
19090 + $orig_ai_command_image = $image_query;
19091 + }
19092 + if($orig_ai_command_image != '')
19093 + {
19094 + $ai_command_image = $orig_ai_command_image;
19095 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
19096 + {
19097 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
19098 + $ai_command_image = array_filter($ai_command_image);
19099 + if(count($ai_command_image) > 0)
19100 + {
19101 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
19102 + }
19103 + else
19104 + {
19105 + $ai_command_image = '';
19106 + }
19107 + }
19108 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19109 + if(!empty($ai_command_image))
19110 + {
19111 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19112 + }
19113 + else
19114 + {
19115 + $ai_command_image = trim(strip_tags($post_title));
19116 + }
19117 + $ai_command_image = trim($ai_command_image);
19118 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
19119 + {
19120 + $txt_content = aiomatic_get_web_page($ai_command_image);
19121 + if ($txt_content !== FALSE)
19122 + {
19123 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19124 + $txt_content = array_filter($txt_content);
19125 + if(count($txt_content) > 0)
19126 + {
19127 + $txt_content = $txt_content[array_rand($txt_content)];
19128 + if(trim($txt_content) != '')
19129 + {
19130 + $ai_command_image = $txt_content;
19131 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19132 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19133 + }
19134 + }
19135 + }
19136 + }
19137 + if(empty($ai_command_image))
19138 + {
19139 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
19140 + }
19141 + else
19142 + {
19143 + if(aiomatic_strlen($ai_command_image) > 2000)
19144 + {
19145 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
19146 + }
19147 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19148 + {
19149 + $api_service = 'Midjourney';
19150 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
19151 + }
19152 + if($image_size == '256x256')
19153 + {
19154 + $width = '512';
19155 + $height = '512';
19156 + }
19157 + elseif($image_size == '512x512')
19158 + {
19159 + $width = '512';
19160 + $height = '512';
19161 + }
19162 + elseif($image_size == '1024x1024')
19163 + {
19164 + $width = '1024';
19165 + $height = '1024';
19166 + }
19167 + elseif($image_size == '1792x1024')
19168 + {
19169 + $width = '1792';
19170 + $height = '1024';
19171 + }
19172 + elseif($image_size == '1024x1792')
19173 + {
19174 + $width = '1024';
19175 + $height = '1792';
19176 + }
19177 + else
19178 + {
19179 + $width = '512';
19180 + $height = '512';
19181 + }
19182 + $skip_this_copy = true;
19183 + if(!function_exists('is_plugin_active'))
19184 + {
19185 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19186 + }
19187 + 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')
19188 + {
19189 + $skip_this_copy = false;
19190 + }
19191 + $ierror = '';
19192 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Midjourney image'));
19193 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
19194 + if($get_img !== false)
19195 + {
19196 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19197 + {
19198 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
19199 + }
19200 + }
19201 + else
19202 + {
19203 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
19204 + $get_img = '';
19205 + }
19206 + }
19207 + }
19208 + else
19209 + {
19210 + aiomatic_log_to_file('Empty AI featured image query entered.');
19211 + }
19212 + }
19213 + elseif($enable_ai_images == '5')
19214 + {
19215 + $query_words = $post_title;
19216 + if($image_query == '')
19217 + {
19218 + $image_query = $temp_post;
19219 + }
19220 + if($orig_ai_command_image == '')
19221 + {
19222 + $orig_ai_command_image = $image_query;
19223 + }
19224 + if($orig_ai_command_image != '')
19225 + {
19226 + $ai_command_image = $orig_ai_command_image;
19227 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
19228 + {
19229 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
19230 + $ai_command_image = array_filter($ai_command_image);
19231 + if(count($ai_command_image) > 0)
19232 + {
19233 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
19234 + }
19235 + else
19236 + {
19237 + $ai_command_image = '';
19238 + }
19239 + }
19240 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19241 + if(!empty($ai_command_image))
19242 + {
19243 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19244 + }
19245 + else
19246 + {
19247 + $ai_command_image = trim(strip_tags($post_title));
19248 + }
19249 + $ai_command_image = trim($ai_command_image);
19250 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
19251 + {
19252 + $txt_content = aiomatic_get_web_page($ai_command_image);
19253 + if ($txt_content !== FALSE)
19254 + {
19255 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19256 + $txt_content = array_filter($txt_content);
19257 + if(count($txt_content) > 0)
19258 + {
19259 + $txt_content = $txt_content[array_rand($txt_content)];
19260 + if(trim($txt_content) != '')
19261 + {
19262 + $ai_command_image = $txt_content;
19263 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19264 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19265 + }
19266 + }
19267 + }
19268 + }
19269 + if(empty($ai_command_image))
19270 + {
19271 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
19272 + }
19273 + else
19274 + {
19275 + if(aiomatic_strlen($ai_command_image) > 2000)
19276 + {
19277 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
19278 + }
19279 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19280 + {
19281 + $api_service = 'Replicate';
19282 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
19283 + }
19284 + if($image_size == '256x256')
19285 + {
19286 + $width = '512';
19287 + $height = '512';
19288 + }
19289 + elseif($image_size == '512x512')
19290 + {
19291 + $width = '512';
19292 + $height = '512';
19293 + }
19294 + elseif($image_size == '1024x1024')
19295 + {
19296 + $width = '1024';
19297 + $height = '1024';
19298 + }
19299 + elseif($image_size == '1792x1024')
19300 + {
19301 + $width = '1792';
19302 + $height = '1024';
19303 + }
19304 + elseif($image_size == '1024x1792')
19305 + {
19306 + $width = '1024';
19307 + $height = '1792';
19308 + }
19309 + else
19310 + {
19311 + $width = '512';
19312 + $height = '512';
19313 + }
19314 + $skip_this_copy = true;
19315 + if(!function_exists('is_plugin_active'))
19316 + {
19317 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19318 + }
19319 + 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')
19320 + {
19321 + $skip_this_copy = false;
19322 + }
19323 + $ierror = '';
19324 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Replicate image'));
19325 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredReplicateImage', $skip_this_copy, $ierror);
19326 + if($get_img !== false)
19327 + {
19328 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19329 + {
19330 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
19331 + }
19332 + }
19333 + else
19334 + {
19335 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
19336 + $get_img = '';
19337 + }
19338 + }
19339 + }
19340 + else
19341 + {
19342 + aiomatic_log_to_file('Empty AI featured image query entered.');
19343 + }
19344 + }
19345 + elseif($enable_ai_images == '6')
19346 + {
19347 + $query_words = $post_title;
19348 + if($image_query == '')
19349 + {
19350 + $image_query = $temp_post;
19351 + }
19352 + if($orig_ai_command_image == '')
19353 + {
19354 + $orig_ai_command_image = $image_query;
19355 + }
19356 + if($orig_ai_command_image != '')
19357 + {
19358 + $ai_command_image = $orig_ai_command_image;
19359 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
19360 + {
19361 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
19362 + $ai_command_image = array_filter($ai_command_image);
19363 + if(count($ai_command_image) > 0)
19364 + {
19365 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
19366 + }
19367 + else
19368 + {
19369 + $ai_command_image = '';
19370 + }
19371 + }
19372 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19373 + if(!empty($ai_command_image))
19374 + {
19375 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19376 + }
19377 + else
19378 + {
19379 + $ai_command_image = trim(strip_tags($post_title));
19380 + }
19381 + $ai_command_image = trim($ai_command_image);
19382 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
19383 + {
19384 + $txt_content = aiomatic_get_web_page($ai_command_image);
19385 + if ($txt_content !== FALSE)
19386 + {
19387 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19388 + $txt_content = array_filter($txt_content);
19389 + if(count($txt_content) > 0)
19390 + {
19391 + $txt_content = $txt_content[array_rand($txt_content)];
19392 + if(trim($txt_content) != '')
19393 + {
19394 + $ai_command_image = $txt_content;
19395 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19396 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19397 + }
19398 + }
19399 + }
19400 + }
19401 + if(empty($ai_command_image))
19402 + {
19403 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
19404 + }
19405 + else
19406 + {
19407 + if(aiomatic_strlen($ai_command_image) > 2000)
19408 + {
19409 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
19410 + }
19411 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19412 + {
19413 + $api_service = 'Ideogram';
19414 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
19415 + }
19416 + if($image_size == '256x256')
19417 + {
19418 + $width = '512';
19419 + $height = '512';
19420 + }
19421 + elseif($image_size == '512x512')
19422 + {
19423 + $width = '512';
19424 + $height = '512';
19425 + }
19426 + elseif($image_size == '1024x1024')
19427 + {
19428 + $width = '1024';
19429 + $height = '1024';
19430 + }
19431 + elseif($image_size == '1792x1024')
19432 + {
19433 + $width = '1792';
19434 + $height = '1024';
19435 + }
19436 + elseif($image_size == '1024x1792')
19437 + {
19438 + $width = '1024';
19439 + $height = '1792';
19440 + }
19441 + else
19442 + {
19443 + $width = '512';
19444 + $height = '512';
19445 + }
19446 + $skip_this_copy = true;
19447 + if(!function_exists('is_plugin_active'))
19448 + {
19449 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19450 + }
19451 + 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')
19452 + {
19453 + $skip_this_copy = false;
19454 + }
19455 + $ierror = '';
19456 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Ideogram image'));
19457 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredIdeogramImage', $skip_this_copy, $ierror);
19458 + if($get_img !== false)
19459 + {
19460 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19461 + {
19462 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
19463 + }
19464 + }
19465 + else
19466 + {
19467 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
19468 + $get_img = '';
19469 + }
19470 + }
19471 + }
19472 + else
19473 + {
19474 + aiomatic_log_to_file('Empty AI featured image query entered.');
19475 + }
19476 + }
19477 + elseif($enable_ai_images == '3')
19478 + {
19479 + if ($image_url != '')
19480 + {
19481 + $replacement = str_replace(array('[', ']'), '', $query_words);
19482 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
19483 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
19484 + if(isset($matches[2]))
19485 + {
19486 + $chance = trim($matches[2], '[]');
19487 + }
19488 + else
19489 + {
19490 + $chance = '';
19491 + }
19492 + $arv = array();
19493 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
19494 + return $my_img;
19495 + }, $image_url_temp);
19496 + $img_rulx = $spintax->Parse(trim($image_url_temp));
19497 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
19498 + if($selected_img === false)
19499 + {
19500 + $img_rulx = explode(',', $img_rulx);
19501 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
19502 + if($img_rulx != '')
19503 + {
19504 + $get_img = $img_rulx;
19505 + }
19506 + }
19507 + else
19508 + {
19509 + $get_img = $selected_img;
19510 + }
19511 + }
19512 + }
19513 + else
19514 + {
19515 + $image_query_set = false;
19516 + $query_words = '';
19517 + $ai_command_image = '';
19518 + if($orig_ai_command_image == '')
19519 + {
19520 + $orig_ai_command_image = $image_query;
19521 + }
19522 + if($orig_ai_command_image != '')
19523 + {
19524 + $ai_command_image = $orig_ai_command_image;
19525 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
19526 + {
19527 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
19528 + $ai_command_image = array_filter($ai_command_image);
19529 + if(count($ai_command_image) > 0)
19530 + {
19531 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
19532 + }
19533 + else
19534 + {
19535 + $ai_command_image = '';
19536 + }
19537 + }
19538 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19539 + if(!empty($ai_command_image))
19540 + {
19541 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19542 + }
19543 + else
19544 + {
19545 + $ai_command_image = trim(strip_tags($post_title));
19546 + }
19547 + $ai_command_image = trim($ai_command_image);
19548 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
19549 + {
19550 + $txt_content = aiomatic_get_web_page($ai_command_image);
19551 + if ($txt_content !== FALSE)
19552 + {
19553 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19554 + $txt_content = array_filter($txt_content);
19555 + if(count($txt_content) > 0)
19556 + {
19557 + $txt_content = $txt_content[array_rand($txt_content)];
19558 + if(trim($txt_content) != '')
19559 + {
19560 + $ai_command_image = $txt_content;
19561 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
19562 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19563 + }
19564 + }
19565 + }
19566 + }
19567 + }
19568 + if($ai_command_image != '')
19569 + {
19570 + $query_words = $ai_command_image;
19571 + $image_query = $ai_command_image;
19572 + $image_query_set = true;
19573 + }
19574 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
19575 + {
19576 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
19577 + {
19578 + try
19579 + {
19580 + if(!class_exists('TextRazor'))
19581 + {
19582 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
19583 + }
19584 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
19585 + $textrazor = new TextRazor();
19586 + $textrazor->addExtractor('entities');
19587 + $response = $textrazor->analyze($image_query);
19588 + if (isset($response['response']['entities']))
19589 + {
19590 + foreach ($response['response']['entities'] as $entity)
19591 + {
19592 + $query_words = '';
19593 + if(isset($entity['entityEnglishId']))
19594 + {
19595 + $query_words = $entity['entityEnglishId'];
19596 + }
19597 + else
19598 + {
19599 + $query_words = $entity['entityId'];
19600 + }
19601 + if($query_words != '')
19602 + {
19603 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
19604 + if(!empty($attached_id))
19605 + {
19606 + $additional_attachments[] = $attached_id;
19607 + }
19608 + if(!empty($z_img))
19609 + {
19610 + $get_img = $z_img;
19611 + $go_local_image = false;
19612 + if(!function_exists('is_plugin_active'))
19613 + {
19614 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19615 + }
19616 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
19617 + {
19618 + $go_local_image = true;
19619 + }
19620 + if($go_local_image == true)
19621 + {
19622 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
19623 + if(!empty($attached_id))
19624 + {
19625 + $additional_attachments[] = $attached_id;
19626 + }
19627 + if($localpath !== false)
19628 + {
19629 + $get_img = $localpath[0];
19630 + }
19631 + }
19632 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19633 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
19634 + }
19635 + break;
19636 + }
19637 + }
19638 + }
19639 + }
19640 + }
19641 + catch(Exception $e)
19642 + {
19643 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19644 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
19645 + }
19646 + }
19647 + }
19648 + }
19649 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
19650 + {
19651 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
19652 + {
19653 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
19654 + {
19655 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
19656 + }
19657 + else
19658 + {
19659 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
19660 + }
19661 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
19662 + {
19663 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
19664 + }
19665 + else
19666 + {
19667 + $keyword_assistant_id = '';
19668 + }
19669 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
19670 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
19671 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
19672 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
19673 + {
19674 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
19675 + $title_ai_command = array_filter($title_ai_command);
19676 + if(count($title_ai_command) > 0)
19677 + {
19678 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
19679 + }
19680 + else
19681 + {
19682 + $title_ai_command = '';
19683 + }
19684 + }
19685 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
19686 + if(!empty($title_ai_command))
19687 + {
19688 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19689 + }
19690 + $title_ai_command = trim($title_ai_command);
19691 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
19692 + {
19693 + $txt_content = aiomatic_get_web_page($title_ai_command);
19694 + if ($txt_content !== FALSE)
19695 + {
19696 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
19697 + $txt_content = array_filter($txt_content);
19698 + if(count($txt_content) > 0)
19699 + {
19700 + $txt_content = $txt_content[array_rand($txt_content)];
19701 + if(trim($txt_content) != '')
19702 + {
19703 + $title_ai_command = $txt_content;
19704 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
19705 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
19706 + }
19707 + }
19708 + }
19709 + }
19710 + if(empty($title_ai_command))
19711 + {
19712 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
19713 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
19714 + }
19715 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
19716 + {
19717 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
19718 + }
19719 + $title_ai_command = trim($title_ai_command);
19720 + if(empty($title_ai_command))
19721 + {
19722 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
19723 + }
19724 + else
19725 + {
19726 + $query_token_count = count(aiomatic_encode($title_ai_command));
19727 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
19728 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
19729 + {
19730 + $string_len = aiomatic_strlen($title_ai_command);
19731 + $string_len = $string_len / 2;
19732 + $string_len = intval(0 - $string_len);
19733 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
19734 + $title_ai_command = trim($title_ai_command);
19735 + $query_token_count = count(aiomatic_encode($title_ai_command));
19736 + $available_tokens = $max_tokens - $query_token_count;
19737 + }
19738 + $aierror = '';
19739 + $finish_reason = '';
19740 + if(!empty($global_prepend))
19741 + {
19742 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
19743 + }
19744 + if(!empty($global_append))
19745 + {
19746 + $title_ai_command = $title_ai_command . ' ' . $global_append;
19747 + }
19748 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19749 + {
19750 + $api_service = aiomatic_get_api_service($token, $kw_model);
19751 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
19752 + }
19753 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free keywords'));
19754 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
19755 + if($generated_text === false)
19756 + {
19757 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
19758 + $ai_title = '';
19759 + }
19760 + else
19761 + {
19762 + $ai_title = trim(trim($generated_text), '.');
19763 + $ai_titles = explode(',', $ai_title);
19764 + foreach($ai_titles as $query_words)
19765 + {
19766 + $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);
19767 + if(!empty($attached_id))
19768 + {
19769 + $additional_attachments[] = $attached_id;
19770 + }
19771 + if(!empty($z_img))
19772 + {
19773 + $get_img = $z_img;
19774 + $go_local_image = false;
19775 + if(!function_exists('is_plugin_active'))
19776 + {
19777 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19778 + }
19779 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
19780 + {
19781 + $go_local_image = true;
19782 + }
19783 + if($go_local_image == true)
19784 + {
19785 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
19786 + if(!empty($attached_id))
19787 + {
19788 + $additional_attachments[] = $attached_id;
19789 + }
19790 + if($localpath !== false)
19791 + {
19792 + $get_img = $localpath[0];
19793 + }
19794 + }
19795 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19796 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
19797 + }
19798 + break;
19799 + }
19800 + }
19801 + }
19802 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
19803 + {
19804 + $api_service = aiomatic_get_api_service($token, $kw_model);
19805 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
19806 + }
19807 + }
19808 + }
19809 + }
19810 + if(empty($get_img))
19811 + {
19812 + if($image_query_set == true && !empty($image_query))
19813 + {
19814 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free image'));
19815 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
19816 + if(!empty($attached_id))
19817 + {
19818 + $additional_attachments[] = $attached_id;
19819 + }
19820 + if($get_img == '' || $get_img === false)
19821 + {
19822 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
19823 + {
19824 + $keyword_class = new Aiomatic_keywords();
19825 + $image_query = $keyword_class->keywords($image_query, 1);
19826 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
19827 + if(!empty($attached_id))
19828 + {
19829 + $additional_attachments[] = $attached_id;
19830 + }
19831 + }
19832 + }
19833 + }
19834 + if(empty($get_img))
19835 + {
19836 + $keyword_class = new Aiomatic_keywords();
19837 + $query_words = $keyword_class->keywords($post_title, 2);
19838 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
19839 + if(!empty($attached_id))
19840 + {
19841 + $additional_attachments[] = $attached_id;
19842 + }
19843 + if($get_img == '' || $get_img === false)
19844 + {
19845 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
19846 + {
19847 + $query_words = $keyword_class->keywords($post_title, 1);
19848 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
19849 + if(!empty($attached_id))
19850 + {
19851 + $additional_attachments[] = $attached_id;
19852 + }
19853 + if($get_img == '' || $get_img === false)
19854 + {
19855 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
19856 + {
19857 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19858 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
19859 + }
19860 + unset($post_title_lines[$current_index]);
19861 + continue;
19862 + }
19863 + }
19864 + }
19865 + else
19866 + {
19867 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
19868 + {
19869 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19870 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
19871 + }
19872 + unset($post_title_lines[$current_index]);
19873 + continue;
19874 + }
19875 + }
19876 + }
19877 + }
19878 + if(!empty($get_img))
19879 + {
19880 + $go_local_image = false;
19881 + if(!function_exists('is_plugin_active'))
19882 + {
19883 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
19884 + }
19885 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
19886 + {
19887 + $go_local_image = true;
19888 + }
19889 + if($go_local_image == true)
19890 + {
19891 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
19892 + if(!empty($attached_id))
19893 + {
19894 + $additional_attachments[] = $attached_id;
19895 + }
19896 + if($localpath !== false)
19897 + {
19898 + $get_img = $localpath[0];
19899 + }
19900 + }
19901 + }
19902 + }
19903 + }
19904 + }
19905 + elseif($royalty_free == '2')
19906 + {
19907 + $get_img = $amazon_thumb;
19908 + }
19909 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
19910 + {
19911 + $already_spinned = '1';
19912 + }
19913 + $my_post = array();
19914 + $my_post['aiomatic_post_image'] = $get_img;
19915 + if($enable_ai_images == '2')
19916 + {
19917 + $my_post['aiomatic_local_image'] = '1';
19918 + }
19919 + else
19920 + {
19921 + $my_post['aiomatic_local_image'] = '0';
19922 + }
19923 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
19924 + $my_post['default_category'] = $default_category;
19925 + $my_post['post_type'] = $post_type;
19926 + $my_post['comment_status'] = $accept_comments;
19927 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
19928 + {
19929 + if($post_status == 'publish')
19930 + {
19931 + $draft_me = true;
19932 + $my_post['post_status'] = 'draft';
19933 + }
19934 + else
19935 + {
19936 + $my_post['post_status'] = $post_status;
19937 + }
19938 + }
19939 + else
19940 + {
19941 + $my_post['post_status'] = $post_status;
19942 + }
19943 + if($user_name_type == 'rand')
19944 + {
19945 + $randid = aiomatic_display_random_user();
19946 + if($randid === false)
19947 + {
19948 + $my_post['post_author'] = aiomatic_randomName();
19949 + }
19950 + else
19951 + {
19952 + $my_post['post_author'] = $randid->ID;
19953 + }
19954 + }
19955 + else
19956 + {
19957 + $my_post['post_author'] = $post_user_name;
19958 + }
19959 + if($strip_title == '1')
19960 + {
19961 + $new_post_content = str_replace($post_title, '', $new_post_content);
19962 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
19963 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
19964 + }
19965 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
19966 + {
19967 + require_once(dirname(__FILE__) . "/res/swear.php");
19968 + $new_post_content = aimogenpro_filterwords($new_post_content);
19969 + }
19970 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
19971 + $continue = false;
19972 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
19973 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
19974 + foreach ($banned_list as $banned_word) {
19975 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
19976 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19977 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
19978 + }
19979 + $continue = true;
19980 + break;
19981 + }
19982 + if (stripos($post_title, trim($banned_word)) !== FALSE) {
19983 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
19984 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
19985 + }
19986 + $continue = true;
19987 + break;
19988 + }
19989 + }
19990 + if ($continue === true) {
19991 + continue;
19992 + }
19993 + }
19994 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
19995 + {
19996 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
19997 + {
19998 + $continue = true;
19999 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
20000 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
20001 + foreach ($required_list as $required_word) {
20002 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($post_title, trim($required_word)) !== FALSE) {
20003 + $continue = false;
20004 + break;
20005 + }
20006 + }
20007 + if ($continue === true) {
20008 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20009 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
20010 + }
20011 + continue;
20012 + }
20013 + }
20014 + else
20015 + {
20016 + $continue = false;
20017 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
20018 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
20019 + foreach ($required_list as $required_word) {
20020 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($post_title, trim($required_word)) === FALSE) {
20021 + $continue = true;
20022 + break;
20023 + }
20024 + }
20025 + if ($continue === true) {
20026 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20027 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
20028 + }
20029 + continue;
20030 + }
20031 + }
20032 + }
20033 + $new_post_title = $post_title;
20034 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
20035 + {
20036 + $skip_spin = '1';
20037 + }
20038 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
20039 + if($arr[0] != $new_post_title)
20040 + {
20041 + $new_post_title = $arr[0];
20042 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
20043 + {
20044 + $posts = get_posts(
20045 + array(
20046 + 'post_type' => $post_type,
20047 + 'title' => html_entity_decode($new_post_title),
20048 + 'post_status' => 'all',
20049 + 'numberposts' => 1,
20050 + 'update_post_term_cache' => false,
20051 + 'update_post_meta_cache' => false,
20052 + 'orderby' => 'post_date ID',
20053 + 'order' => 'ASC',
20054 + )
20055 + );
20056 + if ( ! empty( $posts ) ) {
20057 + $zap = $posts[0];
20058 + } else {
20059 + $zap = null;
20060 + }
20061 + if($zap !== null)
20062 + {
20063 + if($overwrite_existing)
20064 + {
20065 + $update_post_id = $zap->ID;
20066 + }
20067 + else
20068 + {
20069 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $new_post_title);
20070 + unset($post_title_lines[$current_index]);
20071 + continue;
20072 + }
20073 + }
20074 + }
20075 + }
20076 + $new_post_content = $arr[1];
20077 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
20078 + {
20079 + $already_spinned = '1';
20080 + }
20081 + if ($auto_categories == 'content') {
20082 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
20083 + $extra_categories = implode(',', $extra_categories);
20084 + }
20085 + elseif ($auto_categories == 'title') {
20086 + $extra_categories = aiomatic_extractKeyWords($new_post_title);
20087 + $extra_categories = implode(',', $extra_categories);
20088 + }
20089 + elseif ($auto_categories == 'both') {
20090 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
20091 + $extra_categories = implode(',', $extra_categories);
20092 + $extra_categories2 = aiomatic_extractKeyWords($new_post_title);
20093 + $extra_categories2 = implode(',', $extra_categories2);
20094 + if($extra_categories2 != '')
20095 + {
20096 + $extra_categories .= ',' . $extra_categories2;
20097 + }
20098 + }
20099 + elseif ($auto_categories == 'ai')
20100 + {
20101 + $category_ai_command = $orig_ai_command_category;
20102 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
20103 + {
20104 + $category_ai_command = preg_split('/\r\n|\r|\n/', $category_ai_command);
20105 + $category_ai_command = array_filter($category_ai_command);
20106 + if(count($category_ai_command) > 0)
20107 + {
20108 + $category_ai_command = $category_ai_command[array_rand($category_ai_command)];
20109 + }
20110 + else
20111 + {
20112 + $category_ai_command = '';
20113 + }
20114 + }
20115 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
20116 + if(!empty($category_ai_command))
20117 + {
20118 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20119 + $category_ai_command = aiomatic_replace_amazon($category_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20120 + }
20121 + else
20122 + {
20123 + $category_ai_command = trim(strip_tags('Write a comma separated list of categories, for the post title: %%post_title%%'));
20124 + }
20125 + $category_ai_command = trim($category_ai_command);
20126 + if (filter_var($category_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($category_ai_command, '.txt'))
20127 + {
20128 + $txt_content = aiomatic_get_web_page($category_ai_command);
20129 + if ($txt_content !== FALSE)
20130 + {
20131 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
20132 + $txt_content = array_filter($txt_content);
20133 + if(count($txt_content) > 0)
20134 + {
20135 + $txt_content = $txt_content[array_rand($txt_content)];
20136 + if(trim($txt_content) != '')
20137 + {
20138 + $category_ai_command = $txt_content;
20139 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
20140 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20141 + $category_ai_command = aiomatic_replace_amazon($category_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20142 + }
20143 + }
20144 + }
20145 + }
20146 + if(empty($category_ai_command))
20147 + {
20148 + aiomatic_log_to_file('Empty API post category seed expression provided!');
20149 + }
20150 + else
20151 + {
20152 + if(aiomatic_strlen($category_ai_command) > $max_seed_tokens * 4)
20153 + {
20154 + $category_ai_command = aiomatic_substr($category_ai_command, 0, (0 - ($max_seed_tokens * 4)));
20155 + }
20156 + $category_ai_command = trim($category_ai_command);
20157 + if(empty($category_ai_command))
20158 + {
20159 + aiomatic_log_to_file('Empty API category seed expression provided! ' . print_r($category_ai_command, true));
20160 + break;
20161 + }
20162 + $query_token_count = count(aiomatic_encode($category_ai_command));
20163 + $available_tokens = aiomatic_compute_available_tokens($category_model, $max_tokens, $category_ai_command, $query_token_count);
20164 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
20165 + {
20166 + $string_len = aiomatic_strlen($category_ai_command);
20167 + $string_len = $string_len / 2;
20168 + $string_len = intval(0 - $string_len);
20169 + $category_ai_command = aiomatic_substr($category_ai_command, 0, $string_len);
20170 + $category_ai_command = trim($category_ai_command);
20171 + if(empty($category_ai_command))
20172 + {
20173 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($category_ai_command, true));
20174 + break;
20175 + }
20176 + $query_token_count = count(aiomatic_encode($category_ai_command));
20177 + $available_tokens = $max_tokens - $query_token_count;
20178 + }
20179 + $aierror = '';
20180 + $finish_reason = '';
20181 + if(!empty($global_prepend))
20182 + {
20183 + $category_ai_command = $global_prepend . ' ' . $category_ai_command;
20184 + }
20185 + if(!empty($global_append))
20186 + {
20187 + $category_ai_command = $category_ai_command . ' ' . $global_append;
20188 + }
20189 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
20190 + {
20191 + $api_service = aiomatic_get_api_service($token, $category_model);
20192 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $category_model . ')(' . $available_tokens . ') for category generator: ' . $category_ai_command);
20193 + }
20194 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating categories'));
20195 + $generated_text = aiomatic_generate_text($token, $category_model, $category_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
20196 + if($generated_text === false)
20197 + {
20198 + aiomatic_log_to_file('Category generator error: ' . $aierror);
20199 + break;
20200 + }
20201 + else
20202 + {
20203 + $extra_categories = $generated_text;
20204 + }
20205 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
20206 + {
20207 + $api_service = aiomatic_get_api_service($token, $category_model);
20208 + aiomatic_log_to_file('Successfully got API category result from ' . $api_service . ': ' . $generated_text);
20209 + }
20210 + }
20211 + }
20212 + else
20213 + {
20214 + $extra_categories = '';
20215 + }
20216 + $my_post['extra_categories'] = $extra_categories;
20217 +
20218 + $item_tags = aiomatic_extractKeyWords($new_post_content, 3);
20219 + $item_tags = implode(',', $item_tags);
20220 + $title_tags = aiomatic_extractKeyWords($new_post_title, 3);
20221 + $title_tags = implode(',', $title_tags);
20222 + $item_create_tag_sp = $spintax->Parse($item_create_tag);
20223 + if ($can_create_tag == 'content') {
20224 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $item_tags;
20225 + $my_post['extra_tags'] = $item_tags;
20226 + } else if ($can_create_tag == 'title') {
20227 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $title_tags;
20228 + $my_post['extra_tags'] = $title_tags;
20229 + } else if ($can_create_tag == 'both') {
20230 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
20231 + $my_post['extra_tags'] = ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
20232 + } else if ($can_create_tag == 'ai') {
20233 + $ai_tags = '';
20234 + $tag_ai_command = $orig_ai_command_tag;
20235 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
20236 + {
20237 + $tag_ai_command = preg_split('/\r\n|\r|\n/', $tag_ai_command);
20238 + $tag_ai_command = array_filter($tag_ai_command);
20239 + if(count($tag_ai_command) > 0)
20240 + {
20241 + $tag_ai_command = $tag_ai_command[array_rand($tag_ai_command)];
20242 + }
20243 + else
20244 + {
20245 + $tag_ai_command = '';
20246 + }
20247 + }
20248 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
20249 + if(!empty($tag_ai_command))
20250 + {
20251 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20252 + $tag_ai_command = aiomatic_replace_amazon($tag_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20253 + }
20254 + else
20255 + {
20256 + $tag_ai_command = trim(strip_tags('Write a comma separated list of tags, for the post title: %%post_title%%'));
20257 + }
20258 + $tag_ai_command = trim($tag_ai_command);
20259 + if (filter_var($tag_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($tag_ai_command, '.txt'))
20260 + {
20261 + $txt_content = aiomatic_get_web_page($tag_ai_command);
20262 + if ($txt_content !== FALSE)
20263 + {
20264 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
20265 + $txt_content = array_filter($txt_content);
20266 + if(count($txt_content) > 0)
20267 + {
20268 + $txt_content = $txt_content[array_rand($txt_content)];
20269 + if(trim($txt_content) != '')
20270 + {
20271 + $tag_ai_command = $txt_content;
20272 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
20273 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20274 + $tag_ai_command = aiomatic_replace_amazon($tag_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20275 + }
20276 + }
20277 + }
20278 + }
20279 + if(empty($tag_ai_command))
20280 + {
20281 + aiomatic_log_to_file('Empty API post tag seed expression provided!');
20282 + }
20283 + else
20284 + {
20285 + if(aiomatic_strlen($tag_ai_command) > $max_seed_tokens * 4)
20286 + {
20287 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, (0 - ($max_seed_tokens * 4)));
20288 + }
20289 + $tag_ai_command = trim($tag_ai_command);
20290 + if(empty($tag_ai_command))
20291 + {
20292 + aiomatic_log_to_file('Empty API tag seed expression provided! ' . print_r($tag_ai_command, true));
20293 + break;
20294 + }
20295 + $query_token_count = count(aiomatic_encode($tag_ai_command));
20296 + $available_tokens = aiomatic_compute_available_tokens($tag_model, $max_tokens, $tag_ai_command, $query_token_count);
20297 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
20298 + {
20299 + $string_len = aiomatic_strlen($tag_ai_command);
20300 + $string_len = $string_len / 2;
20301 + $string_len = intval(0 - $string_len);
20302 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, $string_len);
20303 + $tag_ai_command = trim($tag_ai_command);
20304 + if(empty($tag_ai_command))
20305 + {
20306 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($tag_ai_command, true));
20307 + break;
20308 + }
20309 + $query_token_count = count(aiomatic_encode($tag_ai_command));
20310 + $available_tokens = $max_tokens - $query_token_count;
20311 + }
20312 + $aierror = '';
20313 + $finish_reason = '';
20314 + if(!empty($global_prepend))
20315 + {
20316 + $tag_ai_command = $global_prepend . ' ' . $tag_ai_command;
20317 + }
20318 + if(!empty($global_append))
20319 + {
20320 + $tag_ai_command = $tag_ai_command . ' ' . $global_append;
20321 + }
20322 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
20323 + {
20324 + $api_service = aiomatic_get_api_service($token, $tag_model);
20325 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $tag_model . ')(' . $available_tokens . ') for tag generator: ' . $tag_ai_command);
20326 + }
20327 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
20328 + $generated_text = aiomatic_generate_text($token, $tag_model, $tag_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
20329 + if($generated_text === false)
20330 + {
20331 + aiomatic_log_to_file('Tag generator error: ' . $aierror);
20332 + break;
20333 + }
20334 + else
20335 + {
20336 + $ai_tags = $generated_text;
20337 + }
20338 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
20339 + {
20340 + $api_service = aiomatic_get_api_service($token, $tag_model);
20341 + aiomatic_log_to_file('Successfully got API tag result from ' . $api_service . ': ' . $generated_text);
20342 + }
20343 + }
20344 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $ai_tags;
20345 + $my_post['extra_tags'] = $ai_tags;
20346 + } else {
20347 + $post_the_tags = $item_create_tag_sp;
20348 + $my_post['extra_tags'] = '';
20349 + }
20350 + $my_post['tags_input'] = $post_the_tags;
20351 + $new_post_content = html_entity_decode($new_post_content);
20352 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
20353 + if ($strip_by_regex !== '')
20354 + {
20355 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
20356 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
20357 + $xcnt = 0;
20358 + foreach($xstrip_by_regex as $sbr)
20359 + {
20360 + if(isset($xreplace_regex[$xcnt]))
20361 + {
20362 + $repreg = $xreplace_regex[$xcnt];
20363 + }
20364 + else
20365 + {
20366 + $repreg = '';
20367 + }
20368 + $xcnt++;
20369 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
20370 + if($temp_cont !== NULL)
20371 + {
20372 + $new_post_content = $temp_cont;
20373 + }
20374 + }
20375 + }
20376 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20377 + $post_prepender = aiomatic_replace_amazon($post_prepender, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20378 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20379 + $post_appender = aiomatic_replace_amazon($post_appender, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20380 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
20381 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
20382 + if($topic_videos == '1' && count($added_vid_list) == 0)
20383 + {
20384 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
20385 + {
20386 + $social_list = $aiomatic_Main_Settings['social_list'];
20387 + }
20388 + else
20389 + {
20390 + $social_list = array();
20391 + }
20392 + $new_vid = aiomatic_get_video($amazon_kw, '', $social_list);
20393 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
20394 + {
20395 + $new_post_content .= $new_vid;
20396 + $added_vid_list[] = $new_vid;
20397 + $new_vid_arr[] = $new_vid;
20398 + $added_videos++;
20399 + }
20400 + }
20401 + $zlang = 'en_US';
20402 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
20403 + $zlang = $aiomatic_Main_Settings['kw_lang'];
20404 + }
20405 + $rel_search = array('post_title', 'post_content');
20406 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
20407 + $rel_search = $aiomatic_Main_Settings['rel_search'];
20408 + }
20409 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
20410 + {
20411 + try
20412 + {
20413 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
20414 + }
20415 + catch(Exception $ex)
20416 + {
20417 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20418 + aiomatic_log_to_file('Failed to add new inbound links to content: ' . $ex->getMessage());
20419 + }
20420 + }
20421 + }
20422 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
20423 + {
20424 + if($new_post_content === strip_tags($new_post_content))
20425 + {
20426 + $new_post_content = nl2br($new_post_content);
20427 + }
20428 + }
20429 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
20430 + {
20431 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
20432 + {
20433 + if(!isset($xchars))
20434 + {
20435 + $xchars = array();
20436 + }
20437 + $rand_percentage = wp_rand(10, 20);
20438 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
20439 + }
20440 + }
20441 + if($ret_content == 1)
20442 + {
20443 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20444 + aiomatic_log_to_file('Returning AI generated content, finished.');
20445 + }
20446 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
20447 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
20448 + }
20449 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
20450 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
20451 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
20452 + {
20453 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
20454 + }
20455 + else
20456 + {
20457 + $my_post['post_content'] = $trunchi_content;
20458 + }
20459 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
20460 + {
20461 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
20462 + {
20463 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
20464 + }
20465 + }
20466 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
20467 + $my_post['aiomatic_source_title'] = $amazon_kw;
20468 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
20469 + $my_post['aiomatic_post_format'] = $post_format;
20470 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
20471 + {
20472 + if(is_array($default_category))
20473 + {
20474 + $cextra = '';
20475 + foreach($default_category as $dc)
20476 + {
20477 + $cextra .= ',' . get_cat_name($dc);
20478 + }
20479 + $extra_categories_temp = trim( $cextra . ',' . $extra_categories, ',');
20480 + }
20481 + else
20482 + {
20483 + $extra_categories_temp = trim(get_cat_name($default_category) . ',' .$extra_categories, ',');
20484 + }
20485 + }
20486 + else
20487 + {
20488 + $extra_categories_temp = $extra_categories;
20489 + }
20490 + $block_arr = array();
20491 + $custom_arr = array();
20492 + if($custom_fields != '')
20493 + {
20494 + if(stristr($custom_fields, '=>') != false)
20495 + {
20496 + $rule_arr = explode(',', trim($custom_fields));
20497 + foreach($rule_arr as $rule)
20498 + {
20499 + $my_args = explode('=>', trim($rule));
20500 + if(isset($my_args[1]))
20501 + {
20502 + if ( isset( $shortcode_tags['aicontent'] ) ) {
20503 + $aicontent_handler = $shortcode_tags['aicontent'];
20504 + unset( $shortcode_tags['aicontent'] );
20505 + } else {
20506 + $aicontent_handler = null;
20507 + }
20508 + $my_args[1] = do_shortcode($my_args[1]);
20509 + $my_args[0] = do_shortcode($my_args[0]);
20510 + if ( $aicontent_handler ) {
20511 + $shortcode_tags['aicontent'] = $aicontent_handler;
20512 + }
20513 + $custom_field_content = trim($my_args[1]);
20514 + $custom_field_content = aiomatic_replaceAIPostShortcodes($custom_field_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20515 + $custom_field_content = aiomatic_replace_amazon($custom_field_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20516 + $custom_field_content = aiomatic_replaceSynergyShortcodes($custom_field_content);
20517 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
20518 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
20519 + if(stristr($my_args[1], 'serialize_') !== false)
20520 + {
20521 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
20522 + }
20523 + else
20524 + {
20525 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
20526 + {
20527 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
20528 + if(isset($cfm[2][0]))
20529 + {
20530 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
20531 + {
20532 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
20533 + }
20534 + else
20535 + {
20536 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
20537 + }
20538 + }
20539 + else
20540 + {
20541 + $custom_arr[trim($my_args[0])] = $custom_field_content;
20542 + }
20543 + }
20544 + else
20545 + {
20546 + $custom_arr[trim($my_args[0])] = $custom_field_content;
20547 + }
20548 + }
20549 + }
20550 + }
20551 + }
20552 + }
20553 + $block_arr = array();
20554 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $extra_categories_temp, 'aiomatic_post_tags' => $post_the_tags));
20555 + $custom_tax_arr = array();
20556 + if($custom_tax != '')
20557 + {
20558 + if(stristr($custom_tax, '=>') != false)
20559 + {
20560 + $rule_arr = explode(';', trim($custom_tax));
20561 + foreach($rule_arr as $rule)
20562 + {
20563 + $my_args = explode('=>', trim($rule));
20564 + if(isset($my_args[1]))
20565 + {
20566 + $custom_tax_content = trim($my_args[1]);
20567 + $custom_tax_content = aiomatic_replaceAIPostShortcodes($custom_tax_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
20568 + $custom_tax_content = aiomatic_replace_amazon($custom_tax_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, implode(',' , $post_sections), $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count, $amazresult);
20569 + $custom_tax_content = aiomatic_replaceSynergyShortcodes($custom_tax_content);
20570 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
20571 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
20572 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
20573 + {
20574 + if(isset($custom_arr['_product_attributes']))
20575 + {
20576 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
20577 + 'name' => trim($my_args[0]),
20578 + 'value' => $custom_tax_content,
20579 + 'is_visible' => '1',
20580 + 'is_taxonomy' => '1'
20581 + )));
20582 + }
20583 + else
20584 + {
20585 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
20586 + 'name' => trim($my_args[0]),
20587 + 'value' => $custom_tax_content,
20588 + 'is_visible' => '1',
20589 + 'is_taxonomy' => '1'
20590 + ));
20591 + }
20592 + }
20593 + if(isset($custom_tax_arr[trim($my_args[0])]))
20594 + {
20595 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
20596 + }
20597 + else
20598 + {
20599 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
20600 + }
20601 + }
20602 + }
20603 + }
20604 + }
20605 + if(count($custom_tax_arr) > 0)
20606 + {
20607 + $my_post['taxo_input'] = $custom_tax_arr;
20608 + }
20609 + $my_post['meta_input'] = $custom_arr;
20610 + if($parent_id != '')
20611 + {
20612 + $my_post['post_parent'] = intval($parent_id);
20613 + }
20614 + if ($enable_pingback == '1') {
20615 + $my_post['ping_status'] = 'open';
20616 + } else {
20617 + $my_post['ping_status'] = 'closed';
20618 + }
20619 + if($min_time != '' && $max_time != '')
20620 + {
20621 + $t1 = strtotime($min_time);
20622 + $t2 = strtotime($max_time);
20623 + if($t1 != false && $t2 != false)
20624 + {
20625 + $int = wp_rand($t1, $t2);
20626 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
20627 + }
20628 + }
20629 + elseif($min_time != '')
20630 + {
20631 + $t1 = strtotime($min_time);
20632 + if($t1 != false)
20633 + {
20634 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
20635 + }
20636 + }
20637 + elseif($max_time != '')
20638 + {
20639 + $t1 = strtotime($max_time);
20640 + if($t1 != false)
20641 + {
20642 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
20643 + }
20644 + }
20645 + if($new_post_excerpt != '')
20646 + {
20647 + $my_post['post_excerpt'] = $new_post_excerpt;
20648 + }
20649 + $count++;
20650 + if($update_post_id != '')
20651 + {
20652 + $my_post['ID'] = $update_post_id;
20653 + }
20654 + remove_filter('content_save_pre', 'wp_filter_post_kses');
20655 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
20656 + try
20657 + {
20658 + $post_id = wp_insert_post($my_post, true);
20659 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
20660 + if (is_wp_error($post_id))
20661 + {
20662 + aiomatic_log_to_file('Trying publishing again');
20663 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
20664 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
20665 + }
20666 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
20667 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
20668 + $post_id = wp_insert_post($my_post, true);
20669 + }
20670 + }
20671 + catch(Exception $e)
20672 + {
20673 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
20674 + }
20675 + add_filter('content_save_pre', 'wp_filter_post_kses');
20676 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
20677 + if (!is_wp_error($post_id)) {
20678 + if($post_id === 0)
20679 + {
20680 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
20681 + continue;
20682 + }
20683 + $posts_inserted++;
20684 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
20685 + {
20686 + pll_set_post_language($post_id, $wpml_lang);
20687 + }
20688 + if(!empty($additional_attachments))
20689 + {
20690 + foreach($additional_attachments as $add_att)
20691 + {
20692 + wp_update_post([
20693 + 'ID' => $add_att,
20694 + 'post_parent' => $post_id,
20695 + ]);
20696 + }
20697 + }
20698 + $default_categories = array();
20699 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
20700 + {
20701 + $default_categories = wp_get_post_categories($post_id);
20702 + }
20703 + if(isset($my_post['taxo_input']))
20704 + {
20705 + foreach($my_post['taxo_input'] as $taxn => $taxval)
20706 + {
20707 + $taxn = trim($taxn);
20708 + $taxval = trim($taxval);
20709 + if(is_taxonomy_hierarchical($taxn))
20710 + {
20711 + $taxval = array_map('trim', explode(',', $taxval));
20712 + for($ii = 0; $ii < count($taxval); $ii++)
20713 + {
20714 + if(!is_numeric($taxval[$ii]))
20715 + {
20716 + $term_ids = [];
20717 + $parent_id = 0;
20718 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
20719 + foreach($hierarchy_parts as $hp)
20720 + {
20721 + if(!is_numeric($hp))
20722 + {
20723 + $xtermid = get_term_by('name', $hp, $taxn);
20724 + if($xtermid !== false)
20725 + {
20726 + $parent_id = $xtermid->term_id;
20727 + if(!is_numeric($taxval[$ii]))
20728 + {
20729 + $taxval[$ii] = intval($xtermid->term_id);
20730 + }
20731 + else
20732 + {
20733 + $taxval[] = intval($xtermid->term_id);
20734 + }
20735 + }
20736 + else
20737 + {
20738 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
20739 + $xtermid = get_term_by('name', $hp, $taxn);
20740 + if($xtermid !== false)
20741 + {
20742 + $parent_id = $xtermid->term_id;
20743 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
20744 + {
20745 + pll_set_term_language($xtermid->term_id, $wpml_lang);
20746 + }
20747 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
20748 + {
20749 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
20750 + $pars['element_id'] = $xtermid->term_id;
20751 + $pars['element_type'] = $wpml_element_type;
20752 + $pars['language_code'] = $wpml_lang;
20753 + $pars['trid'] = FALSE;
20754 + $pars['source_language_code'] = NULL;
20755 + do_action('wpml_set_element_language_details', $pars);
20756 + }
20757 + if(!is_numeric($taxval[$ii]))
20758 + {
20759 + $taxval[$ii] = intval($xtermid->term_id);
20760 + }
20761 + else
20762 + {
20763 + $taxval[] = intval($xtermid->term_id);
20764 + }
20765 + }
20766 + }
20767 + }
20768 + else
20769 + {
20770 + if(!is_numeric($taxval[$ii]))
20771 + {
20772 + $taxval[$ii] = intval($hp);
20773 + }
20774 + else
20775 + {
20776 + $taxval[] = intval($hp);
20777 + }
20778 + }
20779 + }
20780 + }
20781 + }
20782 + wp_set_post_terms($post_id, $taxval, $taxn, true);
20783 + }
20784 + else
20785 + {
20786 + $jterms = array_map('trim', explode(',', $taxval));
20787 + wp_set_post_terms($post_id, $jterms, $taxn, true);
20788 + }
20789 + }
20790 + }
20791 + if (isset($my_post['aiomatic_post_format']) && $my_post['aiomatic_post_format'] != '' && $my_post['aiomatic_post_format'] != 'post-format-standard') {
20792 + wp_set_post_terms($post_id, $my_post['aiomatic_post_format'], 'post_format', true);
20793 + }
20794 + $featured_path = '';
20795 + $get_img = $my_post['aiomatic_post_image'];
20796 + if ($get_img != '') {
20797 + if($my_post['aiomatic_local_image'] == '1')
20798 + {
20799 + $local_get_img = $get_img[0];
20800 + if (!aiomatic_assign_featured_image_path($local_get_img, $post_id)) {
20801 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20802 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed for ' . $local_get_img);
20803 + }
20804 + } else {
20805 + $featured_path = $get_img[1];
20806 + }
20807 + }
20808 + else
20809 + {
20810 + if(is_numeric($get_img))
20811 + {
20812 + $featured_path = aiomatic_assign_featured_image($get_img, $post_id);
20813 + }
20814 + else
20815 + {
20816 + if (!aiomatic_generate_featured_image($get_img, $post_id)) {
20817 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20818 + aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $get_img);
20819 + }
20820 + } else {
20821 + $featured_path = $get_img;
20822 + }
20823 + }
20824 + }
20825 + }
20826 + if($featured_path == '')
20827 + {
20828 + if ($image_url != '') {
20829 + $replacement = str_replace(array('[', ']'), '', $my_post['post_title']);
20830 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
20831 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
20832 + if(isset($matches[2]))
20833 + {
20834 + $chance = trim($matches[2], '[]');
20835 + }
20836 + else
20837 + {
20838 + $chance = '';
20839 + }
20840 + $arv = array();
20841 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
20842 + return $my_img;
20843 + }, $image_url_temp);
20844 + $img_rulx = $spintax->Parse(trim($image_url_temp));
20845 + $selected_img = aiomatic_select_ai_image($my_post['post_title'], $img_rulx);
20846 + if($selected_img === false)
20847 + {
20848 + $img_rulx = explode(',', $img_rulx);
20849 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
20850 + }
20851 + else
20852 + {
20853 + $img_rulx = $selected_img;
20854 + }
20855 + if(is_numeric($img_rulx))
20856 + {
20857 + $featured_path = aiomatic_assign_featured_image($img_rulx, $post_id);
20858 + }
20859 + else
20860 + {
20861 + if($img_rulx != '')
20862 + {
20863 + if (!aiomatic_generate_featured_image($img_rulx, $post_id)) {
20864 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20865 + aiomatic_log_to_file('aiomatic_generate_featured_image failed to default value: ' . $img_rulx . '!');
20866 + }
20867 + } else {
20868 + $featured_path = $img_rulx;
20869 + }
20870 + }
20871 + }
20872 + }
20873 + }
20874 + if ($auto_categories != 'disabled') {
20875 + if ($my_post['extra_categories'] != '') {
20876 + $extra_cats = explode(',', $my_post['extra_categories']);
20877 + if($post_type == 'product')
20878 + {
20879 + $product_tax = 'product_cat';
20880 + }
20881 + else
20882 + {
20883 + $product_tax = 'category';
20884 + }
20885 + foreach($extra_cats as $extra_cat)
20886 + {
20887 + $extra_cat = trim($extra_cat);
20888 + $extra_cat = strip_tags($extra_cat);
20889 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
20890 + if(empty($extra_cat))
20891 + {
20892 + continue;
20893 + }
20894 + if ($skip_inexist == '1')
20895 + {
20896 + if(!term_exists($extra_cat, $product_tax))
20897 + {
20898 + continue;
20899 + }
20900 + }
20901 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
20902 + wp_set_post_terms($post_id, $termid, $product_tax, true);
20903 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
20904 + {
20905 + foreach($termid as $tx)
20906 + {
20907 + pll_set_term_language($tx, $wpml_lang);
20908 + }
20909 + }
20910 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
20911 + {
20912 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
20913 + foreach($termid as $tx)
20914 + {
20915 + $pars['element_id'] = $tx;
20916 + $pars['element_type'] = $wpml_element_type;
20917 + $pars['language_code'] = $wpml_lang;
20918 + $pars['trid'] = FALSE;
20919 + $pars['source_language_code'] = NULL;
20920 + do_action('wpml_set_element_language_details', $pars);
20921 + }
20922 + }
20923 + }
20924 + }
20925 + }
20926 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678') {
20927 + $cats = array();
20928 + if(is_array($default_category))
20929 + {
20930 + foreach($default_category as $dc)
20931 + {
20932 + $cats[] = $dc;
20933 + }
20934 + }
20935 + else
20936 + {
20937 + $cats[] = $default_category;
20938 + }
20939 + global $sitepress;
20940 + if($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
20941 + {
20942 + $current_language = apply_filters( 'wpml_current_language', NULL );
20943 + $sitepress->switch_lang($wpml_lang);
20944 + }
20945 + $target_cat = 'category';
20946 + if($post_type == 'product')
20947 + {
20948 + $target_cat = 'product_cat';
20949 + }
20950 + wp_set_object_terms($post_id, array_map( 'intval', $cats ), $target_cat, true);
20951 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
20952 + {
20953 + foreach($cats as $cc)
20954 + {
20955 + pll_set_term_language($cc, $wpml_lang);
20956 + }
20957 + }
20958 + elseif($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
20959 + {
20960 + $sitepress->switch_lang($current_language);
20961 + }
20962 + }
20963 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
20964 + {
20965 + if($post_type == 'product')
20966 + {
20967 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
20968 + }
20969 + }
20970 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
20971 + if (is_wp_error($tax_rez)) {
20972 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
20973 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
20974 + }
20975 + }
20976 + if($post_type == 'topic' && $parent_id != '')
20977 + {
20978 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
20979 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
20980 + update_post_meta($post_id, '_bbp_voice_count', '0');
20981 + update_post_meta($post_id, '_bbp_reply_count', '0');
20982 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
20983 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
20984 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
20985 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
20986 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
20987 + }
20988 + if($post_type == 'reply' && $parent_id != '')
20989 + {
20990 + if(function_exists('bbp_get_topic_forum_id'))
20991 + {
20992 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
20993 + if(empty($forum_aidi))
20994 + {
20995 + $forum_aidi = 0;
20996 + }
20997 + }
20998 + else
20999 + {
21000 + $forum_aidi = 0;
21001 + }
21002 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
21003 + }
21004 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
21005 + {
21006 + $new_categories = wp_get_post_categories($post_id);
21007 + $removed_cat = false;
21008 + if(isset($default_categories) && !($default_categories == $new_categories))
21009 + {
21010 + foreach($default_categories as $dc)
21011 + {
21012 + $rem_cat = get_category( $dc );
21013 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
21014 + $removed_cat = true;
21015 + }
21016 + }
21017 + if($removed_cat == false)
21018 + {
21019 + wp_remove_object_terms( $post_id, 'uncategorized', 'category' );
21020 + }
21021 + }
21022 + aiomatic_addPostMeta($post_id, $my_post, $param, $type, $featured_path, $post_topic, $rule_unique_id, $post_link);
21023 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
21024 + {
21025 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
21026 + $pars['element_id'] = $post_id;
21027 + $pars['element_type'] = $wpml_element_type;
21028 + $pars['language_code'] = $wpml_lang;
21029 + $pars['source_language_code'] = NULL;
21030 + do_action('wpml_set_element_language_details', $pars);
21031 +
21032 + global $wp_filesystem;
21033 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
21034 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
21035 + wp_filesystem($creds);
21036 + }
21037 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
21038 + {
21039 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
21040 + }
21041 + $wpml_lang = trim($wpml_lang);
21042 + if(function_exists('wpml_update_translatable_content'))
21043 + {
21044 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
21045 + if($my_post['post_title'] != '')
21046 + {
21047 + global $sitepress;
21048 + global $wpdb;
21049 + $keyid = md5($my_post['post_title']);
21050 + $keyName = $keyid . '_wpml';
21051 + $sql = $wpdb->prepare(
21052 + "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = %s LIMIT 1",
21053 + $keyName
21054 + );
21055 + $rezxxxa = $wpdb->get_results($sql, ARRAY_A);
21056 + if(count($rezxxxa) != 0)
21057 + {
21058 + $metaRow = $rezxxxa[0];
21059 + $metaValue = $metaRow['meta_value'];
21060 + $metaParts = explode('_', $metaValue);
21061 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
21062 + }
21063 + else
21064 + {
21065 + $ptrid = $sitepress->get_element_trid($post_id);
21066 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
21067 + }
21068 + }
21069 +
21070 + }
21071 + }
21072 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
21073 + {
21074 + aiomatic_change_post_status($post_id, 'publish');
21075 + }
21076 + } else {
21077 + aiomatic_log_to_file('Failed to insert post into wp database(3)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
21078 + continue;
21079 + }
21080 + }
21081 + }
21082 + elseif($type == '3')
21083 + {
21084 + $amazon_keyword = str_replace(',', '|', $amazon_keyword);
21085 + $amazon_keyword_lines = array();
21086 + if($amazon_keyword != '')
21087 + {
21088 + $amazon_keyword_lines = preg_split('/\r\n|\r|\n/', $amazon_keyword);
21089 + }
21090 + else
21091 + {
21092 + aiomatic_log_to_file('You need to enter an Amazon product search ASIN to use this feature!');
21093 + if($auto == 1)
21094 + {
21095 + aiomatic_clearFromList($param, $type);
21096 + }
21097 + return 'fail';
21098 + }
21099 + $additional_kws = array();
21100 + $post_link = '';
21101 + $user_name = '';
21102 + $featured_image = '';
21103 + $post_cats = '';
21104 + $post_tagz = '';
21105 + $post_excerpt = '';
21106 + $final_content = '';
21107 + $postID = '';
21108 + $heading_val = '';
21109 + $image_query = '';
21110 + $temp_post = '';
21111 + $cntx = count($amazon_keyword_lines);
21112 + $rss_feeds = array();
21113 + for($ji = 0; $ji < $cntx; $ji++)
21114 + {
21115 + if (filter_var($amazon_keyword_lines[$ji], FILTER_VALIDATE_URL) !== false && stristr($amazon_keyword_lines[$ji], 'youtu') === false)
21116 + {
21117 + if(aiomatic_endsWith($amazon_keyword_lines[$ji], '.txt'))
21118 + {
21119 + $txt_content = aiomatic_get_web_page($amazon_keyword_lines[$ji]);
21120 + if ($txt_content === FALSE)
21121 + {
21122 + aiomatic_log_to_file('Failed to read text file: ' . $amazon_keyword_lines[$ji]);
21123 + unset($amazon_keyword_lines[$ji]);
21124 + if($auto == 1)
21125 + {
21126 + aiomatic_log_to_file($param);
21127 + }
21128 + continue;
21129 + }
21130 + unset($amazon_keyword_lines[$ji]);
21131 + $additional_kws = preg_split('/\r\n|\r|\n/', $txt_content);
21132 + }
21133 + else
21134 + {
21135 + aiomatic_log_to_file('Trying to parse RSS feed items(5): ' . $amazon_keyword_lines[$ji]);
21136 + try
21137 + {
21138 + if(!class_exists('SimplePie_Autoloader', false))
21139 + {
21140 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
21141 + }
21142 + }
21143 + catch(Exception $e)
21144 + {
21145 + unset($amazon_keyword_lines[$ji]);
21146 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
21147 + if($auto == 1)
21148 + {
21149 + aiomatic_log_to_file($param);
21150 + }
21151 + continue;
21152 + }
21153 + $feed = new SimplePie();
21154 + $feed->set_timeout(120);
21155 + $feed->set_feed_url($amazon_keyword_lines[$ji]);
21156 + $feed->enable_cache(false);
21157 + $feed->strip_htmltags(false);
21158 + $feed->init();
21159 + $feed->handle_content_type();
21160 + if ($feed->error())
21161 + {
21162 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $amazon_keyword_lines[$ji]);
21163 + unset($amazon_keyword_lines[$ji]);
21164 + if($auto == 1)
21165 + {
21166 + aiomatic_clearFromList($param, $type);
21167 + }
21168 + continue;
21169 + }
21170 + $items = $feed->get_items();
21171 + $zero = true;
21172 + foreach($items as $itemx)
21173 + {
21174 + if($zero == true)
21175 + {
21176 + $post_link = trim($itemx->get_permalink());
21177 + if(isset($rss_items[$post_link]))
21178 + {
21179 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
21180 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
21181 + }
21182 + continue;
21183 + }
21184 + if ($fauthor = $itemx->get_author())
21185 + {
21186 + $user_name = $fauthor->get_name();
21187 + }
21188 + else
21189 + {
21190 + $user_name = '';
21191 + }
21192 + $feed_cats = array();
21193 + $varx = $itemx->get_categories();
21194 + if(is_array($varx))
21195 + {
21196 + foreach ($varx as $xcategory)
21197 + {
21198 + $feed_cats[] = $xcategory->get_label();
21199 + }
21200 + }
21201 + $post_cats = implode(',', $feed_cats);
21202 + $post_excerpt = $itemx->get_description();
21203 + $final_content = $itemx->get_content();
21204 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
21205 + }
21206 + else
21207 + {
21208 + $post_link_temp = trim($itemx->get_permalink());
21209 + if(isset($rss_items[$post_link_temp]))
21210 + {
21211 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
21212 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
21213 + }
21214 + continue;
21215 + }
21216 + if ($fauthor = $itemx->get_author())
21217 + {
21218 + $user_name_temp = $fauthor->get_name();
21219 + }
21220 + else
21221 + {
21222 + $user_name_temp = '';
21223 + }
21224 + $feed_cats = array();
21225 + $varx = $itemx->get_categories();
21226 + if(is_array($varx))
21227 + {
21228 + foreach ($varx as $xcategory)
21229 + {
21230 + $feed_cats[] = $xcategory->get_label();
21231 + }
21232 + }
21233 + $post_cats_temp = implode(',', $feed_cats);
21234 + $post_excerpt_temp = $itemx->get_description();
21235 + $final_content_temp = $itemx->get_content();
21236 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link_temp, 'author' => $user_name_temp, 'cats' => $post_cats_temp, 'excerpt' => $post_excerpt_temp, 'content' => $final_content_temp );
21237 + }
21238 + $additional_kws[] = $itemx->get_title();
21239 + $zero = false;
21240 + }
21241 + unset($amazon_keyword_lines[$ji]);
21242 + }
21243 + }
21244 + }
21245 + if(count($additional_kws) > 0)
21246 + {
21247 + $amazon_keyword_lines = array_merge($amazon_keyword_lines, $additional_kws);
21248 + }
21249 + $amazon_keyword_lines = array_unique($amazon_keyword_lines);
21250 + if($overwrite_existing == '1')
21251 + {
21252 + $amazon_keyword_lines_processed = array();
21253 + foreach($amazon_keyword_lines as $zind => $ptl)
21254 + {
21255 + $ptlprocessed = explode('!###!', $ptl);
21256 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
21257 + {
21258 + $amazon_keyword_lines_processed[$zind] = $ptlprocessed[0];
21259 + }
21260 + else
21261 + {
21262 + $amazon_keyword_lines_processed[$zind] = $ptl;
21263 + }
21264 + }
21265 + foreach($posted_items as $ptit => $pid)
21266 + {
21267 + if (($key = array_search($ptit, $amazon_keyword_lines_processed)) !== false) {
21268 + $update_post_id = $pid;
21269 + break;
21270 + }
21271 + }
21272 + }
21273 + else
21274 + {
21275 + if($title_once == '1')
21276 + {
21277 + $skipt = 0;
21278 + $amazon_keyword_lines_processed = array();
21279 + foreach($amazon_keyword_lines as $zind => $ptl)
21280 + {
21281 + $ptlprocessed = explode('!###!', $ptl);
21282 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
21283 + {
21284 + $amazon_keyword_lines_processed[$zind] = $ptlprocessed[0];
21285 + }
21286 + else
21287 + {
21288 + $amazon_keyword_lines_processed[$zind] = $ptl;
21289 + }
21290 + }
21291 + foreach($posted_items as $ptit => $pid)
21292 + {
21293 + if (($key = array_search($ptit, $amazon_keyword_lines_processed)) !== false) {
21294 + aiomatic_log_to_file('Skipping Amazon reviews title, already processed: ' . $ptit);
21295 + unset($amazon_keyword_lines[$key]);
21296 + unset($amazon_keyword_lines_processed[$key]);
21297 + $skipt++;
21298 + }
21299 + }
21300 + if(count($amazon_keyword_lines) == 0 && $skipt > 0)
21301 + {
21302 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
21303 + {
21304 + if($count === 1)
21305 + {
21306 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
21307 + foreach($email_list as $thisaddr)
21308 + {
21309 + $thisaddr = trim($thisaddr);
21310 + try
21311 + {
21312 + $admin_mail = get_option('admin_email');
21313 + $to = $thisaddr;
21314 + $subject = '[Aimogen] Amazon Reviews Keywords depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
21315 + $message = 'Amazon Reviews Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its Amazon keywords list, no new posts publised! Website URL: ' . get_site_url();
21316 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
21317 + $headers[] = 'Reply-To: ' . $admin_mail;
21318 + $headers[] = 'X-Mailer: PHP/' . phpversion();
21319 + $headers[] = 'Content-Type: text/html';
21320 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
21321 + wp_mail($to, $subject, $message, $headers);
21322 + }
21323 + catch (Exception $e) {
21324 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
21325 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
21326 + }
21327 + }
21328 + }
21329 + }
21330 + }
21331 + aiomatic_log_to_file('All Amazon keywords already processed, nothing to do.');
21332 + if($auto == 1)
21333 + {
21334 + aiomatic_clearFromList($param, $type);
21335 + }
21336 + return 'nochange';
21337 + }
21338 + }
21339 + }
21340 + $spintax = new Aiomatic_Spintax();
21341 + $orig_ai_command = $ai_command;
21342 + $orig_ai_command_title = $title_ai_command;
21343 + $orig_ai_command_category = $category_ai_command;
21344 + $orig_ai_command_tag = $tag_ai_command;
21345 + $orig_ai_command_image = $ai_command_image;
21346 + $already_spinned = 0;
21347 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
21348 + {
21349 + $img_attr = $aiomatic_Main_Settings['attr_text'];
21350 + }
21351 + else
21352 + {
21353 + $img_attr = '';
21354 + }
21355 + $headings_arr = array();
21356 + if($images_list != '')
21357 + {
21358 + $images_arr = preg_split('/\r\n|\r|\n/', $images_list);
21359 + $images_arr = array_map('trim', $images_arr);
21360 + }
21361 + else
21362 + {
21363 + $images_arr = array();
21364 + }
21365 + require_once(__DIR__ . '/res/amazon-direct.php');
21366 + while(true)
21367 + {
21368 + $thread_id = '';
21369 + $my_title_outro = '';
21370 + if ($count > intval($max)) {
21371 + break;
21372 + }
21373 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon product review'));
21374 + $update_post_id = '';
21375 + $product_description = '';
21376 + $current_section = '';
21377 + $post_sections = '';
21378 + $post_topic = '';
21379 + $post_title_keywords = '';
21380 + $headings_arr_copy = $headings_arr;
21381 + $added_img_list = array();
21382 + $raw_img_list = array();
21383 + $full_result_list = array();
21384 + $added_images = 0;
21385 + $heading_results = array();
21386 + if(count($amazon_keyword_lines) == 0)
21387 + {
21388 + break;
21389 + }
21390 + if ($count > intval($max)) {
21391 + break;
21392 + }
21393 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
21394 + {
21395 + $current_index = array_rand($amazon_keyword_lines);
21396 + }
21397 + else
21398 + {
21399 + $current_index = array_key_first($amazon_keyword_lines);
21400 + }
21401 + $amazon_kw = trim($amazon_keyword_lines[$current_index]);
21402 + if($title_once == '1')
21403 + {
21404 + unset($amazon_keyword_lines[$current_index]);
21405 + }
21406 +
21407 + $amazon_kw = explode('|', $amazon_kw);
21408 + $amazon_kw = $amazon_kw[array_rand($amazon_kw)];
21409 +
21410 + $ptlprocessed = explode('!###!', $amazon_kw);
21411 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
21412 + {
21413 + $amazon_kw = $ptlprocessed[0];
21414 + $post_title_keywords = $ptlprocessed[1];
21415 + }
21416 + if(isset($rss_feeds[$amazon_kw]))
21417 + {
21418 + $post_link = $rss_feeds[$amazon_kw]['url'];
21419 + $user_name = $rss_feeds[$amazon_kw]['author'];
21420 + $post_cats = $rss_feeds[$amazon_kw]['cats'];
21421 + $post_excerpt = $rss_feeds[$amazon_kw]['excerpt'];
21422 + $final_content = $rss_feeds[$amazon_kw]['content'];
21423 + }
21424 + $tprepp = $spintax->Parse($amazon_kw);
21425 + if($tprepp != false && $tprepp != '')
21426 + {
21427 + $amazon_kw = $tprepp;
21428 + }
21429 + $old_title = $amazon_kw;
21430 + $already_spinned = 0;
21431 + $allmodels = aiomatic_get_all_models();
21432 + if($custom_shortcodes != '')
21433 + {
21434 + $custom_shortcodes_arr = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
21435 + foreach($custom_shortcodes_arr as $my_short)
21436 + {
21437 + $name_part = explode('=>', $my_short);
21438 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
21439 + {
21440 + $shortname = trim($name_part[0]);
21441 + if(strstr($amazon_kw, '%%' . $shortname . '%%'))
21442 + {
21443 + $shortval = '';
21444 + $name_part[1] = str_replace('%%search_keywords%%', $amazon_kw, $name_part[1]);
21445 + $ai_part = explode('@@', $name_part[1]);
21446 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
21447 + {
21448 + if(!in_array(trim($ai_part[0]), $allmodels))
21449 + {
21450 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
21451 + }
21452 + else
21453 + {
21454 + $aimodel = trim($ai_part[0]);
21455 + }
21456 + $zai_command = trim($ai_part[1]);
21457 + $zai_command = aiomatic_replaceSynergyShortcodes($zai_command);
21458 + $zai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $zai_command);
21459 + foreach($custom_shortcodes_arr as $my_check_short)
21460 + {
21461 + $check_name_part = explode('=>', $my_check_short);
21462 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
21463 + {
21464 + $check_shortname = trim($check_name_part[0]);
21465 + if($shortname !== $check_shortname && strstr($zai_command, '%%' . $check_shortname . '%%') !== false)
21466 + {
21467 + $check_shortval = '';
21468 + $check_ai_part = explode('@@', $check_name_part[1]);
21469 + if(!in_array(trim($check_ai_part[0]), $allmodels))
21470 + {
21471 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
21472 + }
21473 + else
21474 + {
21475 + $check_aimodel = trim($check_ai_part[0]);
21476 + }
21477 + $check_ai_command = trim($check_ai_part[1]);
21478 + $aierror = '';
21479 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
21480 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
21481 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
21482 + if($check_generated_text === false)
21483 + {
21484 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
21485 + }
21486 + else
21487 + {
21488 + $check_shortval = trim(trim($check_generated_text), '.');
21489 + }
21490 + $zai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $zai_command);
21491 + }
21492 + }
21493 + }
21494 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating custom shortcodes'));
21495 + $aierror = '';
21496 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $zai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
21497 + if($generated_text === false)
21498 + {
21499 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
21500 + }
21501 + else
21502 + {
21503 + $shortval = trim(trim($generated_text), '.');
21504 + }
21505 + }
21506 + $amazon_kw = str_replace('%%' . $shortname . '%%', $shortval, $amazon_kw);
21507 + }
21508 + }
21509 + }
21510 + }
21511 + $amazon_kw = aiomatic_replaceSynergyShortcodes($amazon_kw);
21512 + $amazon_kw = apply_filters('aiomatic_replace_aicontent_shortcode', $amazon_kw);
21513 + if(empty($amazon_kw))
21514 + {
21515 + aiomatic_log_to_file('Empty Amazon Keyword Submitted: ' . $amazon_kw);
21516 + unset($amazon_keyword_lines[$current_index]);
21517 + continue;
21518 + }
21519 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
21520 + {
21521 + aiomatic_log_to_file('Starting processing Amazon Review keyword: ' . $amazon_kw);
21522 + }
21523 + $max_prod = 1;
21524 + $amazresult = aiomatic_amazon_get_post($amazon_kw, trim($affiliate_id), $target_country, '', '', '', $max_prod, '1', array());
21525 + if(is_array($amazresult) && isset($amazresult['status']) && $amazresult['status'] == 'nothing')
21526 + {
21527 + aiomatic_log_to_file('No results found for keyword search: ' . $amazon_kw);
21528 + unset($amazon_keyword_lines[$current_index]);
21529 + continue;
21530 + }
21531 + if(!is_array($amazresult))
21532 + {
21533 + aiomatic_log_to_file('Empty rule response from Amazon Reviews: ' . $amazon_kw);
21534 + unset($amazon_keyword_lines[$current_index]);
21535 + continue;
21536 + }
21537 + if(is_array($amazresult) && count($amazresult) == 0)
21538 + {
21539 + aiomatic_log_to_file('Empty results found for keyword search: ' . $amazon_kw);
21540 + unset($amazon_keyword_lines[$current_index]);
21541 + continue;
21542 + }
21543 + $product_titles = '';
21544 + foreach($amazresult as $amr)
21545 + {
21546 + $product_titles .= '"' . $amr->offer_title . '",';
21547 + }
21548 + $amazresult = $amazresult[0];
21549 + $product_titles = rtrim($product_titles, ',');
21550 + $post_title = '';
21551 + $amazon_thumb = $amazresult->product_img;
21552 + $product_title = $amazresult->offer_title;
21553 + $product_reviews = implode('<br><br>', $amazresult->item_reviews);
21554 + $product_description = $amazresult->offer_desc;
21555 + if(trim($affiliate_id) != '')
21556 + {
21557 + $aff_url = $amazresult->offer_url;
21558 + }
21559 + else
21560 + {
21561 + $aff_url = $amazresult->source_link;
21562 + }
21563 + $product_author = $amazresult->product_author;
21564 + $product_brand = $amazresult->product_brand;
21565 + $product_isbn = $amazresult->product_isbn;
21566 + $product_upc = $amazresult->product_upc;
21567 + $offer_price = $amazresult->offer_price;
21568 + $product_price = $amazresult->product_price;
21569 + $product_list_price = $amazresult->product_list_price;
21570 + $price_numeric = $amazresult->price_numeric;
21571 + $price_currency = $amazresult->price_currency;
21572 + $product_asin = $amazresult->product_asin;
21573 + $cart_url = $amazresult->cart_url;
21574 + $list_price_numeric = $amazresult->list_price_numeric;
21575 + $product_imgs = $amazresult->product_imgs;
21576 +
21577 + //new
21578 + $item_score = $amazresult->item_score;
21579 + $language = $amazresult->language;
21580 + $edition = $amazresult->edition;
21581 + $pages_count = $amazresult->pages_count;
21582 + $publication_date = $amazresult->publication_date;
21583 + $contributors = $amazresult->contributors;
21584 + $manufacturer = $amazresult->manufacturer;
21585 + $binding = $amazresult->binding;
21586 + $product_group = $amazresult->product_group;
21587 + $rating = $amazresult->rating;
21588 + $eans = $amazresult->eans;
21589 + $part_no = $amazresult->part_no;
21590 + $model = $amazresult->model;
21591 + $warranty = $amazresult->warranty;
21592 + $color = $amazresult->color;
21593 + $is_adult = $amazresult->is_adult;
21594 + $dimensions = $amazresult->dimensions;
21595 + $date = $amazresult->date;
21596 + $size = $amazresult->size;
21597 + $unit_count = $amazresult->unit_count;
21598 + if(!empty($global_prepend))
21599 + {
21600 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
21601 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
21602 + $global_prepend = aiomatic_replace_review($global_prepend, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21603 + }
21604 + if(!empty($global_append))
21605 + {
21606 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
21607 + $global_append = aiomatic_replace_review($global_append, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21608 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
21609 + }
21610 + if($title_prompt != '')
21611 + {
21612 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
21613 + {
21614 + $title_prompt_arr = preg_split('/\r\n|\r|\n/', $title_prompt);
21615 + $my_title_prompt = $title_prompt_arr[array_rand($title_prompt_arr)];
21616 + }
21617 + else
21618 + {
21619 + $my_title_prompt = $title_prompt;
21620 + }
21621 + $zatitle_prompt = aiomatic_replace_review($my_title_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21622 + if(stristr($run_regex_on, 'title') !== false)
21623 + {
21624 + if ($strip_by_regex_prompts !== '')
21625 + {
21626 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
21627 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
21628 + $xcnt = 0;
21629 + foreach($xstrip_by_regex as $sbr)
21630 + {
21631 + if(isset($xreplace_regex[$xcnt]))
21632 + {
21633 + $repreg = $xreplace_regex[$xcnt];
21634 + }
21635 + else
21636 + {
21637 + $repreg = '';
21638 + }
21639 + $xcnt++;
21640 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $zatitle_prompt);
21641 + if($temp_cont !== NULL)
21642 + {
21643 + $zatitle_prompt = $temp_cont;
21644 + }
21645 + }
21646 + }
21647 + }
21648 + if($zatitle_prompt != '')
21649 + {
21650 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review title'));
21651 + $written = aiomatic_write_content($zatitle_prompt, $topic_title_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'title', 'user', $assistant_id, $thread_id, $store_data);
21652 + if($written === false)
21653 + {
21654 + aiomatic_log_to_file('Failed to generate title!');
21655 + if($auto == 1)
21656 + {
21657 + aiomatic_clearFromList($param, $type);
21658 + }
21659 + return 'fail';
21660 + }
21661 + else
21662 + {
21663 + $written = ucfirst(trim(trim($written), '.'));
21664 + $post_title = $written;
21665 + }
21666 + }
21667 + else
21668 + {
21669 + aiomatic_log_to_file('Empty processed title prompt. Nothing to do.');
21670 + if($auto == 1)
21671 + {
21672 + aiomatic_clearFromList($param, $type);
21673 + }
21674 + return 'fail';
21675 + }
21676 + }
21677 + if(empty($post_title))
21678 + {
21679 + aiomatic_log_to_file('Failed to automatically generate Amazon product roundup title!');
21680 + if($auto == 1)
21681 + {
21682 + aiomatic_clearFromList($param, $type);
21683 + }
21684 + return 'fail';
21685 + }
21686 + if (strpos($post_title, '%%') === false)
21687 + {
21688 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
21689 + {
21690 + $xposts = get_posts(
21691 + array(
21692 + 'post_type' => $post_type,
21693 + 'title' => html_entity_decode($post_title),
21694 + 'post_status' => 'all',
21695 + 'numberposts' => 1,
21696 + 'update_post_term_cache' => false,
21697 + 'update_post_meta_cache' => false,
21698 + 'orderby' => 'post_date ID',
21699 + 'order' => 'ASC',
21700 + )
21701 + );
21702 + if ( ! empty( $xposts ) ) {
21703 + $zap = $xposts[0];
21704 + } else {
21705 + $zap = null;
21706 + }
21707 + if($zap !== null)
21708 + {
21709 + if($overwrite_existing)
21710 + {
21711 + $update_post_id = $zap->ID;
21712 + }
21713 + else
21714 + {
21715 + aiomatic_log_to_file('Post with specified title already existing, skipping it: ' . $post_title);
21716 + unset($amazon_keyword_lines[$current_index]);
21717 + continue;
21718 + }
21719 + }
21720 + }
21721 + $new_post_title = $post_title;
21722 + }
21723 + else
21724 + {
21725 + $new_post_title = $post_title;
21726 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
21727 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
21728 + {
21729 + $xposts = get_posts(
21730 + array(
21731 + 'post_type' => $post_type,
21732 + 'title' => html_entity_decode($new_post_title),
21733 + 'post_status' => 'all',
21734 + 'numberposts' => 1,
21735 + 'update_post_term_cache' => false,
21736 + 'update_post_meta_cache' => false,
21737 + 'orderby' => 'post_date ID',
21738 + 'order' => 'ASC',
21739 + )
21740 + );
21741 + if ( ! empty( $xposts ) ) {
21742 + $zap = $xposts[0];
21743 + } else {
21744 + $zap = null;
21745 + }
21746 + if($zap !== null)
21747 + {
21748 + if($overwrite_existing)
21749 + {
21750 + $update_post_id = $zap->ID;
21751 + }
21752 + else
21753 + {
21754 + aiomatic_log_to_file('Post with specified title already published, skipping it: ' . $new_post_title);
21755 + unset($amazon_keyword_lines[$current_index]);
21756 + continue;
21757 + }
21758 + }
21759 + }
21760 + }
21761 + $current_section = '';
21762 + $headings_arr_copy = $headings_arr;
21763 + $added_img_list = array();
21764 + $raw_img_list = array();
21765 + $full_result_list = array();
21766 + $added_vid_list = array();
21767 + $added_images = 0;
21768 + $added_videos = 0;
21769 + $heading_results = array();
21770 + if(empty($post_title))
21771 + {
21772 + aiomatic_log_to_file('Empty post title submitted, nothing to do!');
21773 + continue;
21774 + }
21775 + $new_post_content = '';
21776 + if(stristr($run_regex_on, 'intro') !== false)
21777 + {
21778 + if ($strip_by_regex_prompts !== '')
21779 + {
21780 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
21781 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
21782 + $xcnt = 0;
21783 + foreach($xstrip_by_regex as $sbr)
21784 + {
21785 + if(isset($xreplace_regex[$xcnt]))
21786 + {
21787 + $repreg = $xreplace_regex[$xcnt];
21788 + }
21789 + else
21790 + {
21791 + $repreg = '';
21792 + }
21793 + $xcnt++;
21794 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $intro_prompt);
21795 + if($temp_cont !== NULL)
21796 + {
21797 + $intro_prompt = $temp_cont;
21798 + }
21799 + }
21800 + }
21801 + }
21802 + if($intro_prompt != '')
21803 + {
21804 + $my_intro_prompt = $intro_prompt;
21805 + $my_intro_prompt = aiomatic_replace_review($my_intro_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21806 + if($my_intro_prompt != '')
21807 + {
21808 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review intro'));
21809 + $written = aiomatic_write_content($my_intro_prompt, $topic_intro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'intro', 'user', $assistant_id, $thread_id, $store_data);
21810 + if($written === false)
21811 + {
21812 + aiomatic_log_to_file('Failed to generate intro for section: ' . $current_section);
21813 + continue;
21814 + }
21815 + else
21816 + {
21817 + if($new_post_content != '')
21818 + {
21819 + $new_post_content .= ' ';
21820 + }
21821 + $new_post_content .= $written;
21822 + }
21823 + }
21824 + else
21825 + {
21826 + aiomatic_log_to_file('Empty processed intro prompt. Nothing to do.');
21827 + }
21828 + }
21829 +
21830 + if(stristr($run_regex_on, 'sections') !== false)
21831 + {
21832 + if ($strip_by_regex_prompts !== '')
21833 + {
21834 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
21835 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
21836 + $xcnt = 0;
21837 + foreach($xstrip_by_regex as $sbr)
21838 + {
21839 + if(isset($xreplace_regex[$xcnt]))
21840 + {
21841 + $repreg = $xreplace_regex[$xcnt];
21842 + }
21843 + else
21844 + {
21845 + $repreg = '';
21846 + }
21847 + $xcnt++;
21848 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $sections_prompt);
21849 + if($temp_cont !== NULL)
21850 + {
21851 + $sections_prompt = $temp_cont;
21852 + }
21853 + }
21854 + }
21855 + }
21856 + if($sections_prompt != '' && $post_sections_list == '')
21857 + {
21858 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
21859 + {
21860 + $sections_prompt_arr = preg_split('/\r\n|\r|\n/', $sections_prompt);
21861 + $my_post_section = $sections_prompt_arr[array_rand($sections_prompt_arr)];
21862 + }
21863 + else
21864 + {
21865 + $my_post_section = $sections_prompt;
21866 + }
21867 + $za_post_title = aiomatic_replaceContentShortcodes($post_title, $img_attr, $ai_command);
21868 + $my_post_section = aiomatic_replace_review($my_post_section, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21869 + if($my_post_section != '')
21870 + {
21871 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review section list'));
21872 + $written = aiomatic_write_content($my_post_section, $topic_sections_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'sections', 'user', $assistant_id, $thread_id, $store_data);
21873 + if($written === false)
21874 + {
21875 + aiomatic_log_to_file('Failed to generate sections!');
21876 + if($auto == 1)
21877 + {
21878 + aiomatic_clearFromList($param, $type);
21879 + }
21880 + return 'fail';
21881 + }
21882 + else
21883 + {
21884 + $post_sections = $written;
21885 + }
21886 + }
21887 + else
21888 + {
21889 + aiomatic_log_to_file('Empty processed sections prompt (2). Nothing to do.');
21890 + if($auto == 1)
21891 + {
21892 + aiomatic_clearFromList($param, $type);
21893 + }
21894 + return 'fail';
21895 + }
21896 + }
21897 + else
21898 + {
21899 + if($post_sections_list != '')
21900 + {
21901 + $post_sections = $spintax->Parse($post_sections_list);
21902 + $post_sections = str_replace('%%topic%%', $post_topic, $post_sections);
21903 + }
21904 + }
21905 + $post_sections = aiomatic_filter_sections($post_sections);
21906 + if($post_sections != '')
21907 + {
21908 + $post_sections_arr = preg_split('/\r\n|\r|\n/', $post_sections);
21909 + }
21910 + else
21911 + {
21912 + $post_sections_arr = array();
21913 + }
21914 + $product_imgs_arr = explode(',', $product_imgs);
21915 + if($title_outro != '')
21916 + {
21917 + $my_title_outro = $title_outro;
21918 + $my_title_outro = aiomatic_replace_review($my_title_outro, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21919 + $my_title_outro = $spintax->Parse(trim($my_title_outro));
21920 + }
21921 + if(!empty($title_toc))
21922 + {
21923 + $my_title_toc = $title_toc;
21924 + $my_title_toc = aiomatic_replace_review($my_title_toc, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21925 + $my_title_toc = $spintax->Parse(trim($my_title_toc));
21926 + }
21927 + if($title_qa != '')
21928 + {
21929 + $my_title_qa = $title_qa;
21930 + $my_title_qa = aiomatic_replace_review($my_title_qa, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21931 + $my_title_qa = $spintax->Parse(trim($my_title_qa));
21932 + }
21933 + if($title_reviews != '')
21934 + {
21935 + $my_title_reviews = $title_reviews;
21936 + $my_title_reviews = aiomatic_replace_review($my_title_reviews, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21937 + $my_title_reviews = $spintax->Parse(trim($my_title_reviews));
21938 + }
21939 + if($title_proscons != '')
21940 + {
21941 + $my_title_proscons = $title_proscons;
21942 + $my_title_proscons = aiomatic_replace_review($my_title_proscons, $za_post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
21943 + $my_title_proscons = $spintax->Parse(trim($my_title_proscons));
21944 + }
21945 + if($content_prompt != '')
21946 + {
21947 + $toc = '';
21948 + if($enable_toc == '1')
21949 + {
21950 + if(!empty($title_toc))
21951 + {
21952 + if($my_title_toc != '')
21953 + {
21954 + $toc .= '<h2>' . $my_title_toc . '</h2>';
21955 + }
21956 + }
21957 + $toc .= '<ul class="toc-class">';
21958 + foreach($post_sections_arr as $the_current_section)
21959 + {
21960 + if(!empty($current_section))
21961 + {
21962 + $toc .= '<li><a href="#' . sanitize_title($the_current_section) . '">' . $the_current_section . '</a></li>';
21963 + }
21964 + }
21965 + if($title_reviews != '')
21966 + {
21967 + if($my_title_reviews != '')
21968 + {
21969 + if($reviews_prompt != '' && $enable_reviews == '1')
21970 + {
21971 + $toc .= '<li><a href="#reviews">' . $my_title_reviews . '</a></li>';
21972 + }
21973 + }
21974 + }
21975 + if($title_proscons != '')
21976 + {
21977 + if($my_title_proscons != '')
21978 + {
21979 + if($proscons_prompt != '' && $enable_proscons == '1')
21980 + {
21981 + $toc .= '<li><a href="#proscons">' . $my_title_proscons . '</a></li>';
21982 + }
21983 + }
21984 + }
21985 + if($title_qa != '')
21986 + {
21987 + if($my_title_qa != '')
21988 + {
21989 + if($qa_prompt != '' && $enable_qa == '1')
21990 + {
21991 + $toc .= '<li><a href="#qa">' . $my_title_qa . '</a></li>';
21992 + }
21993 + }
21994 + }
21995 + if($title_outro != '')
21996 + {
21997 + if($my_title_outro != '')
21998 + {
21999 + if($outro_prompt != '')
22000 + {
22001 + $toc .= '<li><a href="#outro">' . $my_title_outro . '</a></li>';
22002 + }
22003 + }
22004 + }
22005 + $toc .= '</ul>';
22006 + $new_post_content .= '<br/>' . $toc . '<br/>';
22007 + }
22008 + $s_id = 1;
22009 + $last_section_content = '';
22010 + foreach($post_sections_arr as $the_current_section)
22011 + {
22012 + $current_section = trim($the_current_section, '.;');
22013 + if(empty($current_section))
22014 + {
22015 + continue;
22016 + }
22017 + $my_post_content = $content_prompt;
22018 + $my_post_content = aiomatic_replace_review($my_post_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22019 + $my_post_content = str_replace('%%article_so_far%%', $new_post_content, $my_post_content);
22020 + $my_post_content = str_replace('%%last_section_content%%', $last_section_content, $my_post_content);
22021 + if(stristr($run_regex_on, 'content') !== false)
22022 + {
22023 + if ($strip_by_regex_prompts !== '')
22024 + {
22025 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22026 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22027 + $xcnt = 0;
22028 + foreach($xstrip_by_regex as $sbr)
22029 + {
22030 + if(isset($xreplace_regex[$xcnt]))
22031 + {
22032 + $repreg = $xreplace_regex[$xcnt];
22033 + }
22034 + else
22035 + {
22036 + $repreg = '';
22037 + }
22038 + $xcnt++;
22039 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_content);
22040 + if($temp_cont !== NULL)
22041 + {
22042 + $my_post_content = $temp_cont;
22043 + }
22044 + }
22045 + }
22046 + }
22047 + if($my_post_content != '')
22048 + {
22049 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review section ID #' . $s_id));
22050 + $s_id++;
22051 + $written = aiomatic_write_content($my_post_content, $topic_content_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'content', 'user', $assistant_id, $thread_id, $store_data);
22052 + if($written === false)
22053 + {
22054 + aiomatic_log_to_file('Failed to generate content for section: ' . $current_section);
22055 + continue;
22056 + }
22057 + else
22058 + {
22059 + $written = str_ireplace($current_section, '', $written);
22060 + preg_match_all('#\d+\.\s*([\s\S]*)#', $current_section, $mxatches);
22061 + if(isset($mxatches[1][0]))
22062 + {
22063 + $written = str_ireplace(trim($mxatches[1][0]), '', $written);
22064 + $written = str_ireplace(str_replace(['"', '\''], '', $mxatches[1][0]), '', $written);
22065 + }
22066 + $written = str_ireplace('<h2></h2>', '', $written);
22067 + $written = str_ireplace('<h3></h3>', '', $written);
22068 + $link_to_source = $current_section;
22069 + if($no_headlink != '1')
22070 + {
22071 + $link_to_source = '<a target="_blank" rel="nofollow sponsored noopener" href="' . $aff_url . '">' . $current_section . '</a>';
22072 + }
22073 + if($sections_role == 'h2')
22074 + {
22075 + $new_post_content .= '<h2 id="' . sanitize_title($current_section) . '">' . $link_to_source . '</h2>';
22076 + }
22077 + elseif($sections_role == 'h3')
22078 + {
22079 + $new_post_content .= '<h3 id="' . sanitize_title($current_section) . '">' . $link_to_source . '</h3>';
22080 + }
22081 + elseif($sections_role == 'b')
22082 + {
22083 + $new_post_content .= '<b id="' . sanitize_title($current_section) . '">' . $link_to_source . '</b>';
22084 + }
22085 + elseif($sections_role == 'i')
22086 + {
22087 + $new_post_content .= '<i id="' . sanitize_title($current_section) . '">' . $link_to_source . '</i>';
22088 + }
22089 + elseif($sections_role == 'bi')
22090 + {
22091 + $new_post_content .= '<b><i id="' . sanitize_title($current_section) . '">' . $link_to_source . '</i></b>';
22092 + }
22093 + elseif($sections_role == 'p')
22094 + {
22095 + $new_post_content .= '<p id="' . sanitize_title($current_section) . '">' . $link_to_source . '</p>';
22096 + }
22097 + elseif($sections_role == 'x')
22098 + {
22099 + $new_post_content .= '<br/><span id="' . sanitize_title($current_section) . '">' . $link_to_source . '</span><br/>';
22100 + }
22101 + else
22102 + {
22103 + $new_post_content .= '<br/>' . $link_to_source . '<br/>';
22104 + }
22105 + if($topic_images == '1')
22106 + {
22107 + if(count($product_imgs_arr) > 0)
22108 + {
22109 + $img_index = array_rand($product_imgs_arr);
22110 + $this_img = $product_imgs_arr[$img_index];
22111 + unset($product_imgs_arr[$img_index]);
22112 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
22113 + {
22114 + $localpath = aiomatic_copy_image_locally($this_img, $attached_id, '', false, '');
22115 + if(!empty($attached_id))
22116 + {
22117 + $additional_attachments[] = $attached_id;
22118 + }
22119 + if($localpath !== false)
22120 + {
22121 + $this_img = $localpath[0];
22122 + }
22123 + }
22124 + $new_post_content .= '<br/><img class="rimage_class" src="' . $this_img . '" alt="' . esc_attr($post_title) . '"><br/>';
22125 + $added_img_list[] = $this_img;
22126 + $added_images++;
22127 + }
22128 + }
22129 + $last_section_content = $written;
22130 + $new_post_content .= $written;
22131 + }
22132 + }
22133 + else
22134 + {
22135 + aiomatic_log_to_file('Empty processed content prompt. Nothing to do.');
22136 + }
22137 + }
22138 + }
22139 + if($enable_reviews == '1')
22140 + {
22141 + if($reviews_prompt != '')
22142 + {
22143 + $my_reviews_prompt = $reviews_prompt;
22144 + $my_reviews_prompt = aiomatic_replace_review($my_reviews_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22145 + if(stristr($run_regex_on, 'reviews') !== false)
22146 + {
22147 + if ($strip_by_regex_prompts !== '')
22148 + {
22149 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22150 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22151 + $xcnt = 0;
22152 + foreach($xstrip_by_regex as $sbr)
22153 + {
22154 + if(isset($xreplace_regex[$xcnt]))
22155 + {
22156 + $repreg = $xreplace_regex[$xcnt];
22157 + }
22158 + else
22159 + {
22160 + $repreg = '';
22161 + }
22162 + $xcnt++;
22163 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_reviews_prompt);
22164 + if($temp_cont !== NULL)
22165 + {
22166 + $my_reviews_prompt = $temp_cont;
22167 + }
22168 + }
22169 + }
22170 + }
22171 + if($my_reviews_prompt != '')
22172 + {
22173 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review analysis'));
22174 + $written = aiomatic_write_content($my_reviews_prompt, $topic_reviews_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'reviews', 'user', $assistant_id, $thread_id, $store_data);
22175 + if($written === false)
22176 + {
22177 + aiomatic_log_to_file('Failed to generate Reviews Analysis for topic: ' . $current_section);
22178 + continue;
22179 + }
22180 + else
22181 + {
22182 + if($new_post_content != '')
22183 + {
22184 + $new_post_content .= ' ';
22185 + }
22186 + if($my_title_reviews !== '')
22187 + {
22188 + if($sections_role == 'h2')
22189 + {
22190 + $new_post_content .= '<h2 id="reviews">' . $my_title_reviews . '</h2>';
22191 + }
22192 + elseif($sections_role == 'h3')
22193 + {
22194 + $new_post_content .= '<h3 id="reviews">' . $my_title_reviews . '</h3>';
22195 + }
22196 + elseif($sections_role == 'b')
22197 + {
22198 + $new_post_content .= '<b id="reviews">' . $my_title_reviews . '</b>';
22199 + }
22200 + elseif($sections_role == 'i')
22201 + {
22202 + $new_post_content .= '<i id="reviews">' . $my_title_reviews . '</i>';
22203 + }
22204 + elseif($sections_role == 'bi')
22205 + {
22206 + $new_post_content .= '<b><i id="reviews">' . $my_title_reviews . '</i></b>';
22207 + }
22208 + elseif($sections_role == 'p')
22209 + {
22210 + $new_post_content .= '<p id="reviews">' . $my_title_reviews . '</p>';
22211 + }
22212 + elseif($sections_role == 'x')
22213 + {
22214 + $new_post_content .= '<br/><span id="reviews">' . $my_title_reviews . '</span><br/>';
22215 + }
22216 + else
22217 + {
22218 + $new_post_content .= '<br/>' . $my_title_reviews . '<br/>';
22219 + }
22220 + }
22221 + if($topic_images == '1')
22222 + {
22223 + if(count($product_imgs_arr) > 0)
22224 + {
22225 + $img_index = array_rand($product_imgs_arr);
22226 + $this_img = $product_imgs_arr[$img_index];
22227 + unset($product_imgs_arr[$img_index]);
22228 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
22229 + {
22230 + $localpath = aiomatic_copy_image_locally($this_img, $attached_id, '', false, '');
22231 + if(!empty($attached_id))
22232 + {
22233 + $additional_attachments[] = $attached_id;
22234 + }
22235 + if($localpath !== false)
22236 + {
22237 + $this_img = $localpath[0];
22238 + }
22239 + }
22240 + $new_post_content .= '<br/><img class="rimage_class" src="' . $this_img . '" alt="' . esc_attr($post_title) . '"><br/>';
22241 + $added_img_list[] = $this_img;
22242 + $added_images++;
22243 + }
22244 + }
22245 + $new_post_content .= $written;
22246 + }
22247 + }
22248 + else
22249 + {
22250 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
22251 + }
22252 + }
22253 + }
22254 + if($enable_proscons == '1')
22255 + {
22256 + if($proscons_prompt != '')
22257 + {
22258 + $my_proscons_prompt = $proscons_prompt;
22259 + $my_proscons_prompt = aiomatic_replace_review($my_proscons_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22260 + if(stristr($run_regex_on, 'proscons') !== false)
22261 + {
22262 + if ($strip_by_regex_prompts !== '')
22263 + {
22264 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22265 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22266 + $xcnt = 0;
22267 + foreach($xstrip_by_regex as $sbr)
22268 + {
22269 + if(isset($xreplace_regex[$xcnt]))
22270 + {
22271 + $repreg = $xreplace_regex[$xcnt];
22272 + }
22273 + else
22274 + {
22275 + $repreg = '';
22276 + }
22277 + $xcnt++;
22278 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_proscons_prompt);
22279 + if($temp_cont !== NULL)
22280 + {
22281 + $my_proscons_prompt = $temp_cont;
22282 + }
22283 + }
22284 + }
22285 + }
22286 + if($my_proscons_prompt != '')
22287 + {
22288 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review pros&cons'));
22289 + $written = aiomatic_write_content($my_proscons_prompt, $topic_proscons_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'proscons', 'user', $assistant_id, $thread_id, $store_data);
22290 + if($written === false)
22291 + {
22292 + aiomatic_log_to_file('Failed to generate Pros & Cons for topic: ' . $current_section);
22293 + continue;
22294 + }
22295 + else
22296 + {
22297 + if($new_post_content != '')
22298 + {
22299 + $new_post_content .= ' ';
22300 + }
22301 + if($my_title_proscons !== '')
22302 + {
22303 + if($sections_role == 'h2')
22304 + {
22305 + $new_post_content .= '<h2 id="proscons">' . $my_title_proscons . '</h2>';
22306 + }
22307 + elseif($sections_role == 'h3')
22308 + {
22309 + $new_post_content .= '<h3 id="proscons">' . $my_title_proscons . '</h3>';
22310 + }
22311 + elseif($sections_role == 'b')
22312 + {
22313 + $new_post_content .= '<b id="proscons">' . $my_title_proscons . '</b>';
22314 + }
22315 + elseif($sections_role == 'i')
22316 + {
22317 + $new_post_content .= '<i id="proscons">' . $my_title_proscons . '</i>';
22318 + }
22319 + elseif($sections_role == 'bi')
22320 + {
22321 + $new_post_content .= '<b><i id="proscons">' . $my_title_proscons . '</i></b>';
22322 + }
22323 + elseif($sections_role == 'p')
22324 + {
22325 + $new_post_content .= '<p id="proscons">' . $my_title_proscons . '</p>';
22326 + }
22327 + elseif($sections_role == 'x')
22328 + {
22329 + $new_post_content .= '<br/><span id="proscons">' . $my_title_proscons . '</span><br/>';
22330 + }
22331 + else
22332 + {
22333 + $new_post_content .= '<br/>' . $my_title_proscons . '<br/>';
22334 + }
22335 + }
22336 + if($topic_images == '1')
22337 + {
22338 + if(count($product_imgs_arr) > 0)
22339 + {
22340 + $img_index = array_rand($product_imgs_arr);
22341 + $this_img = $product_imgs_arr[$img_index];
22342 + unset($product_imgs_arr[$img_index]);
22343 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
22344 + {
22345 + $localpath = aiomatic_copy_image_locally($this_img, $attached_id, '', false, '');
22346 + if(!empty($attached_id))
22347 + {
22348 + $additional_attachments[] = $attached_id;
22349 + }
22350 + if($localpath !== false)
22351 + {
22352 + $this_img = $localpath[0];
22353 + }
22354 + }
22355 + $new_post_content .= '<br/><img class="rimage_class" src="' . $this_img . '" alt="' . esc_attr($post_title) . '"><br/>';
22356 + $added_img_list[] = $this_img;
22357 + $added_images++;
22358 + }
22359 + }
22360 + $new_post_content .= $written;
22361 + }
22362 + }
22363 + else
22364 + {
22365 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
22366 + }
22367 + }
22368 + }
22369 + if($enable_qa == '1')
22370 + {
22371 + if($qa_prompt != '')
22372 + {
22373 + $my_qa_prompt = $qa_prompt;
22374 + $my_qa_prompt = aiomatic_replace_review($my_qa_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22375 + if(stristr($run_regex_on, 'qa') !== false)
22376 + {
22377 + if ($strip_by_regex_prompts !== '')
22378 + {
22379 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22380 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22381 + $xcnt = 0;
22382 + foreach($xstrip_by_regex as $sbr)
22383 + {
22384 + if(isset($xreplace_regex[$xcnt]))
22385 + {
22386 + $repreg = $xreplace_regex[$xcnt];
22387 + }
22388 + else
22389 + {
22390 + $repreg = '';
22391 + }
22392 + $xcnt++;
22393 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_qa_prompt);
22394 + if($temp_cont !== NULL)
22395 + {
22396 + $my_qa_prompt = $temp_cont;
22397 + }
22398 + }
22399 + }
22400 + }
22401 + if($my_qa_prompt != '')
22402 + {
22403 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review qa'));
22404 + $written = aiomatic_write_content($my_qa_prompt, $topic_qa_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, false, false, 'qa', 'user', $assistant_id, $thread_id, $store_data);
22405 + if($written === false)
22406 + {
22407 + aiomatic_log_to_file('Failed to generate Q&A for topic: ' . $current_section);
22408 + continue;
22409 + }
22410 + else
22411 + {
22412 + if($new_post_content != '')
22413 + {
22414 + $new_post_content .= ' ';
22415 + }
22416 + if($my_title_qa !== '')
22417 + {
22418 + if($sections_role == 'h2')
22419 + {
22420 + $new_post_content .= '<h2 id="qa">' . $my_title_qa . '</h2>';
22421 + }
22422 + elseif($sections_role == 'h3')
22423 + {
22424 + $new_post_content .= '<h3 id="qa">' . $my_title_qa . '</h3>';
22425 + }
22426 + elseif($sections_role == 'b')
22427 + {
22428 + $new_post_content .= '<b id="qa">' . $my_title_qa . '</b>';
22429 + }
22430 + elseif($sections_role == 'i')
22431 + {
22432 + $new_post_content .= '<i id="qa">' . $my_title_qa . '</i>';
22433 + }
22434 + elseif($sections_role == 'bi')
22435 + {
22436 + $new_post_content .= '<b><i id="qa">' . $my_title_qa . '</i></b>';
22437 + }
22438 + elseif($sections_role == 'p')
22439 + {
22440 + $new_post_content .= '<p id="qa">' . $my_title_qa . '</p>';
22441 + }
22442 + elseif($sections_role == 'x')
22443 + {
22444 + $new_post_content .= '<br/><span id="qa">' . $my_title_qa . '</span><br/>';
22445 + }
22446 + else
22447 + {
22448 + $new_post_content .= '<br/>' . $my_title_qa . '<br/>';
22449 + }
22450 + }
22451 + if($topic_images == '1')
22452 + {
22453 + if(count($product_imgs_arr) > 0)
22454 + {
22455 + $img_index = array_rand($product_imgs_arr);
22456 + $this_img = $product_imgs_arr[$img_index];
22457 + unset($product_imgs_arr[$img_index]);
22458 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
22459 + {
22460 + $localpath = aiomatic_copy_image_locally($this_img, $attached_id, '', false, '');
22461 + if(!empty($attached_id))
22462 + {
22463 + $additional_attachments[] = $attached_id;
22464 + }
22465 + if($localpath !== false)
22466 + {
22467 + $this_img = $localpath[0];
22468 + }
22469 + }
22470 + $new_post_content .= '<br/><img class="rimage_class" src="' . $this_img . '" alt="' . esc_attr($post_title) . '"><br/>';
22471 + $added_img_list[] = $this_img;
22472 + $added_images++;
22473 + }
22474 + }
22475 + $new_post_content .= $written;
22476 + }
22477 + }
22478 + else
22479 + {
22480 + aiomatic_log_to_file('Empty processed Q&A prompt. Nothing to do.');
22481 + }
22482 + }
22483 + }
22484 + if($outro_prompt != '')
22485 + {
22486 + $my_outro_prompt = $outro_prompt;
22487 + $my_outro_prompt = aiomatic_replace_review($my_outro_prompt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22488 + if(stristr($run_regex_on, 'outro') !== false)
22489 + {
22490 + if ($strip_by_regex_prompts !== '')
22491 + {
22492 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22493 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22494 + $xcnt = 0;
22495 + foreach($xstrip_by_regex as $sbr)
22496 + {
22497 + if(isset($xreplace_regex[$xcnt]))
22498 + {
22499 + $repreg = $xreplace_regex[$xcnt];
22500 + }
22501 + else
22502 + {
22503 + $repreg = '';
22504 + }
22505 + $xcnt++;
22506 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_outro_prompt);
22507 + if($temp_cont !== NULL)
22508 + {
22509 + $my_outro_prompt = $temp_cont;
22510 + }
22511 + }
22512 + }
22513 + }
22514 + if($my_outro_prompt != '')
22515 + {
22516 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review outro'));
22517 + $written = aiomatic_write_content($my_outro_prompt, $topic_outro_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'outro', 'user', $assistant_id, $thread_id, $store_data);
22518 + if($written === false)
22519 + {
22520 + aiomatic_log_to_file('Failed to generate outro for section: ' . $current_section);
22521 + continue;
22522 + }
22523 + else
22524 + {
22525 + if($new_post_content != '')
22526 + {
22527 + $new_post_content .= ' ';
22528 + }
22529 + if($title_outro !== '')
22530 + {
22531 + if($my_title_outro != '')
22532 + {
22533 + if($sections_role == 'h2')
22534 + {
22535 + $new_post_content .= '<h2 id="outro">' . $my_title_outro . '</h2>';
22536 + }
22537 + elseif($sections_role == 'h3')
22538 + {
22539 + $new_post_content .= '<h3 id="outro">' . $my_title_outro . '</h3>';
22540 + }
22541 + elseif($sections_role == 'b')
22542 + {
22543 + $new_post_content .= '<b id="outro">' . $my_title_outro . '</b>';
22544 + }
22545 + elseif($sections_role == 'i')
22546 + {
22547 + $new_post_content .= '<i id="outro">' . $my_title_outro . '</i>';
22548 + }
22549 + elseif($sections_role == 'bi')
22550 + {
22551 + $new_post_content .= '<b><i id="outro">' . $my_title_outro . '</i></b>';
22552 + }
22553 + elseif($sections_role == 'p')
22554 + {
22555 + $new_post_content .= '<p id="outro">' . $my_title_outro . '</p>';
22556 + }
22557 + elseif($sections_role == 'x')
22558 + {
22559 + $new_post_content .= '<br/><span id="outro">' . $my_title_outro . '</span><br/>';
22560 + }
22561 + else
22562 + {
22563 + $new_post_content .= '<br/>' . $my_title_outro . '<br/>';
22564 + }
22565 + }
22566 + }
22567 + else
22568 + {
22569 + $new_post_content .= '<br/><br/>';
22570 + }
22571 + if($topic_images == '1')
22572 + {
22573 + if(count($product_imgs_arr) > 0)
22574 + {
22575 + $img_index = array_rand($product_imgs_arr);
22576 + $this_img = $product_imgs_arr[$img_index];
22577 + unset($product_imgs_arr[$img_index]);
22578 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
22579 + {
22580 + $localpath = aiomatic_copy_image_locally($this_img, $attached_id, '', false, '');
22581 + if(!empty($attached_id))
22582 + {
22583 + $additional_attachments[] = $attached_id;
22584 + }
22585 + if($localpath !== false)
22586 + {
22587 + $this_img = $localpath[0];
22588 + }
22589 + }
22590 + $new_post_content .= '<br/><img class="rimage_class" src="' . $this_img . '" alt="' . esc_attr($post_title) . '"><br/>';
22591 + $added_img_list[] = $this_img;
22592 + $added_images++;
22593 + }
22594 + }
22595 + $new_post_content .= $written;
22596 + }
22597 + }
22598 + else
22599 + {
22600 + aiomatic_log_to_file('Empty processed outro prompt. Nothing to do.');
22601 + }
22602 + }
22603 + if($new_post_content == '')
22604 + {
22605 + aiomatic_log_to_file("Warning, empty post content because of empty content prompt!");
22606 + }
22607 + $new_post_excerpt = '';
22608 + if($excerpt_prompt != '')
22609 + {
22610 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
22611 + {
22612 + $excerpt_prompt_arr = preg_split('/\r\n|\r|\n/', $excerpt_prompt);
22613 + $my_post_excerpt = $excerpt_prompt_arr[array_rand($excerpt_prompt_arr)];
22614 + }
22615 + else
22616 + {
22617 + $my_post_excerpt = $excerpt_prompt;
22618 + }
22619 + $my_post_excerpt = aiomatic_replace_review($my_post_excerpt, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
22620 + if(stristr($run_regex_on, 'excerpt') !== false)
22621 + {
22622 + if ($strip_by_regex_prompts !== '')
22623 + {
22624 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex_prompts);
22625 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex_prompts);
22626 + $xcnt = 0;
22627 + foreach($xstrip_by_regex as $sbr)
22628 + {
22629 + if(isset($xreplace_regex[$xcnt]))
22630 + {
22631 + $repreg = $xreplace_regex[$xcnt];
22632 + }
22633 + else
22634 + {
22635 + $repreg = '';
22636 + }
22637 + $xcnt++;
22638 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post_excerpt);
22639 + if($temp_cont !== NULL)
22640 + {
22641 + $my_post_excerpt = $temp_cont;
22642 + }
22643 + }
22644 + }
22645 + }
22646 + if($my_post_excerpt != '')
22647 + {
22648 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Amazon review excerpt'));
22649 + $written = aiomatic_write_content($my_post_excerpt, $topic_excerpt_model, $temperature, $top_p, $presence_penalty, $frequency_penalty, $max_tokens, $token, $global_prepend, $global_append, true, false, 'excerpt', 'user', $assistant_id, $thread_id, $store_data);
22650 + if($written === false)
22651 + {
22652 + aiomatic_log_to_file('Failed to generate excerpt!');
22653 + }
22654 + else
22655 + {
22656 + $new_post_excerpt = $written;
22657 + }
22658 + }
22659 + else
22660 + {
22661 + aiomatic_log_to_file('Empty processed excerpt prompt. Nothing to do.');
22662 + }
22663 + }
22664 +
22665 + $get_img = '';
22666 + if($royalty_free == '1')
22667 + {
22668 + if($enable_ai_images == '1')
22669 + {
22670 + $query_words = $post_title;
22671 + if($image_query == '')
22672 + {
22673 + $image_query = $temp_post;
22674 + }
22675 + if($orig_ai_command_image == '')
22676 + {
22677 + $orig_ai_command_image = $image_query;
22678 + }
22679 + if($orig_ai_command_image != '')
22680 + {
22681 + $ai_command_image = $orig_ai_command_image;
22682 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
22683 + {
22684 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
22685 + $ai_command_image = array_filter($ai_command_image);
22686 + if(count($ai_command_image) > 0)
22687 + {
22688 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
22689 + }
22690 + else
22691 + {
22692 + $ai_command_image = '';
22693 + }
22694 + }
22695 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22696 + if(!empty($ai_command_image))
22697 + {
22698 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22699 + }
22700 + else
22701 + {
22702 + $ai_command_image = trim(strip_tags($post_title));
22703 + }
22704 + $ai_command_image = trim($ai_command_image);
22705 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
22706 + {
22707 + $txt_content = aiomatic_get_web_page($ai_command_image);
22708 + if ($txt_content !== FALSE)
22709 + {
22710 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
22711 + $txt_content = array_filter($txt_content);
22712 + if(count($txt_content) > 0)
22713 + {
22714 + $txt_content = $txt_content[array_rand($txt_content)];
22715 + if(trim($txt_content) != '')
22716 + {
22717 + $ai_command_image = $txt_content;
22718 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22719 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22720 + }
22721 + }
22722 + }
22723 + }
22724 + if(empty($ai_command_image))
22725 + {
22726 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
22727 + }
22728 + else
22729 + {
22730 + if(aiomatic_strlen($ai_command_image) > 1000)
22731 + {
22732 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
22733 + }
22734 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22735 + {
22736 + $api_service = aiomatic_get_api_service($token, $image_model);
22737 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
22738 + }
22739 + $skip_this_copy = true;
22740 + if(!function_exists('is_plugin_active'))
22741 + {
22742 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
22743 + }
22744 + 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')
22745 + {
22746 + $skip_this_copy = false;
22747 + }
22748 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
22749 + {
22750 + $skip_this_copy = false;
22751 + }
22752 + $aierror = '';
22753 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating AI image'));
22754 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $post_title);
22755 + if($get_img !== false)
22756 + {
22757 + foreach($get_img as $tmpimg)
22758 + {
22759 + $get_img = $tmpimg;
22760 + break;
22761 + }
22762 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22763 + {
22764 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
22765 + }
22766 + }
22767 + else
22768 + {
22769 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
22770 + $get_img = '';
22771 + }
22772 + }
22773 + }
22774 + else
22775 + {
22776 + aiomatic_log_to_file('Empty AI featured image query entered.');
22777 + }
22778 + }
22779 + elseif($enable_ai_images == '2')
22780 + {
22781 + $query_words = $post_title;
22782 + if($image_query == '')
22783 + {
22784 + $image_query = $temp_post;
22785 + }
22786 + if($orig_ai_command_image == '')
22787 + {
22788 + $orig_ai_command_image = $image_query;
22789 + }
22790 + if($orig_ai_command_image != '')
22791 + {
22792 + $ai_command_image = $orig_ai_command_image;
22793 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
22794 + {
22795 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
22796 + $ai_command_image = array_filter($ai_command_image);
22797 + if(count($ai_command_image) > 0)
22798 + {
22799 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
22800 + }
22801 + else
22802 + {
22803 + $ai_command_image = '';
22804 + }
22805 + }
22806 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22807 + if(!empty($ai_command_image))
22808 + {
22809 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22810 + }
22811 + else
22812 + {
22813 + $ai_command_image = trim(strip_tags($post_title));
22814 + }
22815 + $ai_command_image = trim($ai_command_image);
22816 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
22817 + {
22818 + $txt_content = aiomatic_get_web_page($ai_command_image);
22819 + if ($txt_content !== FALSE)
22820 + {
22821 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
22822 + $txt_content = array_filter($txt_content);
22823 + if(count($txt_content) > 0)
22824 + {
22825 + $txt_content = $txt_content[array_rand($txt_content)];
22826 + if(trim($txt_content) != '')
22827 + {
22828 + $ai_command_image = $txt_content;
22829 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22830 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22831 + }
22832 + }
22833 + }
22834 + }
22835 + if(empty($ai_command_image))
22836 + {
22837 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
22838 + }
22839 + else
22840 + {
22841 + if(aiomatic_strlen($ai_command_image) > 2000)
22842 + {
22843 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
22844 + }
22845 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22846 + {
22847 + $api_service = 'Stability.AI';
22848 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
22849 + }
22850 + if($image_size == '256x256')
22851 + {
22852 + $width = '512';
22853 + $height = '512';
22854 + }
22855 + elseif($image_size == '512x512')
22856 + {
22857 + $width = '512';
22858 + $height = '512';
22859 + }
22860 + elseif($image_size == '1024x1024')
22861 + {
22862 + $width = '1024';
22863 + $height = '1024';
22864 + }
22865 + else
22866 + {
22867 + $width = '512';
22868 + $height = '512';
22869 + }
22870 + $skip_this_copy = true;
22871 + if(!function_exists('is_plugin_active'))
22872 + {
22873 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
22874 + }
22875 + 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')
22876 + {
22877 + $skip_this_copy = false;
22878 + }
22879 + $ierror = '';
22880 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Stable Diffusion AI image'));
22881 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
22882 + if($get_img !== false)
22883 + {
22884 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22885 + {
22886 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
22887 + }
22888 + }
22889 + else
22890 + {
22891 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
22892 + $get_img = '';
22893 + }
22894 + }
22895 + }
22896 + else
22897 + {
22898 + aiomatic_log_to_file('Empty AI featured image query entered.');
22899 + }
22900 + }
22901 + elseif($enable_ai_images == '7')
22902 + {
22903 + $query_words = $post_title;
22904 + if($image_query == '')
22905 + {
22906 + $image_query = $temp_post;
22907 + }
22908 + if($orig_ai_command_image == '')
22909 + {
22910 + $orig_ai_command_image = $image_query;
22911 + }
22912 + if($orig_ai_command_image != '')
22913 + {
22914 + $ai_command_image = $orig_ai_command_image;
22915 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
22916 + {
22917 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
22918 + $ai_command_image = array_filter($ai_command_image);
22919 + if(count($ai_command_image) > 0)
22920 + {
22921 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
22922 + }
22923 + else
22924 + {
22925 + $ai_command_image = '';
22926 + }
22927 + }
22928 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22929 + if(!empty($ai_command_image))
22930 + {
22931 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22932 + }
22933 + else
22934 + {
22935 + $ai_command_image = trim(strip_tags($post_title));
22936 + }
22937 + $ai_command_image = trim($ai_command_image);
22938 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
22939 + {
22940 + $txt_content = aiomatic_get_web_page($ai_command_image);
22941 + if ($txt_content !== FALSE)
22942 + {
22943 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
22944 + $txt_content = array_filter($txt_content);
22945 + if(count($txt_content) > 0)
22946 + {
22947 + $txt_content = $txt_content[array_rand($txt_content)];
22948 + if(trim($txt_content) != '')
22949 + {
22950 + $ai_command_image = $txt_content;
22951 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
22952 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
22953 + }
22954 + }
22955 + }
22956 + }
22957 + if(empty($ai_command_image))
22958 + {
22959 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
22960 + }
22961 + else
22962 + {
22963 + if(aiomatic_strlen($ai_command_image) > 2000)
22964 + {
22965 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
22966 + }
22967 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22968 + {
22969 + $api_service = 'Google';
22970 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
22971 + }
22972 + $skip_this_copy = true;
22973 + if(!function_exists('is_plugin_active'))
22974 + {
22975 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
22976 + }
22977 + 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')
22978 + {
22979 + $skip_this_copy = false;
22980 + }
22981 + if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '')
22982 + {
22983 + $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
22984 + }
22985 + else
22986 + {
22987 + $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
22988 + }
22989 + $ierror = '';
22990 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Google AI image'));
22991 + $get_img = aiomatic_generate_image_google($ai_command_image, $model, 'featuredGoogleImage', false, $ierror, false);
22992 + if($get_img !== false)
22993 + {
22994 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
22995 + {
22996 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
22997 + }
22998 + }
22999 + else
23000 + {
23001 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
23002 + $get_img = '';
23003 + }
23004 + }
23005 + }
23006 + else
23007 + {
23008 + aiomatic_log_to_file('Empty AI featured image query entered.');
23009 + }
23010 + }
23011 + elseif($enable_ai_images == '4')
23012 + {
23013 + $query_words = $post_title;
23014 + if($image_query == '')
23015 + {
23016 + $image_query = $temp_post;
23017 + }
23018 + if($orig_ai_command_image == '')
23019 + {
23020 + $orig_ai_command_image = $image_query;
23021 + }
23022 + if($orig_ai_command_image != '')
23023 + {
23024 + $ai_command_image = $orig_ai_command_image;
23025 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
23026 + {
23027 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
23028 + $ai_command_image = array_filter($ai_command_image);
23029 + if(count($ai_command_image) > 0)
23030 + {
23031 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
23032 + }
23033 + else
23034 + {
23035 + $ai_command_image = '';
23036 + }
23037 + }
23038 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23039 + if(!empty($ai_command_image))
23040 + {
23041 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23042 + }
23043 + else
23044 + {
23045 + $ai_command_image = trim(strip_tags($post_title));
23046 + }
23047 + $ai_command_image = trim($ai_command_image);
23048 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
23049 + {
23050 + $txt_content = aiomatic_get_web_page($ai_command_image);
23051 + if ($txt_content !== FALSE)
23052 + {
23053 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
23054 + $txt_content = array_filter($txt_content);
23055 + if(count($txt_content) > 0)
23056 + {
23057 + $txt_content = $txt_content[array_rand($txt_content)];
23058 + if(trim($txt_content) != '')
23059 + {
23060 + $ai_command_image = $txt_content;
23061 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23062 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23063 + }
23064 + }
23065 + }
23066 + }
23067 + if(empty($ai_command_image))
23068 + {
23069 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
23070 + }
23071 + else
23072 + {
23073 + if(aiomatic_strlen($ai_command_image) > 2000)
23074 + {
23075 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
23076 + }
23077 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23078 + {
23079 + $api_service = 'Midjourney';
23080 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
23081 + }
23082 + if($image_size == '256x256')
23083 + {
23084 + $width = '512';
23085 + $height = '512';
23086 + }
23087 + elseif($image_size == '512x512')
23088 + {
23089 + $width = '512';
23090 + $height = '512';
23091 + }
23092 + elseif($image_size == '1024x1024')
23093 + {
23094 + $width = '1024';
23095 + $height = '1024';
23096 + }
23097 + elseif($image_size == '1792x1024')
23098 + {
23099 + $width = '1792';
23100 + $height = '1024';
23101 + }
23102 + elseif($image_size == '1024x1792')
23103 + {
23104 + $width = '1024';
23105 + $height = '1792';
23106 + }
23107 + else
23108 + {
23109 + $width = '512';
23110 + $height = '512';
23111 + }
23112 + $skip_this_copy = true;
23113 + if(!function_exists('is_plugin_active'))
23114 + {
23115 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23116 + }
23117 + 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')
23118 + {
23119 + $skip_this_copy = false;
23120 + }
23121 + $ierror = '';
23122 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Midjourney AI image'));
23123 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
23124 + if($get_img !== false)
23125 + {
23126 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23127 + {
23128 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
23129 + }
23130 + }
23131 + else
23132 + {
23133 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
23134 + $get_img = '';
23135 + }
23136 + }
23137 + }
23138 + else
23139 + {
23140 + aiomatic_log_to_file('Empty AI featured image query entered.');
23141 + }
23142 + }
23143 + elseif($enable_ai_images == '5')
23144 + {
23145 + $query_words = $post_title;
23146 + if($image_query == '')
23147 + {
23148 + $image_query = $temp_post;
23149 + }
23150 + if($orig_ai_command_image == '')
23151 + {
23152 + $orig_ai_command_image = $image_query;
23153 + }
23154 + if($orig_ai_command_image != '')
23155 + {
23156 + $ai_command_image = $orig_ai_command_image;
23157 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
23158 + {
23159 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
23160 + $ai_command_image = array_filter($ai_command_image);
23161 + if(count($ai_command_image) > 0)
23162 + {
23163 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
23164 + }
23165 + else
23166 + {
23167 + $ai_command_image = '';
23168 + }
23169 + }
23170 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23171 + if(!empty($ai_command_image))
23172 + {
23173 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23174 + }
23175 + else
23176 + {
23177 + $ai_command_image = trim(strip_tags($post_title));
23178 + }
23179 + $ai_command_image = trim($ai_command_image);
23180 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
23181 + {
23182 + $txt_content = aiomatic_get_web_page($ai_command_image);
23183 + if ($txt_content !== FALSE)
23184 + {
23185 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
23186 + $txt_content = array_filter($txt_content);
23187 + if(count($txt_content) > 0)
23188 + {
23189 + $txt_content = $txt_content[array_rand($txt_content)];
23190 + if(trim($txt_content) != '')
23191 + {
23192 + $ai_command_image = $txt_content;
23193 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23194 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23195 + }
23196 + }
23197 + }
23198 + }
23199 + if(empty($ai_command_image))
23200 + {
23201 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
23202 + }
23203 + else
23204 + {
23205 + if(aiomatic_strlen($ai_command_image) > 2000)
23206 + {
23207 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
23208 + }
23209 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23210 + {
23211 + $api_service = 'Replicate';
23212 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
23213 + }
23214 + if($image_size == '256x256')
23215 + {
23216 + $width = '512';
23217 + $height = '512';
23218 + }
23219 + elseif($image_size == '512x512')
23220 + {
23221 + $width = '512';
23222 + $height = '512';
23223 + }
23224 + elseif($image_size == '1024x1024')
23225 + {
23226 + $width = '1024';
23227 + $height = '1024';
23228 + }
23229 + elseif($image_size == '1792x1024')
23230 + {
23231 + $width = '1792';
23232 + $height = '1024';
23233 + }
23234 + elseif($image_size == '1024x1792')
23235 + {
23236 + $width = '1024';
23237 + $height = '1792';
23238 + }
23239 + else
23240 + {
23241 + $width = '512';
23242 + $height = '512';
23243 + }
23244 + $skip_this_copy = true;
23245 + if(!function_exists('is_plugin_active'))
23246 + {
23247 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23248 + }
23249 + 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')
23250 + {
23251 + $skip_this_copy = false;
23252 + }
23253 + $ierror = '';
23254 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Replicate AI image'));
23255 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredReplicateImage', $skip_this_copy, $ierror);
23256 + if($get_img !== false)
23257 + {
23258 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23259 + {
23260 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
23261 + }
23262 + }
23263 + else
23264 + {
23265 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
23266 + $get_img = '';
23267 + }
23268 + }
23269 + }
23270 + else
23271 + {
23272 + aiomatic_log_to_file('Empty AI featured image query entered.');
23273 + }
23274 + }
23275 + elseif($enable_ai_images == '6')
23276 + {
23277 + $query_words = $post_title;
23278 + if($image_query == '')
23279 + {
23280 + $image_query = $temp_post;
23281 + }
23282 + if($orig_ai_command_image == '')
23283 + {
23284 + $orig_ai_command_image = $image_query;
23285 + }
23286 + if($orig_ai_command_image != '')
23287 + {
23288 + $ai_command_image = $orig_ai_command_image;
23289 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
23290 + {
23291 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
23292 + $ai_command_image = array_filter($ai_command_image);
23293 + if(count($ai_command_image) > 0)
23294 + {
23295 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
23296 + }
23297 + else
23298 + {
23299 + $ai_command_image = '';
23300 + }
23301 + }
23302 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23303 + if(!empty($ai_command_image))
23304 + {
23305 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23306 + }
23307 + else
23308 + {
23309 + $ai_command_image = trim(strip_tags($post_title));
23310 + }
23311 + $ai_command_image = trim($ai_command_image);
23312 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
23313 + {
23314 + $txt_content = aiomatic_get_web_page($ai_command_image);
23315 + if ($txt_content !== FALSE)
23316 + {
23317 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
23318 + $txt_content = array_filter($txt_content);
23319 + if(count($txt_content) > 0)
23320 + {
23321 + $txt_content = $txt_content[array_rand($txt_content)];
23322 + if(trim($txt_content) != '')
23323 + {
23324 + $ai_command_image = $txt_content;
23325 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23326 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23327 + }
23328 + }
23329 + }
23330 + }
23331 + if(empty($ai_command_image))
23332 + {
23333 + aiomatic_log_to_file('Empty API featured image seed expression provided!');
23334 + }
23335 + else
23336 + {
23337 + if(aiomatic_strlen($ai_command_image) > 2000)
23338 + {
23339 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
23340 + }
23341 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23342 + {
23343 + $api_service = 'Ideogram';
23344 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
23345 + }
23346 + if($image_size == '256x256')
23347 + {
23348 + $width = '512';
23349 + $height = '512';
23350 + }
23351 + elseif($image_size == '512x512')
23352 + {
23353 + $width = '512';
23354 + $height = '512';
23355 + }
23356 + elseif($image_size == '1024x1024')
23357 + {
23358 + $width = '1024';
23359 + $height = '1024';
23360 + }
23361 + elseif($image_size == '1792x1024')
23362 + {
23363 + $width = '1792';
23364 + $height = '1024';
23365 + }
23366 + elseif($image_size == '1024x1792')
23367 + {
23368 + $width = '1024';
23369 + $height = '1792';
23370 + }
23371 + else
23372 + {
23373 + $width = '512';
23374 + $height = '512';
23375 + }
23376 + $skip_this_copy = true;
23377 + if(!function_exists('is_plugin_active'))
23378 + {
23379 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23380 + }
23381 + 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')
23382 + {
23383 + $skip_this_copy = false;
23384 + }
23385 + $ierror = '';
23386 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating Ideogram AI image'));
23387 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredIdeogramImage', $skip_this_copy, $ierror);
23388 + if($get_img !== false)
23389 + {
23390 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23391 + {
23392 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
23393 + }
23394 + }
23395 + else
23396 + {
23397 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
23398 + $get_img = '';
23399 + }
23400 + }
23401 + }
23402 + else
23403 + {
23404 + aiomatic_log_to_file('Empty AI featured image query entered.');
23405 + }
23406 + }
23407 + elseif($enable_ai_images == '3')
23408 + {
23409 + if ($image_url != '')
23410 + {
23411 + $replacement = str_replace(array('[', ']'), '', $query_words);
23412 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
23413 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
23414 + if(isset($matches[2]))
23415 + {
23416 + $chance = trim($matches[2], '[]');
23417 + }
23418 + else
23419 + {
23420 + $chance = '';
23421 + }
23422 + $arv = array();
23423 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
23424 + return $my_img;
23425 + }, $image_url_temp);
23426 + $img_rulx = $spintax->Parse(trim($image_url_temp));
23427 + $selected_img = aiomatic_select_ai_image($post_title, $img_rulx);
23428 + if($selected_img === false)
23429 + {
23430 + $img_rulx = explode(',', $img_rulx);
23431 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
23432 + if($img_rulx != '')
23433 + {
23434 + $get_img = $img_rulx;
23435 + }
23436 + }
23437 + else
23438 + {
23439 + $get_img = $selected_img;
23440 + }
23441 + }
23442 + }
23443 + else
23444 + {
23445 + $image_query_set = false;
23446 + $query_words = '';
23447 + $ai_command_image = '';
23448 + if($orig_ai_command_image == '')
23449 + {
23450 + $orig_ai_command_image = $image_query;
23451 + }
23452 + if($orig_ai_command_image != '')
23453 + {
23454 + $ai_command_image = $orig_ai_command_image;
23455 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
23456 + {
23457 + $ai_command_image = preg_split('/\r\n|\r|\n/', $ai_command_image);
23458 + $ai_command_image = array_filter($ai_command_image);
23459 + if(count($ai_command_image) > 0)
23460 + {
23461 + $ai_command_image = $ai_command_image[array_rand($ai_command_image)];
23462 + }
23463 + else
23464 + {
23465 + $ai_command_image = '';
23466 + }
23467 + }
23468 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23469 + if(!empty($ai_command_image))
23470 + {
23471 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23472 + }
23473 + else
23474 + {
23475 + $ai_command_image = trim(strip_tags($post_title));
23476 + }
23477 + $ai_command_image = trim($ai_command_image);
23478 + if (filter_var($ai_command_image, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($ai_command_image, '.txt'))
23479 + {
23480 + $txt_content = aiomatic_get_web_page($ai_command_image);
23481 + if ($txt_content !== FALSE)
23482 + {
23483 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
23484 + $txt_content = array_filter($txt_content);
23485 + if(count($txt_content) > 0)
23486 + {
23487 + $txt_content = $txt_content[array_rand($txt_content)];
23488 + if(trim($txt_content) != '')
23489 + {
23490 + $ai_command_image = $txt_content;
23491 + $ai_command_image = aiomatic_replaceSynergyShortcodes($ai_command_image);
23492 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23493 + }
23494 + }
23495 + }
23496 + }
23497 + }
23498 + if($ai_command_image != '')
23499 + {
23500 + $query_words = $ai_command_image;
23501 + $image_query = $ai_command_image;
23502 + $image_query_set = true;
23503 + }
23504 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
23505 + {
23506 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
23507 + {
23508 + try
23509 + {
23510 + if(!class_exists('TextRazor'))
23511 + {
23512 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
23513 + }
23514 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
23515 + $textrazor = new TextRazor();
23516 + $textrazor->addExtractor('entities');
23517 + $response = $textrazor->analyze($image_query);
23518 + if (isset($response['response']['entities']))
23519 + {
23520 + foreach ($response['response']['entities'] as $entity)
23521 + {
23522 + $query_words = '';
23523 + if(isset($entity['entityEnglishId']))
23524 + {
23525 + $query_words = $entity['entityEnglishId'];
23526 + }
23527 + else
23528 + {
23529 + $query_words = $entity['entityId'];
23530 + }
23531 + if($query_words != '')
23532 + {
23533 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
23534 + if(!empty($attached_id))
23535 + {
23536 + $additional_attachments[] = $attached_id;
23537 + }
23538 + if(!empty($z_img))
23539 + {
23540 + $get_img = $z_img;
23541 + $go_local_image = false;
23542 + if(!function_exists('is_plugin_active'))
23543 + {
23544 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23545 + }
23546 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
23547 + {
23548 + $go_local_image = true;
23549 + }
23550 + if($go_local_image == true)
23551 + {
23552 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
23553 + if(!empty($attached_id))
23554 + {
23555 + $additional_attachments[] = $attached_id;
23556 + }
23557 + if($localpath !== false)
23558 + {
23559 + $get_img = $localpath[0];
23560 + }
23561 + }
23562 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23563 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
23564 + }
23565 + break;
23566 + }
23567 + }
23568 + }
23569 + }
23570 + }
23571 + catch(Exception $e)
23572 + {
23573 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23574 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
23575 + }
23576 + }
23577 + }
23578 + }
23579 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
23580 + {
23581 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
23582 + {
23583 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
23584 + {
23585 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
23586 + }
23587 + else
23588 + {
23589 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
23590 + }
23591 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
23592 + {
23593 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
23594 + }
23595 + else
23596 + {
23597 + $keyword_assistant_id = '';
23598 + }
23599 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
23600 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
23601 + $title_ai_command = str_replace('%%original_post_title%%', $post_title, $title_ai_command);
23602 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
23603 + {
23604 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
23605 + $title_ai_command = array_filter($title_ai_command);
23606 + if(count($title_ai_command) > 0)
23607 + {
23608 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
23609 + }
23610 + else
23611 + {
23612 + $title_ai_command = '';
23613 + }
23614 + }
23615 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
23616 + if(!empty($title_ai_command))
23617 + {
23618 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23619 + }
23620 + $title_ai_command = trim($title_ai_command);
23621 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
23622 + {
23623 + $txt_content = aiomatic_get_web_page($title_ai_command);
23624 + if ($txt_content !== FALSE)
23625 + {
23626 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
23627 + $txt_content = array_filter($txt_content);
23628 + if(count($txt_content) > 0)
23629 + {
23630 + $txt_content = $txt_content[array_rand($txt_content)];
23631 + if(trim($txt_content) != '')
23632 + {
23633 + $title_ai_command = $txt_content;
23634 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
23635 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
23636 + }
23637 + }
23638 + }
23639 + }
23640 + if(empty($title_ai_command))
23641 + {
23642 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
23643 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
23644 + }
23645 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
23646 + {
23647 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
23648 + }
23649 + $title_ai_command = trim($title_ai_command);
23650 + if(empty($title_ai_command))
23651 + {
23652 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
23653 + }
23654 + else
23655 + {
23656 + $query_token_count = count(aiomatic_encode($title_ai_command));
23657 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
23658 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
23659 + {
23660 + $string_len = aiomatic_strlen($title_ai_command);
23661 + $string_len = $string_len / 2;
23662 + $string_len = intval(0 - $string_len);
23663 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
23664 + $title_ai_command = trim($title_ai_command);
23665 + $query_token_count = count(aiomatic_encode($title_ai_command));
23666 + $available_tokens = $max_tokens - $query_token_count;
23667 + }
23668 + $aierror = '';
23669 + $finish_reason = '';
23670 + if(!empty($global_prepend))
23671 + {
23672 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
23673 + }
23674 + if(!empty($global_append))
23675 + {
23676 + $title_ai_command = $title_ai_command . ' ' . $global_append;
23677 + }
23678 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23679 + {
23680 + $api_service = aiomatic_get_api_service($token, $kw_model);
23681 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
23682 + }
23683 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating image search keywords'));
23684 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
23685 + if($generated_text === false)
23686 + {
23687 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
23688 + $ai_title = '';
23689 + }
23690 + else
23691 + {
23692 + $ai_title = trim(trim($generated_text), '.');
23693 + $ai_titles = explode(',', $ai_title);
23694 + foreach($ai_titles as $query_words)
23695 + {
23696 + $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);
23697 + if(!empty($attached_id))
23698 + {
23699 + $additional_attachments[] = $attached_id;
23700 + }
23701 + if(!empty($z_img))
23702 + {
23703 + $get_img = $z_img;
23704 + $go_local_image = false;
23705 + if(!function_exists('is_plugin_active'))
23706 + {
23707 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23708 + }
23709 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
23710 + {
23711 + $go_local_image = true;
23712 + }
23713 + if($go_local_image == true)
23714 + {
23715 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
23716 + if(!empty($attached_id))
23717 + {
23718 + $additional_attachments[] = $attached_id;
23719 + }
23720 + if($localpath !== false)
23721 + {
23722 + $get_img = $localpath[0];
23723 + }
23724 + }
23725 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23726 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
23727 + }
23728 + break;
23729 + }
23730 + }
23731 + }
23732 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
23733 + {
23734 + $api_service = aiomatic_get_api_service($token, $kw_model);
23735 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
23736 + }
23737 + }
23738 + }
23739 + }
23740 + if(empty($get_img))
23741 + {
23742 + if($image_query_set == true && !empty($image_query))
23743 + {
23744 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating royalty free image'));
23745 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
23746 + if(!empty($attached_id))
23747 + {
23748 + $additional_attachments[] = $attached_id;
23749 + }
23750 + if($get_img == '' || $get_img === false)
23751 + {
23752 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
23753 + {
23754 + $keyword_class = new Aiomatic_keywords();
23755 + $image_query = $keyword_class->keywords($image_query, 1);
23756 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
23757 + if(!empty($attached_id))
23758 + {
23759 + $additional_attachments[] = $attached_id;
23760 + }
23761 + }
23762 + }
23763 + }
23764 + if(empty($get_img))
23765 + {
23766 + $keyword_class = new Aiomatic_keywords();
23767 + $query_words = $keyword_class->keywords($post_title, 2);
23768 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
23769 + if(!empty($attached_id))
23770 + {
23771 + $additional_attachments[] = $attached_id;
23772 + }
23773 + if($get_img == '' || $get_img === false)
23774 + {
23775 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
23776 + {
23777 + $query_words = $keyword_class->keywords($post_title, 1);
23778 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
23779 + if(!empty($attached_id))
23780 + {
23781 + $additional_attachments[] = $attached_id;
23782 + }
23783 + if($get_img == '' || $get_img === false)
23784 + {
23785 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
23786 + {
23787 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23788 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
23789 + }
23790 + unset($post_title_lines[$current_index]);
23791 + continue;
23792 + }
23793 + }
23794 + }
23795 + else
23796 + {
23797 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
23798 + {
23799 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23800 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
23801 + }
23802 + unset($post_title_lines[$current_index]);
23803 + continue;
23804 + }
23805 + }
23806 + }
23807 + }
23808 + if(!empty($get_img))
23809 + {
23810 + $go_local_image = false;
23811 + if(!function_exists('is_plugin_active'))
23812 + {
23813 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
23814 + }
23815 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
23816 + {
23817 + $go_local_image = true;
23818 + }
23819 + if($go_local_image == true)
23820 + {
23821 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
23822 + if(!empty($attached_id))
23823 + {
23824 + $additional_attachments[] = $attached_id;
23825 + }
23826 + if($localpath !== false)
23827 + {
23828 + $get_img = $localpath[0];
23829 + }
23830 + }
23831 + }
23832 + }
23833 + }
23834 + }
23835 + elseif($royalty_free == '2')
23836 + {
23837 + $get_img = $amazon_thumb;
23838 + }
23839 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
23840 + {
23841 + $already_spinned = '1';
23842 + }
23843 + $my_post = array();
23844 + $my_post['aiomatic_post_image'] = $get_img;
23845 + if($enable_ai_images == '2')
23846 + {
23847 + $my_post['aiomatic_local_image'] = '1';
23848 + }
23849 + else
23850 + {
23851 + $my_post['aiomatic_local_image'] = '0';
23852 + }
23853 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
23854 + $my_post['default_category'] = $default_category;
23855 + $my_post['post_type'] = $post_type;
23856 + $my_post['comment_status'] = $accept_comments;
23857 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
23858 + {
23859 + if($post_status == 'publish')
23860 + {
23861 + $draft_me = true;
23862 + $my_post['post_status'] = 'draft';
23863 + }
23864 + else
23865 + {
23866 + $my_post['post_status'] = $post_status;
23867 + }
23868 + }
23869 + else
23870 + {
23871 + $my_post['post_status'] = $post_status;
23872 + }
23873 + if($user_name_type == 'rand')
23874 + {
23875 + $randid = aiomatic_display_random_user();
23876 + if($randid === false)
23877 + {
23878 + $my_post['post_author'] = aiomatic_randomName();
23879 + }
23880 + else
23881 + {
23882 + $my_post['post_author'] = $randid->ID;
23883 + }
23884 + }
23885 + else
23886 + {
23887 + $my_post['post_author'] = $post_user_name;
23888 + }
23889 + if($strip_title == '1')
23890 + {
23891 + $new_post_content = str_replace($post_title, '', $new_post_content);
23892 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
23893 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
23894 + }
23895 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
23896 + {
23897 + require_once(dirname(__FILE__) . "/res/swear.php");
23898 + $new_post_content = aimogenpro_filterwords($new_post_content);
23899 + }
23900 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
23901 + $continue = false;
23902 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
23903 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
23904 + foreach ($banned_list as $banned_word) {
23905 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
23906 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23907 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
23908 + }
23909 + $continue = true;
23910 + break;
23911 + }
23912 + if (stripos($post_title, trim($banned_word)) !== FALSE) {
23913 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23914 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
23915 + }
23916 + $continue = true;
23917 + break;
23918 + }
23919 + }
23920 + if ($continue === true) {
23921 + continue;
23922 + }
23923 + }
23924 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
23925 + {
23926 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
23927 + {
23928 + $continue = true;
23929 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
23930 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
23931 + foreach ($required_list as $required_word) {
23932 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($post_title, trim($required_word)) !== FALSE) {
23933 + $continue = false;
23934 + break;
23935 + }
23936 + }
23937 + if ($continue === true) {
23938 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23939 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
23940 + }
23941 + continue;
23942 + }
23943 + }
23944 + else
23945 + {
23946 + $continue = false;
23947 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
23948 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
23949 + foreach ($required_list as $required_word) {
23950 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($post_title, trim($required_word)) === FALSE) {
23951 + $continue = true;
23952 + break;
23953 + }
23954 + }
23955 + if ($continue === true) {
23956 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
23957 + aiomatic_log_to_file('Skipping post "' . esc_html($post_title) . '", because it\'s content doesn\'t contain global required words.');
23958 + }
23959 + continue;
23960 + }
23961 + }
23962 + }
23963 + $new_post_title = $post_title;
23964 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
23965 + {
23966 + $skip_spin = '1';
23967 + }
23968 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
23969 + if($arr[0] != $new_post_title)
23970 + {
23971 + $new_post_title = $arr[0];
23972 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
23973 + {
23974 + $posts = get_posts(
23975 + array(
23976 + 'post_type' => $post_type,
23977 + 'title' => html_entity_decode($new_post_title),
23978 + 'post_status' => 'all',
23979 + 'numberposts' => 1,
23980 + 'update_post_term_cache' => false,
23981 + 'update_post_meta_cache' => false,
23982 + 'orderby' => 'post_date ID',
23983 + 'order' => 'ASC',
23984 + )
23985 + );
23986 + if ( ! empty( $posts ) ) {
23987 + $zap = $posts[0];
23988 + } else {
23989 + $zap = null;
23990 + }
23991 + if($zap !== null)
23992 + {
23993 + if($overwrite_existing)
23994 + {
23995 + $update_post_id = $zap->ID;
23996 + }
23997 + else
23998 + {
23999 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $new_post_title);
24000 + unset($post_title_lines[$current_index]);
24001 + continue;
24002 + }
24003 + }
24004 + }
24005 + }
24006 + $new_post_content = $arr[1];
24007 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
24008 + {
24009 + $already_spinned = '1';
24010 + }
24011 + if ($auto_categories == 'content') {
24012 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
24013 + $extra_categories = implode(',', $extra_categories);
24014 + }
24015 + elseif ($auto_categories == 'title') {
24016 + $extra_categories = aiomatic_extractKeyWords($new_post_title);
24017 + $extra_categories = implode(',', $extra_categories);
24018 + }
24019 + elseif ($auto_categories == 'both') {
24020 + $extra_categories = aiomatic_extractKeyWords($new_post_content);
24021 + $extra_categories = implode(',', $extra_categories);
24022 + $extra_categories2 = aiomatic_extractKeyWords($new_post_title);
24023 + $extra_categories2 = implode(',', $extra_categories2);
24024 + if($extra_categories2 != '')
24025 + {
24026 + $extra_categories .= ',' . $extra_categories2;
24027 + }
24028 + }
24029 + elseif ($auto_categories == 'ai')
24030 + {
24031 + $category_ai_command = $orig_ai_command_category;
24032 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
24033 + {
24034 + $category_ai_command = preg_split('/\r\n|\r|\n/', $category_ai_command);
24035 + $category_ai_command = array_filter($category_ai_command);
24036 + if(count($category_ai_command) > 0)
24037 + {
24038 + $category_ai_command = $category_ai_command[array_rand($category_ai_command)];
24039 + }
24040 + else
24041 + {
24042 + $category_ai_command = '';
24043 + }
24044 + }
24045 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
24046 + if(!empty($category_ai_command))
24047 + {
24048 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24049 + $category_ai_command = aiomatic_replace_review($category_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24050 + }
24051 + else
24052 + {
24053 + $category_ai_command = trim(strip_tags('Write a comma separated list of categories, for the post title: %%post_title%%'));
24054 + }
24055 + $category_ai_command = trim($category_ai_command);
24056 + if (filter_var($category_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($category_ai_command, '.txt'))
24057 + {
24058 + $txt_content = aiomatic_get_web_page($category_ai_command);
24059 + if ($txt_content !== FALSE)
24060 + {
24061 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
24062 + $txt_content = array_filter($txt_content);
24063 + if(count($txt_content) > 0)
24064 + {
24065 + $txt_content = $txt_content[array_rand($txt_content)];
24066 + if(trim($txt_content) != '')
24067 + {
24068 + $category_ai_command = $txt_content;
24069 + $category_ai_command = aiomatic_replaceSynergyShortcodes($category_ai_command);
24070 + $category_ai_command = aiomatic_replaceAIPostShortcodes($category_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24071 + $category_ai_command = aiomatic_replace_review($category_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24072 + }
24073 + }
24074 + }
24075 + }
24076 + if(empty($category_ai_command))
24077 + {
24078 + aiomatic_log_to_file('Empty API post category seed expression provided!');
24079 + }
24080 + else
24081 + {
24082 + if(aiomatic_strlen($category_ai_command) > $max_seed_tokens * 4)
24083 + {
24084 + $category_ai_command = aiomatic_substr($category_ai_command, 0, (0 - ($max_seed_tokens * 4)));
24085 + }
24086 + $category_ai_command = trim($category_ai_command);
24087 + if(empty($category_ai_command))
24088 + {
24089 + aiomatic_log_to_file('Empty API category seed expression provided! ' . print_r($category_ai_command, true));
24090 + break;
24091 + }
24092 + $query_token_count = count(aiomatic_encode($category_ai_command));
24093 + $available_tokens = aiomatic_compute_available_tokens($category_model, $max_tokens, $category_ai_command, $query_token_count);
24094 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
24095 + {
24096 + $string_len = aiomatic_strlen($category_ai_command);
24097 + $string_len = $string_len / 2;
24098 + $string_len = intval(0 - $string_len);
24099 + $category_ai_command = aiomatic_substr($category_ai_command, 0, $string_len);
24100 + $category_ai_command = trim($category_ai_command);
24101 + if(empty($category_ai_command))
24102 + {
24103 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($category_ai_command, true));
24104 + break;
24105 + }
24106 + $query_token_count = count(aiomatic_encode($category_ai_command));
24107 + $available_tokens = $max_tokens - $query_token_count;
24108 + }
24109 + $aierror = '';
24110 + $finish_reason = '';
24111 + if(!empty($global_prepend))
24112 + {
24113 + $category_ai_command = $global_prepend . ' ' . $category_ai_command;
24114 + }
24115 + if(!empty($global_append))
24116 + {
24117 + $category_ai_command = $category_ai_command . ' ' . $global_append;
24118 + }
24119 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
24120 + {
24121 + $api_service = aiomatic_get_api_service($token, $category_model);
24122 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $category_model . ')(' . $available_tokens . ') for category generator: ' . $category_ai_command);
24123 + }
24124 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating categories'));
24125 + $generated_text = aiomatic_generate_text($token, $category_model, $category_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'categoryID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
24126 + if($generated_text === false)
24127 + {
24128 + aiomatic_log_to_file('Category generator error: ' . $aierror);
24129 + break;
24130 + }
24131 + else
24132 + {
24133 + $extra_categories = $generated_text;
24134 + }
24135 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
24136 + {
24137 + $api_service = aiomatic_get_api_service($token, $category_model);
24138 + aiomatic_log_to_file('Successfully got API category result from ' . $api_service . ': ' . $generated_text);
24139 + }
24140 + }
24141 + }
24142 + else
24143 + {
24144 + $extra_categories = '';
24145 + }
24146 + $my_post['extra_categories'] = $extra_categories;
24147 +
24148 + $item_tags = aiomatic_extractKeyWords($new_post_content, 3);
24149 + $item_tags = implode(',', $item_tags);
24150 + $title_tags = aiomatic_extractKeyWords($new_post_title, 3);
24151 + $title_tags = implode(',', $title_tags);
24152 + $item_create_tag_sp = $spintax->Parse($item_create_tag);
24153 + if ($can_create_tag == 'content') {
24154 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $item_tags;
24155 + $my_post['extra_tags'] = $item_tags;
24156 + } else if ($can_create_tag == 'title') {
24157 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $title_tags;
24158 + $my_post['extra_tags'] = $title_tags;
24159 + } else if ($can_create_tag == 'both') {
24160 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
24161 + $my_post['extra_tags'] = ($item_tags != '' ? $item_tags . ',' : '') . $title_tags;
24162 + } else if ($can_create_tag == 'ai') {
24163 + $ai_tags = '';
24164 + $tag_ai_command = $orig_ai_command_tag;
24165 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
24166 + {
24167 + $tag_ai_command = preg_split('/\r\n|\r|\n/', $tag_ai_command);
24168 + $tag_ai_command = array_filter($tag_ai_command);
24169 + if(count($tag_ai_command) > 0)
24170 + {
24171 + $tag_ai_command = $tag_ai_command[array_rand($tag_ai_command)];
24172 + }
24173 + else
24174 + {
24175 + $tag_ai_command = '';
24176 + }
24177 + }
24178 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
24179 + if(!empty($tag_ai_command))
24180 + {
24181 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24182 + $tag_ai_command = aiomatic_replace_review($tag_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24183 + }
24184 + else
24185 + {
24186 + $tag_ai_command = trim(strip_tags('Write a comma separated list of tags, for the post title: %%post_title%%'));
24187 + }
24188 + $tag_ai_command = trim($tag_ai_command);
24189 + if (filter_var($tag_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($tag_ai_command, '.txt'))
24190 + {
24191 + $txt_content = aiomatic_get_web_page($tag_ai_command);
24192 + if ($txt_content !== FALSE)
24193 + {
24194 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
24195 + $txt_content = array_filter($txt_content);
24196 + if(count($txt_content) > 0)
24197 + {
24198 + $txt_content = $txt_content[array_rand($txt_content)];
24199 + if(trim($txt_content) != '')
24200 + {
24201 + $tag_ai_command = $txt_content;
24202 + $tag_ai_command = aiomatic_replaceSynergyShortcodes($tag_ai_command);
24203 + $tag_ai_command = aiomatic_replaceAIPostShortcodes($tag_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24204 + $tag_ai_command = aiomatic_replace_review($tag_ai_command, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24205 + }
24206 + }
24207 + }
24208 + }
24209 + if(empty($tag_ai_command))
24210 + {
24211 + aiomatic_log_to_file('Empty API post tag seed expression provided!');
24212 + }
24213 + else
24214 + {
24215 + if(aiomatic_strlen($tag_ai_command) > $max_seed_tokens * 4)
24216 + {
24217 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, (0 - ($max_seed_tokens * 4)));
24218 + }
24219 + $tag_ai_command = trim($tag_ai_command);
24220 + if(empty($tag_ai_command))
24221 + {
24222 + aiomatic_log_to_file('Empty API tag seed expression provided! ' . print_r($tag_ai_command, true));
24223 + break;
24224 + }
24225 + $query_token_count = count(aiomatic_encode($tag_ai_command));
24226 + $available_tokens = aiomatic_compute_available_tokens($tag_model, $max_tokens, $tag_ai_command, $query_token_count);
24227 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
24228 + {
24229 + $string_len = aiomatic_strlen($tag_ai_command);
24230 + $string_len = $string_len / 2;
24231 + $string_len = intval(0 - $string_len);
24232 + $tag_ai_command = aiomatic_substr($tag_ai_command, 0, $string_len);
24233 + $tag_ai_command = trim($tag_ai_command);
24234 + if(empty($tag_ai_command))
24235 + {
24236 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($tag_ai_command, true));
24237 + break;
24238 + }
24239 + $query_token_count = count(aiomatic_encode($tag_ai_command));
24240 + $available_tokens = $max_tokens - $query_token_count;
24241 + }
24242 + $aierror = '';
24243 + $finish_reason = '';
24244 + if(!empty($global_prepend))
24245 + {
24246 + $tag_ai_command = $global_prepend . ' ' . $tag_ai_command;
24247 + }
24248 + if(!empty($global_append))
24249 + {
24250 + $tag_ai_command = $tag_ai_command . ' ' . $global_append;
24251 + }
24252 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
24253 + {
24254 + $api_service = aiomatic_get_api_service($token, $tag_model);
24255 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $tag_model . ')(' . $available_tokens . ') for tag generator: ' . $tag_ai_command);
24256 + }
24257 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating tags'));
24258 + $generated_text = aiomatic_generate_text($token, $tag_model, $tag_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'tagID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
24259 + if($generated_text === false)
24260 + {
24261 + aiomatic_log_to_file('Tag generator error: ' . $aierror);
24262 + break;
24263 + }
24264 + else
24265 + {
24266 + $ai_tags = $generated_text;
24267 + }
24268 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
24269 + {
24270 + $api_service = aiomatic_get_api_service($token, $tag_model);
24271 + aiomatic_log_to_file('Successfully got API tag result from ' . $api_service . ': ' . $generated_text);
24272 + }
24273 + }
24274 + $post_the_tags = ($item_create_tag_sp != '' ? $item_create_tag_sp . ',' : '') . $ai_tags;
24275 + $my_post['extra_tags'] = $ai_tags;
24276 + } else {
24277 + $post_the_tags = $item_create_tag_sp;
24278 + $my_post['extra_tags'] = '';
24279 + }
24280 + $my_post['tags_input'] = $post_the_tags;
24281 + $new_post_content = html_entity_decode($new_post_content);
24282 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
24283 + if ($strip_by_regex !== '')
24284 + {
24285 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
24286 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
24287 + $xcnt = 0;
24288 + foreach($xstrip_by_regex as $sbr)
24289 + {
24290 + if(isset($xreplace_regex[$xcnt]))
24291 + {
24292 + $repreg = $xreplace_regex[$xcnt];
24293 + }
24294 + else
24295 + {
24296 + $repreg = '';
24297 + }
24298 + $xcnt++;
24299 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
24300 + if($temp_cont !== NULL)
24301 + {
24302 + $new_post_content = $temp_cont;
24303 + }
24304 + }
24305 + }
24306 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24307 + $post_prepender = aiomatic_replace_review($post_prepender, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24308 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24309 + $post_appender = aiomatic_replace_review($post_appender, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24310 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
24311 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
24312 + if($topic_videos == '1' && count($added_vid_list) == 0)
24313 + {
24314 + if(isset($aiomatic_Main_Settings['social_list']) && !empty($aiomatic_Main_Settings['social_list']) && is_array($aiomatic_Main_Settings['social_list']))
24315 + {
24316 + $social_list = $aiomatic_Main_Settings['social_list'];
24317 + }
24318 + else
24319 + {
24320 + $social_list = array();
24321 + }
24322 + $new_vid = aiomatic_get_video($product_title, '', $social_list);
24323 + if($new_vid !== false && !in_array($new_vid, $added_vid_list))
24324 + {
24325 + $new_post_content .= $new_vid;
24326 + $added_vid_list[] = $new_vid;
24327 + $new_vid_arr[] = $new_vid;
24328 + $added_videos++;
24329 + }
24330 + }
24331 + $zlang = 'en_US';
24332 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
24333 + $zlang = $aiomatic_Main_Settings['kw_lang'];
24334 + }
24335 + $rel_search = array('post_title', 'post_content');
24336 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
24337 + $rel_search = $aiomatic_Main_Settings['rel_search'];
24338 + }
24339 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
24340 + {
24341 + try
24342 + {
24343 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
24344 + }
24345 + catch(Exception $ex)
24346 + {
24347 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24348 + aiomatic_log_to_file('Failed to add new inbound links to content: ' . $ex->getMessage());
24349 + }
24350 + }
24351 + }
24352 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
24353 + {
24354 + if($new_post_content === strip_tags($new_post_content))
24355 + {
24356 + $new_post_content = nl2br($new_post_content);
24357 + }
24358 + }
24359 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
24360 + {
24361 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
24362 + {
24363 + if(!isset($xchars))
24364 + {
24365 + $xchars = array();
24366 + }
24367 + $rand_percentage = wp_rand(10, 20);
24368 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
24369 + }
24370 + }
24371 + if($ret_content == 1)
24372 + {
24373 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24374 + aiomatic_log_to_file('Returning AI generated content, finished.');
24375 + }
24376 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
24377 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
24378 + }
24379 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
24380 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
24381 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
24382 + {
24383 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
24384 + }
24385 + else
24386 + {
24387 + $my_post['post_content'] = $trunchi_content;
24388 + }
24389 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
24390 + {
24391 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
24392 + {
24393 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
24394 + }
24395 + }
24396 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
24397 + $my_post['aiomatic_source_title'] = $amazon_kw;
24398 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
24399 + $my_post['aiomatic_post_format'] = $post_format;
24400 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')
24401 + {
24402 + if(is_array($default_category))
24403 + {
24404 + $cextra = '';
24405 + foreach($default_category as $dc)
24406 + {
24407 + $cextra .= ',' . get_cat_name($dc);
24408 + }
24409 + $extra_categories_temp = trim( $cextra . ',' . $extra_categories, ',');
24410 + }
24411 + else
24412 + {
24413 + $extra_categories_temp = trim(get_cat_name($default_category) . ',' .$extra_categories, ',');
24414 + }
24415 + }
24416 + else
24417 + {
24418 + $extra_categories_temp = $extra_categories;
24419 + }
24420 + $block_arr = array();
24421 + $custom_arr = array();
24422 + if($custom_fields != '')
24423 + {
24424 + if(stristr($custom_fields, '=>') != false)
24425 + {
24426 + $rule_arr = explode(',', trim($custom_fields));
24427 + foreach($rule_arr as $rule)
24428 + {
24429 + $my_args = explode('=>', trim($rule));
24430 + if(isset($my_args[1]))
24431 + {
24432 + if ( isset( $shortcode_tags['aicontent'] ) ) {
24433 + $aicontent_handler = $shortcode_tags['aicontent'];
24434 + unset( $shortcode_tags['aicontent'] );
24435 + } else {
24436 + $aicontent_handler = null;
24437 + }
24438 + $my_args[1] = do_shortcode($my_args[1]);
24439 + $my_args[0] = do_shortcode($my_args[0]);
24440 + if ( $aicontent_handler ) {
24441 + $shortcode_tags['aicontent'] = $aicontent_handler;
24442 + }
24443 + $custom_field_content = trim($my_args[1]);
24444 + $custom_field_content = aiomatic_replaceAIPostShortcodes($custom_field_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24445 + $custom_field_content = aiomatic_replace_review($custom_field_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24446 + $custom_field_content = aiomatic_replaceSynergyShortcodes($custom_field_content);
24447 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
24448 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
24449 + if(stristr($my_args[1], 'serialize_') !== false)
24450 + {
24451 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
24452 + }
24453 + else
24454 + {
24455 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
24456 + {
24457 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
24458 + if(isset($cfm[2][0]))
24459 + {
24460 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
24461 + {
24462 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
24463 + }
24464 + else
24465 + {
24466 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
24467 + }
24468 + }
24469 + else
24470 + {
24471 + $custom_arr[trim($my_args[0])] = $custom_field_content;
24472 + }
24473 + }
24474 + else
24475 + {
24476 + $custom_arr[trim($my_args[0])] = $custom_field_content;
24477 + }
24478 + }
24479 + }
24480 + }
24481 + }
24482 + }
24483 + $block_arr = array();
24484 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $extra_categories_temp, 'aiomatic_post_tags' => $post_the_tags));
24485 + $custom_tax_arr = array();
24486 + if($custom_tax != '')
24487 + {
24488 + if(stristr($custom_tax, '=>') != false)
24489 + {
24490 + $rule_arr = explode(';', trim($custom_tax));
24491 + foreach($rule_arr as $rule)
24492 + {
24493 + $my_args = explode('=>', trim($rule));
24494 + if(isset($my_args[1]))
24495 + {
24496 + $custom_tax_content = trim($my_args[1]);
24497 + $custom_tax_content = aiomatic_replaceAIPostShortcodes($custom_tax_content, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
24498 + $custom_tax_content = aiomatic_replace_review($custom_tax_content, $post_title, $amazon_kw, $product_title, $product_description, $aff_url, $product_author, $product_brand, $product_isbn, $product_upc, $offer_price, $product_price, $product_list_price, $price_numeric, $price_currency, $product_asin, $cart_url, $list_price_numeric, $product_imgs, $product_titles, $content_language, $writing_style, $writing_tone, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $point_of_view, $product_reviews, $default_cat_list, $item_score, $edition, $language, $pages_count, $publication_date, $contributors, $manufacturer, $binding, $product_group, $rating, $eans, $part_no, $model, $warranty, $color, $is_adult, $dimensions, $date, $size, $unit_count);
24499 + $custom_tax_content = aiomatic_replaceSynergyShortcodes($custom_tax_content);
24500 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
24501 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
24502 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
24503 + {
24504 + if(isset($custom_arr['_product_attributes']))
24505 + {
24506 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
24507 + 'name' => trim($my_args[0]),
24508 + 'value' => $custom_tax_content,
24509 + 'is_visible' => '1',
24510 + 'is_taxonomy' => '1'
24511 + )));
24512 + }
24513 + else
24514 + {
24515 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
24516 + 'name' => trim($my_args[0]),
24517 + 'value' => $custom_tax_content,
24518 + 'is_visible' => '1',
24519 + 'is_taxonomy' => '1'
24520 + ));
24521 + }
24522 + }
24523 + if(isset($custom_tax_arr[trim($my_args[0])]))
24524 + {
24525 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
24526 + }
24527 + else
24528 + {
24529 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
24530 + }
24531 + }
24532 + }
24533 + }
24534 + }
24535 + if(count($custom_tax_arr) > 0)
24536 + {
24537 + $my_post['taxo_input'] = $custom_tax_arr;
24538 + }
24539 + if (isset($aiomatic_Main_Settings['external_products']) && $aiomatic_Main_Settings['external_products'] == 'on')
24540 + {
24541 + if($post_type == 'product')
24542 + {
24543 + $custom_arr['_price'] = $product_price;
24544 + $custom_arr['_regular_price'] = $product_price;
24545 + $custom_arr['_product_url'] = $aff_url;
24546 + }
24547 + }
24548 + $my_post['meta_input'] = $custom_arr;
24549 + if($parent_id != '')
24550 + {
24551 + $my_post['post_parent'] = intval($parent_id);
24552 + }
24553 + if ($enable_pingback == '1') {
24554 + $my_post['ping_status'] = 'open';
24555 + } else {
24556 + $my_post['ping_status'] = 'closed';
24557 + }
24558 + if($min_time != '' && $max_time != '')
24559 + {
24560 + $t1 = strtotime($min_time);
24561 + $t2 = strtotime($max_time);
24562 + if($t1 != false && $t2 != false)
24563 + {
24564 + $int = wp_rand($t1, $t2);
24565 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
24566 + }
24567 + }
24568 + elseif($min_time != '')
24569 + {
24570 + $t1 = strtotime($min_time);
24571 + if($t1 != false)
24572 + {
24573 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
24574 + }
24575 + }
24576 + elseif($max_time != '')
24577 + {
24578 + $t1 = strtotime($max_time);
24579 + if($t1 != false)
24580 + {
24581 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
24582 + }
24583 + }
24584 + if($new_post_excerpt != '')
24585 + {
24586 + $my_post['post_excerpt'] = $new_post_excerpt;
24587 + }
24588 + $count++;
24589 + if($update_post_id != '')
24590 + {
24591 + $my_post['ID'] = $update_post_id;
24592 + }
24593 + remove_filter('content_save_pre', 'wp_filter_post_kses');
24594 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
24595 + try
24596 + {
24597 + $post_id = wp_insert_post($my_post, true);
24598 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
24599 + if (is_wp_error($post_id))
24600 + {
24601 + aiomatic_log_to_file('Trying publishing again');
24602 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
24603 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
24604 + }
24605 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
24606 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
24607 + $post_id = wp_insert_post($my_post, true);
24608 + }
24609 + }
24610 + catch(Exception $e)
24611 + {
24612 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
24613 + }
24614 + add_filter('content_save_pre', 'wp_filter_post_kses');
24615 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
24616 + if (!is_wp_error($post_id)) {
24617 + if($post_id === 0)
24618 + {
24619 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
24620 + continue;
24621 + }
24622 + $posts_inserted++;
24623 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
24624 + {
24625 + pll_set_post_language($post_id, $wpml_lang);
24626 + }
24627 + if(!empty($additional_attachments))
24628 + {
24629 + foreach($additional_attachments as $add_att)
24630 + {
24631 + wp_update_post([
24632 + 'ID' => $add_att,
24633 + 'post_parent' => $post_id,
24634 + ]);
24635 + }
24636 + }
24637 + $default_categories = array();
24638 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
24639 + {
24640 + $default_categories = wp_get_post_categories($post_id);
24641 + }
24642 + if(isset($my_post['taxo_input']))
24643 + {
24644 + foreach($my_post['taxo_input'] as $taxn => $taxval)
24645 + {
24646 + $taxn = trim($taxn);
24647 + $taxval = trim($taxval);
24648 + if(is_taxonomy_hierarchical($taxn))
24649 + {
24650 + $taxval = array_map('trim', explode(',', $taxval));
24651 + for($ii = 0; $ii < count($taxval); $ii++)
24652 + {
24653 + if(!is_numeric($taxval[$ii]))
24654 + {
24655 + $term_ids = [];
24656 + $parent_id = 0;
24657 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
24658 + foreach($hierarchy_parts as $hp)
24659 + {
24660 + if(!is_numeric($hp))
24661 + {
24662 + $xtermid = get_term_by('name', $hp, $taxn);
24663 + if($xtermid !== false)
24664 + {
24665 + $parent_id = $xtermid->term_id;
24666 + if(!is_numeric($taxval[$ii]))
24667 + {
24668 + $taxval[$ii] = intval($xtermid->term_id);
24669 + }
24670 + else
24671 + {
24672 + $taxval[] = intval($xtermid->term_id);
24673 + }
24674 + }
24675 + else
24676 + {
24677 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
24678 + $xtermid = get_term_by('name', $hp, $taxn);
24679 + if($xtermid !== false)
24680 + {
24681 + $parent_id = $xtermid->term_id;
24682 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
24683 + {
24684 + pll_set_term_language($xtermid->term_id, $wpml_lang);
24685 + }
24686 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
24687 + {
24688 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
24689 + $pars['element_id'] = $xtermid->term_id;
24690 + $pars['element_type'] = $wpml_element_type;
24691 + $pars['language_code'] = $wpml_lang;
24692 + $pars['trid'] = FALSE;
24693 + $pars['source_language_code'] = NULL;
24694 + do_action('wpml_set_element_language_details', $pars);
24695 + }
24696 + if(!is_numeric($taxval[$ii]))
24697 + {
24698 + $taxval[$ii] = intval($xtermid->term_id);
24699 + }
24700 + else
24701 + {
24702 + $taxval[] = intval($xtermid->term_id);
24703 + }
24704 + }
24705 + }
24706 + }
24707 + else
24708 + {
24709 + if(!is_numeric($taxval[$ii]))
24710 + {
24711 + $taxval[$ii] = intval($hp);
24712 + }
24713 + else
24714 + {
24715 + $taxval[] = intval($hp);
24716 + }
24717 + }
24718 + }
24719 + }
24720 + }
24721 + wp_set_post_terms($post_id, $taxval, $taxn, true);
24722 + }
24723 + else
24724 + {
24725 + $jterms = array_map('trim', explode(',', $taxval));
24726 + wp_set_post_terms($post_id, $jterms, $taxn, true);
24727 + }
24728 + }
24729 + }
24730 + if (isset($aiomatic_Main_Settings['external_products']) && $aiomatic_Main_Settings['external_products'] == 'on')
24731 + {
24732 + if($post_type == 'product')
24733 + {
24734 + wp_set_post_terms($post_id, 'external', 'product_type', false);
24735 + }
24736 + }
24737 + if (isset($my_post['aiomatic_post_format']) && $my_post['aiomatic_post_format'] != '' && $my_post['aiomatic_post_format'] != 'post-format-standard') {
24738 + wp_set_post_terms($post_id, $my_post['aiomatic_post_format'], 'post_format', true);
24739 + }
24740 + $featured_path = '';
24741 + $get_img = $my_post['aiomatic_post_image'];
24742 + if ($get_img != '') {
24743 + if($my_post['aiomatic_local_image'] == '1')
24744 + {
24745 + $local_get_img = $get_img[0];
24746 + if (!aiomatic_assign_featured_image_path($local_get_img, $post_id)) {
24747 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24748 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed for ' . $local_get_img);
24749 + }
24750 + } else {
24751 + $featured_path = $get_img[1];
24752 + }
24753 + }
24754 + else
24755 + {
24756 + if(is_numeric($get_img))
24757 + {
24758 + $featured_path = aiomatic_assign_featured_image($get_img, $post_id);
24759 + }
24760 + else
24761 + {
24762 + if (!aiomatic_generate_featured_image($get_img, $post_id)) {
24763 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24764 + aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $get_img);
24765 + }
24766 + } else {
24767 + $featured_path = $get_img;
24768 + }
24769 + }
24770 + }
24771 + }
24772 + if($featured_path == '')
24773 + {
24774 + if ($image_url != '') {
24775 + $replacement = str_replace(array('[', ']'), '', $my_post['post_title']);
24776 + $image_url_temp = str_replace('%%item_title%%', $replacement, $image_url);
24777 + $image_url_temp = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
24778 + if(isset($matches[2]))
24779 + {
24780 + $chance = trim($matches[2], '[]');
24781 + }
24782 + else
24783 + {
24784 + $chance = '';
24785 + }
24786 + $arv = array();
24787 + $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
24788 + return $my_img;
24789 + }, $image_url_temp);
24790 + $img_rulx = $spintax->Parse(trim($image_url_temp));
24791 + $selected_img = aiomatic_select_ai_image($my_post['post_title'], $img_rulx);
24792 + if($selected_img === false)
24793 + {
24794 + $img_rulx = explode(',', $img_rulx);
24795 + $img_rulx = trim($img_rulx[array_rand($img_rulx)]);
24796 + }
24797 + else
24798 + {
24799 + $img_rulx = $selected_img;
24800 + }
24801 + if(is_numeric($img_rulx))
24802 + {
24803 + $featured_path = aiomatic_assign_featured_image($img_rulx, $post_id);
24804 + }
24805 + else
24806 + {
24807 + if($img_rulx != '')
24808 + {
24809 + if (!aiomatic_generate_featured_image($img_rulx, $post_id)) {
24810 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24811 + aiomatic_log_to_file('aiomatic_generate_featured_image failed to default value: ' . $img_rulx . '!');
24812 + }
24813 + } else {
24814 + $featured_path = $img_rulx;
24815 + }
24816 + }
24817 + }
24818 + }
24819 + }
24820 + if ($auto_categories != 'disabled') {
24821 + if ($my_post['extra_categories'] != '') {
24822 + $extra_cats = explode(',', $my_post['extra_categories']);
24823 + if($post_type == 'product')
24824 + {
24825 + $product_tax = 'product_cat';
24826 + }
24827 + else
24828 + {
24829 + $product_tax = 'category';
24830 + }
24831 + foreach($extra_cats as $extra_cat)
24832 + {
24833 + $extra_cat = trim($extra_cat);
24834 + $extra_cat = strip_tags($extra_cat);
24835 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
24836 + if(empty($extra_cat))
24837 + {
24838 + continue;
24839 + }
24840 + if ($skip_inexist == '1')
24841 + {
24842 + if(!term_exists($extra_cat, $product_tax))
24843 + {
24844 + continue;
24845 + }
24846 + }
24847 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
24848 + wp_set_post_terms($post_id, $termid, $product_tax, true);
24849 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
24850 + {
24851 + foreach($termid as $tx)
24852 + {
24853 + pll_set_term_language($tx, $wpml_lang);
24854 + }
24855 + }
24856 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
24857 + {
24858 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
24859 + foreach($termid as $tx)
24860 + {
24861 + $pars['element_id'] = $tx;
24862 + $pars['element_type'] = $wpml_element_type;
24863 + $pars['language_code'] = $wpml_lang;
24864 + $pars['trid'] = FALSE;
24865 + $pars['source_language_code'] = NULL;
24866 + do_action('wpml_set_element_language_details', $pars);
24867 + }
24868 + }
24869 + }
24870 + }
24871 + }
24872 + if (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678') {
24873 + $cats = array();
24874 + if(is_array($default_category))
24875 + {
24876 + foreach($default_category as $dc)
24877 + {
24878 + $cats[] = $dc;
24879 + }
24880 + }
24881 + else
24882 + {
24883 + $cats[] = $default_category;
24884 + }
24885 + global $sitepress;
24886 + if($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
24887 + {
24888 + $current_language = apply_filters( 'wpml_current_language', NULL );
24889 + $sitepress->switch_lang($wpml_lang);
24890 + }
24891 + $target_cat = 'category';
24892 + if($post_type == 'product')
24893 + {
24894 + $target_cat = 'product_cat';
24895 + }
24896 + wp_set_object_terms($post_id, array_map( 'intval', $cats ), $target_cat, true);
24897 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
24898 + {
24899 + foreach($cats as $cc)
24900 + {
24901 + pll_set_term_language($cc, $wpml_lang);
24902 + }
24903 + }
24904 + elseif($wpml_lang != '' && has_filter('wpml_current_language') && $sitepress != null)
24905 + {
24906 + $sitepress->switch_lang($current_language);
24907 + }
24908 + }
24909 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
24910 + {
24911 + if($post_type == 'product')
24912 + {
24913 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
24914 + }
24915 + }
24916 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
24917 + if (is_wp_error($tax_rez)) {
24918 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
24919 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
24920 + }
24921 + }
24922 + if($post_type == 'topic' && $parent_id != '')
24923 + {
24924 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
24925 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
24926 + update_post_meta($post_id, '_bbp_voice_count', '0');
24927 + update_post_meta($post_id, '_bbp_reply_count', '0');
24928 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
24929 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
24930 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
24931 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
24932 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
24933 + }
24934 + if($post_type == 'reply' && $parent_id != '')
24935 + {
24936 + if(function_exists('bbp_get_topic_forum_id'))
24937 + {
24938 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
24939 + if(empty($forum_aidi))
24940 + {
24941 + $forum_aidi = 0;
24942 + }
24943 + }
24944 + else
24945 + {
24946 + $forum_aidi = 0;
24947 + }
24948 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
24949 + }
24950 + if($remove_default == '1' && ($auto_categories != 'disabled' || (isset($default_category) && $default_category !== 'aiomatic_no_category_12345678' && $default_category[0] !== 'aiomatic_no_category_12345678')))
24951 + {
24952 + $new_categories = wp_get_post_categories($post_id);
24953 + $removed_cat = false;
24954 + if(isset($default_categories) && !($default_categories == $new_categories))
24955 + {
24956 + foreach($default_categories as $dc)
24957 + {
24958 + $rem_cat = get_category( $dc );
24959 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
24960 + $removed_cat = true;
24961 + }
24962 + }
24963 + if($removed_cat == false)
24964 + {
24965 + wp_remove_object_terms( $post_id, 'uncategorized', 'category' );
24966 + }
24967 + }
24968 + aiomatic_addPostMeta($post_id, $my_post, $param, $type, $featured_path, $post_topic, $rule_unique_id, $post_link);
24969 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
24970 + {
24971 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
24972 + $pars['element_id'] = $post_id;
24973 + $pars['element_type'] = $wpml_element_type;
24974 + $pars['language_code'] = $wpml_lang;
24975 + $pars['source_language_code'] = NULL;
24976 + do_action('wpml_set_element_language_details', $pars);
24977 +
24978 + global $wp_filesystem;
24979 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
24980 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
24981 + wp_filesystem($creds);
24982 + }
24983 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
24984 + {
24985 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
24986 + }
24987 + $wpml_lang = trim($wpml_lang);
24988 + if(function_exists('wpml_update_translatable_content'))
24989 + {
24990 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
24991 + if($my_post['post_title'] != '')
24992 + {
24993 + global $sitepress;
24994 + global $wpdb;
24995 + $keyid = md5($my_post['post_title']);
24996 + $keyName = $keyid . '_wpml';
24997 + $rezxxxa = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '$keyName' limit 1", ARRAY_A );
24998 + if(count($rezxxxa) != 0)
24999 + {
25000 + $metaRow = $rezxxxa[0];
25001 + $metaValue = $metaRow['meta_value'];
25002 + $metaParts = explode('_', $metaValue);
25003 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
25004 + }
25005 + else
25006 + {
25007 + $ptrid = $sitepress->get_element_trid($post_id);
25008 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
25009 + }
25010 + }
25011 +
25012 + }
25013 + }
25014 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
25015 + {
25016 + aiomatic_change_post_status($post_id, 'publish');
25017 + }
25018 + } else {
25019 + aiomatic_log_to_file('Failed to insert post into wp database(4)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
25020 + continue;
25021 + }
25022 + }
25023 + }
25024 + elseif($type == '4')
25025 + {
25026 + if($post_title != '')
25027 + {
25028 + $post_title_lines = preg_split('/\r\n|\r|\n/', $post_title);
25029 + }
25030 + else
25031 + {
25032 + $post_title_lines = array();
25033 + }
25034 + $raw_img_list = array();
25035 + $full_result_list = array();
25036 + $user_name = '';
25037 + $featured_image = '';
25038 + $post_cats = '';
25039 + $post_tagz = '';
25040 + $post_excerpt = '';
25041 + $final_content = '';
25042 + $postID = '';
25043 + $heading_val = '';
25044 + $image_query = '';
25045 + $temp_post = '';
25046 + $cntx = count($post_title_lines);
25047 + shuffle($post_title_lines);
25048 + $rss_feeds = array();
25049 + $csv_data = array();
25050 + $my_csv = '';
25051 + for($ji = 0; $ji < $cntx; $ji++)
25052 + {
25053 + $txt_content = aiomatic_get_web_page($post_title_lines[$ji]);
25054 + if ($txt_content === FALSE)
25055 + {
25056 + aiomatic_log_to_file('Failed to read csv file: ' . $post_title_lines[$ji]);
25057 + if($auto == 1)
25058 + {
25059 + aiomatic_log_to_file($param);
25060 + }
25061 + continue;
25062 + }
25063 + $my_csv = $post_title_lines[$ji];
25064 + try
25065 + {
25066 + if(!class_exists('\ParseCsv\Csv'))
25067 + {
25068 + require_once(dirname(__FILE__) . "/res/parsecsv/src/enums/AbstractEnum.php");
25069 + require_once(dirname(__FILE__) . "/res/parsecsv/src/enums/DatatypeEnum.php");
25070 + require_once(dirname(__FILE__) . "/res/parsecsv/src/enums/FileProcessingModeEnum.php");
25071 + require_once(dirname(__FILE__) . "/res/parsecsv/src/enums/SortEnum.php");
25072 + require_once(dirname(__FILE__) . "/res/parsecsv/src/extensions/DatatypeTrait.php");
25073 + require_once(dirname(__FILE__) . "/res/parsecsv/src/Csv.php");
25074 + }
25075 + $csv = new \ParseCsv\Csv();
25076 + if($csv_separator == '')
25077 + {
25078 + $csv->autoDirectData($txt_content);
25079 + }
25080 + else
25081 + {
25082 + $csv->delimiter = $csv_separator;
25083 + $csv->parseData($txt_content);
25084 + }
25085 + $csv_data = $csv->data;
25086 + }
25087 + catch(Exception $e)
25088 + {
25089 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25090 + aiomatic_log_to_file('Failed to load csv file: Exception thrown ' . esc_html($e->getMessage()) . '!');
25091 + }
25092 + continue;
25093 + }
25094 + break;
25095 + }
25096 + if(!is_array($csv_data) || count($csv_data) == 0)
25097 + {
25098 + if(is_array($csv_data))
25099 + {
25100 + $csv_data = print_r($csv_data, true);
25101 + }
25102 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25103 + aiomatic_log_to_file('Failed to parse csv file data ' . esc_html($csv_data));
25104 + }
25105 + return 'fail';
25106 + }
25107 +
25108 + $spintax = new Aiomatic_Spintax();
25109 + $already_spinned = 0;
25110 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
25111 + {
25112 + $img_attr = $aiomatic_Main_Settings['attr_text'];
25113 + }
25114 + else
25115 + {
25116 + $img_attr = '';
25117 + }
25118 + if($title_once == '1')
25119 + {
25120 + $skipt = 0;
25121 + foreach($csv_data as $indx => $csv)
25122 + {
25123 + $csv = array_combine(
25124 + array_map(fn($key) => trim($key, " \t\n\r\0\x0B\xEF\xBB\xBF"), array_keys($csv)),
25125 + array_values($csv)
25126 + );
25127 + if(!isset($csv['post_title']))
25128 + {
25129 + aiomatic_log_to_file('Incorrect CSV file structure, post_title heading not found, please check! ' . print_r($my_csv, true));
25130 + return 'false';
25131 + }
25132 + $csv_original_title = $csv['post_title'];
25133 + if(array_key_exists($csv_original_title, $posted_items))
25134 + {
25135 + aiomatic_log_to_file('Skipping CSV title, already processed: ' . $csv_original_title);
25136 + unset($csv_data[$indx]);
25137 + $skipt++;
25138 + }
25139 + }
25140 + if(count($csv_data) == 0 && $skipt > 0)
25141 + {
25142 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
25143 + {
25144 + if($count === 1)
25145 + {
25146 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
25147 + foreach($email_list as $thisaddr)
25148 + {
25149 + $thisaddr = trim($thisaddr);
25150 + try
25151 + {
25152 + $admin_mail = get_option('admin_email');
25153 + $to = $thisaddr;
25154 + $subject = '[Aimogen] CSV Titles depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
25155 + $message = 'CSV Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its titles, no new posts publised! Website URL: ' . get_site_url();
25156 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
25157 + $headers[] = 'Reply-To: ' . $admin_mail;
25158 + $headers[] = 'X-Mailer: PHP/' . phpversion();
25159 + $headers[] = 'Content-Type: text/html';
25160 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
25161 + wp_mail($to, $subject, $message, $headers);
25162 + }
25163 + catch (Exception $e) {
25164 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25165 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
25166 + }
25167 + }
25168 + }
25169 + }
25170 + }
25171 + aiomatic_log_to_file('All CSV titles already processed, nothing to do.');
25172 + if($auto == 1)
25173 + {
25174 + aiomatic_clearFromList($param, $type);
25175 + }
25176 + return 'nochange';
25177 + }
25178 + }
25179 + if($random_order == '1')
25180 + {
25181 + shuffle($csv_data);
25182 + }
25183 + $data_id = 1;
25184 + foreach($csv_data as $csv)
25185 + {
25186 + $csv = array_combine(
25187 + array_map(fn($key) => trim($key, " \t\n\r\0\x0B\xEF\xBB\xBF"), array_keys($csv)),
25188 + array_values($csv)
25189 + );
25190 + $thread_id = '';
25191 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing CSV data #' . $data_id));
25192 + $data_id++;
25193 + $update_post_id = '';
25194 + if ($count > intval($max))
25195 + {
25196 + break;
25197 + }
25198 + if(!isset($csv['post_title']))
25199 + {
25200 + aiomatic_log_to_file('Incorrect CSV file structure, post_title heading not found! ' . print_r($my_csv, true));
25201 + return 'false';
25202 + }
25203 + if(!isset($csv['post_content']))
25204 + {
25205 + aiomatic_log_to_file('Incorrect CSV file structure, post_content heading not found! ' . print_r($my_csv, true));
25206 + return 'false';
25207 + }
25208 + if(!isset($csv['post_excerpt']))
25209 + {
25210 + aiomatic_log_to_file('Incorrect CSV file structure, post_excerpt heading not found! ' . print_r($my_csv, true));
25211 + return 'false';
25212 + }
25213 + if(empty($csv['post_title']) && empty($csv['post_content']) && empty($csv['post_excerpt']))
25214 + {
25215 + aiomatic_log_to_file('Blank post title/content/excerpt entered, skipping: ' . print_r($my_csv, true));
25216 + continue;
25217 + }
25218 + if(!isset($csv['post_categories']))
25219 + {
25220 + aiomatic_log_to_file('Incorrect CSV file structure, post_categories heading not found! ' . print_r($my_csv, true));
25221 + return 'false';
25222 + }
25223 + if(!isset($csv['post_tags']))
25224 + {
25225 + aiomatic_log_to_file('Incorrect CSV file structure, post_tags heading not found! ' . print_r($my_csv, true));
25226 + return 'false';
25227 + }
25228 + if(!isset($csv['featured_image']))
25229 + {
25230 + aiomatic_log_to_file('Incorrect CSV file structure, featured_image heading not found! ' . print_r($my_csv, true));
25231 + return 'false';
25232 + }
25233 + if(!isset($csv['featured_image_generator']))
25234 + {
25235 + aiomatic_log_to_file('Incorrect CSV file structure, featured_image_generator heading not found! ' . print_r($my_csv, true));
25236 + return 'false';
25237 + }
25238 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25239 + {
25240 + aiomatic_log_to_file('Starting processing CSV title: ' . $csv['post_title']);
25241 + }
25242 + $csv_original_title = $csv['post_title'];
25243 + foreach($csv as $ind => $valx)
25244 + {
25245 + foreach($csv as $repl_ind => $repl_valx)
25246 + {
25247 + if($ind != $repl_ind)
25248 + {
25249 + if(strstr($csv[$ind], '%%' . $repl_ind . '%%') !== false)
25250 + {
25251 + if(strstr($repl_valx, '[aicontent') !== false)
25252 + {
25253 + $repl_valx = apply_filters('aiomatic_replace_aicontent_shortcode', $repl_valx);
25254 + $csv[$repl_ind] = $repl_valx;
25255 + }
25256 + $csv[$ind] = str_replace('%%' . $repl_ind . '%%', $repl_valx, $csv[$ind]);
25257 + }
25258 + }
25259 + }
25260 + $csv[$ind] = aiomatic_replaceSynergyShortcodes($csv[$ind]);
25261 + }
25262 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
25263 + {
25264 + if(!stristr($csv['post_title'], '[aicontent'))
25265 + {
25266 + $xposts = get_posts(
25267 + array(
25268 + 'post_type' => $post_type,
25269 + 'title' => html_entity_decode($csv['post_title']),
25270 + 'post_status' => 'all',
25271 + 'numberposts' => 1,
25272 + 'update_post_term_cache' => false,
25273 + 'update_post_meta_cache' => false,
25274 + 'orderby' => 'post_date ID',
25275 + 'order' => 'ASC',
25276 + )
25277 + );
25278 + if ( ! empty( $xposts ) ) {
25279 + $zap = $xposts[0];
25280 + } else {
25281 + $zap = null;
25282 + }
25283 + if($zap !== null)
25284 + {
25285 + if($overwrite_existing)
25286 + {
25287 + $update_post_id = $zap->ID;
25288 + }
25289 + else
25290 + {
25291 + aiomatic_log_to_file('Post with specified title is already existing, we are skipping it: ' . $csv['post_title']);
25292 + continue;
25293 + }
25294 + }
25295 + }
25296 + }
25297 + foreach($csv as $ind => $initial_value)
25298 + {
25299 + $is_ai_content = false;
25300 + if(strstr($initial_value, '[aicontent') !== false)
25301 + {
25302 + $is_ai_content = true;
25303 + }
25304 + $csv[$ind] = apply_filters('aiomatic_replace_aicontent_shortcode', $csv[$ind]);
25305 + if($is_ai_content)
25306 + {
25307 + foreach($csv as $ind2 => $initial_value_again)
25308 + {
25309 + if($ind2 !== $ind && strstr($initial_value_again, $initial_value) !== false)
25310 + {
25311 + $csv[$ind2] = str_replace($initial_value, $csv[$ind], $csv[$ind2]);
25312 + }
25313 + }
25314 + }
25315 + }
25316 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
25317 + {
25318 + $xposts = get_posts(
25319 + array(
25320 + 'post_type' => $post_type,
25321 + 'title' => html_entity_decode($csv['post_title']),
25322 + 'post_status' => 'all',
25323 + 'numberposts' => 1,
25324 + 'update_post_term_cache' => false,
25325 + 'update_post_meta_cache' => false,
25326 + 'orderby' => 'post_date ID',
25327 + 'order' => 'ASC',
25328 + )
25329 + );
25330 + if ( ! empty( $xposts ) ) {
25331 + $zap = $xposts[0];
25332 + } else {
25333 + $zap = null;
25334 + }
25335 + if($zap !== null)
25336 + {
25337 + if($overwrite_existing)
25338 + {
25339 + $update_post_id = $zap->ID;
25340 + }
25341 + else
25342 + {
25343 + aiomatic_log_to_file('Post with specified title is already existing, skipping it: ' . $csv['post_title']);
25344 + continue;
25345 + }
25346 + }
25347 + }
25348 + if($update_post_id != '')
25349 + {
25350 + $csv['ID'] = $update_post_id;
25351 + $postID = $update_post_id;
25352 + }
25353 + $get_img = '';
25354 + if($csv['featured_image_generator'] !== 'none')
25355 + {
25356 + if(stristr($csv['featured_image_generator'], 'manual') !== false || empty($csv['featured_image_generator']))
25357 + {
25358 + if(trim($csv['featured_image']) != '')
25359 + {
25360 + $get_img = trim($csv['featured_image']);
25361 + }
25362 + }
25363 + elseif(stristr($csv['featured_image_generator'], 'openai') !== false || stristr($csv['featured_image_generator'], 'aiomaticapi') !== false)
25364 + {
25365 + $query_words = trim($csv['featured_image']);
25366 + if($query_words != '')
25367 + {
25368 + $ai_command_image = $query_words;
25369 + if(aiomatic_strlen($ai_command_image) > 1000)
25370 + {
25371 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 1000);
25372 + }
25373 + $image_model = 'dalle2';
25374 + if(isset($csv['image_model']) && trim($csv['image_model']) !== '')
25375 + {
25376 + $image_model_temp = trim($csv['image_model']);
25377 + if(!in_array($image_model_temp, AIMOGEN_DALLE_IMAGE_MODELS))
25378 + {
25379 + aiomatic_log_to_file('Incorrect Dall-E image model provided ' . $image_model_temp);
25380 + }
25381 + else
25382 + {
25383 + $image_model = $image_model_temp;
25384 + }
25385 + }
25386 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25387 + {
25388 + $api_service = aiomatic_get_api_service($token, $image_model);
25389 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25390 + }
25391 + $skip_this_copy = true;
25392 + if(!function_exists('is_plugin_active'))
25393 + {
25394 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25395 + }
25396 + 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')
25397 + {
25398 + $skip_this_copy = false;
25399 + }
25400 + if (isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] == 'on')
25401 + {
25402 + $skip_this_copy = false;
25403 + }
25404 + $aierror = '';
25405 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing AI image'));
25406 + $get_img = aiomatic_generate_ai_image($token, 1, $ai_command_image, $image_size, 'featuredImage', $skip_this_copy, 0, $aierror, $image_model, $csv_original_title);
25407 + if($get_img !== false)
25408 + {
25409 + foreach($get_img as $tmpimg)
25410 + {
25411 + $get_img = $tmpimg;
25412 + break;
25413 + }
25414 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25415 + {
25416 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img);
25417 + }
25418 + }
25419 + else
25420 + {
25421 + aiomatic_log_to_file('Failed to generate AI featured image: ' . $aierror);
25422 + $get_img = '';
25423 + }
25424 + }
25425 + else
25426 + {
25427 + aiomatic_log_to_file('Empty AI featured image query entered, nothing to do.');
25428 + }
25429 + }
25430 + elseif(stristr($csv['featured_image_generator'], 'stability') !== false)
25431 + {
25432 + $query_words = trim($csv['featured_image']);
25433 + if($query_words != '')
25434 + {
25435 + $ai_command_image = $query_words;
25436 + if(aiomatic_strlen($ai_command_image) > 2000)
25437 + {
25438 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
25439 + }
25440 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25441 + {
25442 + $api_service = 'Stability.AI';
25443 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25444 + }
25445 + if($image_size == '256x256')
25446 + {
25447 + $width = '512';
25448 + $height = '512';
25449 + }
25450 + elseif($image_size == '512x512')
25451 + {
25452 + $width = '512';
25453 + $height = '512';
25454 + }
25455 + elseif($image_size == '1024x1024')
25456 + {
25457 + $width = '1024';
25458 + $height = '1024';
25459 + }
25460 + else
25461 + {
25462 + $width = '512';
25463 + $height = '512';
25464 + }
25465 + $skip_this_copy = true;
25466 + if(!function_exists('is_plugin_active'))
25467 + {
25468 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25469 + }
25470 + 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')
25471 + {
25472 + $skip_this_copy = false;
25473 + }
25474 + $ierror = '';
25475 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing Stable Diffusion AI image'));
25476 + $get_img = aiomatic_generate_stability_image($ai_command_image, $height, $width, 'featuredStableImage', 0, false, $ierror, $skip_this_copy, false, '', 0);
25477 + if($get_img !== false)
25478 + {
25479 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25480 + {
25481 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
25482 + }
25483 + }
25484 + else
25485 + {
25486 + aiomatic_log_to_file('Failed to generate Stability.AI featured image: ' . $ierror);
25487 + $get_img = '';
25488 + }
25489 + }
25490 + }
25491 + elseif(stristr($csv['featured_image_generator'], 'midjourney') !== false)
25492 + {
25493 + $query_words = trim($csv['featured_image']);
25494 + if($query_words != '')
25495 + {
25496 + $ai_command_image = $query_words;
25497 + if(aiomatic_strlen($ai_command_image) > 2000)
25498 + {
25499 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
25500 + }
25501 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25502 + {
25503 + $api_service = 'Midjourney';
25504 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25505 + }
25506 + if($image_size == '256x256')
25507 + {
25508 + $width = '512';
25509 + $height = '512';
25510 + }
25511 + elseif($image_size == '512x512')
25512 + {
25513 + $width = '512';
25514 + $height = '512';
25515 + }
25516 + elseif($image_size == '1024x1024')
25517 + {
25518 + $width = '1024';
25519 + $height = '1024';
25520 + }
25521 + elseif($image_size == '1792x1024')
25522 + {
25523 + $width = '1792';
25524 + $height = '1024';
25525 + }
25526 + elseif($image_size == '1024x1792')
25527 + {
25528 + $width = '1024';
25529 + $height = '1792';
25530 + }
25531 + else
25532 + {
25533 + $width = '512';
25534 + $height = '512';
25535 + }
25536 + $skip_this_copy = true;
25537 + if(!function_exists('is_plugin_active'))
25538 + {
25539 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25540 + }
25541 + 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')
25542 + {
25543 + $skip_this_copy = false;
25544 + }
25545 + $ierror = '';
25546 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing Midjourney AI image'));
25547 + $get_img = aiomatic_generate_ai_image_midjourney($ai_command_image, $width, $height, 'featuredMidjourneyImage', $skip_this_copy, $ierror);
25548 + if($get_img !== false)
25549 + {
25550 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25551 + {
25552 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
25553 + }
25554 + }
25555 + else
25556 + {
25557 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
25558 + $get_img = '';
25559 + }
25560 + }
25561 + }
25562 + elseif(stristr($csv['featured_image_generator'], 'google') !== false)
25563 + {
25564 + $query_words = trim($csv['featured_image']);
25565 + if($query_words != '')
25566 + {
25567 + $ai_command_image = $query_words;
25568 + if(aiomatic_strlen($ai_command_image) > 2000)
25569 + {
25570 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
25571 + }
25572 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25573 + {
25574 + $api_service = 'Google';
25575 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25576 + }
25577 + $skip_this_copy = true;
25578 + if(!function_exists('is_plugin_active'))
25579 + {
25580 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25581 + }
25582 + 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')
25583 + {
25584 + $skip_this_copy = false;
25585 + }
25586 + $ierror = '';
25587 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing Midjourney AI image'));
25588 + $get_img = aiomatic_generate_image_google($input_tai_command_imageext, $model, 'featuredGoogleImage', false, $ierror, $skip_this_copy);
25589 + if($get_img !== false)
25590 + {
25591 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25592 + {
25593 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
25594 + }
25595 + }
25596 + else
25597 + {
25598 + aiomatic_log_to_file('Failed to generate Midjourney featured image: ' . $ierror);
25599 + $get_img = '';
25600 + }
25601 + }
25602 + }
25603 + elseif(stristr($csv['featured_image_generator'], 'replicate') !== false)
25604 + {
25605 + $query_words = trim($csv['featured_image']);
25606 + if($query_words != '')
25607 + {
25608 + $ai_command_image = $query_words;
25609 + if(aiomatic_strlen($ai_command_image) > 2000)
25610 + {
25611 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
25612 + }
25613 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25614 + {
25615 + $api_service = 'Replicate';
25616 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25617 + }
25618 + if($image_size == '256x256')
25619 + {
25620 + $width = '512';
25621 + $height = '512';
25622 + }
25623 + elseif($image_size == '512x512')
25624 + {
25625 + $width = '512';
25626 + $height = '512';
25627 + }
25628 + elseif($image_size == '1024x1024')
25629 + {
25630 + $width = '1024';
25631 + $height = '1024';
25632 + }
25633 + elseif($image_size == '1792x1024')
25634 + {
25635 + $width = '1792';
25636 + $height = '1024';
25637 + }
25638 + elseif($image_size == '1024x1792')
25639 + {
25640 + $width = '1024';
25641 + $height = '1792';
25642 + }
25643 + else
25644 + {
25645 + $width = '512';
25646 + $height = '512';
25647 + }
25648 + $skip_this_copy = true;
25649 + if(!function_exists('is_plugin_active'))
25650 + {
25651 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25652 + }
25653 + 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')
25654 + {
25655 + $skip_this_copy = false;
25656 + }
25657 + $ierror = '';
25658 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing Replicate AI image'));
25659 + $get_img = aiomatic_generate_replicate_image($ai_command_image, $width, $height, 'featuredReplicateImage', $skip_this_copy, $ierror);
25660 + if($get_img !== false)
25661 + {
25662 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25663 + {
25664 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
25665 + }
25666 + }
25667 + else
25668 + {
25669 + aiomatic_log_to_file('Failed to generate Replicate featured image: ' . $ierror);
25670 + $get_img = '';
25671 + }
25672 + }
25673 + }
25674 + elseif(stristr($csv['featured_image_generator'], 'ideogram') !== false)
25675 + {
25676 + $query_words = trim($csv['featured_image']);
25677 + if($query_words != '')
25678 + {
25679 + $ai_command_image = $query_words;
25680 + if(aiomatic_strlen($ai_command_image) > 2000)
25681 + {
25682 + $ai_command_image = aiomatic_substr($ai_command_image, 0, 2000);
25683 + }
25684 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25685 + {
25686 + $api_service = 'Ideogram';
25687 + aiomatic_log_to_file('Calling ' . $api_service . ' for featured image: ' . $ai_command_image);
25688 + }
25689 + if($image_size == '256x256')
25690 + {
25691 + $width = '512';
25692 + $height = '512';
25693 + }
25694 + elseif($image_size == '512x512')
25695 + {
25696 + $width = '512';
25697 + $height = '512';
25698 + }
25699 + elseif($image_size == '1024x1024')
25700 + {
25701 + $width = '1024';
25702 + $height = '1024';
25703 + }
25704 + elseif($image_size == '1792x1024')
25705 + {
25706 + $width = '1792';
25707 + $height = '1024';
25708 + }
25709 + elseif($image_size == '1024x1792')
25710 + {
25711 + $width = '1024';
25712 + $height = '1792';
25713 + }
25714 + else
25715 + {
25716 + $width = '512';
25717 + $height = '512';
25718 + }
25719 + $skip_this_copy = true;
25720 + if(!function_exists('is_plugin_active'))
25721 + {
25722 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25723 + }
25724 + 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')
25725 + {
25726 + $skip_this_copy = false;
25727 + }
25728 + $ierror = '';
25729 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing Ideogram AI image'));
25730 + $get_img = aiomatic_generate_ideogram_image($ai_command_image, $width, $height, 'featuredIdeogramImage', $skip_this_copy, $ierror);
25731 + if($get_img !== false)
25732 + {
25733 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25734 + {
25735 + aiomatic_log_to_file('AI generated featured image returned: ' . $get_img[1]);
25736 + }
25737 + }
25738 + else
25739 + {
25740 + aiomatic_log_to_file('Failed to generate Ideogram featured image: ' . $ierror);
25741 + $get_img = '';
25742 + }
25743 + }
25744 + }
25745 + else
25746 + {
25747 + $image_query = trim($csv['featured_image']);
25748 + if(empty($image_query))
25749 + {
25750 + $image_query = trim($csv['post_title']);
25751 + }
25752 + if(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'textrazor')
25753 + {
25754 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
25755 + {
25756 + try
25757 + {
25758 + if(!class_exists('TextRazor'))
25759 + {
25760 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
25761 + }
25762 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
25763 + $textrazor = new TextRazor();
25764 + $textrazor->addExtractor('entities');
25765 + $response = $textrazor->analyze($image_query);
25766 + if (isset($response['response']['entities']))
25767 + {
25768 + foreach ($response['response']['entities'] as $entity)
25769 + {
25770 + $query_words = '';
25771 + if(isset($entity['entityEnglishId']))
25772 + {
25773 + $query_words = $entity['entityEnglishId'];
25774 + }
25775 + else
25776 + {
25777 + $query_words = $entity['entityId'];
25778 + }
25779 + if($query_words != '')
25780 + {
25781 + $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
25782 + if(!empty($attached_id))
25783 + {
25784 + $additional_attachments[] = $attached_id;
25785 + }
25786 + if(!empty($z_img))
25787 + {
25788 + $get_img = $z_img;
25789 + $go_local_image = false;
25790 + if(!function_exists('is_plugin_active'))
25791 + {
25792 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25793 + }
25794 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
25795 + {
25796 + $go_local_image = true;
25797 + }
25798 + if($go_local_image == true)
25799 + {
25800 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
25801 + if(!empty($attached_id))
25802 + {
25803 + $additional_attachments[] = $attached_id;
25804 + }
25805 + if($localpath !== false)
25806 + {
25807 + $get_img = $localpath[0];
25808 + }
25809 + }
25810 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25811 + aiomatic_log_to_file('Royalty Free Image Generated with help of TextRazor (kw: "' . $query_words . '"): ' . $z_img);
25812 + }
25813 + break;
25814 + }
25815 + }
25816 + }
25817 + }
25818 + }
25819 + catch(Exception $e)
25820 + {
25821 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25822 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
25823 + }
25824 + }
25825 + }
25826 + }
25827 + elseif(isset($aiomatic_Main_Settings['improve_keywords']) && trim($aiomatic_Main_Settings['improve_keywords']) == 'openai')
25828 + {
25829 + if(isset($aiomatic_Main_Settings['keyword_prompts']) && trim($aiomatic_Main_Settings['keyword_prompts']) != '')
25830 + {
25831 + if(isset($aiomatic_Main_Settings['keyword_model']) && $aiomatic_Main_Settings['keyword_model'] != '')
25832 + {
25833 + $kw_model = $aiomatic_Main_Settings['keyword_model'];
25834 + }
25835 + else
25836 + {
25837 + $kw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
25838 + }
25839 + if(isset($aiomatic_Main_Settings['keyword_assistant_id']) && $aiomatic_Main_Settings['keyword_assistant_id'] != '')
25840 + {
25841 + $keyword_assistant_id = $aiomatic_Main_Settings['keyword_assistant_id'];
25842 + }
25843 + else
25844 + {
25845 + $keyword_assistant_id = '';
25846 + }
25847 + $title_ai_command = trim($aiomatic_Main_Settings['keyword_prompts']);
25848 + $title_ai_command = str_replace('%%default_post_cats%%', $default_cat_list, $title_ai_command);
25849 + $title_ai_command = str_replace('%%original_post_title%%', $csv['post_title'], $title_ai_command);
25850 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
25851 + {
25852 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
25853 + $title_ai_command = array_filter($title_ai_command);
25854 + if(count($title_ai_command) > 0)
25855 + {
25856 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
25857 + }
25858 + else
25859 + {
25860 + $title_ai_command = '';
25861 + }
25862 + }
25863 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
25864 + if(!empty($title_ai_command))
25865 + {
25866 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $csv['post_title'], $blog_title, $csv['post_excerpt'], $csv['post_content'], $user_name, '', $csv['post_categories'], $csv['post_tags'], $postID, $img_attr, '', '', '', '', '');
25867 + }
25868 + $title_ai_command = trim($title_ai_command);
25869 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
25870 + {
25871 + $txt_content = aiomatic_get_web_page($title_ai_command);
25872 + if ($txt_content !== FALSE)
25873 + {
25874 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
25875 + $txt_content = array_filter($txt_content);
25876 + if(count($txt_content) > 0)
25877 + {
25878 + $txt_content = $txt_content[array_rand($txt_content)];
25879 + if(trim($txt_content) != '')
25880 + {
25881 + $title_ai_command = $txt_content;
25882 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
25883 + $title_ai_command = aiomatic_replaceAIPostShortcodes($title_ai_command, $post_link, $csv['post_title'], $blog_title, $csv['post_excerpt'], $csv['post_content'], $user_name, '', $csv['post_categories'], $csv['post_tags'], $postID, $img_attr, '', '', '', '', '');
25884 + }
25885 + }
25886 + }
25887 + }
25888 + if(empty($title_ai_command))
25889 + {
25890 + aiomatic_log_to_file('Empty API keyword extractor seed expression provided!');
25891 + $title_ai_command = 'Type the most relevant keyword, no other text before or after it, for a blog post titled: ' . trim(strip_tags($post_title));
25892 + }
25893 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
25894 + {
25895 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
25896 + }
25897 + $title_ai_command = trim($title_ai_command);
25898 + if(empty($title_ai_command))
25899 + {
25900 + aiomatic_log_to_file('Empty API title seed expression provided(3)! ' . print_r($title_ai_command, true));
25901 + }
25902 + else
25903 + {
25904 + $query_token_count = count(aiomatic_encode($title_ai_command));
25905 + $available_tokens = aiomatic_compute_available_tokens($kw_model, $max_tokens, $title_ai_command, $query_token_count);
25906 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
25907 + {
25908 + $string_len = aiomatic_strlen($title_ai_command);
25909 + $string_len = $string_len / 2;
25910 + $string_len = intval(0 - $string_len);
25911 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
25912 + $title_ai_command = trim($title_ai_command);
25913 + $query_token_count = count(aiomatic_encode($title_ai_command));
25914 + $available_tokens = $max_tokens - $query_token_count;
25915 + }
25916 + $aierror = '';
25917 + $finish_reason = '';
25918 + if(!empty($global_prepend))
25919 + {
25920 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
25921 + }
25922 + if(!empty($global_append))
25923 + {
25924 + $title_ai_command = $title_ai_command . ' ' . $global_append;
25925 + }
25926 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25927 + {
25928 + $api_service = aiomatic_get_api_service($token, $kw_model);
25929 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $keyword_assistant_id . '/' . $kw_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
25930 + }
25931 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating image search keywords'));
25932 + $generated_text = aiomatic_generate_text($token, $kw_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'keywordID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $keyword_assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
25933 + if($generated_text === false)
25934 + {
25935 + aiomatic_log_to_file('Keyword generator error: ' . $aierror);
25936 + $ai_title = '';
25937 + }
25938 + else
25939 + {
25940 + $ai_title = trim(trim($generated_text), '.');
25941 + $ai_titles = explode(',', $ai_title);
25942 + foreach($ai_titles as $query_words)
25943 + {
25944 + $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);
25945 + if(!empty($attached_id))
25946 + {
25947 + $additional_attachments[] = $attached_id;
25948 + }
25949 + if(!empty($z_img))
25950 + {
25951 + $get_img = $z_img;
25952 + $go_local_image = false;
25953 + if(!function_exists('is_plugin_active'))
25954 + {
25955 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25956 + }
25957 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
25958 + {
25959 + $go_local_image = true;
25960 + }
25961 + if($go_local_image == true)
25962 + {
25963 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, $query_words);
25964 + if(!empty($attached_id))
25965 + {
25966 + $additional_attachments[] = $attached_id;
25967 + }
25968 + if($localpath !== false)
25969 + {
25970 + $get_img = $localpath[0];
25971 + }
25972 + }
25973 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
25974 + aiomatic_log_to_file('Royalty Free Image Generated with help of AI (kw: "' . $query_words . '"): ' . $z_img);
25975 + }
25976 + break;
25977 + }
25978 + }
25979 + }
25980 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
25981 + {
25982 + $api_service = aiomatic_get_api_service($token, $kw_model);
25983 + aiomatic_log_to_file('Successfully got API keyword result from ' . $api_service . ': ' . $ai_title);
25984 + }
25985 + }
25986 + }
25987 + }
25988 + if(empty($get_img))
25989 + {
25990 + if($image_query_set == true && !empty($image_query))
25991 + {
25992 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing royalty free image'));
25993 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
25994 + if(!empty($attached_id))
25995 + {
25996 + $additional_attachments[] = $attached_id;
25997 + }
25998 + if($get_img == '' || $get_img === false)
25999 + {
26000 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
26001 + {
26002 + $keyword_class = new Aiomatic_keywords();
26003 + $image_query = $keyword_class->keywords($image_query, 1);
26004 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $image_query, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
26005 + if(!empty($attached_id))
26006 + {
26007 + $additional_attachments[] = $attached_id;
26008 + }
26009 + }
26010 + }
26011 + }
26012 + if(empty($get_img))
26013 + {
26014 + $keyword_class = new Aiomatic_keywords();
26015 + $query_words = $keyword_class->keywords($post_title, 2);
26016 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, true, $raw_img_list, array(), $full_result_list);
26017 + if(!empty($attached_id))
26018 + {
26019 + $additional_attachments[] = $attached_id;
26020 + }
26021 + if($get_img == '' || $get_img === false)
26022 + {
26023 + if(isset($aiomatic_Main_Settings['bimage']) && $aiomatic_Main_Settings['bimage'] == 'on')
26024 + {
26025 + $query_words = $keyword_class->keywords($post_title, 1);
26026 + $get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 20, true, $raw_img_list, array(), $full_result_list);
26027 + if(!empty($attached_id))
26028 + {
26029 + $additional_attachments[] = $attached_id;
26030 + }
26031 + if($get_img == '' || $get_img === false)
26032 + {
26033 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
26034 + {
26035 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26036 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
26037 + }
26038 + unset($post_title_lines[$current_index]);
26039 + continue;
26040 + }
26041 + }
26042 + }
26043 + else
26044 + {
26045 + if(isset($aiomatic_Main_Settings['no_royalty_skip']) && $aiomatic_Main_Settings['no_royalty_skip'] == 'on')
26046 + {
26047 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26048 + aiomatic_log_to_file('Skipping importing because no royalty free image found.');
26049 + }
26050 + unset($post_title_lines[$current_index]);
26051 + continue;
26052 + }
26053 + }
26054 + }
26055 + }
26056 + if(!empty($get_img))
26057 + {
26058 + $go_local_image = false;
26059 + if(!function_exists('is_plugin_active'))
26060 + {
26061 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
26062 + }
26063 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
26064 + {
26065 + $go_local_image = true;
26066 + }
26067 + if($go_local_image == true)
26068 + {
26069 + $localpath = aiomatic_copy_image_locally($get_img, $attached_id, '', false, '');
26070 + if(!empty($attached_id))
26071 + {
26072 + $additional_attachments[] = $attached_id;
26073 + }
26074 + if($localpath !== false)
26075 + {
26076 + $get_img = $localpath[0];
26077 + }
26078 + }
26079 + }
26080 + }
26081 + }
26082 + }
26083 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
26084 + {
26085 + $already_spinned = '1';
26086 + }
26087 + $my_post = $csv;
26088 + if(isset($my_post['post_content']) && stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
26089 + {
26090 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
26091 + {
26092 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
26093 + }
26094 + }
26095 + $my_post['aiomatic_post_image'] = $get_img;
26096 + if(stristr($csv['featured_image_generator'], 'stability') !== false)
26097 + {
26098 + $my_post['aiomatic_local_image'] = '1';
26099 + }
26100 + else
26101 + {
26102 + $my_post['aiomatic_local_image'] = '0';
26103 + }
26104 + $my_post['aiomatic_enable_pingbacks'] = $enable_pingback;
26105 + $my_post['post_type'] = $post_type;
26106 + $my_post['comment_status'] = $accept_comments;
26107 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
26108 + {
26109 + if($post_status == 'publish')
26110 + {
26111 + $draft_me = true;
26112 + $my_post['post_status'] = 'draft';
26113 + }
26114 + else
26115 + {
26116 + $my_post['post_status'] = $post_status;
26117 + }
26118 + }
26119 + else
26120 + {
26121 + $my_post['post_status'] = $post_status;
26122 + }
26123 + if($user_name_type == 'rand')
26124 + {
26125 + $randid = aiomatic_display_random_user();
26126 + if($randid === false)
26127 + {
26128 + $my_post['post_author'] = aiomatic_randomName();
26129 + }
26130 + else
26131 + {
26132 + $my_post['post_author'] = $randid->ID;
26133 + }
26134 + }
26135 + else
26136 + {
26137 + if(isset($csv['post_author']) && $csv['post_author'] != '')
26138 + {
26139 + $post_user_name = $csv['post_author'];
26140 + }
26141 + else
26142 + {
26143 + $post_user_name = $user_name_type;
26144 + }
26145 + $my_post['post_author'] = $post_user_name;
26146 + }
26147 + if($strip_title == '1')
26148 + {
26149 + $my_post['post_content'] = str_replace($my_post['post_title'], '', $my_post['post_content']);
26150 + $my_post['post_content'] = str_replace('<h2></h2>', '', $my_post['post_content']);
26151 + $my_post['post_content'] = str_replace('<h3></h3>', '', $my_post['post_content']);
26152 + }
26153 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
26154 + {
26155 + require_once(dirname(__FILE__) . "/res/swear.php");
26156 + $my_post['post_content'] = aimogenpro_filterwords($my_post['post_content']);
26157 + }
26158 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
26159 + $continue = false;
26160 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
26161 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
26162 + foreach ($banned_list as $banned_word) {
26163 + if (stripos($my_post['post_content'], trim($banned_word)) !== FALSE) {
26164 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26165 + aiomatic_log_to_file('Skipping post "' . esc_html($my_post['post_title']) . '", because it\'s content contains global banned word: ' . $banned_word);
26166 + }
26167 + $continue = true;
26168 + break;
26169 + }
26170 + if (stripos($my_post['post_title'], trim($banned_word)) !== FALSE) {
26171 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26172 + aiomatic_log_to_file('Skipping post "' . esc_html($my_post['post_title']) . '", because it\'s title contains global banned word: ' . $banned_word);
26173 + }
26174 + $continue = true;
26175 + break;
26176 + }
26177 + }
26178 + if ($continue === true) {
26179 + continue;
26180 + }
26181 + }
26182 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
26183 + {
26184 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
26185 + {
26186 + $continue = true;
26187 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
26188 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
26189 + foreach ($required_list as $required_word) {
26190 + if (stripos($my_post['post_content'], trim($required_word)) !== FALSE || stripos($my_post['post_title'], trim($required_word)) !== FALSE) {
26191 + $continue = false;
26192 + break;
26193 + }
26194 + }
26195 + if ($continue === true) {
26196 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26197 + aiomatic_log_to_file('Skipping post "' . esc_html($my_post['post_title']) . '", because it\'s content doesn\'t contain global required words.');
26198 + }
26199 + continue;
26200 + }
26201 + }
26202 + else
26203 + {
26204 + $continue = false;
26205 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
26206 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
26207 + foreach ($required_list as $required_word) {
26208 + if (stripos($my_post['post_content'], trim($required_word)) === FALSE && stripos($my_post['post_title'], trim($required_word)) === FALSE) {
26209 + $continue = true;
26210 + break;
26211 + }
26212 + }
26213 + if ($continue === true) {
26214 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26215 + aiomatic_log_to_file('Skipping post "' . esc_html($my_post['post_title']) . '", because it\'s content doesn\'t contain global required words.');
26216 + }
26217 + continue;
26218 + }
26219 + }
26220 + }
26221 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
26222 + {
26223 + $skip_spin = '1';
26224 + }
26225 + $arr = aiomatic_spin_and_translate($my_post['post_title'], $my_post['post_content'], '3', $skip_spin, $skip_translate);
26226 + if($arr[0] != $my_post['post_title'])
26227 + {
26228 + $my_post['post_title'] = aiomatic_truncate_title($arr[0]);
26229 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
26230 + {
26231 + $posts = get_posts(
26232 + array(
26233 + 'post_type' => $post_type,
26234 + 'title' => html_entity_decode($my_post['post_title']),
26235 + 'post_status' => 'all',
26236 + 'numberposts' => 1,
26237 + 'update_post_term_cache' => false,
26238 + 'update_post_meta_cache' => false,
26239 + 'orderby' => 'post_date ID',
26240 + 'order' => 'ASC',
26241 + )
26242 + );
26243 + if ( ! empty( $posts ) ) {
26244 + $zap = $posts[0];
26245 + } else {
26246 + $zap = null;
26247 + }
26248 + if($zap !== null)
26249 + {
26250 + if($overwrite_existing)
26251 + {
26252 + $update_post_id = $zap->ID;
26253 + }
26254 + else
26255 + {
26256 + aiomatic_log_to_file('Post with specified title already existing (after spin/translate), skipping it: ' . $my_post['post_title']);
26257 + unset($post_title_lines[$current_index]);
26258 + continue;
26259 + }
26260 + }
26261 + }
26262 + }
26263 + $my_post['post_content'] = $arr[1];
26264 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
26265 + {
26266 + $already_spinned = '1';
26267 + }
26268 + $my_post['tags_input'] = $my_post['post_tags'];
26269 + if ($strip_by_regex !== '')
26270 + {
26271 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
26272 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
26273 + $xcnt = 0;
26274 + foreach($xstrip_by_regex as $sbr)
26275 + {
26276 + if(isset($xreplace_regex[$xcnt]))
26277 + {
26278 + $repreg = $xreplace_regex[$xcnt];
26279 + }
26280 + else
26281 + {
26282 + $repreg = '';
26283 + }
26284 + $xcnt++;
26285 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $my_post['post_content']);
26286 + if($temp_cont !== NULL)
26287 + {
26288 + $my_post['post_content'] = $temp_cont;
26289 + }
26290 + }
26291 + }
26292 + $zlang = 'en_US';
26293 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
26294 + $zlang = $aiomatic_Main_Settings['kw_lang'];
26295 + }
26296 + $rel_search = array('post_title', 'post_content');
26297 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
26298 + $rel_search = $aiomatic_Main_Settings['rel_search'];
26299 + }
26300 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
26301 + {
26302 + try
26303 + {
26304 + $my_post['post_content'] = $inboundlinker->add_inbound_links($my_post['post_content'], $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
26305 + }
26306 + catch(Exception $ex)
26307 + {
26308 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26309 + aiomatic_log_to_file('Failed to add inbound links to content: ' . $ex->getMessage());
26310 + }
26311 + }
26312 + }
26313 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
26314 + {
26315 + if($my_post['post_content'] === strip_tags($my_post['post_content']))
26316 + {
26317 + $my_post['post_content'] = nl2br($my_post['post_content']);
26318 + }
26319 + }
26320 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
26321 + {
26322 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
26323 + {
26324 + if(!isset($xchars))
26325 + {
26326 + $xchars = array();
26327 + }
26328 + $rand_percentage = wp_rand(10, 20);
26329 + $my_post['post_content'] = aiomatic_make_unique($my_post['post_content'], $xchars, $rand_percentage);
26330 + }
26331 + }
26332 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
26333 + {
26334 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($my_post['post_content']);
26335 + }
26336 + if($ret_content == 1)
26337 + {
26338 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26339 + aiomatic_log_to_file('Returning AI generated content, finished.');
26340 + }
26341 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
26342 + return array($my_post['post_content'], $my_post['post_title']);
26343 + }
26344 + $my_post['aiomatic_source_title'] = $csv_original_title;
26345 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
26346 + $my_post['aiomatic_post_format'] = $post_format;
26347 + $block_arr = array();
26348 + $custom_arr = array();
26349 + if($custom_fields != '')
26350 + {
26351 + if(stristr($custom_fields, '=>') != false)
26352 + {
26353 + foreach($csv as $ind => $valx)
26354 + {
26355 + foreach($csv as $repl_ind => $repl_valx)
26356 + {
26357 + $custom_fields = str_replace('%%' . $repl_ind . '%%', $repl_valx, $custom_fields);
26358 + }
26359 + $custom_fields = aiomatic_replaceSynergyShortcodes($custom_fields);
26360 + }
26361 + foreach($csv as $ind => $valx)
26362 + {
26363 + $custom_fields = apply_filters('aiomatic_replace_aicontent_shortcode', $custom_fields);
26364 + }
26365 + $rule_arr = explode(',', trim($custom_fields));
26366 + foreach($rule_arr as $rule)
26367 + {
26368 + $my_args = explode('=>', trim($rule));
26369 + if(isset($my_args[1]))
26370 + {
26371 + $my_args[1] = do_shortcode($my_args[1]);
26372 + $my_args[0] = do_shortcode($my_args[0]);
26373 + $custom_field_content = trim($my_args[1]);
26374 + $custom_field_content = $spintax->Parse($custom_field_content, $block_arr);
26375 + $custom_field_content = aiomatic_replaceContentShortcodes($custom_field_content, $img_attr, $ai_command);
26376 + if(stristr($my_args[1], 'serialize_') !== false)
26377 + {
26378 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
26379 + }
26380 + else
26381 + {
26382 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
26383 + {
26384 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
26385 + if(isset($cfm[2][0]))
26386 + {
26387 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
26388 + {
26389 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
26390 + }
26391 + else
26392 + {
26393 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
26394 + }
26395 + }
26396 + else
26397 + {
26398 + $custom_arr[trim($my_args[0])] = $custom_field_content;
26399 + }
26400 + }
26401 + else
26402 + {
26403 + $custom_arr[trim($my_args[0])] = $custom_field_content;
26404 + }
26405 + }
26406 + }
26407 + }
26408 + }
26409 + }
26410 + $block_arr = array();
26411 + $custom_arr = array_merge($custom_arr, array('aiomatic_auto_post_spinned' => $already_spinned, 'aiomatic_post_cats' => $csv['post_categories'], 'aiomatic_post_tags' => $csv['post_tags']));
26412 + $custom_tax_arr = array();
26413 + if($custom_tax != '')
26414 + {
26415 + if(stristr($custom_tax, '=>') != false)
26416 + {
26417 + foreach($csv as $ind => $valx)
26418 + {
26419 + foreach($csv as $repl_ind => $repl_valx)
26420 + {
26421 + $custom_tax = str_replace('%%' . $repl_ind . '%%', $repl_valx, $custom_tax);
26422 + }
26423 + $custom_tax = aiomatic_replaceSynergyShortcodes($custom_tax);
26424 + }
26425 + foreach($csv as $ind => $valx)
26426 + {
26427 + $custom_tax = apply_filters('aiomatic_replace_aicontent_shortcode', $custom_tax);
26428 + }
26429 + $rule_arr = explode(';', trim($custom_tax));
26430 + foreach($rule_arr as $rule)
26431 + {
26432 + $my_args = explode('=>', trim($rule));
26433 + if(isset($my_args[1]))
26434 + {
26435 + $custom_tax_content = trim($my_args[1]);
26436 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
26437 + $custom_tax_content = aiomatic_replaceContentShortcodes($custom_tax_content, $img_attr, $ai_command);
26438 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
26439 + {
26440 + if(isset($custom_arr['_product_attributes']))
26441 + {
26442 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
26443 + 'name' => trim($my_args[0]),
26444 + 'value' => $custom_tax_content,
26445 + 'is_visible' => '1',
26446 + 'is_taxonomy' => '1'
26447 + )));
26448 + }
26449 + else
26450 + {
26451 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
26452 + 'name' => trim($my_args[0]),
26453 + 'value' => $custom_tax_content,
26454 + 'is_visible' => '1',
26455 + 'is_taxonomy' => '1'
26456 + ));
26457 + }
26458 + }
26459 + if(isset($custom_tax_arr[trim($my_args[0])]))
26460 + {
26461 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
26462 + }
26463 + else
26464 + {
26465 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
26466 + }
26467 + }
26468 + }
26469 + }
26470 + }
26471 + if(count($custom_tax_arr) > 0)
26472 + {
26473 + $my_post['taxo_input'] = $custom_tax_arr;
26474 + }
26475 + $my_post['meta_input'] = $custom_arr;
26476 + if($parent_id != '')
26477 + {
26478 + $my_post['post_parent'] = intval($parent_id);
26479 + }
26480 + if ($enable_pingback == '1') {
26481 + $my_post['ping_status'] = 'open';
26482 + } else {
26483 + $my_post['ping_status'] = 'closed';
26484 + }
26485 + if($min_time != '' && $max_time != '')
26486 + {
26487 + $t1 = strtotime($min_time);
26488 + $t2 = strtotime($max_time);
26489 + if($t1 != false && $t2 != false)
26490 + {
26491 + $int = wp_rand($t1, $t2);
26492 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
26493 + }
26494 + }
26495 + elseif($min_time != '')
26496 + {
26497 + $t1 = strtotime($min_time);
26498 + if($t1 != false)
26499 + {
26500 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
26501 + }
26502 + }
26503 + elseif($max_time != '')
26504 + {
26505 + $t1 = strtotime($max_time);
26506 + if($t1 != false)
26507 + {
26508 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
26509 + }
26510 + }
26511 + $count++;
26512 + if($update_post_id != '')
26513 + {
26514 + $my_post['ID'] = $update_post_id;
26515 + }
26516 + remove_filter('content_save_pre', 'wp_filter_post_kses');
26517 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
26518 + try
26519 + {
26520 + $post_id = wp_insert_post($my_post, true);
26521 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
26522 + if (is_wp_error($post_id))
26523 + {
26524 + aiomatic_log_to_file('Trying publishing again');
26525 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
26526 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
26527 + }
26528 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
26529 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
26530 + $post_id = wp_insert_post($my_post, true);
26531 + }
26532 + }
26533 + catch(Exception $e)
26534 + {
26535 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
26536 + }
26537 + add_filter('content_save_pre', 'wp_filter_post_kses');
26538 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
26539 + if (!is_wp_error($post_id)) {
26540 + if($post_id === 0)
26541 + {
26542 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
26543 + continue;
26544 + }
26545 + $posts_inserted++;
26546 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
26547 + {
26548 + pll_set_post_language($post_id, $wpml_lang);
26549 + }
26550 + if(!empty($additional_attachments))
26551 + {
26552 + foreach($additional_attachments as $add_att)
26553 + {
26554 + wp_update_post([
26555 + 'ID' => $add_att,
26556 + 'post_parent' => $post_id,
26557 + ]);
26558 + }
26559 + }
26560 + $default_categories = array();
26561 + if($remove_default == '1' && $my_post['post_categories'] != '')
26562 + {
26563 + $default_categories = wp_get_post_categories($post_id);
26564 + }
26565 + if(isset($my_post['taxo_input']))
26566 + {
26567 + foreach($my_post['taxo_input'] as $taxn => $taxval)
26568 + {
26569 + $taxn = trim($taxn);
26570 + $taxval = trim($taxval);
26571 + if(is_taxonomy_hierarchical($taxn))
26572 + {
26573 + $taxval = array_map('trim', explode(',', $taxval));
26574 + for($ii = 0; $ii < count($taxval); $ii++)
26575 + {
26576 + if(!is_numeric($taxval[$ii]))
26577 + {
26578 + $term_ids = [];
26579 + $parent_id = 0;
26580 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
26581 + foreach($hierarchy_parts as $hp)
26582 + {
26583 + if(!is_numeric($hp))
26584 + {
26585 + $xtermid = get_term_by('name', $hp, $taxn);
26586 + if($xtermid !== false)
26587 + {
26588 + $parent_id = $xtermid->term_id;
26589 + if(!is_numeric($taxval[$ii]))
26590 + {
26591 + $taxval[$ii] = intval($xtermid->term_id);
26592 + }
26593 + else
26594 + {
26595 + $taxval[] = intval($xtermid->term_id);
26596 + }
26597 + }
26598 + else
26599 + {
26600 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
26601 + $xtermid = get_term_by('name', $hp, $taxn);
26602 + if($xtermid !== false)
26603 + {
26604 + $parent_id = $xtermid->term_id;
26605 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
26606 + {
26607 + pll_set_term_language($xtermid->term_id, $wpml_lang);
26608 + }
26609 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
26610 + {
26611 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
26612 + $pars['element_id'] = $xtermid->term_id;
26613 + $pars['element_type'] = $wpml_element_type;
26614 + $pars['language_code'] = $wpml_lang;
26615 + $pars['trid'] = FALSE;
26616 + $pars['source_language_code'] = NULL;
26617 + do_action('wpml_set_element_language_details', $pars);
26618 + }
26619 + if(!is_numeric($taxval[$ii]))
26620 + {
26621 + $taxval[$ii] = intval($xtermid->term_id);
26622 + }
26623 + else
26624 + {
26625 + $taxval[] = intval($xtermid->term_id);
26626 + }
26627 + }
26628 + }
26629 + }
26630 + else
26631 + {
26632 + if(!is_numeric($taxval[$ii]))
26633 + {
26634 + $taxval[$ii] = intval($hp);
26635 + }
26636 + else
26637 + {
26638 + $taxval[] = intval($hp);
26639 + }
26640 + }
26641 + }
26642 + }
26643 + }
26644 + wp_set_post_terms($post_id, $taxval, $taxn, true);
26645 + }
26646 + else
26647 + {
26648 + $jterms = array_map('trim', explode(',', $taxval));
26649 + wp_set_post_terms($post_id, $jterms, $taxn, true);
26650 + }
26651 + }
26652 + }
26653 + if (isset($my_post['aiomatic_post_format']) && $my_post['aiomatic_post_format'] != '' && $my_post['aiomatic_post_format'] != 'post-format-standard') {
26654 + wp_set_post_terms($post_id, $my_post['aiomatic_post_format'], 'post_format', true);
26655 + }
26656 + $featured_path = '';
26657 + $get_img = $my_post['aiomatic_post_image'];
26658 + if ($get_img != '') {
26659 + if($my_post['aiomatic_local_image'] == '1')
26660 + {
26661 + $local_get_img = $get_img[0];
26662 + if (!aiomatic_assign_featured_image_path($local_get_img, $post_id)) {
26663 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26664 + aiomatic_log_to_file('aiomatic_assign_featured_image_path failed for ' . $local_get_img);
26665 + }
26666 + } else {
26667 + $featured_path = $get_img[1];
26668 + }
26669 + }
26670 + else
26671 + {
26672 + if(is_numeric($get_img))
26673 + {
26674 + $featured_path = aiomatic_assign_featured_image($get_img, $post_id);
26675 + }
26676 + else
26677 + {
26678 + if (!aiomatic_generate_featured_image($get_img, $post_id)) {
26679 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26680 + aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $get_img);
26681 + }
26682 + } else {
26683 + $featured_path = $get_img;
26684 + }
26685 + }
26686 + }
26687 + }
26688 + if ($my_post['post_categories'] != '') {
26689 + $extra_cats = explode(',', $my_post['post_categories']);
26690 + foreach($extra_cats as $extra_cat)
26691 + {
26692 + $extra_cat = trim($extra_cat);
26693 + $extra_cat = strip_tags($extra_cat);
26694 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
26695 + if($post_type == 'product')
26696 + {
26697 + $product_tax = 'product_cat';
26698 + }
26699 + else
26700 + {
26701 + $product_tax = 'category';
26702 + }
26703 + if(empty($extra_cat))
26704 + {
26705 + continue;
26706 + }
26707 + if ($skip_inexist == '1')
26708 + {
26709 + if(!term_exists($extra_cat, $product_tax))
26710 + {
26711 + continue;
26712 + }
26713 + }
26714 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
26715 + wp_set_post_terms($post_id, $termid, $product_tax, true);
26716 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
26717 + {
26718 + foreach($termid as $tx)
26719 + {
26720 + pll_set_term_language($tx, $wpml_lang);
26721 + }
26722 + }
26723 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
26724 + {
26725 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
26726 + foreach($termid as $tx)
26727 + {
26728 + $pars['element_id'] = $tx;
26729 + $pars['element_type'] = $wpml_element_type;
26730 + $pars['language_code'] = $wpml_lang;
26731 + $pars['trid'] = FALSE;
26732 + $pars['source_language_code'] = NULL;
26733 + do_action('wpml_set_element_language_details', $pars);
26734 + }
26735 + }
26736 + }
26737 + }
26738 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
26739 + {
26740 + if($post_type == 'product')
26741 + {
26742 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
26743 + }
26744 + }
26745 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
26746 + if (is_wp_error($tax_rez)) {
26747 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
26748 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
26749 + }
26750 + }
26751 + if($post_type == 'topic' && $parent_id != '')
26752 + {
26753 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
26754 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
26755 + update_post_meta($post_id, '_bbp_voice_count', '0');
26756 + update_post_meta($post_id, '_bbp_reply_count', '0');
26757 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
26758 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
26759 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
26760 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
26761 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
26762 + }
26763 + if($post_type == 'reply' && $parent_id != '')
26764 + {
26765 + if(function_exists('bbp_get_topic_forum_id'))
26766 + {
26767 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
26768 + if(empty($forum_aidi))
26769 + {
26770 + $forum_aidi = 0;
26771 + }
26772 + }
26773 + else
26774 + {
26775 + $forum_aidi = 0;
26776 + }
26777 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
26778 + }
26779 + if($remove_default == '1' && $my_post['post_categories'] != '')
26780 + {
26781 + $new_categories = wp_get_post_categories($post_id);
26782 + if(isset($default_categories) && !($default_categories == $new_categories))
26783 + {
26784 + foreach($default_categories as $dc)
26785 + {
26786 + $rem_cat = get_category( $dc );
26787 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
26788 + }
26789 + }
26790 + }
26791 + $my_post['extra_tags'] = '';
26792 + $my_post['extra_categories'] = '';
26793 + aiomatic_addPostMeta($post_id, $my_post, $param, $type, $get_img, '', $rule_unique_id, $post_link);
26794 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
26795 + {
26796 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
26797 + $pars['element_id'] = $post_id;
26798 + $pars['element_type'] = $wpml_element_type;
26799 + $pars['language_code'] = $wpml_lang;
26800 + $pars['source_language_code'] = NULL;
26801 + do_action('wpml_set_element_language_details', $pars);
26802 +
26803 + global $wp_filesystem;
26804 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
26805 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
26806 + wp_filesystem($creds);
26807 + }
26808 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
26809 + {
26810 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
26811 + }
26812 + $wpml_lang = trim($wpml_lang);
26813 + if(function_exists('wpml_update_translatable_content'))
26814 + {
26815 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
26816 + if($my_post['post_title'] != '')
26817 + {
26818 + global $sitepress;
26819 + global $wpdb;
26820 + $keyid = md5($my_post['post_title']);
26821 + $keyName = $keyid . '_wpml';
26822 + $rezxxxa = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '$keyName' limit 1", ARRAY_A );
26823 + if(count($rezxxxa) != 0)
26824 + {
26825 + $metaRow = $rezxxxa[0];
26826 + $metaValue = $metaRow['meta_value'];
26827 + $metaParts = explode('_', $metaValue);
26828 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
26829 + }
26830 + else
26831 + {
26832 + $ptrid = $sitepress->get_element_trid($post_id);
26833 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
26834 + }
26835 + }
26836 +
26837 + }
26838 + }
26839 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
26840 + {
26841 + aiomatic_change_post_status($post_id, 'publish');
26842 + }
26843 + } else {
26844 + aiomatic_log_to_file('Failed to insert post into wp database(5)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
26845 + continue;
26846 + }
26847 + }
26848 + }
26849 + elseif($type == '5')
26850 + {
26851 + $count = 1;
26852 + $keyword_arr = preg_split('/\r\n|\r|\n/', trim($main_keywords));
26853 + $keyword_arr = array_unique($keyword_arr);
26854 + $skipt = 0;
26855 + $kiwis = array();
26856 + $more_kws_arr = preg_split('/\r\n|\r|\n/', trim($more_keywords));
26857 + foreach($more_kws_arr as $this_kw)
26858 + {
26859 + if(strstr($this_kw, '=>') !== false)
26860 + {
26861 + $expl_kw = explode('=>', $this_kw);
26862 + if(isset($expl_kw[1]))
26863 + {
26864 + $kiwis[trim($expl_kw[0])] = trim($expl_kw[1]);
26865 + }
26866 + }
26867 + }
26868 + $block_data = json_decode($sortable_cards, true);
26869 + if($block_data === null)
26870 + {
26871 + aiomatic_log_to_file('Error in parsing OmniBlock data: ' . $sortable_cards);
26872 + if($auto == 1)
26873 + {
26874 + aiomatic_clearFromList($param, $type);
26875 + }
26876 + return 'fail';
26877 + }
26878 + if(empty($block_data))
26879 + {
26880 + aiomatic_log_to_file('No Block data added: ' . $sortable_cards);
26881 + if($auto == 1)
26882 + {
26883 + aiomatic_clearFromList($param, $type);
26884 + }
26885 + return 'fail';
26886 + }
26887 + require_once(__DIR__ . '/res/amazon-direct.php');
26888 + require_once(__DIR__ . '/aiomatic-automation.php');
26889 + $default_block_types = aiomatic_omniblocks_default_block_types();
26890 + $omni_cache = array();
26891 + while(!empty($keyword_arr))
26892 + {
26893 + if ($count > intval($max)) {
26894 + break;
26895 + }
26896 + $block_results = array();
26897 + $thread_id = '';
26898 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
26899 + {
26900 + $rand_ind = array_rand($keyword_arr);
26901 + }
26902 + else
26903 + {
26904 + $rand_ind = array_key_first($keyword_arr);
26905 + }
26906 + $current_keyword = $keyword_arr[$rand_ind];
26907 + if($title_once == '1')
26908 + {
26909 + do
26910 + {
26911 + $args = array(
26912 + 'post_type' => 'any',
26913 + 'posts_per_page' => 1,
26914 + 'post_status' => 'any',
26915 + 'title' => $current_keyword
26916 + );
26917 + $query = new WP_Query($args);
26918 + if ($query->have_posts())
26919 + {
26920 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
26921 + {
26922 + aiomatic_log_to_file('A post with the same title as the OmniBlock keyword already processed, skipping it: ' . $current_keyword);
26923 + }
26924 + unset($keyword_arr[$rand_ind]);
26925 + if(!empty($keyword_arr))
26926 + {
26927 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
26928 + {
26929 + $rand_ind = array_rand($keyword_arr);
26930 + }
26931 + else
26932 + {
26933 + $rand_ind = array_key_first($keyword_arr);
26934 + }
26935 + $current_keyword = $keyword_arr[$rand_ind];
26936 + }
26937 + }
26938 + }
26939 + while($query->have_posts() && !empty($keyword_arr));
26940 +
26941 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
26942 + {
26943 + $addmx = 'no';
26944 + if(aiomatic_is_keyword_processed($current_keyword))
26945 + {
26946 + $addmx = 'yes';
26947 + }
26948 + aiomatic_log_to_file('Checking if keyword is processed: "' . $current_keyword . '" - result: ' . $addmx);
26949 + }
26950 + while (aiomatic_is_keyword_processed($current_keyword) && !empty($keyword_arr))
26951 + {
26952 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
26953 + {
26954 + aiomatic_log_to_file('OmniBlock keyword already processed, skipping it: ' . $current_keyword);
26955 + }
26956 + unset($keyword_arr[$rand_ind]);
26957 + if(!empty($keyword_arr))
26958 + {
26959 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
26960 + {
26961 + $rand_ind = array_rand($keyword_arr);
26962 + }
26963 + else
26964 + {
26965 + $rand_ind = array_key_first($keyword_arr);
26966 + }
26967 + $current_keyword = $keyword_arr[$rand_ind];
26968 + }
26969 + }
26970 + if(empty($stop_omnmiblock_id) && empty($keyword_arr))
26971 + {
26972 + aiomatic_log_to_file('All keywords already processed, nothing to do');
26973 + if($auto == 1)
26974 + {
26975 + aiomatic_clearFromList($param, $type);
26976 + }
26977 + return 'nochange';
26978 + }
26979 + if(!empty($keyword_arr))
26980 + {
26981 + unset($keyword_arr[$rand_ind]);
26982 + }
26983 + }
26984 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
26985 + {
26986 + aiomatic_log_to_file('Starting OmniBlock processing for keyword: ' . $current_keyword);
26987 + }
26988 + $index = 0;
26989 + while ($index < count($block_data))
26990 + {
26991 + $current_block = $block_data[$index];
26992 + $index++;
26993 + $card_type_found = array();
26994 + $block_id = $current_block['identifier'];
26995 + $critical = $current_block['parameters']['critical'];
26996 + $disabled = $current_block['parameters']['disabled'];
26997 + if((empty($stop_omnmiblock_id) || $stop_omnmiblock_id != $block_id) && $disabled == '1')
26998 + {
26999 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
27000 + aiomatic_log_to_file('Skipping OmniBlock ID because it is disabled: ' . esc_html($block_id));
27001 + }
27002 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
27003 + continue;
27004 + }
27005 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
27006 + aiomatic_log_to_file('Running OmniBlock type: ' . $current_block['type'] . ' - ID: ' . esc_html($block_id));
27007 + }
27008 + foreach($default_block_types as $def_card)
27009 + {
27010 + if($current_block['type'] == $def_card['id'])
27011 + {
27012 + $card_type_found = $def_card;
27013 + break;
27014 + }
27015 + }
27016 + if(empty($card_type_found))
27017 + {
27018 + if($critical == '1')
27019 + {
27020 + aiomatic_log_to_file('Halting execution (inexistent card type), as critical block failed (ID ' . esc_html($block_id) . ')');
27021 + return 'fail';
27022 + }
27023 + else
27024 + {
27025 + aiomatic_log_to_file('Block type not found in blocks array: ' . print_r($current_block, true));
27026 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27027 + {
27028 + return array('');
27029 + }
27030 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
27031 + continue;
27032 + }
27033 + }
27034 + if(isset($card_type_found['required_plugin']) && !empty($card_type_found['required_plugin']))
27035 + {
27036 + $need_continue = false;
27037 + foreach($card_type_found['required_plugin'] as $pslug => $pname)
27038 + {
27039 + if(!function_exists('is_plugin_active'))
27040 + {
27041 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
27042 + }
27043 + if (!is_plugin_active($pslug))
27044 + {
27045 + if($critical == '1')
27046 + {
27047 + aiomatic_log_to_file('Halting execution (inexistent plugin extension), as critical block failed (ID ' . esc_html($block_id) . ')');
27048 + return 'fail';
27049 + }
27050 + else
27051 + {
27052 + aiomatic_log_to_file('You need enable the "' . $pname[0] . '" (' . $pslug . ') plugin for this OmniBlock type to work: ' . $pname[1]);
27053 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27054 + {
27055 + return array('');
27056 + }
27057 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
27058 + $need_continue = true;
27059 + break;
27060 + }
27061 + }
27062 + }
27063 + if($need_continue == true)
27064 + {
27065 + continue;
27066 + }
27067 + }
27068 + if($current_block['type'] == 'ai_text')
27069 + {
27070 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
27071 + {
27072 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
27073 + }
27074 + else
27075 + {
27076 + $frequency_penalty = '';
27077 + }
27078 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
27079 + {
27080 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
27081 + }
27082 + else
27083 + {
27084 + $presence_penalty = '';
27085 + }
27086 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
27087 + {
27088 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
27089 + }
27090 + else
27091 + {
27092 + $max_p = '';
27093 + }
27094 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
27095 + {
27096 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
27097 + }
27098 + else
27099 + {
27100 + $temperature = '';
27101 + }
27102 + $prompt = $current_block['parameters']['prompt'];
27103 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
27104 + if (empty($prompt))
27105 + {
27106 + if($critical == '1')
27107 + {
27108 + aiomatic_log_to_file('Halting execution (empty text prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27109 + return 'fail';
27110 + }
27111 + else
27112 + {
27113 + aiomatic_log_to_file('Empty text prompt entered, block ID: ' . $block_id);
27114 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27115 + {
27116 + return array('');
27117 + }
27118 + $block_results[$block_id] = array($current_block['type'], '');
27119 + continue;
27120 + }
27121 + }
27122 + $model = $current_block['parameters']['model'];
27123 + $assistant_id = $current_block['parameters']['assistant_id'];
27124 + $max_tokens = aimogenpro_get_max_tokens($model);
27125 + $query_token_count = count(aiomatic_encode($prompt));
27126 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
27127 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
27128 + {
27129 + $string_len = aiomatic_strlen($prompt);
27130 + $string_len = $string_len / 2;
27131 + $string_len = intval(0 - $string_len);
27132 + $prompt = aiomatic_substr($prompt, 0, $string_len);
27133 + $prompt = trim($prompt);
27134 + $query_token_count = count(aiomatic_encode($prompt));
27135 + $available_tokens = $max_tokens - $query_token_count;
27136 + }
27137 + $aierror = '';
27138 + $finish_reason = '';
27139 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
27140 + {
27141 + $api_service = aiomatic_get_api_service($token, $model);
27142 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock text ID: ' . $block_id);
27143 + aiomatic_log_to_file('AI Prompt is: ' . $prompt);
27144 + }
27145 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock AI content'));
27146 + $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
27147 +
27148 + if($generated_text === false)
27149 + {
27150 + if($critical == '1')
27151 + {
27152 + aiomatic_log_to_file('Halting execution (failed to generate AI content), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27153 + return 'fail';
27154 + }
27155 + else
27156 + {
27157 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27158 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27159 + {
27160 + return array('');
27161 + }
27162 + $block_results[$block_id] = array($current_block['type'], '');
27163 + }
27164 + }
27165 + else
27166 + {
27167 + if(empty($generated_text))
27168 + {
27169 + aiomatic_log_to_file('Empty content generated using AI OmniBlock writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27170 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27171 + {
27172 + return array('');
27173 + }
27174 + $block_results[$block_id] = array($current_block['type'], '');
27175 + }
27176 + else
27177 + {
27178 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27179 + {
27180 + return array($generated_text);
27181 + }
27182 + $block_results[$block_id] = array($current_block['type'], $generated_text);
27183 + }
27184 + }
27185 + }
27186 + elseif($current_block['type'] == 'ai_vision')
27187 + {
27188 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
27189 + {
27190 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
27191 + }
27192 + else
27193 + {
27194 + $frequency_penalty = '';
27195 + }
27196 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
27197 + {
27198 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
27199 + }
27200 + else
27201 + {
27202 + $presence_penalty = '';
27203 + }
27204 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
27205 + {
27206 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
27207 + }
27208 + else
27209 + {
27210 + $max_p = '';
27211 + }
27212 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
27213 + {
27214 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
27215 + }
27216 + else
27217 + {
27218 + $temperature = '';
27219 + }
27220 + $image_url = $current_block['parameters']['image_url'];
27221 + $image_url = aiomatic_replace_omniblocks_data($image_url, $current_keyword, $kiwis, $block_results, $load_variables);
27222 + $prompt = $current_block['parameters']['prompt'];
27223 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
27224 + if (empty($prompt))
27225 + {
27226 + if($critical == '1')
27227 + {
27228 + aiomatic_log_to_file('Halting execution (empty text prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27229 + return 'fail';
27230 + }
27231 + else
27232 + {
27233 + if(empty($image_url))
27234 + {
27235 + aiomatic_log_to_file('Empty text prompt entered, block ID: ' . $block_id);
27236 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27237 + {
27238 + return array('');
27239 + }
27240 + $block_results[$block_id] = array($current_block['type'], '');
27241 + continue;
27242 + }
27243 + else
27244 + {
27245 + $prompt = 'Analyze this image in detail';
27246 + }
27247 + }
27248 + }
27249 + $model = $current_block['parameters']['model'];
27250 + $assistant_id = $current_block['parameters']['assistant_id'];
27251 + $max_tokens = aimogenpro_get_max_tokens($model);
27252 + $query_token_count = count(aiomatic_encode($prompt));
27253 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
27254 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
27255 + {
27256 + $string_len = aiomatic_strlen($prompt);
27257 + $string_len = $string_len / 2;
27258 + $string_len = intval(0 - $string_len);
27259 + $prompt = aiomatic_substr($prompt, 0, $string_len);
27260 + $prompt = trim($prompt);
27261 + $query_token_count = count(aiomatic_encode($prompt));
27262 + $available_tokens = $max_tokens - $query_token_count;
27263 + }
27264 + $aierror = '';
27265 + $finish_reason = '';
27266 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
27267 + {
27268 + $api_service = aiomatic_get_api_service($token, $model);
27269 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock text ID: ' . $block_id);
27270 + aiomatic_log_to_file('AI Prompt is: ' . $prompt);
27271 + }
27272 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock AI content'));
27273 + $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, $image_url, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
27274 +
27275 + if($generated_text === false)
27276 + {
27277 + if($critical == '1')
27278 + {
27279 + aiomatic_log_to_file('Halting execution (failed to generate AI vision content), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27280 + return 'fail';
27281 + }
27282 + else
27283 + {
27284 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock vision content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27285 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27286 + {
27287 + return array('');
27288 + }
27289 + $block_results[$block_id] = array($current_block['type'], '');
27290 + }
27291 + }
27292 + else
27293 + {
27294 + if(empty($generated_text))
27295 + {
27296 + aiomatic_log_to_file('Empty vision content generated using AI OmniBlock writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27297 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27298 + {
27299 + return array('');
27300 + }
27301 + $block_results[$block_id] = array($current_block['type'], '');
27302 + }
27303 + else
27304 + {
27305 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27306 + {
27307 + return array($generated_text);
27308 + }
27309 + $block_results[$block_id] = array($current_block['type'], $generated_text);
27310 + }
27311 + }
27312 + }
27313 + elseif($current_block['type'] == 'ai_text_foreach')
27314 + {
27315 + $final_text = '';
27316 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
27317 + {
27318 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
27319 + }
27320 + else
27321 + {
27322 + $frequency_penalty = '';
27323 + }
27324 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
27325 + {
27326 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
27327 + }
27328 + else
27329 + {
27330 + $presence_penalty = '';
27331 + }
27332 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
27333 + {
27334 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
27335 + }
27336 + else
27337 + {
27338 + $max_p = '';
27339 + }
27340 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
27341 + {
27342 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
27343 + }
27344 + else
27345 + {
27346 + $temperature = '';
27347 + }
27348 + $multiline_input = $current_block['parameters']['multiline_input'];
27349 + $multiline_input = aiomatic_replace_omniblocks_data($multiline_input, $current_keyword, $kiwis, $block_results, $load_variables);
27350 + $prepend = $current_block['parameters']['prepend'];
27351 + $prepend = aiomatic_replace_omniblocks_data($prepend, $current_keyword, $kiwis, $block_results, $load_variables);
27352 + $append = $current_block['parameters']['append'];
27353 + $append = aiomatic_replace_omniblocks_data($append, $current_keyword, $kiwis, $block_results, $load_variables);
27354 + $max_runs = $current_block['parameters']['max_runs'];
27355 + if (empty($multiline_input))
27356 + {
27357 + if($critical == '1')
27358 + {
27359 + aiomatic_log_to_file('Halting execution (empty text multiline_input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27360 + return 'fail';
27361 + }
27362 + else
27363 + {
27364 + aiomatic_log_to_file('Empty text multiline_input entered, block ID: ' . $block_id);
27365 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27366 + {
27367 + return array('');
27368 + }
27369 + $block_results[$block_id] = array($current_block['type'], '');
27370 + continue;
27371 + }
27372 + }
27373 + $prompt = $current_block['parameters']['prompt'];
27374 + if (empty($prompt))
27375 + {
27376 + if($critical == '1')
27377 + {
27378 + aiomatic_log_to_file('Halting execution (empty text prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27379 + return 'fail';
27380 + }
27381 + else
27382 + {
27383 + aiomatic_log_to_file('Empty text prompt entered, block ID: ' . $block_id);
27384 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27385 + {
27386 + return array('');
27387 + }
27388 + $block_results[$block_id] = array($current_block['type'], '');
27389 + continue;
27390 + }
27391 + }
27392 + $multiline_input_arr = preg_split('/\r\n|\r|\n/', trim($multiline_input));
27393 + $current_line_counter = 1;
27394 + foreach($multiline_input_arr as $curr_line)
27395 + {
27396 + if(!empty($max_runs) && is_numeric($max_runs))
27397 + {
27398 + if(intval($max_runs) < $current_line_counter)
27399 + {
27400 + break;
27401 + }
27402 + }
27403 + $prompt_now = $prompt;
27404 + $prompt_now = aiomatic_replace_omniblocks_data($prompt_now, $current_keyword, $kiwis, $block_results, $load_variables);
27405 + $prompt_now = str_replace('%%current_input_line%%', $curr_line, $prompt_now);
27406 + $prompt_now = str_replace('%%current_input_line_counter%%', $current_line_counter, $prompt_now);
27407 + $prompt_now = str_replace('%%all_input_lines%%', $multiline_input, $prompt_now);
27408 + $current_line_counter++;
27409 + if (empty($prompt_now))
27410 + {
27411 + if($critical == '1')
27412 + {
27413 + aiomatic_log_to_file('Halting execution (empty text prompt_now entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27414 + return 'fail';
27415 + }
27416 + else
27417 + {
27418 + aiomatic_log_to_file('Empty text prompt_now entered, block ID: ' . $block_id);
27419 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27420 + {
27421 + return array('');
27422 + }
27423 + $block_results[$block_id] = array($current_block['type'], '');
27424 + continue;
27425 + }
27426 + }
27427 + $model = $current_block['parameters']['model'];
27428 + $assistant_id = $current_block['parameters']['assistant_id'];
27429 + $max_tokens = aimogenpro_get_max_tokens($model);
27430 +
27431 + $query_token_count = count(aiomatic_encode($prompt_now));
27432 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt_now, $query_token_count);
27433 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
27434 + {
27435 + $string_len = aiomatic_strlen($prompt_now);
27436 + $string_len = $string_len / 2;
27437 + $string_len = intval(0 - $string_len);
27438 + $prompt_now = aiomatic_substr($prompt_now, 0, $string_len);
27439 + $prompt_now = trim($prompt_now);
27440 + $query_token_count = count(aiomatic_encode($prompt_now));
27441 + $available_tokens = $max_tokens - $query_token_count;
27442 + }
27443 + $aierror = '';
27444 + $finish_reason = '';
27445 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
27446 + {
27447 + $api_service = aiomatic_get_api_service($token, $model);
27448 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock text ID: ' . $block_id);
27449 + aiomatic_log_to_file('Current AI Prompt is: ' . $prompt_now);
27450 + }
27451 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock AI content'));
27452 + $generated_text = aiomatic_generate_text($token, $model, $prompt_now, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, $store_data, array(), '');
27453 + if($generated_text === false)
27454 + {
27455 + if($critical == '1')
27456 + {
27457 + aiomatic_log_to_file('Halting execution (AI content failed), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27458 + return 'fail';
27459 + }
27460 + else
27461 + {
27462 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27463 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27464 + {
27465 + return array('');
27466 + }
27467 + $block_results[$block_id] = array($current_block['type'], '');
27468 + }
27469 + }
27470 + else
27471 + {
27472 + if(empty($generated_text))
27473 + {
27474 + aiomatic_log_to_file('Empty content generated using AI OmniBlock writer (ID ' . esc_html($block_id) . '): ' . $model);
27475 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27476 + {
27477 + return array('');
27478 + }
27479 + $block_results[$block_id] = array($current_block['type'], '');
27480 + }
27481 + else
27482 + {
27483 + $final_text .= ' ' . $prepend . $generated_text . $append;
27484 + }
27485 + }
27486 + }
27487 + if(empty($final_text))
27488 + {
27489 + aiomatic_log_to_file('Empty content generated using AI OmniBlock multiline writer (ID ' . esc_html($block_id) . '): ' . $model);
27490 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27491 + {
27492 + return array('');
27493 + }
27494 + $block_results[$block_id] = array($current_block['type'], '');
27495 + }
27496 + else
27497 + {
27498 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27499 + {
27500 + return array($final_text);
27501 + }
27502 + $block_results[$block_id] = array($current_block['type'], $final_text);
27503 + }
27504 + }
27505 + elseif($current_block['type'] == 'text_translate')
27506 + {
27507 + $translation = false;
27508 + $pre_tags_matches = array();
27509 + $pre_tags_matches_s = array();
27510 + $conseqMatchs = array();
27511 + $final_content = $current_block['parameters']['input_text'];
27512 + $final_content = aiomatic_replace_omniblocks_data($final_content, $current_keyword, $kiwis, $block_results, $load_variables);
27513 + if (empty($final_content))
27514 + {
27515 + if($critical == '1')
27516 + {
27517 + aiomatic_log_to_file('Halting execution (empty text input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27518 + return 'fail';
27519 + }
27520 + else
27521 + {
27522 + aiomatic_log_to_file('Empty text input entered, block ID: ' . $block_id);
27523 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27524 + {
27525 + return array('');
27526 + }
27527 + $block_results[$block_id] = array($current_block['type'], '');
27528 + continue;
27529 + }
27530 + }
27531 + $translate = $current_block['parameters']['translate'];
27532 + if (empty($translate) || $translate == 'disabled')
27533 + {
27534 + if($critical == '1')
27535 + {
27536 + aiomatic_log_to_file('Halting execution (translation disabled), as critical block failed (ID ' . esc_html($block_id) . ')');
27537 + return 'fail';
27538 + }
27539 + else
27540 + {
27541 + aiomatic_log_to_file('Translation disabled for block: ' . $block_id);
27542 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27543 + {
27544 + return array('');
27545 + }
27546 + $block_results[$block_id] = array($current_block['type'], '');
27547 + continue;
27548 + }
27549 + }
27550 + $translate_source = $current_block['parameters']['translate_source'];
27551 + $second_translate = $current_block['parameters']['second_translate'];
27552 + if(!empty($translate_source) && $translate_source != 'disabled')
27553 + {
27554 + $tr = $translate_source;
27555 + }
27556 + else
27557 + {
27558 + $tr = 'auto';
27559 + }
27560 + $htmlfounds = array();
27561 + $final_content = aiomatic_replaceExcludes($final_content, $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
27562 + $translation = aiomatic_translate('test', $final_content, $tr, $translate);
27563 + if (is_array($translation) && isset($translation[1]))
27564 + {
27565 + $translation[1] = preg_replace('#(?<=[\*(])\s+(?=[\*)])#', '', $translation[1]);
27566 + $translation[1] = preg_replace('#([^(*\s]\s)\*+\)#', '$1', $translation[1]);
27567 + $translation[1] = preg_replace('#\(\*+([\s][^)*\s])#', '$1', $translation[1]);
27568 + if(isset($second_translate) && $second_translate != 'disabled')
27569 + {
27570 + $translation = aiomatic_translate('test', $translation[1], $translate, $second_translate);
27571 + if (is_array($translation) && isset($translation[1]))
27572 + {
27573 + $translation[1] = aiomatic_restoreExcludes($translation[1], $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
27574 + }
27575 + else
27576 + {
27577 + if($critical == '1')
27578 + {
27579 + aiomatic_log_to_file('Halting execution (second translation failed), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27580 + return 'fail';
27581 + }
27582 + else
27583 + {
27584 + aiomatic_log_to_file('Failed to translate text the second time, from ' . $translate . ' to ' . $second_translate . ' in OmniBlock ID: ' . $block_id);
27585 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27586 + {
27587 + return array('');
27588 + }
27589 + $block_results[$block_id] = array($current_block['type'], '');
27590 + continue;
27591 + }
27592 + }
27593 + }
27594 + else
27595 + {
27596 + $translation[1] = aiomatic_restoreExcludes($translation[1], $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
27597 + }
27598 + }
27599 + else
27600 + {
27601 + if($critical == '1')
27602 + {
27603 + aiomatic_log_to_file('Halting execution (translation failed), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27604 + return 'fail';
27605 + }
27606 + else
27607 + {
27608 + aiomatic_log_to_file('Failed to translate text, from ' . $tr . ' to ' . $translate . ' in OmniBlock ID: ' . $block_id);
27609 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27610 + {
27611 + return array('');
27612 + }
27613 + $block_results[$block_id] = array($current_block['type'], '');
27614 + continue;
27615 + }
27616 + }
27617 + if ($translation !== FALSE) {
27618 + if (is_array($translation) && isset($translation[0]) && isset($translation[1]))
27619 + {
27620 + $final_content = $translation[1];
27621 + $final_content = str_replace('</ iframe>', '</iframe>', $final_content);
27622 + if(stristr($final_content, '<head>') !== false)
27623 + {
27624 + $d = new DOMDocument;
27625 + $mock = new DOMDocument;
27626 + $internalErrors = libxml_use_internal_errors(true);
27627 + $d->loadHTML('<?xml encoding="utf-8" ?>' . $final_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
27628 + libxml_use_internal_errors($internalErrors);
27629 + $body = $d->getElementsByTagName('body')->item(0);
27630 + foreach ($body->childNodes as $child)
27631 + {
27632 + $mock->appendChild($mock->importNode($child, true));
27633 + }
27634 + $new_post_content_temp = $mock->saveHTML();
27635 + if($new_post_content_temp !== '' && $new_post_content_temp !== false)
27636 + {
27637 + $new_post_content_temp = str_replace('<?xml encoding="utf-8" ?>', '', $new_post_content_temp);
27638 + $final_content = preg_replace("/_addload\(function\(\){([^<]*)/i", "", $new_post_content_temp);
27639 + }
27640 + }
27641 + $final_content = htmlspecialchars_decode($final_content);
27642 + $final_content = str_replace('</ ', '</', $final_content);
27643 + $final_content = str_replace(' />', '/>', $final_content);
27644 + $final_content = str_replace('< br/>', '<br/>', $final_content);
27645 + $final_content = str_replace('< / ', '</', $final_content);
27646 + $final_content = str_replace(' / >', '/>', $final_content);
27647 + $final_content = preg_replace('/[\x00-\x1F\x7F\xA0]/u', '', $final_content);
27648 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27649 + {
27650 + return array($final_content);
27651 + }
27652 + $block_results[$block_id] = array($current_block['type'], $final_content);
27653 + }
27654 + else
27655 + {
27656 + if($critical == '1')
27657 + {
27658 + aiomatic_log_to_file('Halting execution (translation failed, malformed data), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27659 + return 'fail';
27660 + }
27661 + else
27662 + {
27663 + aiomatic_log_to_file('Failed to translate text, malformed data, from ' . $tr . ' to ' . $translate . ' in OmniBlock ID: ' . $block_id);
27664 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27665 + {
27666 + return array('');
27667 + }
27668 + $block_results[$block_id] = array($current_block['type'], '');
27669 + continue;
27670 + }
27671 + }
27672 + }
27673 + else
27674 + {
27675 + if($critical == '1')
27676 + {
27677 + aiomatic_log_to_file('Halting execution (translation returned false), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27678 + return 'fail';
27679 + }
27680 + else
27681 + {
27682 + aiomatic_log_to_file('Failed to translate text, from ' . $tr . ' to ' . $translate . ', translation returned false in OmniBlock ID: ' . $block_id);
27683 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27684 + {
27685 + return array('');
27686 + }
27687 + $block_results[$block_id] = array($current_block['type'], '');
27688 + continue;
27689 + }
27690 + }
27691 + }
27692 + elseif($current_block['type'] == 'text_spinner')
27693 + {
27694 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'bulk')
27695 + {
27696 + if($critical == '1')
27697 + {
27698 + aiomatic_log_to_file('Halting execution (spinning disabled for omniblocks), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
27699 + return 'fail';
27700 + }
27701 + else
27702 + {
27703 + aiomatic_log_to_file('Spinning disabled for OmniBlocks, from plugin\'s \'Settings\' menu -> \'Bulk Posts\' tab -> \'Enable Spinner For\' settings field, in OmniBlock ID: ' . $block_id);
27704 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27705 + {
27706 + return array('');
27707 + }
27708 + $block_results[$block_id] = array($current_block['type'], '');
27709 + continue;
27710 + }
27711 + }
27712 + $translation = false;
27713 + $pre_tags_matches = array();
27714 + $pre_tags_matches_s = array();
27715 + $conseqMatchs = array();
27716 + $final_content = $current_block['parameters']['input_text'];
27717 + $final_content = aiomatic_replace_omniblocks_data($final_content, $current_keyword, $kiwis, $block_results, $load_variables);
27718 + if (empty($final_content))
27719 + {
27720 + if($critical == '1')
27721 + {
27722 + aiomatic_log_to_file('Halting execution (empty text input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27723 + return 'fail';
27724 + }
27725 + else
27726 + {
27727 + aiomatic_log_to_file('Empty text input entered, block ID: ' . $block_id);
27728 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27729 + {
27730 + return array('');
27731 + }
27732 + $block_results[$block_id] = array($current_block['type'], '');
27733 + continue;
27734 + }
27735 + }
27736 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
27737 + {
27738 + $htmlfounds = array();
27739 + $final_content = aiomatic_replaceExcludes($final_content, $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
27740 + if ($aiomatic_Main_Settings['spin_text'] == 'builtin') {
27741 + $translation = aiomatic_builtin_spin_text('test', $final_content);
27742 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'wikisynonyms') {
27743 + $translation = aiomatic_spin_text('test', $final_content, false);
27744 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'freethesaurus') {
27745 + $translation = aiomatic_spin_text('test', $final_content, true);
27746 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'best') {
27747 + $translation = aiomatic_best_spin_text('test', $final_content);
27748 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'wordai') {
27749 + $translation = aiomatic_wordai_spin_text('test', $final_content);
27750 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'spinrewriter') {
27751 + $translation = aiomatic_spinrewriter_spin_text('test', $final_content);
27752 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'spinnerchief') {
27753 + $translation = aiomatic_spinnerchief_spin_text('test', $final_content);
27754 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'chimprewriter') {
27755 + $translation = aiomatic_chimprewriter_spin_text('test', $final_content);
27756 + } elseif ($aiomatic_Main_Settings['spin_text'] == 'contentprofessor') {
27757 + $translation = aiomatic_contentprofessor_spin_text('test', $final_content);
27758 + }
27759 + if ($translation !== FALSE)
27760 + {
27761 + if (is_array($translation) && isset($translation[0]) && isset($translation[1])) {
27762 + if (!isset($aiomatic_Main_Settings['no_title']) || $aiomatic_Main_Settings['no_title'] != 'on') {
27763 + $final_content = $translation[1];
27764 + }
27765 + $final_content = aiomatic_fix_spinned_content($final_content, $aiomatic_Main_Settings['spin_text']);
27766 + $final_content = aiomatic_restoreExcludes($final_content, $htmlfounds, $pre_tags_matches, $pre_tags_matches_s, $conseqMatchs);
27767 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27768 + {
27769 + return array($final_content);
27770 + }
27771 + $block_results[$block_id] = array($current_block['type'], $final_content);
27772 + }
27773 + else
27774 + {
27775 + if($critical == '1')
27776 + {
27777 + aiomatic_log_to_file('Halting execution (spinning failed, malformed data), as critical block failed (ID ' . esc_html($block_id) . ')');
27778 + return 'fail';
27779 + }
27780 + else
27781 + {
27782 + aiomatic_log_to_file('Text Spinning failed - malformed data, block ID: ' . $block_id);
27783 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27784 + {
27785 + return array('');
27786 + }
27787 + $block_results[$block_id] = array($current_block['type'], '');
27788 + continue;
27789 + }
27790 + }
27791 + }
27792 + else
27793 + {
27794 + if($critical == '1')
27795 + {
27796 + aiomatic_log_to_file('Halting execution (spinning failed, returned false), as critical block failed (ID ' . esc_html($block_id) . ')');
27797 + return 'fail';
27798 + }
27799 + else
27800 + {
27801 + aiomatic_log_to_file('Text Spinning failed - returned false, block ID: ' . $block_id);
27802 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27803 + {
27804 + return array('');
27805 + }
27806 + $block_results[$block_id] = array($current_block['type'], '');
27807 + continue;
27808 + }
27809 + }
27810 + }
27811 + else
27812 + {
27813 + if($critical == '1')
27814 + {
27815 + aiomatic_log_to_file('Halting execution (spinning disabled in settings), as critical block failed (ID ' . esc_html($block_id) . ')');
27816 + return 'fail';
27817 + }
27818 + else
27819 + {
27820 + aiomatic_log_to_file('Spinning disabled from \'Settings\' menu of the plugin, skipping, block ID: ' . $block_id);
27821 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27822 + {
27823 + return array('');
27824 + }
27825 + $block_results[$block_id] = array($current_block['type'], '');
27826 + continue;
27827 + }
27828 + }
27829 + }
27830 + elseif($current_block['type'] == 'plain_text')
27831 + {
27832 + $final_content = $current_block['parameters']['input_text'];
27833 + $final_content = aiomatic_replace_omniblocks_data($final_content, $current_keyword, $kiwis, $block_results, $load_variables);
27834 + $final_content = do_shortcode($final_content);
27835 + $block_arr = array();
27836 + $spintax = new Aiomatic_Spintax();
27837 + $final_content = $spintax->Parse($final_content, $block_arr);
27838 + if (empty($final_content))
27839 + {
27840 + if($critical == '1')
27841 + {
27842 + aiomatic_log_to_file('Halting execution (empty text input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27843 + return 'fail';
27844 + }
27845 + else
27846 + {
27847 + aiomatic_log_to_file('Empty text input entered, block ID: ' . $block_id);
27848 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27849 + {
27850 + return array('');
27851 + }
27852 + $block_results[$block_id] = array($current_block['type'], '');
27853 + continue;
27854 + }
27855 + }
27856 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27857 + {
27858 + return array($final_content);
27859 + }
27860 + $block_results[$block_id] = array($current_block['type'], $final_content);
27861 + }
27862 + elseif ($current_block['type'] == 'diy')
27863 + {
27864 + $final_content = $current_block['parameters']['input_text'];
27865 + $final_content = aiomatic_replace_omniblocks_data($final_content, $current_keyword, $kiwis, $block_results, $load_variables);
27866 + if (!has_filter("aimogen_diy_omniblock_{$block_id}"))
27867 + {
27868 + if (!has_filter("aiomatic_diy_omniblock_{$block_id}"))
27869 + {
27870 + if($critical == '1')
27871 + {
27872 + aiomatic_log_to_file('Halting execution (filter not defined: "aimogen_diy_omniblock_' . $block_id . '"), as critical block failed (ID ' . esc_html($block_id) . ')');
27873 + return 'fail';
27874 + }
27875 + else
27876 + {
27877 + aiomatic_log_to_file('Filter not defined: "aimogen_diy_omniblock_' . $block_id . '" for block ID: ' . $block_id);
27878 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27879 + {
27880 + return array('');
27881 + }
27882 + $block_results[$block_id] = array($current_block['type'], '');
27883 + continue;
27884 + }
27885 + }
27886 + $final_content = apply_filters("aiomatic_diy_omniblock_{$block_id}", $final_content, [
27887 + 'current_keyword' => $current_keyword,
27888 + 'more_keywords' => $kiwis,
27889 + 'block_results' => $block_results,
27890 + ]);
27891 + if (empty($final_content))
27892 + {
27893 + if($critical == '1')
27894 + {
27895 + aiomatic_log_to_file('Halting execution (empty text input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27896 + return 'fail';
27897 + }
27898 + else
27899 + {
27900 + aiomatic_log_to_file('Empty text input entered, block ID: ' . $block_id);
27901 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27902 + {
27903 + return array('');
27904 + }
27905 + $block_results[$block_id] = array($current_block['type'], '');
27906 + continue;
27907 + }
27908 + }
27909 + if (!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27910 + {
27911 + return array($final_content);
27912 + }
27913 + $block_results[$block_id] = array($current_block['type'], $final_content);
27914 + }
27915 + $final_content = apply_filters("aimogen_diy_omniblock_{$block_id}", $final_content, [
27916 + 'current_keyword' => $current_keyword,
27917 + 'more_keywords' => $kiwis,
27918 + 'block_results' => $block_results,
27919 + ]);
27920 + if (empty($final_content))
27921 + {
27922 + if($critical == '1')
27923 + {
27924 + aiomatic_log_to_file('Halting execution (empty text input entered), as critical block failed (ID ' . esc_html($block_id) . ')');
27925 + return 'fail';
27926 + }
27927 + else
27928 + {
27929 + aiomatic_log_to_file('Empty text input entered, block ID: ' . $block_id);
27930 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27931 + {
27932 + return array('');
27933 + }
27934 + $block_results[$block_id] = array($current_block['type'], '');
27935 + continue;
27936 + }
27937 + }
27938 + if (!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27939 + {
27940 + return array($final_content);
27941 + }
27942 + $block_results[$block_id] = array($current_block['type'], $final_content);
27943 + }
27944 + elseif($current_block['type'] == 'embeddings')
27945 + {
27946 + $input_text = $current_block['parameters']['input_text'];
27947 + $embeddings_namespace = $current_block['parameters']['embeddings_namespace'];
27948 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
27949 + $embeddings_namespace = aiomatic_replace_omniblocks_data($embeddings_namespace, $current_keyword, $kiwis, $block_results, $load_variables);
27950 + $embed_rez = aiomatic_embeddings_result($input_text, $token, $embeddings_namespace);
27951 + if($embed_rez['status'] == 'error')
27952 + {
27953 + if($critical == '1')
27954 + {
27955 + aiomatic_log_to_file('Halting execution (embeddings result failed), as critical block failed (ID ' . esc_html($block_id) . ')');
27956 + return 'fail';
27957 + }
27958 + else
27959 + {
27960 + aiomatic_log_to_file('Embeddings result failed, block ID: ' . $block_id . ' embeddings data: ' . print_r($embed_rez, true));
27961 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27962 + {
27963 + return array('');
27964 + }
27965 + $block_results[$block_id] = array($current_block['type'], '');
27966 + continue;
27967 + }
27968 + }
27969 + else
27970 + {
27971 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
27972 + {
27973 + return array($embed_rez['data']);
27974 + }
27975 + $block_results[$block_id] = array($current_block['type'], $embed_rez['data']);
27976 + }
27977 + }
27978 + elseif($current_block['type'] == 'internet_access')
27979 + {
27980 + $input_text = $current_block['parameters']['input_text'];
27981 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
27982 + $locale = '';
27983 + if (isset($aiomatic_Main_Settings['internet_gl']) && $aiomatic_Main_Settings['internet_gl'] != '')
27984 + {
27985 + $locale = $aiomatic_Main_Settings['internet_gl'];
27986 + }
27987 + $internet_rez = aiomatic_internet_result($input_text, false, $locale);
27988 + shuffle($internet_rez);
27989 + if (isset($aiomatic_Main_Settings['results_num']) && trim($aiomatic_Main_Settings['results_num']) != '')
27990 + {
27991 + $results = intval(trim($aiomatic_Main_Settings['results_num']));
27992 + }
27993 + else
27994 + {
27995 + $results = 3;
27996 + }
27997 + $gotcnt = 0;
27998 + $internet_results = '';
27999 + foreach($internet_rez as $emb)
28000 + {
28001 + if($gotcnt >= $results)
28002 + {
28003 + break;
28004 + }
28005 + if (isset($aiomatic_Main_Settings['internet_single_template']) && trim($aiomatic_Main_Settings['internet_single_template']) != '')
28006 + {
28007 + $internet_single_template = $aiomatic_Main_Settings['internet_single_template'];
28008 + }
28009 + else
28010 + {
28011 + $internet_single_template = '[%%result_counter%%]: %%result_title%% %%result_snippet%% ' . PHP_EOL . 'URL: %%result_link%%';
28012 + }
28013 + $internet_single_template = str_replace('%%result_counter%%', $gotcnt + 1, $internet_single_template);
28014 + $internet_single_template = str_replace('%%result_title%%', $emb['title'], $internet_single_template);
28015 + $internet_single_template = str_replace('%%result_snippet%%', $emb['snippet'], $internet_single_template);
28016 + $internet_single_template = str_replace('%%result_link%%', $emb['link'], $internet_single_template);
28017 + $internet_results .= $internet_single_template . PHP_EOL;
28018 + $gotcnt++;
28019 + }
28020 + if(empty($internet_results))
28021 + {
28022 + if($critical == '1')
28023 + {
28024 + aiomatic_log_to_file('Halting execution (internet search result no data returned), as critical block failed (ID ' . esc_html($block_id) . ')');
28025 + return 'fail';
28026 + }
28027 + else
28028 + {
28029 + aiomatic_log_to_file('Internet search result failed, block ID: ' . $block_id . ', no data returned');
28030 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28031 + {
28032 + return array('');
28033 + }
28034 + $block_results[$block_id] = array($current_block['type'], '');
28035 + continue;
28036 + }
28037 + }
28038 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28039 + {
28040 + return array($internet_results);
28041 + }
28042 + $block_results[$block_id] = array($current_block['type'], $internet_results);
28043 + }
28044 + elseif($current_block['type'] == 'load_file')
28045 + {
28046 + $input_text = $current_block['parameters']['input_text'];
28047 + global $wp_filesystem;
28048 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
28049 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
28050 + wp_filesystem($creds);
28051 + }
28052 + if($input_text == 'random')
28053 + {
28054 + $omni_files = get_posts([
28055 + 'post_type' => 'aiomatic_omni_file',
28056 + 'post_status' => 'publish',
28057 + 'numberposts' => -1
28058 + ]);
28059 + if(empty($omni_files))
28060 + {
28061 + if($critical == '1')
28062 + {
28063 + aiomatic_log_to_file('Halting execution (no OmniBlock Files found), as critical block failed (ID ' . esc_html($block_id) . ')');
28064 + return 'fail';
28065 + }
28066 + else
28067 + {
28068 + aiomatic_log_to_file('No OmniBlock Files found, block ID: ' . $block_id);
28069 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28070 + {
28071 + return array('');
28072 + }
28073 + $block_results[$block_id] = array($current_block['type'], '');
28074 + continue;
28075 + }
28076 + }
28077 + else
28078 + {
28079 + $this_post = $omni_files[array_rand($omni_files)];
28080 + }
28081 + }
28082 + elseif($input_text == 'latest')
28083 + {
28084 + $paged = 1;
28085 + $posts_per_page = 100;
28086 + $omni_files = array();
28087 + do {
28088 + $args = array(
28089 + 'post_type' => 'aiomatic_omni_file',
28090 + 'post_status' => 'publish',
28091 + 'posts_per_page' => $posts_per_page,
28092 + 'paged' => $paged,
28093 + 'orderby' => 'date',
28094 + 'order' => 'DESC',
28095 + );
28096 +
28097 + $query = new WP_Query($args);
28098 +
28099 + if ($query->have_posts()) {
28100 + $omni_files = array_merge($omni_files, $query->posts);
28101 + }
28102 +
28103 + $paged++;
28104 + wp_reset_postdata();
28105 + } while ($query->have_posts());
28106 + if(empty($omni_files))
28107 + {
28108 + if($critical == '1')
28109 + {
28110 + aiomatic_log_to_file('Halting execution (no OmniBlock Files found), as critical block failed (ID ' . esc_html($block_id) . ')');
28111 + return 'fail';
28112 + }
28113 + else
28114 + {
28115 + aiomatic_log_to_file('No OmniBlock Files found, block ID: ' . $block_id);
28116 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28117 + {
28118 + return array('');
28119 + }
28120 + $block_results[$block_id] = array($current_block['type'], '');
28121 + continue;
28122 + }
28123 + }
28124 + else
28125 + {
28126 + $this_post = reset($omni_files);
28127 + }
28128 + }
28129 + else
28130 + {
28131 + $this_post = get_post($input_text);
28132 + }
28133 + if($this_post === null)
28134 + {
28135 + if($critical == '1')
28136 + {
28137 + aiomatic_log_to_file('Halting execution (file result failed), as critical block failed (ID ' . esc_html($block_id) . ')');
28138 + return 'fail';
28139 + }
28140 + else
28141 + {
28142 + aiomatic_log_to_file('File result failed, block ID: ' . $block_id . ' file ID: ' . print_r($input_text, true));
28143 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28144 + {
28145 + return array('');
28146 + }
28147 + $block_results[$block_id] = array($current_block['type'], '');
28148 + continue;
28149 + }
28150 + }
28151 + else
28152 + {
28153 + $post_urlx = $this_post->post_content;
28154 + if(aiomatic_endsWith($post_urlx, '.xlsx'))
28155 + {
28156 + $items = aiomatic_extract_remote_xlsx($post_urlx, '1');
28157 + if($items === false)
28158 + {
28159 + if($critical == '1')
28160 + {
28161 + aiomatic_log_to_file('Halting execution (Failed to parse xlsx), as critical block failed (ID ' . esc_html($block_id) . ')');
28162 + return 'fail';
28163 + }
28164 + else
28165 + {
28166 + aiomatic_log_to_file('Failed to parse xlsx, block ID: ' . $block_id . ' file URL: ' . print_r($post_urlx, true));
28167 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28168 + {
28169 + return array('');
28170 + }
28171 + $block_results[$block_id] = array($current_block['type'], '');
28172 + continue;
28173 + }
28174 + }
28175 + if (count($items) == 0)
28176 + {
28177 + if($critical == '1')
28178 + {
28179 + aiomatic_log_to_file('Halting execution (Empty xlsx file), as critical block failed (ID ' . esc_html($block_id) . ')');
28180 + return 'fail';
28181 + }
28182 + else
28183 + {
28184 + aiomatic_log_to_file('Empty xlsx file, block ID: ' . $block_id . ' file URL: ' . print_r($post_urlx, true));
28185 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28186 + {
28187 + return array('');
28188 + }
28189 + $block_results[$block_id] = array($current_block['type'], '');
28190 + continue;
28191 + }
28192 + }
28193 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28194 + {
28195 + return array($items);
28196 + }
28197 + $block_results[$block_id] = array($current_block['type'], $items);
28198 + }
28199 + elseif(aiomatic_endsWith($post_urlx, '.pdf'))
28200 + {
28201 + if(!function_exists('is_plugin_active'))
28202 + {
28203 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
28204 + }
28205 + if (!is_plugin_active('aiomatic-extension-pdf-files/aiomatic-extension-pdf-files.php'))
28206 + {
28207 + if($critical == '1')
28208 + {
28209 + aiomatic_log_to_file('Halting execution (PDF Parser Extension plugin is not active, please activate it to use this functionality), as critical block failed (ID ' . esc_html($block_id) . ')');
28210 + return 'fail';
28211 + }
28212 + else
28213 + {
28214 + aiomatic_log_to_file('Aimogen Extension - PDF File Storage And Parsing plugin is not active, please activate it to use this functionality');
28215 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28216 + {
28217 + return array('');
28218 + }
28219 + $block_results[$block_id] = array($current_block['type'], '');
28220 + continue;
28221 + }
28222 + }
28223 + $htmlc = aiomatic_extension_pdfext_getRemoteFile($post_urlx);
28224 + if($htmlc === false)
28225 + {
28226 + if($critical == '1')
28227 + {
28228 + aiomatic_log_to_file('Halting execution (Failed to download pdf file), as critical block failed (ID ' . esc_html($block_id) . ')');
28229 + return 'fail';
28230 + }
28231 + else
28232 + {
28233 + aiomatic_log_to_file('Failed to download file: ' . $post_urlx);
28234 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28235 + {
28236 + return array('');
28237 + }
28238 + $block_results[$block_id] = array($current_block['type'], '');
28239 + continue;
28240 + }
28241 + }
28242 + $file_data = '';
28243 + $pdf = new PdfToText();
28244 + $pdf->LoadFromString($htmlc);
28245 + $page_range = '';
28246 + if($page_range == '')
28247 + {
28248 + $file_data = $pdf->Text;
28249 + }
28250 + else
28251 + {
28252 + $page_range = array_map('trim', aiomatic_extension_pdfext_extract_range($page_range));
28253 + foreach( $pdf->Pages as $page_number => $page_contents)
28254 + {
28255 + if(count($page_range) == 0)
28256 + {
28257 + break;
28258 + }
28259 + if(in_array($page_number, $page_range))
28260 + {
28261 + $page_range = array_diff($page_range, array($page_number));
28262 + $file_data .= $page_contents;
28263 + }
28264 + }
28265 + }
28266 + if($file_data === false || empty(trim($file_data)))
28267 + {
28268 + $pparser = new \Smalot\PdfParser\Parser();
28269 + $document = $pparser->parseContent($htmlc);
28270 + if($page_range == '')
28271 + {
28272 + $file_data = $document->getText();
28273 + }
28274 + else
28275 + {
28276 + $page_range = array_map('trim', aiomatic_extension_pdfext_extract_range($page_range));
28277 + $nr = 1;
28278 + $pages = $document->getPages();
28279 + foreach ($pages as $page) {
28280 + if(count($page_range) == 0)
28281 + {
28282 + break;
28283 + }
28284 + if(in_array($nr, $page_range))
28285 + {
28286 + $page_range = array_diff($page_range, array($nr));
28287 + $file_data .= $page->getText();
28288 + }
28289 + $nr++;
28290 + }
28291 + $pages = count($pages);
28292 + }
28293 + }
28294 + $file_data = nl2br($file_data);
28295 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28296 + {
28297 + return array($file_data);
28298 + }
28299 + $block_results[$block_id] = array($current_block['type'], $file_data);
28300 + }
28301 + else
28302 + {
28303 + $file_data = aiomatic_get_web_page($post_urlx);
28304 + if($file_data === false)
28305 + {
28306 + if($critical == '1')
28307 + {
28308 + aiomatic_log_to_file('Halting execution (file content opening failed), as critical block failed (ID ' . esc_html($block_id) . ')');
28309 + return 'fail';
28310 + }
28311 + else
28312 + {
28313 + aiomatic_log_to_file('Failed to open file content, block ID: ' . $block_id . ' file URL: ' . print_r($post_urlx, true));
28314 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28315 + {
28316 + return array('');
28317 + }
28318 + $block_results[$block_id] = array($current_block['type'], '');
28319 + continue;
28320 + }
28321 + }
28322 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28323 + {
28324 + return array($file_data);
28325 + }
28326 + $block_results[$block_id] = array($current_block['type'], $file_data);
28327 + }
28328 + }
28329 + }
28330 + elseif($current_block['type'] == 'dalle_ai_image')
28331 + {
28332 + $prompt = $current_block['parameters']['prompt'];
28333 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28334 + if (empty($prompt))
28335 + {
28336 + if($critical == '1')
28337 + {
28338 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28339 + return 'fail';
28340 + }
28341 + else
28342 + {
28343 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28344 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28345 + {
28346 + return array('');
28347 + }
28348 + $block_results[$block_id] = array($current_block['type'], '');
28349 + continue;
28350 + }
28351 + }
28352 + $model = $current_block['parameters']['model'];
28353 + $image_size = $current_block['parameters']['image_size'];
28354 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Dall-E images'));
28355 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28356 + {
28357 + $api_service = aiomatic_get_api_service($token, $model);
28358 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock Dall-E image');
28359 + }
28360 + $aierror = '';
28361 + $response_text = aiomatic_generate_ai_image($token, 1, $prompt, $image_size, 'omniBlocksWriterDalleImage', false, 0, $aierror, $model, $prompt);
28362 + if($response_text !== false && is_array($response_text))
28363 + {
28364 + foreach($response_text as $tmpimg)
28365 + {
28366 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28367 + {
28368 + return array($tmpimg);
28369 + }
28370 + $block_results[$block_id] = array($current_block['type'], $tmpimg);
28371 + break;
28372 + }
28373 + }
28374 + else
28375 + {
28376 + if($critical == '1')
28377 + {
28378 + aiomatic_log_to_file('Halting execution (Dall-E error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28379 + return 'fail';
28380 + }
28381 + else
28382 + {
28383 + aiomatic_log_to_file('Error occurred in Dall-E image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28384 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28385 + {
28386 + return array('');
28387 + }
28388 + $block_results[$block_id] = array($current_block['type'], '');
28389 + }
28390 + }
28391 + }
28392 + elseif($current_block['type'] == 'stable_ai_image')
28393 + {
28394 + $prompt = $current_block['parameters']['prompt'];
28395 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28396 + if (empty($prompt))
28397 + {
28398 + if($critical == '1')
28399 + {
28400 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28401 + return 'fail';
28402 + }
28403 + else
28404 + {
28405 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28406 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28407 + {
28408 + return array('');
28409 + }
28410 + $block_results[$block_id] = array($current_block['type'], '');
28411 + continue;
28412 + }
28413 + }
28414 + $model = $current_block['parameters']['model'];
28415 + $image_size = $current_block['parameters']['image_size'];
28416 + if($image_size == '512x512')
28417 + {
28418 + $width = '512';
28419 + $height = '512';
28420 + }
28421 + elseif($image_size == '1024x1024')
28422 + {
28423 + $width = '1024';
28424 + $height = '1024';
28425 + }
28426 + else
28427 + {
28428 + $width = '512';
28429 + $height = '512';
28430 + }
28431 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Stable images'));
28432 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28433 + {
28434 + $api_service = aiomatic_get_api_service($token, $model);
28435 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock Stable image');
28436 + }
28437 + $aierror = '';
28438 + $response_text = aiomatic_generate_stability_image($prompt, $height, $width, 'omniBlocksWriterStableImage', 0, false, $aierror, false, $model, '', 0);
28439 + if($response_text !== false && isset($response_text[1]))
28440 + {
28441 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28442 + {
28443 + return array($response_text[1]);
28444 + }
28445 + $block_results[$block_id] = array($current_block['type'], $response_text[1]);
28446 + }
28447 + else
28448 + {
28449 + if($critical == '1')
28450 + {
28451 + aiomatic_log_to_file('Halting execution (Stable Diffusion error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28452 + return 'fail';
28453 + }
28454 + else
28455 + {
28456 + aiomatic_log_to_file('Error occurred in Stable Diffusion image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28457 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28458 + {
28459 + return array('');
28460 + }
28461 + $block_results[$block_id] = array($current_block['type'], '');
28462 + }
28463 + }
28464 + }
28465 + elseif($current_block['type'] == 'google_ai_image')
28466 + {
28467 + $prompt = $current_block['parameters']['prompt'];
28468 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28469 + if (empty($prompt))
28470 + {
28471 + if($critical == '1')
28472 + {
28473 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28474 + return 'fail';
28475 + }
28476 + else
28477 + {
28478 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28479 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28480 + {
28481 + return array('');
28482 + }
28483 + $block_results[$block_id] = array($current_block['type'], '');
28484 + continue;
28485 + }
28486 + }
28487 + $model = $current_block['parameters']['model'];
28488 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Google images'));
28489 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28490 + {
28491 + $api_service = aiomatic_get_api_service($token, $model);
28492 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ') for OmniBlock Google image');
28493 + }
28494 + $aierror = '';
28495 + $response_text = aiomatic_generate_image_google($prompt, $model, 'omniBlocksWriterGoogleImage', false, $ierror, false);
28496 + if($response_text !== false && isset($response_text[1]))
28497 + {
28498 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28499 + {
28500 + return array($response_text[1]);
28501 + }
28502 + $block_results[$block_id] = array($current_block['type'], $response_text[1]);
28503 + }
28504 + else
28505 + {
28506 + if($critical == '1')
28507 + {
28508 + aiomatic_log_to_file('Halting execution (Stable Diffusion error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28509 + return 'fail';
28510 + }
28511 + else
28512 + {
28513 + aiomatic_log_to_file('Error occurred in Stable Diffusion image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28514 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28515 + {
28516 + return array('');
28517 + }
28518 + $block_results[$block_id] = array($current_block['type'], '');
28519 + }
28520 + }
28521 + }
28522 + elseif($current_block['type'] == 'midjourney_ai_image')
28523 + {
28524 + $prompt = $current_block['parameters']['prompt'];
28525 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28526 + if (empty($prompt))
28527 + {
28528 + if($critical == '1')
28529 + {
28530 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28531 + return 'fail';
28532 + }
28533 + else
28534 + {
28535 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28536 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28537 + {
28538 + return array('');
28539 + }
28540 + $block_results[$block_id] = array($current_block['type'], '');
28541 + continue;
28542 + }
28543 + }
28544 + $image_size = $current_block['parameters']['image_size'];
28545 + if($image_size == '512x512')
28546 + {
28547 + $width = '512';
28548 + $height = '512';
28549 + }
28550 + elseif($image_size == '1024x1024')
28551 + {
28552 + $width = '1024';
28553 + $height = '1024';
28554 + }
28555 + elseif($image_size == '1024x1792')
28556 + {
28557 + $width = '1024';
28558 + $height = '1792';
28559 + }
28560 + elseif($image_size == '1792x1024')
28561 + {
28562 + $width = '1792';
28563 + $height = '1024';
28564 + }
28565 + else
28566 + {
28567 + $width = '512';
28568 + $height = '512';
28569 + }
28570 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Midjourney images'));
28571 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28572 + {
28573 + $api_service = aiomatic_get_api_service($token, $model);
28574 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock Midjourney image');
28575 + }
28576 + $aierror = '';
28577 + $response_text = aiomatic_generate_ai_image_midjourney($prompt, $width, $height, 'omniBlocksWriterMidjourneyImage', false, $aierror);
28578 + if($response_text !== false)
28579 + {
28580 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28581 + {
28582 + return array($response_text);
28583 + }
28584 + $block_results[$block_id] = array($current_block['type'], $response_text);
28585 + }
28586 + else
28587 + {
28588 + if($critical == '1')
28589 + {
28590 + aiomatic_log_to_file('Halting execution (Midjourney error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28591 + return 'fail';
28592 + }
28593 + else
28594 + {
28595 + aiomatic_log_to_file('Error occurred in Midjourney image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28596 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28597 + {
28598 + return array('');
28599 + }
28600 + $block_results[$block_id] = array($current_block['type'], '');
28601 + }
28602 + }
28603 + }
28604 + elseif($current_block['type'] == 'replicate_ai_image')
28605 + {
28606 + $prompt = $current_block['parameters']['prompt'];
28607 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28608 + if (empty($prompt))
28609 + {
28610 + if($critical == '1')
28611 + {
28612 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28613 + return 'fail';
28614 + }
28615 + else
28616 + {
28617 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28618 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28619 + {
28620 + return array('');
28621 + }
28622 + $block_results[$block_id] = array($current_block['type'], '');
28623 + continue;
28624 + }
28625 + }
28626 + $image_size = $current_block['parameters']['image_size'];
28627 + if($image_size == '512x512')
28628 + {
28629 + $width = '512';
28630 + $height = '512';
28631 + }
28632 + elseif($image_size == '1024x1024')
28633 + {
28634 + $width = '1024';
28635 + $height = '1024';
28636 + }
28637 + elseif($image_size == '1024x1792')
28638 + {
28639 + $width = '1024';
28640 + $height = '1792';
28641 + }
28642 + elseif($image_size == '1792x1024')
28643 + {
28644 + $width = '1792';
28645 + $height = '1024';
28646 + }
28647 + else
28648 + {
28649 + $width = '512';
28650 + $height = '512';
28651 + }
28652 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Replicate images'));
28653 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28654 + {
28655 + $api_service = aiomatic_get_api_service($token, $model);
28656 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock Replicate image');
28657 + }
28658 + $aierror = '';
28659 + $response_text = aiomatic_generate_replicate_image($prompt, $width, $height, 'omniBlocksWriterReplicateImage', false, $aierror);
28660 + if($response_text !== false)
28661 + {
28662 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28663 + {
28664 + return array($response_text);
28665 + }
28666 + $block_results[$block_id] = array($current_block['type'], $response_text);
28667 + }
28668 + else
28669 + {
28670 + if($critical == '1')
28671 + {
28672 + aiomatic_log_to_file('Halting execution (Replicate error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28673 + return 'fail';
28674 + }
28675 + else
28676 + {
28677 + aiomatic_log_to_file('Error occurred in Replicate image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28678 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28679 + {
28680 + return array('');
28681 + }
28682 + $block_results[$block_id] = array($current_block['type'], '');
28683 + }
28684 + }
28685 + }
28686 + elseif($current_block['type'] == 'ideogram_ai_image')
28687 + {
28688 + $prompt = $current_block['parameters']['prompt'];
28689 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
28690 + if (empty($prompt))
28691 + {
28692 + if($critical == '1')
28693 + {
28694 + aiomatic_log_to_file('Halting execution (empty image prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28695 + return 'fail';
28696 + }
28697 + else
28698 + {
28699 + aiomatic_log_to_file('Empty image prompt entered: ' . $url);
28700 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28701 + {
28702 + return array('');
28703 + }
28704 + $block_results[$block_id] = array($current_block['type'], '');
28705 + continue;
28706 + }
28707 + }
28708 + $image_size = $current_block['parameters']['image_size'];
28709 + if($image_size == '512x512')
28710 + {
28711 + $width = '512';
28712 + $height = '512';
28713 + }
28714 + elseif($image_size == '1024x1024')
28715 + {
28716 + $width = '1024';
28717 + $height = '1024';
28718 + }
28719 + elseif($image_size == '1024x1792')
28720 + {
28721 + $width = '1024';
28722 + $height = '1792';
28723 + }
28724 + elseif($image_size == '1792x1024')
28725 + {
28726 + $width = '1792';
28727 + $height = '1024';
28728 + }
28729 + else
28730 + {
28731 + $width = '512';
28732 + $height = '512';
28733 + }
28734 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Ideogram images'));
28735 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28736 + {
28737 + $api_service = aiomatic_get_api_service($token, $model);
28738 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock Ideogram image');
28739 + }
28740 + $aierror = '';
28741 + $response_text = aiomatic_generate_ideogram_image($prompt, $width, $height, 'omniBlocksWriterIdeogramImage', false, $aierror);
28742 + if($response_text !== false)
28743 + {
28744 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28745 + {
28746 + return array($response_text);
28747 + }
28748 + $block_results[$block_id] = array($current_block['type'], $response_text);
28749 + }
28750 + else
28751 + {
28752 + if($critical == '1')
28753 + {
28754 + aiomatic_log_to_file('Halting execution (Ideogram error), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28755 + return 'fail';
28756 + }
28757 + else
28758 + {
28759 + aiomatic_log_to_file('Error occurred in Ideogram image generator (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
28760 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28761 + {
28762 + return array('');
28763 + }
28764 + $block_results[$block_id] = array($current_block['type'], '');
28765 + }
28766 + }
28767 + }
28768 + elseif($current_block['type'] == 'stable_ai_video')
28769 + {
28770 + $image_url = $current_block['parameters']['image_url'];
28771 + $image_url = aiomatic_replace_omniblocks_data($image_url, $current_keyword, $kiwis, $block_results, $load_variables);
28772 + if (empty($image_url))
28773 + {
28774 + if($critical == '1')
28775 + {
28776 + aiomatic_log_to_file('Halting execution (empty video image_url entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28777 + return 'fail';
28778 + }
28779 + else
28780 + {
28781 + aiomatic_log_to_file('Empty video image_url entered: ' . $url);
28782 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28783 + {
28784 + return array('');
28785 + }
28786 + $block_results[$block_id] = array($current_block['type'], '');
28787 + continue;
28788 + }
28789 + }
28790 + $image_size = $current_block['parameters']['image_size'];
28791 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Stable video'));
28792 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28793 + {
28794 + $api_service = aiomatic_get_api_service($token, $model);
28795 + aiomatic_log_to_file('Calling ' . $api_service . ' for OmniBlock Stable video');
28796 + }
28797 + $aierror = '';
28798 + $response_text = aiomatic_generate_stability_video($image_url, $image_size, 'omniBlocksWriterStableVideo', 0, false, $aierror, false);
28799 + if($response_text !== false && isset($response_text[1]))
28800 + {
28801 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28802 + {
28803 + return array($response_text[1]);
28804 + }
28805 + $block_results[$block_id] = array($current_block['type'], $response_text[1]);
28806 + }
28807 + else
28808 + {
28809 + if($critical == '1')
28810 + {
28811 + aiomatic_log_to_file('Halting execution (Stable Diffusion video error), as critical block failed (ID ' . esc_html($block_id) . '): error: ' . $aierror);
28812 + return 'fail';
28813 + }
28814 + else
28815 + {
28816 + aiomatic_log_to_file('Error occurred in Stable Diffusion video generator (ID ' . esc_html($block_id) . '): error: ' . $aierror);
28817 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28818 + {
28819 + return array('');
28820 + }
28821 + $block_results[$block_id] = array($current_block['type'], '');
28822 + }
28823 + }
28824 + }
28825 + elseif($current_block['type'] == 'nlp_entities')
28826 + {
28827 + $input_text = $current_block['parameters']['input_text'];
28828 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
28829 + if (empty($input_text))
28830 + {
28831 + if($critical == '1')
28832 + {
28833 + aiomatic_log_to_file('Halting execution (empty input_text entered), as critical block failed (ID ' . esc_html($block_id) . ')');
28834 + return 'fail';
28835 + }
28836 + else
28837 + {
28838 + aiomatic_log_to_file('Empty input_text entered: ' . $url);
28839 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28840 + {
28841 + return array('', '');
28842 + }
28843 + $block_results[$block_id] = array($current_block['type'], '', '');
28844 + continue;
28845 + }
28846 + }
28847 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock NLP Entities With TextRazor'));
28848 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
28849 + {
28850 + aiomatic_log_to_file('Getting TextRazor NLP entities for keyword: ' . $input_text);
28851 + }
28852 + if(isset($aiomatic_Main_Settings['textrazor_key']) && trim($aiomatic_Main_Settings['textrazor_key']) != '')
28853 + {
28854 + try
28855 + {
28856 + if(!class_exists('TextRazor'))
28857 + {
28858 + require_once(dirname(__FILE__) . "/res/TextRazor.php");
28859 + }
28860 + TextRazorSettings::setApiKey(trim($aiomatic_Main_Settings['textrazor_key']));
28861 + $textrazor = new TextRazor();
28862 + $textrazor->addExtractor('entities');
28863 + $zresponse = $textrazor->analyze($input_text);
28864 + if (isset($zresponse['response']['entities']))
28865 + {
28866 + $query_words_tr = array();
28867 + $entity_details = array();
28868 + foreach ($zresponse['response']['entities'] as $entity)
28869 + {
28870 + if(isset($entity['entityEnglishId']))
28871 + {
28872 + $query_words_tr[] = $entity['entityEnglishId'];
28873 + }
28874 + else
28875 + {
28876 + $query_words_tr[] = $entity['entityId'];
28877 + }
28878 + if(isset($entity['id']))
28879 + {
28880 + unset($entity['id']);
28881 + }
28882 + if(isset($entity['matchingTokens']))
28883 + {
28884 + unset($entity['matchingTokens']);
28885 + }
28886 + if(isset($entity['freebaseTypes']))
28887 + {
28888 + unset($entity['freebaseTypes']);
28889 + }
28890 + if(isset($entity['confidenceScore']))
28891 + {
28892 + unset($entity['confidenceScore']);
28893 + }
28894 + if(isset($entity['matchedText']))
28895 + {
28896 + unset($entity['matchedText']);
28897 + }
28898 + if(isset($entity['freebaseId']))
28899 + {
28900 + unset($entity['freebaseId']);
28901 + }
28902 + if(isset($entity['relevanceScore']))
28903 + {
28904 + unset($entity['relevanceScore']);
28905 + }
28906 + if(isset($entity['startingPos']))
28907 + {
28908 + unset($entity['startingPos']);
28909 + }
28910 + if(isset($entity['endingPos']))
28911 + {
28912 + unset($entity['endingPos']);
28913 + }
28914 + if(isset($entity['wikidataId']))
28915 + {
28916 + unset($entity['wikidataId']);
28917 + }
28918 + $entity_details[] = $entity;
28919 + }
28920 + if(count($query_words_tr) > 0)
28921 + {
28922 + $wr = implode(',', $query_words_tr);
28923 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28924 + {
28925 + return array($wr, json_encode($entity_details));
28926 + }
28927 + $block_results[$block_id] = array($current_block['type'], $wr, json_encode($entity_details));
28928 + }
28929 + else
28930 + {
28931 + if($critical == '1')
28932 + {
28933 + aiomatic_log_to_file('Halting execution (TextRazor no empty found), as critical block failed (ID ' . esc_html($block_id) . ')');
28934 + return 'fail';
28935 + }
28936 + else
28937 + {
28938 + aiomatic_log_to_file('Empty results found for TextRazor search: ' . print_r($zresponse, true));
28939 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28940 + {
28941 + return array('', '');
28942 + }
28943 + $block_results[$block_id] = array($current_block['type'], '', '');
28944 + continue;
28945 + }
28946 + }
28947 + }
28948 + else
28949 + {
28950 + if($critical == '1')
28951 + {
28952 + aiomatic_log_to_file('Halting execution (TextRazor no results found), as critical block failed (ID ' . esc_html($block_id) . ')');
28953 + return 'fail';
28954 + }
28955 + else
28956 + {
28957 + aiomatic_log_to_file('No results found for TextRazor search: ' . print_r($zresponse, true));
28958 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28959 + {
28960 + return array('', '');
28961 + }
28962 + $block_results[$block_id] = array($current_block['type'], '', '');
28963 + continue;
28964 + }
28965 + }
28966 + }
28967 + catch(Exception $e)
28968 + {
28969 + if($critical == '1')
28970 + {
28971 + aiomatic_log_to_file('Halting execution (TextRazor exception(2)), as critical block failed (ID ' . esc_html($block_id) . ')');
28972 + return 'fail';
28973 + }
28974 + else
28975 + {
28976 + aiomatic_log_to_file('Failed to search for keywords using TextRazor (2): ' . $e->getMessage());
28977 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28978 + {
28979 + return array('', '');
28980 + }
28981 + $block_results[$block_id] = array($current_block['type'], '', '');
28982 + continue;
28983 + }
28984 + }
28985 + }
28986 + else
28987 + {
28988 + if($critical == '1')
28989 + {
28990 + aiomatic_log_to_file('Halting execution (TextRazor API key not added), as critical block failed (ID ' . esc_html($block_id) . ')');
28991 + return 'fail';
28992 + }
28993 + else
28994 + {
28995 + aiomatic_log_to_file('TextRazor API key not added in plugin settings!');
28996 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
28997 + {
28998 + return array('', '');
28999 + }
29000 + $block_results[$block_id] = array($current_block['type'], '', '');
29001 + continue;
29002 + }
29003 + }
29004 + }
29005 + elseif($current_block['type'] == 'nlp_entities_neuron')
29006 + {
29007 + $input_text = $current_block['parameters']['input_text'];
29008 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
29009 + $engine = $current_block['parameters']['engine'];
29010 + $language = $current_block['parameters']['language'];
29011 + if (empty($input_text))
29012 + {
29013 + if($critical == '1')
29014 + {
29015 + aiomatic_log_to_file('Halting execution (empty input_text entered), as critical block failed (ID ' . esc_html($block_id) . ')');
29016 + return 'fail';
29017 + }
29018 + else
29019 + {
29020 + aiomatic_log_to_file('Empty input_text entered: ' . $url);
29021 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29022 + {
29023 + return array('', '', '', '', '', '', '', '', '');
29024 + }
29025 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29026 + continue;
29027 + }
29028 + }
29029 + if (empty($engine))
29030 + {
29031 + $engine = 'google.com';
29032 + }
29033 + if (empty($language))
29034 + {
29035 + $language = 'English';
29036 + }
29037 +
29038 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock NLP Entities With NeuronWriter'));
29039 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29040 + {
29041 + aiomatic_log_to_file('Getting NeuronWriter NLP entities for keyword: ' . $input_text);
29042 + }
29043 + if(isset($aiomatic_Main_Settings['neuron_key']) && trim($aiomatic_Main_Settings['neuron_key']) != '')
29044 + {
29045 + if(isset($aiomatic_Main_Settings['neuron_project']) && trim($aiomatic_Main_Settings['neuron_project']) != '')
29046 + {
29047 + try
29048 + {
29049 + if(!class_exists('NeuronWriterAPI'))
29050 + {
29051 + require_once (dirname(__FILE__) . "/res/NeuronWriter.php");
29052 + }
29053 + $appk = trim($aiomatic_Main_Settings['neuron_key']);
29054 + $projid = trim($aiomatic_Main_Settings['neuron_project']);
29055 + $neuronAPI = new NeuronWriterAPI($appk);
29056 + $response = $neuronAPI->createNewQuery($projid, $input_text, $engine, $language);
29057 + $rp = json_decode($response);
29058 + if(!isset($rp->query))
29059 + {
29060 + if($critical == '1')
29061 + {
29062 + aiomatic_log_to_file('Halting execution (Failed to create new query in NeuronWriter), as critical block failed (ID ' . esc_html($block_id) . ')');
29063 + return 'fail';
29064 + }
29065 + else
29066 + {
29067 + aiomatic_log_to_file('Failed to create new query in NeuronWriter: ' . print_r($response, true));
29068 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29069 + {
29070 + return array('', '', '', '', '', '', '', '', '');
29071 + }
29072 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29073 + continue;
29074 + }
29075 + }
29076 + else
29077 + {
29078 + $queryId = $rp->query;
29079 + $timeout_max = 300;
29080 + $current_time = 0;
29081 + $qs = false;
29082 + while($current_time <= $timeout_max && $qs === false)
29083 + {
29084 + $qs = $neuronAPI->getQueryStatus($queryId);
29085 + if($qs === false)
29086 + {
29087 + sleep(3);
29088 + }
29089 + $current_time += 3;
29090 + }
29091 + if($current_time > $timeout_max && $qs === false)
29092 + {
29093 + if($critical == '1')
29094 + {
29095 + aiomatic_log_to_file('Halting execution (NeuronWriter timeout), as critical block failed (ID ' . esc_html($block_id) . ')');
29096 + return 'fail';
29097 + }
29098 + else
29099 + {
29100 + aiomatic_log_to_file('NeuronWriter timeout!');
29101 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29102 + {
29103 + return array('', '', '', '', '', '', '', '', '');
29104 + }
29105 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29106 + continue;
29107 + }
29108 + }
29109 + elseif($qs === false)
29110 + {
29111 + if($critical == '1')
29112 + {
29113 + aiomatic_log_to_file('Halting execution (NeuronWriter critical failure), as critical block failed (ID ' . esc_html($block_id) . ')');
29114 + return 'fail';
29115 + }
29116 + else
29117 + {
29118 + aiomatic_log_to_file('NeuronWriter critical failure');
29119 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29120 + {
29121 + return array('', '', '', '', '', '', '', '', '');
29122 + }
29123 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29124 + continue;
29125 + }
29126 + }
29127 + else
29128 + {
29129 + if(!isset($qs['terms_txt']['title']))
29130 + {
29131 + if($critical == '1')
29132 + {
29133 + aiomatic_log_to_file('Halting execution (NeuronWriter failed to interpret results), as critical block failed (ID ' . esc_html($block_id) . ')');
29134 + return 'fail';
29135 + }
29136 + else
29137 + {
29138 + aiomatic_log_to_file('NeuronWriter failed to interpret results: ' . print_r($qs, true));
29139 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29140 + {
29141 + return array('', '', '', '', '', '', '', '', '');
29142 + }
29143 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29144 + continue;
29145 + }
29146 + }
29147 + else
29148 + {
29149 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29150 + {
29151 + return array($qs['terms_txt']['title'], $qs['terms_txt']['desc_title'], $qs['terms_txt']['h1'], $qs['terms_txt']['h2'], $qs['terms_txt']['content_basic'], $qs['terms_txt']['content_basic_w_ranges'], $qs['terms_txt']['content_extended'], $qs['terms_txt']['content_extended_w_ranges'], $qs['terms_txt']['entities']);
29152 + }
29153 + $block_results[$block_id] = array($current_block['type'], $qs['terms_txt']['title'], $qs['terms_txt']['desc_title'], $qs['terms_txt']['h1'], $qs['terms_txt']['h2'], $qs['terms_txt']['content_basic'], $qs['terms_txt']['content_basic_w_ranges'], $qs['terms_txt']['content_extended'], $qs['terms_txt']['content_extended_w_ranges'], $qs['terms_txt']['entities']);
29154 + }
29155 + }
29156 + }
29157 + }
29158 + catch(Exception $e)
29159 + {
29160 + if($critical == '1')
29161 + {
29162 + aiomatic_log_to_file('Halting execution (NeuronWriter exception(2)), as critical block failed (ID ' . esc_html($block_id) . ')');
29163 + return 'fail';
29164 + }
29165 + else
29166 + {
29167 + aiomatic_log_to_file('Failed to search for keywords using NeuronWriter (2): ' . $e->getMessage());
29168 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29169 + {
29170 + return array('', '', '', '', '', '', '', '', '');
29171 + }
29172 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29173 + continue;
29174 + }
29175 + }
29176 + }
29177 + else
29178 + {
29179 + if($critical == '1')
29180 + {
29181 + aiomatic_log_to_file('Halting execution (NeuronWriter Project ID not added), as critical block failed (ID ' . esc_html($block_id) . ')');
29182 + return 'fail';
29183 + }
29184 + else
29185 + {
29186 + aiomatic_log_to_file('NeuronWriter Project ID not added in plugin settings!');
29187 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29188 + {
29189 + return array('', '', '', '', '', '', '', '', '');
29190 + }
29191 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29192 + continue;
29193 + }
29194 + }
29195 + }
29196 + else
29197 + {
29198 + if($critical == '1')
29199 + {
29200 + aiomatic_log_to_file('Halting execution (NeuronWriter API key not added), as critical block failed (ID ' . esc_html($block_id) . ')');
29201 + return 'fail';
29202 + }
29203 + else
29204 + {
29205 + aiomatic_log_to_file('NeuronWriter API key not added in plugin settings!');
29206 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29207 + {
29208 + return array('', '', '', '', '', '', '', '', '');
29209 + }
29210 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '');
29211 + continue;
29212 + }
29213 + }
29214 + }
29215 + elseif($current_block['type'] == 'tts_openai')
29216 + {
29217 + $input_text = $current_block['parameters']['input_text'];
29218 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
29219 + $open_model_id = $current_block['parameters']['model'];
29220 + $open_voice = $current_block['parameters']['voice'];
29221 + $open_format = $current_block['parameters']['output'];
29222 + $stability = $current_block['parameters']['stability'];
29223 + if(empty($input_text))
29224 + {
29225 + if($critical == '1')
29226 + {
29227 + aiomatic_log_to_file('Halting execution (empty input_text), as critical block failed (ID ' . esc_html($block_id) . ')');
29228 + return 'fail';
29229 + }
29230 + else
29231 + {
29232 + aiomatic_log_to_file('Empty input_text provided in TTS OpenAI (ID ' . esc_html($block_id) . ')');
29233 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29234 + {
29235 + return array('');
29236 + }
29237 + $block_results[$block_id] = array($current_block['type'], '');
29238 + }
29239 + }
29240 + else
29241 + {
29242 + $result = aiomatic_openai_voice_stream($token, $open_model_id, $open_voice, $open_format, $stability, $input_text);
29243 + if(is_array($result))
29244 + {
29245 + if($critical == '1')
29246 + {
29247 + aiomatic_log_to_file('Halting execution (OpenAI TTS error), as critical block failed (ID ' . esc_html($block_id) . ')');
29248 + return 'fail';
29249 + }
29250 + else
29251 + {
29252 + aiomatic_log_to_file('OpenAI TTS Failed (ID ' . esc_html($block_id) . '): ' . print_r($result, true));
29253 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29254 + {
29255 + return array('');
29256 + }
29257 + $block_results[$block_id] = array($current_block['type'], '');
29258 + }
29259 + }
29260 + else
29261 + {
29262 + $filename = uniqid() . '.' . $open_format;
29263 + $local_arr = aiomatic_copy_file_locally($result, $filename, '', false);
29264 + if(isset($local_arr[0]))
29265 + {
29266 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29267 + {
29268 + return array($local_arr[0]);
29269 + }
29270 + $block_results[$block_id] = array($current_block['type'], $local_arr[0]);
29271 + }
29272 + else
29273 + {
29274 + if($critical == '1')
29275 + {
29276 + aiomatic_log_to_file('Halting execution (OpenAI TTS failed), as critical block failed (ID ' . esc_html($block_id) . ')');
29277 + return 'fail';
29278 + }
29279 + else
29280 + {
29281 + aiomatic_log_to_file('OpenAI TTS Error (ID ' . esc_html($block_id) . '): ' . print_r($local_arr, true));
29282 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29283 + {
29284 + return array('');
29285 + }
29286 + $block_results[$block_id] = array($current_block['type'], '');
29287 + }
29288 + }
29289 + }
29290 + }
29291 + }
29292 + elseif($current_block['type'] == 'stt_openai')
29293 + {
29294 + $audio_url = $current_block['parameters']['audio_url'];
29295 + $audio_url = aiomatic_replace_omniblocks_data($audio_url, $current_keyword, $kiwis, $block_results, $load_variables);
29296 + $prompt = $current_block['parameters']['prompt'];
29297 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
29298 + $purpose = $current_block['parameters']['purpose'];
29299 + $model = $current_block['parameters']['model'];
29300 + $language = $current_block['parameters']['language'];
29301 + $temperature = $current_block['parameters']['temperature'];
29302 + if(empty($audio_url))
29303 + {
29304 + if($critical == '1')
29305 + {
29306 + aiomatic_log_to_file('Halting execution (empty audio_url), as critical block failed (ID ' . esc_html($block_id) . ')');
29307 + return 'fail';
29308 + }
29309 + else
29310 + {
29311 + aiomatic_log_to_file('Empty audio_url provided in STT OpenAI (ID ' . esc_html($block_id) . ')');
29312 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29313 + {
29314 + return array('');
29315 + }
29316 + $block_results[$block_id] = array($current_block['type'], '');
29317 + }
29318 + }
29319 + else
29320 + {
29321 + $result = aiomatic_generate_audio_convertion($prompt, $audio_url, $purpose, $model, $temperature, $language);
29322 + if(!is_array($result) || !isset($result['status']) || $result['status'] != 'success' || !isset($result['data']))
29323 + {
29324 + if($critical == '1')
29325 + {
29326 + aiomatic_log_to_file('Halting execution (OpenAI STT error), as critical block failed (ID ' . esc_html($block_id) . ')');
29327 + return 'fail';
29328 + }
29329 + else
29330 + {
29331 + aiomatic_log_to_file('OpenAI STT Failed (ID ' . esc_html($block_id) . '): ' . print_r($result, true));
29332 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29333 + {
29334 + return array('');
29335 + }
29336 + $block_results[$block_id] = array($current_block['type'], '');
29337 + }
29338 + }
29339 + else
29340 + {
29341 + $result = $result['data'];
29342 + if(!empty($result))
29343 + {
29344 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29345 + {
29346 + return array($result);
29347 + }
29348 + $block_results[$block_id] = array($current_block['type'], $result);
29349 + }
29350 + else
29351 + {
29352 + if($critical == '1')
29353 + {
29354 + aiomatic_log_to_file('Halting execution (OpenAI STT failed), as critical block failed (ID ' . esc_html($block_id) . ')');
29355 + return 'fail';
29356 + }
29357 + else
29358 + {
29359 + aiomatic_log_to_file('OpenAI STT Error (ID ' . esc_html($block_id) . '): ' . print_r($local_arr, true));
29360 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29361 + {
29362 + return array('');
29363 + }
29364 + $block_results[$block_id] = array($current_block['type'], '');
29365 + }
29366 + }
29367 + }
29368 + }
29369 + }
29370 + elseif($current_block['type'] == 'webhook_fire')
29371 + {
29372 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting OmniBlock Webhook Data'));
29373 + if(empty($omniWebhookID) || $omniWebhookID != $block_id)
29374 + {
29375 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29376 + {
29377 + aiomatic_log_to_file('Getting OmniBlock Webhook Data (this request was not called from a webhook, this OmniBlock will always return an empty string)');
29378 + }
29379 + $block_results[$block_id] = array($current_block['type'], array());
29380 + }
29381 + else
29382 + {
29383 + $api_key = trim($current_block['parameters']['api_key']);
29384 + if (!empty($api_key))
29385 + {
29386 + if(!isset($_REQUEST['apikey']))
29387 + {
29388 + aiomatic_log_to_file('Halting execution (OmniBLock Webhook API key not provided in request) (ID ' . esc_html($block_id) . ')');
29389 + return 'fail';
29390 + }
29391 + if($_REQUEST['apikey'] != $api_key)
29392 + {
29393 + aiomatic_log_to_file('Halting execution (OmniBLock Webhook invalid API key provided in request) (ID ' . esc_html($block_id) . ')');
29394 + return 'fail';
29395 + }
29396 + }
29397 + if (isset($_GET['apikey'])) {
29398 + unset($_GET['apikey']);
29399 + }
29400 + if (isset($_POST['apikey'])) {
29401 + unset($_POST['apikey']);
29402 + }
29403 + if (isset($_REQUEST['apikey'])) {
29404 + unset($_REQUEST['apikey']);
29405 + }
29406 + if (isset($_GET['omniblockid'])) {
29407 + unset($_GET['omniblockid']);
29408 + }
29409 + if (isset($_POST['omniblockid'])) {
29410 + unset($_POST['omniblockid']);
29411 + }
29412 + if (isset($_REQUEST['omniblockid'])) {
29413 + unset($_REQUEST['omniblockid']);
29414 + }
29415 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29416 + {
29417 + aiomatic_log_to_file('Getting OmniBlock Webhook Data (from inside a successful OmniBlock Webhook request)');
29418 + }
29419 + if (isset($_REQUEST['input']))
29420 + {
29421 + $block_results[$block_id] = array($current_block['type'], $_REQUEST['input']);
29422 + }
29423 + else
29424 + {
29425 + $block_results[$block_id] = array($current_block['type'], $_REQUEST);
29426 + }
29427 + }
29428 + }
29429 + elseif($current_block['type'] == 'ai_forms_omniblock')
29430 + {
29431 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting OmniBlock AI Forms Data'));
29432 + if(empty($omniWebhookID) || $omniWebhookID != $block_id)
29433 + {
29434 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29435 + {
29436 + aiomatic_log_to_file('This request was not called from an AI Form, this OmniBlock will always return an empty string');
29437 + }
29438 + $block_results[$block_id] = array($current_block['type'], array());
29439 + }
29440 + else
29441 + {
29442 + $api_key = trim($current_block['parameters']['api_key']);
29443 + if (!empty($api_key))
29444 + {
29445 + if(!isset($_REQUEST['apikey']))
29446 + {
29447 + aiomatic_log_to_file('Halting execution (OmniBLock AI Forms API key not provided in request) (ID ' . esc_html($block_id) . ')');
29448 + return 'fail';
29449 + }
29450 + if($_REQUEST['apikey'] != $api_key)
29451 + {
29452 + aiomatic_log_to_file('Halting execution (OmniBLock AI Forms invalid API key provided in request) (ID ' . esc_html($block_id) . ')');
29453 + return 'fail';
29454 + }
29455 + }
29456 + if (isset($_GET['apikey'])) {
29457 + unset($_GET['apikey']);
29458 + }
29459 + if (isset($_POST['apikey'])) {
29460 + unset($_POST['apikey']);
29461 + }
29462 + if (isset($_REQUEST['apikey'])) {
29463 + unset($_REQUEST['apikey']);
29464 + }
29465 + if (isset($_GET['omniblockid'])) {
29466 + unset($_GET['omniblockid']);
29467 + }
29468 + if (isset($_POST['omniblockid'])) {
29469 + unset($_POST['omniblockid']);
29470 + }
29471 + if (isset($_REQUEST['omniblockid'])) {
29472 + unset($_REQUEST['omniblockid']);
29473 + }
29474 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29475 + {
29476 + aiomatic_log_to_file('Getting OmniBlock AI Forms Data (from inside a successful OmniBlock AI Forms request)');
29477 + }
29478 + if (isset($_REQUEST['input']))
29479 + {
29480 + $block_results[$block_id] = array($current_block['type'], $_REQUEST['input']);
29481 + }
29482 + else
29483 + {
29484 + $block_results[$block_id] = array($current_block['type'], $_REQUEST);
29485 + }
29486 + }
29487 + }
29488 + elseif($current_block['type'] == 'ai_chatbot_omniblock')
29489 + {
29490 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Getting OmniBlock AI Chatbot Data'));
29491 + if(empty($omniWebhookID) || $omniWebhookID != $block_id)
29492 + {
29493 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29494 + {
29495 + aiomatic_log_to_file('This request was not called from an AI Chatbot, this OmniBlock will always return an empty string');
29496 + }
29497 + $block_results[$block_id] = array($current_block['type'], array());
29498 + }
29499 + else
29500 + {
29501 + if (isset($_GET['omniblockid'])) {
29502 + unset($_GET['omniblockid']);
29503 + }
29504 + if (isset($_POST['omniblockid'])) {
29505 + unset($_POST['omniblockid']);
29506 + }
29507 + if (isset($_REQUEST['omniblockid'])) {
29508 + unset($_REQUEST['omniblockid']);
29509 + }
29510 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29511 + {
29512 + aiomatic_log_to_file('Getting OmniBlock AI Chatbot Data (from inside a successful OmniBlock AI Chatbot request)');
29513 + }
29514 + if (isset($_REQUEST['input']))
29515 + {
29516 + $block_results[$block_id] = array($current_block['type'], $_REQUEST['input']);
29517 + }
29518 + else
29519 + {
29520 + $block_results[$block_id] = array($current_block['type'], $_REQUEST);
29521 + }
29522 + }
29523 + }
29524 + elseif($current_block['type'] == 'wp_event_trigger')
29525 + {
29526 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'OmniBlock WordPress Event Trigger'));
29527 + if(empty($omniWebhookID) || $omniWebhookID != $block_id)
29528 + {
29529 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29530 + {
29531 + aiomatic_log_to_file('OmniBlock WordPress Event Trigger (this request was not called from a WordPress Event, this OmniBlock will always return an empty string)');
29532 + }
29533 + $block_results[$block_id] = array($current_block['type'], array());
29534 + }
29535 + else
29536 + {
29537 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29538 + {
29539 + aiomatic_log_to_file('OmniBlock WordPress Event Trigger (running from inside a valid WordPress event context)');
29540 + }
29541 + $event_params = array_values($event_context ?? []);
29542 + $prepared_data = [];
29543 +
29544 + for ($i = 0; $i < 10; $i++) {
29545 + $key = 'event_param' . ($i + 1) . '_';
29546 + $value = $event_params[$i] ?? '';
29547 + if (!is_scalar($value)) {
29548 + $value = json_encode($value);
29549 + }
29550 + $prepared_data[$key] = $value;
29551 + }
29552 +
29553 + $block_results[$block_id] = array($current_block['type'], $prepared_data);
29554 + }
29555 + }
29556 + elseif($current_block['type'] == 'crawl_sites')
29557 + {
29558 + $url = $current_block['parameters']['url'];
29559 + $url = aiomatic_replace_omniblocks_data($url, $current_keyword, $kiwis, $block_results, $load_variables);
29560 + $url = preg_split('/\r\n|\r|\n/', trim($url));
29561 + $url = array_filter($url);
29562 + $max_chars = $current_block['parameters']['max_chars'];
29563 + if(empty($url))
29564 + {
29565 + if($critical == '1')
29566 + {
29567 + aiomatic_log_to_file('Halting execution (empty scraper url), as critical block failed (ID ' . esc_html($block_id) . ')');
29568 + return 'fail';
29569 + }
29570 + else
29571 + {
29572 + aiomatic_log_to_file('No URLs found in scraper input (ID ' . esc_html($block_id) . ')');
29573 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29574 + {
29575 + return array('');
29576 + }
29577 + $block_results[$block_id] = array($current_block['type'], '');
29578 + }
29579 + }
29580 + else
29581 + {
29582 + $url = stripslashes($url[array_rand($url)]);
29583 + $scrape_method = $current_block['parameters']['scrape_method'];
29584 + $scrape_selector = $current_block['parameters']['scrape_selector'];
29585 + $scrape_string = $current_block['parameters']['scrape_string'];
29586 + $strip_tags = $current_block['parameters']['strip_tags'];
29587 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock website scraping results'));
29588 + $scraped_data = '';
29589 + if (isset($aiomatic_Main_Settings['omni_caching']) && trim($aiomatic_Main_Settings['omni_caching']) == 'on')
29590 + {
29591 + if(isset($omni_cache['scrape' . $url . $scrape_method . $scrape_selector . $scrape_string]))
29592 + {
29593 + $scraped_data = $omni_cache['scrape' . $url . $scrape_method . $scrape_selector . $scrape_string];
29594 + }
29595 + }
29596 + if(empty($scraped_data))
29597 + {
29598 + $scraped_data = aiomatic_scrape_page($url, $scrape_method, $scrape_selector, $scrape_string);
29599 + }
29600 + if($scraped_data === false)
29601 + {
29602 + if($critical == '1')
29603 + {
29604 + aiomatic_log_to_file('Halting execution (scraping error), as critical block failed (ID ' . esc_html($block_id) . ')');
29605 + return 'fail';
29606 + }
29607 + else
29608 + {
29609 + aiomatic_log_to_file('Failed to scrape URL (ID ' . esc_html($block_id) . ')');
29610 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29611 + {
29612 + return array('');
29613 + }
29614 + $block_results[$block_id] = array($current_block['type'], '');
29615 + }
29616 + }
29617 + else
29618 + {
29619 + if (isset($aiomatic_Main_Settings['omni_caching']) && trim($aiomatic_Main_Settings['omni_caching']) == 'on')
29620 + {
29621 + if(!isset($omni_cache['scrape' . $url . $scrape_method . $scrape_selector . $scrape_string]))
29622 + {
29623 + $omni_cache['scrape' . $url . $scrape_method . $scrape_selector . $scrape_string] = $scraped_data;
29624 + }
29625 + }
29626 + if($strip_tags == '1')
29627 + {
29628 + $scraped_data = wp_strip_all_tags($scraped_data);
29629 + }
29630 + else
29631 + {
29632 + $scraped_data = aiomatic_fix_relative_links($scraped_data, $url);
29633 + }
29634 + if(!empty($max_chars) && is_numeric($max_chars))
29635 + {
29636 + $scraped_data = (aiomatic_strlen($scraped_data) > intval($max_chars)) ? aiomatic_substr($scraped_data, 0, intval($max_chars)) : $scraped_data;
29637 + }
29638 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29639 + {
29640 + return array($scraped_data, aiomatic_removeDuplicateNewLines(wp_strip_all_tags($scraped_data)));
29641 + }
29642 + $block_results[$block_id] = array($current_block['type'], $scraped_data);
29643 + }
29644 + }
29645 + }
29646 + elseif($current_block['type'] == 'crawl_rss')
29647 + {
29648 + $final_res = '';
29649 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
29650 + {
29651 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
29652 + }
29653 + else
29654 + {
29655 + $frequency_penalty = '';
29656 + }
29657 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
29658 + {
29659 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
29660 + }
29661 + else
29662 + {
29663 + $presence_penalty = '';
29664 + }
29665 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
29666 + {
29667 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
29668 + }
29669 + else
29670 + {
29671 + $max_p = '';
29672 + }
29673 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
29674 + {
29675 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
29676 + }
29677 + else
29678 + {
29679 + $temperature = '';
29680 + }
29681 + $rss_feeds = array();
29682 + $url = $current_block['parameters']['url'];
29683 + $url = aiomatic_replace_omniblocks_data($url, $current_keyword, $kiwis, $block_results, $load_variables);
29684 + $scrape = $current_block['parameters']['scrape'];
29685 + $scrape_method = $current_block['parameters']['scrape_method'];
29686 + $max_chars = $current_block['parameters']['max_chars'];
29687 + $prompt = $current_block['parameters']['prompt'];
29688 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
29689 + $model = $current_block['parameters']['model'];
29690 + $assistant_id = $current_block['parameters']['assistant_id'];
29691 + if (empty($url))
29692 + {
29693 + if($critical == '1')
29694 + {
29695 + aiomatic_log_to_file('Halting execution (empty RSS URL entered), as critical block failed (ID ' . esc_html($block_id) . ')');
29696 + return 'fail';
29697 + }
29698 + else
29699 + {
29700 + aiomatic_log_to_file('Empty RSS feed URL entered: ' . $url);
29701 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29702 + {
29703 + return array('');
29704 + }
29705 + $block_results[$block_id] = array($current_block['type'], '');
29706 + continue;
29707 + }
29708 + }
29709 + $template = $current_block['parameters']['template'];
29710 + $template = aiomatic_replace_omniblocks_data($template, $current_keyword, $kiwis, $block_results, $load_variables);
29711 + if (empty($template))
29712 + {
29713 + if($critical == '1')
29714 + {
29715 + aiomatic_log_to_file('Halting execution (empty RSS template entered), as critical block failed (ID ' . esc_html($block_id) . ')');
29716 + return 'fail';
29717 + }
29718 + else
29719 + {
29720 + aiomatic_log_to_file('Empty RSS feed template entered: ' . $url);
29721 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29722 + {
29723 + return array('');
29724 + }
29725 + $block_results[$block_id] = array($current_block['type'], '');
29726 + continue;
29727 + }
29728 + }
29729 + $max_items = $current_block['parameters']['max_items'];
29730 + if(!empty($max_items))
29731 + {
29732 + $max_items = intval($max_items);
29733 + }
29734 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock RSS feed scraping results'));
29735 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
29736 + aiomatic_log_to_file('Trying to parse RSS feed items: ' . $url);
29737 + }
29738 + try
29739 + {
29740 + if(!class_exists('SimplePie_Autoloader', false))
29741 + {
29742 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
29743 + }
29744 + }
29745 + catch(Exception $e)
29746 + {
29747 + if($critical == '1')
29748 + {
29749 + aiomatic_log_to_file('Halting execution (failed to load RSS parser), as critical block failed (ID ' . esc_html($block_id) . ')');
29750 + return 'fail';
29751 + }
29752 + else
29753 + {
29754 + aiomatic_log_to_file('Failed to load RSS parser (ID ' . esc_html($block_id) . ')');
29755 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29756 + {
29757 + return array('');
29758 + }
29759 + $block_results[$block_id] = array($current_block['type'], '');
29760 + continue;
29761 + }
29762 + }
29763 + $items = false;
29764 + if (isset($aiomatic_Main_Settings['omni_caching']) && trim($aiomatic_Main_Settings['omni_caching']) == 'on')
29765 + {
29766 + if(isset($omni_cache['rss' . $url]))
29767 + {
29768 + $items = $omni_cache['rss' . $url];
29769 + }
29770 + }
29771 + if(empty($items))
29772 + {
29773 + $feed = new SimplePie();
29774 + $feed->set_timeout(120);
29775 + $feed->set_feed_url($url);
29776 + $feed->enable_cache(false);
29777 + $feed->strip_htmltags(false);
29778 + $feed->init();
29779 + $feed->handle_content_type();
29780 + if ($feed->error())
29781 + {
29782 + if($critical == '1')
29783 + {
29784 + aiomatic_log_to_file('Halting execution (error in parsing RSS feed), as critical block failed (ID ' . esc_html($block_id) . ')');
29785 + return 'fail';
29786 + }
29787 + else
29788 + {
29789 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $url);
29790 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29791 + {
29792 + return array('');
29793 + }
29794 + $block_results[$block_id] = array($current_block['type'], '');
29795 + continue;
29796 + }
29797 + }
29798 + $items = $feed->get_items();
29799 + if (isset($aiomatic_Main_Settings['omni_caching']) && trim($aiomatic_Main_Settings['omni_caching']) == 'on')
29800 + {
29801 + if(!isset($omni_cache['rss' . $url]))
29802 + {
29803 + $omni_cache['rss' . $url] = $items;
29804 + }
29805 + }
29806 + }
29807 + if(!empty($items))
29808 + {
29809 + foreach($items as $itemx)
29810 + {
29811 + $post_link = trim($itemx->get_permalink());
29812 + if ($fauthor = $itemx->get_author())
29813 + {
29814 + $user_name = $fauthor->get_name();
29815 + }
29816 + else
29817 + {
29818 + $user_name = '';
29819 + }
29820 + $feed_cats = array();
29821 + $varx = $itemx->get_categories();
29822 + if(is_array($varx))
29823 + {
29824 + foreach ($varx as $xcategory)
29825 + {
29826 + $feed_cats[] = $xcategory->get_label();
29827 + }
29828 + }
29829 + $post_cats = implode(',', $feed_cats);
29830 + $post_excerpt = $itemx->get_description();
29831 + $final_content = $itemx->get_content();
29832 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
29833 + }
29834 + }
29835 + $template_copy = '';
29836 + $processed = 0;
29837 + foreach($rss_feeds as $rtitle => $this_rss)
29838 + {
29839 + if(!empty($max_items) && $processed >= $max_items)
29840 + {
29841 + break;
29842 + }
29843 + $scraped_data = '';
29844 + if($scrape == '1' && strstr($template, '%%item_scraped_data%%') !== false)
29845 + {
29846 + $scraped_data = aiomatic_scrape_page($this_rss['url'], $scrape_method, 'auto', '');
29847 + if($scraped_data === false)
29848 + {
29849 + if($critical == '1')
29850 + {
29851 + aiomatic_log_to_file('Halting execution (RSS scraping error), as critical block failed (ID ' . esc_html($block_id) . ')');
29852 + return 'fail';
29853 + }
29854 + else
29855 + {
29856 + aiomatic_log_to_file('Failed to scrape URL ' . $this_rss['url'] . ' (ID ' . esc_html($block_id) . ')');
29857 + }
29858 + $scraped_data = '';
29859 + }
29860 + else
29861 + {
29862 + if(!empty($max_chars) && is_numeric($max_chars))
29863 + {
29864 + $scraped_data = (aiomatic_strlen($scraped_data) > intval($max_chars)) ? aiomatic_substr($scraped_data, 0, intval($max_chars)) : $scraped_data;
29865 + }
29866 + }
29867 + }
29868 + $template_copy = $template;
29869 + $template_copy = str_replace('%%item_counter%%', $processed + 1, $template_copy);
29870 + $template_copy = str_replace('%%item_title%%', $rtitle, $template_copy);
29871 + $template_copy = str_replace('%%item_content%%', $this_rss['content'], $template_copy);
29872 + $template_copy = str_replace('%%item_description%%', $this_rss['excerpt'], $template_copy);
29873 + $template_copy = str_replace('%%item_url%%', $this_rss['url'], $template_copy);
29874 + $template_copy = str_replace('%%item_author%%', $this_rss['author'], $template_copy);
29875 + $template_copy = str_replace('%%item_categories%%', $this_rss['cats'], $template_copy);
29876 + $template_copy = str_replace('%%item_scraped_data%%', $scraped_data, $template_copy);
29877 + if(!empty($prompt) && !empty($template_copy))
29878 + {
29879 + $prompt_copy = $prompt;
29880 + if(!strstr($prompt_copy, '%%current_item%%'))
29881 + {
29882 + $prompt_copy .= '\n' . $template_copy;
29883 + }
29884 + else
29885 + {
29886 + $prompt_copy = str_replace('%%current_item%%', $template_copy, $prompt_copy);
29887 + }
29888 + $max_tokens = aimogenpro_get_max_tokens($model);
29889 + $query_token_count = count(aiomatic_encode($prompt_copy));
29890 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt_copy, $query_token_count);
29891 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
29892 + {
29893 + $string_len = aiomatic_strlen($prompt_copy);
29894 + $string_len = $string_len / 2;
29895 + $string_len = intval(0 - $string_len);
29896 + $prompt_copy = aiomatic_substr($prompt_copy, 0, $string_len);
29897 + $prompt_copy = trim($prompt_copy);
29898 + $query_token_count = count(aiomatic_encode($prompt_copy));
29899 + $available_tokens = $max_tokens - $query_token_count;
29900 + }
29901 + $aierror = '';
29902 + $finish_reason = '';
29903 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
29904 + {
29905 + $api_service = aiomatic_get_api_service($token, $model);
29906 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock RSS item processing ID: ' . $block_id);
29907 + aiomatic_log_to_file('AI Prompt is: ' . $prompt_copy);
29908 + }
29909 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock AI RSS item content'));
29910 + $generated_text = aiomatic_generate_text($token, $model, $prompt_copy, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
29911 + if($generated_text === false || empty($generated_text))
29912 + {
29913 + if($critical == '1')
29914 + {
29915 + aiomatic_log_to_file('Halting execution (AI generated RSS text failed), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
29916 + return 'fail';
29917 + }
29918 + else
29919 + {
29920 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
29921 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29922 + {
29923 + return array('');
29924 + }
29925 + $block_results[$block_id] = array($current_block['type'], '');
29926 + }
29927 + }
29928 + else
29929 + {
29930 + if(!empty($generated_text))
29931 + {
29932 + $template_copy = $generated_text;
29933 + }
29934 + }
29935 + }
29936 + if(!empty($template_copy))
29937 + {
29938 + $final_res .= $template_copy . PHP_EOL;
29939 + }
29940 + $processed++;
29941 + }
29942 + if($final_res == '')
29943 + {
29944 + if($critical == '1')
29945 + {
29946 + aiomatic_log_to_file('Halting execution (no rss data returned), as critical block failed (ID ' . esc_html($block_id) . ')');
29947 + return 'fail';
29948 + }
29949 + else
29950 + {
29951 + aiomatic_log_to_file('No data returned after parsing RSS feed: ' . $url);
29952 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29953 + {
29954 + return array('');
29955 + }
29956 + $block_results[$block_id] = array($current_block['type'], '');
29957 + }
29958 + }
29959 + else
29960 + {
29961 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
29962 + {
29963 + return array($final_res);
29964 + }
29965 + $block_results[$block_id] = array($current_block['type'], $final_res);
29966 + }
29967 + }
29968 + elseif($current_block['type'] == 'google_search')
29969 + {
29970 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
29971 + {
29972 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
29973 + }
29974 + else
29975 + {
29976 + $frequency_penalty = '';
29977 + }
29978 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
29979 + {
29980 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
29981 + }
29982 + else
29983 + {
29984 + $presence_penalty = '';
29985 + }
29986 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
29987 + {
29988 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
29989 + }
29990 + else
29991 + {
29992 + $max_p = '';
29993 + }
29994 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
29995 + {
29996 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
29997 + }
29998 + else
29999 + {
30000 + $temperature = '';
30001 + }
30002 + $keyword = $current_block['parameters']['keyword'];
30003 + $keyword = aiomatic_replace_omniblocks_data($keyword, $current_keyword, $kiwis, $block_results, $load_variables);
30004 + $locale = $current_block['parameters']['locale'];
30005 + $locale = aiomatic_replace_omniblocks_data($locale, $current_keyword, $kiwis, $block_results, $load_variables);
30006 + $template = $current_block['parameters']['template'];
30007 + $template = aiomatic_replace_omniblocks_data($template, $current_keyword, $kiwis, $block_results, $load_variables);
30008 + $scrape = $current_block['parameters']['scrape'];
30009 + $scrape_method = $current_block['parameters']['scrape_method'];
30010 + $max_chars = $current_block['parameters']['max_chars'];
30011 + $prompt = $current_block['parameters']['prompt'];
30012 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
30013 + $model = $current_block['parameters']['model'];
30014 + $assistant_id = $current_block['parameters']['assistant_id'];
30015 + if(empty($locale))
30016 + {
30017 + if (isset($aiomatic_Main_Settings['internet_gl']) && $aiomatic_Main_Settings['internet_gl'] != '')
30018 + {
30019 + $locale = $aiomatic_Main_Settings['internet_gl'];
30020 + }
30021 + }
30022 + $final_res = '';
30023 + if (empty($template))
30024 + {
30025 + if($critical == '1')
30026 + {
30027 + aiomatic_log_to_file('Halting execution (empty search results template entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30028 + return 'fail';
30029 + }
30030 + else
30031 + {
30032 + aiomatic_log_to_file('Empty search results template entered: ' . $url);
30033 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30034 + {
30035 + return array('');
30036 + }
30037 + $block_results[$block_id] = array($current_block['type'], '');
30038 + continue;
30039 + }
30040 + }
30041 + $max_items = $current_block['parameters']['max_items'];
30042 + if(!empty($max_items))
30043 + {
30044 + $max_items = intval($max_items);
30045 + }
30046 + if (empty($keyword))
30047 + {
30048 + if($critical == '1')
30049 + {
30050 + aiomatic_log_to_file('Halting execution (empty Google search keyword entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30051 + return 'fail';
30052 + }
30053 + else
30054 + {
30055 + aiomatic_log_to_file('Empty Google search keyword entered: ' . $url);
30056 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30057 + {
30058 + return array('');
30059 + }
30060 + $block_results[$block_id] = array($current_block['type'], '');
30061 + continue;
30062 + }
30063 + }
30064 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Google Search results'));
30065 + $internet_rez = aiomatic_internet_result($keyword, true, $locale);
30066 + $processed = 0;
30067 + foreach($internet_rez as $emb)
30068 + {
30069 + if(!empty($max_items) && $processed >= $max_items)
30070 + {
30071 + break;
30072 + }
30073 + $scraped_data = '';
30074 + if($scrape == '1' && strstr($template, '%%item_scraped_data%%') !== false)
30075 + {
30076 + $scraped_data = aiomatic_scrape_page($emb['link'], $scrape_method, 'auto', '');
30077 + if($scraped_data === false)
30078 + {
30079 + if($critical == '1')
30080 + {
30081 + aiomatic_log_to_file('Halting execution (SERP scraping error), as critical block failed (ID ' . esc_html($block_id) . ')');
30082 + return 'fail';
30083 + }
30084 + else
30085 + {
30086 + aiomatic_log_to_file('Failed to scrape URL ' . $emb['link'] . ' (ID ' . esc_html($block_id) . ')');
30087 + }
30088 + $scraped_data = '';
30089 + }
30090 + else
30091 + {
30092 + if(!empty($max_chars) && is_numeric($max_chars))
30093 + {
30094 + $scraped_data = (aiomatic_strlen($scraped_data) > intval($max_chars)) ? aiomatic_substr($scraped_data, 0, intval($max_chars)) : $scraped_data;
30095 + }
30096 + }
30097 + }
30098 + $template_copy = $template;
30099 + $template_copy = str_replace('%%item_counter%%', $processed + 1, $template_copy);
30100 + $template_copy = str_replace('%%item_title%%', $emb['title'], $template_copy);
30101 + $template_copy = str_replace('%%item_snippet%%', $emb['snippet'], $template_copy);
30102 + $template_copy = str_replace('%%item_url%%', $emb['link'], $template_copy);
30103 + $template_copy = str_replace('%%item_scraped_data%%', $scraped_data, $template_copy);
30104 + if(!empty($prompt) && !empty($template_copy))
30105 + {
30106 + $prompt_copy = $prompt;
30107 + if(!strstr($prompt_copy, '%%current_item%%'))
30108 + {
30109 + $prompt_copy .= '\n' . $template_copy;
30110 + }
30111 + else
30112 + {
30113 + $prompt_copy = str_replace('%%current_item%%', $template_copy, $prompt_copy);
30114 + }
30115 + $max_tokens = aimogenpro_get_max_tokens($model);
30116 + $query_token_count = count(aiomatic_encode($prompt_copy));
30117 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt_copy, $query_token_count);
30118 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
30119 + {
30120 + $string_len = aiomatic_strlen($prompt_copy);
30121 + $string_len = $string_len / 2;
30122 + $string_len = intval(0 - $string_len);
30123 + $prompt_copy = aiomatic_substr($prompt_copy, 0, $string_len);
30124 + $prompt_copy = trim($prompt_copy);
30125 + $query_token_count = count(aiomatic_encode($prompt_copy));
30126 + $available_tokens = $max_tokens - $query_token_count;
30127 + }
30128 + $aierror = '';
30129 + $finish_reason = '';
30130 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
30131 + {
30132 + $api_service = aiomatic_get_api_service($token, $model);
30133 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for OmniBlock SERP item processing ID: ' . $block_id);
30134 + aiomatic_log_to_file('AI Prompt is: ' . $prompt_copy);
30135 + }
30136 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock AI SERP item content'));
30137 + $generated_text = aiomatic_generate_text($token, $model, $prompt_copy, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, false, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
30138 + if($generated_text === false || empty($generated_text))
30139 + {
30140 + if($critical == '1')
30141 + {
30142 + aiomatic_log_to_file('Halting execution (AI generated SERP text failed), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
30143 + return 'fail';
30144 + }
30145 + else
30146 + {
30147 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
30148 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30149 + {
30150 + return array('');
30151 + }
30152 + $block_results[$block_id] = array($current_block['type'], '');
30153 + }
30154 + }
30155 + else
30156 + {
30157 + if(!empty($generated_text))
30158 + {
30159 + $template_copy = $generated_text;
30160 + }
30161 + }
30162 + }
30163 + $final_res .= $template_copy . PHP_EOL;
30164 + $processed++;
30165 + }
30166 + if($final_res == '')
30167 + {
30168 + if($critical == '1')
30169 + {
30170 + aiomatic_log_to_file('Halting execution (no search results data found), as critical block failed (ID ' . esc_html($block_id) . ')');
30171 + return 'fail';
30172 + }
30173 + else
30174 + {
30175 + aiomatic_log_to_file('No data returned after parsing search results: ' . $keyword);
30176 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30177 + {
30178 + return array('');
30179 + }
30180 + $block_results[$block_id] = array($current_block['type'], '');
30181 + }
30182 + }
30183 + else
30184 + {
30185 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30186 + {
30187 + return array($final_res);
30188 + }
30189 + $block_results[$block_id] = array($current_block['type'], $final_res);
30190 + }
30191 + }
30192 + elseif($current_block['type'] == 'amazon_product')
30193 + {
30194 + $asin = $current_block['parameters']['asin'];
30195 + $asin = aiomatic_replace_omniblocks_data($asin, $current_keyword, $kiwis, $block_results, $load_variables);
30196 + $aff_id = $current_block['parameters']['aff_id'];
30197 + $aff_id = aiomatic_replace_omniblocks_data($aff_id, $current_keyword, $kiwis, $block_results, $load_variables);
30198 + $target_country = $current_block['parameters']['target_country'];
30199 + $target_country = aiomatic_replace_omniblocks_data($target_country, $current_keyword, $kiwis, $block_results, $load_variables);
30200 + if (empty($asin))
30201 + {
30202 + if($critical == '1')
30203 + {
30204 + aiomatic_log_to_file('Halting execution (empty search results asin entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30205 + return 'fail';
30206 + }
30207 + else
30208 + {
30209 + aiomatic_log_to_file('Empty search results asin entered');
30210 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30211 + {
30212 + return array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30213 + }
30214 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30215 + continue;
30216 + }
30217 + }
30218 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Amazon product details'));
30219 +
30220 + $max_prod = 1;
30221 + $amazresult = aiomatic_amazon_get_post($asin, trim($aff_id), $target_country, '', '', '', $max_prod, '1', array());
30222 + if(is_array($amazresult) && isset($amazresult['status']) && $amazresult['status'] == 'nothing')
30223 + {
30224 + if($critical == '1')
30225 + {
30226 + aiomatic_log_to_file('Halting execution (No results found for keyword search), as critical block failed (ID ' . esc_html($block_id) . ')');
30227 + return 'fail';
30228 + }
30229 + else
30230 + {
30231 + aiomatic_log_to_file('No results found for keyword search: ' . $amazon_kw);
30232 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30233 + {
30234 + return array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30235 + }
30236 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30237 + continue;
30238 + }
30239 + }
30240 + if(!is_array($amazresult))
30241 + {
30242 + if($critical == '1')
30243 + {
30244 + aiomatic_log_to_file('Halting execution (Empty response from Amazon), as critical block failed (ID ' . esc_html($block_id) . ')');
30245 + return 'fail';
30246 + }
30247 + else
30248 + {
30249 + aiomatic_log_to_file('Empty response from Amazon Reviews: ' . $amazon_kw);
30250 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30251 + {
30252 + return array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30253 + }
30254 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30255 + continue;
30256 + }
30257 + }
30258 + if(is_array($amazresult) && count($amazresult) == 0)
30259 + {
30260 + if($critical == '1')
30261 + {
30262 + aiomatic_log_to_file('Halting execution (Empty results found for keyword search), as critical block failed (ID ' . esc_html($block_id) . ')');
30263 + return 'fail';
30264 + }
30265 + else
30266 + {
30267 + aiomatic_log_to_file('Empty results found for keyword search: ' . $amazon_kw);
30268 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30269 + {
30270 + return array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30271 + }
30272 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
30273 + continue;
30274 + }
30275 + }
30276 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30277 + {
30278 + return array($amazresult[0]->offer_title, $amazresult[0]->offer_desc, $amazresult[0]->offer_url, $amazresult[0]->offer_price, $amazresult[0]->product_list_price, $amazresult[0]->offer_img, $amazresult[0]->cart_url, $amazresult[0]->product_imgs, $amazresult[0]->product_imgs_html, implode(PHP_EOL, $amazresult[0]->item_reviews), $amazresult[0]->item_score, $amazresult[0]->language, $amazresult[0]->edition, $amazresult[0]->pages_count, $amazresult[0]->publication_date, $amazresult[0]->contributors, $amazresult[0]->manufacturer, $amazresult[0]->binding, $amazresult[0]->product_group, $amazresult[0]->rating, $amazresult[0]->eans, $amazresult[0]->part_no, $amazresult[0]->model, $amazresult[0]->warranty, $amazresult[0]->color, $amazresult[0]->is_adult, $amazresult[0]->dimensions, $amazresult[0]->date, $amazresult[0]->size, $amazresult[0]->unit_count);
30279 + }
30280 + $block_results[$block_id] = array($current_block['type'], $amazresult[0]->offer_title, $amazresult[0]->offer_desc, $amazresult[0]->offer_url, $amazresult[0]->offer_price, $amazresult[0]->product_list_price, $amazresult[0]->offer_img, $amazresult[0]->cart_url, $amazresult[0]->product_imgs, $amazresult[0]->product_imgs_html, implode(PHP_EOL, $amazresult[0]->item_reviews), $amazresult[0]->item_score, $amazresult[0]->language, $amazresult[0]->edition, $amazresult[0]->pages_count, $amazresult[0]->publication_date, $amazresult[0]->contributors, $amazresult[0]->manufacturer, $amazresult[0]->binding, $amazresult[0]->product_group, $amazresult[0]->rating, $amazresult[0]->eans, $amazresult[0]->part_no, $amazresult[0]->model, $amazresult[0]->warranty, $amazresult[0]->color, $amazresult[0]->is_adult, $amazresult[0]->dimensions, $amazresult[0]->date, $amazresult[0]->size, $amazresult[0]->unit_count);
30281 + }
30282 + elseif($current_block['type'] == 'amazon_listing')
30283 + {
30284 + $asin = $current_block['parameters']['asin'];
30285 + $asin = aiomatic_replace_omniblocks_data($asin, $current_keyword, $kiwis, $block_results, $load_variables);
30286 + $aff_id = $current_block['parameters']['aff_id'];
30287 + $aff_id = aiomatic_replace_omniblocks_data($aff_id, $current_keyword, $kiwis, $block_results, $load_variables);
30288 + $target_country = $current_block['parameters']['target_country'];
30289 + $target_country = aiomatic_replace_omniblocks_data($target_country, $current_keyword, $kiwis, $block_results, $load_variables);
30290 + $max_product_count = $current_block['parameters']['max_product_count'];
30291 + $max_product_count = aiomatic_replace_omniblocks_data($max_product_count, $current_keyword, $kiwis, $block_results, $load_variables);
30292 + if(isset($current_block['parameters']['sort_results']))
30293 + {
30294 + $amaz_sort_results = $current_block['parameters']['sort_results'];
30295 + }
30296 + else
30297 + {
30298 + $amaz_sort_results = '';
30299 + }
30300 + if(strstr($max_product_count, '-') !== false)
30301 + {
30302 + $pr_arr = explode('-', $max_product_count);
30303 + $minx = trim($pr_arr[0]);
30304 + $maxx = trim($pr_arr[1]);
30305 + if(is_numeric($minx) && is_numeric($maxx))
30306 + {
30307 + $max_product_count = wp_rand(intval($minx), intval($maxx));
30308 + }
30309 + else
30310 + {
30311 + if(is_numeric($minx))
30312 + {
30313 + $max_product_count = intval($minx);
30314 + }
30315 + elseif(is_numeric($maxx))
30316 + {
30317 + $max_product_count = intval($maxx);
30318 + }
30319 + else
30320 + {
30321 + $max_product_count = 100;
30322 + }
30323 + }
30324 + }
30325 + $listing_template = $current_block['parameters']['listing_template'];
30326 + $listing_template = aiomatic_replace_omniblocks_data($listing_template, $current_keyword, $kiwis, $block_results, $load_variables);
30327 + if (empty($asin))
30328 + {
30329 + if($critical == '1')
30330 + {
30331 + aiomatic_log_to_file('Halting execution (empty search results asin entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30332 + return 'fail';
30333 + }
30334 + else
30335 + {
30336 + aiomatic_log_to_file('Empty search results asin entered');
30337 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30338 + {
30339 + return array('');
30340 + }
30341 + $block_results[$block_id] = array($current_block['type'], '');
30342 + continue;
30343 + }
30344 + }
30345 + if (empty($listing_template))
30346 + {
30347 + if($critical == '1')
30348 + {
30349 + aiomatic_log_to_file('Halting execution (empty search listing_template entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30350 + return 'fail';
30351 + }
30352 + else
30353 + {
30354 + aiomatic_log_to_file('Empty search listing_template entered');
30355 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30356 + {
30357 + return array('');
30358 + }
30359 + $block_results[$block_id] = array($current_block['type'], '');
30360 + continue;
30361 + }
30362 + }
30363 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock Amazon product listings'));
30364 + if(!empty($max_product_count) && is_numeric($max_product_count))
30365 + {
30366 + $max_prod = intval($max_product_count);
30367 + }
30368 + else
30369 + {
30370 + $max_prod = 100;
30371 + }
30372 + $amazresult = aiomatic_amazon_get_post($asin, trim($aff_id), $target_country, '', '', $amaz_sort_results, $max_prod, '1', array());
30373 + if(is_array($amazresult) && isset($amazresult['status']) && $amazresult['status'] == 'nothing')
30374 + {
30375 + if($critical == '1')
30376 + {
30377 + aiomatic_log_to_file('Halting execution (No results found for keyword search), as critical block failed (ID ' . esc_html($block_id) . ')');
30378 + return 'fail';
30379 + }
30380 + else
30381 + {
30382 + aiomatic_log_to_file('No results found for keyword search: ' . $amazon_kw);
30383 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30384 + {
30385 + return array('');
30386 + }
30387 + $block_results[$block_id] = array($current_block['type'], '');
30388 + continue;
30389 + }
30390 + }
30391 + if(!is_array($amazresult))
30392 + {
30393 + if($critical == '1')
30394 + {
30395 + aiomatic_log_to_file('Halting execution (Empty response from Amazon), as critical block failed (ID ' . esc_html($block_id) . ')');
30396 + return 'fail';
30397 + }
30398 + else
30399 + {
30400 + aiomatic_log_to_file('Empty response from Amazon Listing: ' . $amazon_kw);
30401 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30402 + {
30403 + return array('');
30404 + }
30405 + $block_results[$block_id] = array($current_block['type'], '');
30406 + continue;
30407 + }
30408 + }
30409 + if(is_array($amazresult) && count($amazresult) == 0)
30410 + {
30411 + if($critical == '1')
30412 + {
30413 + aiomatic_log_to_file('Halting execution (Empty results found for keyword search), as critical block failed (ID ' . esc_html($block_id) . ')');
30414 + return 'fail';
30415 + }
30416 + else
30417 + {
30418 + aiomatic_log_to_file('Empty results found for keyword search: ' . $amazon_kw);
30419 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30420 + {
30421 + return array('');
30422 + }
30423 + $block_results[$block_id] = array($current_block['type'], '');
30424 + continue;
30425 + }
30426 + }
30427 + $final_result = '';
30428 + $counter = 1;
30429 + foreach($amazresult as $myprod)
30430 + {
30431 + $copy_template = $listing_template;
30432 + $copy_template = str_replace('%%product_counter%%', $counter, $copy_template);
30433 + $copy_template = str_replace('%%product_title%%', trim(preg_replace('/\s+/', ' ', $myprod->offer_title)), $copy_template);
30434 + $copy_template = str_replace('%%product_description%%', trim(preg_replace('/\s+/', ' ', $myprod->offer_desc)), $copy_template);
30435 + $copy_template = str_replace('%%product_url%%', trim(preg_replace('/\s+/', ' ', $myprod->offer_url)), $copy_template);
30436 + $copy_template = str_replace('%%product_price%%', trim(preg_replace('/\s+/', ' ', $myprod->offer_price)), $copy_template);
30437 + $copy_template = str_replace('%%product_list_price%%', trim(preg_replace('/\s+/', ' ', $myprod->product_list_price)), $copy_template);
30438 + $copy_template = str_replace('%%product_image%%', trim(preg_replace('/\s+/', ' ', $myprod->offer_img)), $copy_template);
30439 + $copy_template = str_replace('%%product_cart_url%%', trim(preg_replace('/\s+/', ' ', $myprod->cart_url)), $copy_template);
30440 + $copy_template = str_replace('%%product_images_urls%%', trim(preg_replace('/\s+/', ' ', $myprod->product_imgs)), $copy_template);
30441 + $copy_template = str_replace('%%product_images%%', trim(preg_replace('/\s+/', ' ', $myprod->product_imgs_html)), $copy_template);
30442 + $copy_template = str_replace('%%product_reviews%%', trim(preg_replace('/\s+/', ' ', implode(PHP_EOL, $myprod->item_reviews))), $copy_template);
30443 + //new
30444 + $copy_template = str_replace('%%product_score%%', $myprod->item_score, $copy_template);
30445 + $copy_template = str_replace('%%product_edition%%', $myprod->edition, $copy_template);
30446 + $copy_template = str_replace('%%product_language%%', $myprod->language, $copy_template);
30447 + $copy_template = str_replace('%%product_pages_count%%', $myprod->pages_count, $copy_template);
30448 + $copy_template = str_replace('%%product_publication_date%%', $myprod->publication_date, $copy_template);
30449 + $copy_template = str_replace('%%product_contributors%%', $myprod->contributors, $copy_template);
30450 + $copy_template = str_replace('%%product_manufacturer%%', $myprod->manufacturer, $copy_template);
30451 + $copy_template = str_replace('%%product_binding%%', $myprod->binding, $copy_template);
30452 + $copy_template = str_replace('%%product_product_group%%', $myprod->product_group, $copy_template);
30453 + $copy_template = str_replace('%%product_rating%%', $myprod->rating, $copy_template);
30454 + $copy_template = str_replace('%%product_ean%%', $myprod->eans, $copy_template);
30455 + $copy_template = str_replace('%%product_part_no%%', $myprod->part_no, $copy_template);
30456 + $copy_template = str_replace('%%product_model%%', $myprod->model, $copy_template);
30457 + $copy_template = str_replace('%%product_warranty%%', $myprod->warranty, $copy_template);
30458 + $copy_template = str_replace('%%product_color%%', $myprod->color, $copy_template);
30459 + $copy_template = str_replace('%%product_is_adult%%', $myprod->is_adult, $copy_template);
30460 + $copy_template = str_replace('%%product_dimensions%%', $myprod->dimensions, $copy_template);
30461 + $copy_template = str_replace('%%product_date%%', $myprod->date, $copy_template);
30462 + $copy_template = str_replace('%%product_size%%', $myprod->size, $copy_template);
30463 + $copy_template = str_replace('%%product_unit_count%%', $myprod->unit_count, $copy_template);
30464 + $counter++;
30465 + $final_result .= $copy_template . '\r\n';
30466 + }
30467 + $final_result = trim($final_result);
30468 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30469 + {
30470 + return array($final_result);
30471 + }
30472 + $block_results[$block_id] = array($current_block['type'], $final_result);
30473 + }
30474 + elseif($current_block['type'] == 'post_import')
30475 + {
30476 + $input_array = array();
30477 + $input_text = $current_block['parameters']['input_text'];
30478 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
30479 + $input_text = trim($input_text);
30480 + if (empty($input_text))
30481 + {
30482 + if($critical == '1')
30483 + {
30484 + aiomatic_log_to_file('Halting execution (empty post ID entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30485 + return 'fail';
30486 + }
30487 + else
30488 + {
30489 + aiomatic_log_to_file('Empty post ID entered');
30490 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30491 + {
30492 + return array('', '', '', '', '', '', '', '', '', '', '', '');
30493 + }
30494 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '');
30495 + continue;
30496 + }
30497 + }
30498 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock post importing search results'));
30499 + if(!is_numeric($input_text))
30500 + {
30501 + if(strstr($input_text, '=') === false)
30502 + {
30503 + $input_text = 's=' . $input_text;
30504 + }
30505 + if(aiomatic_substr($input_text, 0, 1) !== "&")
30506 + {
30507 + $input_text = '&' . $input_text;
30508 + }
30509 + parse_str($input_text, $input_array);
30510 + if(isset($input_array['unique_tag']) && !empty($input_array['unique_tag']))
30511 + {
30512 + $input_array['meta_key'] = 'aiomatic_unique_tag';
30513 + $input_array['meta_value'] = $input_array['unique_tag'];
30514 + $input_array['meta_compare'] = 'NOT EXISTS';
30515 + }
30516 + $the_query = new WP_Query($input_array);
30517 +
30518 + if ($the_query->have_posts()) {
30519 + $the_query->the_post();
30520 + $input_text = get_the_ID();
30521 + wp_reset_postdata();
30522 + }
30523 + else
30524 + {
30525 + if($critical == '1')
30526 + {
30527 + aiomatic_log_to_file('Halting execution (post search query not found), as critical block failed (ID ' . esc_html($block_id) . ')');
30528 + return 'fail';
30529 + }
30530 + else
30531 + {
30532 + aiomatic_log_to_file('Post search query not found: ' . trim($input_text));
30533 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30534 + {
30535 + return array('', '', '', '', '', '', '', '', '', '', '', '');
30536 + }
30537 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '');
30538 + continue;
30539 + }
30540 + }
30541 + }
30542 + $returned_post = get_post(trim($input_text));
30543 + if($returned_post === null)
30544 + {
30545 + if($critical == '1')
30546 + {
30547 + aiomatic_log_to_file('Halting execution (post ID not found), as critical block failed (ID ' . esc_html($block_id) . ')');
30548 + return 'fail';
30549 + }
30550 + else
30551 + {
30552 + aiomatic_log_to_file('Post ID not found: ' . trim($input_text));
30553 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30554 + {
30555 + return array('', '', '', '', '', '', '', '', '', '', '', '');
30556 + }
30557 + $block_results[$block_id] = array($current_block['type'], '', '', '', '', '', '', '', '', '', '', '', '');
30558 + continue;
30559 + }
30560 + }
30561 + $cats = array();
30562 + $categories = get_the_category($returned_post->ID);
30563 + foreach($categories as $thiscat)
30564 + {
30565 + $cats[] = $thiscat->slug;
30566 + }
30567 + $cats = implode(',', $cats);
30568 + $tags = array();
30569 + $categories = get_the_tags($returned_post->ID);
30570 + if(!is_wp_error($categories) && $categories !== false)
30571 + {
30572 + foreach($categories as $thiscat)
30573 + {
30574 + $tags[] = $thiscat->slug;
30575 + }
30576 + }
30577 + $tags = implode(',', $tags);
30578 + $the_img = get_the_post_thumbnail_url($returned_post->ID);
30579 + if($the_img === false)
30580 + {
30581 + $the_img = '';
30582 + }
30583 + if(isset($input_array['unique_tag']) && !empty($input_array['unique_tag']))
30584 + {
30585 + update_post_meta($returned_post->ID, 'aiomatic_unique_tag', $input_array['unique_tag']);
30586 + }
30587 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30588 + {
30589 + return array($returned_post->ID, get_post_permalink($returned_post->ID), $returned_post->post_title, $returned_post->post_content, $returned_post->post_excerpt, $cats, $tags, $returned_post->post_author, $returned_post->post_date, $returned_post->post_status, $returned_post->post_type, $the_img);
30590 + }
30591 + $block_results[$block_id] = array($current_block['type'], $returned_post->ID, get_post_permalink($returned_post->ID), $returned_post->post_title, $returned_post->post_content, $returned_post->post_excerpt, $cats, $tags, $returned_post->post_author, $returned_post->post_date, $returned_post->post_status, $returned_post->post_type, $the_img);
30592 + }
30593 + elseif($current_block['type'] == 'random_line')
30594 + {
30595 + $input_array = array();
30596 + $input_text = $current_block['parameters']['input_text'];
30597 + $input_text = aiomatic_replace_omniblocks_data($input_text, $current_keyword, $kiwis, $block_results, $load_variables);
30598 + $input_text = trim($input_text);
30599 + if (empty($input_text))
30600 + {
30601 + if($critical == '1')
30602 + {
30603 + aiomatic_log_to_file('Halting execution (empty input provided), as critical block failed (ID ' . esc_html($block_id) . ')');
30604 + return 'fail';
30605 + }
30606 + else
30607 + {
30608 + aiomatic_log_to_file('Empty input entered for random line OmniBlock');
30609 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30610 + {
30611 + return array('');
30612 + }
30613 + $block_results[$block_id] = array($current_block['type'], '');
30614 + continue;
30615 + }
30616 + }
30617 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock random line'));
30618 + $input_text_arr = preg_split('/\r\n|\r|\n/', $input_text);
30619 + $random_line = $input_text_arr[array_rand($input_text_arr)];
30620 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30621 + {
30622 + return array($random_line);
30623 + }
30624 + $block_results[$block_id] = array($current_block['type'], $random_line);
30625 + }
30626 + elseif($current_block['type'] == 'royalty_image')
30627 + {
30628 + $raw_img_list = array();
30629 + $full_result_list = array();
30630 + $query_words = $current_block['parameters']['input_text'];
30631 + $query_words = aiomatic_replace_omniblocks_data($query_words, $current_keyword, $kiwis, $block_results, $load_variables);
30632 + if (empty($query_words))
30633 + {
30634 + if($critical == '1')
30635 + {
30636 + aiomatic_log_to_file('Halting execution (empty keyword entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30637 + return 'fail';
30638 + }
30639 + else
30640 + {
30641 + aiomatic_log_to_file('Empty royalty free keyword entered');
30642 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30643 + {
30644 + return array('');
30645 + }
30646 + $block_results[$block_id] = array($current_block['type'], '');
30647 + continue;
30648 + }
30649 + }
30650 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
30651 + {
30652 + $img_attr = $aiomatic_Main_Settings['attr_text'];
30653 + }
30654 + else
30655 + {
30656 + $img_attr = '';
30657 + }
30658 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock royalty free image results'));
30659 + $temp_get_img = aiomatic_get_free_image($aiomatic_Main_Settings, $query_words, $img_attr, $attached_id, 10, false, $raw_img_list, array(), $full_result_list);
30660 + if(!empty($attached_id))
30661 + {
30662 + $additional_attachments[] = $attached_id;
30663 + }
30664 + if($temp_get_img == '' || $temp_get_img === false)
30665 + {
30666 + if($critical == '1')
30667 + {
30668 + aiomatic_log_to_file('Halting execution (failed to find royalty free image), as critical block failed (ID ' . esc_html($block_id) . ')');
30669 + return 'fail';
30670 + }
30671 + else
30672 + {
30673 + aiomatic_log_to_file('Failed to find royalty free image for: ' . $query_words);
30674 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30675 + {
30676 + return array('');
30677 + }
30678 + $block_results[$block_id] = array($current_block['type'], '');
30679 + continue;
30680 + }
30681 + }
30682 + else
30683 + {
30684 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30685 + {
30686 + return array($temp_get_img);
30687 + }
30688 + $block_results[$block_id] = array($current_block['type'], $temp_get_img);
30689 + }
30690 + }
30691 + elseif($current_block['type'] == 'youtube_video')
30692 + {
30693 + $query_words = $current_block['parameters']['input_text'];
30694 + $query_words = aiomatic_replace_omniblocks_data($query_words, $current_keyword, $kiwis, $block_results, $load_variables);
30695 + if (empty($query_words))
30696 + {
30697 + if($critical == '1')
30698 + {
30699 + aiomatic_log_to_file('Halting execution (empty video keyword entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30700 + return 'fail';
30701 + }
30702 + else
30703 + {
30704 + aiomatic_log_to_file('Empty YouTube video keyword entered');
30705 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30706 + {
30707 + return array('', '');
30708 + }
30709 + $block_results[$block_id] = array($current_block['type'], '', '');
30710 + continue;
30711 + }
30712 + }
30713 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock YouTube video results'));
30714 + $new_vid = aiomatic_get_video($query_words, '', array());
30715 + if($new_vid !== false)
30716 + {
30717 + if (isset($aiomatic_Main_Settings['player_width']) && $aiomatic_Main_Settings['player_width'] !== '') {
30718 + $vwidth = esc_attr($aiomatic_Main_Settings['player_width']);
30719 + }
30720 + else
30721 + {
30722 + $vwidth = 580;
30723 + }
30724 + if (isset($aiomatic_Main_Settings['player_height']) && $aiomatic_Main_Settings['player_height'] !== '') {
30725 + $vheight = esc_attr($aiomatic_Main_Settings['player_height']);
30726 + }
30727 + else
30728 + {
30729 + $vheight = 380;
30730 + }
30731 + $vid_id = '';
30732 + preg_match_all('#"https:\/\/www\.youtube\.com\/embed\/([^"]*?)"#i', $new_vid, $zmatches);
30733 + if(isset($zmatches[1][0]))
30734 + {
30735 + $vid_id = 'https://www.youtube.com/watch?v=' . $zmatches[1][0];
30736 + }
30737 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30738 + {
30739 + return array($vid_id, $new_vid);
30740 + }
30741 + $block_results[$block_id] = array($current_block['type'], $vid_id, $new_vid);
30742 + }
30743 + else
30744 + {
30745 + if($critical == '1')
30746 + {
30747 + aiomatic_log_to_file('Halting execution (failed to find YouTube video), as critical block failed (ID ' . esc_html($block_id) . ')');
30748 + return 'fail';
30749 + }
30750 + else
30751 + {
30752 + aiomatic_log_to_file('Failed to find YouTube video for: ' . $query_words);
30753 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30754 + {
30755 + return array('', '');
30756 + }
30757 + $block_results[$block_id] = array($current_block['type'], '', '');
30758 + continue;
30759 + }
30760 + }
30761 + }
30762 + elseif($current_block['type'] == 'youtube_caption')
30763 + {
30764 + $url = $current_block['parameters']['url'];
30765 + $url = aiomatic_replace_omniblocks_data($url, $current_keyword, $kiwis, $block_results, $load_variables);
30766 + $url = aiomatic_extractYouTubeVideoId($url);
30767 + if (empty($url))
30768 + {
30769 + if($critical == '1')
30770 + {
30771 + aiomatic_log_to_file('Halting execution (empty YouTube URL entered), as critical block failed (ID ' . esc_html($block_id) . ')');
30772 + return 'fail';
30773 + }
30774 + else
30775 + {
30776 + aiomatic_log_to_file('Empty YouTube video URL entered: ' . $url);
30777 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30778 + {
30779 + return array('', '', '', '');
30780 + }
30781 + $block_results[$block_id] = array($current_block['type'], '', '', '', '');
30782 + continue;
30783 + }
30784 + }
30785 + $max_caption = $current_block['parameters']['max_caption'];
30786 + if(!empty($max_caption))
30787 + {
30788 + $max_caption = intval($max_caption);
30789 + }
30790 + $default_lang = array();
30791 + $returned_caption = '';
30792 + $vid_title = '';
30793 + $vid_desc = '';
30794 + $youtube_thumb = '';
30795 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating OmniBlock YouTube caption results'));
30796 + $za_video_page = '';
30797 + $ch = curl_init();
30798 + if ($ch !== FALSE)
30799 + {
30800 + if (isset($aiomatic_Main_Settings['proxy_url']) && $aiomatic_Main_Settings['proxy_url'] != '' && $aiomatic_Main_Settings['proxy_url'] != 'disable' && $aiomatic_Main_Settings['proxy_url'] != 'disabled') {
30801 + $prx = explode(',', $aiomatic_Main_Settings['proxy_url']);
30802 + $randomness = array_rand($prx);
30803 + curl_setopt( $ch, CURLOPT_PROXY, trim($prx[$randomness]));
30804 + if (isset($aiomatic_Main_Settings['proxy_auth']) && $aiomatic_Main_Settings['proxy_auth'] != '')
30805 + {
30806 + $prx_auth = explode(',', $aiomatic_Main_Settings['proxy_auth']);
30807 + if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '')
30808 + {
30809 + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));
30810 + }
30811 + }
30812 + }
30813 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
30814 + if (isset($aiomatic_Main_Settings['max_timeout']) && $aiomatic_Main_Settings['max_timeout'] != '')
30815 + {
30816 + $ztime = intval($aiomatic_Main_Settings['max_timeout']);
30817 + }
30818 + else
30819 + {
30820 + $ztime = 300;
30821 + }
30822 + if (isset($aiomatic_Main_Settings['youtube_cookies']) && $aiomatic_Main_Settings['youtube_cookies'] != '')
30823 + {
30824 + curl_setopt($ch, CURLOPT_COOKIE, $aiomatic_Main_Settings['youtube_cookies']);
30825 + }
30826 + curl_setopt($ch, CURLOPT_TIMEOUT, $ztime);
30827 + curl_setopt($ch, CURLOPT_HTTPGET, 1);
30828 + curl_setopt($ch, CURLOPT_REFERER, get_site_url());
30829 + curl_setopt($ch, CURLOPT_URL, $url);
30830 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
30831 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
30832 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
30833 + $za_video_page = curl_exec($ch);
30834 + if($za_video_page === false)
30835 + {
30836 + if($critical == '1')
30837 + {
30838 + aiomatic_log_to_file('Halting execution (failed to download video URL), as critical block failed (ID ' . esc_html($block_id) . ')');
30839 + return 'fail';
30840 + }
30841 + else
30842 + {
30843 + aiomatic_log_to_file('Failed to download video URL: ' . $url);
30844 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30845 + {
30846 + return array('', '', '', '');
30847 + }
30848 + $block_results[$block_id] = array($current_block['type'], '', '', '', '');
30849 + continue;
30850 + }
30851 + }
30852 + curl_close($ch);
30853 + }
30854 + else
30855 + {
30856 + if($critical == '1')
30857 + {
30858 + aiomatic_log_to_file('Halting execution (failed to init curl), as critical block failed (ID ' . esc_html($block_id) . ')');
30859 + return 'fail';
30860 + }
30861 + else
30862 + {
30863 + aiomatic_log_to_file('Failed to init curl in YouTube caption importing: ' . $url);
30864 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30865 + {
30866 + return array('', '', '', '');
30867 + }
30868 + $block_results[$block_id] = array($current_block['type'], '', '', '', '');
30869 + continue;
30870 + }
30871 + }
30872 + if($za_video_page !== false)
30873 + {
30874 + preg_match_all('#<meta name="title" content="([^"]*?)">#i', $za_video_page, $zmatches);
30875 + if(isset($zmatches[1][0]))
30876 + {
30877 + $vid_title = $zmatches[1][0];
30878 + }
30879 + else
30880 + {
30881 + preg_match_all('#<title>([^"]*?) - YouTube<\/title>#i', $za_video_page, $zmatches);
30882 + if(isset($zmatches[1][0]))
30883 + {
30884 + $vid_title = $zmatches[1][0];
30885 + }
30886 + else
30887 + {
30888 + preg_match_all('#<meta property="og:title" content="([^"]*?)">#i', $za_video_page, $zmatches);
30889 + if(isset($zmatches[1][0]))
30890 + {
30891 + $vid_title = $zmatches[1][0];
30892 + }
30893 + else
30894 + {
30895 + preg_match_all('#<meta property="twitter:title" content="([^"]*?)">#i', $za_video_page, $zmatches);
30896 + if(isset($zmatches[1][0]))
30897 + {
30898 + $vid_title = $zmatches[1][0];
30899 + }
30900 + else
30901 + {
30902 + preg_match_all('#<meta itemprop="name" content="([^"]*?)">#i', $za_video_page, $zmatches);
30903 + if(isset($zmatches[1][0]))
30904 + {
30905 + $vid_title = $zmatches[1][0];
30906 + }
30907 + }
30908 + }
30909 + }
30910 + }
30911 + preg_match_all('#"description":{"simpleText":"([^"]*?)"#i', $za_video_page, $zmatches);
30912 + if(isset($zmatches[1][0]))
30913 + {
30914 + $vid_desc = $zmatches[1][0];
30915 + }
30916 + else
30917 + {
30918 + preg_match_all('#"attributedDescription":{"content":"([^"]*?)"#i', $za_video_page, $zmatches);
30919 + if(isset($zmatches[1][0]))
30920 + {
30921 + $vid_desc = $zmatches[1][0];
30922 + }
30923 + else
30924 + {
30925 + preg_match_all('#"attributedDescriptionBodyText":{"content":"([^"]*?)"#i', $za_video_page, $zmatches);
30926 + if(isset($zmatches[1][0]))
30927 + {
30928 + $vid_desc = $zmatches[1][0];
30929 + }
30930 + else
30931 + {
30932 + preg_match_all('#"shortDescription":"([^"]*?)"#i', $za_video_page, $zmatches);
30933 + if(isset($zmatches[1][0]))
30934 + {
30935 + $vid_desc = $zmatches[1][0];
30936 + }
30937 + }
30938 + }
30939 + }
30940 + $video_id = '';
30941 + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $amatches);
30942 + if(isset($amatches[1]))
30943 + {
30944 + $video_id = $amatches[1];
30945 + }
30946 + $caprez = false;
30947 + if(!empty($video_id))
30948 + {
30949 + try
30950 + {
30951 + $caprez = aiomatic_get_video_transcript($video_id);
30952 + }
30953 + catch(Exception $e)
30954 + {
30955 + try
30956 + {
30957 + $caprez = aiomatic_get_video_transcript_2($video_id);
30958 + }
30959 + catch (Exception $e)
30960 + {
30961 + }
30962 + }
30963 + }
30964 + if(is_array($caprez) && isset($caprez['text']))
30965 + {
30966 + $returned_caption = $caprez['text'];
30967 + }
30968 + }
30969 + if(empty($returned_caption))
30970 + {
30971 + if($critical == '1')
30972 + {
30973 + aiomatic_log_to_file('Halting execution (Failed to find subtitles for video URL), as critical block failed (ID ' . esc_html($block_id) . ')');
30974 + return 'fail';
30975 + }
30976 + else
30977 + {
30978 + aiomatic_log_to_file('Failed to find subtitles for video URL: ' . $url);
30979 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
30980 + {
30981 + return array('', '', '', '');
30982 + }
30983 + $block_results[$block_id] = array($current_block['type'], '', '', '', '');
30984 + continue;
30985 + }
30986 + }
30987 + preg_match_all('#<link rel="image_src" href="([^"]*?)">#i', $za_video_page, $zmatches);
30988 + if(isset($zmatches[1][0]))
30989 + {
30990 + $youtube_thumb = $zmatches[1][0];
30991 + }
30992 + else
30993 + {
30994 + preg_match_all('#<meta property="og:image" content="([^"]*?)">#i', $za_video_page, $zmatches);
30995 + if(isset($zmatches[1][0]))
30996 + {
30997 + $youtube_thumb = $zmatches[1][0];
30998 + }
30999 + else
31000 + {
31001 + preg_match_all('#<meta name="twitter:image" content="([^"]*?)">#i', $za_video_page, $zmatches);
31002 + if(isset($zmatches[1][0]))
31003 + {
31004 + $youtube_thumb = $zmatches[1][0];
31005 + }
31006 + else
31007 + {
31008 + preg_match_all('#<link itemprop="thumbnailUrl" href="([^"]*?)">#i', $za_video_page, $zmatches);
31009 + if(isset($zmatches[1][0]))
31010 + {
31011 + $youtube_thumb = $zmatches[1][0];
31012 + }
31013 + }
31014 + }
31015 + }
31016 + if(!empty($max_caption) && aiomatic_strlen($returned_caption) > $max_caption)
31017 + {
31018 + $returned_caption = aiomatic_substr($returned_caption, 0, $max_caption);
31019 + }
31020 + if(!empty($max_caption) && aiomatic_strlen($vid_desc) > $max_caption)
31021 + {
31022 + $vid_desc = aiomatic_substr($vid_desc, 0, $max_caption);
31023 + }
31024 + if($returned_caption == '')
31025 + {
31026 + if($critical == '1')
31027 + {
31028 + aiomatic_log_to_file('Halting execution (no YouTube caption data found), as critical block failed (ID ' . esc_html($block_id) . ')');
31029 + return 'fail';
31030 + }
31031 + else
31032 + {
31033 + aiomatic_log_to_file('No YouTube caption results returned: ' . $keyword);
31034 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31035 + {
31036 + return array('', '', '', '');
31037 + }
31038 + $block_results[$block_id] = array($current_block['type'], '', '', '', '');
31039 + }
31040 + }
31041 + else
31042 + {
31043 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31044 + {
31045 + return array($returned_caption, $vid_title, $vid_desc, $youtube_thumb);
31046 + }
31047 + $block_results[$block_id] = array($current_block['type'], $returned_caption, $vid_title, $vid_desc, $youtube_thumb);
31048 + }
31049 + }
31050 + elseif($current_block['type'] == 'send_email')
31051 + {
31052 + $my_post = array();
31053 + $email_title = $current_block['parameters']['email_title'];
31054 + $email_title = aiomatic_replace_omniblocks_data($email_title, $current_keyword, $kiwis, $block_results, $load_variables);
31055 + $email_content = $current_block['parameters']['email_content'];
31056 + $email_content = aiomatic_replace_omniblocks_data($email_content, $current_keyword, $kiwis, $block_results, $load_variables);
31057 + $email_recipient = $current_block['parameters']['email_recipient'];
31058 + $email_recipient = aiomatic_replace_omniblocks_data($email_recipient, $current_keyword, $kiwis, $block_results, $load_variables);
31059 + if ((empty($email_title) && empty($email_content)) || empty($email_recipient))
31060 + {
31061 + if($critical == '1')
31062 + {
31063 + aiomatic_log_to_file('Halting execution (empty email data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31064 + return 'fail';
31065 + }
31066 + else
31067 + {
31068 + aiomatic_log_to_file('Empty email data entered');
31069 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31070 + {
31071 + return array('');
31072 + }
31073 + $block_results[$block_id] = array($current_block['type']);
31074 + continue;
31075 + }
31076 + }
31077 + try
31078 + {
31079 + $admin_mail = get_option('admin_email');
31080 + $to = $email_recipient;
31081 + $subject = $email_title;
31082 + $message = $email_content;
31083 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
31084 + $headers[] = 'Reply-To: ' . $admin_mail;
31085 + $headers[] = 'X-Mailer: PHP/' . phpversion();
31086 + $headers[] = 'Content-Type: text/html';
31087 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
31088 + $sent = wp_mail($to, $subject, $message, $headers);
31089 + if($sent === false)
31090 + {
31091 + if($critical == '1')
31092 + {
31093 + aiomatic_log_to_file('Halting execution (failed to send email), as critical block failed (ID ' . esc_html($block_id) . ')');
31094 + return 'fail';
31095 + }
31096 + else
31097 + {
31098 + aiomatic_log_to_file('Failed to send email to: ' . $to);
31099 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31100 + {
31101 + return array('');
31102 + }
31103 + $block_results[$block_id] = array($current_block['type']);
31104 + continue;
31105 + }
31106 + }
31107 + else
31108 + {
31109 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31110 + {
31111 + return array('ok');
31112 + }
31113 + $block_results[$block_id] = array($current_block['type']);
31114 + $posts_inserted++;
31115 + }
31116 + }
31117 + catch (Exception $e)
31118 + {
31119 + if($critical == '1')
31120 + {
31121 + aiomatic_log_to_file('Halting execution (send mail exception), as critical block failed (ID ' . esc_html($block_id) . ')');
31122 + return 'fail';
31123 + }
31124 + else
31125 + {
31126 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
31127 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31128 + {
31129 + return array('');
31130 + }
31131 + $block_results[$block_id] = array($current_block['type']);
31132 + continue;
31133 + }
31134 + }
31135 + }
31136 + elseif($current_block['type'] == 'send_twitter')
31137 + {
31138 + $post_template = $current_block['parameters']['post_template'];
31139 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31140 + $featured_image = $current_block['parameters']['featured_image'];
31141 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
31142 + if ((empty($post_template) && empty($featured_image)))
31143 + {
31144 + if($critical == '1')
31145 + {
31146 + aiomatic_log_to_file('Halting execution (empty X (Twitter) post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31147 + return 'fail';
31148 + }
31149 + else
31150 + {
31151 + aiomatic_log_to_file('Empty X (Twitter) post data entered');
31152 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31153 + {
31154 + return array('');
31155 + }
31156 + $block_results[$block_id] = array($current_block['type']);
31157 + continue;
31158 + }
31159 + }
31160 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31161 + $return_me = aiomatic_post_to_twitter($card_type_found, $post_template, $featured_image);
31162 + if(isset($return_me['error']))
31163 + {
31164 + if($critical == '1')
31165 + {
31166 + aiomatic_log_to_file('Halting execution (X (Twitter) social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31167 + return 'fail';
31168 + }
31169 + else
31170 + {
31171 + aiomatic_log_to_file('X (Twitter) social posting failed: ' . $return_me['error']);
31172 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31173 + {
31174 + return array('');
31175 + }
31176 + $block_results[$block_id] = array($current_block['type']);
31177 + continue;
31178 + }
31179 + }
31180 + elseif(empty($return_me))
31181 + {
31182 + if($critical == '1')
31183 + {
31184 + aiomatic_log_to_file('Halting execution (X (Twitter) social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31185 + return 'fail';
31186 + }
31187 + else
31188 + {
31189 + aiomatic_log_to_file('Failed to parse social posting results to X (Twitter)');
31190 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31191 + {
31192 + return array('');
31193 + }
31194 + $block_results[$block_id] = array($current_block['type']);
31195 + continue;
31196 + }
31197 + }
31198 + else
31199 + {
31200 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31201 + {
31202 + return array('ok');
31203 + }
31204 + $block_results[$block_id] = array($current_block['type']);
31205 + $posts_inserted++;
31206 + }
31207 + }
31208 + elseif($current_block['type'] == 'send_threads')
31209 + {
31210 + $post_template = $current_block['parameters']['post_template'];
31211 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31212 + $featured_image = $current_block['parameters']['featured_image'];
31213 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
31214 + if ((empty($post_template) && empty($featured_image)))
31215 + {
31216 + if($critical == '1')
31217 + {
31218 + aiomatic_log_to_file('Halting execution (empty Threads post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31219 + return 'fail';
31220 + }
31221 + else
31222 + {
31223 + aiomatic_log_to_file('Empty Threads post data entered');
31224 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31225 + {
31226 + return array('');
31227 + }
31228 + $block_results[$block_id] = array($current_block['type']);
31229 + continue;
31230 + }
31231 + }
31232 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31233 + $return_me = aiomatic_post_to_threads($card_type_found, $post_template, $featured_image);
31234 + if(isset($return_me['error']))
31235 + {
31236 + if($critical == '1')
31237 + {
31238 + aiomatic_log_to_file('Halting execution (Threads social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31239 + return 'fail';
31240 + }
31241 + else
31242 + {
31243 + aiomatic_log_to_file('Threads social posting failed: ' . $return_me['error']);
31244 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31245 + {
31246 + return array('');
31247 + }
31248 + $block_results[$block_id] = array($current_block['type']);
31249 + continue;
31250 + }
31251 + }
31252 + elseif(empty($return_me))
31253 + {
31254 + if($critical == '1')
31255 + {
31256 + aiomatic_log_to_file('Halting execution (Threads social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31257 + return 'fail';
31258 + }
31259 + else
31260 + {
31261 + aiomatic_log_to_file('Failed to parse social posting results to Threads');
31262 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31263 + {
31264 + return array('');
31265 + }
31266 + $block_results[$block_id] = array($current_block['type']);
31267 + continue;
31268 + }
31269 + }
31270 + else
31271 + {
31272 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31273 + {
31274 + return array('ok');
31275 + }
31276 + $block_results[$block_id] = array($current_block['type']);
31277 + $posts_inserted++;
31278 + }
31279 + }
31280 + elseif($current_block['type'] == 'send_gmb')
31281 + {
31282 + $post_template = $current_block['parameters']['post_template'];
31283 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31284 + $featured_image = $current_block['parameters']['featured_image'];
31285 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
31286 + $page_to_post = $current_block['parameters']['page_to_post'];
31287 + if ((empty($post_template) && empty($featured_image)))
31288 + {
31289 + if($critical == '1')
31290 + {
31291 + aiomatic_log_to_file('Halting execution (empty Google My Business post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31292 + return 'fail';
31293 + }
31294 + else
31295 + {
31296 + aiomatic_log_to_file('Empty Google My Business post data entered');
31297 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31298 + {
31299 + return array('');
31300 + }
31301 + $block_results[$block_id] = array($current_block['type']);
31302 + continue;
31303 + }
31304 + }
31305 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31306 + $return_me = aiomatic_post_to_gmb($card_type_found, $post_template, $featured_image, $page_to_post);
31307 + if(isset($return_me['error']))
31308 + {
31309 + if($critical == '1')
31310 + {
31311 + aiomatic_log_to_file('Halting execution (Google My Business social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31312 + return 'fail';
31313 + }
31314 + else
31315 + {
31316 + aiomatic_log_to_file('Google My Business social posting failed: ' . $return_me['error']);
31317 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31318 + {
31319 + return array('');
31320 + }
31321 + $block_results[$block_id] = array($current_block['type']);
31322 + continue;
31323 + }
31324 + }
31325 + elseif(empty($return_me))
31326 + {
31327 + if($critical == '1')
31328 + {
31329 + aiomatic_log_to_file('Halting execution (Google My Business social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31330 + return 'fail';
31331 + }
31332 + else
31333 + {
31334 + aiomatic_log_to_file('Failed to parse social posting results to Google My Business');
31335 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31336 + {
31337 + return array('');
31338 + }
31339 + $block_results[$block_id] = array($current_block['type']);
31340 + continue;
31341 + }
31342 + }
31343 + else
31344 + {
31345 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31346 + {
31347 + return array('ok');
31348 + }
31349 + $block_results[$block_id] = array($current_block['type']);
31350 + $posts_inserted++;
31351 + }
31352 + }
31353 + elseif($current_block['type'] == 'send_community_youtube')
31354 + {
31355 + $post_template = $current_block['parameters']['post_template'];
31356 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31357 + $featured_image = $current_block['parameters']['featured_image'];
31358 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
31359 + $send_type = $current_block['parameters']['send_type'];
31360 + if(empty($send_type))
31361 + {
31362 + $send_type = 'text';
31363 + }
31364 + if ((empty($post_template) && empty($featured_image)))
31365 + {
31366 + if($critical == '1')
31367 + {
31368 + aiomatic_log_to_file('Halting execution (empty YouTube Community post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31369 + return 'fail';
31370 + }
31371 + else
31372 + {
31373 + aiomatic_log_to_file('Empty YouTube Community post data entered');
31374 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31375 + {
31376 + return array('');
31377 + }
31378 + $block_results[$block_id] = array($current_block['type']);
31379 + continue;
31380 + }
31381 + }
31382 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31383 + if($featured_image != '')
31384 + {
31385 + $media = array($featured_image);
31386 + }
31387 + else
31388 + {
31389 + $media = array();
31390 + }
31391 + $return_me = aiomatic_post_to_youtube_community($card_type_found, $post_template, $send_type, $media);
31392 + if(isset($return_me['error']))
31393 + {
31394 + if($critical == '1')
31395 + {
31396 + aiomatic_log_to_file('Halting execution YouTube Community social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31397 + return 'fail';
31398 + }
31399 + else
31400 + {
31401 + aiomatic_log_to_file('YouTube Community social posting failed: ' . $return_me['error']);
31402 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31403 + {
31404 + return array('');
31405 + }
31406 + $block_results[$block_id] = array($current_block['type']);
31407 + continue;
31408 + }
31409 + }
31410 + elseif(empty($return_me))
31411 + {
31412 + if($critical == '1')
31413 + {
31414 + aiomatic_log_to_file('Halting execution (YouTube Community social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31415 + return 'fail';
31416 + }
31417 + else
31418 + {
31419 + aiomatic_log_to_file('Failed to parse social posting results to YouTube Community');
31420 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31421 + {
31422 + return array('');
31423 + }
31424 + $block_results[$block_id] = array($current_block['type']);
31425 + continue;
31426 + }
31427 + }
31428 + else
31429 + {
31430 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31431 + {
31432 + return array('ok');
31433 + }
31434 + $block_results[$block_id] = array($current_block['type']);
31435 + $posts_inserted++;
31436 + }
31437 + }
31438 + elseif($current_block['type'] == 'send_reddit')
31439 + {
31440 + $title_template = $current_block['parameters']['title_template'];
31441 + $title_template = aiomatic_replace_omniblocks_data($title_template, $current_keyword, $kiwis, $block_results, $load_variables);
31442 + $post_template = $current_block['parameters']['post_template'];
31443 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31444 + $subreddit_to_post = $current_block['parameters']['subreddit_to_post'];
31445 + $subreddit_to_post = aiomatic_replace_omniblocks_data($subreddit_to_post, $current_keyword, $kiwis, $block_results, $load_variables);
31446 + $send_type = $current_block['parameters']['send_type'];
31447 + if(empty($send_type))
31448 + {
31449 + $send_type = 'self';
31450 + }
31451 + if ((empty($post_template) && empty($title_template)))
31452 + {
31453 + if($critical == '1')
31454 + {
31455 + aiomatic_log_to_file('Halting execution (empty Reddit post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31456 + return 'fail';
31457 + }
31458 + else
31459 + {
31460 + aiomatic_log_to_file('Empty Reddit post data entered');
31461 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31462 + {
31463 + return array('');
31464 + }
31465 + $block_results[$block_id] = array($current_block['type']);
31466 + continue;
31467 + }
31468 + }
31469 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31470 + $return_me = aiomatic_post_to_reddit($card_type_found, $title_template, $post_template, $send_type, $subreddit_to_post);
31471 + if(isset($return_me['error']))
31472 + {
31473 + if($critical == '1')
31474 + {
31475 + aiomatic_log_to_file('Halting execution Reddit social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31476 + return 'fail';
31477 + }
31478 + else
31479 + {
31480 + aiomatic_log_to_file('Reddit social posting failed: ' . $return_me['error']);
31481 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31482 + {
31483 + return array('');
31484 + }
31485 + $block_results[$block_id] = array($current_block['type']);
31486 + continue;
31487 + }
31488 + }
31489 + elseif(empty($return_me))
31490 + {
31491 + if($critical == '1')
31492 + {
31493 + aiomatic_log_to_file('Halting execution (Reddit social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31494 + return 'fail';
31495 + }
31496 + else
31497 + {
31498 + aiomatic_log_to_file('Failed to parse social posting results to Reddit');
31499 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31500 + {
31501 + return array('');
31502 + }
31503 + $block_results[$block_id] = array($current_block['type']);
31504 + continue;
31505 + }
31506 + }
31507 + else
31508 + {
31509 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31510 + {
31511 + return array('ok');
31512 + }
31513 + $block_results[$block_id] = array($current_block['type']);
31514 + $posts_inserted++;
31515 + }
31516 + }
31517 + elseif($current_block['type'] == 'god_mode')
31518 + {
31519 + if (isset($aiomatic_Main_Settings['omni_fpenalty']) && trim($aiomatic_Main_Settings['omni_fpenalty']) != '')
31520 + {
31521 + $frequency_penalty = floatval($aiomatic_Main_Settings['omni_fpenalty']);
31522 + }
31523 + else
31524 + {
31525 + $frequency_penalty = '';
31526 + }
31527 + if (isset($aiomatic_Main_Settings['omni_ppenalty']) && trim($aiomatic_Main_Settings['omni_ppenalty']) != '')
31528 + {
31529 + $presence_penalty = floatval($aiomatic_Main_Settings['omni_ppenalty']);
31530 + }
31531 + else
31532 + {
31533 + $presence_penalty = '';
31534 + }
31535 + if (isset($aiomatic_Main_Settings['omni_top_p']) && trim($aiomatic_Main_Settings['omni_top_p']) != '')
31536 + {
31537 + $max_p = floatval($aiomatic_Main_Settings['omni_top_p']);
31538 + }
31539 + else
31540 + {
31541 + $max_p = '';
31542 + }
31543 + if (isset($aiomatic_Main_Settings['omni_temperature']) && trim($aiomatic_Main_Settings['omni_temperature']) != '')
31544 + {
31545 + $temperature = floatval($aiomatic_Main_Settings['omni_temperature']);
31546 + }
31547 + else
31548 + {
31549 + $temperature = '';
31550 + }
31551 + $prompt = $current_block['parameters']['prompt'];
31552 + $prompt = aiomatic_replace_omniblocks_data($prompt, $current_keyword, $kiwis, $block_results, $load_variables);
31553 + if (empty($prompt))
31554 + {
31555 + if($critical == '1')
31556 + {
31557 + aiomatic_log_to_file('Halting execution (empty text prompt entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31558 + return 'fail';
31559 + }
31560 + else
31561 + {
31562 + aiomatic_log_to_file('Empty text prompt entered, block ID: ' . $block_id);
31563 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31564 + {
31565 + return array('');
31566 + }
31567 + $block_results[$block_id] = array($current_block['type'], '');
31568 + continue;
31569 + }
31570 + }
31571 + $model = $current_block['parameters']['model'];
31572 + $assistant_id = $current_block['parameters']['assistant_id'];
31573 + $max_tokens = aimogenpro_get_max_tokens($model);
31574 + $query_token_count = count(aiomatic_encode($prompt));
31575 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
31576 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
31577 + {
31578 + $string_len = aiomatic_strlen($prompt);
31579 + $string_len = $string_len / 2;
31580 + $string_len = intval(0 - $string_len);
31581 + $prompt = aiomatic_substr($prompt, 0, $string_len);
31582 + $prompt = trim($prompt);
31583 + $query_token_count = count(aiomatic_encode($prompt));
31584 + $available_tokens = $max_tokens - $query_token_count;
31585 + }
31586 + $aierror = '';
31587 + $finish_reason = '';
31588 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
31589 + {
31590 + $api_service = aiomatic_get_api_service($token, $model);
31591 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for God Mode OmniBlock text ID: ' . $block_id);
31592 + aiomatic_log_to_file('AI Prompt is: ' . $prompt);
31593 + }
31594 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Calling OmniBlock AI God Mode'));
31595 + require_once (dirname(__FILE__) . "/aiomatic-god-mode-omniblock.php");
31596 + require_once (dirname(__FILE__) . "/aiomatic-god-mode-parser.php");
31597 + $generated_text = aiomatic_run_functions($token, $model, $prompt, $available_tokens, $temperature, $max_p, $presence_penalty, $frequency_penalty, true, 'omniBlocksWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false);
31598 + remove_filter('aiomatic_ai_functions', 'aiomatic_add_god_mode_omniblock', 999);
31599 + if($generated_text === false)
31600 + {
31601 + if($critical == '1')
31602 + {
31603 + aiomatic_log_to_file('Halting execution (failed to generate AI content), as critical block failed (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
31604 + return 'fail';
31605 + }
31606 + else
31607 + {
31608 + aiomatic_log_to_file('Failed to generate non-critical OmniBlock content using AI writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
31609 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31610 + {
31611 + return array('');
31612 + }
31613 + $block_results[$block_id] = array($current_block['type'], '');
31614 + }
31615 + }
31616 + else
31617 + {
31618 + if(empty($generated_text))
31619 + {
31620 + aiomatic_log_to_file('Empty content generated using AI OmniBlock writer (ID ' . esc_html($block_id) . '): ' . $model . ' - error: ' . $aierror);
31621 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31622 + {
31623 + return array('');
31624 + }
31625 + $block_results[$block_id] = array($current_block['type'], '');
31626 + }
31627 + else
31628 + {
31629 + if(is_string($generated_text))
31630 + {
31631 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31632 + {
31633 + return array($generated_text);
31634 + }
31635 + $block_results[$block_id] = array($current_block['type'], $generated_text);
31636 + }
31637 + else
31638 + {
31639 + $return_me = '';
31640 + if(isset($generated_text->tool_calls))
31641 + {
31642 + foreach($generated_text->tool_calls as $tcx)
31643 + {
31644 + $return_me .= 'Executed: ' . $tcx->function->name . '(' . $tcx->function->arguments->parameter_array . ')' . PHP_EOL;
31645 + }
31646 + }
31647 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31648 + {
31649 + return array($return_me);
31650 + }
31651 + $block_results[$block_id] = array($current_block['type'], $return_me);
31652 + }
31653 + }
31654 + }
31655 + }
31656 + elseif($current_block['type'] == 'send_webhook')
31657 + {
31658 + $webhook_url = $current_block['parameters']['webhook_url'];
31659 + $webhook_method = $current_block['parameters']['webhook_method'];
31660 + $content_type = $current_block['parameters']['content_type'];
31661 + $post_template = $current_block['parameters']['post_template'];
31662 + $headers_template = $current_block['parameters']['headers_template'];
31663 + $headers_template = aiomatic_replace_omniblocks_data($headers_template, $current_keyword, $kiwis, $block_results, $load_variables);
31664 + if ((empty($webhook_url)))
31665 + {
31666 + if($critical == '1')
31667 + {
31668 + aiomatic_log_to_file('Halting execution (empty webhook_url entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31669 + return 'fail';
31670 + }
31671 + else
31672 + {
31673 + aiomatic_log_to_file('Empty webhook_url entered');
31674 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31675 + {
31676 + return array('');
31677 + }
31678 + $block_results[$block_id] = array($current_block['type']);
31679 + continue;
31680 + }
31681 + }
31682 + $urlParsed = wp_parse_url( $webhook_url, PHP_URL_HOST );
31683 + if ( filter_var( $webhook_url, FILTER_VALIDATE_URL ) === FALSE || empty( $urlParsed ) )
31684 + {
31685 + if($critical == '1')
31686 + {
31687 + aiomatic_log_to_file('Halting execution (invalid webhook_url entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31688 + return 'fail';
31689 + }
31690 + else
31691 + {
31692 + aiomatic_log_to_file('Invalid webhook_url entered ' . $webhook_url);
31693 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31694 + {
31695 + return array('');
31696 + }
31697 + $block_results[$block_id] = array($current_block['type']);
31698 + continue;
31699 + }
31700 + }
31701 + else if ( $content_type === 'JSON' && empty( json_decode( $post_template, TRUE ) ) )
31702 + {
31703 + if($critical == '1')
31704 + {
31705 + aiomatic_log_to_file('Halting execution (The JSON data must be valid), as critical block failed (ID ' . esc_html($block_id) . ')');
31706 + return 'fail';
31707 + }
31708 + else
31709 + {
31710 + aiomatic_log_to_file('The JSON data must be valid ' . $webhook_url);
31711 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31712 + {
31713 + return array('');
31714 + }
31715 + $block_results[$block_id] = array($current_block['type']);
31716 + continue;
31717 + }
31718 + }
31719 + $headers = [];
31720 + if(!empty($headers_template))
31721 + {
31722 + $headers_template_arr = preg_split('/\r\n|\r|\n/', trim($headers_template));
31723 + foreach($headers_template_arr as $arr_fr)
31724 + {
31725 + if(!empty(trim($arr_fr)) && strstr($arr_fr, '=>'))
31726 + {
31727 + $small_arr = explode('=>', $arr_fr);
31728 + $headers[] = trim($small_arr[0]) . ':' . trim($small_arr[1]);
31729 + }
31730 + }
31731 + }
31732 + $content_params = [];
31733 + if(!empty($post_template))
31734 + {
31735 + $post_template_arr = preg_split('/\r\n|\r|\n/', trim($post_template));
31736 + foreach($post_template_arr as $arr_fr)
31737 + {
31738 + if(!empty(trim($arr_fr)) && strstr($arr_fr, '=>'))
31739 + {
31740 + $small_arr = explode('=>', $arr_fr);
31741 + $small_arr[0] = aiomatic_replace_omniblocks_data($small_arr[0], $current_keyword, $kiwis, $block_results, $load_variables);
31742 + $small_arr[1] = aiomatic_replace_omniblocks_data($small_arr[1], $current_keyword, $kiwis, $block_results, $load_variables);
31743 + $content_params[trim($small_arr[0])] = trim($small_arr[1]);
31744 + }
31745 + }
31746 + }
31747 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31748 + $ch = curl_init();
31749 + if ($ch === false)
31750 + {
31751 + if($critical == '1')
31752 + {
31753 + aiomatic_log_to_file('Halting execution (failed to init curl), as critical block failed (ID ' . esc_html($block_id) . ')');
31754 + return 'fail';
31755 + }
31756 + else
31757 + {
31758 + aiomatic_log_to_file('Failed to init curl in webhook execution');
31759 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31760 + {
31761 + return array('');
31762 + }
31763 + $block_results[$block_id] = array($current_block['type']);
31764 + continue;
31765 + }
31766 + }
31767 + if (isset($aiomatic_Main_Settings['proxy_url']) && $aiomatic_Main_Settings['proxy_url'] != '' && $aiomatic_Main_Settings['proxy_url'] != 'disable' && $aiomatic_Main_Settings['proxy_url'] != 'disabled') {
31768 + $prx = explode(',', $aiomatic_Main_Settings['proxy_url']);
31769 + $randomness = array_rand($prx);
31770 + curl_setopt( $ch, CURLOPT_PROXY, trim($prx[$randomness]));
31771 + if (isset($aiomatic_Main_Settings['proxy_auth']) && $aiomatic_Main_Settings['proxy_auth'] != '')
31772 + {
31773 + $prx_auth = explode(',', $aiomatic_Main_Settings['proxy_auth']);
31774 + if(isset($prx_auth[$randomness]) && trim($prx_auth[$randomness]) != '')
31775 + {
31776 + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, trim($prx_auth[$randomness]));
31777 + }
31778 + }
31779 + }
31780 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31781 + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
31782 + if ($webhook_method === 'POST' || $webhook_method === 'PUT' || $webhook_method === 'DELETE')
31783 + {
31784 + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $webhook_method);
31785 + if (!empty($content_params) && $content_type === 'form') {
31786 + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content_params));
31787 + } elseif (!empty($post_template) && $content_type === 'JSON') {
31788 + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_template));
31789 + $headers[] = 'Content-Type: application/json';
31790 + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
31791 + }
31792 + }
31793 + else
31794 + {
31795 + $query_string = http_build_query($content_params);
31796 + $webhook_url = $webhook_url . (strpos($webhook_url, '?') === false ? '?' : '&') . $query_string;
31797 + }
31798 + curl_setopt($ch, CURLOPT_URL, $webhook_url);
31799 +
31800 + $response = curl_exec($ch);
31801 + if($response === false)
31802 + {
31803 + if($critical == '1')
31804 + {
31805 + aiomatic_log_to_file('Halting execution (failed to send webhook request), as critical block failed (ID ' . esc_html($block_id) . ')');
31806 + return 'fail';
31807 + }
31808 + else
31809 + {
31810 + aiomatic_log_to_file('Failed to send webhook request to ' . $webhook_url);
31811 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31812 + {
31813 + return array('');
31814 + }
31815 + $block_results[$block_id] = array($current_block['type']);
31816 + continue;
31817 + }
31818 + }
31819 + $err = curl_error($ch);
31820 + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
31821 + curl_close($ch);
31822 + if ($err)
31823 + {
31824 + if($critical == '1')
31825 + {
31826 + aiomatic_log_to_file('Halting execution (webhook request error), as critical block failed (ID ' . esc_html($block_id) . ')');
31827 + return 'fail';
31828 + }
31829 + else
31830 + {
31831 + aiomatic_log_to_file('Webhook request error to URL ' . $webhook_url . ' - error: ' . $err);
31832 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31833 + {
31834 + return array('');
31835 + }
31836 + $block_results[$block_id] = array($current_block['type']);
31837 + continue;
31838 + }
31839 + }
31840 +
31841 + if ($statusCode >= 200 && $statusCode <= 299)
31842 + {
31843 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31844 + {
31845 + return array('ok');
31846 + }
31847 + $block_results[$block_id] = array($current_block['type']);
31848 + $posts_inserted++;
31849 + }
31850 + else
31851 + {
31852 + if($critical == '1')
31853 + {
31854 + aiomatic_log_to_file('Halting execution (webhook unexpected return code), as critical block failed (ID ' . esc_html($block_id) . ')');
31855 + return 'fail';
31856 + }
31857 + else
31858 + {
31859 + aiomatic_log_to_file('Webhook unexpected return code to URL ' . $webhook_url . ' - return code: ' . $statusCode);
31860 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31861 + {
31862 + return array('');
31863 + }
31864 + $block_results[$block_id] = array($current_block['type']);
31865 + continue;
31866 + }
31867 + }
31868 + }
31869 + elseif($current_block['type'] == 'send_linkedin')
31870 + {
31871 + $post_template = $current_block['parameters']['post_template'];
31872 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31873 + $featured_image = $current_block['parameters']['featured_image'];
31874 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
31875 + $post_title = $current_block['parameters']['post_title'];
31876 + $post_title = aiomatic_replace_omniblocks_data($post_title, $current_keyword, $kiwis, $block_results, $load_variables);
31877 + $post_link = $current_block['parameters']['post_link'];
31878 + $post_link = aiomatic_replace_omniblocks_data($post_link, $current_keyword, $kiwis, $block_results, $load_variables);
31879 + $post_description = $current_block['parameters']['post_description'];
31880 + $post_description = aiomatic_replace_omniblocks_data($post_description, $current_keyword, $kiwis, $block_results, $load_variables);
31881 + $attach_lnk = $current_block['parameters']['attach_lnk'];
31882 + $selected_pages = $current_block['parameters']['page_to_post'];
31883 + if ((empty($post_template) && empty($featured_image)))
31884 + {
31885 + if($critical == '1')
31886 + {
31887 + aiomatic_log_to_file('Halting execution (empty LinkedIn post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31888 + return 'fail';
31889 + }
31890 + else
31891 + {
31892 + aiomatic_log_to_file('Empty LinkedIn post data entered');
31893 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31894 + {
31895 + return array('');
31896 + }
31897 + $block_results[$block_id] = array($current_block['type']);
31898 + continue;
31899 + }
31900 + }
31901 + if (empty($selected_pages))
31902 + {
31903 + if($critical == '1')
31904 + {
31905 + aiomatic_log_to_file('Halting execution (empty LinkedIn selected_pages entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31906 + return 'fail';
31907 + }
31908 + else
31909 + {
31910 + aiomatic_log_to_file('Empty LinkedIn selected_pages entered');
31911 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31912 + {
31913 + return array('');
31914 + }
31915 + $block_results[$block_id] = array($current_block['type']);
31916 + continue;
31917 + }
31918 + }
31919 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
31920 +
31921 + $return_me = aiomatic_post_to_linkedin($card_type_found, $post_template, $featured_image, $post_title, $post_link, $post_description, $attach_lnk, $selected_pages);
31922 + if(isset($return_me['error']))
31923 + {
31924 + if($critical == '1')
31925 + {
31926 + aiomatic_log_to_file('Halting execution (LinkedIn social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
31927 + return 'fail';
31928 + }
31929 + else
31930 + {
31931 + aiomatic_log_to_file('LinkedIn social posting failed: ' . $return_me['error']);
31932 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31933 + {
31934 + return array('');
31935 + }
31936 + $block_results[$block_id] = array($current_block['type']);
31937 + continue;
31938 + }
31939 + }
31940 + elseif(empty($return_me))
31941 + {
31942 + if($critical == '1')
31943 + {
31944 + aiomatic_log_to_file('Halting execution (LinkedIn social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
31945 + return 'fail';
31946 + }
31947 + else
31948 + {
31949 + aiomatic_log_to_file('Failed to parse social posting results to LinkedIn');
31950 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31951 + {
31952 + return array('');
31953 + }
31954 + $block_results[$block_id] = array($current_block['type']);
31955 + continue;
31956 + }
31957 + }
31958 + else
31959 + {
31960 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31961 + {
31962 + return array('ok');
31963 + }
31964 + $block_results[$block_id] = array($current_block['type']);
31965 + $posts_inserted++;
31966 + }
31967 + }
31968 + elseif($current_block['type'] == 'save_file')
31969 + {
31970 + $post_template = $current_block['parameters']['post_template'];
31971 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
31972 + $send_type = $current_block['parameters']['send_type'];
31973 + $file_type = $current_block['parameters']['file_type'];
31974 + if(empty($file_type))
31975 + {
31976 + $file_type = 'txt';
31977 + }
31978 + $file_name = "file_" . $param . "_" . date("Y-m-d_H-i-s") . "_" . uniqid() . '.' . $file_type;
31979 + if ((empty($post_template) && empty($send_type)))
31980 + {
31981 + if($critical == '1')
31982 + {
31983 + aiomatic_log_to_file('Halting execution (empty local file data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
31984 + return 'fail';
31985 + }
31986 + else
31987 + {
31988 + aiomatic_log_to_file('Empty local file data entered');
31989 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
31990 + {
31991 + return array('');
31992 + }
31993 + $block_results[$block_id] = array($current_block['type']);
31994 + continue;
31995 + }
31996 + }
31997 + if($file_type == 'pdf')
31998 + {
31999 + if(!function_exists('is_plugin_active'))
32000 + {
32001 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
32002 + }
32003 + if (!is_plugin_active('aiomatic-extension-pdf-files/aiomatic-extension-pdf-files.php'))
32004 + {
32005 + if($critical == '1')
32006 + {
32007 + aiomatic_log_to_file('Halting execution (PDF Parser Extension plugin is not active, please activate it to use this functionality), as critical block failed (ID ' . esc_html($block_id) . ')');
32008 + return 'fail';
32009 + }
32010 + else
32011 + {
32012 + aiomatic_log_to_file('Aimogen Extension - PDF File Storage And Parsing plugin is not active, please activate it to use this functionality');
32013 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32014 + {
32015 + return array('');
32016 + }
32017 + $block_results[$block_id] = array($current_block['type'], '');
32018 + continue;
32019 + }
32020 + }
32021 + $html2pdf = new \Spipu\Html2Pdf\Html2Pdf();
32022 + $html2pdf->writeHTML($post_template);
32023 + $pdf_str = $html2pdf->output('doc.pdf', 'S');
32024 + if($pdf_str === false)
32025 + {
32026 + if($critical == '1')
32027 + {
32028 + aiomatic_log_to_file('Halting execution (pdf file saving failed), as critical block failed (ID ' . esc_html($block_id) . ')');
32029 + return 'fail';
32030 + }
32031 + else
32032 + {
32033 + aiomatic_log_to_file('Failed to save pdf document to file');
32034 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32035 + {
32036 + return array('');
32037 + }
32038 + $block_results[$block_id] = array($current_block['type'], '');
32039 + continue;
32040 + }
32041 + }
32042 + else
32043 + {
32044 + $post_template = $pdf_str;
32045 + }
32046 + }
32047 + if($file_type == 'doc')
32048 + {
32049 + require_once(dirname(__FILE__) . "/res/html_to_doc/ExportToWord.inc.php");
32050 + $doc_str = ExportToWord::htmlToDoc($post_template, '');
32051 + if($doc_str === false)
32052 + {
32053 + if($critical == '1')
32054 + {
32055 + aiomatic_log_to_file('Halting execution (doc file saving failed), as critical block failed (ID ' . esc_html($block_id) . ')');
32056 + return 'fail';
32057 + }
32058 + else
32059 + {
32060 + aiomatic_log_to_file('Failed to save doc document to file');
32061 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32062 + {
32063 + return array('');
32064 + }
32065 + $block_results[$block_id] = array($current_block['type'], '');
32066 + continue;
32067 + }
32068 + }
32069 + else
32070 + {
32071 + $post_template = $doc_str;
32072 + }
32073 + }
32074 + $filesize = aiomatic_strlen($post_template);
32075 + if($send_type == 'local')
32076 + {
32077 + global $wp_filesystem;
32078 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
32079 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
32080 + wp_filesystem($creds);
32081 + }
32082 + $upload_dir = wp_upload_dir();
32083 + $aiomatic_directory = $upload_dir['basedir'] . '/aiomatic/';
32084 + $aiomatic_url = $upload_dir['baseurl'] . '/aiomatic/';
32085 + wp_mkdir_p($aiomatic_directory);
32086 + $new_pdf = $aiomatic_directory . $file_name;
32087 + $new_url = $aiomatic_url . $file_name;
32088 + $ret = $wp_filesystem->put_contents($new_pdf, $post_template);
32089 + if ($ret === FALSE)
32090 + {
32091 + if($critical == '1')
32092 + {
32093 + aiomatic_log_to_file('Halting execution (failed to upload file), as critical block failed (ID ' . esc_html($block_id) . ')');
32094 + return 'fail';
32095 + }
32096 + else
32097 + {
32098 + aiomatic_log_to_file('Failed to upload file: ' . $file_name . ' to ' . $new_pdf);
32099 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32100 + {
32101 + return array('');
32102 + }
32103 + $block_results[$block_id] = array($current_block['type']);
32104 + continue;
32105 + }
32106 + }
32107 + else
32108 + {
32109 + $forms_data = array(
32110 + 'post_type' => 'aiomatic_omni_file',
32111 + 'post_title' => $file_name,
32112 + 'post_content' => $new_url,
32113 + 'post_status' => 'publish'
32114 + );
32115 + remove_filter('content_save_pre', 'wp_filter_post_kses');
32116 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
32117 + $forms_id = wp_insert_post($forms_data);
32118 + add_filter('content_save_pre', 'wp_filter_post_kses');
32119 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
32120 + if(is_wp_error($forms_id))
32121 + {
32122 + if($critical == '1')
32123 + {
32124 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
32125 + return 'fail';
32126 + }
32127 + else
32128 + {
32129 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
32130 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32131 + {
32132 + return array('');
32133 + }
32134 + $block_results[$block_id] = array($current_block['type']);
32135 + continue;
32136 + }
32137 + }
32138 + elseif($forms_id === 0)
32139 + {
32140 + if($critical == '1')
32141 + {
32142 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
32143 + return 'fail';
32144 + }
32145 + else
32146 + {
32147 + aiomatic_log_to_file('Failed to insert file to db');
32148 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32149 + {
32150 + return array('');
32151 + }
32152 + $block_results[$block_id] = array($current_block['type']);
32153 + continue;
32154 + }
32155 + }
32156 + else
32157 + {
32158 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32159 + {
32160 + return array('ok');
32161 + }
32162 + $cat_arr = array($send_type);
32163 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
32164 + $new_pdf = str_replace('\\', '/', $new_pdf);
32165 + update_post_meta($forms_id, 'local_id', $new_pdf);
32166 + $block_results[$block_id] = array($current_block['type']);
32167 + $posts_inserted++;
32168 + }
32169 + }
32170 + }
32171 + elseif($send_type == 'amazon')
32172 + {
32173 + if(!function_exists('is_plugin_active'))
32174 + {
32175 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
32176 + }
32177 + if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php'))
32178 + {
32179 + if($critical == '1')
32180 + {
32181 + aiomatic_log_to_file('Halting execution (Amazon S3 extension not loaded), as critical block failed (ID ' . esc_html($block_id) . ')');
32182 + return 'fail';
32183 + }
32184 + else
32185 + {
32186 + aiomatic_log_to_file('You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!');
32187 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32188 + {
32189 + return array('');
32190 + }
32191 + $block_results[$block_id] = array($current_block['type']);
32192 + continue;
32193 + }
32194 + }
32195 + if (!isset($aiomatic_Main_Settings['bucket_name']) || trim($aiomatic_Main_Settings['bucket_name']) == '')
32196 + {
32197 + if($critical == '1')
32198 + {
32199 + aiomatic_log_to_file('Halting execution (Amazon S3 bucket_name not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32200 + return 'fail';
32201 + }
32202 + else
32203 + {
32204 + aiomatic_log_to_file('You need to enter a Amazon S3 bucket_name for this to work!');
32205 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32206 + {
32207 + return array('');
32208 + }
32209 + $block_results[$block_id] = array($current_block['type']);
32210 + continue;
32211 + }
32212 + }
32213 + if (!isset($aiomatic_Main_Settings['s3_user']) || trim($aiomatic_Main_Settings['s3_user']) == '')
32214 + {
32215 + if($critical == '1')
32216 + {
32217 + aiomatic_log_to_file('Halting execution (Amazon S3 s3_user not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32218 + return 'fail';
32219 + }
32220 + else
32221 + {
32222 + aiomatic_log_to_file('You need to enter a Amazon S3 s3_user for this to work!');
32223 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32224 + {
32225 + return array('');
32226 + }
32227 + $block_results[$block_id] = array($current_block['type']);
32228 + continue;
32229 + }
32230 + }
32231 + if (!isset($aiomatic_Main_Settings['s3_pass']) || trim($aiomatic_Main_Settings['s3_pass']) == '')
32232 + {
32233 + if($critical == '1')
32234 + {
32235 + aiomatic_log_to_file('Halting execution (Amazon S3 s3_pass not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32236 + return 'fail';
32237 + }
32238 + else
32239 + {
32240 + aiomatic_log_to_file('You need to enter a Amazon S3 s3_pass for this to work!');
32241 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32242 + {
32243 + return array('');
32244 + }
32245 + $block_results[$block_id] = array($current_block['type']);
32246 + continue;
32247 + }
32248 + }
32249 + if (!isset($aiomatic_Main_Settings['bucket_region']) || trim($aiomatic_Main_Settings['bucket_region']) == '')
32250 + {
32251 + $aiomatic_Main_Settings['bucket_region'] = 'eu-central-1';
32252 + }
32253 + try
32254 + {
32255 + $credentials = array('key' => trim($aiomatic_Main_Settings['s3_user']), 'secret' => trim($aiomatic_Main_Settings['s3_pass']));
32256 + $s3 = new S3Client([
32257 + 'version' => 'latest',
32258 + 'region' => trim($aiomatic_Main_Settings['bucket_region']),
32259 + 'credentials' => $credentials
32260 + ]);
32261 + }
32262 + catch(Exception $e)
32263 + {
32264 + if($critical == '1')
32265 + {
32266 + aiomatic_log_to_file('Halting execution (Amazon S3 init failed), as critical block failed (ID ' . esc_html($block_id) . ')');
32267 + return 'fail';
32268 + }
32269 + else
32270 + {
32271 + aiomatic_log_to_file('Failed to initialize Amazon S3 API: ' . $e->getMessage());
32272 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32273 + {
32274 + return array('');
32275 + }
32276 + $block_results[$block_id] = array($current_block['type']);
32277 + continue;
32278 + }
32279 + }
32280 + if (trim($aiomatic_Main_Settings['drive_directory']) != '') {
32281 + $s3_remote_path = trim(trim($aiomatic_Main_Settings['drive_directory']), '/');
32282 + $s3_remote_path = trailingslashit($s3_remote_path);
32283 + }
32284 + else
32285 + {
32286 + $s3_remote_path = '';
32287 + }
32288 + try
32289 + {
32290 + $obj_arr = [
32291 + 'Bucket' => trim($aiomatic_Main_Settings['bucket_name']),
32292 + 'Key' => $s3_remote_path . $file_name,
32293 + 'Body' => $post_template,
32294 + 'Content-Length' => $filesize,
32295 + 'ContentLength' => $filesize
32296 + ];
32297 + $obj_arr['ACL'] = 'public-read';
32298 + $awsret = $s3->putObject($obj_arr);
32299 + if(isset($awsret['ObjectURL']))
32300 + {
32301 + $forms_data = array(
32302 + 'post_type' => 'aiomatic_omni_file',
32303 + 'post_title' => $file_name,
32304 + 'post_content' => $awsret['ObjectURL'],
32305 + 'post_status' => 'publish'
32306 + );
32307 + remove_filter('content_save_pre', 'wp_filter_post_kses');
32308 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
32309 + $forms_id = wp_insert_post($forms_data);
32310 + add_filter('content_save_pre', 'wp_filter_post_kses');
32311 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
32312 + if(is_wp_error($forms_id))
32313 + {
32314 + if($critical == '1')
32315 + {
32316 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
32317 + return 'fail';
32318 + }
32319 + else
32320 + {
32321 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
32322 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32323 + {
32324 + return array('');
32325 + }
32326 + $block_results[$block_id] = array($current_block['type']);
32327 + continue;
32328 + }
32329 + }
32330 + elseif($forms_id === 0)
32331 + {
32332 + if($critical == '1')
32333 + {
32334 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
32335 + return 'fail';
32336 + }
32337 + else
32338 + {
32339 + aiomatic_log_to_file('Failed to insert file to db');
32340 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32341 + {
32342 + return array('');
32343 + }
32344 + $block_results[$block_id] = array($current_block['type']);
32345 + continue;
32346 + }
32347 + }
32348 + else
32349 + {
32350 + $cat_arr = array($send_type);
32351 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
32352 + $uri = $awsret['ObjectURL'];
32353 + $urlComponents = wp_parse_url($uri);
32354 + $key = ltrim($urlComponents['path'], '/');
32355 + update_post_meta($forms_id, 'local_id', $key);
32356 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32357 + {
32358 + return array('ok');
32359 + }
32360 + $block_results[$block_id] = array($current_block['type']);
32361 + $posts_inserted++;
32362 + }
32363 + }
32364 + else
32365 + {
32366 + if($critical == '1')
32367 + {
32368 + aiomatic_log_to_file('Halting execution (Amazon S3 error response decoding), as critical block failed (ID ' . esc_html($block_id) . ')');
32369 + return 'fail';
32370 + }
32371 + else
32372 + {
32373 + aiomatic_log_to_file("Failed to decode Amazon S3 API response: " . print_r($awsret, true));
32374 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32375 + {
32376 + return array('');
32377 + }
32378 + $block_results[$block_id] = array($current_block['type']);
32379 + continue;
32380 + }
32381 + }
32382 + }
32383 + catch (Exception $e)
32384 + {
32385 + if($critical == '1')
32386 + {
32387 + aiomatic_log_to_file('Halting execution (Amazon S3 error uploading file), as critical block failed (ID ' . esc_html($block_id) . ')');
32388 + return 'fail';
32389 + }
32390 + else
32391 + {
32392 + aiomatic_log_to_file("There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage());
32393 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32394 + {
32395 + return array('');
32396 + }
32397 + $block_results[$block_id] = array($current_block['type']);
32398 + continue;
32399 + }
32400 + }
32401 + }
32402 + elseif($send_type == 'wasabi')
32403 + {
32404 + if(!function_exists('is_plugin_active'))
32405 + {
32406 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
32407 + }
32408 + if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php'))
32409 + {
32410 + if($critical == '1')
32411 + {
32412 + aiomatic_log_to_file('Halting execution (Amazon S3 extension not loaded), as critical block failed (ID ' . esc_html($block_id) . ')');
32413 + return 'fail';
32414 + }
32415 + else
32416 + {
32417 + aiomatic_log_to_file('You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!');
32418 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32419 + {
32420 + return array('');
32421 + }
32422 + $block_results[$block_id] = array($current_block['type']);
32423 + continue;
32424 + }
32425 + }
32426 + if (!isset($aiomatic_Main_Settings['wasabi_bucket']) || trim($aiomatic_Main_Settings['wasabi_bucket']) == '')
32427 + {
32428 + if($critical == '1')
32429 + {
32430 + aiomatic_log_to_file('Halting execution (Amazon S3 wasabi_bucket not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32431 + return 'fail';
32432 + }
32433 + else
32434 + {
32435 + aiomatic_log_to_file('You need to enter a Amazon S3 wasabi_bucket for this to work!');
32436 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32437 + {
32438 + return array('');
32439 + }
32440 + $block_results[$block_id] = array($current_block['type']);
32441 + continue;
32442 + }
32443 + }
32444 + if (!isset($aiomatic_Main_Settings['wasabi_region']) || trim($aiomatic_Main_Settings['wasabi_region']) == '')
32445 + {
32446 + if($critical == '1')
32447 + {
32448 + aiomatic_log_to_file('Halting execution (Amazon S3 wasabi_region not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32449 + return 'fail';
32450 + }
32451 + else
32452 + {
32453 + aiomatic_log_to_file('You need to enter a Amazon S3 wasabi_region for this to work!');
32454 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32455 + {
32456 + return array('');
32457 + }
32458 + $block_results[$block_id] = array($current_block['type']);
32459 + continue;
32460 + }
32461 + }
32462 + if (!isset($aiomatic_Main_Settings['wasabi_user']) || trim($aiomatic_Main_Settings['wasabi_user']) == '')
32463 + {
32464 + if($critical == '1')
32465 + {
32466 + aiomatic_log_to_file('Halting execution (Amazon S3 wasabi_user not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32467 + return 'fail';
32468 + }
32469 + else
32470 + {
32471 + aiomatic_log_to_file('You need to enter a Amazon S3 wasabi_user for this to work!');
32472 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32473 + {
32474 + return array('');
32475 + }
32476 + $block_results[$block_id] = array($current_block['type']);
32477 + continue;
32478 + }
32479 + }
32480 + if (!isset($aiomatic_Main_Settings['wasabi_pass']) || trim($aiomatic_Main_Settings['wasabi_pass']) == '')
32481 + {
32482 + if($critical == '1')
32483 + {
32484 + aiomatic_log_to_file('Halting execution (Amazon S3 wasabi_pass not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32485 + return 'fail';
32486 + }
32487 + else
32488 + {
32489 + aiomatic_log_to_file('You need to enter a Amazon S3 wasabi_pass for this to work!');
32490 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32491 + {
32492 + return array('');
32493 + }
32494 + $block_results[$block_id] = array($current_block['type']);
32495 + continue;
32496 + }
32497 + }
32498 + if (!isset($aiomatic_Main_Settings['bucket_region']) || trim($aiomatic_Main_Settings['bucket_region']) == '')
32499 + {
32500 + $aiomatic_Main_Settings['bucket_region'] = 'eu-central-1';
32501 + }
32502 + try
32503 + {
32504 + $credentials = array('key' => trim($aiomatic_Main_Settings['wasabi_user']), 'secret' => trim($aiomatic_Main_Settings['wasabi_pass']));
32505 + $s3 = new S3Client([
32506 + 'endpoint' => "https://" . trim($aiomatic_Main_Settings['wasabi_bucket']) . ".s3." . trim($aiomatic_Main_Settings['wasabi_region']) . ".wasabisys.com/",
32507 + 'bucket_endpoint' => true,
32508 + 'version' => 'latest',
32509 + 'region' => trim($aiomatic_Main_Settings['wasabi_region']),
32510 + 'credentials' => $credentials
32511 + ]);
32512 + }
32513 + catch(Exception $e)
32514 + {
32515 + if($critical == '1')
32516 + {
32517 + aiomatic_log_to_file('Halting execution (Amazon S3 init failed), as critical block failed (ID ' . esc_html($block_id) . ')');
32518 + return 'fail';
32519 + }
32520 + else
32521 + {
32522 + aiomatic_log_to_file('Failed to initialize Amazon S3 API: ' . $e->getMessage());
32523 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32524 + {
32525 + return array('');
32526 + }
32527 + $block_results[$block_id] = array($current_block['type']);
32528 + continue;
32529 + }
32530 + }
32531 + if (trim($aiomatic_Main_Settings['wasabi_directory']) != '') {
32532 + $s3_remote_path = trim(trim($aiomatic_Main_Settings['wasabi_directory']), '/');
32533 + $s3_remote_path = trailingslashit($s3_remote_path);
32534 + }
32535 + else
32536 + {
32537 + $s3_remote_path = '';
32538 + }
32539 + try
32540 + {
32541 + $obj_arr = [
32542 + 'Bucket' => trim($aiomatic_Main_Settings['wasabi_bucket']),
32543 + 'Key' => $s3_remote_path . $file_name,
32544 + 'Body' => $post_template,
32545 + 'Content-Length' => $filesize,
32546 + 'ContentLength' => $filesize
32547 + ];
32548 + $obj_arr['ACL'] = 'public-read';
32549 + $awsret = $s3->putObject($obj_arr);
32550 + if(isset($awsret['ObjectURL']))
32551 + {
32552 + $forms_data = array(
32553 + 'post_type' => 'aiomatic_omni_file',
32554 + 'post_title' => $file_name,
32555 + 'post_content' => $awsret['ObjectURL'],
32556 + 'post_status' => 'publish'
32557 + );
32558 + remove_filter('content_save_pre', 'wp_filter_post_kses');
32559 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
32560 + $forms_id = wp_insert_post($forms_data);
32561 + add_filter('content_save_pre', 'wp_filter_post_kses');
32562 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
32563 + if(is_wp_error($forms_id))
32564 + {
32565 + if($critical == '1')
32566 + {
32567 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
32568 + return 'fail';
32569 + }
32570 + else
32571 + {
32572 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
32573 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32574 + {
32575 + return array('');
32576 + }
32577 + $block_results[$block_id] = array($current_block['type']);
32578 + continue;
32579 + }
32580 + }
32581 + elseif($forms_id === 0)
32582 + {
32583 + if($critical == '1')
32584 + {
32585 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
32586 + return 'fail';
32587 + }
32588 + else
32589 + {
32590 + aiomatic_log_to_file('Failed to insert file to db');
32591 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32592 + {
32593 + return array('');
32594 + }
32595 + $block_results[$block_id] = array($current_block['type']);
32596 + continue;
32597 + }
32598 + }
32599 + else
32600 + {
32601 + $cat_arr = array($send_type);
32602 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
32603 + $uri = $awsret['ObjectURL'];
32604 + $urlComponents = wp_parse_url($uri);
32605 + $key = ltrim($urlComponents['path'], '/');
32606 + update_post_meta($forms_id, 'local_id', $key);
32607 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32608 + {
32609 + return array('ok');
32610 + }
32611 + $block_results[$block_id] = array($current_block['type']);
32612 + $posts_inserted++;
32613 + }
32614 + }
32615 + else
32616 + {
32617 + if($critical == '1')
32618 + {
32619 + aiomatic_log_to_file('Halting execution (Amazon S3 error response decoding), as critical block failed (ID ' . esc_html($block_id) . ')');
32620 + return 'fail';
32621 + }
32622 + else
32623 + {
32624 + aiomatic_log_to_file("Failed to decode Amazon S3 API response: " . print_r($awsret, true));
32625 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32626 + {
32627 + return array('');
32628 + }
32629 + $block_results[$block_id] = array($current_block['type']);
32630 + continue;
32631 + }
32632 + }
32633 + }
32634 + catch (Exception $e)
32635 + {
32636 + if($critical == '1')
32637 + {
32638 + aiomatic_log_to_file('Halting execution (Amazon S3 error uploading file), as critical block failed (ID ' . esc_html($block_id) . ')');
32639 + return 'fail';
32640 + }
32641 + else
32642 + {
32643 + aiomatic_log_to_file("There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage());
32644 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32645 + {
32646 + return array('');
32647 + }
32648 + $block_results[$block_id] = array($current_block['type']);
32649 + continue;
32650 + }
32651 + }
32652 + }
32653 + elseif($send_type == 'generics3')
32654 + {
32655 + if(!function_exists('is_plugin_active'))
32656 + {
32657 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
32658 + }
32659 + if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php'))
32660 + {
32661 + if($critical == '1')
32662 + {
32663 + aiomatic_log_to_file('Halting execution (Amazon S3 extension not loaded), as critical block failed (ID ' . esc_html($block_id) . ')');
32664 + return 'fail';
32665 + }
32666 + else
32667 + {
32668 + aiomatic_log_to_file('You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!');
32669 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32670 + {
32671 + return array('');
32672 + }
32673 + $block_results[$block_id] = array($current_block['type']);
32674 + continue;
32675 + }
32676 + }
32677 + if (!isset($aiomatic_Main_Settings['generic_bucket']) || trim($aiomatic_Main_Settings['generic_bucket']) == '')
32678 + {
32679 + if($critical == '1')
32680 + {
32681 + aiomatic_log_to_file('Halting execution (Generic Amazon S3 bucket not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32682 + return 'fail';
32683 + }
32684 + else
32685 + {
32686 + aiomatic_log_to_file('You need to enter a Generic Amazon S3 bucket for this to work!');
32687 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32688 + {
32689 + return array('');
32690 + }
32691 + $block_results[$block_id] = array($current_block['type']);
32692 + continue;
32693 + }
32694 + }
32695 + if (!isset($aiomatic_Main_Settings['generic_url']) || trim($aiomatic_Main_Settings['generic_url']) == '')
32696 + {
32697 + if($critical == '1')
32698 + {
32699 + aiomatic_log_to_file('Halting execution (Generic Amazon S3 URL not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32700 + return 'fail';
32701 + }
32702 + else
32703 + {
32704 + aiomatic_log_to_file('You need to enter a Generic Amazon S3 URL for this to work!');
32705 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32706 + {
32707 + return array('');
32708 + }
32709 + $block_results[$block_id] = array($current_block['type']);
32710 + continue;
32711 + }
32712 + }
32713 + if (!isset($aiomatic_Main_Settings['generic_region']) || trim($aiomatic_Main_Settings['generic_region']) == '')
32714 + {
32715 + if($critical == '1')
32716 + {
32717 + aiomatic_log_to_file('Halting execution (Generic Amazon S3 region not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32718 + return 'fail';
32719 + }
32720 + else
32721 + {
32722 + aiomatic_log_to_file('You need to enter a Generic Amazon S3 region for this to work!');
32723 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32724 + {
32725 + return array('');
32726 + }
32727 + $block_results[$block_id] = array($current_block['type']);
32728 + continue;
32729 + }
32730 + }
32731 + if (!isset($aiomatic_Main_Settings['generic_user']) || trim($aiomatic_Main_Settings['generic_user']) == '')
32732 + {
32733 + if($critical == '1')
32734 + {
32735 + aiomatic_log_to_file('Halting execution (Generic Amazon S3 user not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32736 + return 'fail';
32737 + }
32738 + else
32739 + {
32740 + aiomatic_log_to_file('You need to enter a Generic Amazon S3 user for this to work!');
32741 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32742 + {
32743 + return array('');
32744 + }
32745 + $block_results[$block_id] = array($current_block['type']);
32746 + continue;
32747 + }
32748 + }
32749 + if (!isset($aiomatic_Main_Settings['generic_pass']) || trim($aiomatic_Main_Settings['generic_pass']) == '')
32750 + {
32751 + if($critical == '1')
32752 + {
32753 + aiomatic_log_to_file('Halting execution (Generic Amazon S3 password not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32754 + return 'fail';
32755 + }
32756 + else
32757 + {
32758 + aiomatic_log_to_file('You need to enter a Generic Amazon S3 password for this to work!');
32759 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32760 + {
32761 + return array('');
32762 + }
32763 + $block_results[$block_id] = array($current_block['type']);
32764 + continue;
32765 + }
32766 + }
32767 + if (!isset($aiomatic_Main_Settings['bucket_region']) || trim($aiomatic_Main_Settings['bucket_region']) == '')
32768 + {
32769 + $aiomatic_Main_Settings['bucket_region'] = 'eu-central-1';
32770 + }
32771 + try
32772 + {
32773 + $credentials = array('key' => trim($aiomatic_Main_Settings['generic_user']), 'secret' => trim($aiomatic_Main_Settings['generic_pass']));
32774 + $s3 = new S3Client([
32775 + 'endpoint' => trim($aiomatic_Main_Settings['generic_url']),
32776 + 'bucket_endpoint' => true,
32777 + 'version' => 'latest',
32778 + 'region' => trim($aiomatic_Main_Settings['generic_region']),
32779 + 'credentials' => $credentials
32780 + ]);
32781 + }
32782 + catch(Exception $e)
32783 + {
32784 + if($critical == '1')
32785 + {
32786 + aiomatic_log_to_file('Halting execution (Amazon S3 init failed), as critical block failed (ID ' . esc_html($block_id) . ')');
32787 + return 'fail';
32788 + }
32789 + else
32790 + {
32791 + aiomatic_log_to_file('Failed to initialize Amazon S3 API: ' . $e->getMessage());
32792 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32793 + {
32794 + return array('');
32795 + }
32796 + $block_results[$block_id] = array($current_block['type']);
32797 + continue;
32798 + }
32799 + }
32800 + if (trim($aiomatic_Main_Settings['generic_directory']) != '') {
32801 + $s3_remote_path = trim(trim($aiomatic_Main_Settings['generic_directory']), '/');
32802 + $s3_remote_path = trailingslashit($s3_remote_path);
32803 + }
32804 + else
32805 + {
32806 + $s3_remote_path = '';
32807 + }
32808 + try
32809 + {
32810 + $obj_arr = [
32811 + 'Bucket' => trim($aiomatic_Main_Settings['generic_bucket']),
32812 + 'Key' => $s3_remote_path . $file_name,
32813 + 'Body' => $post_template,
32814 + 'Content-Length' => $filesize,
32815 + 'ContentLength' => $filesize
32816 + ];
32817 + $obj_arr['ACL'] = 'public-read';
32818 + $awsret = $s3->putObject($obj_arr);
32819 + if(isset($awsret['ObjectURL']))
32820 + {
32821 + $forms_data = array(
32822 + 'post_type' => 'aiomatic_omni_file',
32823 + 'post_title' => $file_name,
32824 + 'post_content' => $awsret['ObjectURL'],
32825 + 'post_status' => 'publish'
32826 + );
32827 + remove_filter('content_save_pre', 'wp_filter_post_kses');
32828 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
32829 + $forms_id = wp_insert_post($forms_data);
32830 + add_filter('content_save_pre', 'wp_filter_post_kses');
32831 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
32832 + if(is_wp_error($forms_id))
32833 + {
32834 + if($critical == '1')
32835 + {
32836 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
32837 + return 'fail';
32838 + }
32839 + else
32840 + {
32841 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
32842 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32843 + {
32844 + return array('');
32845 + }
32846 + $block_results[$block_id] = array($current_block['type']);
32847 + continue;
32848 + }
32849 + }
32850 + elseif($forms_id === 0)
32851 + {
32852 + if($critical == '1')
32853 + {
32854 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
32855 + return 'fail';
32856 + }
32857 + else
32858 + {
32859 + aiomatic_log_to_file('Failed to insert file to db');
32860 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32861 + {
32862 + return array('');
32863 + }
32864 + $block_results[$block_id] = array($current_block['type']);
32865 + continue;
32866 + }
32867 + }
32868 + else
32869 + {
32870 + $cat_arr = array($send_type);
32871 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
32872 + $uri = $awsret['ObjectURL'];
32873 + $urlComponents = wp_parse_url($uri);
32874 + $key = ltrim($urlComponents['path'], '/');
32875 + update_post_meta($forms_id, 'local_id', $key);
32876 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32877 + {
32878 + return array('ok');
32879 + }
32880 + $block_results[$block_id] = array($current_block['type']);
32881 + $posts_inserted++;
32882 + }
32883 + }
32884 + else
32885 + {
32886 + if($critical == '1')
32887 + {
32888 + aiomatic_log_to_file('Halting execution (Amazon S3 error response decoding), as critical block failed (ID ' . esc_html($block_id) . ')');
32889 + return 'fail';
32890 + }
32891 + else
32892 + {
32893 + aiomatic_log_to_file("Failed to decode Amazon S3 API response: " . print_r($awsret, true));
32894 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32895 + {
32896 + return array('');
32897 + }
32898 + $block_results[$block_id] = array($current_block['type']);
32899 + continue;
32900 + }
32901 + }
32902 + }
32903 + catch (Exception $e)
32904 + {
32905 + if($critical == '1')
32906 + {
32907 + aiomatic_log_to_file('Halting execution (Amazon S3 error uploading file), as critical block failed (ID ' . esc_html($block_id) . ')');
32908 + return 'fail';
32909 + }
32910 + else
32911 + {
32912 + aiomatic_log_to_file("There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage());
32913 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32914 + {
32915 + return array('');
32916 + }
32917 + $block_results[$block_id] = array($current_block['type']);
32918 + continue;
32919 + }
32920 + }
32921 + }
32922 + elseif($send_type == 'cloudflare')
32923 + {
32924 + if(!function_exists('is_plugin_active'))
32925 + {
32926 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
32927 + }
32928 + if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php'))
32929 + {
32930 + if($critical == '1')
32931 + {
32932 + aiomatic_log_to_file('Halting execution (Amazon S3 extension not loaded), as critical block failed (ID ' . esc_html($block_id) . ')');
32933 + return 'fail';
32934 + }
32935 + else
32936 + {
32937 + aiomatic_log_to_file('You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!');
32938 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32939 + {
32940 + return array('');
32941 + }
32942 + $block_results[$block_id] = array($current_block['type']);
32943 + continue;
32944 + }
32945 + }
32946 + if (!isset($aiomatic_Main_Settings['cloud_bucket']) || trim($aiomatic_Main_Settings['cloud_bucket']) == '')
32947 + {
32948 + if($critical == '1')
32949 + {
32950 + aiomatic_log_to_file('Halting execution (Amazon S3 cloud_bucket not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32951 + return 'fail';
32952 + }
32953 + else
32954 + {
32955 + aiomatic_log_to_file('You need to enter a Amazon S3 cloud_bucket for this to work!');
32956 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32957 + {
32958 + return array('');
32959 + }
32960 + $block_results[$block_id] = array($current_block['type']);
32961 + continue;
32962 + }
32963 + }
32964 + if (!isset($aiomatic_Main_Settings['cloud_account']) || trim($aiomatic_Main_Settings['cloud_account']) == '')
32965 + {
32966 + if($critical == '1')
32967 + {
32968 + aiomatic_log_to_file('Halting execution (Amazon S3 cloud_account not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32969 + return 'fail';
32970 + }
32971 + else
32972 + {
32973 + aiomatic_log_to_file('You need to enter a Amazon S3 cloud_account for this to work!');
32974 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32975 + {
32976 + return array('');
32977 + }
32978 + $block_results[$block_id] = array($current_block['type']);
32979 + continue;
32980 + }
32981 + }
32982 + if (!isset($aiomatic_Main_Settings['cloud_user']) || trim($aiomatic_Main_Settings['cloud_user']) == '')
32983 + {
32984 + if($critical == '1')
32985 + {
32986 + aiomatic_log_to_file('Halting execution (Amazon S3 cloud_user not added), as critical block failed (ID ' . esc_html($block_id) . ')');
32987 + return 'fail';
32988 + }
32989 + else
32990 + {
32991 + aiomatic_log_to_file('You need to enter a Amazon S3 cloud_user for this to work!');
32992 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
32993 + {
32994 + return array('');
32995 + }
32996 + $block_results[$block_id] = array($current_block['type']);
32997 + continue;
32998 + }
32999 + }
33000 + if (!isset($aiomatic_Main_Settings['cloud_pass']) || trim($aiomatic_Main_Settings['cloud_pass']) == '')
33001 + {
33002 + if($critical == '1')
33003 + {
33004 + aiomatic_log_to_file('Halting execution (Amazon S3 cloud_pass not added), as critical block failed (ID ' . esc_html($block_id) . ')');
33005 + return 'fail';
33006 + }
33007 + else
33008 + {
33009 + aiomatic_log_to_file('You need to enter a Amazon S3 cloud_pass for this to work!');
33010 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33011 + {
33012 + return array('');
33013 + }
33014 + $block_results[$block_id] = array($current_block['type']);
33015 + continue;
33016 + }
33017 + }
33018 + try
33019 + {
33020 + $credentials = array('key' => trim($aiomatic_Main_Settings['cloud_user']), 'secret' => trim($aiomatic_Main_Settings['cloud_pass']));
33021 + $s3 = new S3Client([
33022 + 'endpoint' => "https://" . trim($aiomatic_Main_Settings['cloud_account']) . ".r2.cloudflarestorage.com",
33023 + 'bucket_endpoint' => true,
33024 + 'version' => 'latest',
33025 + 'region' => 'us-east-1',
33026 + 'credentials' => $credentials
33027 + ]);
33028 + }
33029 + catch(Exception $e)
33030 + {
33031 + if($critical == '1')
33032 + {
33033 + aiomatic_log_to_file('Halting execution (Amazon S3 init failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33034 + return 'fail';
33035 + }
33036 + else
33037 + {
33038 + aiomatic_log_to_file('Failed to initialize Amazon S3 API: ' . $e->getMessage());
33039 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33040 + {
33041 + return array('');
33042 + }
33043 + $block_results[$block_id] = array($current_block['type']);
33044 + continue;
33045 + }
33046 + }
33047 + if (trim($aiomatic_Main_Settings['cloud_directory']) != '') {
33048 + $s3_remote_path = trim(trim($aiomatic_Main_Settings['cloud_directory']), '/');
33049 + $s3_remote_path = trailingslashit($s3_remote_path);
33050 + }
33051 + else
33052 + {
33053 + $s3_remote_path = '';
33054 + }
33055 + try
33056 + {
33057 + $obj_arr = [
33058 + 'Bucket' => trim($aiomatic_Main_Settings['cloud_bucket']),
33059 + 'Key' => $s3_remote_path . $file_name,
33060 + 'Body' => $post_template,
33061 + 'Content-Length' => $filesize,
33062 + 'ContentLength' => $filesize
33063 + ];
33064 + $obj_arr['ACL'] = 'public-read';
33065 + $awsret = $s3->putObject($obj_arr);
33066 + if(isset($awsret['ObjectURL']))
33067 + {
33068 + if (isset($aiomatic_Main_Settings['cloud_public']) && trim($aiomatic_Main_Settings['cloud_public']) != '')
33069 + {
33070 + $awsret['ObjectURL'] = preg_replace(
33071 + '/https:\/\/[^\/]+\.r2\.cloudflarestorage\.com/',
33072 + esc_url(trim($aiomatic_Main_Settings['cloud_public'])),
33073 + $awsret['ObjectURL'],
33074 + 1
33075 + );
33076 + }
33077 + $forms_data = array(
33078 + 'post_type' => 'aiomatic_omni_file',
33079 + 'post_title' => $file_name,
33080 + 'post_content' => $awsret['ObjectURL'],
33081 + 'post_status' => 'publish'
33082 + );
33083 + remove_filter('content_save_pre', 'wp_filter_post_kses');
33084 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
33085 + $forms_id = wp_insert_post($forms_data);
33086 + add_filter('content_save_pre', 'wp_filter_post_kses');
33087 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
33088 + if(is_wp_error($forms_id))
33089 + {
33090 + if($critical == '1')
33091 + {
33092 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
33093 + return 'fail';
33094 + }
33095 + else
33096 + {
33097 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
33098 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33099 + {
33100 + return array('');
33101 + }
33102 + $block_results[$block_id] = array($current_block['type']);
33103 + continue;
33104 + }
33105 + }
33106 + elseif($forms_id === 0)
33107 + {
33108 + if($critical == '1')
33109 + {
33110 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
33111 + return 'fail';
33112 + }
33113 + else
33114 + {
33115 + aiomatic_log_to_file('Failed to insert file to db');
33116 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33117 + {
33118 + return array('');
33119 + }
33120 + $block_results[$block_id] = array($current_block['type']);
33121 + continue;
33122 + }
33123 + }
33124 + else
33125 + {
33126 + $cat_arr = array($send_type);
33127 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
33128 + $uri = $awsret['ObjectURL'];
33129 + $urlComponents = wp_parse_url($uri);
33130 + $key = ltrim($urlComponents['path'], '/');
33131 + update_post_meta($forms_id, 'local_id', $key);
33132 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33133 + {
33134 + return array('ok');
33135 + }
33136 + $block_results[$block_id] = array($current_block['type']);
33137 + $posts_inserted++;
33138 + }
33139 + }
33140 + else
33141 + {
33142 + if($critical == '1')
33143 + {
33144 + aiomatic_log_to_file('Halting execution (Amazon S3 error response decoding), as critical block failed (ID ' . esc_html($block_id) . ')');
33145 + return 'fail';
33146 + }
33147 + else
33148 + {
33149 + aiomatic_log_to_file("Failed to decode Amazon S3 API response: " . print_r($awsret, true));
33150 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33151 + {
33152 + return array('');
33153 + }
33154 + $block_results[$block_id] = array($current_block['type']);
33155 + continue;
33156 + }
33157 + }
33158 + }
33159 + catch (Exception $e)
33160 + {
33161 + if($critical == '1')
33162 + {
33163 + aiomatic_log_to_file('Halting execution (Amazon S3 error uploading file), as critical block failed (ID ' . esc_html($block_id) . ')');
33164 + return 'fail';
33165 + }
33166 + else
33167 + {
33168 + aiomatic_log_to_file("There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage());
33169 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33170 + {
33171 + return array('');
33172 + }
33173 + $block_results[$block_id] = array($current_block['type']);
33174 + continue;
33175 + }
33176 + }
33177 + }
33178 + elseif($send_type == 'digital')
33179 + {
33180 + if(!function_exists('is_plugin_active'))
33181 + {
33182 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
33183 + }
33184 + if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php'))
33185 + {
33186 + if($critical == '1')
33187 + {
33188 + aiomatic_log_to_file('Halting execution (Amazon S3 extension not loaded), as critical block failed (ID ' . esc_html($block_id) . ')');
33189 + return 'fail';
33190 + }
33191 + else
33192 + {
33193 + aiomatic_log_to_file('You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!');
33194 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33195 + {
33196 + return array('');
33197 + }
33198 + $block_results[$block_id] = array($current_block['type']);
33199 + continue;
33200 + }
33201 + }
33202 + if (!isset($aiomatic_Main_Settings['digital_endpoint']) || trim($aiomatic_Main_Settings['digital_endpoint']) == '')
33203 + {
33204 + if($critical == '1')
33205 + {
33206 + aiomatic_log_to_file('Halting execution (Amazon S3 digital_endpoint not added), as critical block failed (ID ' . esc_html($block_id) . ')');
33207 + return 'fail';
33208 + }
33209 + else
33210 + {
33211 + aiomatic_log_to_file('You need to enter a Amazon S3 digital_endpoint for this to work!');
33212 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33213 + {
33214 + return array('');
33215 + }
33216 + $block_results[$block_id] = array($current_block['type']);
33217 + continue;
33218 + }
33219 + }
33220 + if (!isset($aiomatic_Main_Settings['digital_user']) || trim($aiomatic_Main_Settings['digital_user']) == '')
33221 + {
33222 + if($critical == '1')
33223 + {
33224 + aiomatic_log_to_file('Halting execution (Amazon S3 cloud_user not added), as critical block failed (ID ' . esc_html($block_id) . ')');
33225 + return 'fail';
33226 + }
33227 + else
33228 + {
33229 + aiomatic_log_to_file('You need to enter a Amazon S3 cloud_user for this to work!');
33230 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33231 + {
33232 + return array('');
33233 + }
33234 + $block_results[$block_id] = array($current_block['type']);
33235 + continue;
33236 + }
33237 + }
33238 + if (!isset($aiomatic_Main_Settings['digital_pass']) || trim($aiomatic_Main_Settings['digital_pass']) == '')
33239 + {
33240 + if($critical == '1')
33241 + {
33242 + aiomatic_log_to_file('Halting execution (Amazon S3 digital_pass not added), as critical block failed (ID ' . esc_html($block_id) . ')');
33243 + return 'fail';
33244 + }
33245 + else
33246 + {
33247 + aiomatic_log_to_file('You need to enter a Amazon S3 digital_pass for this to work!');
33248 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33249 + {
33250 + return array('');
33251 + }
33252 + $block_results[$block_id] = array($current_block['type']);
33253 + continue;
33254 + }
33255 + }
33256 + $bucket_name = '';
33257 + preg_match_all('#https:\/\/([^.]*?)\.(?:[^.]*?)\.digitaloceanspaces\.com#i', trim($aiomatic_Main_Settings['digital_endpoint']), $zmatches);
33258 + if(isset($zmatches[1][0]))
33259 + {
33260 + $bucket_name = $zmatches[1][0];
33261 + }
33262 + else
33263 + {
33264 + if($critical == '1')
33265 + {
33266 + aiomatic_log_to_file('Halting execution (failed to parse Digital Ocean endpoint), as critical block failed (ID ' . esc_html($block_id) . ')');
33267 + return 'fail';
33268 + }
33269 + else
33270 + {
33271 + aiomatic_log_to_file('Failed to parse Digital Ocean Spaces URL: ' . trim($aiomatic_Main_Settings['digital_endpoint']));
33272 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33273 + {
33274 + return array('');
33275 + }
33276 + $block_results[$block_id] = array($current_block['type']);
33277 + continue;
33278 + }
33279 + }
33280 + $endpoint_plain_url = preg_replace('#https?:\/\/([^.]*?\.)([^.]*?)\.digitaloceanspaces\.com#i', 'https://$2.digitaloceanspaces.com', trim($aiomatic_Main_Settings['digital_endpoint']));
33281 + try
33282 + {
33283 + $credentials = array('key' => trim($aiomatic_Main_Settings['digital_user']), 'secret' => trim($aiomatic_Main_Settings['digital_pass']));
33284 + $s3 = new S3Client([
33285 + 'version' => 'latest',
33286 + 'region' => 'us-east-1',
33287 + 'endpoint' => $endpoint_plain_url,
33288 + 'use_path_style_endpoint' => false,
33289 + 'credentials' => $credentials
33290 + ]);
33291 + }
33292 + catch(Exception $e)
33293 + {
33294 + if($critical == '1')
33295 + {
33296 + aiomatic_log_to_file('Halting execution (Amazon S3 init failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33297 + return 'fail';
33298 + }
33299 + else
33300 + {
33301 + aiomatic_log_to_file('Failed to initialize Amazon S3 API: ' . $e->getMessage());
33302 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33303 + {
33304 + return array('');
33305 + }
33306 + $block_results[$block_id] = array($current_block['type']);
33307 + continue;
33308 + }
33309 + }
33310 + if (trim($aiomatic_Main_Settings['digital_directory']) != '') {
33311 + $s3_remote_path = trim(trim($aiomatic_Main_Settings['digital_directory']), '/');
33312 + $s3_remote_path = trailingslashit($s3_remote_path);
33313 + }
33314 + else
33315 + {
33316 + $s3_remote_path = '';
33317 + }
33318 + try
33319 + {
33320 + $obj_arr = [
33321 + 'Bucket' => trim($bucket_name),
33322 + 'Key' => $s3_remote_path . $file_name,
33323 + 'Body' => $post_template,
33324 + 'Content-Length' => $filesize,
33325 + 'ContentLength' => $filesize
33326 + ];
33327 + $obj_arr['ACL'] = 'public-read';
33328 + $awsret = $s3->putObject($obj_arr);
33329 + if(isset($awsret['ObjectURL']))
33330 + {
33331 + $forms_data = array(
33332 + 'post_type' => 'aiomatic_omni_file',
33333 + 'post_title' => $file_name,
33334 + 'post_content' => $awsret['ObjectURL'],
33335 + 'post_status' => 'publish'
33336 + );
33337 + remove_filter('content_save_pre', 'wp_filter_post_kses');
33338 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
33339 + $forms_id = wp_insert_post($forms_data);
33340 + add_filter('content_save_pre', 'wp_filter_post_kses');
33341 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
33342 + if(is_wp_error($forms_id))
33343 + {
33344 + if($critical == '1')
33345 + {
33346 + aiomatic_log_to_file('Halting execution (failed to insert file data), as critical block failed (ID ' . esc_html($block_id) . ')');
33347 + return 'fail';
33348 + }
33349 + else
33350 + {
33351 + aiomatic_log_to_file('Failed to insert file data: ' . $forms_id->get_error_message());
33352 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33353 + {
33354 + return array('');
33355 + }
33356 + $block_results[$block_id] = array($current_block['type']);
33357 + continue;
33358 + }
33359 + }
33360 + elseif($forms_id === 0)
33361 + {
33362 + if($critical == '1')
33363 + {
33364 + aiomatic_log_to_file('Halting execution (failed to insert file to db), as critical block failed (ID ' . esc_html($block_id) . ')');
33365 + return 'fail';
33366 + }
33367 + else
33368 + {
33369 + aiomatic_log_to_file('Failed to insert file to db');
33370 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33371 + {
33372 + return array('');
33373 + }
33374 + $block_results[$block_id] = array($current_block['type']);
33375 + continue;
33376 + }
33377 + }
33378 + else
33379 + {
33380 + $cat_arr = array($send_type);
33381 + wp_set_object_terms($forms_id, $cat_arr, 'ai_file_type');
33382 + $uri = $awsret['ObjectURL'];
33383 + $urlComponents = wp_parse_url($uri);
33384 + $key = ltrim($urlComponents['path'], '/');
33385 + update_post_meta($forms_id, 'local_id', $key);
33386 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33387 + {
33388 + return array('ok');
33389 + }
33390 + $block_results[$block_id] = array($current_block['type']);
33391 + $posts_inserted++;
33392 + }
33393 + }
33394 + else
33395 + {
33396 + if($critical == '1')
33397 + {
33398 + aiomatic_log_to_file('Halting execution (Amazon S3 error response decoding), as critical block failed (ID ' . esc_html($block_id) . ')');
33399 + return 'fail';
33400 + }
33401 + else
33402 + {
33403 + aiomatic_log_to_file("Failed to decode Amazon S3 API response: " . print_r($awsret, true));
33404 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33405 + {
33406 + return array('');
33407 + }
33408 + $block_results[$block_id] = array($current_block['type']);
33409 + continue;
33410 + }
33411 + }
33412 + }
33413 + catch (Exception $e)
33414 + {
33415 + if($critical == '1')
33416 + {
33417 + aiomatic_log_to_file('Halting execution (Amazon S3 error uploading file), as critical block failed (ID ' . esc_html($block_id) . ')');
33418 + return 'fail';
33419 + }
33420 + else
33421 + {
33422 + aiomatic_log_to_file("There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage());
33423 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33424 + {
33425 + return array('');
33426 + }
33427 + $block_results[$block_id] = array($current_block['type']);
33428 + continue;
33429 + }
33430 + }
33431 + }
33432 + else
33433 + {
33434 + if($critical == '1')
33435 + {
33436 + aiomatic_log_to_file('Halting execution (unknown file location sent), as critical block failed (ID ' . esc_html($block_id) . ')');
33437 + return 'fail';
33438 + }
33439 + else
33440 + {
33441 + aiomatic_log_to_file('Unknown file location sent: ' . $send_type);
33442 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33443 + {
33444 + return array('');
33445 + }
33446 + $block_results[$block_id] = array($current_block['type']);
33447 + continue;
33448 + }
33449 + }
33450 + }
33451 + elseif($current_block['type'] == 'send_facebook')
33452 + {
33453 + $post_template = $current_block['parameters']['post_template'];
33454 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
33455 + $post_link = $current_block['parameters']['post_link'];
33456 + $post_link = aiomatic_replace_omniblocks_data($post_link, $current_keyword, $kiwis, $block_results, $load_variables);
33457 + $page_to_post = $current_block['parameters']['page_to_post'];
33458 + if ((empty($post_template) && empty($post_link)))
33459 + {
33460 + if($critical == '1')
33461 + {
33462 + aiomatic_log_to_file('Halting execution (empty Facebook post data entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33463 + return 'fail';
33464 + }
33465 + else
33466 + {
33467 + aiomatic_log_to_file('Empty Facebook post data entered');
33468 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33469 + {
33470 + return array('');
33471 + }
33472 + $block_results[$block_id] = array($current_block['type']);
33473 + continue;
33474 + }
33475 + }
33476 + if (empty($page_to_post))
33477 + {
33478 + if($critical == '1')
33479 + {
33480 + aiomatic_log_to_file('Halting execution (empty Facebook page_to_post entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33481 + return 'fail';
33482 + }
33483 + else
33484 + {
33485 + aiomatic_log_to_file('Empty Facebook page_to_post entered');
33486 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33487 + {
33488 + return array('');
33489 + }
33490 + $block_results[$block_id] = array($current_block['type']);
33491 + continue;
33492 + }
33493 + }
33494 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
33495 + $return_me = aiomatic_post_to_facebook($card_type_found, $post_template, $post_link, $page_to_post);
33496 + if(isset($return_me['error']))
33497 + {
33498 + if($critical == '1')
33499 + {
33500 + aiomatic_log_to_file('Halting execution (social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33501 + return 'fail';
33502 + }
33503 + else
33504 + {
33505 + aiomatic_log_to_file('Social posting failed: ' . $return_me['error']);
33506 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33507 + {
33508 + return array('');
33509 + }
33510 + $block_results[$block_id] = array($current_block['type']);
33511 + continue;
33512 + }
33513 + }
33514 + elseif(empty($return_me))
33515 + {
33516 + if($critical == '1')
33517 + {
33518 + aiomatic_log_to_file('Halting execution (social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
33519 + return 'fail';
33520 + }
33521 + else
33522 + {
33523 + aiomatic_log_to_file('Failed to parse social posting results');
33524 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33525 + {
33526 + return array('');
33527 + }
33528 + $block_results[$block_id] = array($current_block['type']);
33529 + continue;
33530 + }
33531 + }
33532 + else
33533 + {
33534 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33535 + {
33536 + return array('ok');
33537 + }
33538 + $block_results[$block_id] = array($current_block['type']);
33539 + $posts_inserted++;
33540 + }
33541 + }
33542 + elseif($current_block['type'] == 'send_image_facebook')
33543 + {
33544 + $post_template = $current_block['parameters']['post_template'];
33545 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
33546 + $image_link = $current_block['parameters']['image_link'];
33547 + $image_link = aiomatic_replace_omniblocks_data($image_link, $current_keyword, $kiwis, $block_results, $load_variables);
33548 + $page_to_post = $current_block['parameters']['page_to_post'];
33549 + if (empty($image_link))
33550 + {
33551 + if($critical == '1')
33552 + {
33553 + aiomatic_log_to_file('Halting execution (empty Facebook image_link entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33554 + return 'fail';
33555 + }
33556 + else
33557 + {
33558 + aiomatic_log_to_file('Empty Facebook image_link entered');
33559 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33560 + {
33561 + return array('');
33562 + }
33563 + $block_results[$block_id] = array($current_block['type']);
33564 + continue;
33565 + }
33566 + }
33567 + if (empty($page_to_post))
33568 + {
33569 + if($critical == '1')
33570 + {
33571 + aiomatic_log_to_file('Halting execution (empty Facebook page_to_post entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33572 + return 'fail';
33573 + }
33574 + else
33575 + {
33576 + aiomatic_log_to_file('Empty Facebook page_to_post entered');
33577 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33578 + {
33579 + return array('');
33580 + }
33581 + $block_results[$block_id] = array($current_block['type']);
33582 + continue;
33583 + }
33584 + }
33585 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
33586 + $return_me = aiomatic_post_image_to_facebook($card_type_found, $post_template, $image_link, $page_to_post);
33587 + if(isset($return_me['error']))
33588 + {
33589 + if($critical == '1')
33590 + {
33591 + aiomatic_log_to_file('Halting execution (social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33592 + return 'fail';
33593 + }
33594 + else
33595 + {
33596 + aiomatic_log_to_file('Social posting failed: ' . $return_me['error']);
33597 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33598 + {
33599 + return array('');
33600 + }
33601 + $block_results[$block_id] = array($current_block['type']);
33602 + continue;
33603 + }
33604 + }
33605 + elseif(empty($return_me))
33606 + {
33607 + if($critical == '1')
33608 + {
33609 + aiomatic_log_to_file('Halting execution (social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
33610 + return 'fail';
33611 + }
33612 + else
33613 + {
33614 + aiomatic_log_to_file('Failed to parse social posting results');
33615 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33616 + {
33617 + return array('');
33618 + }
33619 + $block_results[$block_id] = array($current_block['type']);
33620 + continue;
33621 + }
33622 + }
33623 + else
33624 + {
33625 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33626 + {
33627 + return array('ok');
33628 + }
33629 + $block_results[$block_id] = array($current_block['type']);
33630 + $posts_inserted++;
33631 + }
33632 + }
33633 + elseif($current_block['type'] == 'send_image_instagram')
33634 + {
33635 + $post_template = $current_block['parameters']['post_template'];
33636 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
33637 + $image_link = $current_block['parameters']['image_link'];
33638 + $image_link = aiomatic_replace_omniblocks_data($image_link, $current_keyword, $kiwis, $block_results, $load_variables);
33639 + if (empty($image_link))
33640 + {
33641 + if($critical == '1')
33642 + {
33643 + aiomatic_log_to_file('Halting execution (empty Instagram image_link entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33644 + return 'fail';
33645 + }
33646 + else
33647 + {
33648 + aiomatic_log_to_file('Empty Instagram image_link entered');
33649 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33650 + {
33651 + return array('');
33652 + }
33653 + $block_results[$block_id] = array($current_block['type']);
33654 + continue;
33655 + }
33656 + }
33657 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
33658 + $return_me = aiomatic_post_image_to_instagram($card_type_found, $post_template, $image_link);
33659 + if(isset($return_me['error']))
33660 + {
33661 + if($critical == '1')
33662 + {
33663 + aiomatic_log_to_file('Halting execution (social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33664 + return 'fail';
33665 + }
33666 + else
33667 + {
33668 + aiomatic_log_to_file('Social posting failed: ' . $return_me['error']);
33669 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33670 + {
33671 + return array('');
33672 + }
33673 + $block_results[$block_id] = array($current_block['type']);
33674 + continue;
33675 + }
33676 + }
33677 + elseif(empty($return_me))
33678 + {
33679 + if($critical == '1')
33680 + {
33681 + aiomatic_log_to_file('Halting execution (social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
33682 + return 'fail';
33683 + }
33684 + else
33685 + {
33686 + aiomatic_log_to_file('Failed to parse social posting results');
33687 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33688 + {
33689 + return array('');
33690 + }
33691 + $block_results[$block_id] = array($current_block['type']);
33692 + continue;
33693 + }
33694 + }
33695 + else
33696 + {
33697 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33698 + {
33699 + return array('ok');
33700 + }
33701 + $block_results[$block_id] = array($current_block['type']);
33702 + $posts_inserted++;
33703 + }
33704 + }
33705 + elseif($current_block['type'] == 'send_image_pinterest')
33706 + {
33707 + $post_template = $current_block['parameters']['post_template'];
33708 + $post_template = aiomatic_replace_omniblocks_data($post_template, $current_keyword, $kiwis, $block_results, $load_variables);
33709 + $post_title = $current_block['parameters']['post_title'];
33710 + $post_title = aiomatic_replace_omniblocks_data($post_title, $current_keyword, $kiwis, $block_results, $load_variables);
33711 + $pin_me = $current_block['parameters']['pin_me'];
33712 + $pin_me = aiomatic_replace_omniblocks_data($pin_me, $current_keyword, $kiwis, $block_results, $load_variables);
33713 + $image_link = $current_block['parameters']['image_link'];
33714 + $image_link = aiomatic_replace_omniblocks_data($image_link, $current_keyword, $kiwis, $block_results, $load_variables);
33715 + $page_to_post = $current_block['parameters']['page_to_post'];
33716 + if (empty($image_link))
33717 + {
33718 + if($critical == '1')
33719 + {
33720 + aiomatic_log_to_file('Halting execution (empty Pinterest image_link entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33721 + return 'fail';
33722 + }
33723 + else
33724 + {
33725 + aiomatic_log_to_file('Empty Pinterest image_link entered');
33726 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33727 + {
33728 + return array('');
33729 + }
33730 + $block_results[$block_id] = array($current_block['type']);
33731 + continue;
33732 + }
33733 + }
33734 + if (empty($page_to_post))
33735 + {
33736 + if($critical == '1')
33737 + {
33738 + aiomatic_log_to_file('Halting execution (empty Pinterest page_to_post entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33739 + return 'fail';
33740 + }
33741 + else
33742 + {
33743 + aiomatic_log_to_file('Empty Pinterest page_to_post entered');
33744 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33745 + {
33746 + return array('');
33747 + }
33748 + $block_results[$block_id] = array($current_block['type']);
33749 + continue;
33750 + }
33751 + }
33752 + require_once(dirname(__FILE__) . "/aiomatic-socials.php");
33753 + $return_me = aiomatic_post_image_to_pinterest($card_type_found, $post_template, $post_title, $pin_me, $image_link, $page_to_post);
33754 + if(isset($return_me['error']))
33755 + {
33756 + if($critical == '1')
33757 + {
33758 + aiomatic_log_to_file('Halting execution (social posting failed), as critical block failed (ID ' . esc_html($block_id) . ')');
33759 + return 'fail';
33760 + }
33761 + else
33762 + {
33763 + aiomatic_log_to_file('Social posting failed: ' . $return_me['error']);
33764 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33765 + {
33766 + return array('');
33767 + }
33768 + $block_results[$block_id] = array($current_block['type']);
33769 + continue;
33770 + }
33771 + }
33772 + elseif(empty($return_me))
33773 + {
33774 + if($critical == '1')
33775 + {
33776 + aiomatic_log_to_file('Halting execution (social posting failed to be parsed), as critical block failed (ID ' . esc_html($block_id) . ')');
33777 + return 'fail';
33778 + }
33779 + else
33780 + {
33781 + aiomatic_log_to_file('Failed to parse social posting results');
33782 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33783 + {
33784 + return array('');
33785 + }
33786 + $block_results[$block_id] = array($current_block['type']);
33787 + continue;
33788 + }
33789 + }
33790 + else
33791 + {
33792 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33793 + {
33794 + return array('ok');
33795 + }
33796 + $block_results[$block_id] = array($current_block['type']);
33797 + $posts_inserted++;
33798 + }
33799 + }
33800 + elseif($current_block['type'] == 'save_post')
33801 + {
33802 + $my_post = array();
33803 + $post_title = aiomatic_truncate_title($current_block['parameters']['post_title']);
33804 + $post_title = aiomatic_replace_omniblocks_data($post_title, $current_keyword, $kiwis, $block_results, $load_variables);
33805 + $post_content = $current_block['parameters']['post_content'];
33806 + $post_content = aiomatic_replace_omniblocks_data($post_content, $current_keyword, $kiwis, $block_results, $load_variables);
33807 + $post_excerpt = $current_block['parameters']['post_excerpt'];
33808 + $post_excerpt = aiomatic_replace_omniblocks_data($post_excerpt, $current_keyword, $kiwis, $block_results, $load_variables);
33809 + if(isset($current_block['parameters']['post_slug']))
33810 + {
33811 + $post_slug = $current_block['parameters']['post_slug'];
33812 + $post_slug = aiomatic_replace_omniblocks_data($post_slug, $current_keyword, $kiwis, $block_results, $load_variables);
33813 + }
33814 + else
33815 + {
33816 + $post_slug = '';
33817 + }
33818 + $strip_by_regex = $current_block['parameters']['content_regex'];
33819 + $strip_by_regex = aiomatic_replace_omniblocks_data($strip_by_regex, $current_keyword, $kiwis, $block_results, $load_variables);
33820 + $replace_regex = $current_block['parameters']['replace_regex'];
33821 + $replace_regex = aiomatic_replace_omniblocks_data($replace_regex, $current_keyword, $kiwis, $block_results, $load_variables);
33822 + if ($strip_by_regex !== '')
33823 + {
33824 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
33825 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
33826 + $xcnt = 0;
33827 + foreach($xstrip_by_regex as $sbr)
33828 + {
33829 + if(isset($xreplace_regex[$xcnt]))
33830 + {
33831 + $repreg = $xreplace_regex[$xcnt];
33832 + }
33833 + else
33834 + {
33835 + $repreg = '';
33836 + }
33837 + $xcnt++;
33838 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $post_content);
33839 + if($temp_cont !== NULL)
33840 + {
33841 + $post_content = $temp_cont;
33842 + }
33843 + }
33844 + }
33845 +
33846 + if (empty($post_title) && empty($post_content) && empty($post_excerpt))
33847 + {
33848 + if($critical == '1')
33849 + {
33850 + aiomatic_log_to_file('Halting execution (empty post title and content entered), as critical block failed (ID ' . esc_html($block_id) . ')');
33851 + return 'fail';
33852 + }
33853 + else
33854 + {
33855 + aiomatic_log_to_file('Empty post title and content entered');
33856 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
33857 + {
33858 + return array('', '');
33859 + }
33860 + $block_results[$block_id] = array($current_block['type'], '', '');
33861 + continue;
33862 + }
33863 + }
33864 + if(!empty($post_title))
33865 + {
33866 + $my_post['post_title'] = aiomatic_truncate_title($post_title);
33867 + }
33868 + if(isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled')
33869 + {
33870 + preg_match_all('#<img[^>]*?src="([^"]*?)"[^>]*?>#', $post_content, $cfm);
33871 + if(isset($cfm[1][0]))
33872 + {
33873 + foreach($cfm[1] as $timg)
33874 + {
33875 + $show_url = '';
33876 + $localpath = aiomatic_copy_image_locally($timg, $attached_id, '', false, '');
33877 + if(!empty($attached_id))
33878 + {
33879 + $additional_attachments[] = $attached_id;
33880 + }
33881 + if($localpath !== false)
33882 + {
33883 + $show_url = $localpath[0];
33884 + }
33885 + if(!empty($show_url))
33886 + {
33887 + $post_content = str_replace($timg, $show_url, $post_content);
33888 + }
33889 + }
33890 + }
33891 + }
33892 + if(!empty($post_content))
33893 + {
33894 + $my_post['post_content'] = aiomatic_truncate_content($post_content, 16777215);
33895 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
33896 + {
33897 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
33898 + {
33899 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
33900 + }
33901 + }
33902 + }
33903 + if(!empty($post_excerpt))
33904 + {
33905 + $my_post['post_excerpt'] = $post_excerpt;
33906 + }
33907 + if(!empty($post_slug))
33908 + {
33909 + $my_post['post_name'] = sanitize_title($post_slug);
33910 + }
33911 + if (!empty($post_content) && isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
33912 + {
33913 + require_once(dirname(__FILE__) . "/res/swear.php");
33914 + $my_post['post_content'] = aimogenpro_filterwords($my_post['post_content']);
33915 + }
33916 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
33917 + {
33918 + if (!isset($aiomatic_Main_Settings['no_undetectibility_omni']) || $aiomatic_Main_Settings['no_undetectibility_omni'] != 'on')
33919 + {
33920 + if(!isset($xchars))
33921 + {
33922 + $xchars = array();
33923 + }
33924 + $rand_percentage = wp_rand(10, 20);
33925 + $my_post['post_content'] = aiomatic_make_unique($my_post['post_content'], $xchars, $rand_percentage);
33926 + }
33927 + }
33928 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
33929 + {
33930 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($my_post['post_content']);
33931 + }
33932 + $featured_image = $current_block['parameters']['featured_image'];
33933 + $featured_image = aiomatic_replace_omniblocks_data($featured_image, $current_keyword, $kiwis, $block_results, $load_variables);
33934 + $post_user_name = $current_block['parameters']['post_author'];
33935 + $post_user_name = aiomatic_replace_omniblocks_data($post_user_name, $current_keyword, $kiwis, $block_results, $load_variables);
33936 + if($post_user_name == '%%random_user%%')
33937 + {
33938 + $randid = aiomatic_display_random_user();
33939 + if($randid === false)
33940 + {
33941 + $my_post['post_author'] = aiomatic_randomName();
33942 + }
33943 + else
33944 + {
33945 + $my_post['post_author'] = $randid->ID;
33946 + }
33947 + }
33948 + else
33949 + {
33950 + $userid = get_user_by('login', $post_user_name);
33951 + if($userid !== false)
33952 + {
33953 + $my_post['post_author'] = $userid;
33954 + }
33955 + else
33956 + {
33957 + $my_post['post_author'] = $post_user_name;
33958 + }
33959 + }
33960 + $post_status = $current_block['parameters']['post_status'];
33961 + $post_status = trim($post_status);
33962 + if(!empty($post_status))
33963 + {
33964 + $my_post['post_status'] = $post_status;
33965 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] != 'on')
33966 + {
33967 + if($post_status == 'publish')
33968 + {
33969 + $draft_me = true;
33970 + $my_post['post_status'] = 'draft';
33971 + }
33972 + }
33973 + }
33974 + $post_type = $current_block['parameters']['post_type'];
33975 + $post_type = trim($post_type);
33976 + if(!empty($post_type))
33977 + {
33978 + $my_post['post_type'] = $post_type;
33979 + }
33980 + $post_format = $current_block['parameters']['post_format'];
33981 + $post_format = trim($post_format);
33982 + $post_parent = $current_block['parameters']['post_parent'];
33983 + $post_parent = trim($post_parent);
33984 + if(!empty($post_parent))
33985 + {
33986 + $my_post['post_parent'] = intval($post_parent);
33987 + }
33988 + $post_comments = $current_block['parameters']['post_comments'];
33989 + $post_comments = trim($post_comments);
33990 + if(!empty($post_comments))
33991 + {
33992 + if($post_comments == '1')
33993 + {
33994 + $my_post['comment_status'] = 'open';
33995 + }
33996 + else
33997 + {
33998 + $my_post['comment_status'] = 'closed';
33999 + }
34000 + }
34001 + $post_pingbacks = $current_block['parameters']['post_pingbacks'];
34002 + $post_pingbacks = trim($post_pingbacks);
34003 + if(!empty($post_pingbacks))
34004 + {
34005 + if($post_pingbacks == '1')
34006 + {
34007 + $my_post['ping_status'] = 'open';
34008 + }
34009 + else
34010 + {
34011 + $my_post['ping_status'] = 'closed';
34012 + }
34013 + }
34014 + $post_date = $current_block['parameters']['post_date'];
34015 + $post_date = aiomatic_replace_omniblocks_data($post_date, $current_keyword, $kiwis, $block_results, $load_variables);
34016 + if(strstr($post_date, '~') !== false)
34017 + {
34018 + $post_date = explode('~', $post_date);
34019 + $min_time = trim($post_date[0]);
34020 + $max_time = trim($post_date[1]);
34021 + if($min_time != '' && $max_time != '')
34022 + {
34023 + $t1 = strtotime($min_time);
34024 + $t2 = strtotime($max_time);
34025 + if($t1 != false && $t2 != false)
34026 + {
34027 + $int = wp_rand($t1, $t2);
34028 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
34029 + }
34030 + }
34031 + }
34032 + else
34033 + {
34034 + $t1 = strtotime($post_date);
34035 + if($t1 != false)
34036 + {
34037 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
34038 + }
34039 + }
34040 + $wpml_lang = $current_block['parameters']['post_lang'];
34041 + $wpml_lang = aiomatic_replace_omniblocks_data($wpml_lang, $current_keyword, $kiwis, $block_results, $load_variables);
34042 + $overwrite_existing = $current_block['parameters']['overwrite_existing'];
34043 + $overwrite_existing = trim($overwrite_existing);
34044 + if($overwrite_existing == '1')
34045 + {
34046 + $posts = get_posts(
34047 + array(
34048 + 'post_type' => $post_type,
34049 + 'title' => html_entity_decode($post_title),
34050 + 'post_status' => 'all',
34051 + 'numberposts' => 1,
34052 + 'update_post_term_cache' => false,
34053 + 'update_post_meta_cache' => false,
34054 + 'orderby' => 'post_date ID',
34055 + 'order' => 'ASC',
34056 + )
34057 + );
34058 + if ( ! empty( $posts ) ) {
34059 + $zap = $posts[0];
34060 + } else {
34061 + $zap = null;
34062 + }
34063 + if($zap !== null)
34064 + {
34065 + $my_post['ID'] = $zap->ID;
34066 + }
34067 + }
34068 + elseif($overwrite_existing == '2')
34069 + {
34070 + $posts = get_posts(
34071 + array(
34072 + 'post_type' => $post_type,
34073 + 'title' => html_entity_decode($post_title),
34074 + 'post_status' => 'all',
34075 + 'numberposts' => 1,
34076 + 'update_post_term_cache' => false,
34077 + 'update_post_meta_cache' => false,
34078 + 'orderby' => 'post_date ID',
34079 + 'order' => 'ASC',
34080 + )
34081 + );
34082 + if ( ! empty( $posts ) ) {
34083 + aiomatic_log_to_file('Post with same title already published, skipping OmniBlock publishing: ' . esc_html($post_title));
34084 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34085 + {
34086 + return array('0', 'skipped');
34087 + }
34088 + $block_results[$block_id] = array($current_block['type'], '0', 'skipped');
34089 + continue;
34090 + }
34091 + }
34092 + $custom_fields = $current_block['parameters']['post_custom_fields'];
34093 + $block_arr = array();
34094 + $custom_arr = array();
34095 + if($custom_fields != '')
34096 + {
34097 + if(stristr($custom_fields, '=>') != false)
34098 + {
34099 + $rule_arr = explode(',', trim($custom_fields));
34100 + foreach($rule_arr as $rule)
34101 + {
34102 + $my_args = explode('=>', trim($rule));
34103 + if(isset($my_args[1]))
34104 + {
34105 + $my_args[1] = do_shortcode($my_args[1]);
34106 + $my_args[0] = do_shortcode($my_args[0]);
34107 + $my_args[0] = aiomatic_replace_omniblocks_data($my_args[0], $current_keyword, $kiwis, $block_results, $load_variables);
34108 + $custom_field_content = trim($my_args[1]);
34109 + $custom_field_content = aiomatic_replace_omniblocks_data($custom_field_content, $current_keyword, $kiwis, $block_results, $load_variables);
34110 + if(stristr($my_args[1], 'serialize_') !== false)
34111 + {
34112 + $custom_arr[trim($my_args[0])] = array(str_replace('serialize_', '', $custom_field_content));
34113 + }
34114 + else
34115 + {
34116 + if(stristr($my_args[0], '[') !== false && stristr($my_args[0], ']') !== false)
34117 + {
34118 + preg_match_all('#([^\[\]]*?)\[([^\[\]]*?)\]#', $my_args[0], $cfm);
34119 + if(isset($cfm[2][0]))
34120 + {
34121 + if(isset($custom_arr[trim($cfm[1][0])]) && is_array($custom_arr[trim($cfm[1][0])]))
34122 + {
34123 + $custom_arr[trim($cfm[1][0])] = array_merge($custom_arr[trim($cfm[1][0])], array(trim($cfm[2][0]) => $custom_field_content));
34124 + }
34125 + else
34126 + {
34127 + $custom_arr[trim($cfm[1][0])] = array(trim($cfm[2][0]) => $custom_field_content);
34128 + }
34129 + }
34130 + else
34131 + {
34132 + $custom_arr[trim($my_args[0])] = $custom_field_content;
34133 + }
34134 + }
34135 + else
34136 + {
34137 + $custom_arr[trim($my_args[0])] = $custom_field_content;
34138 + }
34139 + }
34140 + }
34141 + }
34142 + }
34143 + }
34144 + if(!empty($custom_arr))
34145 + {
34146 + $my_post['meta_input'] = $custom_arr;
34147 + }
34148 + $post_categories = $current_block['parameters']['post_categories'];
34149 + $post_categories = aiomatic_replace_omniblocks_data($post_categories, $current_keyword, $kiwis, $block_results, $load_variables);
34150 + $post_tags = $current_block['parameters']['post_tags'];
34151 + $post_tags = aiomatic_replace_omniblocks_data($post_tags, $current_keyword, $kiwis, $block_results, $load_variables);
34152 + if(!empty($post_tags))
34153 + {
34154 + $my_post['tags_input'] = $post_tags;
34155 + }
34156 + $post_custom_taxonomies = $current_block['parameters']['post_custom_taxonomies'];
34157 + $custom_tax_arr = array();
34158 + if($post_custom_taxonomies != '')
34159 + {
34160 + if(stristr($post_custom_taxonomies, '=>') != false)
34161 + {
34162 + $rule_arr = explode(';', trim($post_custom_taxonomies));
34163 + foreach($rule_arr as $rule)
34164 + {
34165 + $my_args = explode('=>', trim($rule));
34166 + if(isset($my_args[1]))
34167 + {
34168 + $custom_tax_content = trim($my_args[1]);
34169 + $custom_tax_content = aiomatic_replace_omniblocks_data($custom_tax_content, $current_keyword, $kiwis, $block_results, $load_variables);
34170 + $my_args[0] = aiomatic_replace_omniblocks_data($my_args[0], $current_keyword, $kiwis, $block_results, $load_variables);
34171 + $spintax = new Aiomatic_Spintax();
34172 + $custom_tax_content = $spintax->Parse($custom_tax_content, $block_arr);
34173 + if(isset($custom_tax_arr[trim($my_args[0])]))
34174 + {
34175 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
34176 + }
34177 + else
34178 + {
34179 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
34180 + }
34181 + }
34182 + }
34183 + }
34184 + }
34185 + if(count($custom_tax_arr) > 0)
34186 + {
34187 + $my_post['taxo_input'] = $custom_tax_arr;
34188 + }
34189 + if(isset($current_block['parameters']['post_id']))
34190 + {
34191 + $uppost_id = $current_block['parameters']['post_id'];
34192 + $uppost_id = aiomatic_replace_omniblocks_data($uppost_id, $current_keyword, $kiwis, $block_results, $load_variables);
34193 + }
34194 + else
34195 + {
34196 + $uppost_id = '';
34197 + }
34198 + if(!empty($uppost_id))
34199 + {
34200 + $my_post['ID'] = $uppost_id;
34201 + }
34202 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Saving OmniBlock Post'));
34203 +
34204 + remove_filter('content_save_pre', 'wp_filter_post_kses');
34205 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
34206 + try
34207 + {
34208 + if(!empty($uppost_id))
34209 + {
34210 + $post_id = wp_update_post($my_post, true);
34211 + }
34212 + else
34213 + {
34214 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
34215 + $post_id = wp_insert_post($my_post, true);
34216 + if (is_wp_error($post_id))
34217 + {
34218 + aiomatic_log_to_file('Trying publishing again');
34219 + if (aiomatic_strlen($my_post['post_content']) > 16777215) {
34220 + $my_post['post_content'] = aiomatic_substr($my_post['post_content'], 0, 16777215);
34221 + }
34222 + $my_post['post_content'] = wp_kses_post($my_post['post_content']);
34223 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
34224 + $post_id = wp_insert_post($my_post, true);
34225 + }
34226 + }
34227 + }
34228 + catch(Exception $e)
34229 + {
34230 + aiomatic_log_to_file('Exception in publishing post (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
34231 + }
34232 + add_filter('content_save_pre', 'wp_filter_post_kses');
34233 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
34234 + if (!is_wp_error($post_id))
34235 + {
34236 + if($post_id === 0)
34237 + {
34238 + aiomatic_log_to_file('An error occurred while inserting post into wp database! Title:' . $my_post['post_title']);
34239 + if($critical == '1')
34240 + {
34241 + return 'fail';
34242 + }
34243 + else
34244 + {
34245 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34246 + {
34247 + return array('', '');
34248 + }
34249 + $block_results[$block_id] = array($current_block['type'], '', '');
34250 + continue;
34251 + }
34252 + }
34253 + else
34254 + {
34255 + $posts_inserted++;
34256 + if(!isset($my_post['taxo_input']))
34257 + {
34258 + $my_post['taxo_input'] = array();
34259 + }
34260 + if(!empty($featured_image))
34261 + {
34262 + if(is_numeric($featured_image))
34263 + {
34264 + $featured_path = aiomatic_assign_featured_image($featured_image, $post_id);
34265 + }
34266 + else
34267 + {
34268 + $go_local_image = false;
34269 + if(!function_exists('is_plugin_active'))
34270 + {
34271 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
34272 + }
34273 + 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' && isset($aiomatic_Main_Settings['copy_locally']) && $aiomatic_Main_Settings['copy_locally'] != 'disabled' && $aiomatic_Main_Settings['copy_locally'] != 'on')
34274 + {
34275 + $go_local_image = true;
34276 + }
34277 + if($go_local_image == true)
34278 + {
34279 + $localpath = aiomatic_copy_image_locally($featured_image, $attached_id, '', false, '');
34280 + if(!empty($attached_id))
34281 + {
34282 + $additional_attachments[] = $attached_id;
34283 + }
34284 + if($localpath !== false)
34285 + {
34286 + $featured_image = $localpath[0];
34287 + }
34288 + }
34289 + if (!aiomatic_generate_featured_image($featured_image, $post_id)) {
34290 + aiomatic_log_to_file('Failed to generate featured image for post ID: ' . $post_id . ' - image URL: ' . $featured_image);
34291 + if($critical == '1')
34292 + {
34293 + return 'fail';
34294 + }
34295 + else
34296 + {
34297 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34298 + {
34299 + return array('', '');
34300 + }
34301 + $block_results[$block_id] = array($current_block['type'], '', '');
34302 + continue;
34303 + }
34304 + }
34305 + }
34306 + }
34307 + $cat_str = http_build_query($my_post['taxo_input'], '', ',');
34308 + $wp_default_category = get_option('default_category');
34309 + $def_cat_name = get_cat_name($wp_default_category);
34310 + $default_categories = array();
34311 + $extra_cats = explode(',', $post_categories);
34312 + if(($post_categories != '' && !in_array($def_cat_name, $extra_cats)) || (isset($my_post['taxo_input']) && stristr($cat_str, 'category') !== false && strstr($cat_str, $def_cat_name) === false && !in_array($def_cat_name, $extra_cats)))
34313 + {
34314 + $default_categories = wp_get_post_categories($post_id);
34315 + }
34316 + if(isset($my_post['taxo_input']))
34317 + {
34318 + foreach($my_post['taxo_input'] as $taxn => $taxval)
34319 + {
34320 + $taxn = trim($taxn);
34321 + $taxval = trim($taxval);
34322 + if(is_taxonomy_hierarchical($taxn))
34323 + {
34324 + $taxval = array_map('trim', explode(',', $taxval));
34325 + for($ii = 0; $ii < count($taxval); $ii++)
34326 + {
34327 + if(!is_numeric($taxval[$ii]))
34328 + {
34329 + $term_ids = [];
34330 + $parent_id = 0;
34331 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
34332 + foreach($hierarchy_parts as $hp)
34333 + {
34334 + if(!is_numeric($hp))
34335 + {
34336 + $xtermid = get_term_by('name', $hp, $taxn);
34337 + if($xtermid !== false)
34338 + {
34339 + $parent_id = $xtermid->term_id;
34340 + if(!is_numeric($taxval[$ii]))
34341 + {
34342 + $taxval[$ii] = intval($xtermid->term_id);
34343 + }
34344 + else
34345 + {
34346 + $taxval[] = intval($xtermid->term_id);
34347 + }
34348 + }
34349 + else
34350 + {
34351 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
34352 + $xtermid = get_term_by('name', $hp, $taxn);
34353 + if($xtermid !== false)
34354 + {
34355 + $parent_id = $xtermid->term_id;
34356 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
34357 + {
34358 + pll_set_term_language($xtermid->term_id, $wpml_lang);
34359 + }
34360 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
34361 + {
34362 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
34363 + $pars['element_id'] = $xtermid->term_id;
34364 + $pars['element_type'] = $wpml_element_type;
34365 + $pars['language_code'] = $wpml_lang;
34366 + $pars['trid'] = FALSE;
34367 + $pars['source_language_code'] = NULL;
34368 + do_action('wpml_set_element_language_details', $pars);
34369 + }
34370 + if(!is_numeric($taxval[$ii]))
34371 + {
34372 + $taxval[$ii] = intval($xtermid->term_id);
34373 + }
34374 + else
34375 + {
34376 + $taxval[] = intval($xtermid->term_id);
34377 + }
34378 + }
34379 + }
34380 + }
34381 + else
34382 + {
34383 + if(!is_numeric($taxval[$ii]))
34384 + {
34385 + $taxval[$ii] = intval($hp);
34386 + }
34387 + else
34388 + {
34389 + $taxval[] = intval($hp);
34390 + }
34391 + }
34392 + }
34393 + }
34394 + }
34395 + wp_set_post_terms($post_id, $taxval, $taxn, true);
34396 + }
34397 + else
34398 + {
34399 + $jterms = array_map('trim', explode(',', $taxval));
34400 + wp_set_post_terms($post_id, $jterms, $taxn, true);
34401 + }
34402 + }
34403 + }
34404 + if(!empty($post_format) && $post_format != 'post-format-standard')
34405 + {
34406 + wp_set_post_terms($post_id, $post_format, 'post_format', true);
34407 + }
34408 + if($wpml_lang != '' && function_exists('pll_set_post_language'))
34409 + {
34410 + pll_set_post_language($post_id, $wpml_lang);
34411 + }
34412 + if(!empty($additional_attachments))
34413 + {
34414 + foreach($additional_attachments as $add_att)
34415 + {
34416 + wp_update_post([
34417 + 'ID' => $add_att,
34418 + 'post_parent' => $post_id,
34419 + ]);
34420 + }
34421 + }
34422 + if($wpml_lang != '' && (class_exists('SitePress') || function_exists('wpml_object_id')))
34423 + {
34424 + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
34425 + $pars['element_id'] = $post_id;
34426 + $pars['element_type'] = $wpml_element_type;
34427 + $pars['language_code'] = $wpml_lang;
34428 + $pars['source_language_code'] = NULL;
34429 + do_action('wpml_set_element_language_details', $pars);
34430 +
34431 + global $wp_filesystem;
34432 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
34433 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
34434 + wp_filesystem($creds);
34435 + }
34436 + if($wp_filesystem->exists(WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php'))
34437 + {
34438 + include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
34439 + }
34440 + if(function_exists('wpml_update_translatable_content'))
34441 + {
34442 + wpml_update_translatable_content('post_' . $post_type, $post_id, $wpml_lang);
34443 + if($my_post['post_title'] != '')
34444 + {
34445 + global $sitepress;
34446 + global $wpdb;
34447 + $keyid = md5($my_post['post_title']);
34448 + $keyName = $keyid . '_wpml';
34449 + $rezxxxa = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = '$keyName' limit 1", ARRAY_A );
34450 + if(count($rezxxxa) != 0)
34451 + {
34452 + $metaRow = $rezxxxa[0];
34453 + $metaValue = $metaRow['meta_value'];
34454 + $metaParts = explode('_', $metaValue);
34455 + $sitepress->set_element_language_details($post_id, 'post_'.$my_post['post_type'] , $metaParts[0], $wpml_lang, $metaParts[1] );
34456 + }
34457 + else
34458 + {
34459 + $ptrid = $sitepress->get_element_trid($post_id);
34460 + update_post_meta($post_id, $keyid.'_wpml', $ptrid.'_'.$wpml_lang );
34461 + }
34462 + }
34463 +
34464 + }
34465 + }
34466 + if(!empty($post_custom_taxonomies))
34467 + {
34468 + $custom_tax_arr = array();
34469 + if(stristr($post_custom_taxonomies, '=>') != false)
34470 + {
34471 + $rule_arr = explode(';', trim($post_custom_taxonomies));
34472 + foreach($rule_arr as $rule)
34473 + {
34474 + $my_args = explode('=>', trim($rule));
34475 + if(isset($my_args[1]))
34476 + {
34477 + $custom_tax_content = trim($my_args[1]);
34478 + $custom_tax_content = aiomatic_replace_omniblocks_data($custom_tax_content, $current_keyword, $kiwis, $block_results, $load_variables);
34479 + if(aiomatic_substr(trim($my_args[0]), 0, 3) === "pa_" && $post_type == 'product' && !empty($custom_tax_content))
34480 + {
34481 + if(isset($custom_arr['_product_attributes']))
34482 + {
34483 + $custom_arr['_product_attributes'] = array_merge($custom_arr['_product_attributes'], array(trim($my_args[0]) =>array(
34484 + 'name' => trim($my_args[0]),
34485 + 'value' => $custom_tax_content,
34486 + 'is_visible' => '1',
34487 + 'is_taxonomy' => '1'
34488 + )));
34489 + }
34490 + else
34491 + {
34492 + $custom_arr['_product_attributes'] = array(trim($my_args[0]) =>array(
34493 + 'name' => trim($my_args[0]),
34494 + 'value' => $custom_tax_content,
34495 + 'is_visible' => '1',
34496 + 'is_taxonomy' => '1'
34497 + ));
34498 + }
34499 + }
34500 + if(isset($custom_tax_arr[trim($my_args[0])]))
34501 + {
34502 + $custom_tax_arr[trim($my_args[0])] .= ',' . $custom_tax_content;
34503 + }
34504 + else
34505 + {
34506 + $custom_tax_arr[trim($my_args[0])] = $custom_tax_content;
34507 + }
34508 + }
34509 + }
34510 + }
34511 + foreach($custom_tax_arr as $taxn => $taxval)
34512 + {
34513 + $taxn = trim($taxn);
34514 + $taxval = trim($taxval);
34515 + if(is_taxonomy_hierarchical($taxn))
34516 + {
34517 + $taxval = array_map('trim', explode(',', $taxval));
34518 + for($ii = 0; $ii < count($taxval); $ii++)
34519 + {
34520 + if(!is_numeric($taxval[$ii]))
34521 + {
34522 + $term_ids = [];
34523 + $parent_id = 0;
34524 + $hierarchy_parts = array_map('trim', explode('>', $taxval[$ii]));
34525 + foreach($hierarchy_parts as $hp)
34526 + {
34527 + if(!is_numeric($hp))
34528 + {
34529 + $xtermid = get_term_by('name', $hp, $taxn);
34530 + if($xtermid !== false)
34531 + {
34532 + $parent_id = $xtermid->term_id;
34533 + if(!is_numeric($taxval[$ii]))
34534 + {
34535 + $taxval[$ii] = intval($xtermid->term_id);
34536 + }
34537 + else
34538 + {
34539 + $taxval[] = intval($xtermid->term_id);
34540 + }
34541 + }
34542 + else
34543 + {
34544 + wp_insert_term( $hp, $taxn, ['parent' => $parent_id]);
34545 + $xtermid = get_term_by('name', $hp, $taxn);
34546 + if($xtermid !== false)
34547 + {
34548 + $parent_id = $xtermid->term_id;
34549 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
34550 + {
34551 + pll_set_term_language($xtermid->term_id, $wpml_lang);
34552 + }
34553 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
34554 + {
34555 + $wpml_element_type = apply_filters( 'wpml_element_type', $taxn );
34556 + $pars['element_id'] = $xtermid->term_id;
34557 + $pars['element_type'] = $wpml_element_type;
34558 + $pars['language_code'] = $wpml_lang;
34559 + $pars['trid'] = FALSE;
34560 + $pars['source_language_code'] = NULL;
34561 + do_action('wpml_set_element_language_details', $pars);
34562 + }
34563 + if(!is_numeric($taxval[$ii]))
34564 + {
34565 + $taxval[$ii] = intval($xtermid->term_id);
34566 + }
34567 + else
34568 + {
34569 + $taxval[] = intval($xtermid->term_id);
34570 + }
34571 + }
34572 + }
34573 + }
34574 + else
34575 + {
34576 + if(!is_numeric($taxval[$ii]))
34577 + {
34578 + $taxval[$ii] = intval($hp);
34579 + }
34580 + else
34581 + {
34582 + $taxval[] = intval($hp);
34583 + }
34584 + }
34585 + }
34586 + }
34587 + }
34588 + wp_set_post_terms($post_id, $taxval, $taxn, true);
34589 + }
34590 + else
34591 + {
34592 + $jterms = array_map('trim', explode(',', $taxval));
34593 + wp_set_post_terms($post_id, $jterms, $taxn, true);
34594 + }
34595 + }
34596 + }
34597 + if ($post_categories != '')
34598 + {
34599 + if($post_type == 'product')
34600 + {
34601 + $product_tax = 'product_cat';
34602 + }
34603 + else
34604 + {
34605 + $product_tax = 'category';
34606 + }
34607 + foreach($extra_cats as $extra_cat)
34608 + {
34609 + $extra_cat = trim($extra_cat);
34610 + $extra_cat = strip_tags($extra_cat);
34611 + $extra_cat = preg_replace('#^\d+\.\s*#', '', $extra_cat);
34612 + if(empty($extra_cat))
34613 + {
34614 + continue;
34615 + }
34616 + if ($skip_inexist == '1')
34617 + {
34618 + if(!term_exists($extra_cat, $product_tax))
34619 + {
34620 + continue;
34621 + }
34622 + }
34623 + $termid = aiomatic_create_terms($product_tax, null, trim($extra_cat));
34624 + wp_set_post_terms($post_id, $termid, $product_tax, true);
34625 + if($wpml_lang != '' && function_exists('pll_set_term_language'))
34626 + {
34627 + foreach($termid as $tx)
34628 + {
34629 + pll_set_term_language($tx, $wpml_lang);
34630 + }
34631 + }
34632 + elseif($wpml_lang != '' && has_filter('wpml_object_id'))
34633 + {
34634 + $wpml_element_type = apply_filters( 'wpml_element_type', 'product_cat' );
34635 + foreach($termid as $tx)
34636 + {
34637 + $pars['element_id'] = $tx;
34638 + $pars['element_type'] = $wpml_element_type;
34639 + $pars['language_code'] = $wpml_lang;
34640 + $pars['trid'] = FALSE;
34641 + $pars['source_language_code'] = NULL;
34642 + do_action('wpml_set_element_language_details', $pars);
34643 + }
34644 + }
34645 + }
34646 + }
34647 + if (isset($my_post['tags_input']) && $my_post['tags_input'] != '')
34648 + {
34649 + if($post_type == 'product')
34650 + {
34651 + wp_set_post_terms($post_id, $my_post['tags_input'], 'product_tag', true);
34652 + }
34653 + }
34654 + $tax_rez = wp_set_object_terms( $post_id, 'aiomatic_' . $type . '_' . $param, 'coderevolution_post_source', true);
34655 + if (is_wp_error($tax_rez)) {
34656 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
34657 + aiomatic_log_to_file('wp_set_object_terms failed for: ' . $post_id . '!');
34658 + }
34659 + }
34660 + if($post_type == 'topic' && $parent_id != '')
34661 + {
34662 + update_post_meta($post_id, '_bbp_forum_id', $parent_id);
34663 + update_post_meta($post_id, '_bbp_topic_id', $post_id);
34664 + update_post_meta($post_id, '_bbp_voice_count', '0');
34665 + update_post_meta($post_id, '_bbp_reply_count', '0');
34666 + update_post_meta($post_id, '_bbp_reply_count_hidden', '0');
34667 + update_post_meta($post_id, '_bbp_last_reply_id', '0');
34668 + update_post_meta($post_id, '_bbp_last_active_id', $post_id);
34669 + update_post_meta($post_id, '_bbp_last_active_time', get_post_field( 'post_date', $topic_id, 'db' ));
34670 + do_action( 'bbp_insert_topic', (int) $post_id, (int) $parent_id );
34671 + }
34672 + if($post_type == 'reply' && $parent_id != '')
34673 + {
34674 + if(function_exists('bbp_get_topic_forum_id'))
34675 + {
34676 + $forum_aidi = bbp_get_topic_forum_id($parent_id);
34677 + if(empty($forum_aidi))
34678 + {
34679 + $forum_aidi = 0;
34680 + }
34681 + }
34682 + else
34683 + {
34684 + $forum_aidi = 0;
34685 + }
34686 + do_action( 'bbp_insert_reply', (int) $post_id, (int) $parent_id, (int) $forum_aidi );
34687 + }
34688 + if(($post_categories != '' && !in_array($def_cat_name, $extra_cats)) || (isset($my_post['taxo_input']) && stristr($cat_str, 'category') !== false && strstr($cat_str, $def_cat_name) === false && !in_array($def_cat_name, $extra_cats)))
34689 + {
34690 + $new_categories = wp_get_post_categories($post_id);
34691 + if(isset($default_categories) && !($default_categories == $new_categories))
34692 + {
34693 + foreach($default_categories as $dc)
34694 + {
34695 + $rem_cat = get_category( $dc );
34696 + wp_remove_object_terms( $post_id, $rem_cat->slug, 'category' );
34697 + }
34698 + }
34699 + }
34700 + aiomatic_addPostMeta_special($post_id, $param, $type, $current_keyword, $rule_unique_id);
34701 + if (isset($aiomatic_Main_Settings['draft_first']) && $aiomatic_Main_Settings['draft_first'] == 'on' && $draft_me == true)
34702 + {
34703 + aiomatic_change_post_status($post_id, 'publish');
34704 + }
34705 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34706 + {
34707 + return array($post_id, get_permalink($post_id));
34708 + }
34709 + $block_results[$block_id] = array($current_block['type'], $post_id, get_permalink($post_id));
34710 + }
34711 + }
34712 + else
34713 + {
34714 + aiomatic_log_to_file('Failed to insert post into wp database(6)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
34715 + if($critical == '1')
34716 + {
34717 + return 'fail';
34718 + }
34719 + else
34720 + {
34721 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34722 + {
34723 + return array('', '');
34724 + }
34725 + $block_results[$block_id] = array($current_block['type'], '', '');
34726 + continue;
34727 + }
34728 + }
34729 + }
34730 + elseif($current_block['type'] == 'if_block')
34731 + {
34732 + $condition = $current_block['parameters']['condition'];
34733 + $condition = aiomatic_replace_omniblocks_data($condition, $current_keyword, $kiwis, $block_results, $load_variables);
34734 + $evaluation_method = $current_block['parameters']['evaluation_method'];
34735 + $expected_value = $current_block['parameters']['expected_value'];
34736 + $expected_value = aiomatic_replace_omniblocks_data($expected_value, $current_keyword, $kiwis, $block_results, $load_variables);
34737 + $true_blocks = $current_block['parameters']['true_blocks'];
34738 + $true_blocks = aiomatic_replace_omniblocks_data($true_blocks, $current_keyword, $kiwis, $block_results, $load_variables);
34739 + $true_blocks = explode(',', $true_blocks);
34740 + $true_blocks = array_map('trim', $true_blocks);
34741 + $true_blocks = array_filter($true_blocks);
34742 + $false_blocks = $current_block['parameters']['false_blocks'];
34743 + $false_blocks = aiomatic_replace_omniblocks_data($false_blocks, $current_keyword, $kiwis, $block_results, $load_variables);
34744 + $false_blocks = explode(',', $false_blocks);
34745 + $false_blocks = array_map('trim', $false_blocks);
34746 + $false_blocks = array_filter($false_blocks);
34747 + if (empty($evaluation_method))
34748 + {
34749 + if($critical == '1')
34750 + {
34751 + aiomatic_log_to_file('Halting execution (empty evaluation_method), as critical block failed (ID ' . esc_html($block_id) . ')');
34752 + return 'fail';
34753 + }
34754 + else
34755 + {
34756 + aiomatic_log_to_file('Empty evaluation_method entered');
34757 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34758 + {
34759 + return array('');
34760 + }
34761 + $block_results[$block_id] = array($current_block['type']);
34762 + continue;
34763 + }
34764 + }
34765 + $new_blocks = [];
34766 + $condition_met = 'not_run';
34767 + if($evaluation_method == 'equals')
34768 + {
34769 + if($condition == $expected_value)
34770 + {
34771 + $condition_met = true;
34772 + }
34773 + else
34774 + {
34775 + $condition_met = false;
34776 + }
34777 + }
34778 + elseif($evaluation_method == 'not_equals')
34779 + {
34780 + if($condition != $expected_value)
34781 + {
34782 + $condition_met = true;
34783 + }
34784 + else
34785 + {
34786 + $condition_met = false;
34787 + }
34788 + }
34789 + elseif($evaluation_method == 'contains')
34790 + {
34791 + if(strstr($condition, $expected_value) !== false)
34792 + {
34793 + $condition_met = true;
34794 + }
34795 + else
34796 + {
34797 + $condition_met = false;
34798 + }
34799 + }
34800 + elseif($evaluation_method == 'not_contains')
34801 + {
34802 + if(strstr($condition, $expected_value) === false)
34803 + {
34804 + $condition_met = true;
34805 + }
34806 + else
34807 + {
34808 + $condition_met = false;
34809 + }
34810 + }
34811 + elseif($evaluation_method == 'greater_than')
34812 + {
34813 + if(floatval($conditio) > floatval($expected_value))
34814 + {
34815 + $condition_met = true;
34816 + }
34817 + else
34818 + {
34819 + $condition_met = false;
34820 + }
34821 + }
34822 + elseif($evaluation_method == 'starts_with')
34823 + {
34824 + if(aiomatic_starts_with($conditio, $expected_value) === true)
34825 + {
34826 + $condition_met = true;
34827 + }
34828 + else
34829 + {
34830 + $condition_met = false;
34831 + }
34832 + }
34833 + elseif($evaluation_method == 'not_starts_with')
34834 + {
34835 + if(aiomatic_starts_with($conditio, $expected_value) === false)
34836 + {
34837 + $condition_met = true;
34838 + }
34839 + else
34840 + {
34841 + $condition_met = false;
34842 + }
34843 + }
34844 + elseif($evaluation_method == 'ends_with')
34845 + {
34846 + if(aiomatic_ends_with($conditio, $expected_value) === true)
34847 + {
34848 + $condition_met = true;
34849 + }
34850 + else
34851 + {
34852 + $condition_met = false;
34853 + }
34854 + }
34855 + elseif($evaluation_method == 'not_ends_with')
34856 + {
34857 + if(aiomatic_ends_with($conditio, $expected_value) === false)
34858 + {
34859 + $condition_met = true;
34860 + }
34861 + else
34862 + {
34863 + $condition_met = false;
34864 + }
34865 + }
34866 + else
34867 + {
34868 + aiomatic_log_to_file('Unknown expected_value parameter sent: ' . $evaluation_method);
34869 + }
34870 + if($condition_met === true)
34871 + {
34872 + if(!empty($true_blocks))
34873 + {
34874 + foreach($true_blocks as $my_block)
34875 + {
34876 + if($my_block == $block_id)
34877 + {
34878 + aiomatic_log_to_file('You cannot execute the same IF OmniBlock in a loop: ' . $block_id);
34879 + continue;
34880 + }
34881 + $bl_found = false;
34882 + foreach($block_data as $swindex => $sw_current_block)
34883 + {
34884 + if($my_block == $sw_current_block['identifier'])
34885 + {
34886 + $sw_current_block['parameters']['disabled'] = 0;
34887 + $new_blocks[] = $sw_current_block;
34888 + $bl_found = true;
34889 + }
34890 + }
34891 + if($bl_found === false)
34892 + {
34893 + aiomatic_log_to_file('Failed to find True condition block ID: ' . $my_block);
34894 + }
34895 + }
34896 + }
34897 + }
34898 + elseif($condition_met === false)
34899 + {
34900 + if(!empty($false_blocks))
34901 + {
34902 + foreach($false_blocks as $my_block)
34903 + {
34904 + if($my_block == $block_id)
34905 + {
34906 + aiomatic_log_to_file('You cannot execute the same IF OmniBlock in a loop: ' . $block_id);
34907 + continue;
34908 + }
34909 + $bl_found = false;
34910 + foreach($block_data as $swindex => $sw_current_block)
34911 + {
34912 + if($my_block == $sw_current_block['identifier'])
34913 + {
34914 + $sw_current_block['parameters']['disabled'] = 0;
34915 + $new_blocks[] = $sw_current_block;
34916 + $bl_found = true;
34917 + }
34918 + }
34919 + if($bl_found === false)
34920 + {
34921 + aiomatic_log_to_file('Failed to find False condition block ID: ' . $my_block);
34922 + }
34923 + }
34924 + }
34925 + }
34926 + else
34927 + {
34928 + aiomatic_log_to_file('Error in processing IF block, condition state is unknown: ' . $condition_met);
34929 + if($critical == '1')
34930 + {
34931 + return 'fail';
34932 + }
34933 + else
34934 + {
34935 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34936 + {
34937 + return array('');
34938 + }
34939 + $block_results[$block_id] = array($current_block['type']);
34940 + continue;
34941 + }
34942 + }
34943 + if(!empty($new_blocks))
34944 + {
34945 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
34946 + {
34947 + $blids = [];
34948 + foreach($new_blocks as $nb)
34949 + {
34950 + $blids[] = $nb['identifier'];
34951 + }
34952 + aiomatic_log_to_file('Adding new OmniBlock IDs to the queue: ' . implode(',', $blids));
34953 + }
34954 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34955 + {
34956 + $stop_omnmiblock_id = array_values(array_slice($new_blocks, -1))[0];
34957 + }
34958 + array_splice($block_data, $index, 0, $new_blocks);
34959 + $block_results[$block_id] = array($current_block['type']);
34960 + }
34961 + else
34962 + {
34963 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34964 + {
34965 + return array('ok');
34966 + }
34967 + $block_results[$block_id] = array($current_block['type']);
34968 + }
34969 + }
34970 + elseif($current_block['type'] == 'jump_block')
34971 + {
34972 + $jumpto = $current_block['parameters']['jumpto'];
34973 + $jumpto = aiomatic_replace_omniblocks_data($jumpto, $current_keyword, $kiwis, $block_results, $load_variables);
34974 + $jumpto = explode(',', $jumpto);
34975 + $jumpto = trim($jumpto[array_rand($jumpto)]);
34976 + if(empty($jumpto))
34977 + {
34978 + aiomatic_log_to_file('Error in processing JUMP block, empty jump block ID provided');
34979 + if($critical == '1')
34980 + {
34981 + return 'fail';
34982 + }
34983 + else
34984 + {
34985 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
34986 + {
34987 + return array('');
34988 + }
34989 + $block_results[$block_id] = array($current_block['type']);
34990 + continue;
34991 + }
34992 + }
34993 + $jumpfound = false;
34994 + for($searchindex = 0; $searchindex < count($block_data); $searchindex++)
34995 + {
34996 + if($block_data[$searchindex]['identifier'] == trim($jumpto))
34997 + {
34998 + $index = $searchindex;
34999 + $jumpfound = true;
35000 + break;
35001 + }
35002 + }
35003 + if($jumpfound == false)
35004 + {
35005 + aiomatic_log_to_file('Error in processing JUMP block, jump block ID not found: ' . $jumpto);
35006 + if($critical == '1')
35007 + {
35008 + return 'fail';
35009 + }
35010 + else
35011 + {
35012 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35013 + {
35014 + return array('');
35015 + }
35016 + $block_results[$block_id] = array($current_block['type']);
35017 + continue;
35018 + }
35019 + }
35020 + else
35021 + {
35022 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35023 + {
35024 + $stop_omnmiblock_id = $block_data[$index]['identifier'];
35025 + }
35026 + $block_results[$block_id] = array($current_block['type']);
35027 + }
35028 + }
35029 + elseif($current_block['type'] == 'wait_block')
35030 + {
35031 + $wait = $current_block['parameters']['wait'];
35032 + $wait = aiomatic_replace_omniblocks_data($wait, $current_keyword, $kiwis, $block_results, $load_variables);
35033 + $wait = explode(',', $wait);
35034 + $wait = trim($wait[array_rand($wait)]);
35035 + if(empty($wait))
35036 + {
35037 + aiomatic_log_to_file('Error in processing WAIT block, empty wait time provided');
35038 + if($critical == '1')
35039 + {
35040 + return 'fail';
35041 + }
35042 + else
35043 + {
35044 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35045 + {
35046 + return array('');
35047 + }
35048 + $block_results[$block_id] = array($current_block['type']);
35049 + continue;
35050 + }
35051 + }
35052 + try
35053 + {
35054 + $microseconds = aiomatic_parseTime($wait);
35055 + usleep((int)$microseconds);
35056 + }
35057 + catch (Exception $e)
35058 + {
35059 + aiomatic_log_to_file('Error in processing SLEEP block: ' . $e->getMessage());
35060 + if($critical == '1')
35061 + {
35062 + return 'fail';
35063 + }
35064 + else
35065 + {
35066 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35067 + {
35068 + return array('');
35069 + }
35070 + $block_results[$block_id] = array($current_block['type']);
35071 + continue;
35072 + }
35073 + }
35074 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35075 + {
35076 + $stop_omnmiblock_id = $block_data[$index]['identifier'];
35077 + }
35078 + $block_results[$block_id] = array($current_block['type']);
35079 + }
35080 + elseif($current_block['type'] == 'exit_block')
35081 + {
35082 + aiomatic_log_to_file('Exit OmniBlock encountered, stopping execution and exiting OmniBlock queue: ' . $current_block['type']);
35083 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35084 + {
35085 + return array('');
35086 + }
35087 + break;
35088 + }
35089 + else
35090 + {
35091 + aiomatic_log_to_file('Unknown OmniBlock type found: ' . $current_block['type']);
35092 + if(!empty($stop_omnmiblock_id) && $stop_omnmiblock_id == $block_id)
35093 + {
35094 + return array('');
35095 + }
35096 + }
35097 + }
35098 + if($title_once == '1')
35099 + {
35100 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35101 + aiomatic_log_to_file('Remembering processed keyword: ' . $current_keyword);
35102 + }
35103 + aiomatic_add_processed_keyword($current_keyword);
35104 + }
35105 + $count++;
35106 + }
35107 +
35108 + if(count($keyword_arr) == 0 && $skipt > 0)
35109 + {
35110 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
35111 + {
35112 + if($count === 1)
35113 + {
35114 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
35115 + foreach($email_list as $thisaddr)
35116 + {
35117 + $thisaddr = trim($thisaddr);
35118 + try
35119 + {
35120 + $admin_mail = get_option('admin_email');
35121 + $to = $thisaddr;
35122 + $subject = '[Aimogen] OmniBlocks Keywords depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
35123 + $message = 'OmniBlocks Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its keywords list, no new posts publised! Website URL: ' . get_site_url();
35124 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
35125 + $headers[] = 'Reply-To: ' . $admin_mail;
35126 + $headers[] = 'X-Mailer: PHP/' . phpversion();
35127 + $headers[] = 'Content-Type: text/html';
35128 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
35129 + wp_mail($to, $subject, $message, $headers);
35130 + }
35131 + catch (Exception $e) {
35132 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35133 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
35134 + }
35135 + }
35136 + }
35137 + }
35138 + }
35139 + aiomatic_log_to_file('All OmniBlocks keywords already processed, nothing to do.');
35140 + if($auto == 1)
35141 + {
35142 + aiomatic_clearFromList($param, $type);
35143 + }
35144 + return 'nochange';
35145 + }
35146 + }
35147 + elseif($type == 7)
35148 + {
35149 + if(!function_exists('is_plugin_active'))
35150 + {
35151 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
35152 + }
35153 + if (!is_plugin_active('aiomatic-extension-wpforo/aiomatic-extension-wpforo.php'))
35154 + {
35155 + aiomatic_log_to_file('This feature requires the Aimogen Extension for AI Forum Creation plugin to be installed and activated!');
35156 + if($auto == 1)
35157 + {
35158 + aiomatic_clearFromList($param, $type);
35159 + }
35160 + return 'fail';
35161 + }
35162 + if (!is_plugin_active('wpforo/wpforo.php'))
35163 + {
35164 + aiomatic_log_to_file('This feature requires the wpForo plugin to be installed and activated!');
35165 + if($auto == 1)
35166 + {
35167 + aiomatic_clearFromList($param, $type);
35168 + }
35169 + return 'fail';
35170 + }
35171 + if($post_title != '')
35172 + {
35173 + $post_title_lines = preg_split('/\r\n|\r|\n/', $post_title);
35174 + }
35175 + else
35176 + {
35177 + $post_title_lines = array();
35178 + }
35179 + $additional_kws = array();
35180 + $user_name = '';
35181 + $featured_image = '';
35182 + $post_cats = '';
35183 + $post_tagz = '';
35184 + $post_excerpt = '';
35185 + $final_content = '';
35186 + $postID = '';
35187 + $heading_val = '';
35188 + $image_query = '';
35189 + $temp_post = '';
35190 + $cntx = count($post_title_lines);
35191 + $rss_feeds = array();
35192 + for($ji = 0; $ji < $cntx; $ji++)
35193 + {
35194 + if (filter_var($post_title_lines[$ji], FILTER_VALIDATE_URL) !== false)
35195 + {
35196 + if(aiomatic_endsWith($post_title_lines[$ji], '.txt'))
35197 + {
35198 +
35199 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing txt file'));
35200 + $txt_content = aiomatic_get_web_page($post_title_lines[$ji]);
35201 + if ($txt_content === FALSE)
35202 + {
35203 + aiomatic_log_to_file('Failed to read text file: ' . $post_title_lines[$ji]);
35204 + unset($post_title_lines[$ji]);
35205 + if($auto == 1)
35206 + {
35207 + aiomatic_log_to_file($param);
35208 + }
35209 + continue;
35210 + }
35211 + unset($post_title_lines[$ji]);
35212 + $additional_kws = preg_split('/\r\n|\r|\n/', $txt_content);
35213 + }
35214 + else
35215 + {
35216 + aiomatic_log_to_file('Trying to parse RSS feed items(1): ' . $post_title_lines[$ji]);
35217 + try
35218 + {
35219 + if(!class_exists('SimplePie_Autoloader', false))
35220 + {
35221 + require_once(dirname(__FILE__) . "/res/simplepie/autoloader.php");
35222 + }
35223 + }
35224 + catch(Exception $e)
35225 + {
35226 + unset($post_title_lines[$ji]);
35227 + aiomatic_log_to_file('Exception thrown in SimplePie autoloader: ' . $e->getMessage());
35228 + if($auto == 1)
35229 + {
35230 + aiomatic_log_to_file($param);
35231 + }
35232 + continue;
35233 + }
35234 +
35235 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Processing RSS feed'));
35236 + $feed = new SimplePie();
35237 + $feed->set_timeout(120);
35238 + $feed->set_feed_url($post_title_lines[$ji]);
35239 + $feed->enable_cache(false);
35240 + $feed->strip_htmltags(false);
35241 + $feed->init();
35242 + $feed->handle_content_type();
35243 + if ($feed->error())
35244 + {
35245 + aiomatic_log_to_file('Error in parsing RSS feed: ' . $feed->error() . ' for ' . $post_title_lines[$ji]);
35246 + unset($post_title_lines[$ji]);
35247 + if($auto == 1)
35248 + {
35249 + aiomatic_clearFromList($param, $type);
35250 + }
35251 + continue;
35252 + }
35253 + $items = $feed->get_items();
35254 + $zero = true;
35255 + foreach($items as $itemx)
35256 + {
35257 + if($zero == true)
35258 + {
35259 + $post_link = trim($itemx->get_permalink());
35260 + if(isset($rss_items[$post_link]))
35261 + {
35262 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35263 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
35264 + }
35265 + continue;
35266 + }
35267 + if ($fauthor = $itemx->get_author())
35268 + {
35269 + $user_name = $fauthor->get_name();
35270 + }
35271 + else
35272 + {
35273 + $user_name = '';
35274 + }
35275 + $feed_cats = array();
35276 + $varx = $itemx->get_categories();
35277 + if(is_array($varx))
35278 + {
35279 + foreach ($varx as $xcategory)
35280 + {
35281 + $feed_cats[] = $xcategory->get_label();
35282 + }
35283 + }
35284 + $post_cats = implode(',', $feed_cats);
35285 + $post_excerpt = $itemx->get_description();
35286 + $final_content = $itemx->get_content();
35287 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link, 'author' => $user_name, 'cats' => $post_cats, 'excerpt' => $post_excerpt, 'content' => $final_content );
35288 + }
35289 + else
35290 + {
35291 + $post_link_temp = trim($itemx->get_permalink());
35292 + if(isset($rss_items[$post_link_temp]))
35293 + {
35294 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35295 + aiomatic_log_to_file('Skipping RSS title because it was already processed before: ' . $itemx->get_title());
35296 + }
35297 + continue;
35298 + }
35299 + if ($fauthor = $itemx->get_author())
35300 + {
35301 + $user_name_temp = $fauthor->get_name();
35302 + }
35303 + else
35304 + {
35305 + $user_name_temp = '';
35306 + }
35307 + $feed_cats = array();
35308 + $varx = $itemx->get_categories();
35309 + if(is_array($varx))
35310 + {
35311 + foreach ($varx as $xcategory)
35312 + {
35313 + $feed_cats[] = $xcategory->get_label();
35314 + }
35315 + }
35316 + $post_cats_temp = implode(',', $feed_cats);
35317 + $post_excerpt_temp = $itemx->get_description();
35318 + $final_content_temp = $itemx->get_content();
35319 + $rss_feeds[$itemx->get_title()] = array('url' => $post_link_temp, 'author' => $user_name_temp, 'cats' => $post_cats_temp, 'excerpt' => $post_excerpt_temp, 'content' => $final_content_temp );
35320 + }
35321 + $additional_kws[] = $itemx->get_title();
35322 + $zero = false;
35323 + }
35324 + unset($post_title_lines[$ji]);
35325 + }
35326 + }
35327 + }
35328 + if(count($additional_kws) > 0)
35329 + {
35330 + $post_title_lines = array_merge($post_title_lines, $additional_kws);
35331 + }
35332 + $post_title_lines = array_unique($post_title_lines);
35333 + $spintax = new Aiomatic_Spintax();
35334 + $orig_ai_command = $ai_command;
35335 + $orig_ai_command_title = $title_ai_command;
35336 + $orig_ai_command_image = $ai_command_image;
35337 + $already_spinned = 0;
35338 + if(isset($aiomatic_Main_Settings['attr_text']) && $aiomatic_Main_Settings['attr_text'] != '')
35339 + {
35340 + $img_attr = $aiomatic_Main_Settings['attr_text'];
35341 + }
35342 + else
35343 + {
35344 + $img_attr = '';
35345 + }
35346 + $headings_arr = array();
35347 + $images_arr = array();
35348 + $no_init_topics = false;
35349 + while(true)
35350 + {
35351 + $thread_id = '';
35352 + $post_link = '';
35353 + $user_name = '';
35354 + $post_cats = '';
35355 + $post_excerpt = '';
35356 + $final_content = '';
35357 + $update_post_id = '';
35358 + $featured_image = '';
35359 + $post_tagz = '';
35360 + $postID = '';
35361 + $old_title = '';
35362 + if ($count > intval($max)) {
35363 + break;
35364 + }
35365 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Starting processing topic ID #' . $count));
35366 + $current_section = '';
35367 + $post_sections = '';
35368 + $post_topic = '';
35369 + $post_title_keywords = '';
35370 + $posting_mode = 'title';
35371 + if($posting_mode == 'title')
35372 + {
35373 + $headings_arr_copy = $headings_arr;
35374 + $added_img_list = array();
35375 + $raw_img_list = array();
35376 + $full_result_list = array();
35377 + $added_images = 0;
35378 + $heading_results = array();
35379 + if(count($post_title_lines) == 0)
35380 + {
35381 + break;
35382 + }
35383 + if ($count > intval($max)) {
35384 + break;
35385 + }
35386 + if (!isset($aiomatic_Main_Settings['no_random_titles']) || $aiomatic_Main_Settings['no_random_titles'] != 'on')
35387 + {
35388 + $current_index = array_rand($post_title_lines);
35389 + }
35390 + else
35391 + {
35392 + $current_index = array_key_first($post_title_lines);
35393 + }
35394 + $post_title = trim($post_title_lines[$current_index]);
35395 + $ptlprocessed = explode('!###!', $post_title);
35396 + if(isset($ptlprocessed[1]) && !empty($ptlprocessed[1]) && !empty($ptlprocessed[0]))
35397 + {
35398 + $post_title = $ptlprocessed[0];
35399 + $post_title_keywords = $ptlprocessed[1];
35400 + }
35401 + if(isset($rss_feeds[$post_title]))
35402 + {
35403 + $post_link = $rss_feeds[$post_title]['url'];
35404 + $user_name = $rss_feeds[$post_title]['author'];
35405 + $post_cats = $rss_feeds[$post_title]['cats'];
35406 + $post_excerpt = $rss_feeds[$post_title]['excerpt'];
35407 + $final_content = $rss_feeds[$post_title]['content'];
35408 + }
35409 + $tprepp = $spintax->Parse($post_title);
35410 + if($tprepp != false && $tprepp != '')
35411 + {
35412 + $post_title = $tprepp;
35413 + }
35414 + if($title_once == '1' && aiomatic_check_processed_keyword($post_title))
35415 + {
35416 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35417 + aiomatic_log_to_file('Skipping already processed topic title: ' . $post_title);
35418 + }
35419 + unset($post_title_lines[$current_index]);
35420 + continue;
35421 + }
35422 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
35423 + {
35424 + aiomatic_log_to_file('Starting processing post title: ' . $post_title);
35425 + }
35426 + $old_title = $post_title;
35427 + $already_spinned = 0;
35428 + if (filter_var($post_title, FILTER_VALIDATE_URL) === false && stristr($post_title, '%%ai_generated_title%%') === false)
35429 + {
35430 + unset($post_title_lines[$current_index]);
35431 + }
35432 + $allmodels = aiomatic_get_all_models();
35433 + if(!empty($global_prepend))
35434 + {
35435 + $global_prepend = aiomatic_replaceSynergyShortcodes($global_prepend);
35436 + $global_prepend = aiomatic_replaceAIPostShortcodes($global_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
35437 + }
35438 + if(!empty($global_append))
35439 + {
35440 + $global_append = aiomatic_replaceSynergyShortcodes($global_append);
35441 + $global_append = aiomatic_replaceAIPostShortcodes($global_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
35442 + }
35443 + if($custom_shortcodes != '')
35444 + {
35445 + $custom_shortcodes_arr = preg_split('/\r\n|\r|\n/', $custom_shortcodes);
35446 + foreach($custom_shortcodes_arr as $my_short)
35447 + {
35448 + $name_part = explode('=>', $my_short);
35449 + if(isset($name_part[1]) && !empty(trim($name_part[1])))
35450 + {
35451 + $shortname = trim($name_part[0]);
35452 + if(strstr($post_title, '%%' . $shortname . '%%'))
35453 + {
35454 + $shortval = '';
35455 + $name_part[1] = str_replace('%%post_title%%', $post_title, $name_part[1]);
35456 + $ai_part = explode('@@', $name_part[1]);
35457 + if(isset($ai_part[1]) && !empty(trim($ai_part[1])))
35458 + {
35459 + if(!in_array(trim($ai_part[0]), $allmodels))
35460 + {
35461 + $aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
35462 + }
35463 + else
35464 + {
35465 + $aimodel = trim($ai_part[0]);
35466 + }
35467 + $zai_command = trim($ai_part[1]);
35468 + $zai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $zai_command);
35469 + $zai_command = aiomatic_replaceSynergyShortcodes($zai_command);
35470 + $zai_command = aiomatic_replaceAIPostShortcodes($zai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
35471 + $zai_command = str_replace('%%topic_user_name%%', $post_user_name, $zai_command);
35472 + $zai_command = str_replace('%%language%%', $content_language, $zai_command);
35473 + $zai_command = str_replace('%%writing_style%%', $writing_style, $zai_command);
35474 + $zai_command = str_replace('%%writing_tone%%', $writing_tone, $zai_command);
35475 + foreach($custom_shortcodes_arr as $my_check_short)
35476 + {
35477 + $check_name_part = explode('=>', $my_check_short);
35478 + if(isset($check_name_part[1]) && !empty(trim($check_name_part[1])))
35479 + {
35480 + $check_shortname = trim($check_name_part[0]);
35481 + if($shortname !== $check_shortname && strstr($zai_command, '%%' . $check_shortname . '%%') !== false)
35482 + {
35483 + $check_shortval = '';
35484 + $check_ai_part = explode('@@', $check_name_part[1]);
35485 + if(!in_array(trim($check_ai_part[0]), $allmodels))
35486 + {
35487 + $check_aimodel = aiomatic_get_default_model_name($aiomatic_Main_Settings);
35488 + }
35489 + else
35490 + {
35491 + $check_aimodel = trim($check_ai_part[0]);
35492 + }
35493 + $check_ai_command = trim($check_ai_part[1]);
35494 + $aierror = '';
35495 +
35496 + $check_ai_command = aiomatic_replaceSynergyShortcodes($check_ai_command);
35497 + $check_ai_command = aiomatic_replaceAIPostShortcodes($check_ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, '', $custom_shortcodes, '', '');
35498 +
35499 + $check_ai_command = apply_filters('aiomatic_replace_aicontent_shortcode', $check_ai_command);
35500 + $check_generated_text = aiomatic_generate_custom_shortcode_content($token, $check_aimodel, $check_ai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
35501 + if($check_generated_text === false)
35502 + {
35503 + aiomatic_log_to_file('Custom nested shortcode generator error: ' . $aierror);
35504 + }
35505 + else
35506 + {
35507 + $check_shortval = trim(trim($check_generated_text), '.');
35508 + }
35509 + $zai_command = str_replace('%%' . $check_shortname . '%%', $check_shortval, $zai_command);
35510 + }
35511 + }
35512 + }
35513 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating title based shortcodes'));
35514 + $aierror = '';
35515 + $generated_text = aiomatic_generate_custom_shortcode_content($token, $aimodel, $zai_command, $global_prepend, $global_append, $aiomatic_Main_Settings, $aierror);
35516 + if($generated_text === false)
35517 + {
35518 + aiomatic_log_to_file('Custom shortcode generator error: ' . $aierror);
35519 + }
35520 + else
35521 + {
35522 + $shortval = trim(trim($generated_text), '.');
35523 + }
35524 + }
35525 + $post_title = str_replace('%%' . $shortname . '%%', $shortval, $post_title);
35526 + }
35527 + }
35528 + }
35529 + }
35530 + if($user_name_type == 'rand')
35531 + {
35532 + if($user_role != 'any')
35533 + {
35534 + $randid = aiomatic_display_random_user([$user_role]);
35535 + if($randid === false)
35536 + {
35537 + $post_user_name = aiomatic_randomName();
35538 + }
35539 + else
35540 + {
35541 + $post_user_name = $randid->data->user_login;
35542 + }
35543 + }
35544 + else
35545 + {
35546 + $randid = aiomatic_display_random_user();
35547 + if($randid === false)
35548 + {
35549 + $post_user_name = aiomatic_randomName();
35550 + }
35551 + else
35552 + {
35553 + $post_user_name = $randid->data->user_login;
35554 + }
35555 + }
35556 + }
35557 + if(stristr($post_title, '%%ai_generated_title%%') !== false || $title_source == 'ai')
35558 + {
35559 + if($orig_ai_command_title == '')
35560 + {
35561 + $orig_ai_command_title = $post_title;
35562 + }
35563 + if($orig_ai_command_title != '')
35564 + {
35565 + $title_ai_command = $orig_ai_command_title;
35566 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
35567 + {
35568 + $title_ai_command = preg_split('/\r\n|\r|\n/', $title_ai_command);
35569 + $title_ai_command = array_filter($title_ai_command);
35570 + if(count($title_ai_command) > 0)
35571 + {
35572 + $title_ai_command = $title_ai_command[array_rand($title_ai_command)];
35573 + }
35574 + else
35575 + {
35576 + $title_ai_command = '';
35577 + }
35578 + }
35579 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
35580 + if(!empty($title_ai_command))
35581 + {
35582 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
35583 + }
35584 + else
35585 + {
35586 + $title_ai_command = trim(strip_tags($post_title));
35587 + }
35588 + $title_ai_command = trim($title_ai_command);
35589 + if (filter_var($title_ai_command, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($title_ai_command, '.txt'))
35590 + {
35591 + $txt_content = aiomatic_get_web_page($title_ai_command);
35592 + if ($txt_content !== FALSE)
35593 + {
35594 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
35595 + $txt_content = array_filter($txt_content);
35596 + if(count($txt_content) > 0)
35597 + {
35598 + $txt_content = $txt_content[array_rand($txt_content)];
35599 + if(trim($txt_content) != '')
35600 + {
35601 + $title_ai_command = $txt_content;
35602 + $title_ai_command = aiomatic_replaceSynergyShortcodes($title_ai_command);
35603 + $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, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
35604 + }
35605 + }
35606 + }
35607 + }
35608 + $title_ai_command = str_replace('%%topic_user_name%%', $post_user_name, $title_ai_command);
35609 + $title_ai_command = str_replace('%%language%%', $content_language, $title_ai_command);
35610 + $title_ai_command = str_replace('%%writing_style%%', $writing_style, $title_ai_command);
35611 + $title_ai_command = str_replace('%%writing_tone%%', $writing_tone, $title_ai_command);
35612 + if(empty($title_ai_command))
35613 + {
35614 + aiomatic_log_to_file('Empty API post title seed expression provided!');
35615 + }
35616 + else
35617 + {
35618 + if(aiomatic_strlen($title_ai_command) > $max_seed_tokens * 4)
35619 + {
35620 + $title_ai_command = aiomatic_substr($title_ai_command, 0, (0 - ($max_seed_tokens * 4)));
35621 + }
35622 + $title_ai_command = trim($title_ai_command);
35623 + if(empty($title_ai_command))
35624 + {
35625 + aiomatic_log_to_file('Empty API title seed expression provided(2)! ' . print_r($title_ai_command, true));
35626 + break;
35627 + }
35628 + $query_token_count = count(aiomatic_encode($title_ai_command));
35629 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $title_ai_command, $query_token_count);
35630 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
35631 + {
35632 + $string_len = aiomatic_strlen($title_ai_command);
35633 + $string_len = $string_len / 2;
35634 + $string_len = intval(0 - $string_len);
35635 + $title_ai_command = aiomatic_substr($title_ai_command, 0, $string_len);
35636 + $title_ai_command = trim($title_ai_command);
35637 + if(empty($title_ai_command))
35638 + {
35639 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($title_ai_command, true));
35640 + break;
35641 + }
35642 + $query_token_count = count(aiomatic_encode($title_ai_command));
35643 + $available_tokens = $max_tokens - $query_token_count;
35644 + }
35645 + $aierror = '';
35646 + $finish_reason = '';
35647 + if(!empty($global_prepend))
35648 + {
35649 + $title_ai_command = $global_prepend . ' ' . $title_ai_command;
35650 + }
35651 + if(!empty($global_append))
35652 + {
35653 + $title_ai_command = $title_ai_command . ' ' . $global_append;
35654 + }
35655 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
35656 + {
35657 + $api_service = aiomatic_get_api_service($token, $title_model);
35658 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $title_model . ')(' . $available_tokens . ') for title text: ' . $title_ai_command);
35659 + }
35660 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating title'));
35661 + $generated_text = aiomatic_generate_text($token, $title_model, $title_ai_command, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'titleID' . $param, 0, $finish_reason, $aierror, true, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
35662 + if($generated_text === false)
35663 + {
35664 + aiomatic_log_to_file('Title generator error: ' . $aierror);
35665 + break;
35666 + }
35667 + else
35668 + {
35669 + $ai_title = ucfirst(trim(trim($generated_text), '.'));
35670 + if($title_source == 'ai')
35671 + {
35672 + $post_title = $ai_title;
35673 + }
35674 + else
35675 + {
35676 + $post_title = str_ireplace('%%ai_generated_title%%', $ai_title, $post_title);
35677 + }
35678 + }
35679 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
35680 + {
35681 + $api_service = aiomatic_get_api_service($token, $title_model);
35682 + aiomatic_log_to_file('Successfully got API title result from ' . $api_service . ': ' . $post_title);
35683 + }
35684 + }
35685 + }
35686 + else
35687 + {
35688 + aiomatic_log_to_file('Empty AI title query entered.');
35689 + }
35690 + }
35691 + else
35692 + {
35693 + $post_title = aiomatic_replaceSynergyShortcodes($post_title);
35694 + }
35695 + $post_title = apply_filters('aiomatic_replace_aicontent_shortcode', $post_title);
35696 + if(empty($post_title))
35697 + {
35698 + unset($post_title_lines[$current_index]);
35699 + continue;
35700 + }
35701 + if (strpos($post_title, '%%') === false)
35702 + {
35703 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
35704 + {
35705 + $zap = aiomatic_wpforo_get_topic_by_title($post_title, $selected_forum);
35706 + if($zap !== false)
35707 + {
35708 + if($overwrite_existing)
35709 + {
35710 + $update_post_id = $zap;
35711 + }
35712 + else
35713 + {
35714 + aiomatic_log_to_file('Post with specified title is already existing, skipping it: ' . $post_title);
35715 + unset($post_title_lines[$current_index]);
35716 + continue;
35717 + }
35718 + }
35719 + }
35720 + $new_post_title = $post_title;
35721 + }
35722 + else
35723 + {
35724 + $new_post_title = $post_title;
35725 + $new_post_title = aiomatic_replaceContentShortcodes($new_post_title, $img_attr, $ai_command);
35726 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
35727 + {
35728 + $zap = aiomatic_wpforo_get_topic_by_title($new_post_title, $selected_forum);
35729 + if($zap !== false)
35730 + {
35731 + if($overwrite_existing)
35732 + {
35733 + $update_post_id = $zap;
35734 + }
35735 + else
35736 + {
35737 + aiomatic_log_to_file('Post with specified title is already existing, skipping it: ' . $post_title);
35738 + unset($post_title_lines[$current_index]);
35739 + continue;
35740 + }
35741 + }
35742 + }
35743 + }
35744 + $get_img = '';
35745 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
35746 + {
35747 + $already_spinned = '1';
35748 + }
35749 + $my_post = array();
35750 + $my_post['aiomatic_post_image'] = $get_img;
35751 + $my_post['comment_status'] = $accept_comments;
35752 + $my_post['post_author'] = $post_user_name;
35753 + $ai_command = $orig_ai_command;
35754 + $ai_command = str_replace('%%topic_user_name%%', $post_user_name, $ai_command);
35755 + $ai_command = str_replace('%%language%%', $content_language, $ai_command);
35756 + $ai_command = str_replace('%%writing_style%%', $writing_style, $ai_command);
35757 + $ai_command = str_replace('%%writing_tone%%', $writing_tone, $ai_command);
35758 + $ai_command = aiomatic_replaceSynergyShortcodes($ai_command);
35759 + if(!empty($ai_command))
35760 + {
35761 + $aicontent = aiomatic_replaceAIPostShortcodes($ai_command, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
35762 + }
35763 + else
35764 + {
35765 + $aicontent = trim(strip_tags($post_title));
35766 + }
35767 + if (filter_var($aicontent, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($aicontent, '.txt'))
35768 + {
35769 + $txt_content = aiomatic_get_web_page($aicontent);
35770 + if ($txt_content !== FALSE)
35771 + {
35772 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
35773 + $txt_content = array_filter($txt_content);
35774 + if(count($txt_content) > 0)
35775 + {
35776 + $txt_content = $txt_content[array_rand($txt_content)];
35777 + if(trim($txt_content) != '')
35778 + {
35779 + $aicontent = $txt_content;
35780 + $aicontent = aiomatic_replaceSynergyShortcodes($aicontent);
35781 + $aicontent = aiomatic_replaceAIPostShortcodes($aicontent, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
35782 + }
35783 + }
35784 + }
35785 + }
35786 + $last_char = aiomatic_substr($aicontent, -1, null);
35787 + if(!ctype_punct($last_char))
35788 + {
35789 + $aicontent .= '.';
35790 + }
35791 + if(aiomatic_strlen($aicontent) > $max_seed_tokens * 4)
35792 + {
35793 + $aicontent = aiomatic_substr($aicontent, 0, (0-($max_seed_tokens * 4)));
35794 + }
35795 + $aicontent = trim($aicontent);
35796 + if(empty($aicontent))
35797 + {
35798 + aiomatic_log_to_file('Empty API seed expression provided! ' . print_r($ai_command, true));
35799 + break;
35800 + }
35801 + $query_token_count = count(aiomatic_encode($aicontent));
35802 + $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $aicontent, $query_token_count);
35803 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
35804 + {
35805 + $string_len = aiomatic_strlen($aicontent);
35806 + $string_len = $string_len / 2;
35807 + $string_len = intval(0 - $string_len);
35808 + $aicontent = aiomatic_substr($aicontent, 0, $string_len);
35809 + $aicontent = trim($aicontent);
35810 + if(empty($aicontent))
35811 + {
35812 + aiomatic_log_to_file('Empty API seed expression provided (after processing) ' . print_r($ai_command, true));
35813 + break;
35814 + }
35815 + $query_token_count = count(aiomatic_encode($aicontent));
35816 + $available_tokens = $max_tokens - $query_token_count;
35817 + }
35818 + $aierror = '';
35819 + $finish_reason = '';
35820 + if(!empty($global_prepend))
35821 + {
35822 + $aicontent = $global_prepend . ' ' . $aicontent;
35823 + }
35824 + if(!empty($global_append))
35825 + {
35826 + $aicontent = $aicontent . ' ' . $global_append;
35827 + }
35828 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
35829 + {
35830 + $api_service = aiomatic_get_api_service($token, $model);
35831 + aiomatic_log_to_file('Calling ' . $api_service . ' (' . $assistant_id . '/' . $model . ')(' . $available_tokens . ') for text: ' . $aicontent);
35832 + }
35833 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Generating post content'));
35834 + $generated_text = aiomatic_generate_text($token, $model, $aicontent, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'contentID' . $param, 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '');
35835 + if($generated_text === false)
35836 + {
35837 + aiomatic_log_to_file($aierror);
35838 + break;
35839 + }
35840 + else
35841 + {
35842 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
35843 + {
35844 + $new_post_content = ucfirst(trim(nl2br(trim($generated_text))));
35845 + }
35846 + else
35847 + {
35848 + $new_post_content = ucfirst(trim($generated_text));
35849 + }
35850 + }
35851 + if (isset($aiomatic_Main_Settings['enable_detailed_logging']))
35852 + {
35853 + $api_service = aiomatic_get_api_service($token, $model);
35854 + aiomatic_log_to_file('Successfully got API result from ' . $api_service . '.');
35855 + }
35856 + if($min_char == '')
35857 + {
35858 + $min_char = 0;
35859 + }
35860 + else
35861 + {
35862 + $min_char = intval($min_char);
35863 + }
35864 + $cnt = 1;
35865 + $max_fails = 10;
35866 + $failed_calls = 0;
35867 + if($strip_title == '1')
35868 + {
35869 + $new_post_content = str_replace($post_title, '', $new_post_content);
35870 + $new_post_content = str_replace('<h2></h2>', '', $new_post_content);
35871 + $new_post_content = str_replace('<h3></h3>', '', $new_post_content);
35872 + }
35873 + if (isset($aiomatic_Main_Settings['swear_filter']) && $aiomatic_Main_Settings['swear_filter'] == 'on')
35874 + {
35875 + require_once(dirname(__FILE__) . "/res/swear.php");
35876 + $new_post_content = aimogenpro_filterwords($new_post_content);
35877 + }
35878 + if(isset($aiomatic_Main_Settings['global_ban_words']) && $aiomatic_Main_Settings['global_ban_words'] != '') {
35879 + $continue = false;
35880 + $aiomatic_Main_Settings['global_ban_words'] = trim(trim(trim($aiomatic_Main_Settings['global_ban_words']), ','));
35881 + $banned_list = explode(',', $aiomatic_Main_Settings['global_ban_words']);
35882 + foreach ($banned_list as $banned_word) {
35883 + if (stripos($new_post_content, trim($banned_word)) !== FALSE) {
35884 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35885 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content contains global banned word: ' . $banned_word);
35886 + }
35887 + $continue = true;
35888 + break;
35889 + }
35890 + if (stripos($new_post_title, trim($banned_word)) !== FALSE) {
35891 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35892 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s title contains global banned word: ' . $banned_word);
35893 + }
35894 + $continue = true;
35895 + break;
35896 + }
35897 + }
35898 + if ($continue === true) {
35899 + unset($post_title_lines[$current_index]);
35900 + continue;
35901 + }
35902 + }
35903 + if(isset($aiomatic_Main_Settings['global_req_words']) && $aiomatic_Main_Settings['global_req_words'] != '')
35904 + {
35905 + if(isset($aiomatic_Main_Settings['require_only_one']) && $aiomatic_Main_Settings['require_only_one'] == 'on')
35906 + {
35907 + $continue = true;
35908 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
35909 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
35910 + foreach ($required_list as $required_word) {
35911 + if (stripos($new_post_content, trim($required_word)) !== FALSE || stripos($new_post_title, trim($required_word)) !== FALSE) {
35912 + $continue = false;
35913 + break;
35914 + }
35915 + }
35916 + if ($continue === true) {
35917 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35918 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content doesn\'t contain global required words.');
35919 + }
35920 + unset($post_title_lines[$current_index]);
35921 + continue;
35922 + }
35923 + }
35924 + else
35925 + {
35926 + $continue = false;
35927 + $aiomatic_Main_Settings['global_req_words'] = trim(trim(trim($aiomatic_Main_Settings['global_req_words']), ','));
35928 + $required_list = explode(',', $aiomatic_Main_Settings['global_req_words']);
35929 + foreach ($required_list as $required_word) {
35930 + if (stripos($new_post_content, trim($required_word)) === FALSE && stripos($new_post_title, trim($required_word)) === FALSE) {
35931 + $continue = true;
35932 + break;
35933 + }
35934 + }
35935 + if ($continue === true) {
35936 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
35937 + aiomatic_log_to_file('Skipping post "' . esc_html($new_post_title) . '", because it\'s content doesn\'t contain global required words.');
35938 + }
35939 + unset($post_title_lines[$current_index]);
35940 + continue;
35941 + }
35942 + }
35943 + }
35944 + if (isset($aiomatic_Main_Settings['spin_what']) && $aiomatic_Main_Settings['spin_what'] === 'omni')
35945 + {
35946 + $skip_spin = '1';
35947 + }
35948 + $arr = aiomatic_spin_and_translate($new_post_title, $new_post_content, '3', $skip_spin, $skip_translate);
35949 + if($arr[0] != $new_post_title)
35950 + {
35951 + $new_post_title = $arr[0];
35952 + if (!isset($aiomatic_Main_Settings['do_not_check_duplicates']) || $aiomatic_Main_Settings['do_not_check_duplicates'] != 'on')
35953 + {
35954 + $zap = aiomatic_wpforo_get_topic_by_title($new_post_title, $selected_forum);
35955 + if($zap !== false)
35956 + {
35957 + if($overwrite_existing)
35958 + {
35959 + $update_post_id = $zap;
35960 + }
35961 + else
35962 + {
35963 + aiomatic_log_to_file('Post with specified title is already existing, skipping it: ' . $post_title);
35964 + unset($post_title_lines[$current_index]);
35965 + continue;
35966 + }
35967 + }
35968 + }
35969 + }
35970 + $new_post_content = $arr[1];
35971 + if (isset($aiomatic_Main_Settings['spin_text']) && $aiomatic_Main_Settings['spin_text'] !== 'disabled')
35972 + {
35973 + $already_spinned = '1';
35974 + }
35975 + $new_post_content = html_entity_decode($new_post_content);
35976 + $new_post_content = str_replace('</ iframe>', '</iframe>', $new_post_content);
35977 + if ($strip_by_regex !== '')
35978 + {
35979 + $xstrip_by_regex = preg_split('/\r\n|\r|\n/', $strip_by_regex);
35980 + $xreplace_regex = preg_split('/\r\n|\r|\n/', $replace_regex);
35981 + $xcnt = 0;
35982 + foreach($xstrip_by_regex as $sbr)
35983 + {
35984 + if(isset($xreplace_regex[$xcnt]))
35985 + {
35986 + $repreg = $xreplace_regex[$xcnt];
35987 + }
35988 + else
35989 + {
35990 + $repreg = '';
35991 + }
35992 + $xcnt++;
35993 + $temp_cont = preg_replace("~" . $sbr . "~i", $repreg, $new_post_content);
35994 + if($temp_cont !== NULL)
35995 + {
35996 + $new_post_content = $temp_cont;
35997 + }
35998 + }
35999 + }
36000 + $post_prepender = aiomatic_replaceAIPostShortcodes($post_prepend, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36001 + $post_prepender = aiomatic_replacetopics($post_prepender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
36002 + $post_appender = aiomatic_replaceAIPostShortcodes($post_append, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36003 + $post_appender = aiomatic_replacetopics($post_appender, $post_title, $content_language, $writing_style, $writing_tone, $post_topic, $post_sections, $current_section, $section_count, $paragraph_count, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append, $default_cat_list, $img_attr);
36004 + $post_appender = aiomatic_replaceSynergyShortcodes($post_appender);
36005 + $post_prepender = aiomatic_replaceSynergyShortcodes($post_prepender);
36006 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
36007 + {
36008 + if($new_post_content === strip_tags($new_post_content))
36009 + {
36010 + if (isset($aiomatic_Main_Settings['nlbr_parse']) && $aiomatic_Main_Settings['nlbr_parse'] == 'on')
36011 + {
36012 + $new_post_content = nl2br($new_post_content);
36013 + }
36014 + }
36015 + }
36016 + if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on')
36017 + {
36018 + if (!isset($aiomatic_Main_Settings['no_undetectibility_bulk']) || $aiomatic_Main_Settings['no_undetectibility_bulk'] != 'on')
36019 + {
36020 + if(!isset($xchars))
36021 + {
36022 + $xchars = array();
36023 + }
36024 + $rand_percentage = wp_rand(10, 20);
36025 + $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
36026 + }
36027 + }
36028 + $zlang = 'en_US';
36029 + if (isset($aiomatic_Main_Settings['kw_lang']) && !empty($aiomatic_Main_Settings['kw_lang'])) {
36030 + $zlang = $aiomatic_Main_Settings['kw_lang'];
36031 + }
36032 + $rel_search = array('post_title', 'post_content');
36033 + if (isset($aiomatic_Main_Settings['rel_search']) && is_array($aiomatic_Main_Settings['rel_search'])) {
36034 + $rel_search = $aiomatic_Main_Settings['rel_search'];
36035 + }
36036 + if($max_links !== '' && $inboundlinker !== null && $link_type != 'disabled')
36037 + {
36038 + try
36039 + {
36040 + $new_post_content = $inboundlinker->add_inbound_links($new_post_content, $max_links, $link_post_types, $zlang, $rel_search, null, $link_type, $link_list, $link_nofollow, '', '', array());
36041 + }
36042 + catch(Exception $ex)
36043 + {
36044 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36045 + aiomatic_log_to_file('Failed to add inbound links to content: ' . $ex->getMessage());
36046 + }
36047 + }
36048 + }
36049 + if($ret_content == 1)
36050 + {
36051 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36052 + aiomatic_log_to_file('Returning AI generated content, finished.');
36053 + }
36054 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Finished processing'));
36055 + return array($post_prepender . ' ' . $new_post_content . ' ' . $post_appender, $new_post_title);
36056 + }
36057 + $the_final_cont = $post_prepender . ' ' . $new_post_content . ' ' . $post_appender;
36058 + $trunchi_content = aiomatic_truncate_content($the_final_cont, 16777215);
36059 + if (isset($aiomatic_Main_Settings['convert_gutenberg']) && trim($aiomatic_Main_Settings['convert_gutenberg']) == 'on')
36060 + {
36061 + $my_post['post_content'] = aiomatic_convert_html_to_gutenberg_blocks($trunchi_content);
36062 + }
36063 + else
36064 + {
36065 + $my_post['post_content'] = $trunchi_content;
36066 + }
36067 + if(stristr($my_post['post_content'], '<pre><code>&lt;') !== false)
36068 + {
36069 + if (!isset($aiomatic_Main_Settings['pre_code_off']) || trim($aiomatic_Main_Settings['pre_code_off']) != 'on')
36070 + {
36071 + $my_post['post_content'] = aiomatic_parse_pre_code_entities($my_post['post_content']);
36072 + }
36073 + }
36074 + $my_post['post_title'] = aiomatic_truncate_title($new_post_title);
36075 + $my_post['aiomatic_source_title'] = $post_title;
36076 + $my_post['aiomatic_timestamp'] = aiomatic_get_date_now();
36077 + $my_post['forum_id'] = $selected_forum;
36078 + $block_arr = array();
36079 + $custom_arr = array('aiomatic_title' => $old_title, 'aiomatic_parent_rule' => $rule_unique_id);
36080 + $block_arr = array();
36081 + $custom_tax_arr = array();
36082 + if(count($custom_tax_arr) > 0)
36083 + {
36084 + $my_post['taxo_input'] = $custom_tax_arr;
36085 + }
36086 + $my_post['meta_input'] = $custom_arr;
36087 + if($parent_id != '')
36088 + {
36089 + $my_post['post_parent'] = intval($parent_id);
36090 + }
36091 + if($min_time != '' && $max_time != '')
36092 + {
36093 + $t1 = strtotime($min_time);
36094 + $t2 = strtotime($max_time);
36095 + if($t1 != false && $t2 != false)
36096 + {
36097 + $int = wp_rand($t1, $t2);
36098 + $my_post['post_date'] = date('Y-m-d H:i:s', $int);
36099 + }
36100 + }
36101 + elseif($min_time != '')
36102 + {
36103 + $t1 = strtotime($min_time);
36104 + if($t1 != false)
36105 + {
36106 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
36107 + }
36108 + }
36109 + elseif($max_time != '')
36110 + {
36111 + $t1 = strtotime($max_time);
36112 + if($t1 != false)
36113 + {
36114 + $my_post['post_date'] = date('Y-m-d H:i:s', $t1);
36115 + }
36116 + }
36117 + $count++;
36118 + }
36119 + else
36120 + {
36121 + aiomatic_log_to_file('Unknown posting mode submitted: ' . $posting_mode . '!');
36122 + if($auto == 1)
36123 + {
36124 + aiomatic_clearFromList($param, $type);
36125 + }
36126 + return 'fail';
36127 + }
36128 + if($update_post_id != '')
36129 + {
36130 + $my_post['ID'] = $update_post_id;
36131 + }
36132 + remove_filter('content_save_pre', 'wp_filter_post_kses');
36133 + remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
36134 + try
36135 + {
36136 + $my_post['post_content'] = str_replace('\r\n', '', $my_post['post_content']);
36137 + $post_id = aiomatic_insert_wpforo_topic($my_post);
36138 + }
36139 + catch(Exception $e)
36140 + {
36141 + aiomatic_log_to_file('Exception in publishing topic (this is coming from another plugin, so we ignore it): ' . $e->getMessage());
36142 + }
36143 + add_filter('content_save_pre', 'wp_filter_post_kses');
36144 + add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
36145 + if (!is_wp_error($post_id)) {
36146 + if($post_id === 0)
36147 + {
36148 + aiomatic_log_to_file('An error occurred while inserting topic into wp database! Title:' . $my_post['post_title']);
36149 + continue;
36150 + }
36151 + $posts_inserted++;
36152 + if($reply_count !== '')
36153 + {
36154 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $reply_count, $mxatches);
36155 + if(isset($mxatches[2][0]))
36156 + {
36157 + $min_l = $mxatches[1][0];
36158 + $max_l = $mxatches[2][0];
36159 + $reply_count = wp_rand(intval($min_l), intval($max_l));
36160 + }
36161 + else
36162 + {
36163 + $reply_count = intval($reply_count);
36164 + }
36165 + if($reply_count > 0)
36166 + {
36167 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36168 + aiomatic_log_to_file('Creating ' . $reply_count . ' replies for topic ID ' . $post_id . '!');
36169 + }
36170 + $orig_choose_prompt = $choose_prompt;
36171 + $orig_reply_prompt = $reply_prompt;
36172 + for($ij = 0; $ij < $reply_count; $ij++)
36173 + {
36174 + $choose_prompt = $orig_choose_prompt;
36175 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
36176 + {
36177 + $choose_prompt = preg_split('/\r\n|\r|\n/', $choose_prompt);
36178 + $choose_prompt = array_filter($choose_prompt);
36179 + if(count($choose_prompt) > 0)
36180 + {
36181 + $choose_prompt = $choose_prompt[array_rand($choose_prompt)];
36182 + }
36183 + else
36184 + {
36185 + $choose_prompt = '';
36186 + }
36187 + }
36188 + $choose_prompt = aiomatic_replaceSynergyShortcodes($choose_prompt);
36189 + if(!empty($choose_prompt))
36190 + {
36191 + $choose_prompt = aiomatic_replaceAIPostShortcodes($choose_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36192 + }
36193 + else
36194 + {
36195 + $choose_prompt = trim(strip_tags($post_title));
36196 + }
36197 + $choose_prompt = trim($choose_prompt);
36198 + if (filter_var($choose_prompt, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($choose_prompt, '.txt'))
36199 + {
36200 + $txt_content = aiomatic_get_web_page($choose_prompt);
36201 + if ($txt_content !== FALSE)
36202 + {
36203 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
36204 + $txt_content = array_filter($txt_content);
36205 + if(count($txt_content) > 0)
36206 + {
36207 + $txt_content = $txt_content[array_rand($txt_content)];
36208 + if(trim($txt_content) != '')
36209 + {
36210 + $choose_prompt = $txt_content;
36211 + $choose_prompt = aiomatic_replaceSynergyShortcodes($choose_prompt);
36212 + $choose_prompt = aiomatic_replaceAIPostShortcodes($choose_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36213 + }
36214 + }
36215 + }
36216 + }
36217 + if(empty($choose_prompt))
36218 + {
36219 + aiomatic_log_to_file('Empty API user select seed provided!');
36220 + }
36221 + else
36222 + {
36223 + if(aiomatic_strlen($choose_prompt) > $max_seed_tokens * 4)
36224 + {
36225 + $choose_prompt = aiomatic_substr($choose_prompt, 0, (0 - ($max_seed_tokens * 4)));
36226 + }
36227 + $choose_prompt = trim($choose_prompt);
36228 + if(empty($choose_prompt))
36229 + {
36230 + aiomatic_log_to_file('Empty API user select seed provided (2)! ' . print_r($choose_prompt, true));
36231 + break;
36232 + }
36233 + $query_token_count = count(aiomatic_encode($choose_prompt));
36234 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $choose_prompt, $query_token_count);
36235 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
36236 + {
36237 + $string_len = aiomatic_strlen($choose_prompt);
36238 + $string_len = $string_len / 2;
36239 + $string_len = intval(0 - $string_len);
36240 + $choose_prompt = aiomatic_substr($choose_prompt, 0, $string_len);
36241 + $choose_prompt = trim($choose_prompt);
36242 + if(empty($choose_prompt))
36243 + {
36244 + aiomatic_log_to_file('Empty API user select seed provided (after processing) ' . print_r($choose_prompt, true));
36245 + break;
36246 + }
36247 + $query_token_count = count(aiomatic_encode($choose_prompt));
36248 + $available_tokens = $max_tokens - $query_token_count;
36249 + }
36250 + $aierror = '';
36251 + $finish_reason = '';
36252 + if(!empty($global_prepend))
36253 + {
36254 + $choose_prompt = $global_prepend . ' ' . $choose_prompt;
36255 + }
36256 + if(!empty($global_append))
36257 + {
36258 + $choose_prompt = $choose_prompt . ' ' . $global_append;
36259 + }
36260 + }
36261 +
36262 + $reply_prompt = $orig_reply_prompt;
36263 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
36264 + {
36265 + $reply_prompt = preg_split('/\r\n|\r|\n/', $reply_prompt);
36266 + $reply_prompt = array_filter($reply_prompt);
36267 + if(count($reply_prompt) > 0)
36268 + {
36269 + $reply_prompt = $reply_prompt[array_rand($reply_prompt)];
36270 + }
36271 + else
36272 + {
36273 + $reply_prompt = '';
36274 + }
36275 + }
36276 + $reply_prompt = aiomatic_replaceSynergyShortcodes($reply_prompt);
36277 + if(!empty($reply_prompt))
36278 + {
36279 + $reply_prompt = aiomatic_replaceAIPostShortcodes($reply_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36280 + }
36281 + else
36282 + {
36283 + $reply_prompt = trim(strip_tags($post_title));
36284 + }
36285 + $reply_prompt = trim($reply_prompt);
36286 + if (filter_var($reply_prompt, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($reply_prompt, '.txt'))
36287 + {
36288 + $txt_content = aiomatic_get_web_page($reply_prompt);
36289 + if ($txt_content !== FALSE)
36290 + {
36291 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
36292 + $txt_content = array_filter($txt_content);
36293 + if(count($txt_content) > 0)
36294 + {
36295 + $txt_content = $txt_content[array_rand($txt_content)];
36296 + if(trim($txt_content) != '')
36297 + {
36298 + $reply_prompt = $txt_content;
36299 + $reply_prompt = aiomatic_replaceSynergyShortcodes($reply_prompt);
36300 + $reply_prompt = aiomatic_replaceAIPostShortcodes($reply_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36301 + }
36302 + }
36303 + }
36304 + }
36305 + if(empty($reply_prompt))
36306 + {
36307 + aiomatic_log_to_file('Empty API reply select seed provided!');
36308 + }
36309 + else
36310 + {
36311 + if(aiomatic_strlen($reply_prompt) > $max_seed_tokens * 4)
36312 + {
36313 + $reply_prompt = aiomatic_substr($reply_prompt, 0, (0 - ($max_seed_tokens * 4)));
36314 + }
36315 + $reply_prompt = trim($reply_prompt);
36316 + if(empty($reply_prompt))
36317 + {
36318 + aiomatic_log_to_file('Empty API reply select seed provided (2)! ' . print_r($reply_prompt, true));
36319 + break;
36320 + }
36321 + $query_token_count = count(aiomatic_encode($reply_prompt));
36322 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $reply_prompt, $query_token_count);
36323 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
36324 + {
36325 + $string_len = aiomatic_strlen($reply_prompt);
36326 + $string_len = $string_len / 2;
36327 + $string_len = intval(0 - $string_len);
36328 + $reply_prompt = aiomatic_substr($reply_prompt, 0, $string_len);
36329 + $reply_prompt = trim($reply_prompt);
36330 + if(empty($reply_prompt))
36331 + {
36332 + aiomatic_log_to_file('Empty API reply select seed provided (after processing) ' . print_r($reply_prompt, true));
36333 + break;
36334 + }
36335 + $query_token_count = count(aiomatic_encode($reply_prompt));
36336 + $available_tokens = $max_tokens - $query_token_count;
36337 + }
36338 + $aierror = '';
36339 + $finish_reason = '';
36340 + if(!empty($global_prepend))
36341 + {
36342 + $reply_prompt = $global_prepend . ' ' . $reply_prompt;
36343 + }
36344 + if(!empty($global_append))
36345 + {
36346 + $reply_prompt = $reply_prompt . ' ' . $global_append;
36347 + }
36348 + }
36349 +
36350 + $reply_result = aiomatic_auto_generate_wpforo_reply($selected_forum, $post_id, $reply_model, $token, $choose_prompt, $reply_prompt, $temperature, $top_p, $presence_penalty, $frequency_penalty, $assistant_id, $thread_id, $store_data, $user_role);
36351 + if($reply_result === false)
36352 + {
36353 + aiomatic_log_to_file('Failed to create reply for topic ID ' . $post_id . '!');
36354 + }
36355 + elseif(is_wp_error( $reply_result ))
36356 + {
36357 + aiomatic_log_to_file('Failed to create reply for topic ID ' . $post_id . '! Error: ' . $reply_result->get_error_message() . ' Error code: ' . $reply_result->get_error_code() . ' Error data: ' . $reply_result->get_error_data());
36358 + }
36359 + }
36360 + }
36361 + }
36362 + } else {
36363 + aiomatic_log_to_file('Failed to insert topic into wp database(1)! Title:' . $my_post['post_title'] . '! Error: ' . $post_id->get_error_message() . ' Error code: ' . $post_id->get_error_code() . ' Error data: ' . $post_id->get_error_data());
36364 + continue;
36365 + }
36366 + }
36367 + }
36368 + elseif($type == 8)
36369 + {
36370 + if(!function_exists('is_plugin_active'))
36371 + {
36372 + include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
36373 + }
36374 + if (!is_plugin_active('aiomatic-extension-wpforo/aiomatic-extension-wpforo.php'))
36375 + {
36376 + aiomatic_log_to_file('This feature requires the Aimogen Extension for AI Forum Creation plugin to be installed and activated!');
36377 + if($auto == 1)
36378 + {
36379 + aiomatic_clearFromList($param, $type);
36380 + }
36381 + return 'fail';
36382 + }
36383 + if (!is_plugin_active('wpforo/wpforo.php'))
36384 + {
36385 + aiomatic_log_to_file('This feature requires the wpForo plugin to be installed and activated!');
36386 + if($auto == 1)
36387 + {
36388 + aiomatic_clearFromList($param, $type);
36389 + }
36390 + return 'fail';
36391 + }
36392 + if($post_title == '')
36393 + {
36394 + aiomatic_log_to_file('Strategy cannot be empty for wpForo posting!');
36395 + if($auto == 1)
36396 + {
36397 + aiomatic_clearFromList($param, $type);
36398 + }
36399 + return 'fail';
36400 + }
36401 + while(true)
36402 + {
36403 + $thread_id = '';
36404 + $post_link = '';
36405 + $user_name = '';
36406 + $post_cats = '';
36407 + $post_excerpt = '';
36408 + $final_content = '';
36409 + $update_post_id = '';
36410 + $featured_image = '';
36411 + $post_tagz = '';
36412 + $postID = '';
36413 + $old_title = '';
36414 + $img_attr = '';
36415 + if ($count > intval($max)) {
36416 + break;
36417 + }
36418 + aiomatic_job_set_status_pending($rule_unique_id, array('step' => 'Starting processing topic ID #' . $count));
36419 + $current_section = '';
36420 + $post_sections = '';
36421 + $post_topic = '';
36422 + $post_title_keywords = '';
36423 + $posting_mode = 'title';
36424 + $post_id = aiomatic_wpforo_find_topic_by_strategy($selected_forum, $post_title, $more_replies);
36425 + if($post_id === false)
36426 + {
36427 + aiomatic_log_to_file('No topic found for the specified strategy: ' . $post_title);
36428 + if($auto == 1)
36429 + {
36430 + aiomatic_clearFromList($param, $type);
36431 + }
36432 + return 'nochange';
36433 + }
36434 + if($reply_count !== '')
36435 + {
36436 + preg_match_all('#\s*(\d+)\s*-\s*(\d+)\s*#', $reply_count, $mxatches);
36437 + if(isset($mxatches[2][0]))
36438 + {
36439 + $min_l = $mxatches[1][0];
36440 + $max_l = $mxatches[2][0];
36441 + $reply_count = wp_rand(intval($min_l), intval($max_l));
36442 + }
36443 + else
36444 + {
36445 + $reply_count = intval($reply_count);
36446 + }
36447 + if($reply_count > 0)
36448 + {
36449 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36450 + aiomatic_log_to_file('Creating ' . $reply_count . ' replies for topic ID ' . $post_id . '!');
36451 + }
36452 + $orig_choose_prompt = $choose_prompt;
36453 + $orig_reply_prompt = $reply_prompt;
36454 + for($ij = 0; $ij < $reply_count; $ij++)
36455 + {
36456 + $choose_prompt = $orig_choose_prompt;
36457 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
36458 + {
36459 + $choose_prompt = preg_split('/\r\n|\r|\n/', $choose_prompt);
36460 + $choose_prompt = array_filter($choose_prompt);
36461 + if(count($choose_prompt) > 0)
36462 + {
36463 + $choose_prompt = $choose_prompt[array_rand($choose_prompt)];
36464 + }
36465 + else
36466 + {
36467 + $choose_prompt = '';
36468 + }
36469 + }
36470 + $choose_prompt = aiomatic_replaceSynergyShortcodes($choose_prompt);
36471 + if(!empty($choose_prompt))
36472 + {
36473 + $choose_prompt = aiomatic_replaceAIPostShortcodes($choose_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36474 + }
36475 + else
36476 + {
36477 + $choose_prompt = trim(strip_tags($post_title));
36478 + }
36479 + $choose_prompt = trim($choose_prompt);
36480 + if (filter_var($choose_prompt, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($choose_prompt, '.txt'))
36481 + {
36482 + $txt_content = aiomatic_get_web_page($choose_prompt);
36483 + if ($txt_content !== FALSE)
36484 + {
36485 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
36486 + $txt_content = array_filter($txt_content);
36487 + if(count($txt_content) > 0)
36488 + {
36489 + $txt_content = $txt_content[array_rand($txt_content)];
36490 + if(trim($txt_content) != '')
36491 + {
36492 + $choose_prompt = $txt_content;
36493 + $choose_prompt = aiomatic_replaceSynergyShortcodes($choose_prompt);
36494 + $choose_prompt = aiomatic_replaceAIPostShortcodes($choose_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36495 + }
36496 + }
36497 + }
36498 + }
36499 + if(empty($choose_prompt))
36500 + {
36501 + aiomatic_log_to_file('Empty API user select seed provided!');
36502 + }
36503 + else
36504 + {
36505 + if(aiomatic_strlen($choose_prompt) > $max_seed_tokens * 4)
36506 + {
36507 + $choose_prompt = aiomatic_substr($choose_prompt, 0, (0 - ($max_seed_tokens * 4)));
36508 + }
36509 + $choose_prompt = trim($choose_prompt);
36510 + if(empty($choose_prompt))
36511 + {
36512 + aiomatic_log_to_file('Empty API user select seed provided (2)! ' . print_r($choose_prompt, true));
36513 + break;
36514 + }
36515 + $query_token_count = count(aiomatic_encode($choose_prompt));
36516 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $choose_prompt, $query_token_count);
36517 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
36518 + {
36519 + $string_len = aiomatic_strlen($choose_prompt);
36520 + $string_len = $string_len / 2;
36521 + $string_len = intval(0 - $string_len);
36522 + $choose_prompt = aiomatic_substr($choose_prompt, 0, $string_len);
36523 + $choose_prompt = trim($choose_prompt);
36524 + if(empty($choose_prompt))
36525 + {
36526 + aiomatic_log_to_file('Empty API user select seed provided (after processing) ' . print_r($choose_prompt, true));
36527 + break;
36528 + }
36529 + $query_token_count = count(aiomatic_encode($choose_prompt));
36530 + $available_tokens = $max_tokens - $query_token_count;
36531 + }
36532 + $aierror = '';
36533 + $finish_reason = '';
36534 + if(!empty($global_prepend))
36535 + {
36536 + $choose_prompt = $global_prepend . ' ' . $choose_prompt;
36537 + }
36538 + if(!empty($global_append))
36539 + {
36540 + $choose_prompt = $choose_prompt . ' ' . $global_append;
36541 + }
36542 + }
36543 +
36544 + $reply_prompt = $orig_reply_prompt;
36545 + if(!isset($aiomatic_Main_Settings['whole_prompt']) || trim($aiomatic_Main_Settings['whole_prompt']) != 'on')
36546 + {
36547 + $reply_prompt = preg_split('/\r\n|\r|\n/', $reply_prompt);
36548 + $reply_prompt = array_filter($reply_prompt);
36549 + if(count($reply_prompt) > 0)
36550 + {
36551 + $reply_prompt = $reply_prompt[array_rand($reply_prompt)];
36552 + }
36553 + else
36554 + {
36555 + $reply_prompt = '';
36556 + }
36557 + }
36558 + $reply_prompt = aiomatic_replaceSynergyShortcodes($reply_prompt);
36559 + if(!empty($reply_prompt))
36560 + {
36561 + $reply_prompt = aiomatic_replaceAIPostShortcodes($reply_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36562 + }
36563 + else
36564 + {
36565 + $reply_prompt = trim(strip_tags($post_title));
36566 + }
36567 + $reply_prompt = trim($reply_prompt);
36568 + if (filter_var($reply_prompt, FILTER_VALIDATE_URL) !== false && aiomatic_endsWith($reply_prompt, '.txt'))
36569 + {
36570 + $txt_content = aiomatic_get_web_page($reply_prompt);
36571 + if ($txt_content !== FALSE)
36572 + {
36573 + $txt_content = preg_split('/\r\n|\r|\n/', $txt_content);
36574 + $txt_content = array_filter($txt_content);
36575 + if(count($txt_content) > 0)
36576 + {
36577 + $txt_content = $txt_content[array_rand($txt_content)];
36578 + if(trim($txt_content) != '')
36579 + {
36580 + $reply_prompt = $txt_content;
36581 + $reply_prompt = aiomatic_replaceSynergyShortcodes($reply_prompt);
36582 + $reply_prompt = aiomatic_replaceAIPostShortcodes($reply_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $final_content, $user_name, $featured_image, $post_cats, $post_tagz, $postID, $img_attr, $old_title, $post_title_keywords, $custom_shortcodes, $global_prepend, $global_append);
36583 + }
36584 + }
36585 + }
36586 + }
36587 + if(empty($reply_prompt))
36588 + {
36589 + aiomatic_log_to_file('Empty API reply select seed provided!');
36590 + }
36591 + else
36592 + {
36593 + if(aiomatic_strlen($reply_prompt) > $max_seed_tokens * 4)
36594 + {
36595 + $reply_prompt = aiomatic_substr($reply_prompt, 0, (0 - ($max_seed_tokens * 4)));
36596 + }
36597 + $reply_prompt = trim($reply_prompt);
36598 + if(empty($reply_prompt))
36599 + {
36600 + aiomatic_log_to_file('Empty API reply select seed provided (2)! ' . print_r($reply_prompt, true));
36601 + break;
36602 + }
36603 + $query_token_count = count(aiomatic_encode($reply_prompt));
36604 + $available_tokens = aiomatic_compute_available_tokens($title_model, $max_tokens, $reply_prompt, $query_token_count);
36605 + if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
36606 + {
36607 + $string_len = aiomatic_strlen($reply_prompt);
36608 + $string_len = $string_len / 2;
36609 + $string_len = intval(0 - $string_len);
36610 + $reply_prompt = aiomatic_substr($reply_prompt, 0, $string_len);
36611 + $reply_prompt = trim($reply_prompt);
36612 + if(empty($reply_prompt))
36613 + {
36614 + aiomatic_log_to_file('Empty API reply select seed provided (after processing) ' . print_r($reply_prompt, true));
36615 + break;
36616 + }
36617 + $query_token_count = count(aiomatic_encode($reply_prompt));
36618 + $available_tokens = $max_tokens - $query_token_count;
36619 + }
36620 + $aierror = '';
36621 + $finish_reason = '';
36622 + if(!empty($global_prepend))
36623 + {
36624 + $reply_prompt = $global_prepend . ' ' . $reply_prompt;
36625 + }
36626 + if(!empty($global_append))
36627 + {
36628 + $reply_prompt = $reply_prompt . ' ' . $global_append;
36629 + }
36630 + }
36631 + $reply_result = aiomatic_auto_generate_wpforo_reply($selected_forum, $post_id, $reply_model, $token, $choose_prompt, $reply_prompt, $temperature, $top_p, $presence_penalty, $frequency_penalty, $assistant_id, $thread_id, $store_data, $user_role);
36632 + if($reply_result === false)
36633 + {
36634 + aiomatic_log_to_file('Failed to create reply for topic ID ' . $post_id . '!');
36635 + }
36636 + elseif(is_wp_error( $reply_result ))
36637 + {
36638 + aiomatic_log_to_file('Failed to create reply for topic ID ' . $post_id . '! Error: ' . $reply_result->get_error_message() . ' Error code: ' . $reply_result->get_error_code() . ' Error data: ' . $reply_result->get_error_data());
36639 + }
36640 + else
36641 + {
36642 +
36643 + }
36644 + $count++;
36645 + $posts_inserted++;
36646 + }
36647 + }
36648 + }
36649 + }
36650 + }
36651 + else
36652 + {
36653 + aiomatic_log_to_file('Unknown type given ' . $type . '!');
36654 + if($auto == 1)
36655 + {
36656 + aiomatic_clearFromList($param, $type);
36657 + }
36658 + return 'fail';
36659 + }
36660 + }
36661 + catch (Exception $e) {
36662 + aiomatic_log_to_file('Exception thrown ' . esc_html($e->getMessage()) . '!');
36663 + if($auto == 1)
36664 + {
36665 + aiomatic_clearFromList($param, $type);
36666 + }
36667 + return 'fail';
36668 + }
36669 + if($type == '5')
36670 + {
36671 + $logaction = 'OmniBlock saving actions done!';
36672 + }
36673 + elseif($type == '7')
36674 + {
36675 + $logaction = 'topics created!';
36676 + }
36677 + elseif($type == '8')
36678 + {
36679 + $logaction = 'replies created!';
36680 + }
36681 + else
36682 + {
36683 + $logaction = 'posts created!';
36684 + }
36685 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36686 + aiomatic_log_to_file('Rule ID ' . esc_html($param) . ' succesfully run! ' . esc_html($posts_inserted) . ' ' . $logaction);
36687 + }
36688 + if (isset($aiomatic_Main_Settings['send_email']) && $aiomatic_Main_Settings['send_email'] == 'on' && $aiomatic_Main_Settings['email_address'] !== '') {
36689 + try {
36690 + $admin_mail = get_option('admin_email');
36691 + $to = $aiomatic_Main_Settings['email_address'];
36692 + $subject = '[aiomatic] Rule running report - ' . aiomatic_get_date_now();
36693 + $message = 'Rule ID ' . esc_html($param) . ' succesfully run! ' . esc_html($posts_inserted) . ' ' . $logaction;
36694 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
36695 + $headers[] = 'Reply-To: ' . $admin_mail;
36696 + $headers[] = 'X-Mailer: PHP/' . phpversion();
36697 + $headers[] = 'Content-Type: text/html';
36698 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
36699 + wp_mail($to, $subject, $message, $headers);
36700 + }
36701 + catch (Exception $e) {
36702 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36703 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
36704 + }
36705 + }
36706 + }
36707 + }
36708 + if($type == 5)
36709 + {
36710 + if(isset($block_results) && count($block_results) > 0)
36711 + {
36712 + if($return_last_result == true)
36713 + {
36714 + $rez_end = end($block_results);
36715 + $zoutput = '';
36716 + foreach ($rez_end as $key => $value) {
36717 + if ($key === 0 || empty($value)) {
36718 + continue;
36719 + }
36720 + $zoutput .= $value . "\n";
36721 + //break to return only the first result
36722 + break;
36723 + }
36724 + $zoutput = trim($zoutput);
36725 + if(!empty($zoutput))
36726 + {
36727 + if($auto == 1)
36728 + {
36729 + aiomatic_clearFromList($param, $type);
36730 + }
36731 + return $zoutput;
36732 + }
36733 + }
36734 + }
36735 + }
36736 + if ($posts_inserted == 0)
36737 + {
36738 + if($type == 4)
36739 + {
36740 + if (isset($aiomatic_Main_Settings['email_notification']) && $aiomatic_Main_Settings['email_notification'] != '')
36741 + {
36742 + if($count === 1)
36743 + {
36744 + $email_list = explode(',', $aiomatic_Main_Settings['email_notification']);
36745 + foreach($email_list as $thisaddr)
36746 + {
36747 + $thisaddr = trim($thisaddr);
36748 + try
36749 + {
36750 + $admin_mail = get_option('admin_email');
36751 + $to = $thisaddr;
36752 + $subject = '[Aimogen] Titles depleted rule ID ' . $param . ' - ' . aiomatic_get_date_now();
36753 + $message = 'Rule ID ' . esc_html($param) . ' (' . $rule_description . ') depleted its titles from the CSV file, no new posts publised! Website URL: ' . get_site_url();
36754 + $headers[] = 'From: Aimogen Plugin <' . $admin_mail . '>';
36755 + $headers[] = 'Reply-To: ' . $admin_mail;
36756 + $headers[] = 'X-Mailer: PHP/' . phpversion();
36757 + $headers[] = 'Content-Type: text/html';
36758 + $headers[] = 'Charset: ' . get_option('blog_charset', 'UTF-8');
36759 + wp_mail($to, $subject, $message, $headers);
36760 + }
36761 + catch (Exception $e)
36762 + {
36763 + if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
36764 + aiomatic_log_to_file('Failed to send mail: Exception thrown ' . esc_html($e->getMessage()) . '!');
36765 + }
36766 + }
36767 + }
36768 + }
36769 + }
36770 + }
36771 + if($auto == 1)
36772 + {
36773 + aiomatic_clearFromList($param, $type);
36774 + }
36775 + return 'nochange';
36776 + } else {
36777 + if($auto == 1)
36778 + {
36779 + aiomatic_clearFromList($param, $type);
36780 + }
36781 + return 'ok';
36782 + }
36783 + }
36784 + ?>