PluginProbe
Polylang / 3.8.8
Polylang v3.8.8
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / constant-functions.php

constant-functions.php in Polylang 3.8.8, at src/constant-functions.php

67 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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