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
AttachmentCard.php
243 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AttachmentCard 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 | use TUTOR\Icon; |
| 16 | |
| 17 | /** |
| 18 | * Class AttachmentCard |
| 19 | * |
| 20 | * Example Usage: |
| 21 | * ```php |
| 22 | * // Basic downloadable attachment |
| 23 | * AttachmentCard::make() |
| 24 | * ->file_name( 'lesson-plan.pdf' ) |
| 25 | * ->file_size( '1258291' ) // bytes — size_format() is applied internally |
| 26 | * ->is_downloadable( true ) |
| 27 | * ->action_attr( '@click', 'downloadFile()' ) |
| 28 | * ->render(); |
| 29 | * |
| 30 | * // Removable attachment (delete icon) |
| 31 | * AttachmentCard::make() |
| 32 | * ->file_name( 'assignment.docx' ) |
| 33 | * ->file_size( '524288' ) |
| 34 | * ->is_downloadable( false ) |
| 35 | * ->action_attr( '@click', 'removeFile(index)' ) |
| 36 | * ->render(); |
| 37 | * |
| 38 | * // With Alpine.js dynamic bindings on title and meta |
| 39 | * AttachmentCard::make() |
| 40 | * ->title_attr( 'x-text', 'file.name' ) |
| 41 | * ->meta_attr( 'x-text', 'formatBytes(file.size)' ) |
| 42 | * ->action_attr( '@click.stop', 'removeFile(index)' ) |
| 43 | * ->render(); |
| 44 | * |
| 45 | * // With extra attributes on the wrapper |
| 46 | * AttachmentCard::make() |
| 47 | * ->file_name( 'course-notes.pdf' ) |
| 48 | * ->file_size( '2097152' ) |
| 49 | * ->is_downloadable( true ) |
| 50 | * ->attr( 'data-id', '42' ) |
| 51 | * ->render(); |
| 52 | * ``` |
| 53 | * |
| 54 | * @since 4.0.0 |
| 55 | */ |
| 56 | class AttachmentCard extends BaseComponent { |
| 57 | |
| 58 | /** |
| 59 | * File name |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $file_name = ''; |
| 64 | |
| 65 | /** |
| 66 | * File size |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | protected $file_size = ''; |
| 71 | |
| 72 | /** |
| 73 | * Is downloadable |
| 74 | * |
| 75 | * @var bool |
| 76 | */ |
| 77 | protected $is_downloadable = false; |
| 78 | |
| 79 | /** |
| 80 | * Title attribute |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
| 84 | protected $title_attr = array(); |
| 85 | |
| 86 | /** |
| 87 | * Meta attribute |
| 88 | * |
| 89 | * @var array |
| 90 | */ |
| 91 | protected $meta_attr = array(); |
| 92 | |
| 93 | /** |
| 94 | * Action attribute |
| 95 | * |
| 96 | * @var array |
| 97 | */ |
| 98 | protected $action_attr = array(); |
| 99 | |
| 100 | /** |
| 101 | * Set file name |
| 102 | * |
| 103 | * @param string $file_name file name. |
| 104 | * |
| 105 | * @return self |
| 106 | */ |
| 107 | public function file_name( string $file_name ): self { |
| 108 | $this->file_name = $file_name; |
| 109 | return $this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set file size |
| 114 | * |
| 115 | * @param string $file_size file size. |
| 116 | * |
| 117 | * @return self |
| 118 | */ |
| 119 | public function file_size( string $file_size ): self { |
| 120 | $this->file_size = $file_size; |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set is downloadable |
| 126 | * |
| 127 | * @param bool $is_downloadable is downloadable. |
| 128 | * |
| 129 | * @return self |
| 130 | */ |
| 131 | public function is_downloadable( bool $is_downloadable ): self { |
| 132 | $this->is_downloadable = $is_downloadable; |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set title attribute |
| 138 | * |
| 139 | * @param string $key Attribute name. |
| 140 | * @param string $value Attribute value. |
| 141 | * |
| 142 | * @return self |
| 143 | */ |
| 144 | public function title_attr( string $key, string $value ): self { |
| 145 | $this->title_attr[ $key ] = $value; |
| 146 | return $this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Set meta attribute |
| 151 | * |
| 152 | * @param string $key Attribute name. |
| 153 | * @param string $value Attribute value. |
| 154 | * |
| 155 | * @return self |
| 156 | */ |
| 157 | public function meta_attr( string $key, string $value ): self { |
| 158 | $this->meta_attr[ $key ] = $value; |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set action attribute |
| 164 | * |
| 165 | * @param string $key Attribute name. |
| 166 | * @param string $value Attribute value. |
| 167 | * |
| 168 | * @return self |
| 169 | */ |
| 170 | public function action_attr( string $key, string $value ): self { |
| 171 | $this->action_attr[ $key ] = $value; |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get custom attributes string |
| 177 | * |
| 178 | * @param array $attributes attributes. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | private function get_custom_attributes_string( array $attributes ): string { |
| 183 | $compiled = array(); |
| 184 | |
| 185 | foreach ( $attributes as $key => $value ) { |
| 186 | $compiled[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $value ) ); |
| 187 | } |
| 188 | |
| 189 | return implode( ' ', $compiled ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get component content |
| 194 | * |
| 195 | * @return string |
| 196 | */ |
| 197 | public function get(): string { |
| 198 | $file_name = $this->file_name; |
| 199 | $file_size = $this->file_size; |
| 200 | $is_downloadable = $this->is_downloadable; |
| 201 | $title_attr = $this->get_custom_attributes_string( $this->title_attr ); |
| 202 | $meta_attr = $this->get_custom_attributes_string( $this->meta_attr ); |
| 203 | $action_attr = $this->get_custom_attributes_string( $this->action_attr ); |
| 204 | |
| 205 | if ( '' === $file_name && '' === $title_attr ) { |
| 206 | return ''; |
| 207 | } |
| 208 | |
| 209 | $icon_classes = array( 'tutor-attachment-card-icon' ); |
| 210 | $icon_class_attr = implode( ' ', $icon_classes ); |
| 211 | |
| 212 | $action_icon = $is_downloadable ? Icon::DOWNLOAD_2 : Icon::CROSS; |
| 213 | |
| 214 | ob_start(); |
| 215 | ?> |
| 216 | <div class="tutor-card tutor-attachment-card"> |
| 217 | <div class="<?php echo esc_attr( $icon_class_attr ); ?>" aria-hidden="true"> |
| 218 | <?php SvgIcon::make()->name( Icon::RESOURCES )->size( 24 )->render(); ?> |
| 219 | </div> |
| 220 | |
| 221 | <div class="tutor-attachment-card-body"> |
| 222 | <div class="tutor-attachment-card-title" <?php echo $title_attr; // phpcs:ignore --already-escaped WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 223 | <?php echo esc_html( $file_name ); ?> |
| 224 | </div> |
| 225 | |
| 226 | <?php if ( $file_size || $meta_attr ) : ?> |
| 227 | <span class="tutor-attachment-card-meta" <?php echo $meta_attr; // phpcs:ignore --already-escaped WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 228 | <?php echo esc_html( size_format( $file_size, 2 ) ); ?> |
| 229 | </span> |
| 230 | <?php endif; ?> |
| 231 | </div> |
| 232 | |
| 233 | <button type="button" class="tutor-btn tutor-btn-ghost tutor-btn-x-small tutor-btn-icon" aria-label="<?php echo $is_downloadable ? esc_attr__( 'Download file', 'tutor' ) : esc_attr__( 'Remove file', 'tutor' ); ?>" <?php echo $action_attr; // phpcs:ignore --already-escaped WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 234 | <?php SvgIcon::make()->name( $action_icon )->size( 16 )->render(); ?> |
| 235 | </button> |
| 236 | </div> |
| 237 | <?php |
| 238 | |
| 239 | $this->component_string = ob_get_clean(); |
| 240 | return $this->component_string; |
| 241 | } |
| 242 | } |
| 243 |