PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-constants / src / class-constants.php

class-constants.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-constants/src/class-constants.php

125 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A constants manager for Jetpack.
4 *
5 * @package automattic/jetpack-constants
6 */
7
8 namespace Automattic\Jetpack;
9
10 /**
11 * Class Automattic\Jetpack\Constants
12 *
13 * Testing constants is hard. Once you define a constant, it's defined. Constants Manager is an
14 * abstraction layer so that unit tests can set "constants" for tests.
15 *
16 * To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Automattic\Jetpack\Constants::is_defined( 'CONSTANT' )`
17 * and replace `CONSTANT` with `Automattic\Jetpack\Constants::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the
18 * constant with `Automattic\Jetpack\Constants::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like
19 * this:
20 *
21 * function tearDown() {
22 * Automattic\Jetpack\Constants::clear_constants();
23 * }
24 */
25 class Constants {
26 /**
27 * A container for all defined constants.
28 *
29 * @access public
30 * @static
31 *
32 * @var array
33 */
34 public static $set_constants = array();
35
36 /**
37 * Checks if a "constant" has been set in constants Manager
38 * and has a truthy value (e.g. not null, not false, not 0, any string).
39 *
40 * @param string $name The name of the constant.
41 *
42 * @return bool
43 */
44 public static function is_true( $name ) {
45 return self::is_defined( $name ) && self::get_constant( $name );
46 }
47
48 /**
49 * Checks if a "constant" has been set in constants Manager, and if not,
50 * checks if the constant was defined with define( 'name', 'value ).
51 *
52 * @param string $name The name of the constant.
53 *
54 * @return bool
55 */
56 public static function is_defined( $name ) {
57 return array_key_exists( $name, self::$set_constants )
58 ? true
59 : defined( $name );
60 }
61
62 /**
63 * Attempts to retrieve the "constant" from constants Manager, and if it hasn't been set,
64 * then attempts to get the constant with the constant() function. If that also hasn't
65 * been set, attempts to get a value from filters.
66 *
67 * @param string $name The name of the constant.
68 *
69 * @return mixed null if the constant does not exist or the value of the constant.
70 */
71 public static function get_constant( $name ) {
72 if ( array_key_exists( $name, self::$set_constants ) ) {
73 return self::$set_constants[ $name ];
74 }
75
76 if ( defined( $name ) ) {
77 return constant( $name );
78 }
79
80 /**
81 * Filters the value of the constant.
82 *
83 * @since 1.2.0
84 *
85 * @param null The constant value to be filtered. The default is null.
86 * @param String $name The constant name.
87 */
88 return apply_filters( 'jetpack_constant_default_value', null, $name );
89 }
90
91 /**
92 * Sets the value of the "constant" within constants Manager.
93 *
94 * @param string $name The name of the constant.
95 * @param int|float|string|bool|array|null $value The value of the constant.
96 */
97 public static function set_constant( $name, $value ) {
98 self::$set_constants[ $name ] = $value;
99 }
100
101 /**
102 * Will unset a "constant" from constants Manager if the constant exists.
103 *
104 * @param string $name The name of the constant.
105 *
106 * @return bool Whether the constant was removed.
107 */
108 public static function clear_single_constant( $name ) {
109 if ( ! array_key_exists( $name, self::$set_constants ) ) {
110 return false;
111 }
112
113 unset( self::$set_constants[ $name ] );
114
115 return true;
116 }
117
118 /**
119 * Resets all of the constants within constants Manager.
120 */
121 public static function clear_constants() {
122 self::$set_constants = array();
123 }
124 }
125