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
AttachmentCard.php
217 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 | * AttachmentCard::make() |
| 22 | * ->file_name( 'lesson-plan.pdf' ) |
| 23 | * ->file_size( '1.2 MB' ) |
| 24 | * ->is_downloadable( true ) |
| 25 | * ->action_attr( '@click', 'downloadFile()' ) |
| 26 | * ->render(); |
| 27 | * |
| 28 | * @since 4.0.0 |
| 29 | */ |
| 30 | class AttachmentCard extends BaseComponent { |
| 31 | |
| 32 | /** |
| 33 | * File name |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $file_name = ''; |
| 38 | |
| 39 | /** |
| 40 | * File size |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $file_size = ''; |
| 45 | |
| 46 | /** |
| 47 | * Is downloadable |
| 48 | * |
| 49 | * @var bool |
| 50 | */ |
| 51 | protected $is_downloadable = false; |
| 52 | |
| 53 | /** |
| 54 | * Title attribute |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | protected $title_attr = array(); |
| 59 | |
| 60 | /** |
| 61 | * Meta attribute |
| 62 | * |
| 63 | * @var array |
| 64 | */ |
| 65 | protected $meta_attr = array(); |
| 66 | |
| 67 | /** |
| 68 | * Action attribute |
| 69 | * |
| 70 | * @var array |
| 71 | */ |
| 72 | protected $action_attr = array(); |
| 73 | |
| 74 | /** |
| 75 | * Set file name |
| 76 | * |
| 77 | * @param string $file_name file name. |
| 78 | * |
| 79 | * @return self |
| 80 | */ |
| 81 | public function file_name( string $file_name ): self { |
| 82 | $this->file_name = $file_name; |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set file size |
| 88 | * |
| 89 | * @param string $file_size file size. |
| 90 | * |
| 91 | * @return self |
| 92 | */ |
| 93 | public function file_size( string $file_size ): self { |
| 94 | $this->file_size = $file_size; |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set is downloadable |
| 100 | * |
| 101 | * @param bool $is_downloadable is downloadable. |
| 102 | * |
| 103 | * @return self |
| 104 | */ |
| 105 | public function is_downloadable( bool $is_downloadable ): self { |
| 106 | $this->is_downloadable = $is_downloadable; |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Set title attribute |
| 112 | * |
| 113 | * @param string $key Attribute name. |
| 114 | * @param string $value Attribute value. |
| 115 | * |
| 116 | * @return self |
| 117 | */ |
| 118 | public function title_attr( string $key, string $value ): self { |
| 119 | $this->title_attr[ $key ] = $value; |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set meta attribute |
| 125 | * |
| 126 | * @param string $key Attribute name. |
| 127 | * @param string $value Attribute value. |
| 128 | * |
| 129 | * @return self |
| 130 | */ |
| 131 | public function meta_attr( string $key, string $value ): self { |
| 132 | $this->meta_attr[ $key ] = $value; |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set action attribute |
| 138 | * |
| 139 | * @param string $key Attribute name. |
| 140 | * @param string $value Attribute value. |
| 141 | * |
| 142 | * @return self |
| 143 | */ |
| 144 | public function action_attr( string $key, string $value ): self { |
| 145 | $this->action_attr[ $key ] = $value; |
| 146 | return $this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get custom attributes string |
| 151 | * |
| 152 | * @param array $attributes attributes. |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | private function get_custom_attributes_string( array $attributes ): string { |
| 157 | $compiled = array(); |
| 158 | |
| 159 | foreach ( $attributes as $key => $value ) { |
| 160 | $compiled[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $value ) ); |
| 161 | } |
| 162 | |
| 163 | return implode( ' ', $compiled ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get component content |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | public function get(): string { |
| 172 | $file_name = $this->file_name; |
| 173 | $file_size = $this->file_size; |
| 174 | $is_downloadable = $this->is_downloadable; |
| 175 | $title_attr = $this->get_custom_attributes_string( $this->title_attr ); |
| 176 | $meta_attr = $this->get_custom_attributes_string( $this->meta_attr ); |
| 177 | $action_attr = $this->get_custom_attributes_string( $this->action_attr ); |
| 178 | |
| 179 | if ( '' === $file_name && '' === $title_attr ) { |
| 180 | return ''; |
| 181 | } |
| 182 | |
| 183 | $icon_classes = array( 'tutor-attachment-card-icon' ); |
| 184 | $icon_class_attr = implode( ' ', $icon_classes ); |
| 185 | |
| 186 | $action_icon = $is_downloadable ? Icon::DOWNLOAD_2 : Icon::CROSS; |
| 187 | |
| 188 | ob_start(); |
| 189 | ?> |
| 190 | <div class="tutor-card tutor-attachment-card"> |
| 191 | <div class="<?php echo esc_attr( $icon_class_attr ); ?>" aria-hidden="true"> |
| 192 | <?php SvgIcon::make()->name( Icon::RESOURCES )->size( 24 )->render(); ?> |
| 193 | </div> |
| 194 | |
| 195 | <div class="tutor-attachment-card-body"> |
| 196 | <div class="tutor-attachment-card-title" <?php echo $title_attr; // phpcs:ignore --already-escaped WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 197 | <?php echo esc_html( $file_name ); ?> |
| 198 | </div> |
| 199 | |
| 200 | <?php if ( $file_size || $meta_attr ) : ?> |
| 201 | <span class="tutor-attachment-card-meta" <?php echo $meta_attr; // phpcs:ignore --already-escaped WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 202 | <?php echo esc_html( size_format( $file_size, 2 ) ); ?> |
| 203 | </span> |
| 204 | <?php endif; ?> |
| 205 | </div> |
| 206 | |
| 207 | <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 ?>> |
| 208 | <?php SvgIcon::make()->name( $action_icon )->size( 16 )->render(); ?> |
| 209 | </button> |
| 210 | </div> |
| 211 | <?php |
| 212 | |
| 213 | $this->component_string = ob_get_clean(); |
| 214 | return $this->component_string; |
| 215 | } |
| 216 | } |
| 217 |