array(), // Empty = every role that can upload files. 'replace_enabled' => true, 'replace_roles' => array( 'administrator' ), 'default_format' => 'keep', 'quality' => 90, 'allowed_formats' => array( 'image/jpeg', 'image/png', 'image/webp' ), 'filename_suffix' => '-edited', 'keep_alt' => true, 'drafts_enabled' => false, 'remove_on_uninstall' => false, 'debug' => false, ); } /** * Current settings merged over defaults. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); return wp_parse_args( is_array( $saved ) ? $saved : array(), self::defaults() ); } /** * One setting. * * @param string $key Key. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Sanitize a submitted settings array. * * @param mixed $in Raw input. * @return array */ public static function sanitize( $in ) { $d = self::defaults(); $in = is_array( $in ) ? $in : array(); $out = array(); $roles = array_keys( wp_roles()->roles ); $out['roles'] = array_values( array_intersect( array_map( 'sanitize_key', (array) ( isset( $in['roles'] ) ? $in['roles'] : array() ) ), $roles ) ); $out['replace_enabled'] = ! empty( $in['replace_enabled'] ); $out['replace_roles'] = array_values( array_intersect( array_map( 'sanitize_key', (array) ( isset( $in['replace_roles'] ) ? $in['replace_roles'] : array() ) ), $roles ) ); $formats = array( 'keep', 'image/jpeg', 'image/png', 'image/webp' ); $out['default_format'] = in_array( isset( $in['default_format'] ) ? $in['default_format'] : '', $formats, true ) ? $in['default_format'] : $d['default_format']; $out['quality'] = max( 30, min( 100, absint( isset( $in['quality'] ) ? $in['quality'] : $d['quality'] ) ) ); $allowed = array_values( array_intersect( (array) ( isset( $in['allowed_formats'] ) ? $in['allowed_formats'] : array() ), array( 'image/jpeg', 'image/png', 'image/webp' ) ) ); $out['allowed_formats'] = $allowed ? $allowed : array( 'image/jpeg', 'image/png' ); // sanitize_file_name() would strip the leading dash of "-edited"; allow only safe characters instead. $suffix = isset( $in['filename_suffix'] ) ? preg_replace( '/[^A-Za-z0-9._-]/', '', (string) $in['filename_suffix'] ) : $d['filename_suffix']; $out['filename_suffix'] = substr( $suffix, 0, 40 ); $out['keep_alt'] = ! empty( $in['keep_alt'] ); $out['drafts_enabled'] = ! empty( $in['drafts_enabled'] ); $out['remove_on_uninstall'] = ! empty( $in['remove_on_uninstall'] ); $out['debug'] = ! empty( $in['debug'] ); return $out; } /** * Activation: store defaults once. Never touches media. */ public static function on_activate() { if ( false === get_option( self::OPTION, false ) ) { add_option( self::OPTION, self::defaults(), '', false ); } } }