Config.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Core Config |
| 12 | * |
| 13 | * @since 6.3.0 Added new method `replace` |
| 14 | * @since 6.0.0 Initial implementation of the class |
| 15 | * |
| 16 | * @package AAM |
| 17 | * @version 6.3.0 |
| 18 | */ |
| 19 | class AAM_Core_Config |
| 20 | { |
| 21 | |
| 22 | /** |
| 23 | * Core AAM config db option |
| 24 | * |
| 25 | * @version 6.0.0 |
| 26 | */ |
| 27 | const DB_OPTION = 'aam_config'; |
| 28 | |
| 29 | /** |
| 30 | * Core config |
| 31 | * |
| 32 | * @var array |
| 33 | * |
| 34 | * @access protected |
| 35 | * @version 6.0.0 |
| 36 | */ |
| 37 | protected static $config = array(); |
| 38 | |
| 39 | /** |
| 40 | * Load core AAM config |
| 41 | * |
| 42 | * @return void |
| 43 | * |
| 44 | * @access public |
| 45 | * @version 6.0.0 |
| 46 | */ |
| 47 | public static function bootstrap() |
| 48 | { |
| 49 | self::$config = AAM_Core_API::getOption(self::DB_OPTION, array()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get config option |
| 54 | * |
| 55 | * @param string $option |
| 56 | * @param mixed $default |
| 57 | * |
| 58 | * @return mixed |
| 59 | * |
| 60 | * @access public |
| 61 | * @version 6.0.0 |
| 62 | */ |
| 63 | public static function get($option, $default = null) |
| 64 | { |
| 65 | if (array_key_exists($option, self::$config)) { |
| 66 | $response = self::$config[$option]; |
| 67 | } else { |
| 68 | $response = self::readConfigPress($option, $default); |
| 69 | } |
| 70 | |
| 71 | return ($response ? self::normalize($response) : $response); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Normalize config option |
| 76 | * |
| 77 | * @param string $setting |
| 78 | * |
| 79 | * @return string |
| 80 | * |
| 81 | * @access protected |
| 82 | * @version 6.0.0 |
| 83 | */ |
| 84 | protected static function normalize($setting) |
| 85 | { |
| 86 | return str_replace(array('{ABSPATH}'), array(ABSPATH), $setting); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set config option |
| 91 | * |
| 92 | * @param string $option |
| 93 | * @param mixed $value |
| 94 | * |
| 95 | * @return boolean |
| 96 | * |
| 97 | * @access public |
| 98 | * @version 6.0.0 |
| 99 | */ |
| 100 | public static function set($option, $value) |
| 101 | { |
| 102 | self::$config[$option] = $value; |
| 103 | |
| 104 | // Save config to database |
| 105 | return AAM_Core_API::updateOption(self::DB_OPTION, self::$config); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Replace the entire config |
| 110 | * |
| 111 | * @param array $config |
| 112 | * |
| 113 | * @return boolean |
| 114 | * |
| 115 | * @access public |
| 116 | * @version 6.3.0 |
| 117 | */ |
| 118 | public static function replace($config) |
| 119 | { |
| 120 | self::$config = $config; |
| 121 | |
| 122 | // Save config to database |
| 123 | return AAM_Core_API::updateOption(self::DB_OPTION, self::$config); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Delete config option |
| 128 | * |
| 129 | * @param string $option |
| 130 | * |
| 131 | * @return boolean |
| 132 | * |
| 133 | * @access public |
| 134 | * @version 6.0.0 |
| 135 | */ |
| 136 | public static function delete($option) |
| 137 | { |
| 138 | if (array_key_exists($option, self::$config)) { |
| 139 | unset(self::$config[$option]); |
| 140 | |
| 141 | $result = AAM_Core_API::updateOption(self::DB_OPTION, self::$config); |
| 142 | } |
| 143 | |
| 144 | return !empty($result); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get ConfigPress parameter |
| 149 | * |
| 150 | * @param string $param |
| 151 | * @param mixed $default |
| 152 | * |
| 153 | * @return mixed |
| 154 | * |
| 155 | * @access public |
| 156 | * @version 6.0.0 |
| 157 | */ |
| 158 | protected static function readConfigPress($param, $default = null) |
| 159 | { |
| 160 | $config = AAM_Core_ConfigPress::get('aam.' . $param, $default); |
| 161 | |
| 162 | if (is_array($config) && isset($config['userFunc'])) { |
| 163 | if (is_callable($config['userFunc'])) { |
| 164 | $response = call_user_func($config['userFunc']); |
| 165 | } else { |
| 166 | $response = $default; |
| 167 | } |
| 168 | } else { |
| 169 | $response = $config; |
| 170 | } |
| 171 | |
| 172 | return $response; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Reset internal cache |
| 177 | * |
| 178 | * @return void |
| 179 | * |
| 180 | * @access public |
| 181 | * @version 6.0.0 |
| 182 | */ |
| 183 | public static function reset() |
| 184 | { |
| 185 | self::$config = array(); |
| 186 | } |
| 187 | |
| 188 | } |