| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - E4J srl |
| 6 |
* @copyright Copyright (C) 2026 E4J srl. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Helper class to load webfont utilities and render font icons. |
| 15 |
* Fonts supported are FontAwesome and IcoMoon. |
| 16 |
* |
| 17 |
* @since 1.11 (J) - 1.1 (WP) |
| 18 |
* @since 1.18.8 (J) - 1.8.8 (WP) adopted FA 7.2.0 as default webfont library in favour of FA 5.12.1. |
| 19 |
*/ |
| 20 |
final class VikBookingIcons |
| 21 |
{ |
| 22 |
/** |
| 23 |
* The list of CSS files to load for the FontAwesome webfonts. |
| 24 |
* To save up resources, we only load the solid and regular icon styles. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public static $fa_default_styles = [ |
| 29 |
'fontawesome.min.css', |
| 30 |
'solid.min.css', |
| 31 |
'regular.min.css', |
| 32 |
]; |
| 33 |
|
| 34 |
/** |
| 35 |
* Associative list of known webfont style assets and related (relative) URL. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
* |
| 39 |
* @since 1.18.8 (J) - 1.8.8 (WP) |
| 40 |
*/ |
| 41 |
public static $fa_style_assets = [ |
| 42 |
'brands' => 'brands.min.css', |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* The list of CSS files that can be loaded from remote CDN URLs. |
| 47 |
* |
| 48 |
* @var array |
| 49 |
* |
| 50 |
* @deprecated 1.18.8 (J) - 1.8.8 (WP) |
| 51 |
*/ |
| 52 |
public static $fa_remote_assets = []; |
| 53 |
|
| 54 |
/** |
| 55 |
* Default font style prefix (Solid). |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
private static $fa_default_style = 'fas'; |
| 60 |
|
| 61 |
/** |
| 62 |
* List of name-class pairs for the icons that need specific |
| 63 |
* classes or adjustments in the current FontAwesome version. |
| 64 |
* |
| 65 |
* @var array |
| 66 |
*/ |
| 67 |
private static $fa_resolve_map = [ |
| 68 |
'calendar' => 'far fa-calendar-days', |
| 69 |
'refresh' => 'fas fa-rotate', |
| 70 |
'sync' => 'fas fa-arrows-rotate', |
| 71 |
'external-link' => 'fas fa-arrow-up-right-from-square', |
| 72 |
'external-link-square' => 'fas fa-square-up-right', |
| 73 |
'sort-asc' => 'fas fa-sort-up', |
| 74 |
'sort-desc' => 'fas fa-sort-down', |
| 75 |
'commenting' => 'fas fa-comments', |
| 76 |
'file-text' => 'far fa-file-lines', |
| 77 |
'file-text-o' => 'fas fa-file-lines', |
| 78 |
'sign-in' => 'fas fa-right-to-bracket', |
| 79 |
'sign-out' => 'fas fa-right-from-bracket', |
| 80 |
'edit' => 'far fa-pen-to-square', |
| 81 |
'clock' => 'far fa-clock', |
| 82 |
'clock-o' => 'fas fa-clock', |
| 83 |
'calendar-check' => 'far fa-calendar-check', |
| 84 |
'envelope-o' => 'far fa-envelope', |
| 85 |
'mobile' => 'fas fa-mobile-screen-button', |
| 86 |
'tablet' => 'fas fa-tablet-screen-button', |
| 87 |
'pie-chart' => 'fas fa-chart-pie', |
| 88 |
'credit-card' => 'far fa-credit-card', |
| 89 |
'sticky-note' => 'fas fa-note-sticky', |
| 90 |
'invoice' => 'fas fa-file-invoice', |
| 91 |
'snowflake' => 'far fa-snowflake', |
| 92 |
'long-arrow-up' => 'fas fa-arrow-up-long', |
| 93 |
'long-arrow-right' => 'fas fa-arrow-right-long', |
| 94 |
'long-arrow-down' => 'fas fa-arrow-down-long', |
| 95 |
'long-arrow-left' => 'fas fa-arrow-left-long', |
| 96 |
'th' => 'fas fa-table-cells', |
| 97 |
'ellipsis-h' => 'fas fa-ellipsis', |
| 98 |
'ellipsis-v' => 'fas fa-ellipsis-vertical', |
| 99 |
'magic' => 'fas fa-wand-magic-sparkles', |
| 100 |
'cog' => 'fas fa-gear', |
| 101 |
'cogs' => 'fas fa-gears', |
| 102 |
'user-cog' => 'fas fa-user-gear', |
| 103 |
'circle-question' => 'far fa-circle-question', |
| 104 |
|
| 105 |
/** |
| 106 |
* Resolving map used until the older FA v5.12.1. |
| 107 |
* |
| 108 |
'__fa5' => [ |
| 109 |
'calendar' => 'far fa-calendar-alt', |
| 110 |
'refresh' => 'fas fa-sync-alt', |
| 111 |
'external-link' => 'fas fa-external-link-alt', |
| 112 |
'external-link-square' => 'fas fa-external-link-square-alt', |
| 113 |
'sort-asc' => 'fas fa-sort-up', |
| 114 |
'sort-desc' => 'fas fa-sort-down', |
| 115 |
'commenting' => 'fas fa-comments', |
| 116 |
'file-text' => 'fas fa-file-alt', |
| 117 |
'file-text-o' => 'far fa-file-alt', |
| 118 |
'sign-in' => 'fas fa-sign-in-alt', |
| 119 |
'sign-out' => 'fas fa-sign-out-alt', |
| 120 |
'edit' => 'far fa-edit', |
| 121 |
'clock' => 'far fa-clock', |
| 122 |
'clock-o' => 'far fa-clock', |
| 123 |
'calendar-check' => 'far fa-calendar-check', |
| 124 |
'envelope-o' => 'far fa-envelope', |
| 125 |
'mobile' => 'fas fa-mobile-alt', |
| 126 |
'tablet' => 'fas fa-tablet-alt', |
| 127 |
'pie-chart' => 'fas fa-chart-pie', |
| 128 |
'credit-card' => 'far fa-credit-card', |
| 129 |
'sticky-note' => 'fas fa-sticky-note', |
| 130 |
'invoice' => 'fas fa-file-invoice', |
| 131 |
'snowflake' => 'far fa-snowflake', |
| 132 |
'long-arrow-up' => 'fas fa-long-arrow-alt-up', |
| 133 |
'long-arrow-right' => 'fas fa-long-arrow-alt-right', |
| 134 |
'long-arrow-down' => 'fas fa-long-arrow-alt-down', |
| 135 |
'long-arrow-left' => 'fas fa-long-arrow-alt-left', |
| 136 |
], |
| 137 |
* |
| 138 |
*/ |
| 139 |
]; |
| 140 |
|
| 141 |
/** |
| 142 |
* Loads the necessary assets for the requested font family. |
| 143 |
* |
| 144 |
* @param string $font the font identifier to load |
| 145 |
* |
| 146 |
* @return void |
| 147 |
* |
| 148 |
* @since 1.16.5 (J) - 1.6.5 (WP) added event to implement custom loadings. |
| 149 |
*/ |
| 150 |
public static function loadAssets($font = 'fa') |
| 151 |
{ |
| 152 |
$document = JFactory::getDocument(); |
| 153 |
|
| 154 |
// trigger event to allow third-party plugins to prevent this loading and to load custom assets |
| 155 |
$load_assets = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadFontAssets', [$document, $font]); |
| 156 |
if ($load_assets === false) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$versioning = [ |
| 161 |
'version' => defined('VIKBOOKING_SOFTWARE_VERSION') ? VIKBOOKING_SOFTWARE_VERSION : '1.0', |
| 162 |
]; |
| 163 |
|
| 164 |
if ($font == 'fa') { |
| 165 |
// get list of default asset files to load |
| 166 |
$font_files = self::$fa_default_styles; |
| 167 |
if (is_scalar($font_files)) { |
| 168 |
$font_files = [$font_files]; |
| 169 |
} |
| 170 |
|
| 171 |
// load files |
| 172 |
$baseuri = VBO_SITE_URI . 'resources/'; |
| 173 |
foreach ($font_files as $kf => $ff) { |
| 174 |
// we allow the use of an array with the URI expressed in the key |
| 175 |
$useuri = !is_numeric($kf) ? $kf : $baseuri; |
| 176 |
// load the file |
| 177 |
$document->addStyleSheet($useuri . $ff, $versioning); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if ($font == 'icomoon') { |
| 182 |
// we only need this file for IcoMoon |
| 183 |
$document->addStyleSheet(VBO_ADMIN_URI . 'resources/fonts/vboicomoon.css', $versioning); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Loads a specific webfont stylesheet asset, usually not loaded by default. |
| 189 |
* |
| 190 |
* @param string $style Known style identifier or stylesheet URL. |
| 191 |
* |
| 192 |
* @return bool True if an asset was loaded, false otherwise. |
| 193 |
* |
| 194 |
* @since 1.18.8 (J) - 1.8.8 (WP) |
| 195 |
*/ |
| 196 |
public static function loadAssetStyle(string $style) |
| 197 |
{ |
| 198 |
$document = JFactory::getDocument(); |
| 199 |
|
| 200 |
// trigger event to allow third-party plugins to prevent this loading and to load custom assets |
| 201 |
$load_asset = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadFontAssetStyle', [$document, $style]); |
| 202 |
if ($load_asset === false) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
if (preg_match('/^http/', $style)) { |
| 207 |
// load asset style URL |
| 208 |
$document->addStyleSheet($style); |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
$versioning = [ |
| 213 |
'version' => defined('VIKBOOKING_SOFTWARE_VERSION') ? VIKBOOKING_SOFTWARE_VERSION : '1.0', |
| 214 |
]; |
| 215 |
|
| 216 |
if (self::$fa_style_assets[$style] ?? null) { |
| 217 |
// load known and local webfont style asset |
| 218 |
$document->addStyleSheet(VBO_SITE_URI . 'resources/' . self::$fa_style_assets[$style], $versioning); |
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Loads the remote assets from the CDN, if any, by invoking third-party plugins. |
| 227 |
* |
| 228 |
* @return bool |
| 229 |
* |
| 230 |
* @since 1.16.5 (J) - 1.6.5 (WP) added event to implement custom loadings. |
| 231 |
* @since 1.18.8 (J) - 1.8.8 (WP) no more remote assets to load, FA brands asset loaded by default for BC. |
| 232 |
* |
| 233 |
* @see loadAssetStyle() |
| 234 |
*/ |
| 235 |
public static function loadRemoteAssets() |
| 236 |
{ |
| 237 |
static $loaded = null; |
| 238 |
|
| 239 |
if ($loaded !== null) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
$loaded = true; |
| 244 |
|
| 245 |
$document = JFactory::getDocument(); |
| 246 |
|
| 247 |
// trigger event to allow third-party plugins to prevent this loading and to load custom assets |
| 248 |
$load_assets = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadFontRemoteAssets', [$document]); |
| 249 |
if ($load_assets === false) { |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* For BC this method will no longer load remote assets, but it will |
| 255 |
* rather load the FA "brands" style asset through the new method. |
| 256 |
* |
| 257 |
* @since 1.18.8 (J) - 1.8.8 (WP) |
| 258 |
*/ |
| 259 |
self::loadAssetStyle('brands'); |
| 260 |
|
| 261 |
// scan remote assets list property, even if empty by default |
| 262 |
foreach (self::$fa_remote_assets as $cdn_url) { |
| 263 |
$document->addStyleSheet($cdn_url); |
| 264 |
} |
| 265 |
|
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Gets the proper class name for the icon depending on the type |
| 271 |
* and on the FA version currently in use. |
| 272 |
* |
| 273 |
* @param string $type the icon identifier. |
| 274 |
* @param string $classes a string with some optional classes. |
| 275 |
* @param string $style optional font icon style ("fas", "fab"..). |
| 276 |
* |
| 277 |
* @return string the full class name of the icon to load. |
| 278 |
* |
| 279 |
* @since 1.16.5 (J) - 1.6.5 (WP) added 3rd argument $style. |
| 280 |
*/ |
| 281 |
public static function i($type, $classes = '', $style = '') |
| 282 |
{ |
| 283 |
$classes = !empty($classes) ? ' ' . ltrim($classes) : $classes; |
| 284 |
|
| 285 |
if (substr($type, 0, 6) == 'vboicn') { |
| 286 |
// no mapping required for IcoMoon |
| 287 |
return $type . $classes; |
| 288 |
} |
| 289 |
|
| 290 |
// we can force the loading of a specific (full) and exact class, like for brands, in this case just return it |
| 291 |
if (substr($type, 0, 2) == 'fa' && strpos($type, ' ') > 2 && strpos($type, 'fa-') !== false && strlen($type) > 5) { |
| 292 |
return "{$type}{$classes}"; |
| 293 |
} |
| 294 |
|
| 295 |
// get the map to eventually adjust or override certain icons |
| 296 |
$override_map = self::getOverridesMap(); |
| 297 |
|
| 298 |
if (isset($override_map[$type])) { |
| 299 |
// this type of icon requires a specific class for the current FontAwesome version |
| 300 |
return $override_map[$type] . $classes; |
| 301 |
} |
| 302 |
|
| 303 |
// build font style (default to solid "fas") |
| 304 |
$fstyle = !empty($style) ? $style : self::getDefaultFontStyle(); |
| 305 |
|
| 306 |
// by default we use the solid style for the current FontAwesome version |
| 307 |
return "{$fstyle} fa-{$type}{$classes}"; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Echoes the requested icon string by passing all arguments to i(). |
| 312 |
* |
| 313 |
* @return void |
| 314 |
* |
| 315 |
* @uses i() |
| 316 |
*/ |
| 317 |
public static function e() |
| 318 |
{ |
| 319 |
$params = func_get_args(); |
| 320 |
|
| 321 |
$icn_class = call_user_func_array(['VikBookingIcons', 'i'], $params); |
| 322 |
|
| 323 |
echo '<i class="' . $icn_class . '"></i>'; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Returns a list of default font-icons for characteristics. |
| 328 |
* |
| 329 |
* @param array $exclude_html a list of HTML full i tags to exclude. |
| 330 |
* @param string $extra_class the default extra class for the icon tags. |
| 331 |
* @param bool $sort whether the icons should be sorted by name ASC. |
| 332 |
* |
| 333 |
* @return array a list of pre-set icons into associative arrays. |
| 334 |
* |
| 335 |
* @since 1.13.5 |
| 336 |
*/ |
| 337 |
public static function loadCharacteristicsPreset($exclude_html = [], $extra_class = 'vbo-icn-carat', $sort = true) |
| 338 |
{ |
| 339 |
$preset_icons = [ |
| 340 |
[ |
| 341 |
'name' => 'Square Meters', |
| 342 |
'class' => self::i('cube', $extra_class), |
| 343 |
], |
| 344 |
[ |
| 345 |
'name' => 'Swimming Pool', |
| 346 |
'class' => self::i('swimming-pool', $extra_class), |
| 347 |
], |
| 348 |
[ |
| 349 |
'name' => 'Wi-Fi', |
| 350 |
'class' => self::i('wifi', $extra_class), |
| 351 |
], |
| 352 |
[ |
| 353 |
'name' => 'TV', |
| 354 |
'class' => self::i('tv', $extra_class), |
| 355 |
], |
| 356 |
[ |
| 357 |
'name' => 'Air Conditioning', |
| 358 |
'class' => self::i('snowflake', $extra_class), |
| 359 |
], |
| 360 |
[ |
| 361 |
'name' => 'Mini Bar', |
| 362 |
'class' => self::i('cocktail', $extra_class), |
| 363 |
], |
| 364 |
[ |
| 365 |
'name' => 'Extra Bed', |
| 366 |
'class' => self::i('bed', $extra_class), |
| 367 |
], |
| 368 |
[ |
| 369 |
'name' => 'Disabled Access', |
| 370 |
'class' => self::i('wheelchair', $extra_class), |
| 371 |
], |
| 372 |
[ |
| 373 |
'name' => 'Bath', |
| 374 |
'class' => self::i('bath', $extra_class), |
| 375 |
], |
| 376 |
[ |
| 377 |
'name' => 'Shower', |
| 378 |
'class' => self::i('shower', $extra_class), |
| 379 |
], |
| 380 |
[ |
| 381 |
'name' => 'No Smoking', |
| 382 |
'class' => self::i('smoking-ban', $extra_class), |
| 383 |
], |
| 384 |
[ |
| 385 |
'name' => 'Smoking', |
| 386 |
'class' => self::i('smoking', $extra_class), |
| 387 |
], |
| 388 |
[ |
| 389 |
'name' => 'Coffee', |
| 390 |
'class' => self::i('coffee', $extra_class), |
| 391 |
], |
| 392 |
[ |
| 393 |
'name' => 'Tea Mug', |
| 394 |
'class' => self::i('mug-hot', $extra_class), |
| 395 |
], |
| 396 |
[ |
| 397 |
'name' => 'Kitchen Utensils', |
| 398 |
'class' => self::i('utensils', $extra_class), |
| 399 |
], |
| 400 |
[ |
| 401 |
'name' => 'Gift', |
| 402 |
'class' => self::i('gift', $extra_class), |
| 403 |
], |
| 404 |
[ |
| 405 |
'name' => 'Terrace', |
| 406 |
'class' => self::i('umbrella-beach', $extra_class), |
| 407 |
], |
| 408 |
[ |
| 409 |
'name' => 'Mountain', |
| 410 |
'class' => self::i('mountain', $extra_class), |
| 411 |
], |
| 412 |
[ |
| 413 |
'name' => 'Landscape', |
| 414 |
'class' => self::i('image', $extra_class), |
| 415 |
], |
| 416 |
[ |
| 417 |
'name' => 'Nature', |
| 418 |
'class' => self::i('tree', $extra_class), |
| 419 |
], |
| 420 |
[ |
| 421 |
'name' => 'City', |
| 422 |
'class' => self::i('city', $extra_class), |
| 423 |
], |
| 424 |
[ |
| 425 |
'name' => 'Sea', |
| 426 |
'class' => self::i('water', $extra_class), |
| 427 |
], |
| 428 |
]; |
| 429 |
|
| 430 |
// check if some icons should be unset, maybe because already in use |
| 431 |
$exclude_html = !is_array($exclude_html) ? [] : $exclude_html; |
| 432 |
foreach ($exclude_html as $html) { |
| 433 |
if (empty($html)) { |
| 434 |
continue; |
| 435 |
} |
| 436 |
foreach ($preset_icons as $k => $v) { |
| 437 |
if (strpos($html, $v['class']) !== false) { |
| 438 |
// this class matches in the HTML passed, unset it |
| 439 |
unset($preset_icons[$k]); |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
// sort icons by name |
| 446 |
if ($sort) { |
| 447 |
$names_map = []; |
| 448 |
foreach ($preset_icons as $k => $v) { |
| 449 |
$names_map[$k] = $v['name']; |
| 450 |
} |
| 451 |
asort($names_map); |
| 452 |
$sorted_preset = []; |
| 453 |
foreach ($names_map as $k => $v) { |
| 454 |
if (!isset($preset_icons[$k])) { |
| 455 |
continue; |
| 456 |
} |
| 457 |
array_push($sorted_preset, $preset_icons[$k]); |
| 458 |
} |
| 459 |
$preset_icons = $sorted_preset; |
| 460 |
} |
| 461 |
|
| 462 |
// return the list |
| 463 |
return $preset_icons; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Loads the font icons override map. |
| 468 |
* |
| 469 |
* @return array |
| 470 |
* |
| 471 |
* @since 1.16.5 (J) - 1.6.5 (WP). |
| 472 |
*/ |
| 473 |
private static function getOverridesMap() |
| 474 |
{ |
| 475 |
static $icons_map = null; |
| 476 |
|
| 477 |
if ($icons_map) { |
| 478 |
return $icons_map; |
| 479 |
} |
| 480 |
|
| 481 |
$icons_map = self::$fa_resolve_map; |
| 482 |
|
| 483 |
// trigger event to allow third-party plugins to override the icons map |
| 484 |
VBOFactory::getPlatform()->getDispatcher()->trigger('onBuildFontOverridesMap', [&$icons_map]); |
| 485 |
|
| 486 |
return $icons_map ?: []; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Gets the default font style. |
| 491 |
* |
| 492 |
* @return string |
| 493 |
* |
| 494 |
* @since 1.16.5 (J) - 1.6.5 (WP). |
| 495 |
*/ |
| 496 |
private static function getDefaultFontStyle() |
| 497 |
{ |
| 498 |
static $icons_style = null; |
| 499 |
|
| 500 |
if ($icons_style !== null) { |
| 501 |
return $icons_style; |
| 502 |
} |
| 503 |
|
| 504 |
$icons_style = self::$fa_default_style; |
| 505 |
|
| 506 |
// trigger event to allow third-party plugins to override the default icons style |
| 507 |
VBOFactory::getPlatform()->getDispatcher()->trigger('onGetFontDefaultStyle', [&$icons_style]); |
| 508 |
|
| 509 |
return (string) $icons_style; |
| 510 |
} |
| 511 |
} |
| 512 |
|