class-admin.php
4 years ago
class-columns.php
4 years ago
class-counter.php
4 years ago
class-crawler-detect.php
4 years ago
class-cron.php
4 years ago
class-dashboard.php
4 years ago
class-frontend.php
4 years ago
class-functions.php
4 years ago
class-query.php
4 years ago
class-settings-api.php
4 years ago
class-settings.php
4 years ago
class-update.php
4 years ago
class-widgets.php
4 years ago
functions.php
4 years ago
class-settings-api.php
670 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Settings_API class. |
| 8 | * |
| 9 | * @class Post_Views_Counter_Settings_API |
| 10 | */ |
| 11 | class Post_Views_Counter_Settings_API { |
| 12 | |
| 13 | private $settings; |
| 14 | private $pages; |
| 15 | private $page_types; |
| 16 | private $prefix; |
| 17 | private $slug; |
| 18 | private $domain; |
| 19 | private $short; |
| 20 | private $object; |
| 21 | private $plugin; |
| 22 | private $plugin_url; |
| 23 | |
| 24 | /** |
| 25 | * Class constructor. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function __construct( $args ) { |
| 30 | // set initial data |
| 31 | $this->prefix = $args['prefix']; |
| 32 | $this->domain = $args['domain']; |
| 33 | |
| 34 | // empty slug? |
| 35 | if ( empty( $args['slug'] ) ) |
| 36 | $this->slug = $args['domain']; |
| 37 | else |
| 38 | $this->slug = $args['slug']; |
| 39 | |
| 40 | // empty short name? |
| 41 | if ( empty( $args['short'] ) ) { |
| 42 | $short = ''; |
| 43 | |
| 44 | // prepare short name based on prefix |
| 45 | $parts = explode( '_', $this->prefix ); |
| 46 | |
| 47 | foreach ( $parts as $string ) { |
| 48 | $short .= substr( $string, 0, 1 ); |
| 49 | } |
| 50 | |
| 51 | // set short name |
| 52 | $this->short = $short; |
| 53 | } else |
| 54 | $this->short = $args['short']; |
| 55 | |
| 56 | $this->object = $args['object']; |
| 57 | $this->plugin = $args['plugin']; |
| 58 | $this->plugin_url = $args['plugin_url']; |
| 59 | |
| 60 | // actions |
| 61 | add_action( 'admin_menu', [ $this, 'admin_menu_options' ], 11 ); |
| 62 | add_action( 'admin_init', [ $this, 'register_settings' ], 11 ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get prefix. |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | public function get_prefix() { |
| 71 | return $this->prefix; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get settings. |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function get_settings() { |
| 80 | return $this->settings; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Add menu pages. |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function admin_menu_options() { |
| 89 | $this->pages = apply_filters( $this->prefix . '_settings_pages', [] ); |
| 90 | $types = [ |
| 91 | 'page' => [], |
| 92 | 'subpage' => [], |
| 93 | 'settings_page' => [] |
| 94 | ]; |
| 95 | |
| 96 | foreach ( $this->pages as $page => $data ) { |
| 97 | // skip invalid page types |
| 98 | if ( empty( $data['type'] ) || ! array_key_exists( $data['type'], $types ) ) |
| 99 | continue; |
| 100 | |
| 101 | // menu subpage? |
| 102 | if ( $data['type'] === 'subpage' ) { |
| 103 | add_submenu_page( $data['parent_slug'], $data['page_title'], $data['menu_title'], $data['capability'], $data['menu_slug'], ! empty( $data['callback'] ) ? $data['callback'] : [ $this, 'options_page' ] ); |
| 104 | |
| 105 | // add subpage type |
| 106 | $types['subpage'][$data['menu_slug']] = $page; |
| 107 | // menu settings page? |
| 108 | } elseif ( $data['type'] === 'settings_page' ) { |
| 109 | add_options_page( $data['page_title'], $data['menu_title'], $data['capability'], $data['menu_slug'], ! empty( $data['callback'] ) ? $data['callback'] : [ $this, 'options_page' ] ); |
| 110 | |
| 111 | // add settings type |
| 112 | $types['settings_page'][$data['menu_slug']] = $page; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // set page types |
| 117 | $this->page_types = $types; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Render settings. |
| 122 | * |
| 123 | * @return void |
| 124 | */ |
| 125 | public function options_page() { |
| 126 | global $pagenow; |
| 127 | |
| 128 | $valid_page = false; |
| 129 | |
| 130 | // get current screen |
| 131 | $screen = get_current_screen(); |
| 132 | |
| 133 | // display sub level settings page? |
| 134 | if ( $pagenow === 'admin.php' && preg_match( '/^(?:toplevel|' . $this->prefix . ')_page_' . $this->prefix . '-(' . implode( '|', $this->page_types['subpage'] ) . ')-settings$/', $screen->base, $matches ) === 1 && ! empty( $matches[1] ) ) { |
| 135 | $valid_page = true; |
| 136 | $page_type = 'subpage'; |
| 137 | $url_page = 'admin.php'; |
| 138 | } |
| 139 | |
| 140 | // display settings page? |
| 141 | if ( ! $valid_page && $pagenow === 'options-general.php' && preg_match( '/^(?:settings_page_)(' . implode( '|', array_keys( $this->page_types['settings_page'] ) ) . ')$/', $screen->base, $matches ) === 1 ) { |
| 142 | $valid_page = true; |
| 143 | $page_type = 'settings_page'; |
| 144 | $url_page = 'options-general.php'; |
| 145 | } |
| 146 | |
| 147 | // skip invalid pages |
| 148 | if ( ! $valid_page ) |
| 149 | return; |
| 150 | |
| 151 | echo ' |
| 152 | <div class="wrap"> |
| 153 | <h2>' . esc_html( $this->settings[$matches[1]]['label'] ) . '</h2>'; |
| 154 | |
| 155 | // any tabs? |
| 156 | if ( array_key_exists( 'tabs', $this->pages[$this->page_types[$page_type][$matches[1]]] ) ) { |
| 157 | // get tabs |
| 158 | $tabs = $this->pages[$this->page_types[$page_type][$matches[1]]]['tabs']; |
| 159 | |
| 160 | // reset tabs |
| 161 | reset( $tabs ); |
| 162 | |
| 163 | // get first default tab |
| 164 | $first_tab = key( $tabs ); |
| 165 | |
| 166 | // get current tab |
| 167 | $tab_key = ! empty( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ? $_GET['tab'] : $first_tab; |
| 168 | |
| 169 | echo ' |
| 170 | <h2 class="nav-tab-wrapper">'; |
| 171 | |
| 172 | foreach ( $tabs as $key => $tab ) { |
| 173 | echo ' |
| 174 | <a class="nav-tab ' . ( $tab_key === $key ? 'nav-tab-active' : '' ) . '" href="' . esc_url( admin_url( $url_page . '?page=' . $matches[1] . '&tab=' . $key ) ) . '">' . esc_html( $tab['label'] ) . '</a>'; |
| 175 | } |
| 176 | |
| 177 | echo ' |
| 178 | </h2>'; |
| 179 | } |
| 180 | |
| 181 | // skip for internal options page |
| 182 | if ( $page_type !== 'settings_page' ) |
| 183 | settings_errors(); |
| 184 | |
| 185 | echo ' |
| 186 | <div class="' . $this->slug . '-settings"> |
| 187 | <div class="df-credits"> |
| 188 | <h3 class="hndle">' . $this->plugin . ' ' . $this->object->defaults['version'] . '</h3> |
| 189 | <div class="inside"> |
| 190 | <h4 class="inner">' . __( 'Need support?', $this->domain ) . '</h4> |
| 191 | <p class="inner">' . sprintf( __( 'If you are having problems with this plugin, please browse it\'s <a href="%s" target="_blank">Documentation</a> or talk about them in the <a href="%s" target="_blank">Support forum</a>', $this->domain ), 'https://www.dfactory.eu/docs/' . $this->slug . '/?utm_source=' . $this->slug . '-settings&utm_medium=link&utm_campaign=docs', 'https://www.dfactory.eu/support/?utm_source=' . $this->slug . '-settings&utm_medium=link&utm_campaign=support' ) . '</p> |
| 192 | <hr /> |
| 193 | <h4 class="inner">' . __( 'Do you like this plugin?', $this->domain ) . '</h4> |
| 194 | <p class="inner">' . sprintf( __( '<a href="%s" target="_blank">Rate it 5</a> on WordPress.org', $this->domain ), 'https://wordpress.org/support/plugin/' . $this->slug . '/reviews/?filter=5' ) . '<br />' . |
| 195 | sprintf( __( 'Blog about it & link to the <a href="%s" target="_blank">plugin page</a>.', $this->domain ), 'https://dfactory.eu/plugins/' . $this->slug . '/?utm_source=' . $this->slug . '-settings&utm_medium=link&utm_campaign=blog-about' ) . '<br />' . |
| 196 | sprintf( __( 'Check out our other <a href="%s" target="_blank">WordPress plugins</a>.', $this->domain ), 'https://dfactory.eu/plugins/?utm_source=' . $this->slug . '-settings&utm_medium=link&utm_campaign=other-plugins' ) . ' |
| 197 | </p> |
| 198 | <hr /> |
| 199 | <p class="df-link inner"><a href="http://www.dfactory.eu/?utm_source=' . $this->slug . '-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="Digital Factory"><img src="//pvc-53eb.kxcdn.com/df-black-sm.png" alt="Digital Factory"></a></p> |
| 200 | </div> |
| 201 | </div> |
| 202 | <form action="options.php" method="post">'; |
| 203 | |
| 204 | // any tabs? |
| 205 | if ( ! empty( $tab_key ) ) { |
| 206 | if ( ! empty( $tabs[$tab_key]['option_name'] ) ) |
| 207 | $setting = $tabs[$tab_key]['option_name']; |
| 208 | else |
| 209 | $setting = $this->prefix . '_' . $tab_key . '_settings'; |
| 210 | } else |
| 211 | $setting = $this->prefix . '_' . $matches[1] . '_settings'; |
| 212 | |
| 213 | settings_fields( $setting ); |
| 214 | do_settings_sections( $setting ); |
| 215 | |
| 216 | echo ' |
| 217 | <p class="submit">'; |
| 218 | |
| 219 | submit_button( '', 'primary', 'save_' . $setting, false ); |
| 220 | |
| 221 | echo ' '; |
| 222 | |
| 223 | submit_button( __( 'Reset to defaults', $this->domain ), 'secondary reset_' . $setting . ' reset_' . $this->short . '_settings', 'reset_' . $setting, false ); |
| 224 | |
| 225 | echo ' |
| 226 | </p> |
| 227 | </form> |
| 228 | </div> |
| 229 | <div class="clear"></div> |
| 230 | </div>'; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Register settings. |
| 235 | * |
| 236 | * @return void |
| 237 | */ |
| 238 | public function register_settings() { |
| 239 | $this->settings = apply_filters( $this->prefix . '_settings_data', [] ); |
| 240 | |
| 241 | // check settings |
| 242 | foreach ( $this->settings as $setting_id => $setting ) { |
| 243 | // tabs? |
| 244 | if ( is_array( $setting['option_name'] ) ) { |
| 245 | foreach ( $setting['option_name'] as $tab => $option_name ) { |
| 246 | $this->register_setting_fields( $tab, $setting, $option_name ); |
| 247 | } |
| 248 | } else |
| 249 | $this->register_setting_fields( $setting_id, $setting ); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public function register_setting_fields( $setting_id, $setting, $option_name = '' ) { |
| 254 | if ( empty( $option_name ) ) |
| 255 | $option_name = $setting['option_name']; |
| 256 | |
| 257 | // register setting |
| 258 | register_setting( $option_name, $option_name, ! empty( $setting['validate'] ) ? $setting['validate'] : [ $this, 'validate_settings' ] ); |
| 259 | |
| 260 | // register setting sections |
| 261 | if ( ! empty( $setting['sections'] ) ) { |
| 262 | foreach ( $setting['sections'] as $section_id => $section ) { |
| 263 | add_settings_section( |
| 264 | $section_id, |
| 265 | ! empty( $section['title'] ) ? esc_html( $section['title'] ) : '', |
| 266 | ! empty( $section['callback'] ) ? $section['callback'] : null, |
| 267 | ! empty( $section['page'] ) ? $section['page'] : $option_name |
| 268 | ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // register setting fields |
| 273 | if ( ! empty( $setting['fields'] ) ) { |
| 274 | foreach ( $setting['fields'] as $field_key => $field ) { |
| 275 | if ( ! empty( $field['tab'] ) && $field['tab'] !== $setting_id ) |
| 276 | continue; |
| 277 | |
| 278 | // set field ID |
| 279 | $field_id = implode( '_', [ $this->prefix, $setting_id, $field_key ] ); |
| 280 | |
| 281 | // skip rendering this field? |
| 282 | if ( ! empty( $field['skip_rendering'] ) ) |
| 283 | continue; |
| 284 | |
| 285 | add_settings_field( |
| 286 | $field_id, |
| 287 | ! empty( $field['title'] ) ? esc_html( $field['title'] ) : '', |
| 288 | [ $this, 'render_field' ], |
| 289 | $option_name, |
| 290 | ! empty( $field['section'] ) ? esc_attr( $field['section'] ) : '', |
| 291 | array_merge( $this->prepare_field_args( $field, $field_id, $field_key, $setting_id, $option_name ), $field ) |
| 292 | ); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Prepare field arguments. |
| 299 | * |
| 300 | * @param array $args |
| 301 | * @return array |
| 302 | */ |
| 303 | public function prepare_field_args( $field, $field_id, $field_key, $setting_id, $setting_name ) { |
| 304 | // get field type |
| 305 | $field_type = ! empty( $field['type'] ) ? $field['type'] : ''; |
| 306 | |
| 307 | return [ |
| 308 | 'id' => $field_id, |
| 309 | 'name' => $setting_name . '[' . $field_key . ']', |
| 310 | 'class' => ! empty( $field['class'] ) ? $field['class'] : '', |
| 311 | 'type' => $field_type, |
| 312 | 'label' => ! empty( $field['label'] ) ? $field['label'] : '', |
| 313 | 'description' => ! empty( $field['description'] ) ? $field['description'] : '', |
| 314 | 'text' => ! empty( $field['text'] ) ? $field['text'] : '', |
| 315 | 'min' => ! empty( $field['min'] ) ? (int) $field['min'] : 0, |
| 316 | 'max' => ! empty( $field['max'] ) ? (int) $field['max'] : 0, |
| 317 | 'options' => ! empty( $field['options'] ) ? $field['options'] : [], |
| 318 | 'callback' => ! empty( $field['callback'] ) ? $field['callback'] : null, |
| 319 | 'validate' => ! empty( $field['validate'] ) ? $field['validate'] : null, |
| 320 | 'callback_args' => ! empty( $field['callback_args'] ) ? $field['callback_args'] : [], |
| 321 | 'default' => $field_type !== 'custom' ? $this->object->defaults[$setting_id][$field_key] : null, |
| 322 | 'value' => $field_type !== 'custom' ? $this->object->options[$setting_id][$field_key] : null |
| 323 | /* |
| 324 | after_field |
| 325 | before_field |
| 326 | */ |
| 327 | ]; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Render settings field. |
| 332 | * |
| 333 | * @param array $args |
| 334 | * @return void|string |
| 335 | */ |
| 336 | public function render_field( $args ) { |
| 337 | if ( empty( $args ) || ! is_array( $args ) ) |
| 338 | return; |
| 339 | |
| 340 | $html = '<div id="' . $args['id'] . '_setting"' . ( ! empty( $args['class'] ) ? ' class="' . $args['class'] . '"' : '' ) . '>'; |
| 341 | |
| 342 | if ( ! empty ( $args['before_field'] ) ) |
| 343 | $html .= $args['before_field']; |
| 344 | |
| 345 | switch ( $args['type'] ) { |
| 346 | case 'boolean': |
| 347 | $html .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="false" />'; |
| 348 | $html .= '<label><input id="' . esc_attr( $args['id'] ) . '" type="checkbox" name="' . esc_attr( $args['name'] ) . '" value="true" ' . checked( (bool) $args['value'], true, false ) . ' ' . disabled( empty( $args['disabled'] ), false, false ) . ' />' . esc_html( $args['label'] ) . '</label>'; |
| 349 | break; |
| 350 | |
| 351 | case 'radio': |
| 352 | foreach ( $args['options'] as $key => $name ) { |
| 353 | $html .= '<label><input id="' . esc_attr( $args['id'] . '_' . $key ) . '" type="radio" name="' . esc_attr( $args['name'] ) . '" value="' . esc_attr( $key ) . '" ' . checked( $key, $args['value'], false ) . ' ' . disabled( empty( $args['disabled'] ), false, false ) . ' />' . esc_html( $name ) . '</label> '; |
| 354 | } |
| 355 | break; |
| 356 | |
| 357 | case 'checkbox': |
| 358 | // possible "empty" value |
| 359 | if ( $args['value'] === 'empty' ) |
| 360 | $args['value'] = []; |
| 361 | |
| 362 | $display_type = ! empty( $args['display_type'] ) && in_array( $args['display_type'], [ 'horizontal', 'vertical' ], true ) ? $args['display_type'] : 'horizontal'; |
| 363 | |
| 364 | $html .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="empty" />'; |
| 365 | |
| 366 | foreach ( $args['options'] as $key => $name ) { |
| 367 | $html .= '<label><input id="' . esc_attr( $args['id'] . '_' . $key ) . '" type="checkbox" name="' . esc_attr( $args['name'] ) . '[]" value="' . esc_attr( $key ) . '" ' . checked( in_array( $key, $args['value'] ), true, false ) . ' ' . disabled( empty( $args['disabled'] ), false, false ) . ' />' . esc_html( $name ) . '</label>' . ( $display_type === 'horizontal' ? ' ' : '<br />' ); |
| 368 | } |
| 369 | break; |
| 370 | |
| 371 | case 'select': |
| 372 | $html .= '<select id="' . esc_attr( $args['id'] ) . '" name="' . esc_attr( $args['name'] ) . '" />'; |
| 373 | |
| 374 | foreach ( $args['options'] as $key => $name ) { |
| 375 | $html .= '<option value="' . esc_attr( $key ) . '" ' . selected( $args['value'], $key, false ) . '>' . esc_html( $name ) . '</option>'; |
| 376 | } |
| 377 | |
| 378 | $html .= '</select>'; |
| 379 | break; |
| 380 | |
| 381 | case 'range': |
| 382 | $html .= '<input id="' . esc_attr( $args['id'] . '_slider' ) . '" type="range" name="' . esc_attr( $args['name'] ) . '" value="' . esc_attr( $args['value'] ) . '" min="' . esc_attr( $args['min'] ) . '" max="' . esc_attr( $args['max'] ) . '" oninput="this.form.' . esc_attr( $args['id'] ) . '_range.value = this.value" /><output class="' . esc_attr( $this->slug ) . '-range" name="' . esc_attr( $args['id'] ) . '_range">' . ( (int) $args['value'] ) . '</output>'; |
| 383 | break; |
| 384 | |
| 385 | case 'number': |
| 386 | $html .= ( ! empty( $args['prepend'] ) ? '<span>' . esc_html( $args['prepend'] ) . '</span> ' : '' ); |
| 387 | $html .= '<input id="' . $args['id'] . '" type="text" value="' . esc_attr( $args['value'] ) . '" name="' . esc_attr( $args['name'] ) . '" />'; |
| 388 | $html .= ( ! empty( $args['append'] ) ? ' <span>' . esc_html( $args['append'] ) . '</span>' : '' ); |
| 389 | break; |
| 390 | |
| 391 | case 'custom': |
| 392 | $html .= call_user_func( $args['callback'], $args ); |
| 393 | break; |
| 394 | |
| 395 | case 'info': |
| 396 | $html .= '<span' . ( ! empty( $args['subclass'] ) ? ' class="' . esc_attr( $args['subclass'] ) . '"' : '' ) . '>' . esc_html( $args['text'] ) . '</span>'; |
| 397 | break; |
| 398 | |
| 399 | case 'input': |
| 400 | default: |
| 401 | $html .= ( ! empty( $args['prepend'] ) ? '<span>' . esc_html( $args['prepend'] ) . '</span> ' : '' ); |
| 402 | $html .= '<input id="' . $args['id'] . '"' . ( ! empty( $args['subclass'] ) ? ' class="' . esc_attr( $args['subclass'] ) . '"' : '' ) . ' type="text" value="' . esc_attr( $args['value'] ) . '" name="' . esc_attr( $args['name'] ) . '" />'; |
| 403 | $html .= ( ! empty( $args['append'] ) ? ' <span>' . esc_html( $args['append'] ) . '</span>' : '' ); |
| 404 | } |
| 405 | |
| 406 | if ( ! empty ( $args['after_field'] ) ) |
| 407 | $html .= $args['after_field']; |
| 408 | |
| 409 | if ( ! empty ( $args['description'] ) ) |
| 410 | $html .= '<p class="description">' . $args['description'] . '</p>'; |
| 411 | |
| 412 | $html .= '</div>'; |
| 413 | |
| 414 | if ( ! empty( $args['return'] ) ) |
| 415 | return $html; |
| 416 | else |
| 417 | echo $html; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Validate settings field. |
| 422 | * |
| 423 | * @param mixed $value |
| 424 | * @param string $type |
| 425 | * @param array $args |
| 426 | * @return mixed |
| 427 | */ |
| 428 | public function validate_field( $value = null, $type = '', $args = [] ) { |
| 429 | if ( is_null( $value ) ) |
| 430 | return null; |
| 431 | |
| 432 | switch ( $type ) { |
| 433 | case 'boolean': |
| 434 | // possible value: 'true' or 'false' |
| 435 | $value = ( $value === 'true' || $value === true ); |
| 436 | break; |
| 437 | |
| 438 | case 'radio': |
| 439 | $value = is_array( $value ) ? false : sanitize_text_field( $value ); |
| 440 | break; |
| 441 | |
| 442 | case 'checkbox': |
| 443 | // possible value: 'empty' or array |
| 444 | if ( $value === 'empty' ) |
| 445 | $value = []; |
| 446 | else { |
| 447 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 448 | $value = array_map( 'sanitize_text_field', $value ); |
| 449 | $values = []; |
| 450 | |
| 451 | foreach ( $value as $single_value ) { |
| 452 | if ( array_key_exists( $single_value, $args['options'] ) ) |
| 453 | $values[] = $single_value; |
| 454 | } |
| 455 | |
| 456 | $value = $values; |
| 457 | } else |
| 458 | $value = []; |
| 459 | } |
| 460 | break; |
| 461 | |
| 462 | case 'number': |
| 463 | $value = (int) $value; |
| 464 | |
| 465 | // is value lower than? |
| 466 | if ( isset( $args['min'] ) && $value < $args['min'] ) |
| 467 | $value = $args['min']; |
| 468 | |
| 469 | // is value greater than? |
| 470 | if ( isset( $args['max'] ) && $value > $args['max'] ) |
| 471 | $value = $args['max']; |
| 472 | break; |
| 473 | |
| 474 | case 'info': |
| 475 | $value = ''; |
| 476 | break; |
| 477 | |
| 478 | case 'custom': |
| 479 | // do nothing |
| 480 | break; |
| 481 | |
| 482 | case 'input': |
| 483 | case 'select': |
| 484 | default: |
| 485 | $value = is_array( $value ) ? array_map( 'sanitize_text_field', $value ) : sanitize_text_field( $value ); |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | return stripslashes_deep( $value ); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Validate settings. |
| 494 | * |
| 495 | * @param array $input |
| 496 | * @return array |
| 497 | */ |
| 498 | public function validate_settings( $input ) { |
| 499 | // check capability |
| 500 | //@TODO dodac tu capability |
| 501 | // if ( ! current_user_can( apply_filters( 'rl_lightbox_settings_capability', 'edit_lightbox_settings' ) ) ) |
| 502 | // return $input; |
| 503 | |
| 504 | // check capability |
| 505 | if ( ! current_user_can( 'manage_options' ) ) |
| 506 | return $input; |
| 507 | |
| 508 | // check option page |
| 509 | if ( empty( $_POST['option_page'] ) ) |
| 510 | return $input; |
| 511 | |
| 512 | // try to get setting name and ID |
| 513 | foreach ( $this->settings as $id => $setting ) { |
| 514 | // tabs? |
| 515 | if ( is_array( $setting['option_name'] ) ) { |
| 516 | foreach ( $setting['option_name'] as $tab => $option_name ) { |
| 517 | // found valid setting? |
| 518 | if ( $option_name === $_POST['option_page'] ) { |
| 519 | // assign setting ID |
| 520 | $setting_id = $tab; |
| 521 | |
| 522 | // assign setting name |
| 523 | $setting_name = $option_name; |
| 524 | |
| 525 | // assign setting key |
| 526 | $setting_key = $id; |
| 527 | |
| 528 | // already found setting, no need to check the rest |
| 529 | break 2; |
| 530 | } |
| 531 | } |
| 532 | } else { |
| 533 | // found valid setting? |
| 534 | if ( $setting['option_name'] === $_POST['option_page'] ) { |
| 535 | // assign setting ID and key |
| 536 | $setting_key = $setting_id = $id; |
| 537 | |
| 538 | // assign setting name |
| 539 | $setting_name = $setting['option_name']; |
| 540 | |
| 541 | // already found setting, no need to check the rest |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // check setting id, no need to check $setting_name since it was at the same stage |
| 548 | if ( empty( $setting_id ) ) |
| 549 | return $input; |
| 550 | |
| 551 | // save settings |
| 552 | if ( isset( $_POST['save_' . $setting_name] ) ) { |
| 553 | $input = $this->validate_input_settings( $setting_id, $setting_key, $input ); |
| 554 | |
| 555 | add_settings_error( $setting_name, 'settings_saved', __( 'Settings saved.', $this->domain ), 'updated' ); |
| 556 | // reset settings |
| 557 | } elseif ( isset( $_POST['reset_' . $setting_name] ) ) { |
| 558 | // get default values |
| 559 | $input = $this->object->defaults[$setting_id]; |
| 560 | |
| 561 | // check custom reset functions |
| 562 | if ( ! empty( $this->settings[$setting_key]['fields'] ) ) { |
| 563 | foreach ( $this->settings[$setting_key]['fields'] as $field_id => $field ) { |
| 564 | // skip invalid tab field if any |
| 565 | if ( ! empty( $field['tab'] ) && $field['tab'] !== $setting_id ) |
| 566 | continue; |
| 567 | |
| 568 | // custom reset function? |
| 569 | if ( ! empty( $field['reset'] ) ) { |
| 570 | // valid function? no need to check "else" since all default values are already set |
| 571 | if ( $this->callback_function_exists( $field['reset'] ) ) { |
| 572 | if ( $field['type'] === 'custom' ) |
| 573 | $input = call_user_func( $field['reset'], $input, $field ); |
| 574 | else |
| 575 | $input[$field_id] = call_user_func( $field['reset'], $input[$field_id], $field ); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | add_settings_error( $setting_name, 'settings_restored', __( 'Settings restored to defaults.', $this->domain ), 'updated' ); |
| 582 | } |
| 583 | |
| 584 | return $input; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Validate input settings. |
| 589 | * |
| 590 | * @param string $setting_id |
| 591 | * @param array $input |
| 592 | * @return array |
| 593 | */ |
| 594 | public function validate_input_settings( $setting_id, $setting_key, $input ) { |
| 595 | if ( ! empty( $this->settings[$setting_key]['fields'] ) ) { |
| 596 | foreach ( $this->settings[$setting_key]['fields'] as $field_id => $field ) { |
| 597 | // skip saving this field? |
| 598 | if ( ! empty( $field['skip_saving'] ) ) |
| 599 | continue; |
| 600 | |
| 601 | // skip invalid tab field if any |
| 602 | if ( ! empty( $field['tab'] ) && $field['tab'] !== $setting_id ) |
| 603 | continue; |
| 604 | |
| 605 | // custom validate function? |
| 606 | if ( ! empty( $field['validate'] ) ) { |
| 607 | // valid function? |
| 608 | if ( $this->callback_function_exists( $field['validate'] ) ) { |
| 609 | if ( $field['type'] === 'custom' ) |
| 610 | $input = call_user_func( $field['validate'], $input, $field ); |
| 611 | else |
| 612 | $input[$field_id] = isset( $input[$field_id] ) ? call_user_func( $field['validate'], $input[$field_id], $field ) : $this->object->defaults[$setting_id][$field_id]; |
| 613 | } else |
| 614 | $input[$field_id] = $this->object->defaults[$setting_id][$field_id]; |
| 615 | } else { |
| 616 | // field data? |
| 617 | if ( isset( $input[$field_id] ) ) |
| 618 | $input[$field_id] = $this->validate_field( $input[$field_id], $field['type'], $field ); |
| 619 | else |
| 620 | $input[$field_id] = $this->object->defaults[$setting_id][$field_id]; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return $input; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Check whether callback is a valid function. |
| 630 | * |
| 631 | * @param string|array $callback |
| 632 | * @return bool |
| 633 | */ |
| 634 | public function callback_function_exists( $callback ) { |
| 635 | // function as array? |
| 636 | if ( is_array( $callback ) ) { |
| 637 | list( $object, $function ) = $callback; |
| 638 | |
| 639 | // check function existence |
| 640 | $function_exists = method_exists( $object, $function ); |
| 641 | // function as string? |
| 642 | } elseif ( is_string( $callback ) ) { |
| 643 | // check function existence |
| 644 | $function_exists = function_exists( $callback ); |
| 645 | } else |
| 646 | $function_exists = false; |
| 647 | |
| 648 | return $function_exists; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Get value based on minimum and maximum. |
| 653 | * |
| 654 | * @param array $data |
| 655 | * @param string $setting_name |
| 656 | * @param int $default |
| 657 | * @param int $min |
| 658 | * @param int $max |
| 659 | * @return void |
| 660 | */ |
| 661 | public function get_int_value( $data, $setting_name, $default, $min, $max ) { |
| 662 | // check existence of value |
| 663 | $value = array_key_exists( $setting_name, $data ) ? (int) $data[$setting_name] : $default; |
| 664 | |
| 665 | if ( $value > $max || $value < $min ) |
| 666 | $value = $default; |
| 667 | |
| 668 | return $value; |
| 669 | } |
| 670 | } |