InKend
in what to do
$post_id, 'provider' => $provider]); return 0; } $att_id = self::attach($path, $post_id, $title); if (!$att_id) @unlink($path); return $att_id; } /* ---- Providers ---- */ private static function fetch_pollinations(string $title, string $style): string|false { $map = ['photo'=>'realistic high quality photo','illustration'=>'digital illustration', 'artistic'=>'artistic painting','3d'=>'3D render','flat'=>'flat design vector art']; $clean = preg_replace('/[^\p{L}\p{N}\s]/u', ' ', mb_substr($title, 0, 80)); $prompt = ($map[$style] ?? 'realistic high quality photo') . ', ' . trim($clean) . ', professional, no text, no watermark'; $seed = abs(crc32($title . time())) % 99999; $url = self::POLLINATIONS_BASE . urlencode($prompt) . '?seed=' . $seed . '&width=1200&height=630&nologo=true&model=flux'; return self::download_image($url, $title); } private static function fetch_unsplash(string $title, string $api_key): string|false { if (!$api_key) { ACI_Logger::warn('ImageGen: Unsplash API key missing'); return false; } $keyword = urlencode(mb_substr($title, 0, 50)); $api_url = "https://api.unsplash.com/photos/random?query={$keyword}&orientation=landscape&content_filter=high"; $resp = wp_remote_get($api_url, [ 'timeout' => 20, 'sslverify' => false, 'headers' => ['Authorization' => 'Client-ID ' . $api_key, 'Accept-Version' => 'v1'], ]); if (is_wp_error($resp)) { ACI_Logger::error('ImageGen Unsplash', ['err' => $resp->get_error_message()]); return false; } $data = json_decode(wp_remote_retrieve_body($resp), true); $img_url = $data['urls']['regular'] ?? ($data['urls']['full'] ?? ''); if (!$img_url) { ACI_Logger::warn('ImageGen: Unsplash no URL', ['resp' => substr(wp_remote_retrieve_body($resp), 0, 200)]); return false; } return self::download_image($img_url, $title); } private static function fetch_pexels(string $title, string $api_key): string|false { if (!$api_key) { ACI_Logger::warn('ImageGen: Pexels API key missing'); return false; } $keyword = urlencode(mb_substr($title, 0, 50)); $api_url = "https://api.pexels.com/v1/search?query={$keyword}&per_page=1&orientation=landscape"; $resp = wp_remote_get($api_url, [ 'timeout' => 20, 'sslverify' => false, 'headers' => ['Authorization' => $api_key], ]); if (is_wp_error($resp)) { ACI_Logger::error('ImageGen Pexels', ['err' => $resp->get_error_message()]); return false; } $data = json_decode(wp_remote_retrieve_body($resp), true); $img_url = $data['photos'][0]['src']['large2x'] ?? ($data['photos'][0]['src']['large'] ?? ''); if (!$img_url) { ACI_Logger::warn('ImageGen: Pexels no URL', ['resp' => substr(wp_remote_retrieve_body($resp), 0, 200)]); return false; } return self::download_image($img_url, $title); } /* ---- Shared helpers ---- */ private static function download_image(string $url, string $title): string|false { $resp = wp_remote_get($url, [ 'timeout' => 45, 'redirection' => 10, 'sslverify' => false, 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'headers' => ['Accept' => 'image/webp,image/png,image/jpeg,image/*'], ]); if (is_wp_error($resp)) { ACI_Logger::error('ImageGen download', ['err' => $resp->get_error_message()]); return false; } if ((int)wp_remote_retrieve_response_code($resp) !== 200) { ACI_Logger::warn('ImageGen: non-200', ['code' => wp_remote_retrieve_response_code($resp)]); return false; } $body = wp_remote_retrieve_body($resp); if (strlen($body) < 1000) return false; $ext = self::detect_ext($body); if (!$ext) return false; $upload = wp_upload_dir(); if (!empty($upload['error']) || !wp_mkdir_p($upload['path'])) return false; $slug = sanitize_title(mb_substr($title, 0, 40)) ?: 'image'; $path = trailingslashit($upload['path']) . $slug . '-' . time() . '.' . $ext; if (file_put_contents($path, $body) === false) return false; return $path; } private static function detect_ext(string $body): string|false { $h = substr($body, 0, 12); if (str_starts_with($h, "\xFF\xD8\xFF")) return 'jpg'; if (str_starts_with($h, "\x89PNG\r\n\x1A\n")) return 'png'; if (str_starts_with($h, 'RIFF') && substr($h, 8, 4) === 'WEBP') return 'webp'; if (str_starts_with($h, 'GIF8')) return 'gif'; return false; } private static function attach(string $path, int $post_id, string $title): int { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $upload = wp_upload_dir(); $filetype = wp_check_filetype(basename($path)); if (empty($filetype['type'])) $filetype['type'] = 'image/jpeg'; $att_id = wp_insert_attachment([ 'post_mime_type' => $filetype['type'], 'post_title' => sanitize_text_field($title), 'post_content' => '', 'post_status' => 'inherit', 'guid' => str_replace($upload['basedir'], $upload['baseurl'], $path), ], $path, $post_id); if (is_wp_error($att_id)) { ACI_Logger::error('ImageGen attach', ['err' => $att_id->get_error_message()]); return 0; } $meta = wp_generate_attachment_metadata($att_id, $path); if ($meta) wp_update_attachment_metadata($att_id, $meta); set_post_thumbnail($post_id, $att_id); return $att_id; } } ]) } } ]) } ]) } ]) } } } } }
Sunday, June 07, 2026
in what to do
The Adventure of Turkish Archery | the Huns
