'site_default']); } $size = self::normalize_size($args['generation_size'] ?? ($args['size'] ?? '1024x1024')); $placement = sanitize_key($args['placement'] ?? 'featured'); $allowed_placements = class_exists('AIWEST_Graphics_Rules') ? AIWEST_Graphics_Rules::placement_values() : ['featured', 'content_top', 'featured_and_content_top']; if (!in_array($placement, $allowed_placements, true)) { $placement = 'featured'; } $prompt = self::build_prompt($post->post_title, $context ?: wp_trim_words(wp_strip_all_tags($post->post_content), 80, '...')); $image = AIWEST_OpenAI::image_generation($prompt, $size); if (is_wp_error($image)) { update_post_meta($post_id, '_aiwest_image_error', $image->get_error_message()); return $image; } $filename = sanitize_file_name('aiwest-' . $post_id . '-' . time() . '.png'); $binary = ''; if ($image['type'] === 'b64_json') { $binary = base64_decode($image['data'], true); if (!$binary) return new WP_Error('aiwest_image_decode_failed', __('The generated image could not be decoded.', 'ai-webmaster-studio')); } elseif ($image['type'] === 'url') { $response = wp_safe_remote_get($image['data'], ['timeout' => 120]); if (is_wp_error($response)) return $response; $binary = wp_remote_retrieve_body($response); if (!$binary) return new WP_Error('aiwest_image_download_failed', __('The generated image could not be downloaded.', 'ai-webmaster-studio')); } else { return new WP_Error('aiwest_image_unknown_type', __('The image provider returned an unknown data type.', 'ai-webmaster-studio')); } $uploaded = wp_upload_bits($filename, null, $binary); if (!empty($uploaded['error']) || empty($uploaded['file'])) { return new WP_Error('aiwest_image_save_failed', sanitize_text_field($uploaded['error'] ?? __('The generated image could not be saved.', 'ai-webmaster-studio'))); } $filepath = $uploaded['file']; $filename = wp_basename($filepath); // wp_generate_attachment_metadata() is defined in this core image helper. require_once ABSPATH . 'wp-admin/includes/image.php'; $filetype = wp_check_filetype($filename, null); $attachment_id = wp_insert_attachment([ 'post_mime_type' => $filetype['type'] ?: 'image/png', 'post_title' => sanitize_text_field($post->post_title . ' — AI image'), 'post_content' => '', 'post_status' => 'inherit', ], $filepath, $post_id); if (is_wp_error($attachment_id)) return $attachment_id; $metadata = wp_generate_attachment_metadata($attachment_id, $filepath); wp_update_attachment_metadata($attachment_id, $metadata); update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($post->post_title)); if (class_exists('AIWEST_Content_Engine') && method_exists('AIWEST_Content_Engine', 'apply_attachment_to_post')) { AIWEST_Content_Engine::apply_attachment_to_post($post_id, intval($attachment_id), $placement, $args); } else { set_post_thumbnail($post_id, $attachment_id); } update_post_meta($post_id, '_aiwest_featured_image_generated', 'yes'); update_post_meta($post_id, '_aiwest_featured_image_id', intval($attachment_id)); update_post_meta($post_id, '_aiwest_image_size', $size); update_post_meta($post_id, '_aiwest_image_placement', $placement); update_post_meta($post_id, '_aiwest_image_display_size', sanitize_key($args['display_size'] ?? 'large')); update_post_meta($post_id, '_aiwest_image_alignment', sanitize_key($args['alignment'] ?? 'none')); update_post_meta($post_id, '_aiwest_graphics_rules_used', $args); if (class_exists('AIWEST_Logger')) { AIWEST_Logger::info('AI image generated and assigned.', [ 'post_id' => $post_id, 'attachment_id' => $attachment_id, 'size' => $size, 'placement' => $placement, ]); } return intval($attachment_id); } }