| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
* |
| 5 |
* The aim of these functions is to be stubed in unit tests. |
| 6 |
* |
| 7 |
* /!\ THE CODE IN THIS FILE MUST BE COMPATIBLE WITH PHP 5.6. |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Tells if a constant is defined. |
| 12 |
* |
| 13 |
* @since 3.5 |
| 14 |
* |
| 15 |
* @param string $constant_name Name of the constant. |
| 16 |
* @return bool True if the constant is defined, false otherwise. |
| 17 |
* |
| 18 |
* @phpstan-param non-falsy-string $constant_name |
| 19 |
*/ |
| 20 |
function pll_has_constant( $constant_name ) { |
| 21 |
return defined( $constant_name ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns the value of a constant if it is defined. |
| 26 |
* |
| 27 |
* @since 3.5 |
| 28 |
* |
| 29 |
* @param string $constant_name Name of the constant. |
| 30 |
* @param mixed $default Optional. Value to return if the constant is not defined. Defaults to `null`. |
| 31 |
* @return mixed The value of the constant. |
| 32 |
* |
| 33 |
* @phpstan-template D of int|float|string|bool|array|null |
| 34 |
* @phpstan-param non-falsy-string $constant_name |
| 35 |
* @phpstan-param D $default |
| 36 |
* @phpstan-return D |
| 37 |
*/ |
| 38 |
function pll_get_constant( $constant_name, $default = null ) { |
| 39 |
if ( ! pll_has_constant( $constant_name ) ) { |
| 40 |
return $default; |
| 41 |
} |
| 42 |
|
| 43 |
/** @phpstan-var D $return */ |
| 44 |
$return = constant( $constant_name ); |
| 45 |
return $return; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Defines a constant if it is not already defined. |
| 50 |
* |
| 51 |
* @since 3.5 |
| 52 |
* |
| 53 |
* @param string $constant_name Name of the constant. |
| 54 |
* @param mixed $value Value to set. |
| 55 |
* @return bool True on success, false on failure or already defined. |
| 56 |
* |
| 57 |
* @phpstan-param non-falsy-string $constant_name |
| 58 |
* @phpstan-param int|float|string|bool|array|null $value |
| 59 |
*/ |
| 60 |
function pll_set_constant( $constant_name, $value ) { |
| 61 |
if ( pll_has_constant( $constant_name ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
return define( $constant_name, $value ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName |
| 66 |
} |
| 67 |
|