PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.3
Yatra – Travel Booking & Tour Operator Software v3.0.3
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / app / Helpers / SlugHelper.php

SlugHelper.php in Yatra – Travel Booking & Tour Operator Software 3.0.3, at app/Helpers/SlugHelper.php

146 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Helpers;
6
7 /**
8 * Slug Helper
9 * Generates URL-friendly slugs from text
10 * Uses WordPress sanitize_title() for consistency
11 */
12 class SlugHelper
13 {
14 /**
15 * Generate a slug from a string
16 * Uses WordPress sanitize_title() function
17 *
18 * @param string $text The text to convert to a slug
19 * @return string A URL-friendly slug
20 */
21 public static function generate(string $text): string
22 {
23 if (empty($text)) {
24 return '';
25 }
26
27 // Use WordPress's sanitize_title function for consistency
28 return sanitize_title($text);
29 }
30
31 /**
32 * Generate a unique slug by appending a number if needed
33 *
34 * @param string $baseSlug The base slug
35 * @param array $existingSlugs Array of existing slugs to check against
36 * @return string A unique slug
37 */
38 public static function generateUnique(string $baseSlug, array $existingSlugs): string
39 {
40 $slug = self::generate($baseSlug);
41
42 if (empty($slug)) {
43 $slug = 'untitled';
44 }
45
46 // If slug is already unique, return it
47 if (!in_array($slug, $existingSlugs, true)) {
48 return $slug;
49 }
50
51 // Try appending numbers until we find a unique slug
52 $counter = 1;
53 $uniqueSlug = $slug . '-' . $counter;
54
55 while (in_array($uniqueSlug, $existingSlugs, true)) {
56 $counter++;
57 $uniqueSlug = $slug . '-' . $counter;
58 }
59
60 return $uniqueSlug;
61 }
62
63 /**
64 * Generate a unique slug by checking against database
65 *
66 * @param string $baseSlug The base slug
67 * @param string $tableName The table name (without prefix)
68 * @param string $columnName The column name (default: 'slug')
69 * @param int|null $excludeId ID to exclude from check (for updates)
70 * @return string A unique slug
71 */
72 public static function generateUniqueFromDatabase(
73 string $baseSlug,
74 string $tableName,
75 string $columnName = 'slug',
76 ?int $excludeId = null
77 ): string {
78 global $wpdb;
79
80 $slug = self::generate($baseSlug);
81
82 if (empty($slug)) {
83 $slug = 'untitled';
84 }
85
86 // Check if table name already has prefix to avoid double prefix
87 if (strpos($tableName, $wpdb->prefix) === 0) {
88 $table = $tableName; // Already has prefix
89 } else {
90 $table = $wpdb->prefix . $tableName; // Add prefix
91 }
92 $table = esc_sql($table);
93 $columnName = esc_sql($columnName);
94
95 // Check if slug exists
96 $query = $wpdb->prepare(
97 "SELECT COUNT(*) FROM `{$table}` WHERE `{$columnName}` = %s",
98 $slug
99 );
100
101 // Exclude current ID if updating
102 if ($excludeId !== null) {
103 $query = $wpdb->prepare(
104 "SELECT COUNT(*) FROM `{$table}` WHERE `{$columnName}` = %s AND id != %d",
105 $slug,
106 $excludeId
107 );
108 }
109
110 $exists = (int) $wpdb->get_var($query) > 0;
111
112 if (!$exists) {
113 return $slug;
114 }
115
116 // Try appending numbers until we find a unique slug
117 $counter = 1;
118 $uniqueSlug = $slug . '-' . $counter;
119
120 while (true) {
121 $checkQuery = $wpdb->prepare(
122 "SELECT COUNT(*) FROM `{$table}` WHERE `{$columnName}` = %s",
123 $uniqueSlug
124 );
125
126 if ($excludeId !== null) {
127 $checkQuery = $wpdb->prepare(
128 "SELECT COUNT(*) FROM `{$table}` WHERE `{$columnName}` = %s AND id != %d",
129 $uniqueSlug,
130 $excludeId
131 );
132 }
133
134 $exists = (int) $wpdb->get_var($checkQuery) > 0;
135
136 if (!$exists) {
137 return $uniqueSlug;
138 }
139
140 $counter++;
141 $uniqueSlug = $slug . '-' . $counter;
142 }
143 }
144 }
145
146