*/ public static function defaults() { return array( 'quality' => 82, 'max_dimension' => 0, 'delete_originals' => 1, 'create_backups' => 0, 'convert_on_upload' => 0, ); } /** * Create the option on activation when it does not exist yet. * * @return void */ public static function install() { if ( false === get_option( self::OPTION, false ) ) { add_option( self::OPTION, self::defaults(), '', true ); } } /** * Register the setting with WordPress. * * @return void */ public static function register() { add_action( 'admin_init', array( self::class, 'register_setting' ) ); } /** * Register the option and its sanitizer. * * @return void */ public static function register_setting() { register_setting( 'flipwebp_settings_group', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( self::class, 'sanitize' ), 'default' => self::defaults(), 'show_in_rest' => false, ) ); } /** * Saved settings merged onto defaults. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } $settings = wp_parse_args( $saved, self::defaults() ); return self::normalize( $settings ); } /** * One boolean setting. * * @param string $key Setting key. * @return bool */ public static function enabled( $key ) { $all = self::all(); return ! empty( $all[ $key ] ); } /** * Quality value used for encoding, after the developer filter. * * @param int $attachment_id Attachment ID. * @return int */ public static function quality( $attachment_id = 0 ) { $quality = (int) self::all()['quality']; /** * Filters the WebP quality for one conversion. * * Values are clamped to 40–100. * * @param int $quality Quality from 40 to 100. * @param int $attachment_id Attachment ID. */ $quality = (int) apply_filters( 'flipwebp_quality', $quality, (int) $attachment_id ); return min( 100, max( 40, $quality ) ); } /** * Longest-side resize limit. Zero keeps the current pixel size. * * @param int $attachment_id Attachment ID. * @return int */ public static function max_dimension( $attachment_id = 0 ) { $max = (int) self::all()['max_dimension']; /** * Filters the maximum width or height in pixels. * * Zero disables resizing. Positive values are clamped to 320–12000. * * @param int $max Maximum dimension. * @param int $attachment_id Attachment ID. */ $max = (int) apply_filters( 'flipwebp_max_dimension', $max, (int) $attachment_id ); if ( $max < 1 ) { return 0; } return min( 12000, max( 320, $max ) ); } /** * Sanitize the settings form. * * The Settings API unslashes this value before the callback runs. * * @param mixed $input Raw setting value. * @return array */ public static function sanitize( $input ) { $output = self::defaults(); if ( ! is_array( $input ) ) { return $output; } $output['quality'] = isset( $input['quality'] ) ? absint( $input['quality'] ) : $output['quality']; $output['max_dimension'] = isset( $input['max_dimension'] ) ? absint( $input['max_dimension'] ) : 0; $output['delete_originals'] = empty( $input['delete_originals'] ) ? 0 : 1; $output['create_backups'] = empty( $input['create_backups'] ) ? 0 : 1; $output['convert_on_upload'] = empty( $input['convert_on_upload'] ) ? 0 : 1; return self::normalize( $output ); } /** * Clamp a settings array to the allowed ranges. * * @param array $settings Settings to clamp. * @return array */ private static function normalize( $settings ) { $quality = isset( $settings['quality'] ) ? absint( $settings['quality'] ) : 82; $max = isset( $settings['max_dimension'] ) ? absint( $settings['max_dimension'] ) : 0; if ( $quality < 40 ) { $quality = 40; } elseif ( $quality > 100 ) { $quality = 100; } if ( $max > 0 && $max < 320 ) { $max = 320; } elseif ( $max > 12000 ) { $max = 12000; } return array( 'quality' => $quality, 'max_dimension' => $max, 'delete_originals' => empty( $settings['delete_originals'] ) ? 0 : 1, 'create_backups' => empty( $settings['create_backups'] ) ? 0 : 1, 'convert_on_upload' => empty( $settings['convert_on_upload'] ) ? 0 : 1, ); } }