class-options-data.php
2.56 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace WP_Rocket\Admin;
/**
* Manages the data inside an option.
*
* @since 3.0
* @author Remy Perona
*/
class Options_Data {
/**
* Option data
*
* @var Array Array of data inside the option
*/
private $options;
/**
* Constructor
*
* @param Array $options Array of data coming from an option.
*/
public function __construct( $options ) {
$this->options = $options;
}
/**
* Checks if the provided key exists in the option data array.
*
* @since 3.0
* @author Remy Perona
*
* @param string $key key name.
* @return boolean true if it exists, false otherwise
*/
public function has( $key ) {
return isset( $this->options[ $key ] );
}
/**
* Gets the value associated with a specific key.
*
* @since 3.0
* @author Remy Perona
*
* @param string $key key name.
* @param mixed $default default value to return if key doesn't exist.
* @return mixed
*/
public function get( $key, $default = '' ) {
/**
* Pre-filter any WP Rocket option before read
*
* @since 2.5
*
* @param mixed $default The default value.
*/
$value = apply_filters( 'pre_get_rocket_option_' . $key, null, $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( null !== $value ) {
return $value;
}
if ( 'consumer_key' === $key && rocket_has_constant( 'WP_ROCKET_KEY' ) ) {
return WP_ROCKET_KEY;
} elseif ( 'consumer_email' === $key && rocket_has_constant( 'WP_ROCKET_EMAIL' ) ) {
return WP_ROCKET_EMAIL;
}
if ( ! $this->has( $key ) ) {
return $default;
}
/**
* Filter any WP Rocket option after read
*
* @since 2.5
*
* @param mixed $default The default value.
*/
return apply_filters( 'get_rocket_option_' . $key, $this->options[ $key ], $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}
/**
* Sets the value associated with a specific key.
*
* @since 3.0
* @author Remy Perona
*
* @param string $key key name.
* @param mixed $value to set.
* @return void
*/
public function set( $key, $value ) {
$this->options[ $key ] = $value;
}
/**
* Sets multiple values.
*
* @since 3.0
* @author Remy Perona
*
* @param array $options An array of key/value pairs to set.
* @return void
*/
public function set_values( $options ) {
foreach ( $options as $key => $value ) {
$this->set( $key, $value );
}
}
/**
* Gets the option array.
*
* @since 3.0
* @author Remy Perona
*
* @return array
*/
public function get_options() {
return $this->options;
}
}