| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking - Libraries |
| 4 |
* @subpackage wordpress |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2018 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
// do nothing if the class already exists |
| 15 |
if (!class_exists('VersionListener')) |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Version recognizer class to identify which platform version is running. |
| 19 |
* |
| 20 |
* @see JVersion Used to identify the installed platform version. |
| 21 |
* |
| 22 |
* @since 1.6 |
| 23 |
*/ |
| 24 |
class VersionListener |
| 25 |
{ |
| 26 |
/** |
| 27 |
* The platform name. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
* @since 1.6.3 |
| 31 |
*/ |
| 32 |
private static $platform = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* The platform version. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
* @since 1.6.3 |
| 39 |
*/ |
| 40 |
private static $version = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* The identifier of the platform version. |
| 44 |
* |
| 45 |
* @var integer |
| 46 |
*/ |
| 47 |
private static $id = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* Class constructor. |
| 51 |
*/ |
| 52 |
private function __construct() |
| 53 |
{ |
| 54 |
// not accessible |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Class cloner. |
| 59 |
*/ |
| 60 |
private function __clone() |
| 61 |
{ |
| 62 |
// not accessible |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the current platform name. |
| 67 |
* |
| 68 |
* @return string The platform name. |
| 69 |
* |
| 70 |
* @since 1.6.3 |
| 71 |
*/ |
| 72 |
public static function getPlatform() |
| 73 |
{ |
| 74 |
if (self::$platform === null) |
| 75 |
{ |
| 76 |
if (defined('WPINC')) |
| 77 |
{ |
| 78 |
// wordpress is installed |
| 79 |
self::$platform = 'wordpress'; |
| 80 |
} |
| 81 |
else if (defined('_JEXEC')) |
| 82 |
{ |
| 83 |
// joomla is installed |
| 84 |
self::$platform = 'joomla'; |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
// platform not supported |
| 89 |
$self::$platform = false; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return self::$platform; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Checks if the current platform is Wordpress. |
| 98 |
* |
| 99 |
* @return boolean |
| 100 |
* |
| 101 |
* @since 1.6.3 |
| 102 |
*/ |
| 103 |
public static function isWordpress() |
| 104 |
{ |
| 105 |
return self::getPlatform() === 'wordpress'; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Checks if the current platform is Wordpress. |
| 110 |
* |
| 111 |
* @return boolean |
| 112 |
* |
| 113 |
* @since 1.6.3 |
| 114 |
*/ |
| 115 |
public static function isJoomla() |
| 116 |
{ |
| 117 |
return self::getPlatform() === 'joomla'; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the current platform version. |
| 122 |
* |
| 123 |
* @return string The platform version. |
| 124 |
* |
| 125 |
* @since 1.6.3 |
| 126 |
*/ |
| 127 |
public static function getVersion() |
| 128 |
{ |
| 129 |
if (self::$version === null) |
| 130 |
{ |
| 131 |
$version = new JVersion(); |
| 132 |
self::$version = $version->getShortVersion(); |
| 133 |
} |
| 134 |
|
| 135 |
return self::$version; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Recognizes the Joomla version and return the respective indetifier. |
| 140 |
* |
| 141 |
* @return integer The identifier of the Joomla version. |
| 142 |
*/ |
| 143 |
public static function getID() |
| 144 |
{ |
| 145 |
if (self::$id === null) |
| 146 |
{ |
| 147 |
// UNSUPPORTED flag will be overridden (if supported) |
| 148 |
self::$id = self::UNSUPPORTED; |
| 149 |
|
| 150 |
if (class_exists('JVersion')) |
| 151 |
{ |
| 152 |
// get platform version |
| 153 |
$v = static::getVersion(); |
| 154 |
|
| 155 |
if (static::isJoomla()) |
| 156 |
{ |
| 157 |
// get Joomla version identifier |
| 158 |
self::$id = static::getJoomlaID($v); |
| 159 |
} |
| 160 |
else if (static::isWordpress()) |
| 161 |
{ |
| 162 |
// get Wordpress version identifier |
| 163 |
self::$id = static::getWordpressID($v); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return self::$id; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Detects the current Wordpress version constant. |
| 173 |
* |
| 174 |
* @param string $v The platform short version. |
| 175 |
* |
| 176 |
* @return integer The version constant. |
| 177 |
* |
| 178 |
* @since 1.6.3 |
| 179 |
*/ |
| 180 |
protected static function getWordpressID($v) |
| 181 |
{ |
| 182 |
if (version_compare($v, '4.0') >= 0 && version_compare($v, '5.0') < 0) |
| 183 |
{ |
| 184 |
// wordpress 4.0 |
| 185 |
return self::WP4; |
| 186 |
} |
| 187 |
else if (version_compare($v, '5.0') >= 0) |
| 188 |
{ |
| 189 |
// wordpress 5.0 |
| 190 |
return self::WP5; |
| 191 |
} |
| 192 |
|
| 193 |
// fallback to Wordpress 3.0 or lower |
| 194 |
return self::WP3; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Detects the current Joomla! version constant. |
| 199 |
* |
| 200 |
* @param string $v The platform short version. |
| 201 |
* |
| 202 |
* @return integer The version constant. |
| 203 |
* |
| 204 |
* @since 1.6.3 |
| 205 |
*/ |
| 206 |
protected static function getJoomlaID($v) |
| 207 |
{ |
| 208 |
if (version_compare($v, '2.5') >= 0 && version_compare($v, '3.0') < 0) |
| 209 |
{ |
| 210 |
// joomla 2.5 |
| 211 |
return self::J25; |
| 212 |
} |
| 213 |
else if (version_compare($v, '3.0') >= 0 && version_compare($v, '3.5') < 0) |
| 214 |
{ |
| 215 |
// joomla 3.0, 3.1, 3.2, 3.3, 3.4 |
| 216 |
return self::J30; |
| 217 |
} |
| 218 |
else if (version_compare($v, '3.5') >= 0 && version_compare($v, '3.7') < 0) |
| 219 |
{ |
| 220 |
// joomla 3.5, 3.6 |
| 221 |
return self::J35; |
| 222 |
} |
| 223 |
else if (version_compare($v, '3.7') >= 0 && version_compare($v, '4.0') < 0) |
| 224 |
{ |
| 225 |
// joomla 3.7, 3.8, 3.9 |
| 226 |
return self::J37; |
| 227 |
} |
| 228 |
else if (version_compare($v, '4.0') >= 0) |
| 229 |
{ |
| 230 |
// joomla 4.0 |
| 231 |
return self::J40; |
| 232 |
} |
| 233 |
|
| 234 |
// fallback to Joomla 1.5 |
| 235 |
return self::J15; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Checks if the installed Joomla is 1.5 or 1.6. |
| 240 |
* |
| 241 |
* @return boolean True if Joomla is 1.5 or 1.6, otherwise false. |
| 242 |
*/ |
| 243 |
public static function isJoomla15() |
| 244 |
{ |
| 245 |
return self::getID() == self::J15; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Checks if the installed Joomla is 2.5. |
| 250 |
* |
| 251 |
* @return boolean True if Joomla is 2.5, otherwise false. |
| 252 |
*/ |
| 253 |
public static function isJoomla25() |
| 254 |
{ |
| 255 |
return self::getID() == self::J25; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Checks if the installed Joomla is 3.x. |
| 260 |
* |
| 261 |
* @return boolean True if Joomla is between 3.0 and 4.0 (excluded), otherwise false. |
| 262 |
* |
| 263 |
* @since 1.8.3 |
| 264 |
*/ |
| 265 |
public static function isJoomla3x() |
| 266 |
{ |
| 267 |
return self::isJoomla30() || self::isJoomla35() || self::isJoomla37(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Checks if the installed Joomla is between 3.0 and 3.4. |
| 272 |
* |
| 273 |
* @return boolean True if Joomla is between 3.0 and 3.4, otherwise false. |
| 274 |
*/ |
| 275 |
public static function isJoomla30() |
| 276 |
{ |
| 277 |
return self::getID() == self::J30; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Checks if the installed Joomla is between 3.5 and 4.0 (excluded). |
| 282 |
* |
| 283 |
* @return boolean True if Joomla is between 3.0 and 4.0 (excluded), otherwise false. |
| 284 |
*/ |
| 285 |
public static function isJoomla35() |
| 286 |
{ |
| 287 |
return self::getID() == self::J35; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Checks if the installed Joomla is 3.7 or higher. |
| 292 |
* |
| 293 |
* @return boolean True if Joomla is 3.7 or higher, otherwise false. |
| 294 |
*/ |
| 295 |
public static function isJoomla37() |
| 296 |
{ |
| 297 |
return self::getID() == self::J37; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Checks if the installed Joomla is 4.x. |
| 302 |
* |
| 303 |
* @return boolean True if Joomla is between 4.0 and 5.0 (excluded), otherwise false. |
| 304 |
* |
| 305 |
* @since 1.8.3 |
| 306 |
*/ |
| 307 |
public static function isJoomla4x() |
| 308 |
{ |
| 309 |
return self::isJoomla40(); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Checks if the installed Joomla is 4.0 or higher. |
| 314 |
* |
| 315 |
* @return boolean True if Joomla is 4.0 or higher, otherwise false. |
| 316 |
*/ |
| 317 |
public static function isJoomla40() |
| 318 |
{ |
| 319 |
return self::getID() == self::J40; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Checks if the installed Joomla is 5.0 or higher. |
| 324 |
* |
| 325 |
* @return boolean True if Joomla is 5.0 or higher, otherwise false. |
| 326 |
*/ |
| 327 |
public static function isJoomla50() |
| 328 |
{ |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Checks if the installed Wordpress is 4.0 or higher. |
| 334 |
* |
| 335 |
* @return boolean True if Wordpress is 4.0 or higher, otherwise false. |
| 336 |
*/ |
| 337 |
public static function isWordpress4() |
| 338 |
{ |
| 339 |
return self::getID() == self::WP4; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Checks if the installed Wordpress is 5.0 or higher. |
| 344 |
* |
| 345 |
* @return boolean True if Wordpress is 5.0 or higher, otherwise false. |
| 346 |
*/ |
| 347 |
public static function isWordpress5() |
| 348 |
{ |
| 349 |
return self::getID() == self::WP5; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Checks if the installed Joomla is supported. |
| 354 |
* The Joomla version is not supported when the class is not able |
| 355 |
* to recognize the installed version. |
| 356 |
* |
| 357 |
* @return boolean True if the Joomla version is supported, otherwise false. |
| 358 |
*/ |
| 359 |
public static function isSupported() |
| 360 |
{ |
| 361 |
return self::getID() != self::UNSUPPORTED; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Check if the current Joomla version is higher than the provided one. |
| 366 |
* |
| 367 |
* @param mixed The version to check. |
| 368 |
* - int The platform ID will be compared; |
| 369 |
* - string The platform version will be compared. |
| 370 |
* |
| 371 |
* @return boolean True if the current version is higher, otherwise false. |
| 372 |
*/ |
| 373 |
public static function isHigherThan($version) |
| 374 |
{ |
| 375 |
if (is_int($version)) |
| 376 |
{ |
| 377 |
// compare platform ID only if an int was passed |
| 378 |
return self::getID() > $version; |
| 379 |
} |
| 380 |
|
| 381 |
// compare current version with provided version |
| 382 |
return version_compare(static::getVersion(), $version, '>'); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Check if the current Joomla version is lower than the provided one. |
| 387 |
* |
| 388 |
* @param mixed The version to check. |
| 389 |
* - int The platform ID will be compared; |
| 390 |
* - string The platform version will be compared. |
| 391 |
* |
| 392 |
* @return boolean True if the current version is lower, otherwise false. |
| 393 |
*/ |
| 394 |
public static function isLowerThan($version) |
| 395 |
{ |
| 396 |
if (is_int($version)) |
| 397 |
{ |
| 398 |
return self::getID() < $version; |
| 399 |
} |
| 400 |
|
| 401 |
// compare current version with provided version |
| 402 |
return version_compare(static::getVersion(), $version, '<'); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* The UNSUPPORTED version identifier. |
| 407 |
* |
| 408 |
* @var integer |
| 409 |
*/ |
| 410 |
const UNSUPPORTED = -1; |
| 411 |
|
| 412 |
/** |
| 413 |
* The Joomla 1.5 version identifier. |
| 414 |
* |
| 415 |
* @var integer |
| 416 |
*/ |
| 417 |
const J15 = 0; |
| 418 |
|
| 419 |
/** |
| 420 |
* The Joomla 2.5 version identifier. |
| 421 |
* |
| 422 |
* @var integer |
| 423 |
*/ |
| 424 |
const J25 = 1; |
| 425 |
|
| 426 |
/** |
| 427 |
* The Joomla 3.0 to 3.4 version identifier. |
| 428 |
* |
| 429 |
* @var integer |
| 430 |
*/ |
| 431 |
const J30 = 2; |
| 432 |
|
| 433 |
/** |
| 434 |
* The Joomla 3.5 to 4.0 (excluded) version identifier. |
| 435 |
* |
| 436 |
* @var integer |
| 437 |
*/ |
| 438 |
const J35 = 3; |
| 439 |
|
| 440 |
/** |
| 441 |
* The Joomla 3.7 version identifier. |
| 442 |
* |
| 443 |
* @var integer |
| 444 |
*/ |
| 445 |
const J37 = 4; |
| 446 |
|
| 447 |
/** |
| 448 |
* The Joomla 4.0 version identifier. |
| 449 |
* |
| 450 |
* @var integer |
| 451 |
*/ |
| 452 |
const J40 = 5; |
| 453 |
|
| 454 |
/** |
| 455 |
* The Wordpress 3.0 version identifier. |
| 456 |
* |
| 457 |
* @var integer |
| 458 |
* @since 1.6.3 |
| 459 |
*/ |
| 460 |
const WP3 = 0; |
| 461 |
|
| 462 |
/** |
| 463 |
* The Wordpress 4.0 version identifier. |
| 464 |
* |
| 465 |
* @var integer |
| 466 |
* @since 1.6.3 |
| 467 |
*/ |
| 468 |
const WP4 = 1; |
| 469 |
|
| 470 |
/** |
| 471 |
* The Wordpress 5.0 version identifier. |
| 472 |
* |
| 473 |
* @var integer |
| 474 |
* @since 1.6.3 |
| 475 |
*/ |
| 476 |
const WP5 = 2; |
| 477 |
} |
| 478 |
} |
| 479 |
|