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