-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-mps-options.php
More file actions
129 lines (108 loc) · 2.96 KB
/
class-mps-options.php
File metadata and controls
129 lines (108 loc) · 2.96 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
125
126
127
128
129
<?php
class MPS_Options {
/**
* Returns an array of option names for a given type.
*
* @return array
*/
public static function get_option_names() {
return [
'activated',
'active_modules',
];
}
/**
* Checks if the option name valid.
*
* @param string $name The name of the option
*
* @return bool If the option name is valid.
*/
public static function is_valid( $name ) {
if ( in_array( $name, self::get_option_names() ) ) {
return true;
}
return false;
}
/**
* Updates the single given option.
*
* @param string $name Option name.
* @param mixed $value Option value.
* @param string $autoload
*
* @return bool If the option successfully updated.
*/
public static function update_option( $name, $value, $autoload = null ) {
if ( self::is_valid( $name ) ) {
return update_option( "mps_$name", $value, $autoload );
}
trigger_error( sprintf( 'Invalid option name: %s', $name ), E_USER_WARNING );
return false;
}
/**
* Returns the requested option.
*
* @param string $name Option name.
*
* @return mixed
*/
public static function get_option( $name ) {
if ( self::is_valid( $name ) ) {
return get_option( "mps_$name" );
}
trigger_error( sprintf( 'Invalid option name: %s', $name ), E_USER_WARNING );
return false;
}
/**
* Returns the requested option, and ensures it's toggleable from settings.
*
* @param string $name Option name
* @param mixed $default (optional)
*
* @return mixed
*/
public static function get_option_and_ensure_autoload( $name, $default ) {
$value = get_option( $name );
if ( $value == false && $default !== false ) {
update_option( $name, $default );
$value = $default;
}
return $value;
}
/**
* Delete all known options
*
* @return void
*/
static function delete_all_known_options() {
foreach ( (array) self::get_option_names() as $option_name ) {
delete_option( $option_name );
}
}
/**
* Prints out a setting's section and settings added to a particular settings page
*
* @global $wp_settings_sections Storage array of all settings sections added to admin pages.
* @global $wp_settings_fields Storage array of settings fields and info about their pages/sections.
*
* @param string $page The slug name of the page whose settings sections you want to output.
* @param string $section Slug title of the settings section whose fields you want to show.
*/
static function do_settings_section( $page, $section ) {
global $wp_settings_sections, $wp_settings_fields;
if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
return;
}
$section = (array) $wp_settings_sections[$page][ $section ];
if ( $section['title'] ) {
echo "<h2>{$section['title']}</h2>\n";
}
if ( $section['callback'] ) {
call_user_func( $section['callback'], $section );
}
echo '<table class="form-table">';
do_settings_fields( $page, $section['id'] );
echo '</table>';
}
}