| @@ -1,179 +1,241 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Class ProfileQuizzesTemplate. | |
| 4 | - * | |
| 5 | - * @since 4.2.8.2 | |
| 6 | - * @version 1.0.0 | |
| 7 | - */ | |
| 8 | - | |
| 9 | -namespace LearnPress\TemplateHooks\Table; | |
| 10 | - | |
| 11 | -use LearnPress\Helpers\Singleton; | |
| 12 | -use LearnPress\Helpers\Template; | |
| 13 | - | |
| 14 | - | |
| 15 | -class TableListTemplate { | |
| 16 | - use Singleton; | |
| 17 | - | |
| 18 | - public function init() { | |
| 19 | - } | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * Render the HTML table. | |
| 23 | - * | |
| 24 | - * @param array $data | |
| 25 | - * | |
| 26 | - * @return string | |
| 27 | - */ | |
| 28 | - public function html_table( array $data = [] ): string { | |
| 29 | - $class_tb = $data['class_table'] ?? ''; | |
| 30 | - $section = [ | |
| 31 | - 'wrap' => '<div class="lp-table-wrap">', | |
| 32 | - 'table' => sprintf( | |
| 33 | - '<table class="lp-list-table %s">', | |
| 34 | - esc_attr( $class_tb ) | |
| 35 | - ), | |
| 36 | - 'header' => $this->html_header( $data['header'] ?? [] ), | |
| 37 | - 'body' => $this->html_body( $data['body'] ?? [] ), | |
| 38 | - 'footer' => $this->html_footer( $data['footer'] ?? '' ), | |
| 39 | - 'table-end' => '</table>', | |
| 40 | - 'wrap-end' => '</div>', | |
| 41 | - ]; | |
| 42 | - | |
| 43 | - return Template::combine_components( $section ); | |
| 44 | - } | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * Render the HTML table header. | |
| 48 | - * | |
| 49 | - * @param array $data | |
| 50 | - * | |
| 51 | - * @return string | |
| 52 | - */ | |
| 53 | - public function html_header( array $data = [] ): string { | |
| 54 | - $html_th = ''; | |
| 55 | - | |
| 56 | - foreach ( $data as $item ) { | |
| 57 | - $html_th .= sprintf( | |
| 58 | - '<th class="%s">%s</th>', | |
| 59 | - esc_attr( $item['class'] ?? '' ), | |
| 60 | - wp_kses_post( $item['title'] ?? '' ) | |
| 61 | - ); | |
| 62 | - } | |
| 63 | - | |
| 64 | - $section = [ | |
| 65 | - 'wrapper' => '<thead>', | |
| 66 | - 'tr' => '<tr>', | |
| 67 | - 'ths' => $html_th, | |
| 68 | - 'tr_end' => '</tr>', | |
| 69 | - 'wrapper_end' => '</thead>', | |
| 70 | - ]; | |
| 71 | - | |
| 72 | - return Template::combine_components( $section ); | |
| 73 | - } | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * Render the HTML table body. | |
| 77 | - * | |
| 78 | - * @param array $data | |
| 79 | - * | |
| 80 | - * @return string | |
| 81 | - */ | |
| 82 | - public function html_body( array $data = [] ): string { | |
| 83 | - $html_tr = ''; | |
| 84 | - | |
| 85 | - // Allow callers to pass fully prepared rows HTML. | |
| 86 | - if ( isset( $data['rows_html'] ) && is_string( $data['rows_html'] ) ) { | |
| 87 | - $html_tr = $data['rows_html']; | |
| 88 | - } else { | |
| 89 | - foreach ( $data as $item_tr ) { | |
| 90 | - $html_tr .= '<tr>'; | |
| 91 | - foreach ( $item_tr as $item_td ) { | |
| 92 | - $html_td = sprintf( | |
| 93 | - '<td>%s</td>', | |
| 94 | - wp_kses_post( $item_td ) | |
| 95 | - ); | |
| 96 | - $html_tr .= $html_td; | |
| 97 | - } | |
| 98 | - $html_tr .= '</tr>'; | |
| 99 | - } | |
| 100 | - } | |
| 101 | - | |
| 102 | - $section = [ | |
| 103 | - 'wrapper' => '<tbody>', | |
| 104 | - 'trs' => $html_tr, | |
| 105 | - 'wrapper_end' => '</tbody>', | |
| 106 | - ]; | |
| 107 | - | |
| 108 | - return Template::combine_components( $section ); | |
| 109 | - } | |
| 110 | - | |
| 111 | - /** | |
| 112 | - * Reder the HTML table footer. | |
| 113 | - * | |
| 114 | - * @param string $footer_html | |
| 115 | - * | |
| 116 | - * @return string | |
| 117 | - */ | |
| 118 | - public function html_footer( string $footer_html = '' ): string { | |
| 119 | - if ( '' === trim( $footer_html ) ) { | |
| 120 | - return ''; | |
| 121 | - } | |
| 122 | - | |
| 123 | - $html_td = sprintf( | |
| 124 | - '<td colspan="10">%s</td>', | |
| 125 | - wp_kses_post( $footer_html ) | |
| 126 | - ); | |
| 127 | - | |
| 128 | - $section = [ | |
| 129 | - 'wrapper' => '<tfoot>', | |
| 130 | - 'tr' => '<tr>', | |
| 131 | - 'tds' => $html_td, | |
| 132 | - 'tr_end' => '</tr>', | |
| 133 | - 'wrapper_end' => '</tfoot>', | |
| 134 | - ]; | |
| 135 | - | |
| 136 | - return Template::combine_components( $section ); | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * Render the HTML info page query. | |
| 141 | - * | |
| 142 | - * @param array $data | |
| 143 | - * | |
| 144 | - * @return string | |
| 145 | - */ | |
| 146 | - public function html_page_result( array $data = [] ): string { | |
| 147 | - $total_rows = $data['total_rows'] ?? 0; | |
| 148 | - $paged = $data['paged'] ?? 1; | |
| 149 | - $per_page = $data['per_page'] ?? 1; | |
| 150 | - $item_name = $data['item_name'] ?? _n( 'item', 'items', $total_rows, 'learnpress' ); | |
| 151 | - | |
| 152 | - $from = ( $paged - 1 ) * $per_page + 1; | |
| 153 | - $to = $from + $per_page - 1; | |
| 154 | - $to = min( $to, $total_rows ); | |
| 155 | - if ( $total_rows < 1 ) { | |
| 156 | - $from = 0; | |
| 157 | - } | |
| 158 | - | |
| 159 | - $format = __( 'Displaying {{from}} to {{to}} of {{total}} {{item_name}}.', 'learnpress' ); | |
| 160 | - $format = apply_filters( | |
| 161 | - 'learnpress/table/list-page-result', | |
| 162 | - $format, | |
| 163 | - $data | |
| 164 | - ); | |
| 165 | - | |
| 166 | - $output = str_replace( | |
| 167 | - array( '{{from}}', '{{to}}', '{{total}}', '{{item_name}}' ), | |
| 168 | - array( | |
| 169 | - $from, | |
| 170 | - $to, | |
| 171 | - $total_rows, | |
| 172 | - $item_name, | |
| 173 | - ), | |
| 174 | - $format | |
| 175 | - ); | |
| 176 | - | |
| 177 | - return wp_kses_post( $output ); | |
| 178 | - } | |
| 179 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Class ProfileQuizzesTemplate. | |
| 4 | + * | |
| 5 | + * @since 4.2.8.2 | |
| 6 | + * @version 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace LearnPress\TemplateHooks\Table; | |
| 10 | + | |
| 11 | +use LearnPress\Helpers\Singleton; | |
| 12 | +use LearnPress\Helpers\Template; | |
| 13 | + | |
| 14 | + | |
| 15 | +class TableListTemplate { | |
| 16 | + use Singleton; | |
| 17 | + | |
| 18 | + public function init(): void {} | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Render the HTML table. | |
| 22 | + * | |
| 23 | + * @param array $data | |
| 24 | + * | |
| 25 | + * @return string | |
| 26 | + */ | |
| 27 | + public function html_table( array $data = [] ): string { | |
| 28 | + $class_tb = $data['class_table'] ?? ''; | |
| 29 | + $section = [ | |
| 30 | + 'wrap' => '<div class="lp-table-wrap">', | |
| 31 | + 'table' => sprintf( | |
| 32 | + '<table class="lp-list-table %s">', | |
| 33 | + esc_attr( $class_tb ) | |
| 34 | + ), | |
| 35 | + 'header' => $this->html_header( $data['header'] ?? [] ), | |
| 36 | + 'body' => $this->html_body( $data['body'] ?? [] ), | |
| 37 | + 'footer' => $this->html_footer( $data['footer'] ?? '' ), | |
| 38 | + 'table-end' => '</table>', | |
| 39 | + 'wrap-end' => '</div>', | |
| 40 | + ]; | |
| 41 | + | |
| 42 | + return Template::combine_components( $section ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Render the HTML table header. | |
| 47 | + * | |
| 48 | + * @param array $data | |
| 49 | + * | |
| 50 | + * @return string | |
| 51 | + */ | |
| 52 | + public function html_header( array $data = [] ): string { | |
| 53 | + $html_th = ''; | |
| 54 | + | |
| 55 | + foreach ( $data as $item ) { | |
| 56 | + $html_th .= $this->html_header_sort( $item ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + $section = [ | |
| 60 | + 'wrapper' => '<thead>', | |
| 61 | + 'tr' => '<tr>', | |
| 62 | + 'ths' => $html_th, | |
| 63 | + 'tr_end' => '</tr>', | |
| 64 | + 'wrapper_end' => '</thead>', | |
| 65 | + ]; | |
| 66 | + | |
| 67 | + return Template::combine_components( $section ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Render a single sortable/non-sortable column header. | |
| 72 | + * | |
| 73 | + * Mirrors WordPress WP_List_Table column headers, replacing the | |
| 74 | + * default .sorting-indicator pseudo-element icon with lp-icon carets. | |
| 75 | + * | |
| 76 | + * Expected item keys: | |
| 77 | + * - class : CSS class(es) for the <th>. | |
| 78 | + * - title : Column title. | |
| 79 | + * - sortable : Whether the column supports sorting. | |
| 80 | + * - sort_url : URL to use when the header is clicked (required when sortable). | |
| 81 | + * - sorted : Whether this column is the currently sorted one. | |
| 82 | + * - sort_order : Current sort order, 'asc' or 'desc' (default 'asc'). | |
| 83 | + * | |
| 84 | + * @param array $item | |
| 85 | + * | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | + public function html_header_sort( array $item = [] ): string { | |
| 89 | + $classes = array_filter( [ (string) ( $item['class'] ?? '' ) ] ); | |
| 90 | + $sortable = ! empty( $item['sortable'] ); | |
| 91 | + $sorted = $sortable && ! empty( $item['sorted'] ); | |
| 92 | + $order = $sorted ? strtolower( (string) ( $item['sort_order'] ?? 'asc' ) ) : ''; | |
| 93 | + if ( ! in_array( $order, [ 'asc', 'desc' ], true ) ) { | |
| 94 | + $order = 'asc'; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( $sortable ) { | |
| 98 | + $classes[] = $sorted ? 'sorted' : 'sortable'; | |
| 99 | + if ( $sorted ) { | |
| 100 | + $classes[] = $order; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + $title = wp_kses_post( $item['title'] ?? '' ); | |
| 105 | + $class = implode( ' ', $classes ); | |
| 106 | + | |
| 107 | + if ( ! $sortable ) { | |
| 108 | + return sprintf( | |
| 109 | + '<th scope="col" class="%s">%s</th>', | |
| 110 | + esc_attr( $class ), | |
| 111 | + $title | |
| 112 | + ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + $up_active = $sorted && 'asc' === $order ? ' is-active' : ''; | |
| 116 | + $down_active = $sorted && 'desc' === $order ? ' is-active' : ''; | |
| 117 | + $sort_url = $item['sort_url'] ?? '#'; | |
| 118 | + | |
| 119 | + return sprintf( | |
| 120 | + '<th scope="col" class="%s"> | |
| 121 | + <a href="%s"> | |
| 122 | + <span>%s</span> | |
| 123 | + <span class="sorting-indicator"> | |
| 124 | + <i class="lp-icon-caret-up%4$s"></i> | |
| 125 | + <i class="lp-icon-caret-down%5$s"></i> | |
| 126 | + </span> | |
| 127 | + </a> | |
| 128 | + </th>', | |
| 129 | + esc_attr( $class ), | |
| 130 | + esc_url( $sort_url ), | |
| 131 | + $title, | |
| 132 | + esc_attr( $up_active ), | |
| 133 | + esc_attr( $down_active ) | |
| 134 | + ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Render the HTML table body. | |
| 139 | + * | |
| 140 | + * @param array $data | |
| 141 | + * | |
| 142 | + * @return string | |
| 143 | + */ | |
| 144 | + public function html_body( array $data = [] ): string { | |
| 145 | + $html_tr = ''; | |
| 146 | + | |
| 147 | + // Allow callers to pass fully prepared rows HTML. | |
| 148 | + if ( isset( $data['rows_html'] ) && is_string( $data['rows_html'] ) ) { | |
| 149 | + $html_tr = $data['rows_html']; | |
| 150 | + } else { | |
| 151 | + foreach ( $data as $item_tr ) { | |
| 152 | + $html_tr .= '<tr>'; | |
| 153 | + foreach ( $item_tr as $item_td ) { | |
| 154 | + $html_td = sprintf( | |
| 155 | + '<td>%s</td>', | |
| 156 | + wp_kses_post( $item_td ) | |
| 157 | + ); | |
| 158 | + $html_tr .= $html_td; | |
| 159 | + } | |
| 160 | + $html_tr .= '</tr>'; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + | |
| 164 | + $section = [ | |
| 165 | + 'wrapper' => '<tbody>', | |
| 166 | + 'trs' => $html_tr, | |
| 167 | + 'wrapper_end' => '</tbody>', | |
| 168 | + ]; | |
| 169 | + | |
| 170 | + return Template::combine_components( $section ); | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Render the HTML table footer. | |
| 175 | + * | |
| 176 | + * @param string $footer_html | |
| 177 | + * | |
| 178 | + * @return string | |
| 179 | + */ | |
| 180 | + public function html_footer( string $footer_html = '' ): string { | |
| 181 | + if ( '' === trim( $footer_html ) ) { | |
| 182 | + return ''; | |
| 183 | + } | |
| 184 | + | |
| 185 | + $html_td = sprintf( | |
| 186 | + '<td colspan="10">%s</td>', | |
| 187 | + wp_kses_post( $footer_html ) | |
| 188 | + ); | |
| 189 | + | |
| 190 | + $section = [ | |
| 191 | + 'wrapper' => '<tfoot>', | |
| 192 | + 'tr' => '<tr>', | |
| 193 | + 'tds' => $html_td, | |
| 194 | + 'tr_end' => '</tr>', | |
| 195 | + 'wrapper_end' => '</tfoot>', | |
| 196 | + ]; | |
| 197 | + | |
| 198 | + return Template::combine_components( $section ); | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * Render the HTML info page query. | |
| 203 | + * | |
| 204 | + * @param array $data | |
| 205 | + * | |
| 206 | + * @return string | |
| 207 | + */ | |
| 208 | + public function html_page_result( array $data = [] ): string { | |
| 209 | + $total_rows = $data['total_rows'] ?? 0; | |
| 210 | + $paged = $data['paged'] ?? 1; | |
| 211 | + $per_page = $data['per_page'] ?? 1; | |
| 212 | + $item_name = $data['item_name'] ?? _n( 'item', 'items', $total_rows, 'learnpress' ); | |
| 213 | + | |
| 214 | + $from = ( $paged - 1 ) * $per_page + 1; | |
| 215 | + $to = $from + $per_page - 1; | |
| 216 | + $to = min( $to, $total_rows ); | |
| 217 | + if ( $total_rows < 1 ) { | |
| 218 | + $from = 0; | |
| 219 | + } | |
| 220 | + | |
| 221 | + $format = __( 'Displaying {{from}} to {{to}} of {{total}} {{item_name}}.', 'learnpress' ); | |
| 222 | + $format = apply_filters( | |
| 223 | + 'learnpress/table/list-page-result', | |
| 224 | + $format, | |
| 225 | + $data | |
| 226 | + ); | |
| 227 | + | |
| 228 | + $output = str_replace( | |
| 229 | + array( '{{from}}', '{{to}}', '{{total}}', '{{item_name}}' ), | |
| 230 | + array( | |
| 231 | + $from, | |
| 232 | + $to, | |
| 233 | + $total_rows, | |
| 234 | + $item_name, | |
| 235 | + ), | |
| 236 | + $format | |
| 237 | + ); | |
| 238 | + | |
| 239 | + return wp_kses_post( $output ); | |
| 240 | + } | |
| 241 | +} | |