$col, 'label' => $col ); } $type = isset( $col['type'] ) ? (string) $col['type'] : 'text'; if ( ! in_array( $type, self::TYPES, true ) && ! in_array( $type, self::INTERACTIVE_TYPES, true ) ) { $type = 'text'; } if ( in_array( $type, array( 'control', 'expander', 'actions', 'selector' ), true ) ) { $has_sort_value = ( 'control' === $type && ! empty( $col['sort_key'] ) ); if ( ! $has_sort_value ) { $col['sortable'] = false; } } return array_merge( array( 'key' => '', 'label' => '', 'type' => 'text', 'align' => '', 'nowrap' => false, 'tone' => '', 'tone_key' => '', 'width' => '', 'subline' => '', 'suffix' => '', 'empty' => null, 'empty_zero' => false, 'truncate' => 0, 'tooltip' => false, 'header_tip' => '', 'header_html' => '', 'sr_label' => '', 'sortable' => true, 'sort_key' => '', 'span' => null, 'group_span' => false, 'bands' => array(), 'format' => array(), ), $col, array( 'type' => $type ) ); } public static function html_hatch_count() { return self::$html_hatch; } public static function control_count() { return self::$control_cells; } public static function is_fn( $v ) { if ( is_string( $v ) ) { return false; } return ( $v instanceof Closure ) || ( is_array( $v ) && is_callable( $v ) ) || ( is_object( $v ) && method_exists( $v, '__invoke' ) ); } public static function value( $row, $key, $default = null ) { if ( '' === $key || null === $key ) { return $default; } if ( is_array( $row ) ) { return array_key_exists( $key, $row ) ? $row[ $key ] : $default; } if ( is_object( $row ) ) { return isset( $row->$key ) ? $row->$key : $default; } return $default; } public static function resolve( $row, $spec, $default = null ) { if ( self::is_fn( $spec ) ) { return call_user_func( $spec, $row ); } if ( ! is_string( $spec ) || '' === $spec ) { return $default; } $has = is_array( $row ) ? array_key_exists( $spec, $row ) : ( is_object( $row ) && isset( $row->$spec ) ); if ( ! $has && 1 === preg_match( '/^aieo_[a-z0-9_]+$/', $spec ) && function_exists( $spec ) ) { return call_user_func( $spec, $row ); } return self::value( $row, $spec, $default ); } public static function is_empty( $v, $col ) { if ( null === $v ) { return true; } if ( is_string( $v ) && '' === trim( $v ) ) { return true; } if ( is_array( $v ) && ! $v ) { return true; } $type = isset( $col['type'] ) ? $col['type'] : ''; if ( in_array( $type, array( 'date', 'datetime', 'relative', 'duration' ), true ) ) { $s = trim( (string) $v ); if ( '0000-00-00' === substr( $s, 0, 10 ) ) { return true; } } if ( ! empty( $col['empty_zero'] ) && ( 0 === $v || '0' === $v || 0.0 === $v ) ) { return true; } return false; } public static function money( $amount ) { return array( 'html' => self::render( array( 'key' => 'v', 'type' => 'money' ), array( 'v' => (float) $amount ) ) ); } public static function render( $col, $row, $ctx = array() ) { $col = self::normalise( $col ); $raw = self::value( $row, $col['key'] ); if ( in_array( $col['type'], self::INTERACTIVE_TYPES, true ) ) { return self::render_interactive( $col, $row, $raw, $ctx ); } if ( self::is_empty( $raw, $col ) && 'html' !== $col['type'] ) { return self::empty_marker( $col ); } $html = self::render_value( $col['type'], $raw, $col, $row ); return self::decorate( $html, $col, $row, $raw ); } private static function empty_marker( $col ) { $marker = ( null === $col['empty'] ) ? self::EMPTY_MARKER : (string) $col['empty']; if ( '' === $marker ) { return ''; } return '' . esc_html( $marker ) . ''; } private static function render_value( $type, $v, $col, $row ) { $f = is_array( $col['format'] ) ? $col['format'] : array(); switch ( $type ) { case 'code': return '' . esc_html( (string) $v ) . ''; case 'id': return esc_html( trim( (string) $v ) ); case 'integer': return esc_html( self::sign( $v, $f ) . number_format_i18n( (float) $v, 0 ) ); case 'decimal': $dp = isset( $f['precision'] ) ? (int) $f['precision'] : 2; return esc_html( self::sign( $v, $f ) . number_format_i18n( (float) $v, $dp ) ); case 'money': return self::money_markup( (float) $v, $f ); case 'percent': return self::percent( $v, $f, $row ); case 'date': return self::date( $v, $col, isset( $f['format'] ) ? $f['format'] : get_option( 'date_format' ) ); case 'datetime': $fmt = isset( $f['format'] ) ? $f['format'] : get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); return self::date( $v, $col, $fmt ); case 'duration': return self::duration( $v, $f ); case 'relative': return self::relative( $v, $col ); case 'boolean': return self::boolean( $v, $col ); case 'badge': return self::badge( $v, $col, $row ); case 'link': return self::link( $v, $col, $row ); case 'image': return self::image( $v, $col, $row ); case 'object': return self::object_ref( $v, $col, $row ); case 'user': return self::user_ref( $v, $col, $row ); case 'bar': return self::bar( $v, $col, $row ); case 'list': return self::list_chips( $v, $col, $row ); case 'diff': return self::diff( $v, $col, $row ); case 'range': return self::range( $v, $col, $row ); case 'prose': return wp_kses_post( (string) $v ); case 'html': self::$html_hatch++; return (string) $v; case 'text': default: return esc_html( (string) $v ); } } private static function money_markup( $v, $f ) { $args = array(); if ( ! empty( $f['currency'] ) ) { $args['currency'] = $f['currency']; } if ( function_exists( 'wc_price' ) ) { return wp_kses_post( wc_price( $v, $args ) ); } return esc_html( number_format_i18n( $v, 2 ) ); } private static function percent( $v, $f, $row ) { $dp = isset( $f['precision'] ) ? (int) $f['precision'] : 1; $n = (float) $v; if ( isset( $f['of'] ) && 'ratio' === $f['of'] ) { $n *= 100; } elseif ( isset( $f['of'] ) && is_string( $f['of'] ) && 'total' !== $f['of'] ) { $d = (float) self::value( $row, $f['of'], 0 ); $n = $d > 0 ? ( $n / $d ) * 100 : 0; } return esc_html( self::sign( $n, $f ) . number_format_i18n( $n, $dp ) . '%' ); } private static function sign( $v, $f ) { if ( empty( $f['sign'] ) || ! is_numeric( $v ) ) { return ''; } return (float) $v >= 0 ? '+' : ''; } private static function date( $v, $col, $format ) { $ts = self::timestamp( $v, $col ); if ( null === $ts ) { return esc_html( (string) $v ); } return esc_html( wp_date( $format, $ts ) ); } private static function timestamp( $v, $col ) { if ( is_numeric( $v ) && (int) $v > 100000 ) { return (int) $v; } $s = trim( (string) $v ); if ( '' === $s || '0000-00-00 00:00:00' === $s ) { return null; } $utc = isset( $col['format']['tz'] ) && 'utc' === $col['format']['tz']; $ts = strtotime( $utc ? $s . ' UTC' : $s ); return false === $ts ? null : $ts; } private static function duration( $v, $f ) { $sec = (float) $v; $mode = isset( $f['format'] ) ? $f['format'] : 'human'; if ( 'hours' === $mode ) { $h = $sec / HOUR_IN_SECONDS; $d = $sec / DAY_IN_SECONDS; return esc_html( sprintf( __( '%1$s h (%2$s d)', 'ai-eshop-optimizer' ), number_format_i18n( $h, 1 ), number_format_i18n( $d, 1 ) ) ); } if ( $sec <= 0 ) { return esc_html( number_format_i18n( 0 ) ); } return esc_html( human_time_diff( 0, (int) $sec ) ); } private static function relative( $v, $col ) { $ts = self::timestamp( $v, $col ); if ( null === $ts ) { return esc_html( (string) $v ); } return esc_html( sprintf( __( '%s ago', 'ai-eshop-optimizer' ), human_time_diff( $ts, time() ) ) ); } private static function boolean( $v, $col ) { $true = ! empty( $v ) && 'no' !== $v && '0' !== (string) $v; if ( $true ) { if ( ! isset( $col['format']['true'] ) && class_exists( '\Rooster\Admin\Icons' ) ) { $icon = \Rooster\Admin\Icons::svg( 'check', 16 ); if ( '' !== $icon ) { return '' . $icon . '' . esc_html__( 'Yes', 'ai-eshop-optimizer' ) . ''; } } $label = isset( $col['format']['true'] ) ? $col['format']['true'] : '✓'; return '' . esc_html( $label ) . ''; } $label = isset( $col['format']['false'] ) ? $col['format']['false'] : self::EMPTY_MARKER; return '' . esc_html( $label ) . ''; } private static function badge( $v, $col, $row = null ) { $key = sanitize_html_class( (string) $v ); $map = isset( $col['format']['map'] ) && is_array( $col['format']['map'] ) ? $col['format']['map'] : array(); $tone = isset( $map[ (string) $v ] ) ? $map[ (string) $v ] : 'neutral'; if ( ! empty( $col['format']['tone_key'] ) ) { $rt = self::value( $row, $col['format']['tone_key'], '' ); if ( '' !== (string) $rt ) { $tone = (string) $rt; } } $lbl = isset( $col['format']['labels'][ (string) $v ] ) ? $col['format']['labels'][ (string) $v ] : (string) $v; if ( class_exists( 'AIEO_Variants' ) ) { $tone = AIEO_Variants::badge( $tone ); } return '' . esc_html( $lbl ) . ''; } private static function image( $v, $col, $row ) { $url = trim( (string) $v ); if ( '' === $url ) { return self::empty_marker( $col ); } $f = is_array( $col['format'] ) ? $col['format'] : array(); $size = isset( $f['size'] ) ? max( 16, (int) $f['size'] ) : 40; if ( ! empty( $f['rehost'] ) ) { $path = wp_parse_url( $url, PHP_URL_PATH ); if ( is_string( $path ) && '' !== $path ) { $url = home_url( $path ); } } $alt = ''; if ( ! empty( $f['alt'] ) ) { $alt = (string) self::value( $row, (string) $f['alt'], '' ); } $img = '' . esc_attr( $alt ) . ''; if ( isset( $f['href'] ) ) { $href = (string) self::resolve( $row, $f['href'], $f['href'] ); if ( '' !== (string) $href ) { $img = ''; } } return $img; } private static function link( $v, $col, $row ) { $f = $col['format']; $href = ''; if ( isset( $f['href'] ) ) { $href = (string) self::resolve( $row, $f['href'], $f['href'] ); } if ( '' === $href ) { if ( isset( $f['text'] ) && '' !== (string) $f['text'] ) { return self::empty_marker( $col ); } return esc_html( (string) $v ); } $external = ! empty( $f['external'] ); $text = isset( $f['text'] ) && '' !== (string) $f['text'] ? (string) $f['text'] : (string) $v; $class = isset( $f['class'] ) && '' !== (string) $f['class'] ? ' class="' . esc_attr( (string) $f['class'] ) . '"' : ''; $attrs = ''; if ( isset( $f['attrs'] ) ) { $map = self::is_fn( $f['attrs'] ) ? call_user_func( $f['attrs'], $row ) : $f['attrs']; foreach ( (array) $map as $name => $val ) { if ( preg_match( '/^data-[a-z0-9_-]+$/', (string) $name ) ) { $attrs .= ' ' . $name . '="' . esc_attr( (string) $val ) . '"'; } } } return '' . esc_html( $text ) . ''; } private static function user_ref( $v, $col, $row ) { $id = (int) $v; $user = $id > 0 ? get_user_by( 'id', $id ) : null; if ( ! $user ) { return '' . esc_html( sprintf( __( 'user #%d', 'ai-eshop-optimizer' ), $id ) ) . ''; } $name = trim( (string) $user->display_name ); if ( '' === $name ) { $name = (string) $user->user_login; } $edit = current_user_can( 'edit_user', $id ) ? get_edit_user_link( $id ) : ''; $out = $edit ? '' . esc_html( $name ) . '' : esc_html( $name ); if ( ! empty( $col['format']['email'] ) && '' !== (string) $user->user_email ) { $out .= '' . esc_html( (string) $user->user_email ) . ''; } return $out; } private static function object_ref( $v, $col, $row ) { $id = (int) $v; $title = self::value( $row, $col['key'] . '__title' ); if ( null === $title || '' === $title ) { $title = get_the_title( $id ); } if ( '' === (string) $title ) { $title = sprintf( __( '#%d', 'ai-eshop-optimizer' ), $id ); } $edit = get_edit_post_link( $id ); $out = $edit ? '' . esc_html( $title ) . '' : esc_html( $title ); if ( ! empty( $col['format']['show_id'] ) ) { $out .= ' #' . esc_html( (string) $id ) . ''; } return $out; } private static function bar( $v, $col, $row ) { $f = $col['format']; $n = (float) $v; $max = 0.0; if ( isset( $f['max'] ) ) { $max = is_numeric( $f['max'] ) ? (float) $f['max'] : (float) self::value( $row, $f['max'], 0 ); } $pct = $max > 0 ? max( 0, min( 100, ( $n / $max ) * 100 ) ) : 0; if ( ! empty( $f['invert'] ) ) { $pct = 100 - $pct; } $label = isset( $f['label'] ) && false === $f['label'] ? '' : number_format_i18n( $n, isset( $f['precision'] ) ? (int) $f['precision'] : 0 ); return '' . ( '' !== $label ? '' . esc_html( $label ) . '' : '' ); } private static function list_chips( $v, $col, $row = null ) { $items = is_array( $v ) ? $v : array_filter( array_map( 'trim', explode( ',', (string) $v ) ), 'strlen' ); if ( ! $items ) { return self::empty_marker( $col ); } $f = is_array( $col['format'] ) ? $col['format'] : array(); $titles = array(); if ( ! empty( $f['titles'] ) ) { $titles = is_array( $f['titles'] ) ? $f['titles'] : (array) self::value( $row, (string) $f['titles'], array() ); } $max = isset( $f['max'] ) ? (int) $f['max'] : 0; $more = 0; if ( $max > 0 && count( $items ) > $max ) { $more = count( $items ) - $max; $items = array_slice( $items, 0, $max ); } $out = ''; foreach ( $items as $it ) { $it = (string) $it; $title = isset( $titles[ $it ] ) ? trim( (string) $titles[ $it ] ) : ''; $out .= '' !== $title ? '' . esc_html( $it ) . '' : '' . esc_html( $it ) . ''; } if ( $more > 0 ) { $out .= '' . esc_html( sprintf( __( '+%d more', 'ai-eshop-optimizer' ), $more ) ) . ''; } return $out; } private static function diff( $v, $col, $row ) { $from = self::value( $row, isset( $col['format']['from'] ) ? $col['format']['from'] : '', null ); $type = isset( $col['format']['as'] ) ? $col['format']['as'] : 'text'; $a = null === $from ? '' : self::render_value( $type, $from, $col, $row ); $b = self::render_value( $type, $v, $col, $row ); if ( '' === $a ) { return $b; } return '' . $a . '' . '' . '' . $b . ''; } private static function range( $v, $col, $row ) { $f = $col['format']; $type = isset( $f['as'] ) ? $f['as'] : 'decimal'; $out = self::render_value( $type, $v, $col, $row ); $min = isset( $f['min'] ) ? self::value( $row, $f['min'] ) : null; $max = isset( $f['max'] ) ? self::value( $row, $f['max'] ) : null; if ( null === $min || null === $max || '' === $min || '' === $max ) { return $out; } return $out . '' . self::render_value( $type, $min, $col, $row ) . ' – ' . self::render_value( $type, $max, $col, $row ) . ''; } private static function render_interactive( $col, $row, $raw, $ctx ) { $key = isset( $ctx['row_key_value'] ) ? (string) $ctx['row_key_value'] : ''; switch ( $col['type'] ) { case 'selector': return ''; case 'toggle': return ''; case 'editable': $as = isset( $col['format']['as'] ) ? $col['format']['as'] : 'text'; if ( 'select' === $as ) { $opts = isset( $col['format']['options'] ) ? (array) $col['format']['options'] : array(); $out = ''; } return ''; case 'control': $fn = isset( $col['format']['render'] ) ? $col['format']['render'] : null; if ( ! self::is_fn( $fn ) ) { return self::empty_marker( $col ); } self::$control_cells++; return (string) call_user_func( $fn, $row, $key ); case 'expander': return ''; case 'actions': default: return ''; } } private static function decorate( $html, $col, $row, $raw ) { $trunc = (int) $col['truncate']; if ( $trunc > 0 && is_scalar( $raw ) ) { $full = (string) $raw; if ( mb_strlen( $full ) > $trunc ) { $short = mb_substr( $full, 0, $trunc ) . '…'; $title = $col['tooltip'] ? ' title="' . esc_attr( $full ) . '"' : ''; $html = '' . esc_html( $short ) . ''; } } elseif ( $col['tooltip'] ) { $tip = ( true === $col['tooltip'] ) ? $raw : self::value( $row, $col['tooltip'] ); if ( null !== $tip && '' !== $tip ) { $html = '' . $html . ''; } } if ( ! empty( $col['suffix'] ) ) { $sfx = self::is_fn( $col['suffix'] ) ? call_user_func( $col['suffix'], $row ) : self::value( $row, $col['suffix'], $col['suffix'] ); if ( null !== $sfx && '' !== $sfx ) { $html .= ' ' . esc_html( (string) $sfx ) . ''; } } if ( ! empty( $col['subline'] ) ) { $sub = self::resolve( $row, $col['subline'] ); if ( null !== $sub && '' !== $sub ) { $html .= '' . esc_html( (string) $sub ) . ''; } } return $html; } public static function cell_attrs( $col, $row ) { $col = self::normalise( $col ); $classes = array( 'aieo-t-c', 'aieo-t-c--' . $col['type'] ); $align = $col['align']; if ( '' === $align && in_array( $col['type'], array( 'id', 'integer', 'decimal', 'money', 'percent' ), true ) ) { $align = 'right'; } if ( '' !== $align ) { $classes[] = 'aieo-t-a--' . $align; } if ( $col['nowrap'] ) { $classes[] = 'aieo-t-nowrap'; } $tone = $col['tone']; if ( $col['bands'] ) { $banded = self::band_tone( self::value( $row, $col['key'] ), $col['bands'] ); if ( '' !== $banded ) { $tone = $banded; } } if ( '' !== (string) $col['tone_key'] ) { $rt = self::value( $row, (string) $col['tone_key'], '' ); if ( '' !== (string) $rt ) { $tone = (string) $rt; } } if ( '' !== $tone ) { $classes[] = 'aieo-t-tone--' . $tone; } $attrs = 'class="' . esc_attr( implode( ' ', $classes ) ) . '"' . ' data-key="' . esc_attr( $col['key'] ) . '"'; $sort_raw = $col['sort_key'] ? self::value( $row, $col['sort_key'] ) : self::value( $row, $col['key'] ); if ( null !== $sort_raw && ! is_array( $sort_raw ) ) { $attrs .= ' data-sort-value="' . esc_attr( (string) $sort_raw ) . '"'; } return $attrs; } public static function band_tone( $v, $bands ) { if ( ! is_numeric( $v ) ) { return ''; } $n = (float) $v; $best = ''; $high = null; $tight = null; foreach ( (array) $bands as $b ) { if ( ! isset( $b['tone'] ) ) { continue; } $min = isset( $b['min'] ) ? (float) $b['min'] : -INF; $max = isset( $b['max'] ) ? (float) $b['max'] : INF; if ( $n < $min || $n > $max ) { continue; } if ( null === $high || $min > $high || ( $min === $high && $max < $tight ) ) { $high = $min; $tight = $max; $best = (string) $b['tone']; } } return $best; } }