forked from WordPress/two-factor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-core-style-plugin-admin.php
204 lines (183 loc) · 4.68 KB
/
class-core-style-plugin-admin.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Setup Settings/ Admin of the plugin.
*/
class Core_Style_Plugin_Admin {
public function add_hooks() {
add_action( 'admin_menu', array( $this, 'add_page' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
}
/**
* Registers the plugin's setting.
*
* @since 0.0.1
*/
public static function register_settings() {
$args = array(
'type' => 'array',
'sanitize_callback' => array( Core_Style_Plugin::class, 'sanitize_settings' ),
'default' => Core_Style_Plugin::settings_default(),
);
register_setting(
CORE_STYLE_PLUGIN_SCREEN,
CORE_STYLE_PLUGIN_SETTING,
$args
);
}
/**
* Adds the settings page to the Settings menu.
*
* @since 0.0.1
*/
public function add_page() {
// Register settings and sections
$this->register_fields();
// Add the page
$hook_suffix = add_options_page(
__( 'Core Style Plugin', 'core-style-plugin' ),
__( 'Settings', 'core-style-plugin' ),
'manage_options',
CORE_STYLE_PLUGIN_SCREEN,
array( $this, 'render_page' )
);
// This adds a link in the plugins list table
add_action(
'plugin_action_links_' . plugin_basename( CORE_STYLE_PLUGIN_MAIN_FILE ),
array(
$this,
'plugin_action_links_add_settings',
)
);
return $hook_suffix;
}
/**
* All settings sections and fields.
*/
protected function sections() {
return array(
// Section id
'api' => array(
// Section title
'name' => __( 'API', 'core-style-plugin' ),
// Settings fields in this section
'settings' => array(
// Setting id
'api_key' => array(
// Setting label
'label' => __( 'Api Key', 'core-style-plugin' ),
// Setting type
// text|email|url
'type' => 'text',
),
),
),
);
}
/**
* Initializes settings sections and fields for the settings page.
*
* @since 0.01
*/
public function register_fields() {
$sections = $this->sections();
// Get saved settings
$saved = Core_Style_Plugin::get_settings();
// Register sections
foreach ( $sections as $section_slug => $section_data ) {
add_settings_section(
$section_slug,
$section_data['name'],
null,
CORE_STYLE_PLUGIN_SCREEN
);
// Register fields in this section
foreach ( $section_data['settings'] as $id => $setting ) {
$value = isset( $saved[ $id ] ) ? $saved[ $id ] : null;
add_settings_field(
$id,
$setting['label'],
function()use ( $setting, $id, $value ) {
$this->render_field(
$setting['label'],
$id,
$value,
isset( $setting['required'] ) ? $setting['required'] : false,
);
},
CORE_STYLE_PLUGIN_SCREEN,
$section_slug
);
}
}
}
/**
* Renders a field.
*
* @param string $label
* @param string $name Field identifier, used to make name attr
* @param string|null $value Optional. Field value. Default null.
* @param bool $required Optional. Whether the field is required
* @param string $type text|email|url @todo add more types
*/
public function render_field( $label, $name, $value = null, $required = false, $type = 'text' ) {
$name = CORE_STYLE_PLUGIN_SETTING . '[' . $name . ']';
$label = sprintf(
"<label for='%s'>%s</label>",
esc_attr( $name ),
esc_html( $label ),
);
$field = sprintf(
'<input type="%s" name="%s" %s />',
esc_attr( $type ),
esc_attr( $name ),
sprintf(
' %s %s',
$value ? 'value="' . esc_attr( $value ) . '"' : '',
$required ? 'required' : ''
)
);
$wrapper = sprintf(
'<div class="field-wrapper">%s %s</div>',
$label,
$field
);
echo $wrapper;
}
/**
* Renders the settings page.
*
* @since 0.0.1
*/
public function render_page() {
?>
<div class="core-style-plugin-wrap">
<h1>
<?php esc_html_e( 'Core Style Plugin', 'core-style-plugin' ); ?>
</h1>
<form action="options.php" method="post" novalidate="novalidate">
<?php settings_fields( CORE_STYLE_PLUGIN_SCREEN ); ?>
<?php do_settings_sections( CORE_STYLE_PLUGIN_SCREEN ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Adds a link to the setting page to the plugin's entry in the plugins list table.
*
* @since 1.0.0
*
* @param array $links List of plugin action links HTML.
* @return array Modified list of plugin action links HTML.
*/
function plugin_action_links_add_settings( $links ) {
// Add link as the first plugin action link.
$settings_link = sprintf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( 'page', CORE_STYLE_PLUGIN_SCREEN, admin_url( 'options-general.php' ) ) ),
esc_html__( 'Settings', 'core-style-plugin' )
);
array_unshift( $links, $settings_link );
return $links;
}
}