*/ public static function defaults(): array { return array( 'default_conflict' => Conflict_Resolver::SKIP, 'match_by' => 'slug', 'restore_retention_days' => 30, 'full_site_packages' => 3, 'capability' => 'manage_options', 'delete_on_uninstall' => false, ); } /** * Every setting, defaults filled in. * * @return array */ public static function all(): array { $stored = get_option( self::OPTION ); return array_merge( self::defaults(), is_array( $stored ) ? $stored : array() ); } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function get( string $key ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : null; } /** * Write settings, dropping anything not in the schema. * * @param array $values Incoming values. * @return array The settings as stored. */ public static function update( array $values ): array { $current = self::all(); foreach ( $values as $key => $value ) { if ( ! array_key_exists( $key, self::defaults() ) ) { continue; } $current[ $key ] = self::sanitize( (string) $key, $value ); } update_option( self::OPTION, $current ); return $current; } /** * The capability a request must hold, never weaker than the floor. * * @return string */ public static function capability(): string { $capability = (string) self::get( 'capability' ); return in_array( $capability, self::CAPABILITIES, true ) ? $capability : 'manage_options'; } /** * Coerce one value into its allowed shape. * * @param string $key Setting name. * @param mixed $value Incoming value. * @return mixed */ private static function sanitize( string $key, $value ) { switch ( $key ) { case 'default_conflict': $value = sanitize_key( (string) $value ); return in_array( $value, Conflict_Resolver::strategies(), true ) ? $value : Conflict_Resolver::SKIP; case 'match_by': return 'slug'; case 'restore_retention_days': return max( 1, min( 365, (int) $value ) ); case 'full_site_packages': return max( 1, min( Job_Store::LIMIT, (int) $value ) ); case 'capability': $value = sanitize_key( (string) $value ); return in_array( $value, self::CAPABILITIES, true ) ? $value : 'manage_options'; case 'delete_on_uninstall': return (bool) $value; default: return $value; } } }