first(); if ($isExist) { $urls[$cacheKey] = $isExist->short; return $isExist->short; } $maxRetries = 3; for ($attempt = 0; $attempt < $maxRetries; $attempt++) { $short = self::generateRandomSlug(); try { self::insert([ 'url' => $longUrl, 'short' => $short, 'created_at' => current_time('mysql'), 'updated_at' => current_time('mysql') ]); $urls[$cacheKey] = $short; return $short; } catch (\Exception $e) { if (strpos($e->getMessage(), 'Duplicate entry') !== false) { continue; } throw $e; } } return ''; } public static function generateRandomSlug($length = 6) { $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $charsLen = strlen($chars); $slug = ''; $bytes = random_bytes($length); for ($i = 0; $i < $length; $i++) { $slug .= $chars[ord($bytes[$i]) % $charsLen]; } return $slug; } public static function getRowByShort($short) { global $wpdb; return $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "fc_url_stores WHERE BINARY `short` = %s ORDER BY `id` DESC LIMIT 1", $short)); } }