-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.wp_options.php
185 lines (154 loc) Β· 4.92 KB
/
class.wp_options.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
<?php
/**
* Create A Simple Theme Options Panel
* Source : http://www.wpexplorer.com/wordpress-theme-options/
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Start Class
if ( ! class_exists( 'WPEX_Theme_Options' ) ) {
class WPEX_Theme_Options {
/**
* Start things up
*
* @since 1.0.0
*/
public function __construct() {
// We only need to register the admin panel on the back-end
if ( is_admin() ) {
add_action( 'admin_menu', array( 'WPEX_Theme_Options', 'add_admin_menu' ) );
add_action( 'admin_init', array( 'WPEX_Theme_Options', 'register_settings' ) );
}
}
/**
* Returns all theme options
*
* @since 1.0.0
*/
public static function get_theme_options() {
return get_option( 'theme_options' );
}
/**
* Returns single theme option
*
* @since 1.0.0
*/
public static function get_theme_option( $id ) {
$options = self::get_theme_options();
if ( isset( $options[$id] ) ) {
return $options[$id];
}
}
/**
* Add sub menu page
*
* @since 1.0.0
*/
public static function add_admin_menu() {
add_menu_page(
esc_html__( 'Theme Settings', get_bloginfo('name') ),
esc_html__( 'Theme Settings', get_bloginfo('name') ),
'manage_options',
'theme-settings',
array( 'WPEX_Theme_Options', 'create_admin_page' )
);
}
/**
* Register a setting and its sanitization callback.
*
* We are only registering 1 setting so we can store all options in a single option as
* an array. You could, however, register a new setting for each option
*
* @since 1.0.0
*/
public static function register_settings() {
register_setting( 'theme_options', 'theme_options', array( 'WPEX_Theme_Options', 'sanitize' ) );
}
/**
* Sanitization callback
*
* @since 1.0.0
*/
public static function sanitize( $options ) {
// If we have options lets sanitize them
if ( $options ) {
// Checkbox
if ( ! empty( $options['active_pwa'] ) ) {
$options['active_pwa'] = 'on';
} else {
unset( $options['active_pwa'] ); // Remove from options if not checked
}
if ( ! empty( $options['auth_menu'] ) ) {
$options['auth_menu'] = 'on';
} else {
unset( $options['auth_menu'] ); // Remove from options if not checked
}
// Input
//if ( ! empty( $options['input_example'] ) ) {
// $options['input_example'] = sanitize_text_field( $options['input_example'] );
//} else {
// unset( $options['input_example'] ); // Remove from options if empty
//}
// Select
if ( ! empty( $options['styleRequest'] ) ) {
$options['styleRequest'] = sanitize_text_field( $options['styleRequest'] );
}
}
// Return sanitized options
return $options;
}
/**
* Settings page output
*
* @since 1.0.0
*/
public static function create_admin_page() { ?>
<div class="wrap">
<h1><?php esc_html_e( 'Theme Options', get_bloginfo('name') ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'theme_options' ); ?>
<table class="form-table wpex-custom-admin-login-table">
<?php // Checkbox example ?>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'PWA OPTIONS', get_bloginfo('name') ); ?></th>
<td>
<?php $value = self::get_theme_option( 'active_pwa' ); ?>
<input type="checkbox" name="theme_options[active_pwa]" <?php checked( $value, 'on' ); ?>> <?php esc_html_e( 'Activated PWA', get_bloginfo('name') ); ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Enable Register\Login Menu ?', get_bloginfo('name') ); ?></th>
<td>
<?php $value = self::get_theme_option( 'auth_menu' ); ?>
<input type="checkbox" name="theme_options[auth_menu]" <?php checked( $value, 'on' ); ?>> <?php esc_html_e( 'Enable Auth Menu', get_bloginfo('name') ); ?>
</td>
</tr>
<tr valign="top" class="wpex-custom-admin-screen-background-section">
<th scope="row"><?php esc_html_e( 'Static style request mode', get_bloginfo('name') ); ?></th>
<td>
<?php $value = self::get_theme_option( 'styleRequest' ); ?>
<select name="theme_options[styleRequest]">
<?php
$options = array(
'' => esc_html(__( 'Select', get_bloginfo('name') ) ),
'prefetch' => esc_html__( 'Prefetch', get_bloginfo('name') ),
'preload' => esc_html__( 'Preload', get_bloginfo('name') ),
'dsn-prefetch' => esc_html__( 'dns-Prefetch', get_bloginfo('name') ),
);
foreach ( $options as $id => $label ) { ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $value, $id, true ); ?>>
<?php echo strip_tags( $label ); ?>
</option>
<?php } ?>
</select>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div><!-- .wrap -->
<?php }
}
}