array( 'type' => 'bool', 'default' => true, ), 'instant_click' => array( 'type' => 'bool', 'default' => true, ), 'prefetch_mode' => array( 'type' => 'enum', 'default' => 'viewport', 'options' => array( 'hover', 'viewport', 'off' ), ), 'prefetch_delay' => array( 'type' => 'int', 'default' => 0, 'min' => 0, 'max' => 1000, ), 'cache_ttl' => array( 'type' => 'int', 'default' => 300, 'min' => 0, 'max' => 3600, ), 'transition' => array( 'type' => 'enum', 'default' => 'fade', 'options' => array( 'none', 'fade' ), ), 'progress_bar' => array( 'type' => 'bool', 'default' => false, ), 'progress_color' => array( 'type' => 'color', 'default' => '#0a3df0', ), 'loader_style' => array( 'type' => 'enum', 'default' => 'ring', 'options' => array( 'none', 'ring', 'dots', 'pulse' ), ), 'loader_delay' => array( 'type' => 'int', 'default' => 100, 'min' => 0, 'max' => 2000, ), 'loader_overlay' => array( 'type' => 'bool', 'default' => true, ), 'scroll_restore' => array( 'type' => 'bool', 'default' => true, ), 'sync_head' => array( 'type' => 'bool', 'default' => true, ), 'rerun_scripts' => array( 'type' => 'enum', 'default' => 'inline', 'options' => array( 'all', 'inline' ), ), 'bypass_admins' => array( 'type' => 'bool', 'default' => false, ), 'elementor_support' => array( 'type' => 'bool', 'default' => true, ), 'woocommerce_support' => array( 'type' => 'bool', 'default' => true, ), 'excluded_paths' => array( 'type' => 'list', 'format' => 'path', 'default' => array(), ), 'admin_ajax_navigation' => array( 'type' => 'bool', 'default' => false, ), 'admin_hover_preload' => array( 'type' => 'bool', 'default' => false, ), 'admin_excluded' => array( 'type' => 'list', 'default' => array(), ), 'remove_data_on_uninstall' => array( 'type' => 'bool', 'default' => false, ), ); /** * Filters the setting definitions, so add-ons (QuicklyFly Pro) can add settings or choices. * * @param array $schema Definitions keyed by setting name. */ return apply_filters( 'quicklyfly_settings_schema', $schema ); } /** * Central default values (including the schema version). * * @return array */ public function defaults() { $defaults = array( 'schema_version' => self::SCHEMA_VERSION ); foreach ( $this->schema() as $key => $definition ) { $defaults[ $key ] = $definition['default']; } return $defaults; } /** * Current settings merged over defaults. * * @return array */ public function get_all() { $stored = get_option( self::OPTION_NAME, array() ); return wp_parse_args( is_array( $stored ) ? $stored : array(), $this->defaults() ); } /** * Get. * * @param string $key Setting key. * @param mixed $fallback Fallback. * @return mixed */ public function get( $key, $fallback = null ) { $all = $this->get_all(); return array_key_exists( $key, $all ) ? $all[ $key ] : $fallback; } /** * Registers the option with the Settings API so every write is sanitized. * * @return void */ public function register() { register_setting( 'quicklyfly_settings_group', self::OPTION_NAME, array( 'type' => 'object', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => $this->defaults(), ) ); } /** * Validates and sanitizes a settings array. Keys missing from the input * fall back to their default; unknown keys are dropped. Idempotent. * * @param mixed $input Raw input. * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $clean = $this->defaults(); foreach ( $this->schema() as $key => $definition ) { if ( array_key_exists( $key, $input ) ) { $clean[ $key ] = $this->sanitize_value( $input[ $key ], $definition ); } } return $clean; } /** * Sanitize value. * * @param mixed $value Raw value. * @param array $definition Schema entry. * @return mixed */ private function sanitize_value( $value, $definition ) { switch ( $definition['type'] ) { case 'bool': return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); case 'int': if ( ! is_numeric( $value ) ) { return $definition['default']; } return max( $definition['min'], min( $definition['max'], (int) $value ) ); case 'enum': $value = is_scalar( $value ) ? sanitize_key( (string) $value ) : ''; return in_array( $value, $definition['options'], true ) ? $value : $definition['default']; case 'color': $color = is_scalar( $value ) ? sanitize_hex_color( (string) $value ) : null; return $color ? $color : $definition['default']; case 'text': $text = is_scalar( $value ) ? sanitize_text_field( wp_strip_all_tags( (string) $value ) ) : ''; return function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $definition['max'] ) : substr( $text, 0, $definition['max'] ); case 'url': $url = is_scalar( $value ) ? esc_url_raw( trim( (string) $value ), array( 'http', 'https' ) ) : ''; return $url ? $url : $definition['default']; case 'list': return $this->sanitize_list( $value, isset( $definition['format'] ) ? $definition['format'] : '' ); } return $definition['default']; } /** * Accepts an array or a newline separated string. Trims, normalizes, * de-duplicates and caps the list so a bad import cannot bloat the option. * * @param mixed $value Raw list. * @param string $format "path", "selector" or "". * @return string[] */ private function sanitize_list( $value, $format ) { if ( is_string( $value ) ) { $value = preg_split( '/\r\n|\r|\n/', $value ); } if ( ! is_array( $value ) ) { return array(); } $items = array(); foreach ( $value as $item ) { if ( ! is_scalar( $item ) ) { continue; } $item = trim( sanitize_text_field( wp_strip_all_tags( (string) $item ) ) ); if ( 'selector' === $format && preg_match( '/[<>{};\\\\]/', (string) $item ) ) { continue; } if ( 'path' === $format && '' !== $item && '/' !== $item[0] ) { $item = '/' . $item; } if ( '' !== $item && strlen( $item ) <= 200 ) { $items[] = $item; } } return array_slice( array_values( array_unique( $items ) ), 0, 'selector' === $format ? self::MAX_SELECTORS : self::MAX_ITEMS ); } /** * Schema description for the admin app (limits and allowed values only). * * @return array */ public function public_schema() { $schema = array(); foreach ( $this->schema() as $key => $definition ) { unset( $definition['default'] ); $schema[ $key ] = $definition; } return $schema; } /** * Saves a full or partial settings array on top of the stored values. * * @param array $input Partial or full settings. * @return array The settings that were stored. */ public function save( array $input ) { $clean = $this->sanitize( array_merge( $this->get_all(), $input ) ); update_option( self::OPTION_NAME, $clean ); return $clean; } /** * Restores every setting to its default. * * @return array */ public function reset() { $defaults = $this->defaults(); update_option( self::OPTION_NAME, $defaults ); return $defaults; } /** * Settings as a portable document (export and import share this format). * * @return array */ public function export() { return array( 'plugin' => 'quicklyfly', 'schema' => self::SCHEMA_VERSION, 'exported_at' => gmdate( 'c' ), 'settings' => $this->get_all(), ); } /** * Export json. * * @return string The export document as pretty-printed JSON. */ public function export_json() { return wp_json_encode( $this->export(), JSON_PRETTY_PRINT ); } /** * Imports a document produced by export_json(). Every value is validated like a * normal save and keys the file does not mention fall back to their defaults. * A rejected file changes nothing. * * @param string $json Document text. * @return array|WP_Error Stored settings, or an error when the file is not a QuicklyFly export. */ public function import_json( $json ) { $document = json_decode( (string) $json, true ); if ( ! is_array( $document ) || ! isset( $document['plugin'], $document['settings'] ) || 'quicklyfly' !== $document['plugin'] || ! is_array( $document['settings'] ) ) { return new WP_Error( 'quicklyfly_bad_import', __( 'This file is not a QuicklyFly settings export.', 'quicklyfly' ) ); } $clean = $this->sanitize( $document['settings'] ); update_option( self::OPTION_NAME, $clean ); return $clean; } /** * Adds defaults on first run without touching existing values. Idempotent. * * @return void */ public function ensure_defaults() { $stored = get_option( self::OPTION_NAME, null ); if ( ! is_array( $stored ) ) { add_option( self::OPTION_NAME, $this->defaults() ); return; } $this->maybe_migrate(); } /** * Upgrades stored settings when their schema is older than the current one. * User values are preserved; only missing keys are added. Idempotent. * * Schema history: * 1 - initial release (enabled, prefetch, integrations, exclusions). * 2 - adds cache, motion, script handling, persistent elements, analytics * and admin navigation settings. * 3 - the "Fastest" setup became the default. Sites that still run the old * default setup (nothing changed by the user) are moved to it. * * @return void */ public function maybe_migrate() { $stored = get_option( self::OPTION_NAME, null ); if ( ! is_array( $stored ) ) { return; } $version = isset( $stored['schema_version'] ) ? (int) $stored['schema_version'] : 0; if ( $version >= self::SCHEMA_VERSION ) { return; } // Version 3: only an untouched copy of the old default setup is moved to the new one. if ( $version < 3 ) { $old = array( 'prefetch_mode' => 'hover', 'prefetch_delay' => 65, 'cache_ttl' => 60, 'rerun_scripts' => 'all', ); $new = array( 'prefetch_mode' => 'viewport', 'prefetch_delay' => 0, 'cache_ttl' => 300, 'rerun_scripts' => 'inline', ); $untouched = true; foreach ( $old as $key => $value ) { if ( isset( $stored[ $key ] ) && $stored[ $key ] !== $value ) { $untouched = false; } } if ( $untouched ) { $stored = array_merge( $stored, $new ); } } // Version 4: a site still on the old default loader (progress bar only) moves to the new default, the spinner. if ( $version < 4 && 'none' === ( isset( $stored['loader_style'] ) ? $stored['loader_style'] : 'none' ) && ! empty( $stored['progress_bar'] ) ) { $stored['loader_style'] = 'ring'; $stored['progress_bar'] = false; } // Version 5: instant navigation in wp-admin is off by default; switch it (and its hover preload) off once. if ( $version < 5 ) { $stored['admin_ajax_navigation'] = false; $stored['admin_hover_preload'] = false; } // Existing values win; defaults only fill what is missing. Future // migrations that rename or reshape keys go here, oldest first. $migrated = $this->sanitize( wp_parse_args( $stored, $this->defaults() ) ); $migrated['schema_version'] = self::SCHEMA_VERSION; update_option( self::OPTION_NAME, $migrated ); } }