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.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / components / Table.php
tutor / components Last commit date
Constants 3 weeks ago Accordion.php 1 week ago Alert.php 1 week ago AttachmentCard.php 1 week ago Avatar.php 1 week ago Badge.php 1 week ago BaseComponent.php 3 weeks ago Button.php 1 week ago ConfirmationModal.php 1 week ago CourseFilter.php 1 week ago DateFilter.php 1 week ago DropdownFilter.php 1 week ago EmptyState.php 1 week ago FileUploader.php 1 week ago InputField.php 1 week ago Modal.php 1 week ago Nav.php 1 week ago Pagination.php 1 week ago Popover.php 1 week ago PreviewTrigger.php 1 week ago Progress.php 1 week ago SearchFilter.php 1 week ago Sorting.php 1 week ago StarRating.php 1 week ago StarRatingInput.php 1 week ago StatusSelect.php 1 week ago SvgIcon.php 1 week ago Table.php 1 week ago Tabs.php 1 week ago Tooltip.php 1 week ago WPEditor.php 1 week ago
Table.php
259 lines
1 <?php
2 /**
3 * Table Component Class.
4 *
5 * @package Tutor\Components
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 4.0.0
9 */
10
11 namespace Tutor\Components;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Class Table
17 *
18 * Responsible for rendering table component with variable number
19 * of rows and columns.
20 *
21 * ```php
22 * // Minimal table with headings and rows
23 * $headings = array(
24 * array( 'content' => __( 'Student', 'tutor' ) ),
25 * array( 'content' => __( 'Progress', 'tutor' ) ),
26 * array( 'content' => __( 'Score', 'tutor' ) ),
27 * );
28 *
29 * $contents = array(
30 * array(
31 * 'columns' => array(
32 * array( 'content' => 'Jane Doe' ),
33 * array( 'content' => '80%' ),
34 * array( 'content' => '92/100' ),
35 * ),
36 * ),
37 * array(
38 * 'columns' => array(
39 * array( 'content' => 'John Smith' ),
40 * array( 'content' => '55%' ),
41 * array( 'content' => '61/100' ),
42 * ),
43 * ),
44 * );
45 *
46 * Table::make()
47 * ->headings( $headings )
48 * ->contents( $contents )
49 * ->render();
50 *
51 * // Table with rich HTML inside cells and column borders
52 * $headings = array(
53 * array( 'content' => __( 'Quiz Info', 'tutor' ) ),
54 * array( 'content' => __( 'Marks', 'tutor' ) ),
55 * );
56 *
57 * $contents = array(
58 * array(
59 * 'columns' => array(
60 * array(
61 * 'content' =>
62 * '<div class="tutor-flex tutor-gap-3 tutor-items-center">' .
63 * SvgIcon::make()->name( Icon::QUESTION_CIRCLE )->size( 16 )->get() .
64 * esc_html__( 'Total Questions', 'tutor' ) .
65 * '</div>',
66 * ),
67 * array( 'content' => 20 ),
68 * ),
69 * ),
70 * array(
71 * 'columns' => array(
72 * array(
73 * 'content' =>
74 * '<div class="tutor-flex tutor-gap-3 tutor-items-center">' .
75 * SvgIcon::make()->name( Icon::STAR_FILL )->size( 16 )->get() .
76 * esc_html__( 'Pass Mark', 'tutor' ) .
77 * '</div>',
78 * ),
79 * array( 'content' => 14 ),
80 * ),
81 * ),
82 * );
83 *
84 * Table::make()
85 * ->headings( $headings )
86 * ->contents( $contents )
87 * ->attrs( array( 'class' => 'tutor-table-column-borders tutor-mb-6' ) )
88 * ->render();
89 *
90 * // Table with a custom class on a specific heading column
91 * $headings = array(
92 * array( 'content' => 'Name', 'class' => 'tutor-text-left' ),
93 * array( 'content' => 'Actions', 'class' => 'tutor-text-right' ),
94 * );
95 *
96 * Table::make()
97 * ->headings( $headings )
98 * ->contents( $contents )
99 * ->render();
100 *
101 * // Retrieve HTML without echoing
102 * $html = Table::make()->headings( $headings )->contents( $contents )->get();
103 * ```
104 *
105 * @since 4.0.0
106 */
107 class Table extends BaseComponent {
108
109 /**
110 * Table header content array.
111 *
112 * @var array
113 */
114 protected $cell_headers;
115
116 /**
117 * A 2D array of cell content.
118 *
119 * @var array
120 */
121 protected $cell_content;
122
123 /**
124 * Set table column headings.
125 *
126 * @since 4.0.0
127 *
128 * Headings must be provided in the following format.
129 *
130 * ```
131 * [
132 * [
133 * 'content' => '',
134 * ]
135 * ]
136 * ```
137 *
138 * @param array $headings the table cell headings.
139 *
140 * @return self
141 */
142 public function headings( array $headings ): self {
143 $this->cell_headers = $headings;
144 return $this;
145 }
146
147 /**
148 * Set table cell contents.
149 *
150 * @since 4.0.0
151 *
152 * Content must be given in the following format
153 * ```
154 * [
155 * [
156 * 'columns' => [
157 * [
158 * 'content' => '',
159 * ]
160 * ],
161 * ]
162 * ]
163 * ```
164 * @param array $contents the cell contents.
165 *
166 * @return self
167 */
168 public function contents( array $contents ): self {
169 $this->cell_content = $contents;
170 return $this;
171 }
172
173 /**
174 * Render table heading.
175 *
176 * @since 4.0.0
177 *
178 * @return string HTML
179 */
180 protected function render_table_headings(): string {
181 $headings = '';
182 if ( ! tutor_utils()->count( $this->cell_headers ) ) {
183 return $headings;
184 }
185
186 foreach ( $this->cell_headers as $heading ) {
187 $headings .= sprintf(
188 '<th class="%s">%s</th>',
189 $heading['class'] ?? '',
190 apply_filters( 'tutor_table_heading', $heading['content'] )
191 );
192 }
193
194 return $headings;
195 }
196
197 /**
198 * Render table body.
199 *
200 * @since 4.0.0
201 *
202 * @return string HTML
203 */
204 protected function render_table_body(): string {
205 $rows = '';
206
207 if ( ! count( $this->cell_content ) ) {
208 return $rows;
209 }
210
211 foreach ( $this->cell_content as $row ) {
212 $columns = '';
213 foreach ( $row['columns'] as $column ) {
214 $columns .= sprintf(
215 '<td>%s</td>',
216 apply_filters( 'tutor_table_content', $column['content'] )
217 );
218 }
219
220 $rows .= sprintf( '<tr>%s</tr>', $columns );
221 }
222
223 return $rows;
224 }
225
226 /**
227 * Get the final Table HTML Content.
228 *
229 * @since 4.0.0
230 *
231 * @return string HTML
232 */
233 public function get(): string {
234 if ( isset( $this->attributes['class'] ) && ! empty( $this->attributes['class'] ) ) {
235 $this->attributes['class'] = 'tutor-table ' . $this->attributes['class'];
236 } else {
237 $this->attributes['class'] = 'tutor-table';
238 }
239
240 ob_start();
241 $this->render_attributes();
242 $attrs = ob_get_clean();
243 return sprintf(
244 '<table %s>
245 <thead>
246 %s
247 </thead>
248 <tbody>
249 %s
250 </tbody>
251 </table>
252 ',
253 $attrs,
254 $this->render_table_headings(),
255 $this->render_table_body()
256 );
257 }
258 }
259