PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 6.2.1
پارسی دیت – Parsi Date v6.2.1
6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / Settings / Settings.php
wp-parsidate / inc / Settings Last commit date
Settings.php 3 weeks ago
Settings.php
93 lines
1 <?php
2
3 namespace WPParsidate\Settings;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use WPParsidate\Helper\{Cache, Validating};
8
9 class Settings {
10 public static function addToArray( $key, $value, $optionsName = null, $reverse = false ): bool {
11 $optionValue = self::get( $key, [], $optionsName );
12 $optionValue = is_array( $optionValue ) ? $optionValue : [];
13 if ( $reverse ) {
14 $optionValue = array_values( array_reverse( $optionValue ) );
15 }
16 $optionValue[] = $value;
17
18 if ( $reverse ) {
19 $optionValue = array_values( array_reverse( $optionValue ) );
20 }
21
22 return self::save( $key, $optionValue, $optionsName );
23 }
24
25 public static function deleteFromArray( $key, $index, $optionsName = null ): bool {
26 $optionValue = self::get( $key, [], $optionsName );
27 $optionValue = is_array( $optionValue ) ? $optionValue : [];
28
29 if ( isset( $optionValue[ $index ] ) ) {
30 unset( $optionValue[ $index ] );
31 $optionValue = array_values( $optionValue );
32 } else {
33 return false;
34 }
35
36 return self::save( $key, $optionValue, $optionsName );
37 }
38
39 public static function save( $key, $value, $optionsName = null ): bool {
40 return self::saves( [ $key => $value ], $optionsName );
41 }
42
43 public static function saves( $options, $optionsName = null ): bool {
44 if ( ! is_array( $options ) || empty( $options ) ) {
45 return false;
46 }
47
48 $optionsName = is_string( $optionsName ) ? WP_PARSI_KEY . '_' . $optionsName : WP_PARSI_KEY;
49 $savedOptions = get_option( $optionsName, [] );
50 $savedOptions = is_array( $savedOptions ) ? $savedOptions : [];
51 $now = current_time( 'timestamp' );
52
53 $newOptions = wp_parse_args( $options, $savedOptions );
54 // Prevent from save option error
55 $newOptions['save_options_time_123456'] = ( Validating::isTimeStamp( $now ) ? $now : time() ) + random_int( 999, 9999 );
56
57 Cache::delete( 'options_' . $optionsName );
58
59 return update_option( $optionsName, $newOptions, false );
60 }
61
62 public static function get( ?string $key = null, $default = null, $optionsName = null, bool $useCache = true ) {
63 $optionsName = is_string( $optionsName ) ? WP_PARSI_KEY . '_' . $optionsName : WP_PARSI_KEY;
64 $options = Cache::get( 'options_' . $optionsName, false );
65
66 if ( ! $useCache || ! is_array( $options ) ) {
67 $options = get_option( $optionsName, [] );
68 $options = is_array( $options ) ? $options : [];
69 Cache::set( 'options_' . $optionsName, $options );
70 }
71
72 if ( $key !== null ) {
73 return apply_filters( 'wp_parsidate_get_setting', wp_unslash( $options[ $key ] ?? $default ), $key, $default, $options, $optionsName );
74 }
75
76 return apply_filters( 'wp_parsidate_get_settings', $options ?: $default, $optionsName );
77 }
78
79 public static function delete( string $key, $optionsName = null ): bool {
80 $optionsName = is_string( $optionsName ) ? WP_PARSI_KEY . '_' . $optionsName : WP_PARSI_KEY;
81 $savedOptions = get_option( $optionsName, [] );
82 $savedOptions = is_array( $savedOptions ) ? $savedOptions : [];
83
84 if ( isset( $savedOptions[ $key ] ) ) {
85 unset( $savedOptions[ $key ] );
86 }
87
88 Cache::delete( 'options_' . $optionsName );
89
90 return update_option( $optionsName, $savedOptions, false );
91 }
92 }
93