| @@ -1,0 +1,484 @@ | ||
| 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::isJoomla() && 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::isJoomla() && 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::isJoomla() && 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::isJoomla() && 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::isJoomla() && self::getID() == self::J37; | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Checks if the installed Joomla is 4.0 or higher. | |
| 302 | + * | |
| 303 | + * @return boolean True if Joomla is 4.0 or higher, otherwise false. | |
| 304 | + */ | |
| 305 | + public static function isJoomla40() | |
| 306 | + { | |
| 307 | + return self::isJoomla() && self::getID() == self::J40; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Alias for `isJoomla40`. | |
| 312 | + */ | |
| 313 | + public static function isJoomla4x() | |
| 314 | + { | |
| 315 | + return self::isJoomla40(); | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 319 | + * Checks if the installed Joomla is 5.0 or higher. | |
| 320 | + * | |
| 321 | + * @return boolean True if Joomla is 5.0 or higher, otherwise false. | |
| 322 | + */ | |
| 323 | + public static function isJoomla50() | |
| 324 | + { | |
| 325 | + return self::isJoomla() && version_compare(static::getVersion(), '5.0', '>='); | |
| 326 | + } | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * Checks if the installed Joomla is 6.0 or higher. | |
| 330 | + * | |
| 331 | + * @return boolean True if Joomla is 6.0 or higher, otherwise false. | |
| 332 | + */ | |
| 333 | + public static function isJoomla60() | |
| 334 | + { | |
| 335 | + return self::isJoomla() && version_compare(static::getVersion(), '6.0', '>='); | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * Checks if the installed Wordpress is 4.0 or higher. | |
| 340 | + * | |
| 341 | + * @return boolean True if Wordpress is 4.0 or higher, otherwise false. | |
| 342 | + */ | |
| 343 | + public static function isWordpress4() | |
| 344 | + { | |
| 345 | + return self::isWordpress() && self::getID() == self::WP4; | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** | |
| 349 | + * Checks if the installed Wordpress is 5.0 or higher. | |
| 350 | + * | |
| 351 | + * @return boolean True if Wordpress is 5.0 or higher, otherwise false. | |
| 352 | + */ | |
| 353 | + public static function isWordpress5() | |
| 354 | + { | |
| 355 | + return self::isWordpress() && self::getID() == self::WP5; | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Checks if the installed Joomla is supported. | |
| 360 | + * The Joomla version is not supported when the class is not able | |
| 361 | + * to recognize the installed version. | |
| 362 | + * | |
| 363 | + * @return boolean True if the Joomla version is supported, otherwise false. | |
| 364 | + */ | |
| 365 | + public static function isSupported() | |
| 366 | + { | |
| 367 | + return self::getID() != self::UNSUPPORTED; | |
| 368 | + } | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * Check if the current Joomla version is higher than the provided one. | |
| 372 | + * | |
| 373 | + * @param mixed The version to check. | |
| 374 | + * - int The platform ID will be compared; | |
| 375 | + * - string The platform version will be compared. | |
| 376 | + * | |
| 377 | + * @return boolean True if the current version is higher, otherwise false. | |
| 378 | + */ | |
| 379 | + public static function isHigherThan($version) | |
| 380 | + { | |
| 381 | + if (is_int($version)) | |
| 382 | + { | |
| 383 | + // compare platform ID only if an int was passed | |
| 384 | + return self::getID() > $version; | |
| 385 | + } | |
| 386 | + | |
| 387 | + // compare current version with provided version | |
| 388 | + return version_compare(static::getVersion(), $version, '>'); | |
| 389 | + } | |
| 390 | + | |
| 391 | + /** | |
| 392 | + * Check if the current Joomla version is lower than the provided one. | |
| 393 | + * | |
| 394 | + * @param mixed The version to check. | |
| 395 | + * - int The platform ID will be compared; | |
| 396 | + * - string The platform version will be compared. | |
| 397 | + * | |
| 398 | + * @return boolean True if the current version is lower, otherwise false. | |
| 399 | + */ | |
| 400 | + public static function isLowerThan($version) | |
| 401 | + { | |
| 402 | + if (is_int($version)) | |
| 403 | + { | |
| 404 | + return self::getID() < $version; | |
| 405 | + } | |
| 406 | + | |
| 407 | + // compare current version with provided version | |
| 408 | + return version_compare(static::getVersion(), $version, '<'); | |
| 409 | + } | |
| 410 | + | |
| 411 | + /** | |
| 412 | + * The UNSUPPORTED version identifier. | |
| 413 | + * | |
| 414 | + * @var integer | |
| 415 | + */ | |
| 416 | + const UNSUPPORTED = -1; | |
| 417 | + | |
| 418 | + /** | |
| 419 | + * The Joomla 1.5 version identifier. | |
| 420 | + * | |
| 421 | + * @var integer | |
| 422 | + */ | |
| 423 | + const J15 = 0; | |
| 424 | + | |
| 425 | + /** | |
| 426 | + * The Joomla 2.5 version identifier. | |
| 427 | + * | |
| 428 | + * @var integer | |
| 429 | + */ | |
| 430 | + const J25 = 1; | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * The Joomla 3.0 to 3.4 version identifier. | |
| 434 | + * | |
| 435 | + * @var integer | |
| 436 | + */ | |
| 437 | + const J30 = 2; | |
| 438 | + | |
| 439 | + /** | |
| 440 | + * The Joomla 3.5 to 4.0 (excluded) version identifier. | |
| 441 | + * | |
| 442 | + * @var integer | |
| 443 | + */ | |
| 444 | + const J35 = 3; | |
| 445 | + | |
| 446 | + /** | |
| 447 | + * The Joomla 3.7 version identifier. | |
| 448 | + * | |
| 449 | + * @var integer | |
| 450 | + */ | |
| 451 | + const J37 = 4; | |
| 452 | + | |
| 453 | + /** | |
| 454 | + * The Joomla 4.0 version identifier. | |
| 455 | + * | |
| 456 | + * @var integer | |
| 457 | + */ | |
| 458 | + const J40 = 5; | |
| 459 | + | |
| 460 | + /** | |
| 461 | + * The Wordpress 3.0 version identifier. | |
| 462 | + * | |
| 463 | + * @var integer | |
| 464 | + * @since 1.6.3 | |
| 465 | + */ | |
| 466 | + const WP3 = 0; | |
| 467 | + | |
| 468 | + /** | |
| 469 | + * The Wordpress 4.0 version identifier. | |
| 470 | + * | |
| 471 | + * @var integer | |
| 472 | + * @since 1.6.3 | |
| 473 | + */ | |
| 474 | + const WP4 = 1; | |
| 475 | + | |
| 476 | + /** | |
| 477 | + * The Wordpress 5.0 version identifier. | |
| 478 | + * | |
| 479 | + * @var integer | |
| 480 | + * @since 1.6.3 | |
| 481 | + */ | |
| 482 | + const WP5 = 2; | |
| 483 | + } | |
| 484 | +} | |