forked from ConnectThink/WP-SCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.php
300 lines (273 loc) · 10.2 KB
/
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
class Wp_Scss_Settings
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'WP-SCSS',
'manage_options',
'wpscss_options',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'wpscss_options' );
?>
<div class="wrap">
<h2>WP-SCSS Settings</h2>
<p>
<span class="version">Version <em><?php echo get_option('wpscss_version'); ?></em>
<br/>
<span class="author">By: <a href="http://connectthink.com" target="_blank">Connect Think</a></span>
<br/>
<span class="repo">Help & Issues: <a href="https://github.com/ConnectThink/WP-SCSS" target="_blank">Github</a></span>
</p>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'wpscss_options_group' );
do_settings_sections( 'wpscss_options' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'wpscss_options_group', // Option group
'wpscss_options', // Option name
array( $this, 'sanitize' ) // Sanitize
);
// Paths to Directories
add_settings_section(
'wpscss_paths_section', // ID
'Configure Paths', // Title
array( $this, 'print_paths_info' ), // Callback
'wpscss_options' // Page
);
add_settings_field(
'wpscss_base_folder', // ID
'Base Location', // Title
array( $this, 'input_select_callback' ), // Callback
'wpscss_options', // Page
'wpscss_paths_section', // Section
array( // args
'name' => 'base_compiling_folder',
'type' => apply_filters( 'wp_scss_base_compiling_modes',
array(
get_stylesheet_directory() => 'Current theme',
wp_get_upload_dir()['basedir'] => 'Uploads directory',
WPSCSS_PLUGIN_DIR => 'WP-SCSS Plugin',
)
)
)
);
add_settings_field(
'wpscss_scss_dir', // ID
'SCSS Location', // Title
array( $this, 'input_text_callback' ), // Callback
'wpscss_options', // Page
'wpscss_paths_section', // Section
array( // args
'name' => 'scss_dir',
)
);
add_settings_field(
'wpscss_css_dir', // ID
'CSS Location', // Title
array( $this, 'input_text_callback' ), // Callback
'wpscss_options', // Page
'wpscss_paths_section', // Section
array( // args
'name' => 'css_dir',
)
);
// Compiling Options
add_settings_section(
'wpscss_compile_section', // ID
'Compiling Options', // Title
array( $this, 'print_compile_info' ), // Callback
'wpscss_options' // Page
);
add_settings_field(
'Compiling Mode', // ID
'Compiling Mode', // Title
array( $this, 'input_select_callback' ), // Callback
'wpscss_options', // Page
'wpscss_compile_section', // Section
array( // args
'name' => 'compiling_options',
'type' => apply_filters( 'wp_scss_compiling_modes',
array(
'ScssPhp\ScssPhp\Formatter\Expanded' => 'Expanded',
'ScssPhp\ScssPhp\Formatter\Nested' => 'Nested',
'ScssPhp\ScssPhp\Formatter\Compressed' => 'Compressed',
'ScssPhp\ScssPhp\Formatter\Compact' => 'Compact',
'ScssPhp\ScssPhp\Formatter\Crunched' => 'Crunched',
'ScssPhp\ScssPhp\Formatter\Debug' => 'Debug'
)
)
)
);
add_settings_field(
'Source Map Mode', // ID
'Source Map Mode', // Title
array( $this, 'input_select_callback' ), // Callback
'wpscss_options', // Page
'wpscss_compile_section', // Section
array( // args
'name' => 'sourcemap_options',
'type' => apply_filters( 'wp_scss_sourcemap_modes',
array(
'SOURCE_MAP_NONE' => 'None',
'SOURCE_MAP_INLINE' => 'Inline',
'SOURCE_MAP_FILE' => 'File'
)
)
)
);
add_settings_field(
'Error Display', // ID
'Error Display', // Title
array( $this, 'input_select_callback' ), // Callback
'wpscss_options', // Page
'wpscss_compile_section', // Section
array( // args
'name' => 'errors',
'type' => apply_filters( 'wp_scss_error_diplay',
array(
'show' => 'Show in Header',
'show-logged-in' => 'Show to Logged In Users',
'hide' => 'Print to Log',
)
)
)
);
// Enqueuing Options
add_settings_section(
'wpscss_enqueue_section', // ID
'Enqueuing Options', // Title
array( $this, 'print_enqueue_info' ), // Callback
'wpscss_options' // Page
);
add_settings_field(
'Enqueue Stylesheets', // ID
'Enqueue Stylesheets', // Title
array( $this, 'input_checkbox_callback' ), // Callback
'wpscss_options', // Page
'wpscss_enqueue_section', // Section
array( // args
'name' => 'enqueue'
)
);
// Development options
add_settings_section(
'wpscss_development_section', // ID
'Development Settings', // Title
'', // Callback
'wpscss_options' // Page
);
add_settings_field(
'wpscss_scss_always_recompile', // ID
'Always Recompile', // Title
array( $this, 'input_checkbox_callback' ), // Callback
'wpscss_options', // Page
'wpscss_development_section', // Section
array( // args
'name' => 'always_recompile',
)
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input ) {
foreach( ['scss_dir', 'css_dir'] as $dir ){
if( !empty( $input[$dir] ) ) {
$input[$dir] = sanitize_text_field( $input[$dir] );
// Add a trailing slash if not already present
if(substr($input[$dir], -1) != '/'){
$input[$dir] .= '/';
}
}
}
return $input;
}
/**
* Print the Section text
*/
public function print_paths_info() {
print 'Location of your SCSS/CSS folders. Folders must be nested under your <b>Base Location</b> and start with <code>/</code>.</br>Examples: <code>/custom-scss/</code> and <code>/custom-css/</code>';
}
public function print_compile_info() {
print 'Choose how you would like SCSS and source maps to be compiled and how you would like the plugin to handle errors';
}
public function print_enqueue_info() {
print 'WP-SCSS can enqueue your css stylesheets in the header automatically.';
}
/**
* Text Fields' Callback
*/
public function input_text_callback( $args ) {
printf(
'<input type="text" id="%s" name="wpscss_options[%s]" value="%s" />',
esc_attr( $args['name'] ), esc_attr( $args['name'] ), esc_attr( isset($this->options[$args['name']]) ? $this->options[$args['name']] : '' )
);
}
/**
* Select Boxes' Callbacks
*/
public function input_select_callback( $args ) {
$this->options = get_option( 'wpscss_options' );
$html = sprintf( '<select id="%s" name="wpscss_options[%s]">', esc_attr( $args['name'] ), esc_attr( $args['name'] ) );
foreach( $args['type'] as $value => $title ) {
$html .= '<option value="' . esc_attr( $value ) . '"' . selected( isset($this->options[esc_attr( $args['name'] )]) ? $this->options[esc_attr( $args['name'] )] : '', esc_attr( $value ), false) . '>' . esc_attr( $title ) . '</option>';
}
$html .= '</select>';
echo $html;
}
/**
* Checkboxes' Callbacks
*/
public function input_checkbox_callback( $args ) {
$this->options = get_option( 'wpscss_options' );
$html = "";
if($args['name'] == 'always_recompile' && defined('WP_SCSS_ALWAYS_RECOMPILE') && WP_SCSS_ALWAYS_RECOMPILE){
$html .= '<input type="checkbox" id="' . esc_attr( $args['name'] ) . '" name="wpscss_options[' . esc_attr( $args['name'] ) . ']" value="1"' . checked( 1, isset( $this->options[esc_attr( $args['name'] )] ) ? $this->options[esc_attr( $args['name'] )] : 1, false ) . ' disabled=disabled/>';
$html .= '<label for="' . esc_attr( $args['name'] ) . '">Currently overwritten by constant <code>WP_SCSS_ALWAYS_RECOMPILE</code></label>';
}else{
$html .= '<input type="checkbox" id="' . esc_attr( $args['name'] ) . '" name="wpscss_options[' . esc_attr( $args['name'] ) . ']" value="1"' . checked( 1, isset( $this->options[esc_attr( $args['name'] )] ) ? $this->options[esc_attr( $args['name'] )] : 0, false ) . '/>';
$html .= '<label for="' . esc_attr( $args['name'] ) . '"></label>';
}
echo $html;
}
}