…, …options ). Types: bool, int, float, * enum, color, url, text, textarea, selectors, html_classes, html_id, * css, css_length, duration, bar_weight, font_weight, id_list, * ref_list, special_list. * * @return array */ public static function schema() { $schema = array( 'enable_navigation' => array( 'type' => 'bool' ), 'disable_for_admins' => array( 'type' => 'bool' ), 'bypass_auth_forms' => array( 'type' => 'bool' ), 'ignore_links' => array( 'type' => 'textarea' ), 'loader_type' => array( 'type' => 'enum', 'values' => array( 'progressbar', 'spinner', 'none' ), ), 'progressbar' => array( 'type' => 'bool' ), 'loader' => array( 'type' => 'bool' ), 'scroll_to_top' => array( 'type' => 'bool' ), 'custom_css' => array( 'type' => 'css' ), 'cf_exclude_urls' => array( 'type' => 'textarea' ), 'cf_exclude_posts' => array( 'type' => 'id_list' ), 'cf_exclude_terms' => array( 'type' => 'ref_list' ), 'cf_exclude_special' => array( 'type' => 'special_list' ), ); /** * Filters the setting types, so add-ons can declare their own. * * @param array $schema Rules keyed by setting name. */ return apply_filters( 'ajaxpress_settings_schema', $schema ); } /** * Clean a set of settings about to be saved. * * @param array $settings Settings to save. * @param array $defaults Defaults, the fallback for invalid values. * @return array Only settings with a type, each cleaned. */ public static function sanitize( $settings, $defaults ) { if ( ! is_array( $settings ) ) { return array(); } $schema = self::schema(); $clean = array(); foreach ( $settings as $key => $value ) { if ( ! isset( $schema[ $key ] ) ) { continue; } $default = isset( $defaults[ $key ] ) ? $defaults[ $key ] : null; $clean[ $key ] = self::sanitize_value( $value, $schema[ $key ], $default ); } return $clean; } /** * Clean one value by its rule. * * @param mixed $value Value. * @param array $rule Rule. * @param mixed $fallback Value to use when it does not fit. * @return mixed */ public static function sanitize_value( $value, $rule, $fallback ) { switch ( $rule['type'] ) { case 'bool': return (bool) wp_validate_boolean( $value ); case 'int': if ( ! is_numeric( $value ) ) { return $fallback; } return self::clamp( (int) round( (float) $value ), $rule ); case 'float': if ( ! is_numeric( $value ) ) { return $fallback; } return self::clamp( (float) $value, $rule ); case 'enum': $value = is_scalar( $value ) ? (string) $value : ''; return in_array( $value, $rule['values'], true ) ? $value : $fallback; case 'color': $value = is_string( $value ) ? trim( $value ) : ''; return self::is_color( $value ) ? $value : $fallback; case 'url': return is_string( $value ) ? esc_url_raw( trim( $value ) ) : $fallback; case 'text': return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : $fallback; case 'textarea': case 'selectors': return is_scalar( $value ) ? sanitize_textarea_field( (string) $value ) : $fallback; case 'html_classes': if ( ! is_scalar( $value ) ) { return $fallback; } $classes = array_filter( array_map( 'sanitize_html_class', preg_split( '/\s+/', (string) $value ) ) ); return implode( ' ', $classes ); case 'html_id': return is_scalar( $value ) ? sanitize_html_class( (string) $value ) : $fallback; case 'css': // CSS has no use for markup, and a tag is the only way out of // the