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