|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for managing plugin setting cache |
|
4
|
|
|
* Note: only use for internal purpose. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Give |
|
7
|
|
|
* @subpackage Classes/Give_Cache_Setting |
|
8
|
|
|
* @copyright Copyright (c) 2018, GiveWP |
|
9
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
10
|
|
|
* @since 2.4.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
// Exit if accessed directly. |
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
class Give_Cache_Setting { |
|
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 2.4.0 |
|
23
|
|
|
* @access private |
|
24
|
|
|
* @var Give_Cache_Setting |
|
25
|
|
|
*/ |
|
26
|
|
|
static private $instance; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 2.4.0 |
|
32
|
|
|
* @access private |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
static private $settings; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List of core options |
|
39
|
|
|
* @since 2.4.0 |
|
40
|
|
|
* @access private |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
static private $options = array( |
|
44
|
|
|
'give_settings', |
|
45
|
|
|
'give_version', |
|
46
|
|
|
'give_completed_upgrades', |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Singleton pattern. |
|
51
|
|
|
* |
|
52
|
|
|
* @since 2.4.0 |
|
53
|
|
|
* @access private |
|
54
|
|
|
*/ |
|
55
|
|
|
private function __construct() { |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get instance. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 2.4.0 |
|
63
|
|
|
* @access public |
|
64
|
|
|
* @return Give_Cache_Setting |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public static function get_instance() { |
|
|
|
|
|
|
67
|
|
|
if ( null === static::$instance ) { |
|
68
|
|
|
self::$instance = new static(); |
|
69
|
|
|
|
|
70
|
|
|
self::$instance->setup(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return self::$instance; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Setup |
|
78
|
|
|
* |
|
79
|
|
|
* @since 2.4.0 |
|
80
|
|
|
* @access private |
|
81
|
|
|
*/ |
|
82
|
|
|
private function setup() { |
|
83
|
|
|
$this->load_plugin_settings(); |
|
84
|
|
|
|
|
85
|
|
|
add_action( 'added_option', array( $this, '__reload_plugin_settings' ) ); |
|
|
|
|
|
|
86
|
|
|
add_action( 'updated_option', array( $this, '__reload_plugin_settings' ) ); |
|
|
|
|
|
|
87
|
|
|
add_action( 'deleted_option', array( $this, '__reload_plugin_settings' ) ); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Load plugin settings |
|
92
|
|
|
* |
|
93
|
|
|
* @since 2.4.0 |
|
94
|
|
|
* @access private |
|
95
|
|
|
*/ |
|
96
|
|
|
private function load_plugin_settings() { |
|
97
|
|
|
global $wpdb; |
|
98
|
|
|
|
|
99
|
|
|
$cache = wp_cache_get( 'giveAllOptions','options' ); |
|
100
|
|
|
|
|
101
|
|
|
// Load options from cache. |
|
102
|
|
|
if( false !== $cache ) { |
|
|
|
|
|
|
103
|
|
|
self::$settings = $cache; |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$options = '\'' . implode( '\',\'', self::$options ) . '\''; |
|
108
|
|
|
|
|
109
|
|
|
$tmp = array(); |
|
110
|
|
|
$sql = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name IN ({$options}) "; |
|
111
|
|
|
$results = $wpdb->get_results( $sql ); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
if ( ! empty( $results ) ) { |
|
114
|
|
|
|
|
115
|
|
|
/* @var stdClass $result */ |
|
116
|
|
|
foreach ( $results as $result ) { |
|
117
|
|
|
$tmp[ $result->option_name ] = maybe_unserialize( $result->option_value ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
self::$settings = $tmp; |
|
121
|
|
|
wp_cache_set( 'giveAllOptions', $tmp, 'options' ); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Reload option when add, update or delete |
|
127
|
|
|
* Note: only for internal logic |
|
128
|
|
|
* |
|
129
|
|
|
* @since 2.4.0 |
|
130
|
|
|
* |
|
131
|
|
|
* @param $option_name |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __reload_plugin_settings( $option_name ) { |
|
|
|
|
|
|
134
|
|
|
// Bailout. |
|
135
|
|
|
if ( ! in_array( $option_name, self::$options ) ) { |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
wp_cache_delete( 'giveAllOptions', 'options' ); |
|
140
|
|
|
$this->$this->load_plugin_settings(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get option |
|
146
|
|
|
* |
|
147
|
|
|
* @since 2.4.0 |
|
148
|
|
|
* @access public |
|
149
|
|
|
* |
|
150
|
|
|
* @param $option_name |
|
151
|
|
|
* @param bool $default |
|
152
|
|
|
* |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function get_option( $option_name, $default = false ) { |
|
156
|
|
|
if ( in_array( $option_name, self::$options ) ) { |
|
157
|
|
|
$value = ! empty( self::$settings[ $option_name ] ) |
|
158
|
|
|
? self::$settings[ $option_name ] |
|
159
|
|
|
: $default; |
|
160
|
|
|
|
|
161
|
|
|
} else { |
|
162
|
|
|
|
|
163
|
|
|
$value = self::get_option( $option_name, $default ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $value; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get option |
|
171
|
|
|
* |
|
172
|
|
|
* @since 2.4.0 |
|
173
|
|
|
* @access public |
|
174
|
|
|
* |
|
175
|
|
|
* @param $setting_name |
|
176
|
|
|
* @param bool $default |
|
177
|
|
|
* |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
View Code Duplication |
public static function get_give_option( $setting_name, $default = false ) { |
|
|
|
|
|
|
181
|
|
|
$give_settings = self::$settings['give_settings']; |
|
182
|
|
|
|
|
183
|
|
|
$value = ! empty( $give_settings[ $setting_name ] ) |
|
184
|
|
|
? self::$settings[ $setting_name ] |
|
185
|
|
|
: $default; |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Filter the option |
|
189
|
|
|
* |
|
190
|
|
|
* @since 2.4.0 |
|
191
|
|
|
*/ |
|
192
|
|
|
$value = apply_filters( 'give_get_option', $value, $setting_name, $default ); |
|
193
|
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* filter the specific option |
|
197
|
|
|
* |
|
198
|
|
|
* @since 2.4.0 |
|
199
|
|
|
*/ |
|
200
|
|
|
$value = apply_filters( "give_get_option_{$setting_name}", $value, $setting_name, $default ); |
|
201
|
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
return $value; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get plugin settings |
|
208
|
|
|
* |
|
209
|
|
|
* @since 2.4.0 |
|
210
|
|
|
* @access public |
|
211
|
|
|
*/ |
|
212
|
|
|
public static function get_settings() { |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Filter the plugin setting |
|
216
|
|
|
*/ |
|
217
|
|
|
return (array) apply_filters( 'give_get_settings', self::$settings['give_settings'] ); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
Give_Cache_Setting::get_instance(); |
|
222
|
|
|
|