PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / class-lp-settings.php

class-lp-settings.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.0, at inc/class-lp-settings.php

413 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Settings
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 1.0.1
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class LP_Settings {
15 /**
16 * @var array
17 */
18 protected $_options = array();
19
20 /**
21 * @var string
22 */
23 protected $_prefix = '';
24
25 /**
26 * @var bool
27 */
28 protected $_load_data = false;
29
30 /**
31 * @var bool
32 */
33 protected static $_instance = null;
34
35 /**
36 * Constructor.
37 *
38 * @param array|mixed $data
39 * @param string $prefix
40 */
41 protected function __construct( $data = false, $prefix = 'learn_press_' ) {
42
43 $this->_prefix = $prefix;
44
45 if ( false === $data ) {
46 $this->_load_data = true;
47 $this->_load_options();
48 } else {
49 settype( $data, 'array' );
50 $this->_options = $data;
51 }
52 }
53
54 /**
55 * @param string $group
56 * @param string $prefix
57 *
58 * @return LP_Settings
59 */
60 public function get_group( $group, $prefix = '' ) {
61 $options = ! empty( $this->_options[ $this->_prefix . $group ] ) ? $this->get( $this->_prefix . $group ) : array();
62
63 return new LP_Settings( $options, $prefix );
64 }
65
66 /**
67 * Load options from database.
68 *
69 * @param bool $force
70 */
71 protected function _load_options( $force = false ) {
72 global $wpdb;
73 $query = $wpdb->prepare(
74 "
75 SELECT option_name, option_value
76 FROM {$wpdb->options}
77 WHERE option_name LIKE %s",
78 $wpdb->esc_like( $this->_prefix ) . '%'
79 );
80
81 $options = $wpdb->get_results( $query );
82
83 if ( $options ) {
84 foreach ( $options as $option ) {
85 $this->_options[ $option->option_name ] = LP_Helper::maybe_unserialize( $option->option_value );
86 }
87 }
88 }
89
90 /**
91 * Set new value for a key
92 *
93 * @param $name
94 * @param $value
95 */
96 public function set( $name, $value ) {
97 if ( $this->_prefix && strpos( $name, $this->_prefix ) === false ) {
98 $name = $this->_prefix . $name;
99 }
100 $this->_set_option( $this->_options, $name, $value );
101 }
102
103 private function _set_option( &$obj, $var, $value ) {
104 $var = (array) explode( '.', $var );
105 $current_var = array_shift( $var );
106 if ( is_object( $obj ) ) {
107 if ( isset( $obj->{$current_var} ) ) {
108 $obj->{$current_var} = LP_Helper::maybe_unserialize( $obj->{$current_var} );
109 if ( count( $var ) ) {
110 $this->_set_option( $obj->{$current_var}, join( '.', $var ), $value );
111 } else {
112 $obj->{$current_var} = $value;
113 }
114 } else {
115 $obj->{$current_var} = $value;
116 }
117 } else {
118 if ( isset( $obj[ $current_var ] ) ) {
119 $obj[ $current_var ] = LP_Helper::maybe_unserialize( $obj[ $current_var ] );
120 if ( count( $var ) ) {
121 $this->_set_option( $obj[ $current_var ], join( '.', $var ), $value );
122 } else {
123 $obj[ $current_var ] = $value;
124 }
125 } else {
126 $obj[ $current_var ] = $value;
127 }
128 }
129 }
130
131 /**
132 * Get option recurse separated by DOT
133 *
134 * @param string $var
135 * @param mixed $default
136 *
137 * @return mixed
138 */
139 public function get( string $var = '', $default = null ) {
140 if ( empty( $var ) ) {
141 return $this->_options;
142 }
143
144 if ( $this->_prefix && strpos( $var, $this->_prefix ) === false ) {
145 $var = $this->_prefix . $var;
146 }
147
148 $return = $this->_get_option( $this->_options, $var, $default );
149
150 if ( $return == '' || is_null( $return ) ) {
151 $return = $default;
152 }
153
154 return $return;
155 }
156
157 /**
158 * @param $obj
159 * @param $var
160 * @param null $default
161 *
162 * @return null
163 */
164 public function _get_option( $obj, $var, $default = null ) {
165 $var = (array) explode( '.', $var );
166 $current_var = array_shift( $var );
167 if ( is_object( $obj ) ) {
168 if ( isset( $obj->{$current_var} ) ) {
169 $obj->{$current_var} = LP_Helper::maybe_unserialize( $obj->{$current_var} );
170 if ( count( $var ) ) {
171 return $this->_get_option( $obj->{$current_var}, join( '.', $var ), $default );
172 } else {
173 return $obj->{$current_var};
174 }
175 } else {
176 return $default;
177 }
178 } else {
179 if ( isset( $obj[ $current_var ] ) ) {
180 $obj[ $current_var ] = LP_Helper::maybe_unserialize( $obj[ $current_var ] );
181 if ( count( $var ) ) {
182 return $this->_get_option( $obj[ $current_var ], join( '.', $var ), $default );
183 } else {
184 return $obj[ $current_var ];
185 }
186 } else {
187 return $default;
188 }
189 }
190 }
191
192 public function update( $key, $value = '' ) {
193 if ( func_num_args() == 1 ) {
194 $value = $this->get( $key );
195 }
196 update_option( $this->_prefix . $key, $value );
197 // $this->refresh();
198 }
199
200 public function refresh() {
201 if ( $this->_load_data ) {
202 // $this->_load_options( true );
203 }
204
205 return $this;
206 }
207
208 /**
209 * Update option with default prefix is learn_press_
210 *
211 * @param string $name
212 * @param mixed $value
213 * @param string $prefix
214 */
215 public static function update_option( $name, $value, $prefix = 'learn_press_' ) {
216 update_option( "{$prefix}{$name}", $value );
217 }
218
219 /**
220 * Get option with default prefix is learn_press_
221 *
222 * @param string $name
223 * @param mixed $default
224 *
225 * @return mixed
226 * @since 3.2.8
227 * @editor tungnx
228 */
229 public static function get_option( string $name = '', $default = false ) {
230 return get_option( "learn_press_{$name}", $default );
231 }
232
233 public function get_int( $key ) {
234 $value = $this->get( $key );
235
236 return intval( $value );
237 }
238
239 /**
240 * @return bool|LP_Settings
241 */
242 public static function instance() {
243 if ( is_null( self::$_instance ) ) {
244 self::$_instance = new self();
245 }
246
247 return self::$_instance;
248 }
249
250 /**
251 * Load all 'no' options from other plugins for caching purpose.
252 *
253 * @since 3.0.0
254 * @deprecated 4.0.0
255 * @editor tungnx
256 * @reason not use
257 */
258 /*
259 public static function load_site_options() {
260 static $loaded = false;
261
262 if ( $loaded ) {
263 return;
264 }
265
266 $options = array(
267 'pmpro_updates',
268 'pmpro_stripe_billingaddress',
269 'pmpro_only_filter_pmpro_emails',
270 'pmpro_email_member_notification',
271 'pmpro_hideads',
272 'pmpro_hideadslevels',
273 '_bbp_enable_group_forums',
274 '_bbp_theme_package_id',
275 '_bbp_root_slug',
276 '_bbp_include_root',
277 '_bbp_forum_slug',
278 '_bbp_topic_slug',
279 '_bbp_show_on_root',
280 '_bbp_topic_archive_slug',
281 '_bbp_reply_slug',
282 '_bbp_topic_tag_slug',
283 '_bbp_allow_topic_tags',
284 '_bbp_use_autoembed',
285 '_bbp_user_slug',
286 '_bbp_view_slug',
287 '_bbp_search_slug',
288 '_bbp_reply_archive_slug',
289 '_bbp_user_favs_slug',
290 '_bbp_user_subs_slug',
291 'pmpro_nuclear_HTTPS',
292 'pmpro_gateway',
293 'pmpro_recaptcha',
294 'pmpro_use_ssl',
295 '_bbp_enable_favorites',
296 '_bbp_enable_subscriptions',
297 '_bbp_allow_search',
298 '_bbp_use_wp_editor',
299 'pmpro_hide_footer_link',
300 'learn-press-flush-rewrite-rules',
301 '_lp_tabs_data',
302 'learn_press_permalinks',
303 );
304 global $wpdb;
305
306 $format = array_fill( 0, sizeof( $options ), '%s' );
307 $q = $wpdb->prepare( "
308 SELECT option_name, option_value
309 FROM $wpdb->options
310 WHERE 1
311 AND option_name IN(" . join( ',', $format ) . ")
312 ", $options );
313
314 $alloptions_db = $wpdb->get_results( $q, OBJECT_K );
315 $notoptions = wp_cache_get( 'notoptions', 'options' );
316
317 foreach ( $options as $o_name ) {
318 if ( ! empty( $alloptions_db[ $o_name ] ) ) {
319 $o_value = LP_Helper::maybe_unserialize( $alloptions_db[ $o_name ]->option_value );
320 wp_cache_set( $o_name, $o_value, 'options' );
321 } else {
322 if ( ! is_array( $notoptions ) ) {
323 $notoptions = array();
324 }
325 $notoptions[ $o_name ] = '';
326 }
327 }
328
329 wp_cache_set( 'notoptions', $notoptions, 'options' );
330 $loaded = true;
331 }*/
332
333 /**
334 * Get settings endpoints for checkout page.
335 *
336 * @return array
337 * @since 3.0.0
338 */
339 public function get_checkout_endpoints() {
340 $endpoints = LP_Object_Cache::get( 'checkout', 'learn-press-endpoints' );
341
342 if ( false === $endpoints ) {
343 $defaults = array(
344 'lp-order-received' => 'lp-order-received',
345 );
346
347 $endpoints = array();
348 $settings = LP_Settings::instance()->get( 'checkout_endpoints' );
349
350 if ( $settings ) {
351 foreach ( $settings as $k => $v ) {
352 $k = preg_replace( '!_!', '-', $k );
353 $endpoints[ $k ] = $v;
354 }
355 }
356
357 foreach ( $defaults as $k => $v ) {
358 if ( empty( $endpoints[ $k ] ) ) {
359 $endpoints[ $k ] = $v;
360 }
361 }
362
363 LP_Object_Cache::set( 'checkout', $endpoints, 'learn-press-endpoints' );
364 }
365
366 return apply_filters( 'learn-press/endpoints/checkout', $endpoints );
367 }
368
369 /**
370 * Get settings endpoints for profile page.
371 *
372 * @return array
373 * @since 3.0.0
374 */
375 public function get_profile_endpoints() {
376 $endpoints = LP_Object_Cache::get( 'profile', 'learn-press-endpoints' );
377
378 if ( false === $endpoints ) {
379 $defaults = array();
380 $endpoints = array();
381
382 $settings = LP_Settings::instance()->get( 'profile_endpoints' );
383 if ( $settings ) {
384 foreach ( $settings as $k => $v ) {
385 $k = preg_replace( '!_!', '-', $k );
386 $endpoints[ $k ] = $v;
387 }
388 }
389
390 foreach ( $defaults as $k => $v ) {
391 if ( empty( $endpoints[ $k ] ) ) {
392 $endpoints[ $k ] = $v;
393 }
394 }
395
396 LP_Object_Cache::set( 'profile', $endpoints, 'learn-press-endpoints' );
397 }
398
399 return apply_filters( 'learn-press/endpoints/profile', $endpoints );
400 }
401
402 /**
403 * Check setting enable option "Auto start"
404 *
405 * @return bool
406 */
407 public static function is_auto_start_course(): bool {
408 return 'yes' === self::get_option( 'auto_enroll', 'yes' );
409 }
410 }
411
412 LP_Settings::instance();
413