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.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 4 days ago Accordion.php 4 days ago Alert.php 4 days ago AttachmentCard.php 4 days ago Avatar.php 4 days ago Badge.php 4 days ago BaseComponent.php 4 days ago Button.php 4 days ago ConfirmationModal.php 4 days ago CourseFilter.php 4 days ago DateFilter.php 4 days ago DropdownFilter.php 4 days ago EmptyState.php 4 days ago FileUploader.php 4 days ago InputField.php 4 days ago Modal.php 4 days ago Nav.php 4 days ago Pagination.php 4 days ago Popover.php 4 days ago PreviewTrigger.php 4 days ago Progress.php 4 days ago SearchFilter.php 4 days ago Sorting.php 4 days ago StarRating.php 4 days ago StarRatingInput.php 4 days ago StatusSelect.php 4 days ago SvgIcon.php 4 days ago Table.php 4 days ago Tabs.php 4 days ago Tooltip.php 4 days ago WPEditor.php 4 days ago
Table.php
230 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 * ```
22 * // Component Data Structure
23 * Table::make()
24 * ->headings(
25 * [
26 * [
27 * 'content' => '',
28 * ]
29 * ]
30 * )
31 * ->contents(
32 * [
33 * [
34 * 'columns' => [
35 * [
36 * 'content' => '',
37 * ]
38 * ],
39 * ]
40 * ]
41 * )
42 * ->attributes('')
43 * ->render();
44 * ```
45 *
46 *
47 * ```
48 * // Example Usage:
49 * $heading = array(
50 * array(
51 * 'content' => __( 'Quiz Info', 'tutor' ),
52 * ),
53 * array(
54 * 'content' => __( 'Marks', 'tutor' ),
55 * ),
56 * );
57 *
58 * $content = array(
59 * array(
60 * 'columns' => array(
61 * array(
62 * 'content' => '<div class="tutor-flex tutor-gap-3 tutor-items-center">' . SvgIcon::make()->name( Icon::QUESTION_CIRCLE )->render() . __( 'Questions', 'tutor' ) . '</div>',
63 * ),
64 * array( 'content' => 20 ),
65 * ),
66 * ),
67 * );
68 *
69 * echo Table::make()
70 * ->headings( $heading )
71 * ->contents( $content )
72 * ->attributes( 'tutor-table-wrapper tutor-table-column-borders tutor-mb-6' )
73 * ->render();
74 * ```
75 *
76 * @since 4.0.0
77 */
78 class Table extends BaseComponent {
79
80 /**
81 * Table header content array.
82 *
83 * @var array
84 */
85 protected $cell_headers;
86
87 /**
88 * A 2D array of cell content.
89 *
90 * @var array
91 */
92 protected $cell_content;
93
94 /**
95 * Set table column headings.
96 *
97 * @since 4.0.0
98 *
99 * Headings must be provided in the following format.
100 *
101 * ```
102 * [
103 * [
104 * 'content' => '',
105 * ]
106 * ]
107 * ```
108 *
109 * @param array $headings the table cell headings.
110 *
111 * @return self
112 */
113 public function headings( array $headings ): self {
114 $this->cell_headers = $headings;
115 return $this;
116 }
117
118 /**
119 * Set table cell contents.
120 *
121 * @since 4.0.0
122 *
123 * Content must be given in the following format
124 * ```
125 * [
126 * [
127 * 'columns' => [
128 * [
129 * 'content' => '',
130 * ]
131 * ],
132 * ]
133 * ]
134 * ```
135 * @param array $contents the cell contents.
136 *
137 * @return self
138 */
139 public function contents( array $contents ): self {
140 $this->cell_content = $contents;
141 return $this;
142 }
143
144 /**
145 * Render table heading.
146 *
147 * @since 4.0.0
148 *
149 * @return string HTML
150 */
151 protected function render_table_headings(): string {
152 $headings = '';
153 if ( ! tutor_utils()->count( $this->cell_headers ) ) {
154 return $headings;
155 }
156
157 foreach ( $this->cell_headers as $heading ) {
158 $headings .= sprintf(
159 '<th class="%s">%s</th>',
160 $heading['class'] ?? '',
161 apply_filters( 'tutor_table_heading', $heading['content'] )
162 );
163 }
164
165 return $headings;
166 }
167
168 /**
169 * Render table body.
170 *
171 * @since 4.0.0
172 *
173 * @return string HTML
174 */
175 protected function render_table_body(): string {
176 $rows = '';
177
178 if ( ! count( $this->cell_content ) ) {
179 return $rows;
180 }
181
182 foreach ( $this->cell_content as $row ) {
183 $columns = '';
184 foreach ( $row['columns'] as $column ) {
185 $columns .= sprintf(
186 '<td>%s</td>',
187 apply_filters( 'tutor_table_content', $column['content'] )
188 );
189 }
190
191 $rows .= sprintf( '<tr>%s</tr>', $columns );
192 }
193
194 return $rows;
195 }
196
197 /**
198 * Get the final Table HTML Content.
199 *
200 * @since 4.0.0
201 *
202 * @return string HTML
203 */
204 public function get(): string {
205 if ( isset( $this->attributes['class'] ) && ! empty( $this->attributes['class'] ) ) {
206 $this->attributes['class'] = 'tutor-table ' . $this->attributes['class'];
207 } else {
208 $this->attributes['class'] = 'tutor-table';
209 }
210
211 ob_start();
212 $this->render_attributes();
213 $attrs = ob_get_clean();
214 return sprintf(
215 '<table %s>
216 <thead>
217 %s
218 </thead>
219 <tbody>
220 %s
221 </tbody>
222 </table>
223 ',
224 $attrs,
225 $this->render_table_headings(),
226 $this->render_table_body()
227 );
228 }
229 }
230