PluginProbe
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads / trunk
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads vtrunk
1.2.1 1.2.0 1.1.7 trunk 1.0.0 1.1.0 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6
siteleads / lib / Core / Flags.php

Flags.php in Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads trunk, at lib/Core/Flags.php

182 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SiteLeads\Core;
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( '__siteleads_instance_flags', array() );
16 add_action( 'shutdown', array( $this, 'save' ) );
17
18 $default_settings = require_once SITELEADS_ROOT_DIR . '/defaults/flags.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 public 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 $flags_defaults = apply_filters( 'siteleads/instance-flags-default', array() );
63
64 if ( isset( $flags_defaults[ $flag ] ) ) {
65 return $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 static
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 public 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 public function getFlag( $flag, $fallback = null ) {
133 return $this->withFlags( 'get', $flag, $fallback );
134 }
135
136 public function save() {
137 if ( $this->is_dirty_value ) {
138 update_option( '__siteleads_instance_flags', $this->flags, false );
139 $this->is_dirty_value = 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( $raw = false ) {
153 $settings = static::get( '_settings', array() );
154
155 if ( $raw ) {
156 return $settings;
157 }
158
159 return apply_filters( 'siteleads/flags/settings', $settings );
160 }
161
162 public static function setSettings( $settings ) {
163 static::set( '_settings', $settings );
164 }
165
166 public static function setSetting( $path, $value ) {
167 $settings = static::getSettings( true );
168 Arr::set( $settings, $path, $value );
169 static::setSettings( $settings );
170 }
171
172 public static function getSiteUUID() {
173 $uuid = static::getSetting( 'site_uuid', null );
174 if ( ! $uuid ) {
175 $uuid = wp_generate_uuid4();
176 static::setSetting( 'site_uuid', $uuid );
177 }
178
179 return $uuid;
180 }
181 }
182