-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclass.mesh-settings.php
524 lines (438 loc) · 13.9 KB
/
class.mesh-settings.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/**
* Control all of our plugin Settings
*
* @since 1.0.0
* @package Mesh
* @subpackage Settings
*/
// Make sure we don't expose any info if called directly.
if ( ! function_exists( 'add_action' ) ) {
exit;
}
/**
* Class Mesh_Settings
*/
class Mesh_Settings {
/**
* Define our settings page
*
* @var string
*/
public static $settings_page = 'mesh';
/**
* Give our plugin a name
*
* @var string
*/
public static $plugin_name = LINCHPIN_MESH_PLUGIN_NAME;
/**
* Initialize our plugin settings.
*
* @since 1.0.0
*/
public static function init() {
add_action( 'admin_menu', array( 'Mesh_Settings', 'add_admin_menu' ) );
add_action( 'admin_init', array( 'Mesh_Settings', 'settings_init' ) );
add_filter( 'plugin_action_links', array( 'Mesh_Settings', 'add_settings_link' ), 10, 5 );
}
/**
* Add the options page to our settings menu
*/
public static function add_admin_menu() {
add_options_page( LINCHPIN_MESH_PLUGIN_NAME, LINCHPIN_MESH_PLUGIN_NAME, 'manage_options', self::$settings_page, array( 'Mesh_Settings', 'add_options_page' ) );
add_submenu_page( 'edit.php?post_type=mesh_template', esc_html__( 'Settings', 'mesh' ), esc_html__( 'Settings', 'mesh' ), 'manage_options', 'options-general.php?page=mesh' );
}
public static function add_settings_link( $actions, $plugin_file ) {
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = 'mesh/mesh.php';
}
if ( $plugin === $plugin_file ) {
$settings = array( 'settings' => '<a href="options-general.php?page=mesh">' . esc_html__( 'Settings', 'mesh' ) . '</a>' );
$site_link = array( 'faq' => '<a href="https://meshplugin.com/knowledgebase/" target="_blank">' . esc_html__( 'FAQ', 'mesh' ) . '</a>');
$actions = array_merge( $settings, $actions );
$actions = array_merge( $site_link, $actions );
}
return $actions;
}
/**
* Create our settings section
*
* @param $args
* @since 1.0.0
*/
public static function create_section( $args ) {
?>
<div class="gray-bg negative-bg">
<div class="wrapper">
<h2 class="color-darkpurple light-weight">
<?php echo esc_html( $args['title'] ); ?>
</h2>
</div>
</div>
<?php
}
/**
* Create a post type settings section.
*/
public static function create_post_type_section() {
?>
<div class="gray-bg negative-bg">
<div class="wrapper">
<h2 class="color-darkpurple light-weight">
<?php esc_html_e( 'Enable Mesh for the following Post Types', 'mesh' ); ?>
</h2>
</div>
</div>
<div class="wrapper">
<p><?php esc_html_e( 'Select the post types that allow Mesh functionality.', 'mesh' ); ?></p>
</div>
<?php
}
/**
* Validate that the user has enabled a post type
*
* @since 1.2.4
* @param $input
*
* @return $input
*/
public static function validate_mesh_post_types( $input ) {
$message = '';
$type = '';
if ( null !== $input ) {
$message = esc_html__( 'Mesh Settings Saved.', 'mesh' );
$type = 'updated';
} else {
$message = esc_html__( 'Mesh Settings Saved. You have disabled all post types. We suggest disabling the plugin if it is no longer needed', 'mesh' );
$type = 'updated';
}
add_settings_error( 'mesh_post_types', 'mesh_post_types_notice', $message, $type );
$input['mesh_template'] = '1'; // Always make sure we have a mesh_template in our options
return $input;
}
/**
* Get all of our foundation related grids
*
* @since 1.2.5
*
* @param $args
*
* @return array
*/
public static function get_foundation_grid_systems( $args ) {
$grid_systems = mesh_get_responsive_grid_systems();
$foundation_grid_systems = $grid_systems['foundation'];
$available_grids = array();
foreach ( $foundation_grid_systems as $key => $grid_system ) {
$available_grids[] = array(
'label' => $grid_system['name'],
'value' => $key,
);
}
return $available_grids;
}
/**
* Add all of our settings from the API
*/
public static function settings_init() {
register_setting( self::$settings_page, 'mesh_settings' );
register_setting( self::$settings_page, 'mesh_post_types', array( 'Mesh_Settings', 'validate_mesh_post_types' ) );
// Default Settings Section.
add_settings_section(
'mesh_sections',
esc_html__( 'Basic Settings', 'mesh' ),
array( 'Mesh_Settings', 'create_section' ),
self::$settings_page
);
// Option : CSS Mode.
$css_mode = array(
array(
'label' => esc_html__( 'Use Mesh CSS', 'mesh' ),
'value' => 0,
),
array(
'label' => esc_html__( 'Disable Mesh CSS', 'mesh' ),
'value' => -1,
),
array(
'label' => esc_html__( 'Use Foundation built into my theme', 'mesh' ),
'value' => 1,
),
array(
'label' => esc_html__( 'Use Bootstrap', 'mesh' ),
'value' => 2,
),
);
// Allow filtering of available css_mode options.
$css_mode = apply_filters( 'mesh_css_mode', $css_mode );
add_settings_field(
'css_mode',
esc_html__( 'CSS Settings', 'mesh' ),
array( 'Mesh_Settings', 'add_select' ),
self::$settings_page,
'mesh_sections',
array(
'field' => 'css_mode',
'label' => esc_html__( 'CSS', 'mesh' ),
'description' => esc_html__( 'Choose whether or not to enqueue CSS with Mesh. You can also select which responsive framework you are using Foundation, Bootstrap or custom (using hooks)', 'mesh' ),
'options' => $css_mode,
)
);
/*
* Option: Foundation Version
* Add an option for Foundation Version
* @since 1.1.3
*/
$foundation_version = array(
array(
'label' => esc_html__( 'Foundation 5', 'mesh' ),
'value' => '',
),
array(
'label' => esc_html__( 'Foundation 6', 'mesh' ),
'value' => 6,
),
array(
'label' => esc_html__( 'Foundation 6.4.X', 'mesh' ),
'value' => 6.4,
),
);
$foundation_version = apply_filters( 'mesh_foundation_version', $foundation_version );
add_settings_field(
'foundation_version',
esc_html__( 'Foundation Version', 'mesh' ),
array( 'Mesh_Settings', 'add_select' ),
self::$settings_page,
'mesh_sections',
array(
'field' => 'foundation_version',
'label' => esc_html__( 'Foundation Version', 'mesh' ),
'description' => esc_html__( 'Choose which version of Foundation you are using. Foundation 5 and 6 have subtle yet important differences when it comes to markup for components like interchange that Mesh utilizes.', 'mesh' ),
'options' => $foundation_version,
)
);
$mesh_grid_systems = mesh_get_responsive_grid_systems();
add_settings_field(
'grid_system',
esc_html__( 'Foundation Grid System', 'mesh' ),
array( 'Mesh_Settings', 'add_select' ),
self::$settings_page,
'mesh_sections',
array(
'field' => 'grid_system',
'label' => esc_html__( 'Foundation Grid System', 'mesh' ),
'description' => esc_html__( 'Choose which version of Foundation Flexbox you are using: Flexbox or XY-grid. Foundation 6.4.0 introduced new class names for row and columns: grid-x and cell.', 'mesh' ),
'options_cb' => array( 'Mesh_Settings', 'get_foundation_grid_systems' ),
'default' => 'float',
)
);
// Add an option for each post type.
$post_types = get_post_types();
if ( ! empty( $post_types ) ) {
add_settings_section(
'mesh_post_type_section',
esc_html__( 'Post Types', 'mesh' ),
array( 'Mesh_Settings', 'create_post_type_section' ),
self::$settings_page
);
foreach ( $post_types as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
// Skip any of the following post types and post types that ARE NOT public.
if ( in_array( $post_type, array( 'revision', 'nav_menu_item', 'attachment', 'mesh_template' ), true ) || ! $post_type_object->public ) {
continue;
}
add_settings_field(
'mesh_post_types_' . $post_type,
$post_type_object->labels->name,
array( 'Mesh_Settings', 'add_checkbox' ),
self::$settings_page,
'mesh_post_type_section',
array(
'field' => $post_type_object->name,
'name' => $post_type_object->labels->name,
'options' => 'mesh_post_types',
)
);
}
}
// Uninstall Option
// Default Settings Section.
add_settings_section(
'mesh_uninstall',
esc_html__( 'Mesh Uninstall', 'mesh' ),
array( 'Mesh_Settings', 'create_section' ),
self::$settings_page
);
add_settings_field(
'mesh_uninstall',
esc_html__( 'Remove All Data on Uninstall?', 'mesh' ),
array( 'Mesh_Settings', 'add_checkbox' ),
self::$settings_page,
'mesh_uninstall',
array(
'options' => 'mesh_settings',
'field' => 'uninstall',
'label' => esc_html__( 'Uninstall', 'mesh' ),
'type' => $post_type_object->name,
'name' => $post_type_object->labels->name,
)
);
}
/**
* Add our options page wrapper Form
*/
public static function add_options_page() {
$tabs = self::get_tabs();
$default_tab = self::get_default_tab_slug();
$active_tab = isset( $_GET['tab'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['tab'] ) ), $tabs ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : $default_tab; // WPCS: input var okay, CSRF ok.
include_once( LINCHPIN_MESH___PLUGIN_DIR . '/admin/settings.php' );
}
/**
* FIELD CONTROLS
*
* Below you will find all field control.
*/
/**
* Build out our settings fields as needed.
* Echos our field html.
*
* @since 1.0.0
* @param object $args An Object of our field customizations.
*/
public static function add_textfield( $args ) {
/**
* Define our field defaults
*/
$defaults = array(
'type' => 'text',
'class' => '',
'description' => '',
'label' => '',
);
// Parse incoming $args into an array and merge it with $defaults.
$args = wp_parse_args( $args, $defaults );
$options = get_option( 'mesh_settings' );
?>
<?php if ( ! empty( $args['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $args['description'] ); ?></p>
<?php endif; ?>
<input type="<?php echo esc_attr( $args['type'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="mesh_settings[<?php echo esc_attr( $args['field'] ); ?>]" value="<?php echo esc_attr( $options[ $args['field'] ] ); ?>">
<?php
}
/**
* Used any time we need to add in a select field
*
* @param array $args Args for Customization.
*/
public static function add_select( $args ) {
/**
* Define our field defaults
*/
$defaults = array(
'class' => '',
'options' => array(),
);
// Parse incoming $args into an array and merge it with $defaults.
$args = wp_parse_args( $args, $defaults );
$options = get_option( 'mesh_settings' );
// @since 1.2.5
// @todo this needs to be cleaned up to meet wpcs
$select_options = ( ! empty( $args['options_cb'] ) && is_callable( $args['options_cb'] ) )
? call_user_func_array( $args['options_cb'], $args )
: $args['options'];
?>
<?php if ( ! empty( $args['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $args['description'] ); ?></p>
<?php endif; ?>
<label for="mesh-<?php echo esc_attr( $args['field'] ); ?>"><?php echo esc_html( $args['label'] ); ?></label>
<select id="mesh-<?php echo esc_attr( $args['field'] ); ?>" name="mesh_settings[<?php echo esc_attr( $args['field'] ); ?>]">
<?php foreach ( $select_options as $option ) : ?>
<?php
$selected = '';
if ( ! empty( $options[ $args['field'] ] ) ) {
$selected = selected( $options[ $args['field'] ], $option['value'], false );
}
// @since 1.2.5 If we don't have a selected option and we have a default value use that
if ( empty( $options[ $args['field'] ] ) && ( isset( $args['default'] ) && '' !== $args['default'] ) ) {
$selected = selected( $option['value'], $args['default'], false );
}
?>
<option value="<?php echo esc_attr( $option['value'] ); ?>" <?php echo $selected; // XSS ok. ?>><?php echo esc_html( $option['label'] ); ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Create a checkbox field.
*
* @param array $args Customizations.
*/
public static function add_checkbox( $args ) {
/**
* Define our field defaults
*/
$defaults = array(
'class' => '',
'description' => '',
'label' => '',
'options' => '', // @since 1.2.4
);
// Parse incoming $args into an array and merge it with $defaults.
$args = wp_parse_args( $args, $defaults );
if ( empty( $args['options'] ) ) { // If we don't have any option, die early.
return;
}
$options = get_option( $args['options'] );
$checked = false;
if ( ! empty( $options[ $args['field'] ] ) ) {
$checked = true;
}
?>
<?php if ( ! empty( $args['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $args['description'] ); ?></p>
<?php endif; ?>
<input type="checkbox" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['options'] ); ?>[<?php echo esc_attr( $args['field'] ); ?>]" value="1" <?php checked( $checked ); ?>>
<?php
}
/**
* Allow filtering of the settings tabs.
*
* @param array $default_settings Default Settings Array.
*
* @return array
*/
static private function apply_tab_slug_filters( $default_settings ) {
$extended_settings[] = array();
$extended_tabs = self::get_tabs();
foreach ( $extended_tabs as $tab_slug => $tab_desc ) {
$options = isset( $default_settings[ $tab_slug ] ) ? $default_settings[ $tab_slug ] : array();
$extended_settings[ $tab_slug ] = apply_filters( 'mesh_' . $tab_slug, $options );
}
return $extended_settings;
}
/**
* Get the default tab slug
*
* @return mixed
*/
static public function get_default_tab_slug() {
return key( self::get_tabs() );
}
/**
* Retrieve settings tabs
*
* @since 1.0.0
* @return array $tabs Settings tabs
*/
static public function get_tabs() {
$tabs = array(
'settings' => esc_html__( 'Settings', 'mesh' ),
'about' => esc_html__( 'About Mesh', 'mesh' ),
'new' => esc_html__( "What's New", 'mesh' ),
'changelog' => esc_html__( 'Change Log', 'mesh' ),
);
return apply_filters( 'mesh_tabs', $tabs );
}
}