'color', 'color'=> 'color', 'ff' => 'font', 'fs' => 'num', 'fw' => array( '100', '200', '300', '400', '500', '600', '700', '800', '900' ), 'fst' => array( 'normal', 'italic' ), 'lh' => 'float', 'ls' => 'float', 'tt' => array( 'none', 'uppercase', 'lowercase', 'capitalize' ), 'tdec' => array( 'none', 'underline', 'line-through' ), 'ta' => array( 'left', 'center', 'right', 'justify' ), 'va' => array( 'top', 'middle', 'bottom' ), 'pt' => 'num', 'pr' => 'num', 'pb' => 'num', 'pl' => 'num', 'bc' => 'color', 'bw' => 'num', 'bs' => array( 'solid', 'dashed', 'dotted', 'double', 'none' ), 'ws' => array( 'normal', 'nowrap' ), ); } /** Is the Pro add-on active? */ public static function is_pro() { return (bool) apply_filters( 'tablewright_pro_active', false ); } /** * Small-screen layouts. Pro adds more through the filter. * * @return array id => [label, needs_pro] */ public static function layouts() { $free = array( 'scroll' => array( __( 'Scroll sideways', 'tablewright' ), false ), 'stack' => array( __( 'Stack as cards', 'tablewright' ), false ), 'none' => array( __( 'Squeeze to fit', 'tablewright' ), false ), ); $pro = array( 'accordion' => array( __( 'Accordion', 'tablewright' ), true ), 'compare' => array( __( 'Compare columns', 'tablewright' ), true ), 'hide' => array( __( 'Hide columns', 'tablewright' ), true ), 'pin' => array( __( 'Scroll, first column pinned', 'tablewright' ), true ), ); $all = array_merge( $free, $pro ); /** Pro marks its layouts as available by setting needs_pro to false. */ return apply_filters( 'tablewright_layouts', $all ); } /** Layout ids usable right now. */ public static function available_layouts() { return array_keys( array_filter( self::layouts(), function ( $l ) { return empty( $l[1] ); } ) ); } /** Table-level settings defaults (the neutral "Clean" look). */ public static function default_settings() { return apply_filters( 'tablewright_default_settings', self::base_default_settings() ); } public static function base_default_settings() { return array( 'ff' => '', 'fs' => 15, 'color' => '#1f2933', 'bg' => '#ffffff', 'lh' => 1.5, 'header_rows' => 1, 'footer_rows' => 0, 'h_ff' => '', 'h_bg' => '#f3f5f7', 'h_color' => '#111827', 'h_fw' => '600', 'h_fs' => 0, 'h_tt' => 'none', 'f_bg' => '#f3f5f7', 'f_color' => '#111827', 'stripe' => 0, 'stripe_bg' => '#f8f9fb', 'hover' => 1, 'hover_bg' => '#f1f5f9', 'first_col_bold' => 0, 'border_mode' => 'horizontal', // all | horizontal | outer | none 'bc' => '#e3e7ec', 'bw' => 1, 'radius' => 8, 'shadow' => 0, 'pv' => 12, 'ph' => 16, 'max_w' => 0, 'align' => 'left', 'ta' => 'left', 'va' => 'middle', 'caption' => '', 'caption_pos' => 'top', 'sort' => 0, 'search' => 0, 'search_ph' => 'Search…', 'resp' => 'scroll', // Phone layout. 'resp_t' => '', // Tablet layout ('' = same as desktop). Pro. 'bp' => 0, // Phone breakpoint (0 = site default). 'bp_t' => 0, // Tablet breakpoint (0 = site default). 'btn_bg' => '#1f6feb', 'btn_color' => '#ffffff', 'btn_radius' => 6, ); } /** Built-in style presets (partial settings merged over current). */ public static function builtin_presets() { $presets = array( array( 'id' => 'clean', 'name' => 'Clean', 'builtin' => 1, 'settings' => array( 'h_bg' => '#f3f5f7', 'h_color' => '#111827', 'color' => '#1f2933', 'bg' => '#ffffff', 'border_mode' => 'horizontal', 'bc' => '#e3e7ec', 'stripe' => 0, 'hover' => 1, 'hover_bg' => '#f1f5f9', 'radius' => 8, 'shadow' => 0, 'h_tt' => 'none', 'h_fw' => '600', 'h_fs' => 0, 'f_bg' => '#f3f5f7', 'f_color' => '#111827' ), ), array( 'id' => 'striped', 'name' => 'Striped', 'builtin' => 1, 'settings' => array( 'h_bg' => '#243447', 'h_color' => '#ffffff', 'color' => '#1f2933', 'bg' => '#ffffff', 'border_mode' => 'none', 'stripe' => 1, 'stripe_bg' => '#f2f5f8', 'hover' => 1, 'hover_bg' => '#e8eef5', 'radius' => 6, 'shadow' => 0, 'h_tt' => 'none', 'h_fw' => '600', 'h_fs' => 0, 'f_bg' => '#243447', 'f_color' => '#ffffff' ), ), array( 'id' => 'grid', 'name' => 'Full grid', 'builtin' => 1, 'settings' => array( 'h_bg' => '#ffffff', 'h_color' => '#111827', 'color' => '#1f2933', 'bg' => '#ffffff', 'border_mode' => 'all', 'bc' => '#cfd6de', 'stripe' => 0, 'hover' => 0, 'radius' => 0, 'shadow' => 0, 'h_tt' => 'none', 'h_fw' => '700', 'h_fs' => 0, 'f_bg' => '#ffffff', 'f_color' => '#111827' ), ), ); return apply_filters( 'tablewright_presets', $presets ); } public static function cell( $v = '', $extra = array() ) { return array_merge( array( 't' => 'text', 'v' => $v ), $extra ); } /** New blank table, styled with the site's default preset. */ public static function blank( $rows = 4, $cols = 4 ) { $rows = max( 1, min( 100, (int) $rows ) ); $cols = max( 1, min( 30, (int) $cols ) ); $data = array( 'settings' => self::default_settings(), 'rows' => array(), 'cols' => array(), 'cells' => array(), ); $global = Tablewright_Plugin::get_settings(); if ( ! empty( $global['default_preset'] ) ) { foreach ( self::builtin_presets() as $p ) { if ( $p['id'] === $global['default_preset'] ) { $data['settings'] = array_merge( $data['settings'], $p['settings'] ); } } } for ( $c = 0; $c < $cols; $c++ ) { $data['cols'][] = array( 's' => (object) array(), 'w' => 0 ); } for ( $r = 0; $r < $rows; $r++ ) { $data['rows'][] = array( 's' => (object) array(), 'h' => 0 ); $row = array(); for ( $c = 0; $c < $cols; $c++ ) { $row[] = self::cell( 0 === $r ? sprintf( 'Heading %d', $c + 1 ) : '' ); } $data['cells'][] = $row; } return $data; } /** Built-in starter templates. Pro adds more through the filter. */ public static function builtin_templates() { $hours = self::blank( 8, 2 ); $hours['cells'] = array( array( self::cell( 'Day' ), self::cell( 'Hours' ) ) ); foreach ( array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' ) as $d ) { $hours['cells'][] = array( self::cell( $d ), self::cell( '9:00 – 17:30' ) ); } $hours['cells'][] = array( self::cell( 'Saturday' ), self::cell( '10:00 – 14:00' ) ); $hours['cells'][] = array( self::cell( 'Sunday' ), self::cell( 'Closed', array( 's' => array( 'color' => '#b42318' ) ) ) ); $hours['settings']['max_w'] = 480; $hours['settings']['resp'] = 'none'; $templates = array( array( 'id' => 'hours', 'name' => __( 'Opening hours', 'tablewright' ), 'builtin' => 1, 'data' => $hours ), ); return apply_filters( 'tablewright_templates', $templates ); } /* ------------------------------------------------------------------ */ /* Load / save */ /* ------------------------------------------------------------------ */ public static function get( $post_id ) { $raw = get_post_meta( $post_id, self::META_KEY, true ); if ( ! $raw ) { return null; } $data = json_decode( $raw, true ); if ( ! is_array( $data ) || empty( $data['cells'] ) ) { return null; } // Never trust what is stored: clean it again on every read, so the editor and the front end only ever see // sanitized data, even if the meta was changed outside Tablewright. The round trip turns the empty style // objects back into arrays, as json_decode() gave them before. $clean = json_decode( wp_json_encode( self::sanitize_table( $data ) ), true ); return is_array( $clean ) && ! empty( $clean['cells'] ) ? $clean : null; } public static function save( $post_id, $data ) { $clean = self::sanitize_table( $data ); update_post_meta( $post_id, self::META_KEY, wp_slash( wp_json_encode( $clean ) ) ); return $clean; } /* ------------------------------------------------------------------ */ /* Sanitization */ /* ------------------------------------------------------------------ */ public static function sanitize_color( $v ) { $v = trim( (string) $v ); if ( '' === $v || 'transparent' === $v ) { return $v; } if ( preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $v ) ) { return strtolower( $v ); } if ( preg_match( '/^(rgb|hsl)a?\(\s*[\d.]+%?\s*,\s*[\d.]+%?\s*,\s*[\d.]+%?\s*(,\s*[\d.]+%?\s*)?\)$/i', $v ) ) { return strtolower( preg_replace( '/\s+/', '', $v ) ); } return ''; } public static function sanitize_font( $v ) { return trim( preg_replace( '/[^A-Za-z0-9 \-]/', '', (string) $v ) ); } public static function sanitize_style( $style ) { $out = array(); if ( ! is_array( $style ) ) { return $out; } foreach ( self::style_schema() as $key => $type ) { if ( ! isset( $style[ $key ] ) || '' === $style[ $key ] ) { continue; } $val = $style[ $key ]; if ( is_array( $type ) ) { if ( in_array( (string) $val, $type, true ) ) { $out[ $key ] = (string) $val; } } elseif ( 'color' === $type ) { $c = self::sanitize_color( $val ); if ( '' !== $c ) { $out[ $key ] = $c; } } elseif ( 'font' === $type ) { $f = self::sanitize_font( $val ); if ( '' !== $f ) { $out[ $key ] = $f; } } elseif ( 'num' === $type ) { $out[ $key ] = max( 0, min( 400, (int) $val ) ); } elseif ( 'float' === $type ) { $out[ $key ] = round( max( -10, min( 10, (float) $val ) ), 2 ); } } return $out; } public static function sanitize_settings( $s ) { $d = self::default_settings(); $s = is_array( $s ) ? $s : array(); $out = array(); $enum = array( 'h_fw' => array( '100', '200', '300', '400', '500', '600', '700', '800', '900' ), 'h_tt' => array( 'none', 'uppercase', 'lowercase', 'capitalize' ), 'border_mode' => array( 'all', 'horizontal', 'outer', 'none' ), 'align' => array( 'left', 'center', 'right' ), 'ta' => array( 'left', 'center', 'right', 'justify' ), 'va' => array( 'top', 'middle', 'bottom' ), 'caption_pos' => array( 'top', 'bottom' ), ); $colors = array( 'color', 'bg', 'h_bg', 'h_color', 'f_bg', 'f_color', 'stripe_bg', 'hover_bg', 'bc', 'btn_bg', 'btn_color' ); $bools = array( 'stripe', 'hover', 'first_col_bold', 'shadow', 'sort', 'search' ); $ints = array( 'fs' => array( 8, 72 ), 'header_rows' => array( 0, 10 ), 'footer_rows' => array( 0, 10 ), 'h_fs' => array( 0, 72 ), 'bw' => array( 0, 10 ), 'radius' => array( 0, 40 ), 'pv' => array( 0, 60 ), 'ph' => array( 0, 60 ), 'max_w' => array( 0, 4000 ), 'bp' => array( 0, 2000 ), 'bp_t' => array( 0, 3000 ), 'btn_radius' => array( 0, 99 ), ); foreach ( $d as $key => $default ) { $val = array_key_exists( $key, $s ) ? $s[ $key ] : $default; if ( isset( $enum[ $key ] ) ) { $out[ $key ] = in_array( (string) $val, $enum[ $key ], true ) ? (string) $val : $default; } elseif ( in_array( $key, $colors, true ) ) { $out[ $key ] = self::sanitize_color( $val ); } elseif ( in_array( $key, $bools, true ) ) { $out[ $key ] = $val ? 1 : 0; } elseif ( isset( $ints[ $key ] ) ) { $out[ $key ] = max( $ints[ $key ][0], min( $ints[ $key ][1], (int) $val ) ); } elseif ( 'lh' === $key ) { $out[ $key ] = round( max( 0.8, min( 4, (float) $val ) ), 2 ); } elseif ( in_array( $key, array( 'ff', 'h_ff' ), true ) ) { $out[ $key ] = self::sanitize_font( $val ); } elseif ( in_array( $key, array( 'resp', 'resp_t' ), true ) ) { // Keep unknown layout ids (a Pro layout saved while Pro is off); rendering falls back. $val = (string) $val; $out[ $key ] = preg_match( '/^[a-z]{0,16}$/', $val ) ? $val : $default; } elseif ( is_array( $default ) ) { $out[ $key ] = is_array( $val ) ? array_map( 'sanitize_text_field', array_slice( array_map( 'strval', $val ), 0, 100 ) ) : array(); } else { $out[ $key ] = sanitize_text_field( (string) $val ); } } // Preserve settings added by an add-on that isn't active right now, so nothing is lost on save. $extra = 0; foreach ( $s as $key => $val ) { if ( isset( $out[ $key ] ) || ! is_string( $key ) || ! preg_match( '/^[a-z][a-z0-9_]{0,23}$/', $key ) || ++$extra > 40 ) { continue; } if ( is_scalar( $val ) ) { $out[ $key ] = is_string( $val ) ? sanitize_text_field( substr( $val, 0, 200 ) ) : $val; } elseif ( is_array( $val ) ) { $out[ $key ] = array_map( 'sanitize_text_field', array_map( 'strval', array_filter( array_slice( $val, 0, 100 ), 'is_scalar' ) ) ); } } return apply_filters( 'tablewright_sanitize_settings', $out, $s ); } public static function allowed_inline_html() { $attr = array( 'class' => true ); return array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true, 'title' => true ), 'strong' => $attr, 'b' => $attr, 'em' => $attr, 'i' => $attr, 'u' => $attr, 's' => $attr, 'span' => $attr, 'small' => $attr, 'sup' => $attr, 'sub' => $attr, 'mark' => $attr, 'code' => $attr, 'br' => array(), ); } public static function allowed_svg() { $common = array( 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'fill-rule' => true, 'clip-rule' => true, 'opacity' => true, 'transform' => true, ); return array( 'svg' => array_merge( $common, array( 'xmlns' => true, 'viewbox' => true, 'width' => true, 'height' => true, 'aria-hidden' => true, 'focusable' => true, 'class' => true ) ), 'g' => $common, 'path' => array_merge( $common, array( 'd' => true ) ), 'circle' => array_merge( $common, array( 'cx' => true, 'cy' => true, 'r' => true ) ), 'ellipse' => array_merge( $common, array( 'cx' => true, 'cy' => true, 'rx' => true, 'ry' => true ) ), 'rect' => array_merge( $common, array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true ) ), 'line' => array_merge( $common, array( 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true ) ), 'polyline' => array_merge( $common, array( 'points' => true ) ), 'polygon' => array_merge( $common, array( 'points' => true ) ), ); } public static function sanitize_cell( $c ) { if ( ! is_array( $c ) ) { return self::cell(); } $out = array( 't' => isset( $c['t'] ) && in_array( $c['t'], array( 'text', 'button', 'image' ), true ) ? $c['t'] : 'text', 'v' => isset( $c['v'] ) ? wp_kses( (string) $c['v'], self::allowed_inline_html() ) : '', ); if ( ! empty( $c['m'] ) ) { return array( 't' => 'text', 'v' => '', 'm' => 1 ); } if ( ! empty( $c['url'] ) ) { $out['url'] = esc_url_raw( $c['url'] ); } if ( ! empty( $c['nt'] ) ) { $out['nt'] = 1; } if ( ! empty( $c['cs'] ) && (int) $c['cs'] > 1 ) { $out['cs'] = min( self::MAX_COLS, (int) $c['cs'] ); } if ( ! empty( $c['rs'] ) && (int) $c['rs'] > 1 ) { $out['rs'] = min( self::MAX_ROWS, (int) $c['rs'] ); } if ( ! empty( $c['ic'] ) && is_array( $c['ic'] ) && ! empty( $c['ic']['svg'] ) ) { $ic = $c['ic']; $out['ic'] = array( 'set' => sanitize_key( isset( $ic['set'] ) ? $ic['set'] : '' ), 'name' => sanitize_text_field( isset( $ic['name'] ) ? $ic['name'] : '' ), 'svg' => wp_kses( (string) $ic['svg'], self::allowed_svg() ), 'pos' => isset( $ic['pos'] ) && in_array( $ic['pos'], array( 'before', 'after', 'only', 'above' ), true ) ? $ic['pos'] : 'before', 'size' => max( 8, min( 160, (int) ( isset( $ic['size'] ) ? $ic['size'] : 18 ) ) ), 'color' => self::sanitize_color( isset( $ic['color'] ) ? $ic['color'] : '' ), ); } if ( ! empty( $c['img'] ) && is_array( $c['img'] ) && ! empty( $c['img']['url'] ) ) { $out['img'] = array( 'id' => absint( isset( $c['img']['id'] ) ? $c['img']['id'] : 0 ), 'url' => esc_url_raw( $c['img']['url'] ), 'alt' => sanitize_text_field( isset( $c['img']['alt'] ) ? $c['img']['alt'] : '' ), 'w' => max( 0, min( 2000, (int) ( isset( $c['img']['w'] ) ? $c['img']['w'] : 0 ) ) ), 'r' => max( 0, min( 999, (int) ( isset( $c['img']['r'] ) ? $c['img']['r'] : 0 ) ) ), ); } if ( ! empty( $c['b'] ) && is_array( $c['b'] ) ) { $b = $c['b']; $out['b'] = array( 'bg' => self::sanitize_color( isset( $b['bg'] ) ? $b['bg'] : '' ), 'color' => self::sanitize_color( isset( $b['color'] ) ? $b['color'] : '' ), 'bc' => self::sanitize_color( isset( $b['bc'] ) ? $b['bc'] : '' ), 'radius' => isset( $b['radius'] ) && '' !== $b['radius'] ? max( 0, min( 99, (int) $b['radius'] ) ) : '', 'size' => isset( $b['size'] ) && in_array( $b['size'], array( 'sm', 'md', 'lg' ), true ) ? $b['size'] : 'md', 'full' => empty( $b['full'] ) ? 0 : 1, ); } if ( ! empty( $c['s'] ) ) { $s = self::sanitize_style( $c['s'] ); if ( $s ) { $out['s'] = $s; } } return $out; } public static function sanitize_table( $data ) { $data = is_array( $data ) ? $data : array(); $cells = isset( $data['cells'] ) && is_array( $data['cells'] ) ? array_values( $data['cells'] ) : array(); $cells = array_slice( $cells, 0, self::MAX_ROWS ); $ncols = 1; foreach ( $cells as $row ) { $ncols = max( $ncols, is_array( $row ) ? count( $row ) : 0 ); } $ncols = min( self::MAX_COLS, $ncols ); $nrows = max( 1, count( $cells ) ); $out = array( 'settings' => self::sanitize_settings( isset( $data['settings'] ) ? $data['settings'] : array() ), 'rows' => array(), 'cols' => array(), 'cells' => array(), ); for ( $r = 0; $r < $nrows; $r++ ) { $src = isset( $data['rows'][ $r ] ) && is_array( $data['rows'][ $r ] ) ? $data['rows'][ $r ] : array(); $out['rows'][] = array( 's' => (object) self::sanitize_style( isset( $src['s'] ) ? (array) $src['s'] : array() ), 'h' => max( 0, min( 1000, (int) ( isset( $src['h'] ) ? $src['h'] : 0 ) ) ), ); $row = array(); for ( $c = 0; $c < $ncols; $c++ ) { $row[] = self::sanitize_cell( isset( $cells[ $r ][ $c ] ) ? $cells[ $r ][ $c ] : array() ); } $out['cells'][] = $row; } for ( $c = 0; $c < $ncols; $c++ ) { $src = isset( $data['cols'][ $c ] ) && is_array( $data['cols'][ $c ] ) ? $data['cols'][ $c ] : array(); $out['cols'][] = array( 's' => (object) self::sanitize_style( isset( $src['s'] ) ? (array) $src['s'] : array() ), 'w' => max( 0, min( 2000, (int) ( isset( $src['w'] ) ? $src['w'] : 0 ) ) ), ); } $out['cells'] = self::normalize_merges( $out['cells'] ); return $out; } /** Make merge data consistent: clip spans, flag covered cells, drop overlaps. */ public static function normalize_merges( $cells ) { $nr = count( $cells ); $nc = $nr ? count( $cells[0] ) : 0; $covered = array(); for ( $r = 0; $r < $nr; $r++ ) { for ( $c = 0; $c < $nc; $c++ ) { unset( $cells[ $r ][ $c ]['m'] ); } } for ( $r = 0; $r < $nr; $r++ ) { for ( $c = 0; $c < $nc; $c++ ) { if ( isset( $covered[ $r ][ $c ] ) ) { $cells[ $r ][ $c ] = array( 't' => 'text', 'v' => '', 'm' => 1 ); continue; } $cs = isset( $cells[ $r ][ $c ]['cs'] ) ? (int) $cells[ $r ][ $c ]['cs'] : 1; $rs = isset( $cells[ $r ][ $c ]['rs'] ) ? (int) $cells[ $r ][ $c ]['rs'] : 1; $cs = max( 1, min( $cs, $nc - $c ) ); $rs = max( 1, min( $rs, $nr - $r ) ); for ( $cc = 1; $cc < $cs; $cc++ ) { if ( isset( $covered[ $r ][ $c + $cc ] ) ) { $cs = $cc; break; } } unset( $cells[ $r ][ $c ]['cs'], $cells[ $r ][ $c ]['rs'] ); if ( $cs > 1 ) { $cells[ $r ][ $c ]['cs'] = $cs; } if ( $rs > 1 ) { $cells[ $r ][ $c ]['rs'] = $rs; } for ( $rr = 0; $rr < $rs; $rr++ ) { for ( $cc = 0; $cc < $cs; $cc++ ) { if ( $rr || $cc ) { $covered[ $r + $rr ][ $c + $cc ] = 1; } } } } } return $cells; } }