Select
1 month ago
Arrays.php
1 month ago
Creatable.php
1 month ago
Date.php
1 month ago
File.php
1 month ago
Html.php
1 month ago
Icon.php
1 month ago
Image.php
1 month ago
Mbstring.php
1 month ago
Media.php
1 month ago
Menu.php
1 month ago
Network.php
1 month ago
Post.php
1 month ago
Strings.php
1 month ago
Taxonomy.php
1 month ago
Translations.php
1 month ago
User.php
1 month ago
UserRoles.php
1 month ago
Html.php
296 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper; |
| 4 | |
| 5 | use DOMDocument; |
| 6 | use DOMElement; |
| 7 | |
| 8 | class Html extends Creatable |
| 9 | { |
| 10 | |
| 11 | public function get_attribute_as_string(string $key, ?string $value = null): string |
| 12 | { |
| 13 | return Strings::create()->is_not_empty($value) |
| 14 | ? sprintf('%s="%s"', $key, esc_attr(trim($value))) |
| 15 | : $key; |
| 16 | } |
| 17 | |
| 18 | public function get_style_attributes_as_string(array $attributes): string |
| 19 | { |
| 20 | $style = ''; |
| 21 | |
| 22 | foreach ($attributes as $key => $value) { |
| 23 | $style .= $key . ':' . $value . '; '; |
| 24 | } |
| 25 | |
| 26 | return $style; |
| 27 | } |
| 28 | |
| 29 | public function link(string $url, ?string $label = null, array $attributes = []): string |
| 30 | { |
| 31 | if ( ! $url) { |
| 32 | return $label; |
| 33 | } |
| 34 | |
| 35 | if (null === $label) { |
| 36 | $label = urldecode($url); |
| 37 | } |
| 38 | |
| 39 | if ( ! $label) { |
| 40 | return ''; |
| 41 | } |
| 42 | |
| 43 | if ( ! $this->contains_html($label)) { |
| 44 | $label = esc_html($label); |
| 45 | } |
| 46 | |
| 47 | if (array_key_exists('tooltip', $attributes)) { |
| 48 | $attributes['data-ac-tip'] = $attributes['tooltip']; |
| 49 | |
| 50 | unset($attributes['tooltip']); |
| 51 | } |
| 52 | |
| 53 | $allowed = wp_allowed_protocols(); |
| 54 | $allowed[] = 'skype'; |
| 55 | $allowed[] = 'call'; |
| 56 | |
| 57 | return sprintf( |
| 58 | '<a href="%s" %s>%s</a>', |
| 59 | esc_url($url, $allowed), |
| 60 | $this->get_attributes($attributes), |
| 61 | $label |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | public function divider(): string |
| 66 | { |
| 67 | return '<span class="ac-divider"></span>'; |
| 68 | } |
| 69 | |
| 70 | public function get_tooltip_attr(string $content): string |
| 71 | { |
| 72 | if ( ! $content) { |
| 73 | return ''; |
| 74 | } |
| 75 | |
| 76 | return 'data-ac-tip="' . esc_attr($content) . '"'; |
| 77 | } |
| 78 | |
| 79 | public function tooltip(string $label, string $tooltip, array $attributes = []): string |
| 80 | { |
| 81 | if (Strings::create()->is_not_empty($label) && $tooltip) { |
| 82 | $label = sprintf( |
| 83 | '<span %s %s>%s</span>', |
| 84 | $this->get_tooltip_attr($tooltip), |
| 85 | $this->get_attributes($attributes), |
| 86 | $label |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return $label; |
| 91 | } |
| 92 | |
| 93 | public function codearea(string $string, int $max_chars = 1000): string |
| 94 | { |
| 95 | if ( ! $string) { |
| 96 | return ''; |
| 97 | } |
| 98 | |
| 99 | $contents = substr( |
| 100 | $string, |
| 101 | 0, |
| 102 | $max_chars |
| 103 | ); |
| 104 | |
| 105 | return '<textarea style="color: #808080; width: 100%; min-height: 60px;" readonly>' . esc_textarea($contents) . '</textarea>'; |
| 106 | } |
| 107 | |
| 108 | private function get_attributes(array $attributes): string |
| 109 | { |
| 110 | $_attributes = []; |
| 111 | |
| 112 | foreach ($attributes as $attribute => $value) { |
| 113 | if ( |
| 114 | in_array($attribute, ['title', 'id', 'class', 'style', 'target', 'rel', 'download']) || |
| 115 | 'data-' === substr($attribute, 0, 5)) { |
| 116 | $_attributes[] = $this->get_attribute_as_string($attribute, (string)$value); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return ' ' . implode(' ', $_attributes); |
| 121 | } |
| 122 | |
| 123 | public function get_links(string $string): ?array |
| 124 | { |
| 125 | if ( ! class_exists('DOMDocument')) { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | // Just do a very simple check to check for possible links |
| 130 | if (false === strpos($string, '<a')) { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | $hrefs = []; |
| 135 | |
| 136 | $dom = new DOMDocument(); |
| 137 | |
| 138 | libxml_use_internal_errors(true); |
| 139 | $dom->loadHTML($string); |
| 140 | libxml_clear_errors(); |
| 141 | |
| 142 | $links = $dom->getElementsByTagName('a'); |
| 143 | |
| 144 | // TODO check for DOMNodeList object |
| 145 | |
| 146 | foreach ($links as $link) { |
| 147 | /** |
| 148 | * @var DOMElement $link |
| 149 | */ |
| 150 | $href = $link->getAttribute('href'); |
| 151 | |
| 152 | if (0 === strpos($href, '#')) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | $hrefs[] = $href; |
| 157 | } |
| 158 | |
| 159 | return $hrefs; |
| 160 | } |
| 161 | |
| 162 | private function contains_html(string $string): bool |
| 163 | { |
| 164 | return $string && $string !== strip_tags($string); |
| 165 | } |
| 166 | |
| 167 | public function implode(array $array, bool $divider = true): string |
| 168 | { |
| 169 | // Remove empty values |
| 170 | $array = $this->remove_empty($array); |
| 171 | |
| 172 | if (true === $divider) { |
| 173 | $divider = $this->divider(); |
| 174 | } |
| 175 | |
| 176 | return implode($divider, $array); |
| 177 | } |
| 178 | |
| 179 | public function remove_empty(array $array): array |
| 180 | { |
| 181 | return array_filter($array, [Strings::create(), 'is_not_empty']); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Small HTML block with grey background and rounded corners |
| 186 | */ |
| 187 | public function small_block(array $items): string |
| 188 | { |
| 189 | $blocks = []; |
| 190 | |
| 191 | foreach ($items as $item) { |
| 192 | if ($item && is_scalar($item)) { |
| 193 | $blocks[] = sprintf('<span class="ac-small-block">%s</span>', $item); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return implode($blocks); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Return round HTML span |
| 202 | */ |
| 203 | public function rounded(string $string): string |
| 204 | { |
| 205 | return sprintf('<span class="ac-rounded">%s</span>', $string); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns star rating based on X start from $max count. Does support decimals. |
| 210 | */ |
| 211 | public function stars(int $count, int $max = 0): string |
| 212 | { |
| 213 | $stars = [ |
| 214 | 'filled' => floor($count), |
| 215 | 'half' => floor(round(($count * 2)) - (floor($count) * 2)) ? 1 : 0, |
| 216 | 'empty' => 0, |
| 217 | ]; |
| 218 | |
| 219 | $max = absint($max); |
| 220 | |
| 221 | if ($max > 0) { |
| 222 | $star_count = $stars['filled'] + $stars['half']; |
| 223 | |
| 224 | $stars['empty'] = $max - $star_count; |
| 225 | |
| 226 | if ($star_count > $max) { |
| 227 | $stars['filled'] = $max; |
| 228 | $stars['half'] = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | $icons = []; |
| 233 | |
| 234 | foreach ($stars as $type => $_count) { |
| 235 | for ($i = 1; $i <= $_count; $i++) { |
| 236 | $icons[] = Icon::create()->dashicon(['icon' => 'star-' . $type, 'class' => 'ac-value-star']); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | ob_start(); |
| 241 | ?> |
| 242 | <span class="ac-value-stars"> |
| 243 | <?php |
| 244 | echo implode(' ', $icons); ?> |
| 245 | </span> |
| 246 | <?php |
| 247 | return ob_get_clean(); |
| 248 | } |
| 249 | |
| 250 | public function images(string $html, ?int $removed = null): string |
| 251 | { |
| 252 | if ( ! $html) { |
| 253 | return ''; |
| 254 | } |
| 255 | |
| 256 | if ($removed) { |
| 257 | $html .= $this->rounded('+' . $removed); |
| 258 | } |
| 259 | |
| 260 | return '<div class="ac-image-container">' . $html . '</div>'; |
| 261 | } |
| 262 | |
| 263 | public function file_pill(string $extension, string $filename): string |
| 264 | { |
| 265 | return sprintf( |
| 266 | '<span class="ac-file-type">%s</span><span class="ac-file-name">%s</span>', |
| 267 | esc_html(strtoupper($extension)), |
| 268 | esc_html($filename) |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @depecated 7.0.9 |
| 274 | */ |
| 275 | public function get_internal_external_links(): ?array |
| 276 | { |
| 277 | _deprecated_function(__METHOD__, '7.0.9'); |
| 278 | |
| 279 | return []; |
| 280 | } |
| 281 | |
| 282 | public function strip_attributes(string $html): string |
| 283 | { |
| 284 | _deprecated_function(__METHOD__, '7.0.9'); |
| 285 | |
| 286 | return $html; |
| 287 | } |
| 288 | |
| 289 | public function more(array $array, int $limit = 10, string $glue = ', '): string |
| 290 | { |
| 291 | _deprecated_function(__METHOD__, '7.0.11'); |
| 292 | |
| 293 | return implode($glue, $array); |
| 294 | } |
| 295 | |
| 296 | } |