| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* weDevs Settings API wrapper class |
| 5 |
* |
| 6 |
* @version 1.2 (18-Oct-2015) |
| 7 |
* |
| 8 |
* @author Tareq Hasan |
| 9 |
* @link http://tareq.weDevs.com Tareq's Planet |
| 10 |
* @example src/settings-api.php How to use the class |
| 11 |
*/ |
| 12 |
|
| 13 |
if (! class_exists('ngallery_box_settings')): |
| 14 |
class ngallery_box_settings |
| 15 |
{ |
| 16 |
|
| 17 |
protected $settings_sections = array(); |
| 18 |
protected $settings_fields = array(); |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 23 |
} |
| 24 |
|
| 25 |
function admin_enqueue_scripts() |
| 26 |
{ |
| 27 |
wp_enqueue_style('wp-color-picker'); |
| 28 |
wp_enqueue_media(); |
| 29 |
wp_enqueue_script('wp-color-picker'); |
| 30 |
wp_enqueue_script('jquery'); |
| 31 |
} |
| 32 |
|
| 33 |
function set_sections($sections) |
| 34 |
{ |
| 35 |
$this->settings_sections = $sections; |
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
function add_section($section) |
| 40 |
{ |
| 41 |
$this->settings_sections[] = $section; |
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
function set_fields($fields) |
| 46 |
{ |
| 47 |
$this->settings_fields = $fields; |
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
function add_field($section, $field) |
| 52 |
{ |
| 53 |
$defaults = array( |
| 54 |
'name' => '', |
| 55 |
'label' => '', |
| 56 |
'desc' => '', |
| 57 |
'type' => 'text' |
| 58 |
); |
| 59 |
|
| 60 |
$arg = wp_parse_args($field, $defaults); |
| 61 |
$this->settings_fields[$section][] = $arg; |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
function admin_init() |
| 67 |
{ |
| 68 |
foreach ($this->settings_sections as $section) { |
| 69 |
if (false === get_option($section['id'])) { |
| 70 |
add_option($section['id']); |
| 71 |
} |
| 72 |
|
| 73 |
$callback = isset($section['callback']) ? $section['callback'] : null; |
| 74 |
|
| 75 |
if (isset($section['desc']) && !empty($section['desc'])) { |
| 76 |
$callback = function () use ($section) { |
| 77 |
echo '<div class="inside">' . esc_html($section['desc']) . '</div>'; |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
add_settings_section($section['id'], esc_html($section['title']), $callback, $section['id']); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ($this->settings_fields as $section => $field) { |
| 85 |
foreach ($field as $option) { |
| 86 |
$type = isset($option['type']) ? $option['type'] : 'text'; |
| 87 |
|
| 88 |
$args = array( |
| 89 |
'id' => $option['name'], |
| 90 |
'label_for' => "{$section}[{$option['name']}]", |
| 91 |
'desc' => isset($option['desc']) ? $option['desc'] : '', |
| 92 |
'name' => $option['label'], |
| 93 |
'section' => $section, |
| 94 |
'size' => isset($option['size']) ? $option['size'] : null, |
| 95 |
'options' => isset($option['options']) ? $option['options'] : '', |
| 96 |
'std' => isset($option['default']) ? $option['default'] : '', |
| 97 |
'sanitize_callback' => isset($option['sanitize_callback']) ? $option['sanitize_callback'] : '', |
| 98 |
'type' => $type, |
| 99 |
); |
| 100 |
|
| 101 |
add_settings_field($section . '[' . $option['name'] . ']', esc_html($option['label']), array($this, 'callback_' . $type), $section, $section, $args); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
foreach ($this->settings_sections as $section) { |
| 106 |
register_setting($section['id'], $section['id'], array($this, 'sanitize_options')); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public function get_field_description($args) |
| 111 |
{ |
| 112 |
if (! empty($args['desc'])) { |
| 113 |
$desc = sprintf('<p class="description">%s</p>', esc_html($args['desc'])); |
| 114 |
} else { |
| 115 |
$desc = ''; |
| 116 |
} |
| 117 |
|
| 118 |
return $desc; |
| 119 |
} |
| 120 |
|
| 121 |
function callback_text($args) |
| 122 |
{ |
| 123 |
$value = esc_attr($this->get_option($args['id'], $args['section'], $args['std'])); |
| 124 |
$size = isset($args['size']) && !is_null($args['size']) ? $args['size'] : 'regular'; |
| 125 |
$type = isset($args['type']) ? $args['type'] : 'text'; |
| 126 |
|
| 127 |
$html = sprintf( |
| 128 |
'<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"/>', |
| 129 |
$type, |
| 130 |
$size, |
| 131 |
esc_attr($args['section']), |
| 132 |
esc_attr($args['id']), |
| 133 |
$value |
| 134 |
); |
| 135 |
$html .= $this->get_field_description($args); |
| 136 |
|
| 137 |
echo wp_kses_post($html); |
| 138 |
} |
| 139 |
|
| 140 |
function callback_url($args) |
| 141 |
{ |
| 142 |
$this->callback_text($args); |
| 143 |
} |
| 144 |
|
| 145 |
function callback_number($args) |
| 146 |
{ |
| 147 |
$this->callback_text($args); |
| 148 |
} |
| 149 |
|
| 150 |
function callback_checkbox($args) |
| 151 |
{ |
| 152 |
$value = esc_attr($this->get_option($args['id'], $args['section'], $args['std'])); |
| 153 |
|
| 154 |
$html = '<fieldset>'; |
| 155 |
$html .= sprintf('<label for="wpuf-%1$s[%2$s]">', esc_attr($args['section']), esc_attr($args['id'])); |
| 156 |
$html .= sprintf('<input type="hidden" name="%1$s[%2$s]" value="off" />', esc_attr($args['section']), esc_attr($args['id'])); |
| 157 |
$html .= sprintf('<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', esc_attr($args['section']), esc_attr($args['id']), checked($value, 'on', false)); |
| 158 |
$html .= sprintf('%1$s</label>', esc_html($args['desc'])); |
| 159 |
$html .= '</fieldset>'; |
| 160 |
|
| 161 |
echo wp_kses_post($html); |
| 162 |
} |
| 163 |
|
| 164 |
function callback_multicheck($args) |
| 165 |
{ |
| 166 |
$value = $this->get_option($args['id'], $args['section'], $args['std']); |
| 167 |
$html = '<fieldset>'; |
| 168 |
|
| 169 |
foreach ($args['options'] as $key => $label) { |
| 170 |
$checked = isset($value[$key]) ? $value[$key] : '0'; |
| 171 |
$html .= sprintf('<label for="wpuf-%1$s[%2$s][%3$s]">', esc_attr($args['section']), esc_attr($args['id']), esc_attr($key)); |
| 172 |
$html .= sprintf('<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', esc_attr($args['section']), esc_attr($args['id']), esc_attr($key), checked($checked, $key, false)); |
| 173 |
$html .= sprintf('%1$s</label><br>', esc_html($label)); |
| 174 |
} |
| 175 |
|
| 176 |
$html .= $this->get_field_description($args); |
| 177 |
$html .= '</fieldset>'; |
| 178 |
|
| 179 |
echo wp_kses_post($html); |
| 180 |
} |
| 181 |
|
| 182 |
function callback_select($args) |
| 183 |
{ |
| 184 |
$value = esc_attr($this->get_option($args['id'], $args['section'], $args['std'])); |
| 185 |
$size = isset($args['size']) && !is_null($args['size']) ? $args['size'] : 'regular'; |
| 186 |
$html = sprintf('<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, esc_attr($args['section']), esc_attr($args['id'])); |
| 187 |
|
| 188 |
foreach ($args['options'] as $key => $label) { |
| 189 |
$html .= sprintf('<option value="%s"%s>%s</option>', esc_attr($key), selected($value, $key, false), esc_html($label)); |
| 190 |
} |
| 191 |
|
| 192 |
$html .= sprintf('</select>'); |
| 193 |
$html .= $this->get_field_description($args); |
| 194 |
|
| 195 |
echo wp_kses_post($html); |
| 196 |
} |
| 197 |
|
| 198 |
function callback_textarea($args) |
| 199 |
{ |
| 200 |
$value = esc_textarea($this->get_option($args['id'], $args['section'], $args['std'])); |
| 201 |
$size = isset($args['size']) && !is_null($args['size']) ? $args['size'] : 'regular'; |
| 202 |
|
| 203 |
$html = sprintf('<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', $size, esc_attr($args['section']), esc_attr($args['id']), $value); |
| 204 |
$html .= $this->get_field_description($args); |
| 205 |
|
| 206 |
echo wp_kses_post($html); |
| 207 |
} |
| 208 |
|
| 209 |
function sanitize_options($options) |
| 210 |
{ |
| 211 |
foreach ($options as $option_slug => $option_value) { |
| 212 |
$sanitize_callback = $this->get_sanitize_callback($option_slug); |
| 213 |
|
| 214 |
if ($sanitize_callback) { |
| 215 |
$options[$option_slug] = call_user_func($sanitize_callback, $option_value); |
| 216 |
continue; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return $options; |
| 221 |
} |
| 222 |
|
| 223 |
function get_sanitize_callback($slug = '') |
| 224 |
{ |
| 225 |
if (empty($slug)) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
foreach ($this->settings_fields as $section => $options) { |
| 230 |
foreach ($options as $option) { |
| 231 |
if ($option['name'] !== $slug) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
return isset($option['sanitize_callback']) && is_callable($option['sanitize_callback']) ? $option['sanitize_callback'] : false; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
function get_option($option, $section, $default = '') |
| 243 |
{ |
| 244 |
$options = get_option($section); |
| 245 |
return isset($options[$option]) ? $options[$option] : $default; |
| 246 |
} |
| 247 |
|
| 248 |
function show_navigation() |
| 249 |
{ |
| 250 |
$html = '<h2 class="nav-tab-wrapper">'; |
| 251 |
foreach ($this->settings_sections as $tab) { |
| 252 |
$html .= sprintf('<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', esc_attr($tab['id']), esc_html($tab['title'])); |
| 253 |
} |
| 254 |
$html .= '</h2>'; |
| 255 |
echo wp_kses_post($html); |
| 256 |
} |
| 257 |
|
| 258 |
function show_forms() |
| 259 |
{ |
| 260 |
?> |
| 261 |
<div class="metabox-holder"> |
| 262 |
<?php foreach ($this->settings_sections as $form) { ?> |
| 263 |
<div id="<?php echo esc_attr($form['id']); ?>" class="group" style="display: none;"> |
| 264 |
<form method="post" action="options.php"> |
| 265 |
<?php |
| 266 |
do_action('wsa_form_top_' . $form['id'], $form); |
| 267 |
settings_fields($form['id']); |
| 268 |
do_settings_sections($form['id']); |
| 269 |
do_action('wsa_form_bottom_' . $form['id'], $form); |
| 270 |
?> |
| 271 |
<div style="padding-left: 10px"> |
| 272 |
<?php submit_button(); ?> |
| 273 |
</div> |
| 274 |
</form> |
| 275 |
</div> |
| 276 |
<?php } ?> |
| 277 |
</div> |
| 278 |
<?php |
| 279 |
$this->script(); |
| 280 |
} |
| 281 |
|
| 282 |
function script() |
| 283 |
{ |
| 284 |
?> |
| 285 |
<script> |
| 286 |
jQuery(document).ready(function($) { |
| 287 |
$('.wp-color-picker-field').wpColorPicker(); |
| 288 |
$('.group').hide(); |
| 289 |
var activetab = ''; |
| 290 |
if (typeof(localStorage) != 'undefined') { |
| 291 |
activetab = localStorage.getItem("activetab"); |
| 292 |
} |
| 293 |
if (activetab != '' && $(activetab).length) { |
| 294 |
$(activetab).fadeIn(); |
| 295 |
} else { |
| 296 |
$('.group:first').fadeIn(); |
| 297 |
} |
| 298 |
if (activetab != '' && $(activetab + '-tab').length) { |
| 299 |
$(activetab + '-tab').addClass('nav-tab-active'); |
| 300 |
} else { |
| 301 |
$('.nav-tab-wrapper a:first').addClass('nav-tab-active'); |
| 302 |
} |
| 303 |
$('.nav-tab-wrapper a').click(function(evt) { |
| 304 |
$('.nav-tab-wrapper a').removeClass('nav-tab-active'); |
| 305 |
$(this).addClass('nav-tab-active').blur(); |
| 306 |
var clicked_group = $(this).attr('href'); |
| 307 |
if (typeof(localStorage) != 'undefined') { |
| 308 |
localStorage.setItem("activetab", $(this).attr('href')); |
| 309 |
} |
| 310 |
$('.group').hide(); |
| 311 |
$(clicked_group).fadeIn(); |
| 312 |
evt.preventDefault(); |
| 313 |
}); |
| 314 |
|
| 315 |
$('.wpsa-browse').on('click', function(event) { |
| 316 |
event.preventDefault(); |
| 317 |
var self = $(this); |
| 318 |
var file_frame = wp.media.frames.file_frame = wp.media({ |
| 319 |
title: self.data('uploader_title'), |
| 320 |
button: { |
| 321 |
text: self.data('uploader_button_text'), |
| 322 |
}, |
| 323 |
multiple: false |
| 324 |
}); |
| 325 |
file_frame.on('select', function() { |
| 326 |
attachment = file_frame.state().get('selection').first().toJSON(); |
| 327 |
self.prev('.wpsa-url').val(attachment.url); |
| 328 |
}); |
| 329 |
file_frame.open(); |
| 330 |
}); |
| 331 |
}); |
| 332 |
</script> |
| 333 |
|
| 334 |
<style type="text/css"> |
| 335 |
.form-table th { |
| 336 |
padding: 20px 10px; |
| 337 |
} |
| 338 |
|
| 339 |
#wpbody-content .metabox-holder { |
| 340 |
padding-top: 5px; |
| 341 |
} |
| 342 |
</style> |
| 343 |
<?php |
| 344 |
} |
| 345 |
} |
| 346 |
endif; |
| 347 |
new ngallery_box_settings(); |
| 348 |
|