| @@ -1,179 +1,178 @@ | ||
| 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 .= sprintf( | |
| 57 | + '<th class="%s">%s</th>', | |
| 58 | + esc_attr( $item['class'] ?? '' ), | |
| 59 | + wp_kses_post( $item['title'] ?? '' ) | |
| 60 | + ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + $section = [ | |
| 64 | + 'wrapper' => '<thead>', | |
| 65 | + 'tr' => '<tr>', | |
| 66 | + 'ths' => $html_th, | |
| 67 | + 'tr_end' => '</tr>', | |
| 68 | + 'wrapper_end' => '</thead>', | |
| 69 | + ]; | |
| 70 | + | |
| 71 | + return Template::combine_components( $section ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Render the HTML table body. | |
| 76 | + * | |
| 77 | + * @param array $data | |
| 78 | + * | |
| 79 | + * @return string | |
| 80 | + */ | |
| 81 | + public function html_body( array $data = [] ): string { | |
| 82 | + $html_tr = ''; | |
| 83 | + | |
| 84 | + // Allow callers to pass fully prepared rows HTML. | |
| 85 | + if ( isset( $data['rows_html'] ) && is_string( $data['rows_html'] ) ) { | |
| 86 | + $html_tr = $data['rows_html']; | |
| 87 | + } else { | |
| 88 | + foreach ( $data as $item_tr ) { | |
| 89 | + $html_tr .= '<tr>'; | |
| 90 | + foreach ( $item_tr as $item_td ) { | |
| 91 | + $html_td = sprintf( | |
| 92 | + '<td>%s</td>', | |
| 93 | + wp_kses_post( $item_td ) | |
| 94 | + ); | |
| 95 | + $html_tr .= $html_td; | |
| 96 | + } | |
| 97 | + $html_tr .= '</tr>'; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + $section = [ | |
| 102 | + 'wrapper' => '<tbody>', | |
| 103 | + 'trs' => $html_tr, | |
| 104 | + 'wrapper_end' => '</tbody>', | |
| 105 | + ]; | |
| 106 | + | |
| 107 | + return Template::combine_components( $section ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Render the HTML table footer. | |
| 112 | + * | |
| 113 | + * @param string $footer_html | |
| 114 | + * | |
| 115 | + * @return string | |
| 116 | + */ | |
| 117 | + public function html_footer( string $footer_html = '' ): string { | |
| 118 | + if ( '' === trim( $footer_html ) ) { | |
| 119 | + return ''; | |
| 120 | + } | |
| 121 | + | |
| 122 | + $html_td = sprintf( | |
| 123 | + '<td colspan="10">%s</td>', | |
| 124 | + wp_kses_post( $footer_html ) | |
| 125 | + ); | |
| 126 | + | |
| 127 | + $section = [ | |
| 128 | + 'wrapper' => '<tfoot>', | |
| 129 | + 'tr' => '<tr>', | |
| 130 | + 'tds' => $html_td, | |
| 131 | + 'tr_end' => '</tr>', | |
| 132 | + 'wrapper_end' => '</tfoot>', | |
| 133 | + ]; | |
| 134 | + | |
| 135 | + return Template::combine_components( $section ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Render the HTML info page query. | |
| 140 | + * | |
| 141 | + * @param array $data | |
| 142 | + * | |
| 143 | + * @return string | |
| 144 | + */ | |
| 145 | + public function html_page_result( array $data = [] ): string { | |
| 146 | + $total_rows = $data['total_rows'] ?? 0; | |
| 147 | + $paged = $data['paged'] ?? 1; | |
| 148 | + $per_page = $data['per_page'] ?? 1; | |
| 149 | + $item_name = $data['item_name'] ?? _n( 'item', 'items', $total_rows, 'learnpress' ); | |
| 150 | + | |
| 151 | + $from = ( $paged - 1 ) * $per_page + 1; | |
| 152 | + $to = $from + $per_page - 1; | |
| 153 | + $to = min( $to, $total_rows ); | |
| 154 | + if ( $total_rows < 1 ) { | |
| 155 | + $from = 0; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $format = __( 'Displaying {{from}} to {{to}} of {{total}} {{item_name}}.', 'learnpress' ); | |
| 159 | + $format = apply_filters( | |
| 160 | + 'learnpress/table/list-page-result', | |
| 161 | + $format, | |
| 162 | + $data | |
| 163 | + ); | |
| 164 | + | |
| 165 | + $output = str_replace( | |
| 166 | + array( '{{from}}', '{{to}}', '{{total}}', '{{item_name}}' ), | |
| 167 | + array( | |
| 168 | + $from, | |
| 169 | + $to, | |
| 170 | + $total_rows, | |
| 171 | + $item_name, | |
| 172 | + ), | |
| 173 | + $format | |
| 174 | + ); | |
| 175 | + | |
| 176 | + return wp_kses_post( $output ); | |
| 177 | + } | |
| 178 | +} | |