|null */ protected static $cache = null; /** * Register hooks. */ public static function init() { add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); } /** * Default values. * * @return array */ public static function defaults() { return array( 'default_format' => 'code128', 'scale' => 2, 'height' => 60, 'text' => 1, 'bar_color' => '#000000', 'bg_color' => 'transparent', 'credit' => 0, 'credit_text' => '', ); } /** * Read all settings, merged with defaults. * * @return array */ public static function all() { if ( null === self::$cache ) { $stored = get_option( self::OPTION, array() ); self::$cache = wp_parse_args( is_array( $stored ) ? $stored : array(), self::defaults() ); } return self::$cache; } /** * Read one setting. * * @param string $key Setting key. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Register the settings screen fields. */ public static function register_settings() { register_setting( 'bcgn_settings', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => self::defaults(), ) ); } /** * Sanitize every submitted value. * * @param mixed $input Raw settings. * @return array */ public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $clean = self::defaults(); $formats = BCGN_Barcode::formats(); if ( isset( $input['default_format'] ) && isset( $formats[ $input['default_format'] ] ) ) { $clean['default_format'] = $input['default_format']; } if ( isset( $input['scale'] ) ) { $clean['scale'] = (string) max( 0.5, min( 10, (float) $input['scale'] ) ); } if ( isset( $input['height'] ) ) { $clean['height'] = (int) max( 10, min( 400, (int) $input['height'] ) ); } $clean['text'] = empty( $input['text'] ) ? 0 : 1; if ( isset( $input['bar_color'] ) ) { $clean['bar_color'] = BCGN_SVG::color( $input['bar_color'], '#000000' ); } if ( isset( $input['bg_color'] ) ) { $clean['bg_color'] = BCGN_SVG::color( $input['bg_color'], 'transparent' ); } $clean['credit'] = empty( $input['credit'] ) ? 0 : 1; $clean['credit_text'] = isset( $input['credit_text'] ) ? sanitize_text_field( $input['credit_text'] ) : ''; self::$cache = $clean; return $clean; } }