Constants
1 day ago
Accordion.php
1 day ago
Alert.php
1 day ago
AttachmentCard.php
1 day ago
Avatar.php
1 day ago
Badge.php
1 day ago
BaseComponent.php
1 day ago
Button.php
1 day ago
ConfirmationModal.php
1 day ago
CourseFilter.php
1 day ago
DateFilter.php
1 day ago
DropdownFilter.php
1 day ago
EmptyState.php
1 day ago
FileUploader.php
1 day ago
InputField.php
1 day ago
Modal.php
1 day ago
Nav.php
1 day ago
Pagination.php
1 day ago
Popover.php
1 day ago
PreviewTrigger.php
1 day ago
Progress.php
1 day ago
SearchFilter.php
1 day ago
Sorting.php
1 day ago
StarRating.php
1 day ago
StarRatingInput.php
1 day ago
StatusSelect.php
1 day ago
SvgIcon.php
1 day ago
Table.php
1 day ago
Tabs.php
1 day ago
Tooltip.php
1 day ago
WPEditor.php
1 day ago
BaseComponent.php
209 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Tutor Component: BaseComponent |
| 4 | * |
| 5 | * Provides a base abstract class for all Tutor UI components. |
| 6 | * Handles attribute management and basic HTML escaping. |
| 7 | * |
| 8 | * @package Tutor\Components |
| 9 | * @author Themeum |
| 10 | * @link https://themeum.com |
| 11 | * @since 4.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Tutor\Components; |
| 15 | |
| 16 | use TUTOR\Input; |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; |
| 19 | |
| 20 | /** |
| 21 | * Abstract Base Component class. |
| 22 | * |
| 23 | * Defines shared behavior for all UI components. |
| 24 | * |
| 25 | * @since 4.0.0 |
| 26 | */ |
| 27 | abstract class BaseComponent { |
| 28 | |
| 29 | /** |
| 30 | * Keep the component as string |
| 31 | * |
| 32 | * @since 4.0.0 |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $component_string = ''; |
| 37 | |
| 38 | /** |
| 39 | * Create a new component instance. |
| 40 | * |
| 41 | * @since 4.0.0 |
| 42 | * |
| 43 | * @return static |
| 44 | */ |
| 45 | public static function make() { |
| 46 | return new static(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * HTML attributes for the component. |
| 51 | * |
| 52 | * Example: |
| 53 | * [ |
| 54 | * 'id' => 'tutor-button', |
| 55 | * 'class' => 'tutor-btn tutor-btn-primary' |
| 56 | * ] |
| 57 | * |
| 58 | * @since 4.0.0 |
| 59 | * |
| 60 | * @var array |
| 61 | */ |
| 62 | protected $attributes = array(); |
| 63 | |
| 64 | /** |
| 65 | * Set or merge multiple HTML attributes. |
| 66 | * |
| 67 | * This method allows components to attach |
| 68 | * dynamic attributes such as data-* or aria-*. |
| 69 | * |
| 70 | * @since 4.0.0 |
| 71 | * |
| 72 | * @param array $attrs Key–value pairs of HTML attributes. |
| 73 | * |
| 74 | * @return static |
| 75 | */ |
| 76 | public function attrs( array $attrs ) { |
| 77 | $this->attributes = array_merge( $this->attributes, $attrs ); |
| 78 | return $this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set a custom HTML attribute. |
| 83 | * |
| 84 | * @since 4.0.0 |
| 85 | * |
| 86 | * @param string $key Attribute name. |
| 87 | * @param string $value Attribute value. |
| 88 | * |
| 89 | * @return static |
| 90 | */ |
| 91 | public function attr( $key, $value ) { |
| 92 | $this->attributes[ $key ] = $value; |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Compile the stored attributes into an HTML string. |
| 98 | * |
| 99 | * Converts attributes array into a properly escaped |
| 100 | * string suitable for rendering in HTML tags. |
| 101 | * |
| 102 | * Example output: |
| 103 | * `id="tutor-btn" class="tutor-btn tutor-btn-primary"` |
| 104 | * |
| 105 | * @since 4.0.0 |
| 106 | * |
| 107 | * @return string Escaped and concatenated attributes. |
| 108 | */ |
| 109 | protected function get_attributes_string(): string { |
| 110 | $compiled = array(); |
| 111 | |
| 112 | foreach ( $this->attributes as $key => $value ) { |
| 113 | $compiled[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $value ) ); |
| 114 | } |
| 115 | |
| 116 | return implode( ' ', $compiled ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Render attributes |
| 121 | * |
| 122 | * @since 4.0.0 |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | protected function render_attributes(): void { |
| 127 | echo $this->get_attributes_string(); //phpcs:ignore -- Sanitization is performed inside get_attributes_string method. |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Escape a value for safe HTML output. |
| 132 | * |
| 133 | * Wrapper for WordPress `esc_html()` function. |
| 134 | * |
| 135 | * @since 4.0.0 |
| 136 | * |
| 137 | * @param mixed $value Value to escape. |
| 138 | * @param string $esc_fn Callable esc func. |
| 139 | * |
| 140 | * @return string Escaped string. |
| 141 | */ |
| 142 | protected function esc( $value, $esc_fn = 'esc_html' ): string { |
| 143 | return call_user_func( $esc_fn, $value ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Retrieve the list of allowed HTML tags and attributes. |
| 148 | * |
| 149 | * Provides a base set of safe HTML tags and merges additional |
| 150 | * SVG tags and any custom tags passed |
| 151 | * through the $extra_tags parameter. |
| 152 | * |
| 153 | * @since 4.0.0 |
| 154 | * |
| 155 | * @param array<string, array<string, bool>> $extra_tags Optional. |
| 156 | * Additional HTML tags and their allowed attributes |
| 157 | * in KSES-compatible format. |
| 158 | * |
| 159 | * @return array<string, array<string, bool>> Allowed HTML tags |
| 160 | * and attributes formatted for wp_kses(). |
| 161 | */ |
| 162 | protected function get_allowed_html_tags( $extra_tags = array() ) { |
| 163 | |
| 164 | $allowed_html_tags = array( |
| 165 | 'div' => array( |
| 166 | 'class' => true, |
| 167 | ), |
| 168 | 'span' => array( |
| 169 | 'class' => true, |
| 170 | ), |
| 171 | 'p' => array( |
| 172 | 'class' => true, |
| 173 | ), |
| 174 | 'b' => array(), |
| 175 | 'strong' => array(), |
| 176 | 'i' => array(), |
| 177 | ); |
| 178 | |
| 179 | $allowed_html_tags = wp_parse_args( Input::allow_svg( array() ), $allowed_html_tags ); |
| 180 | $allowed_html_tags = wp_parse_args( $extra_tags, $allowed_html_tags ); |
| 181 | |
| 182 | return $allowed_html_tags; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the component output as an HTML string. |
| 187 | * |
| 188 | * Note: Child classes must implement this method and are responsible |
| 189 | * for preparing and properly sanitizing the component’s HTML output. |
| 190 | * |
| 191 | * @since 4.0.0 |
| 192 | * |
| 193 | * @return string The component HTML output. |
| 194 | */ |
| 195 | abstract public function get(): string; |
| 196 | |
| 197 | /** |
| 198 | * Render the component |
| 199 | * |
| 200 | * @since 4.0.0 |
| 201 | * |
| 202 | * @return void |
| 203 | */ |
| 204 | public function render(): void { |
| 205 | // phpcs:ignore -- Sanitization is performed within each child class’s `get` method implementation. |
| 206 | echo $this->get(); |
| 207 | } |
| 208 | } |
| 209 |