create_temp_file( $image_data['bytes'], $image_data['mime_type'] ); if ( is_wp_error( $temp_file ) ) { return $temp_file; } // Prepare file array for media_handle_sideload(). $file_array = [ 'name' => $this->generate_filename( $image_data['model'], $image_data['width'], $image_data['height'] ), 'tmp_name' => $temp_file, ]; // Import the file into media library. $attachment_id = media_handle_sideload( $file_array, $post_id ); // Clean up temp file. @unlink( $temp_file ); if ( is_wp_error( $attachment_id ) ) { PRAutoBlogger_Logger::instance()->error( 'Failed to sideload image: ' . $attachment_id->get_error_message(), 'image_media_sideloader' ); return $attachment_id; } // Set alt text and metadata. if ( ! empty( $alt_text ) ) { update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $alt_text ) ); } // Store image generation metadata. update_post_meta( $attachment_id, '_prautoblogger_image_model', sanitize_text_field( $image_data['model'] ) ); update_post_meta( $attachment_id, '_prautoblogger_image_width', (int) $image_data['width'] ); update_post_meta( $attachment_id, '_prautoblogger_image_height', (int) $image_data['height'] ); update_post_meta( $attachment_id, '_prautoblogger_image_cost_usd', (float) $image_data['cost_usd'] ); update_post_meta( $attachment_id, '_prautoblogger_image_latency_ms', (int) $image_data['latency_ms'] ); if ( isset( $image_data['seed'] ) && null !== $image_data['seed'] ) { update_post_meta( $attachment_id, '_prautoblogger_image_seed', (int) $image_data['seed'] ); } return $attachment_id; } /** * Create a temporary file containing the image bytes. * * @param string $bytes Raw image bytes. * @param string $mime_type MIME type (e.g., 'image/png'). * * @return string|\WP_Error Path to temporary file on success, WP_Error on failure. */ private function create_temp_file( string $bytes, string $mime_type ) { $temp_dir = get_temp_dir(); $filename = 'prautoblogger_img_' . wp_generate_uuid4() . $this->get_extension_for_mime( $mime_type ); $temp_path = $temp_dir . $filename; $written = file_put_contents( $temp_path, $bytes ); if ( false === $written ) { return new \WP_Error( 'temp_file_write_failed', sprintf( /* translators: %s: path to temp file */ esc_html__( 'Could not write temporary image file to %s', 'prautoblogger' ), $temp_path ) ); } return $temp_path; } /** * Generate a descriptive filename for the attachment. * * @param string $model Model name (e.g., 'flux-1-schnell'). * @param int $width Image width. * @param int $height Image height. * * @return string Sanitized filename. */ private function generate_filename( string $model, int $width, int $height ): string { $base = sprintf( 'prautoblogger_%s_%dx%d_%s', sanitize_title( $model ), $width, $height, gmdate( 'Y-m-d_H-i-s' ) ); return $base . '.png'; // Default to PNG since FLUX.1 outputs PNG. } /** * Get file extension for a MIME type. * * @param string $mime_type MIME type string. * * @return string File extension with leading dot (e.g., '.png'). */ private function get_extension_for_mime( string $mime_type ): string { $map = [ 'image/png' => '.png', 'image/jpeg' => '.jpg', 'image/jpg' => '.jpg', ]; return $map[ strtolower( $mime_type ) ] ?? '.png'; } }