-1;}).map(function(c){return c.index;}).join(" "));}}catch(e){}})(document.getElementById(%s));'; private static array $scripts = array(); public static function render( array $args ): string { Assets::enqueue_tree_table(); $id = isset( $args['id'] ) && '' !== $args['id'] ? sanitize_html_class( (string) $args['id'] ) : 'rooster-lms-tree-' . ( ++self::$count ); $columns = self::columns( $args['columns'] ?? array() ); $select = ! empty( $args['select'] ); $menu = $args['menu'] ?? true; $rows = self::normalise( $args['rows'] ?? array(), $columns ); $facets = array_filter( is_array( $args['facets'] ?? null ) ? $args['facets'] : array(), static fn( $v ): bool => null !== $v && '' !== $v && array() !== $v ); $allowed = null; if ( array() !== $facets ) { list( $allowed, $opened ) = self::facet_rows( $rows, $facets ); foreach ( $rows as $i => $r ) { if ( isset( $opened[ $r['id'] ] ) ) { $rows[ $i ]['open'] = true; } } } $tree = self::tree( $rows ); $count = array_values( array_filter( array_map( 'strval', (array) ( $args['count_kind'] ?? array() ) ), 'strlen' ) ); $pages = is_array( $args['pages'] ?? null ) && (int) ( $args['pages']['size'] ?? 0 ) > 0 && array() !== $count && Ui::kit_markup() ? $args['pages'] : null; $paging = null; if ( null !== $pages ) { $paging = self::page_rows( $tree, $rows, $allowed, $count, (int) $pages['size'], (int) ( $pages['paged'] ?? 1 ) ); $allowed = $paging['allowed']; } $kinds = $args['kinds'] ?? array(); $l10n = array_merge( self::l10n(), array_map( 'strval', $args['l10n'] ?? array() ) ); $counted = self::counted( $rows, $count ); $offset = $select ? 1 : 0; $config = array( 'id' => $id, 'columns' => array_map( static fn( array $c, int $i ): array => array( 'key' => $c['key'], 'sticky' => $c['sticky'], 'edit' => $c['edit'], 'search' => $c['search'], 'class' => $c['class'], 'hideable' => $c['hideable'], 'index' => $i + 1 + $offset, 'sort' => $c['sort'], ), $columns, array_keys( $columns ) ), 'kinds' => self::kinds( $kinds ), 'actions' => self::actions_config( $args['actions'] ?? array() ), 'rules' => self::rules( $args['rules'] ?? array() ), 'select' => $select, 'menu' => (bool) $menu, 'count' => 1 < count( $count ) ? $count : (string) ( $count[0] ?? '' ), 'kit' => Ui::kit_markup(), 'l10n' => $l10n, ); if ( array() !== $facets ) { $config['facets'] = (object) $facets; } if ( null !== $paging ) { $config['pages'] = array( 'size' => (int) $pages['size'], 'paged' => $paging['paged'], 'chevron' => Pager::chevron(), 'l10n' => array( /* translators: 1: first row number, 2: last row number, 3: number of rows. */ 'summary' => __( 'Showing %1$s–%2$s of %3$s', 'rooster-lms' ), 'label' => __( 'Pages', 'rooster-lms' ), 'prev' => __( 'Previous page', 'rooster-lms' ), 'next' => __( 'Next page', 'rooster-lms' ), /* translators: %s: page number. */ 'page' => __( 'Page %s', 'rooster-lms' ), ), ); } $sort = self::sort_args( $args['sort'] ?? null ); if ( null !== $sort ) { $config['sort'] = array( 'mode' => $sort['mode'], 'kinds' => $sort['kinds'], ); if ( $sort['address'] ) { $config['sort'] += array( 'address' => true, 'key' => $sort['key'], 'dir' => $sort['dir'], 'none' => $sort['none'], ); } } if ( ! empty( $args['save'] ) ) { $config['save'] = self::user_locale( esc_url_raw( (string) $args['save'] ) ); } if ( ! empty( $args['children'] ) ) { $marks = array( '{id}' => 'rl-tree-id-mark', '{kind}' => 'rl-tree-kind-mark', ); $config['children'] = self::user_locale( strtr( esc_url_raw( strtr( (string) $args['children'], $marks ) ), array_flip( $marks ) ) ); } if ( ! empty( $args['batch'] ) ) { $config['childrenMany'] = self::user_locale( esc_url_raw( (string) $args['batch'] ) ); } if ( isset( $config['save'] ) || isset( $config['children'] ) || isset( $config['childrenMany'] ) ) { $config['nonce'] = is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : ''; } $search = $args['search'] ?? true; $html = '
| '; } foreach ( $columns as $i => $column ) { $label = esc_html( $column['label'] ); $aria = ''; if ( null !== $sort && '' !== $column['sort'] && '' !== $column['label'] ) { $current = ( 'server' === $sort['mode'] || $sort['address'] ) && $sort['key'] === $column['key']; $aria = ' aria-sort="' . ( $current ? ( 'desc' === $sort['dir'] ? 'descending' : 'ascending' ) : 'none' ) . '"'; $icon = $kit ? DataTable::sort_chevron() : ''; $link = $kit ? 'rooster-admin-table__sort' : self::BLOCK . '__sort'; if ( 'server' === $sort['mode'] ) { $href = add_query_arg( array( 'orderby' => $column['key'], 'order' => $current && 'asc' === $sort['dir'] ? 'desc' : 'asc', ), '' !== $sort['url'] ? $sort['url'] : remove_query_arg( array( 'orderby', 'order', 'paged' ) ) ); $label = '' . $label . $icon . ''; } else { $label = ''; } } $html .= ' | ' . $label . ' | '; } if ( $menu ) { $html .= '' . esc_html( $l10n['actions'] ) . ' | '; } return $html . '|
|---|---|---|---|
| ' . ( $row['selectable'] ? '' : '' ) . ' | '; } foreach ( $columns as $i => $column ) { $classes = self::classes( $b . '__' . ( 0 === $i ? 'title' : 'cell' ), $column['class'], $sticky[ $i ] ); $fixed = ! empty( $at['grid'] ) && ! in_array( $column['key'], $row['edit'], true ) ? ' aria-readonly="true"' : ''; if ( 0 === $i ) { $html .= ''; $html .= $row['movable'] ? '' : ''; $html .= $expandable ? '' : ''; $label = '' !== $row['href'] ? '' . esc_html( $title ) . '' : esc_html( $title ); $lang = $row['lang']; if ( '' !== $row['icon'] ) { $html .= KindIcon::html( $row['icon'] ); } $html .= '' . $label . ''; $badge = Ui::kit_markup() ? 'rooster-admin-badge is-mono is-sm ' : 'rooster-lms-badge rooster-lms-badge--sm '; if ( null !== $lang ) { $code = esc_html( strtoupper( Language::primary( $lang['code'] ) ) ); $html .= Ui::kit_markup() ? '' . esc_html( $lang['tip'] ) . '' : '' . $code . ''; } $noun = $kinds[ $row['kind'] ]['count'] ?? null; if ( $expandable && is_array( $noun ) && isset( $noun[0], $noun[1] ) ) { $html .= '' . esc_html( self::fill( 1 === $children ? (string) $noun[0] : (string) $noun[1], array( Format::number( $children ) ) ) ) . ''; } $html .= ' | '; continue; } $cell = $row['cells'][ $column['key'] ] ?? ''; $by = is_array( $cell ) && isset( $cell['sort'] ) && is_scalar( $cell['sort'] ) ? ' data-sort="' . esc_attr( (string) $cell['sort'] ) . '"' : ''; $html .= ''; if ( is_array( $cell ) && ! isset( $cell['html'] ) ) { $html .= esc_html( (string) ( $cell['text'] ?? '' ) ); } elseif ( is_array( $cell ) ) { $html .= wp_kses( (string) ( $cell['html'] ?? '' ), is_admin() ? Kses::admin() : Kses::front() ); } elseif ( in_array( $column['key'], $row['edit'], true ) ) { $html .= '' . esc_html( (string) $cell ) . ''; } else { $html .= esc_html( (string) $cell ); } $html .= ' | '; } if ( $menu ) { $html .= ''; } return $html . ' |