| 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 |
} |
| 242 |
|