*/ public static function defaults() { return array( 'accent' => '', // Empty: theme primary color, else a hex color or a CSS custom property like var(--brand). 'style' => 'clean', // clean, striped, bordered, minimal. 'density' => 'comfortable', // comfortable, compact. 'radius' => 12, // Corner radius in px (0-24). 'header_style' => 'subtle', // subtle, accent. 'cards' => '600', // Container width below which rows become cards: 480, 600, 760 or 'off'. 'show_count' => 1, // Row counter next to the search box. 'search_placeholder' => '', // Empty: translated "Search this table". 'editor_role' => 'editor', // Who edits tables: admin, editor, author. 'css_mode' => 'file', // 'file' (stylesheet) or 'inline' (for sites that strip unused plugin CSS). 'dark_mode' => 'off', // 'off', 'auto' (prefers-color-scheme) or 'selector'. 'dark_selector' => '', // Used when dark_mode = 'selector', e.g. "html.dark". 'search_min' => 8, // Default for new tables: show search when the table has at least this many rows (0 = never). 'sortable' => 1, // Default for new tables. 'highlight_label' => '', // Empty: translated "Latest". 'compat_shortcode' => 0, // Also handle [table id=N] (TablePress-style) shortcodes. 'delete_on_uninstall' => 0, ); } /** @return array */ public static function all() { if ( null !== self::$overrides ) { return self::$overrides; } $saved = get_option( self::OPTION, array() ); return self::sanitize( is_array( $saved ) ? array_merge( self::defaults(), $saved ) : self::defaults() ); } /** @return mixed */ public static function get( $key ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : null; } /** @param array $values */ public static function update( array $values ) { update_option( self::OPTION, self::sanitize( array_merge( self::all(), $values ) ), true ); } /** * @param array $in * @return array */ public static function sanitize( array $in ) { $d = self::defaults(); $out = array(); $out['accent'] = self::sanitize_color( isset( $in['accent'] ) ? $in['accent'] : '' ); $out['css_mode'] = ( isset( $in['css_mode'] ) && 'inline' === $in['css_mode'] ) ? 'inline' : 'file'; $out['style'] = ( isset( $in['style'] ) && in_array( $in['style'], array( 'clean', 'striped', 'bordered', 'minimal' ), true ) ) ? $in['style'] : 'clean'; $out['density'] = ( isset( $in['density'] ) && 'compact' === $in['density'] ) ? 'compact' : 'comfortable'; $out['radius'] = isset( $in['radius'] ) ? max( 0, min( 24, (int) $in['radius'] ) ) : $d['radius']; $out['header_style'] = ( isset( $in['header_style'] ) && 'accent' === $in['header_style'] ) ? 'accent' : 'subtle'; $out['cards'] = ( isset( $in['cards'] ) && in_array( (string) $in['cards'], array( '480', '600', '760', 'off' ), true ) ) ? (string) $in['cards'] : '600'; $out['show_count'] = ( isset( $in['show_count'] ) && empty( $in['show_count'] ) ) ? 0 : 1; $out['search_placeholder'] = isset( $in['search_placeholder'] ) ? mb_substr( sanitize_text_field( (string) $in['search_placeholder'] ), 0, 60 ) : ''; $out['editor_role'] = ( isset( $in['editor_role'] ) && in_array( $in['editor_role'], array( 'admin', 'editor', 'author' ), true ) ) ? $in['editor_role'] : 'editor'; $out['dark_mode'] = ( isset( $in['dark_mode'] ) && in_array( $in['dark_mode'], array( 'off', 'auto', 'selector' ), true ) ) ? $in['dark_mode'] : 'off'; $out['dark_selector'] = self::sanitize_selector( isset( $in['dark_selector'] ) ? $in['dark_selector'] : '' ); $out['search_min'] = isset( $in['search_min'] ) ? max( 0, min( 1000, (int) $in['search_min'] ) ) : $d['search_min']; $out['sortable'] = empty( $in['sortable'] ) ? 0 : 1; $out['highlight_label'] = isset( $in['highlight_label'] ) ? mb_substr( sanitize_text_field( (string) $in['highlight_label'] ), 0, 40 ) : ''; $out['compat_shortcode'] = empty( $in['compat_shortcode'] ) ? 0 : 1; $out['delete_on_uninstall'] = empty( $in['delete_on_uninstall'] ) ? 0 : 1; return $out; } /** Hex color (#abc, #aabbcc) or var(--custom-property). Anything else becomes empty. */ public static function sanitize_color( $v ) { $v = trim( (string) $v ); if ( preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $v ) ) { return strtolower( $v ); } if ( preg_match( '/^var\(--[a-z0-9_-]{1,60}\)$/i', $v ) ) { return $v; } return ''; } /** A simple CSS selector list: letters, digits, spaces and . # - _ , > : [ ] = " ' only. */ public static function sanitize_selector( $v ) { $v = trim( (string) $v ); if ( '' === $v || strlen( $v ) > 120 ) { return ''; } return preg_match( '/^[a-zA-Z0-9_\-\.\#\s,>:\[\]="\']+$/', $v ) ? $v : ''; } /** The label shown on the highlighted first row. */ public static function highlight_label() { $l = (string) self::get( 'highlight_label' ); return '' !== $l ? $l : __( 'Latest', 'nitida-tables' ); } /** Capability needed to create and edit tables (settings always need manage_options). */ public static function capability() { $map = array( 'admin' => 'manage_options', 'editor' => 'publish_pages', 'author' => 'publish_posts' ); $role = (string) self::get( 'editor_role' ); return (string) apply_filters( 'nitida_tables_capability', isset( $map[ $role ] ) ? $map[ $role ] : 'publish_pages' ); } /** Settings with temporary overrides (used by the live preview on the settings screen). */ public static function with( array $overrides, callable $fn ) { self::$overrides = self::sanitize( array_merge( self::all(), $overrides ) ); try { return call_user_func( $fn ); } finally { self::$overrides = null; } } /** @var array|null */ private static $overrides = null; }