1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Main Cassava plugin file. |
4
|
|
|
* |
5
|
|
|
* @version 1.2.0 |
6
|
|
|
* @since 1.2.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cassava; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Plugin options class. |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
class Options { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Plugin options key. |
20
|
|
|
*/ |
21
|
|
|
const KEY = 'wp_cas_server'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Default plugin options. |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $defaults = array( |
28
|
|
|
/** |
29
|
|
|
* CAS server endpoint path fragment (e.g. `<scheme>://<host>/wp-cas/login`). |
30
|
|
|
*/ |
31
|
|
|
'endpoint_slug' => Plugin::ENDPOINT_SLUG, |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Service ticket expiration, in seconds [0..300]. |
35
|
|
|
*/ |
36
|
|
|
'expiration' => 30, |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* `allow_ticket_reuse` exists as a workaround for potential issues with |
40
|
|
|
* WordPress's Transients API. |
41
|
|
|
*/ |
42
|
|
|
'allow_ticket_reuse' => false, |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @todo Allow requests from these service URIs only. |
46
|
|
|
*/ |
47
|
|
|
'allowed_services' => array(), |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* User attributes to return on a successful `/serviceValidate` response. |
51
|
|
|
*/ |
52
|
|
|
'attributes' => array(), |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets all options. |
57
|
|
|
* |
58
|
|
|
* @return array All plugin options. |
59
|
|
|
*/ |
60
|
|
|
public static function getAll() { |
61
|
|
|
return \get_option( static::KEY ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get plugin option by key. |
66
|
|
|
* |
67
|
|
|
* @param string $key Plugin option key to return. |
68
|
|
|
* @param mixed $default Option value to return if `$key` is not found. |
69
|
|
|
* |
70
|
|
|
* @return mixed Plugin option value. |
71
|
|
|
* |
72
|
|
|
* @uses \get_option() |
73
|
|
|
*/ |
74
|
|
|
public static function get( $key = '', $default = null ) { |
75
|
|
|
$options = \get_option( static::KEY ); |
76
|
|
|
return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set plugin option by key. |
81
|
|
|
* |
82
|
|
|
* @param string $key Plugin option key to set. |
83
|
|
|
* @param mixed $value Plugin option value to set. |
84
|
|
|
* |
85
|
|
|
* @uses \get_option() |
86
|
|
|
* @uses \update_option() |
87
|
|
|
*/ |
88
|
|
|
public static function set( $key, $value ) { |
89
|
|
|
if ( ! isset( $key ) ) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$options = \get_option( static::KEY ); |
94
|
|
|
|
95
|
|
|
if ( ! isset( $value ) ) { |
96
|
|
|
unset( $options[ $key ] ); |
97
|
|
|
} else { |
98
|
|
|
$options[ $key ] = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
\update_option( static::KEY, $options ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the default plugin options in the database. |
106
|
|
|
* |
107
|
|
|
* @uses \get_option() |
108
|
|
|
* @uses \update_option() |
109
|
|
|
*/ |
110
|
|
|
public static function setDefaults() { |
111
|
|
|
$options = \get_option( static::KEY, self::$defaults ); |
112
|
|
|
\update_option( static::KEY, $options ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|