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.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 / BaseComponent.php
tutor / components Last commit date
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