3, 'timeout' => 60, 'urls' => array(), // Empty = automatic defaults. 'history' => 20, 'cookie_ttl' => 120, 'mode' => 'auto', 'remote_head' => false, 'warmup' => true, 'keep_data' => false, 'expected_slugs' => array(), ); } /** * All settings. */ public static function get_all(): array { if ( null === self::$cache ) { $stored = get_option( self::OPTION, array() ); self::$cache = array_merge( self::defaults(), is_array( $stored ) ? $stored : array() ); } return self::$cache; } /** * One setting. * * @param string $key Key. * @param mixed $fallback Fallback. * @return mixed */ public static function get( string $key, $fallback = null ) { $all = self::get_all(); return $all[ $key ] ?? $fallback; } /** * Flush cache. */ public static function flush(): void { self::$cache = null; } /** * Save defaults when missing. */ public static function ensure_defaults(): void { if ( false === get_option( self::OPTION ) ) { update_option( self::OPTION, self::defaults(), false ); } self::flush(); } /** * Update (sanitised). * * @param array $input Input. */ public static function update( array $input ): array { $clean = array_merge( self::get_all(), self::sanitize( $input ) ); update_option( self::OPTION, $clean, false ); self::flush(); return self::get_all(); } /** * Sanitise. * * @param array $input Input. */ public static function sanitize( array $input ): array { $out = array(); if ( isset( $input['passes'] ) ) { $out['passes'] = max( 1, min( 10, (int) $input['passes'] ) ); } if ( isset( $input['timeout'] ) ) { $out['timeout'] = max( 10, min( 300, (int) $input['timeout'] ) ); } if ( isset( $input['history'] ) ) { $out['history'] = max( 1, min( 100, (int) $input['history'] ) ); } if ( isset( $input['cookie_ttl'] ) ) { $out['cookie_ttl'] = max( 30, min( 900, (int) $input['cookie_ttl'] ) ); } if ( isset( $input['mode'] ) ) { $out['mode'] = 'browse' === $input['mode'] ? 'browse' : 'auto'; } foreach ( array( 'remote_head', 'warmup', 'keep_data' ) as $b ) { if ( isset( $input[ $b ] ) ) { $out[ $b ] = rest_sanitize_boolean( $input[ $b ] ); // Form-encoded "false" / "0" are false. } } if ( isset( $input['urls'] ) ) { $urls = array(); foreach ( (array) $input['urls'] as $u ) { $u = esc_url_raw( trim( (string) $u ) ); if ( '' !== $u ) { $urls[] = $u; } } $out['urls'] = array_values( array_unique( array_slice( $urls, 0, 20 ) ) ); } if ( isset( $input['expected_slugs'] ) ) { $out['expected_slugs'] = array_values( array_filter( array_map( 'sanitize_text_field', (array) $input['expected_slugs'] ) ) ); } return $out; } /** * Capability. */ public static function capability(): string { return (string) apply_filters( 'bloatwright_capability', 'manage_options' ); } }