class-affiliates-options.php
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* class-affiliates-options.php
*
* Copyright (c) 2010, 2011 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates
* @since affiliates 1.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Affiliates options
*/
class Affiliates_Options {
/**
* Returns the value of a setting.
*
* @param string $option the option id
* @param mixed $default default value to retrieve if option is not set
* @return option value, $default if set or null
*/
function get_option( $option, $default = null ) {
$current_user = wp_get_current_user();
$value = null;
if ( !empty( $current_user ) ) {
$options = get_option( 'affiliates_plugin' );
if ( is_array( $options ) && isset( $options[$current_user->ID] ) && is_array( $options[$current_user->ID] ) ) {
$value = isset( $options[$current_user->ID][$option] ) ? $options[$current_user->ID][$option] : null;
} else {
$value = null;
}
}
if ( $value === null ) {
$value = $default;
}
return $value;
}
/**
* Updates a setting.
*
* @param string $option the option's id
* @param mixed $new_value the new value
*/
function update_option( $option, $new_value ) {
$current_user = wp_get_current_user();
if ( !empty( $current_user ) ) {
$options = get_option( 'affiliates_plugin' );
if ( !is_array( $options ) ) {
$options = array( $current_user->ID => array() );
}
$options[$current_user->ID][$option] = $new_value;
update_option( 'affiliates_plugin', $options );
}
}
/**
* Deletes a setting.
*
* @param string $option the option's id
*/
function delete_option( $option ) {
$current_user = wp_get_current_user();
if ( !empty( $current_user ) ) {
$options = get_option( 'affiliates_plugin' );
if ( isset( $options[$current_user->ID][$option] ) ) {
unset( $options[$current_user->ID][$option] );
update_option( 'affiliates_plugin', $options );
}
}
}
/**
* Deletes all settings.
*/
function flush_options() {
delete_option( 'affiliates_plugin' );
}
}