| 1 |
<?php |
| 2 |
|
| 3 |
namespace Kubio; |
| 4 |
|
| 5 |
use IlluminateAgnostic\Arr\Support\Arr; |
| 6 |
|
| 7 |
class Flags { |
| 8 |
private static $instance = null; |
| 9 |
private $flags = array(); |
| 10 |
private $is_dirty_value = false; |
| 11 |
|
| 12 |
|
| 13 |
private function __construct() { |
| 14 |
|
| 15 |
$this->flags = get_option( '__kubio_instance_flags', array() ); |
| 16 |
add_action( 'shutdown', array( $this, 'save' ) ); |
| 17 |
|
| 18 |
$default_settings = require_once KUBIO_ROOT_DIR . '/defaults/settings.php'; |
| 19 |
|
| 20 |
$this->flags['_settings'] = array_replace_recursive( |
| 21 |
$default_settings, |
| 22 |
Arr::get( $this->flags, '_settings', array() ) |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param string $flag |
| 28 |
* @param mixed $value |
| 29 |
* |
| 30 |
*/ |
| 31 |
public static function set( $flag, $value ) { |
| 32 |
static::getInstance()->setFlag( $flag, $value ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param string $flag |
| 37 |
* @param mixed $value |
| 38 |
* |
| 39 |
|
| 40 |
*/ |
| 41 |
function setFlag( $flag, $value ) { |
| 42 |
$this->withFlags( 'set', $flag, $value ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param $action |
| 47 |
* @param null $flag |
| 48 |
* @param null $data |
| 49 |
* |
| 50 |
* @return mixed|null |
| 51 |
*/ |
| 52 |
private function withFlags( $action, $flag = null, $data = null ) { |
| 53 |
if ( $action === 'get-all' ) { |
| 54 |
return $this->flags; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $action === 'get' ) { |
| 58 |
if ( isset( $this->flags[ $flag ] ) ) { |
| 59 |
return $this->flags[ $flag ]; |
| 60 |
} |
| 61 |
|
| 62 |
$kubio_flags_defaults = apply_filters( 'kubio/instance-flags-default', array() ); |
| 63 |
|
| 64 |
if ( isset( $kubio_flags_defaults[ $flag ] ) ) { |
| 65 |
return $kubio_flags_defaults[ $flag ]; |
| 66 |
} |
| 67 |
|
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
if ( $action === 'set' ) { |
| 72 |
$this->flags[ $flag ] = $data; |
| 73 |
$this->is_dirty_value = true; |
| 74 |
$this->save(); |
| 75 |
return $data; |
| 76 |
} |
| 77 |
|
| 78 |
if ( $action === 'delete' ) { |
| 79 |
if ( isset( $this->flags[ $flag ] ) ) { |
| 80 |
unset( $this->flags[ $flag ] ); |
| 81 |
$this->is_dirty_value = true; |
| 82 |
} |
| 83 |
$this->save(); |
| 84 |
return null; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return null |
| 90 |
*/ |
| 91 |
private static function getInstance() { |
| 92 |
|
| 93 |
if ( ! self::$instance ) { |
| 94 |
self::$instance = new self(); |
| 95 |
} |
| 96 |
|
| 97 |
return self::$instance; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param string $flag |
| 102 |
* |
| 103 |
*/ |
| 104 |
public static function delete( $flag ) { |
| 105 |
static::getInstance()->deleteFlag( $flag ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param string $flag |
| 110 |
* |
| 111 |
*/ |
| 112 |
function deleteFlag( $flag ) { |
| 113 |
$this->withFlags( 'delete', $flag ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @param string $flag |
| 118 |
* @param mixed $fallback |
| 119 |
* |
| 120 |
* @return mixed|null |
| 121 |
*/ |
| 122 |
public static function get( $flag, $fallback = null ) { |
| 123 |
return static::getInstance()->getFlag( $flag, $fallback ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string $flag |
| 128 |
* @param mixed $fallback |
| 129 |
* |
| 130 |
* @return mixed|null |
| 131 |
*/ |
| 132 |
function getFlag( $flag, $fallback = null ) { |
| 133 |
return $this->withFlags( 'get', $flag, $fallback ); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
public function save() { |
| 138 |
if ( $this->is_dirty_value ) { |
| 139 |
update_option( '__kubio_instance_flags', $this->flags, false ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public static function touch( $flag ) { |
| 144 |
static::set( $flag, time() ); |
| 145 |
} |
| 146 |
|
| 147 |
public static function getSetting( $path, $fallback = null ) { |
| 148 |
$settings = static::getSettings(); |
| 149 |
return Arr::get( $settings, $path, $fallback ); |
| 150 |
} |
| 151 |
|
| 152 |
public static function getSettings() { |
| 153 |
return static::get( '_settings', array() ); |
| 154 |
} |
| 155 |
|
| 156 |
public static function setSettings( $settings ) { |
| 157 |
static::set( '_settings', $settings ); |
| 158 |
} |
| 159 |
|
| 160 |
public static function setSetting( $path, $value ) { |
| 161 |
$settings = static::getSettings(); |
| 162 |
Arr::set( $settings, $path, $value ); |
| 163 |
static::setSettings( $settings ); |
| 164 |
} |
| 165 |
} |
| 166 |
|