0, 'class' => '' ), $atts, 'tablewright' ); return self::render( (int) $atts['id'], $atts['class'] ); } public static function render( $id, $extra_class = '' ) { $post = get_post( $id ); if ( ! $post || Tablewright_Plugin::CPT !== $post->post_type || 'trash' === $post->post_status ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html__( 'Tablewright: table not found.', 'tablewright' ) . '

' : ''; } $data = Tablewright_Data::get( $id ); if ( ! $data ) { return ''; } $html = self::render_data( $data, self::uid( $id ), $extra_class ); return apply_filters( 'tablewright_output', $html, $id, $data ); } /** Render table data (saved or unsaved) and enqueue what it needs. */ public static function render_data( $data, $uid, $extra_class = '' ) { wp_enqueue_style( 'tablewright-frontend' ); $s = $data['settings']; if ( $s['sort'] || $s['search'] || apply_filters( 'tablewright_needs_js', false, $data, self::layouts_for( $data ) ) ) { wp_enqueue_script( 'tablewright-frontend' ); } Tablewright_Fonts::enqueue_for( $data ); do_action( 'tablewright_render', $data, self::layouts_for( $data ) ); self::queue_css( $uid, $data ); return self::table_html( $uid, $data, $extra_class ); } /** The HTML id of a table on this page: twr-12 the first time, then twr-12-2, twr-12-3 for repeats. */ public static function uid( $id ) { $id = (int) $id; self::$seen[ $id ] = isset( self::$seen[ $id ] ) ? self::$seen[ $id ] + 1 : 1; return 'twr-' . $id . ( self::$seen[ $id ] > 1 ? '-' . self::$seen[ $id ] : '' ); } /** * Queues a table's CSS the WordPress way (wp_add_inline_style). In the page head when the * table is known before the head is printed (see Tablewright_Plugin::preload_for_current_post()), * otherwise with the late styles at the end of the page. */ public static function queue_css( $uid, $data ) { if ( isset( self::$styled[ $uid ] ) ) { return; } self::$styled[ $uid ] = true; $css = self::table_css( $uid, $data ); $handle = 'tablewright-frontend'; if ( wp_style_is( $handle, 'done' ) ) { $handle = 'tablewright-' . sanitize_key( $uid ); wp_register_style( $handle, false, array(), TABLEWRIGHT_VERSION ); } wp_enqueue_style( $handle ); wp_add_inline_style( $handle, self::esc_css( $css ) ); } /* ------------------------------------------------------------------ */ /* Escaping CSS at output */ /* ------------------------------------------------------------------ */ /** * Last pass over a finished stylesheet before it is printed: no HTML can survive in it (so it can't close the * style element), and no CSS at-import or expression either. Every value in it was already escaped one by one * (see css_color(), css_enum(), css_font()). * * @param string $css Stylesheet built by table_css() or an add-on. * @return string */ public static function esc_css( $css ) { $css = wp_strip_all_tags( (string) $css ); return (string) preg_replace( '/<\/?|@import|expression\s*\(|javascript\s*:/i', '', $css ); } /** A colour for CSS: a valid colour, else $fallback. */ public static function css_color( $v, $fallback ) { $c = Tablewright_Data::sanitize_color( is_scalar( $v ) ? $v : '' ); return '' !== $c ? $c : $fallback; } /** A keyword for CSS from an allow-list, else $fallback. */ public static function css_enum( $v, $allowed, $fallback ) { $v = is_scalar( $v ) ? (string) $v : ''; return in_array( $v, $allowed, true ) ? $v : $fallback; } /** A font-family stack for CSS, from a cleaned family name. */ public static function css_font( $family, $fallback ) { $family = Tablewright_Data::sanitize_font( is_scalar( $family ) ? $family : '' ); return '' !== $family ? Tablewright_Fonts::css_stack( $family ) : $fallback; } /** * Resolve the layouts and breakpoints a table uses right now. * Unknown layouts (a Pro layout while Pro is off) fall back gracefully. */ public static function layouts_for( $data ) { $s = $data['settings']; $global = Tablewright_Plugin::get_settings(); $ok = Tablewright_Data::available_layouts(); $phone = in_array( $s['resp'], $ok, true ) ? $s['resp'] : ( 'pin' === $s['resp'] ? 'scroll' : 'stack' ); /** * The layout tablets use: none in the free plugin (tablets show the desktop table). Tablewright Pro answers * this filter with the table's tablet layout. * * @param string $tablet Layout id, or '' for the desktop table. * @param array $s The table's settings. * @param array $ok Layout ids that may be used. */ $tablet = (string) apply_filters( 'tablewright_tablet_layout', '', $s, $ok ); $tablet = in_array( $tablet, $ok, true ) ? $tablet : ''; $bp = $s['bp'] ? (int) $s['bp'] : (int) $global['breakpoint']; $bp_t = $s['bp_t'] ? (int) $s['bp_t'] : (int) $global['breakpoint_t']; return array( 'p' => $phone, 't' => $tablet, 'bp' => $bp, 'bpt' => max( $bp_t, $bp + 1 ), ); } /* ------------------------------------------------------------------ */ /* CSS */ /* ------------------------------------------------------------------ */ public static function style_to_css( $st ) { $st = (array) $st; $css = ''; $schema = Tablewright_Data::style_schema(); $map = array( 'bg' => 'background-color', 'color' => 'color', 'fw' => 'font-weight', 'fst' => 'font-style', 'tt' => 'text-transform', 'tdec' => 'text-decoration', 'ta' => 'text-align', 'va' => 'vertical-align', 'ws' => 'white-space', ); foreach ( $map as $k => $prop ) { if ( ! isset( $st[ $k ] ) || '' === $st[ $k ] ) { continue; } // Escaped here, at output: colours must be valid colours, the rest must be one of the allowed keywords. $val = 'color' === $schema[ $k ] ? self::css_color( $st[ $k ], '' ) : self::css_enum( $st[ $k ], $schema[ $k ], '' ); if ( '' !== $val ) { $css .= $prop . ':' . $val . ';'; } } if ( ! empty( $st['ff'] ) ) { $css .= 'font-family:' . self::css_font( $st['ff'], 'inherit' ) . ';'; } if ( ! empty( $st['fs'] ) ) { $css .= 'font-size:' . (int) $st['fs'] . 'px;'; } if ( isset( $st['lh'] ) && '' !== $st['lh'] ) { $css .= 'line-height:' . (float) $st['lh'] . ';'; } if ( isset( $st['ls'] ) && '' !== $st['ls'] ) { $css .= 'letter-spacing:' . (float) $st['ls'] . 'px;'; } foreach ( array( 'pt' => 'padding-top', 'pr' => 'padding-right', 'pb' => 'padding-bottom', 'pl' => 'padding-left' ) as $k => $prop ) { if ( isset( $st[ $k ] ) && '' !== $st[ $k ] ) { $css .= $prop . ':' . (int) $st[ $k ] . 'px;'; } } if ( ! empty( $st['bs'] ) || ! empty( $st['bc'] ) || ( isset( $st['bw'] ) && '' !== $st['bw'] ) ) { $bw = isset( $st['bw'] ) && '' !== $st['bw'] ? (int) $st['bw'] : 1; $bs = self::css_enum( isset( $st['bs'] ) ? $st['bs'] : '', $schema['bs'], 'solid' ); $bc = self::css_color( isset( $st['bc'] ) ? $st['bc'] : '', 'var(--twr-bc)' ); $css .= 'border:' . $bw . 'px ' . $bs . ' ' . $bc . ';'; } return $css; } public static function table_css( $uid, $data ) { $s = $data['settings']; $sel = '#' . sanitize_html_class( $uid ); // Every value is escaped here, where the CSS is written, whatever the data says. $fw = array( '100', '200', '300', '400', '500', '600', '700', '800', '900' ); $v = array( '--twr-ff' => self::css_font( $s['ff'], 'inherit' ), '--twr-hff' => self::css_font( $s['h_ff'], 'var(--twr-ff)' ), '--twr-fs' => (int) $s['fs'] . 'px', '--twr-lh' => (float) $s['lh'], '--twr-c' => self::css_color( $s['color'], 'inherit' ), '--twr-bg' => self::css_color( $s['bg'], 'transparent' ), '--twr-hbg' => self::css_color( $s['h_bg'], 'transparent' ), '--twr-hc' => self::css_color( $s['h_color'], 'inherit' ), '--twr-hfw' => self::css_enum( $s['h_fw'], $fw, '600' ), '--twr-hfs' => (int) $s['h_fs'] ? (int) $s['h_fs'] . 'px' : 'inherit', '--twr-htt' => self::css_enum( $s['h_tt'], array( 'none', 'uppercase', 'lowercase', 'capitalize' ), 'none' ), '--twr-fbg' => self::css_color( $s['f_bg'], 'transparent' ), '--twr-fc' => self::css_color( $s['f_color'], 'inherit' ), '--twr-stripe' => self::css_color( $s['stripe_bg'], 'transparent' ), '--twr-hover' => self::css_color( $s['hover_bg'], 'transparent' ), '--twr-bc' => self::css_color( $s['bc'], 'transparent' ), '--twr-bw' => (int) $s['bw'] . 'px', '--twr-r' => (int) $s['radius'] . 'px', '--twr-pv' => (int) $s['pv'] . 'px', '--twr-ph' => (int) $s['ph'] . 'px', '--twr-ta' => self::css_enum( $s['ta'], array( 'left', 'center', 'right', 'justify' ), 'left' ), '--twr-va' => self::css_enum( $s['va'], array( 'top', 'middle', 'bottom' ), 'middle' ), '--twr-btn-bg' => self::css_color( $s['btn_bg'], '#1f6feb' ), '--twr-btn-c' => self::css_color( $s['btn_color'], '#fff' ), '--twr-btn-r' => (int) $s['btn_radius'] . 'px', ); $css = $sel . '{'; foreach ( $v as $k => $val ) { $css .= $k . ':' . $val . ';'; } if ( $s['max_w'] ) { $css .= 'max-width:' . (int) $s['max_w'] . 'px;'; } $css .= '}'; // Specificity grows per layer: column < row < cell. foreach ( $data['cols'] as $c => $col ) { if ( ! empty( $col['w'] ) ) { $css .= $sel . ' .twr-col-' . $c . '{width:' . (int) $col['w'] . 'px}'; } $rule = self::style_to_css( $col['s'] ); if ( $rule ) { $css .= $sel . ' .twr-table .twr-c-' . $c . '{' . $rule . '}'; } } foreach ( $data['rows'] as $r => $row ) { if ( ! empty( $row['h'] ) ) { $css .= $sel . ' .twr-tr-' . $r . '{height:' . (int) $row['h'] . 'px}'; } $rule = self::style_to_css( $row['s'] ); if ( $rule ) { $css .= $sel . ' .twr-table .twr-cell.twr-r-' . $r . '{' . $rule . '}'; } } foreach ( $data['cells'] as $r => $row ) { foreach ( $row as $c => $cell ) { if ( empty( $cell['m'] ) && ! empty( $cell['s'] ) ) { $rule = self::style_to_css( $cell['s'] ); if ( $rule ) { $k = '.twr-x-' . $r . '-' . $c; $css .= $sel . ' .twr-table .twr-cell' . $k . $k . '{' . $rule . '}'; } } } } $lay = self::layouts_for( $data ); $css .= '@media (max-width:' . (int) $lay['bp'] . 'px){' . self::layout_css( $lay['p'], $sel, $data, 'phone' ) . '}'; if ( $lay['t'] ) { $css .= '@media (min-width:' . ( (int) $lay['bp'] + 1 ) . 'px) and (max-width:' . (int) $lay['bpt'] . 'px){' . self::layout_css( $lay['t'], $sel, $data, 'tablet' ) . '}'; } return $css; } /** CSS for one layout, inside its media query. */ public static function layout_css( $layout, $sel, $data, $device ) { switch ( $layout ) { case 'scroll': return $sel . ' .twr-table{min-width:max(100%,600px)}'; case 'none': return $sel . ' .twr-table td,' . $sel . ' .twr-table th{overflow-wrap:anywhere}'; case 'stack': return self::stack_css( $sel ); } return (string) apply_filters( 'tablewright_layout_css', '', $layout, $sel, $data, $device ); } /** Shared "rows as cards" base, also used by the Pro accordion. */ public static function stack_css( $sel ) { $t = $sel . ' .twr-table'; return $t . ' thead{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}' . $t . ',' . $t . ' tbody,' . $t . ' tfoot,' . $t . ' tr{display:block;width:100%}' . $t . ' caption{display:block;width:100%}' . $t . ' tr{border-bottom:var(--twr-bw) solid var(--twr-bc)}' . $t . ' tr:last-child{border-bottom:0}' . $t . ' td{display:flex;justify-content:space-between;align-items:center;gap:1em;width:auto;height:auto;text-align:right;border:0}' . $t . ' td[data-label]::before{content:attr(data-label);font-weight:600;text-align:left;flex:0 1 45%}' . $t . ' tbody td:empty{display:none}' . $t . ' .twr-dup{display:flex}' . $sel . ' .twr-scroll{overflow:visible}'; } /* ------------------------------------------------------------------ */ /* HTML */ /* ------------------------------------------------------------------ */ public static function icon_html( $ic ) { $style = 'font-size:' . (int) $ic['size'] . 'px;'; $color = self::css_color( isset( $ic['color'] ) ? $ic['color'] : '', '' ); if ( '' !== $color ) { $style .= 'color:' . $color . ';'; } $svg = preg_replace( '/