Constants
20 hours ago
Accordion.php
20 hours ago
Alert.php
20 hours ago
AttachmentCard.php
20 hours ago
Avatar.php
20 hours ago
Badge.php
20 hours ago
BaseComponent.php
20 hours ago
Button.php
20 hours ago
ConfirmationModal.php
20 hours ago
CourseFilter.php
20 hours ago
DateFilter.php
20 hours ago
DropdownFilter.php
20 hours ago
EmptyState.php
20 hours ago
FileUploader.php
20 hours ago
InputField.php
20 hours ago
Modal.php
20 hours ago
Nav.php
20 hours ago
Pagination.php
20 hours ago
Popover.php
20 hours ago
PreviewTrigger.php
20 hours ago
Progress.php
20 hours ago
SearchFilter.php
20 hours ago
Sorting.php
20 hours ago
StarRating.php
20 hours ago
StarRatingInput.php
20 hours ago
StatusSelect.php
20 hours ago
SvgIcon.php
20 hours ago
Table.php
20 hours ago
Tabs.php
20 hours ago
Tooltip.php
20 hours ago
WPEditor.php
20 hours 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 |