'
',
'table' => sprintf(
'
',
esc_attr( $class_tb )
),
'header' => $this->html_header( $data['header'] ?? [] ),
'body' => $this->html_body( $data['body'] ?? [] ),
'footer' => $this->html_footer( $data['footer'] ?? '' ),
'table-end' => '
',
'wrap-end' => '
',
];
return Template::combine_components( $section );
}
/**
* Render the HTML table header.
*
* @param array $data
*
* @return string
*/
public function html_header( array $data = [] ): string {
$html_th = '';
foreach ( $data as $item ) {
$html_th .= sprintf(
'%s | ',
esc_attr( $item['class'] ?? '' ),
wp_kses_post( $item['title'] ?? '' )
);
}
$section = [
'wrapper' => '',
'tr' => '',
'ths' => $html_th,
'tr_end' => '
',
'wrapper_end' => '',
];
return Template::combine_components( $section );
}
/**
* Render the HTML table body.
*
* @param array $data
*
* @return string
*/
public function html_body( array $data = [] ): string {
$html_tr = '';
// Allow callers to pass fully prepared rows HTML.
if ( isset( $data['rows_html'] ) && is_string( $data['rows_html'] ) ) {
$html_tr = $data['rows_html'];
} else {
foreach ( $data as $item_tr ) {
$html_tr .= '';
foreach ( $item_tr as $item_td ) {
$html_td = sprintf(
'| %s | ',
wp_kses_post( $item_td )
);
$html_tr .= $html_td;
}
$html_tr .= '
';
}
}
$section = [
'wrapper' => '',
'trs' => $html_tr,
'wrapper_end' => '',
];
return Template::combine_components( $section );
}
/**
* Render the HTML table footer.
*
* @param string $footer_html
*
* @return string
*/
public function html_footer( string $footer_html = '' ): string {
if ( '' === trim( $footer_html ) ) {
return '';
}
$html_td = sprintf(
'%s | ',
wp_kses_post( $footer_html )
);
$section = [
'wrapper' => '',
'tr' => '',
'tds' => $html_td,
'tr_end' => '
',
'wrapper_end' => '',
];
return Template::combine_components( $section );
}
/**
* Render the HTML info page query.
*
* @param array $data
*
* @return string
*/
public function html_page_result( array $data = [] ): string {
$total_rows = $data['total_rows'] ?? 0;
$paged = $data['paged'] ?? 1;
$per_page = $data['per_page'] ?? 1;
$item_name = $data['item_name'] ?? _n( 'item', 'items', $total_rows, 'learnpress' );
$from = ( $paged - 1 ) * $per_page + 1;
$to = $from + $per_page - 1;
$to = min( $to, $total_rows );
if ( $total_rows < 1 ) {
$from = 0;
}
$format = __( 'Displaying {{from}} to {{to}} of {{total}} {{item_name}}.', 'learnpress' );
$format = apply_filters(
'learnpress/table/list-page-result',
$format,
$data
);
$output = str_replace(
array( '{{from}}', '{{to}}', '{{total}}', '{{item_name}}' ),
array(
$from,
$to,
$total_rows,
$item_name,
),
$format
);
return wp_kses_post( $output );
}
}