> Keyed by site, since multisite code can switch sites. */ private array $cache = array(); /** * Default values. * * @return array */ public static function defaults(): array { return array( 'auto_optimize' => true, 'backup' => true, 'engine' => 'auto', 'batch_size' => 5, 'concurrency' => 1, 'preset' => self::PRESET_BALANCED, 'jpeg_lossless' => false, 'jpeg_quality' => 82, 'webp_quality' => 80, 'webp_lossless' => false, 'avif_quality' => 60, 'convert_webp' => false, 'convert_avif' => false, 'delivery' => 'picture', 'delivery_full_page' => true, 'exclude_gif' => false, 'use_binaries' => false, 'png_lossy' => false, 'png_quality' => 80, 'strip_metadata' => true, 'keep_copyright' => false, 'keep_icc' => false, 'smart_quality' => false, 'smart_target' => 0.98, 'resize_enabled' => false, 'resize_width' => 1920, 'resize_height' => 1920, 'min_savings' => 3, 'max_file_mb' => 30, 'timeout' => 60, 'retention_days' => 0, 'debug_log' => false, 'delete_on_uninstall' => false, ); } /** * The values each preset fixes. Anything not listed stays as configured. * * @return array> */ public static function presets(): array { return array( self::PRESET_LOSSLESS => array( 'jpeg_lossless' => true, 'jpeg_quality' => 90, 'webp_lossless' => true, 'webp_quality' => 80, 'avif_quality' => 80, 'png_lossy' => false, 'smart_quality' => false, ), self::PRESET_BALANCED => array( 'jpeg_lossless' => false, 'jpeg_quality' => 82, 'webp_lossless' => false, 'webp_quality' => 80, 'avif_quality' => 60, 'smart_quality' => false, ), self::PRESET_MAXIMUM => array( 'jpeg_lossless' => false, 'jpeg_quality' => 70, 'webp_lossless' => false, 'webp_quality' => 66, 'avif_quality' => 50, 'smart_quality' => false, ), ); } /** * Returns all settings, merged over the defaults. * * @return array */ public function all(): array { $blog = get_current_blog_id(); if ( ! isset( $this->cache[ $blog ] ) ) { $base = self::defaults(); if ( $this->is_network_managed() ) { $network = get_site_option( self::NETWORK_OPTION, array() ); $base = array_merge( $base, is_array( $network ) ? $network : array() ); } $stored = $this->is_network_managed() && ! $this->has_site_override() ? array() : get_option( self::OPTION, array() ); $this->cache[ $blog ] = array_merge( $base, is_array( $stored ) ? $stored : array() ); } return $this->cache[ $blog ]; } /** * Whether the plugin is network-activated, so network defaults apply. * * @return bool */ public function is_network_managed(): bool { if ( ! is_multisite() ) { return false; } $active = get_site_option( 'active_sitewide_plugins', array() ); return is_array( $active ) && isset( $active[ plugin_basename( PIXELKEEP_FILE ) ] ); } /** * Whether this site uses its own settings instead of the network defaults. * * @return bool */ public function has_site_override(): bool { return (bool) get_option( self::OVERRIDE_OPTION, false ); } /** * Returns the network defaults, merged over the built-in defaults. * * @return array */ public function network_values(): array { $network = get_site_option( self::NETWORK_OPTION, array() ); return array_merge( self::defaults(), is_array( $network ) ? $network : array() ); } /** * Saves the network defaults from the network settings form. * * @param mixed $input Raw form values. * @return void */ public function save_network( mixed $input ): void { update_site_option( self::NETWORK_OPTION, $this->clean_form_input( is_array( $input ) ? $input : array(), $this->network_values() ) ); $this->flush(); } /** * Returns one setting. * * @param string $key Setting name. * @return mixed */ public function get( string $key ): mixed { return $this->all()[ $key ] ?? null; } /** * Returns one setting as a boolean. * * @param string $key Setting name. * @return bool */ public function flag( string $key ): bool { return (bool) $this->get( $key ); } /** * Returns one setting as an integer. * * @param string $key Setting name. * @return int */ public function int( string $key ): int { $value = $this->get( $key ); return is_numeric( $value ) ? (int) $value : 0; } /** * Replaces the stored settings, sanitizing them first. * * @param array $values New values. * @return void */ public function save( array $values ): void { update_option( self::OPTION, $this->sanitize_values( $values, $this->all() ), false ); $this->flush(); } /** * Clears the in-memory copy, for example after another process changed the option. * * @return void */ public function flush(): void { $this->cache = array(); } /** * Fingerprint of every setting that changes encoded output. * * Re-optimizing with the same fingerprint is a no-op. * * @return string */ public function output_hash(): string { $settings = $this->all(); $keys = array( 'jpeg_lossless', 'jpeg_quality', 'webp_quality', 'webp_lossless', 'strip_metadata', 'keep_copyright', 'keep_icc', 'smart_quality', 'smart_target', 'use_binaries', 'png_lossy', 'png_quality', 'resize_enabled', 'resize_width', 'resize_height', 'min_savings', ); return $this->fingerprint_of( $keys ); } /** * Fingerprint of every setting that changes derivatives. * * @return string */ public function conversion_hash(): string { return $this->fingerprint_of( array( 'convert_webp', 'convert_avif', 'use_binaries', 'webp_quality', 'webp_lossless', 'avif_quality', 'strip_metadata', 'keep_copyright', 'keep_icc', 'min_savings' ) ); } /** * Fingerprints settings by meaning, not storage: `1`, `"1"` and `true` are the same setting. * * Values written by code or WP-CLI can differ in type from those the Settings page saves, * and a type change alone must not make every image look out of date. * * @param string[] $keys Settings to include. * @return string */ private function fingerprint_of( array $keys ): string { $settings = $this->all(); $defaults = self::defaults(); $relevant = array(); foreach ( $keys as $key ) { $value = $settings[ $key ] ?? null; if ( is_bool( $defaults[ $key ] ?? null ) ) { $value = (bool) $value; } elseif ( is_numeric( $value ) ) { $value = (float) $value; } $relevant[ $key ] = $value; } return substr( sha1( (string) wp_json_encode( $relevant ) ), 0, 16 ); } /** * Settings API sanitize callback. * * Backups can only be switched off when the confirmation field was sent, so a * crafted request or disabled JavaScript cannot turn them off silently. * * @param mixed $input Raw form values. * @return array */ public function sanitize( mixed $input ): array { $input = is_array( $input ) ? $input : array(); if ( $this->is_network_managed() ) { $override = ! empty( $input['site_override'] ); update_option( self::OVERRIDE_OPTION, $override, false ); $this->flush(); if ( ! $override ) { // Keep what the site saved earlier, in case it goes back to its own settings. $stored = get_option( self::OPTION, array() ); return is_array( $stored ) ? $stored : array(); } } $clean = $this->clean_form_input( $input, $this->all() ); $this->flush(); return $clean; } /** * Cleans values from a settings form, where an unchecked checkbox is simply absent. * * @param array $input Raw form values. * @param array $current Current values. * @return array */ private function clean_form_input( array $input, array $current ): array { $checkboxes = array( 'auto_optimize', 'backup', 'jpeg_lossless', 'webp_lossless', 'convert_webp', 'convert_avif', 'exclude_gif', 'use_binaries', 'png_lossy', 'strip_metadata', 'keep_copyright', 'keep_icc', 'smart_quality', 'resize_enabled', 'delivery_full_page', 'debug_log', 'delete_on_uninstall' ); foreach ( $checkboxes as $checkbox ) { $input[ $checkbox ] = ! empty( $input[ $checkbox ] ); } if ( empty( $input['backup'] ) && ! empty( $current['backup'] ) && empty( $input['confirm_disable_backup'] ) ) { $input['backup'] = true; add_settings_error( self::OPTION, 'pixelkeep_backup_kept', __( 'Backups stay on because disabling them was not confirmed.', 'pixelkeep' ), 'warning' ); } unset( $input['confirm_disable_backup'] ); return $this->sanitize_values( $input, $current ); } /** * Clamps and normalizes values, then applies the selected preset. * * @param array $input Values to clean. * @param array $fallback Values used for anything missing. * @return array */ private function sanitize_values( array $input, array $fallback ): array { $clean = array_merge( self::defaults(), $fallback ); foreach ( array( 'auto_optimize', 'backup', 'jpeg_lossless', 'webp_lossless', 'convert_webp', 'convert_avif', 'exclude_gif', 'use_binaries', 'png_lossy', 'strip_metadata', 'keep_copyright', 'keep_icc', 'smart_quality', 'resize_enabled', 'delivery_full_page', 'debug_log', 'delete_on_uninstall' ) as $flag ) { if ( array_key_exists( $flag, $input ) ) { $clean[ $flag ] = (bool) $input[ $flag ]; } } $ranges = array( 'batch_size' => array( 1, 50 ), 'concurrency' => array( 1, 5 ), 'jpeg_quality' => array( 30, 100 ), 'webp_quality' => array( 30, 100 ), 'avif_quality' => array( 20, 100 ), 'png_quality' => array( 40, 100 ), 'resize_width' => array( 320, 12000 ), 'resize_height' => array( 320, 12000 ), 'min_savings' => array( 0, 50 ), 'max_file_mb' => array( 1, 512 ), 'timeout' => array( 10, 600 ), 'retention_days' => array( 0, 3650 ), ); foreach ( $ranges as $key => $range ) { if ( isset( $input[ $key ] ) && is_numeric( $input[ $key ] ) ) { $clean[ $key ] = max( $range[0], min( $range[1], (int) $input[ $key ] ) ); } } if ( isset( $input['smart_target'] ) && is_numeric( $input['smart_target'] ) ) { $clean['smart_target'] = max( 0.90, min( 0.999, round( (float) $input['smart_target'], 3 ) ) ); } if ( isset( $input['engine'] ) && in_array( $input['engine'], array( 'auto', 'imagick', 'gd' ), true ) ) { $clean['engine'] = $input['engine']; } if ( isset( $input['delivery'] ) && in_array( $input['delivery'], array( 'picture', 'rewrite', 'off' ), true ) ) { $clean['delivery'] = $input['delivery']; } if ( isset( $input['preset'] ) && in_array( $input['preset'], array( self::PRESET_LOSSLESS, self::PRESET_BALANCED, self::PRESET_MAXIMUM, self::PRESET_CUSTOM ), true ) ) { $clean['preset'] = $input['preset']; } $presets = self::presets(); if ( isset( $presets[ $clean['preset'] ] ) ) { $clean = array_merge( $clean, $presets[ $clean['preset'] ] ); } return array_intersect_key( $clean, self::defaults() ); } }